Ver Fonte

add sound support

David Rose há 24 anos atrás
pai
commit
75133e5e10

+ 1 - 1
panda/src/pgui/Sources.pp

@@ -4,7 +4,7 @@
 #begin lib_target
 #begin lib_target
   #define TARGET pgui
   #define TARGET pgui
   #define LOCAL_LIBS \
   #define LOCAL_LIBS \
-    grutil text tform graph linmath event putil gobj \
+    audio grutil text tform graph linmath event putil gobj \
     mathutil sgraph sgraphutil
     mathutil sgraph sgraphutil
 
 
 //  #define COMBINED_SOURCES $[TARGET]_composite1.cxx $[TARGET]_composite2.cxx 
 //  #define COMBINED_SOURCES $[TARGET]_composite1.cxx $[TARGET]_composite2.cxx 

+ 3 - 2
panda/src/pgui/pgButton.cxx

@@ -165,8 +165,9 @@ release(const MouseWatcherParameter &param) {
 void PGButton::
 void PGButton::
 click(const MouseWatcherParameter &param) {
 click(const MouseWatcherParameter &param) {
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
-  throw_event(get_click_event(param.get_button()), 
-              EventParameter(ep));
+  string event = get_click_event(param.get_button());
+  play_sound(event);
+  throw_event(event, EventParameter(ep));
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////

+ 2 - 2
panda/src/pgui/pgEntry.I

@@ -246,6 +246,6 @@ get_overflow_prefix() {
 //               set_max_chars() or via set_max_width().
 //               set_max_chars() or via set_max_width().
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 INLINE string PGEntry::
 INLINE string PGEntry::
-get_overflow_event(const ButtonHandle &button) const {
-  return "overflow-" + button.get_name() + "-" + get_id();
+get_overflow_event() const {
+  return "overflow-" + get_id();
 }
 }

+ 6 - 4
panda/src/pgui/pgEntry.cxx

@@ -306,8 +306,9 @@ press(const MouseWatcherParameter &param) {
 void PGEntry::
 void PGEntry::
 accept(const MouseWatcherParameter &param) {
 accept(const MouseWatcherParameter &param) {
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
-  throw_event(get_accept_event(param.get_button()), 
-              EventParameter(ep));
+  string event = get_accept_event(param.get_button());
+  play_sound(event);
+  throw_event(event, EventParameter(ep));
   set_focus(false);
   set_focus(false);
 }
 }
 
 
@@ -322,8 +323,9 @@ accept(const MouseWatcherParameter &param) {
 void PGEntry::
 void PGEntry::
 overflow(const MouseWatcherParameter &param) {
 overflow(const MouseWatcherParameter &param) {
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
-  throw_event(get_overflow_event(param.get_button()), 
-              EventParameter(ep));
+  string event = get_overflow_event();
+  play_sound(event);
+  throw_event(event, EventParameter(ep));
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////

+ 1 - 1
panda/src/pgui/pgEntry.h

@@ -90,7 +90,7 @@ PUBLISHED:
   INLINE static string get_accept_prefix();
   INLINE static string get_accept_prefix();
   INLINE string get_accept_event(const ButtonHandle &button) const;
   INLINE string get_accept_event(const ButtonHandle &button) const;
   INLINE static string get_overflow_prefix();
   INLINE static string get_overflow_prefix();
-  INLINE string get_overflow_event(const ButtonHandle &button) const;
+  INLINE string get_overflow_event() const;
 
 
 private:
 private:
   void slot_text_def(int state);
   void slot_text_def(int state);

+ 90 - 10
panda/src/pgui/pgItem.cxx

@@ -29,6 +29,10 @@
 #include "directRenderTraverser.h"
 #include "directRenderTraverser.h"
 #include "allTransitionsWrapper.h"
 #include "allTransitionsWrapper.h"
 
 
+#ifdef HAVE_AUDIO
+#include "audioSound.h"
+#endif
+
 TypeHandle PGItem::_type_handle;
 TypeHandle PGItem::_type_handle;
 PT(TextNode) PGItem::_text_node;
 PT(TextNode) PGItem::_text_node;
 PGItem *PGItem::_focus_item = (PGItem *)NULL;
 PGItem *PGItem::_focus_item = (PGItem *)NULL;
@@ -216,8 +220,9 @@ draw_item(PGTop *top, GraphicsStateGuardian *gsg,
 void PGItem::
 void PGItem::
 enter(const MouseWatcherParameter &param) {
 enter(const MouseWatcherParameter &param) {
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
-  throw_event(get_enter_event(),
-              EventParameter(ep));
+  string event = get_enter_event();
+  play_sound(event);
+  throw_event(event, EventParameter(ep));
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -229,8 +234,9 @@ enter(const MouseWatcherParameter &param) {
 void PGItem::
 void PGItem::
 exit(const MouseWatcherParameter &param) {
 exit(const MouseWatcherParameter &param) {
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
-  throw_event(get_exit_event(),
-              EventParameter(ep));
+  string event = get_exit_event();
+  play_sound(event);
+  throw_event(event, EventParameter(ep));
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -241,7 +247,9 @@ exit(const MouseWatcherParameter &param) {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void PGItem::
 void PGItem::
 focus_in() {
 focus_in() {
-  throw_event(get_focus_in_event());
+  string event = get_focus_in_event();
+  play_sound(event);
+  throw_event(event);
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -252,7 +260,9 @@ focus_in() {
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 void PGItem::
 void PGItem::
 focus_out() {
 focus_out() {
-  throw_event(get_focus_out_event());
+  string event = get_focus_out_event();
+  play_sound(event);
+  throw_event(event);
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -265,8 +275,9 @@ focus_out() {
 void PGItem::
 void PGItem::
 press(const MouseWatcherParameter &param) {
 press(const MouseWatcherParameter &param) {
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
-  throw_event(get_press_event(param.get_button()), 
-              EventParameter(ep));
+  string event = get_press_event(param.get_button());
+  play_sound(event);
+  throw_event(event, EventParameter(ep));
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -279,8 +290,9 @@ press(const MouseWatcherParameter &param) {
 void PGItem::
 void PGItem::
 release(const MouseWatcherParameter &param) {
 release(const MouseWatcherParameter &param) {
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
   PGMouseWatcherParameter *ep = new PGMouseWatcherParameter(param);
-  throw_event(get_release_event(param.get_button()), 
-              EventParameter(ep));
+  string event = get_release_event(param.get_button());
+  play_sound(event);
+  throw_event(event, EventParameter(ep));
 }
 }
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
@@ -483,6 +495,56 @@ set_frame_style(int state, const PGFrameStyle &style) {
   _state_defs[state]._frame_stale = true;
   _state_defs[state]._frame_stale = true;
 }
 }
 
 
+#ifdef HAVE_AUDIO
+////////////////////////////////////////////////////////////////////
+//     Function: PGItem::set_sound
+//       Access: Published
+//  Description: Sets the sound that will be played whenever the
+//               indicated event occurs.
+////////////////////////////////////////////////////////////////////
+void PGItem::
+set_sound(const string &event, AudioSound *sound) {
+  _sounds[event] = sound;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PGItem::clear_sound
+//       Access: Published
+//  Description: Removes the sound associated with the indicated
+//               event.
+////////////////////////////////////////////////////////////////////
+void PGItem::
+clear_sound(const string &event) {
+  _sounds.erase(event);
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PGItem::get_sound
+//       Access: Published
+//  Description: Returns the sound associated with the indicated
+//               event, or NULL if there is no associated sound.
+////////////////////////////////////////////////////////////////////
+AudioSound *PGItem::
+get_sound(const string &event) const {
+  Sounds::const_iterator si = _sounds.find(event);
+  if (si != _sounds.end()) {
+    return (*si).second;
+  }
+  return (AudioSound *)NULL;
+}
+
+////////////////////////////////////////////////////////////////////
+//     Function: PGItem::has_sound
+//       Access: Published
+//  Description: Returns true if there is a sound associated with the
+//               indicated event, or false otherwise.
+////////////////////////////////////////////////////////////////////
+bool PGItem::
+has_sound(const string &event) const {
+  return (_sounds.count(event) != 0);
+}
+#endif  // HAVE_AUDIO
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PGItem::get_text_node
 //     Function: PGItem::get_text_node
 //       Access: Published, Static
 //       Access: Published, Static
@@ -504,6 +566,24 @@ get_text_node() {
   return _text_node;
   return _text_node;
 }
 }
 
 
+////////////////////////////////////////////////////////////////////
+//     Function: PGItem::play_sound
+//       Access: Protected
+//  Description: Plays the sound associated with the indicated event,
+//               if there is one.
+////////////////////////////////////////////////////////////////////
+void PGItem::
+play_sound(const string &event) {
+#ifdef HAVE_AUDIO
+  Sounds::const_iterator si = _sounds.find(event);
+  if (si != _sounds.end()) {
+    AudioSound *sound = (*si).second;
+    sound->play();
+  }
+  return (AudioSound *)NULL;
+#endif  // HAVE_AUDIO
+}
+
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //     Function: PGItem::remove_all_children
 //     Function: PGItem::remove_all_children
 //       Access: Protected, Static
 //       Access: Protected, Static

+ 16 - 0
panda/src/pgui/pgItem.h

@@ -32,12 +32,15 @@
 #include "pt_NodeRelation.h"
 #include "pt_NodeRelation.h"
 #include "textNode.h"
 #include "textNode.h"
 
 
+#include "pmap.h"
+
 class PGTop;
 class PGTop;
 class GraphicsStateGuardian;
 class GraphicsStateGuardian;
 class AllAttributesWrapper;
 class AllAttributesWrapper;
 class AllTransitionsWrapper;
 class AllTransitionsWrapper;
 class MouseWatcherParameter;
 class MouseWatcherParameter;
 class ArcChain;
 class ArcChain;
+class AudioSound;
 
 
 ////////////////////////////////////////////////////////////////////
 ////////////////////////////////////////////////////////////////////
 //       Class : PGItem
 //       Class : PGItem
@@ -120,12 +123,20 @@ PUBLISHED:
   INLINE string get_press_event(const ButtonHandle &button) const;
   INLINE string get_press_event(const ButtonHandle &button) const;
   INLINE string get_release_event(const ButtonHandle &button) const;
   INLINE string get_release_event(const ButtonHandle &button) const;
 
 
+#ifdef HAVE_AUDIO
+  void set_sound(const string &event, AudioSound *sound);
+  void clear_sound(const string &event);
+  AudioSound *get_sound(const string &event) const;
+  bool has_sound(const string &event) const;
+#endif
+
   static TextNode *get_text_node();
   static TextNode *get_text_node();
   INLINE static void set_text_node(TextNode *node);
   INLINE static void set_text_node(TextNode *node);
 
 
   INLINE static PGItem *get_focus_item();
   INLINE static PGItem *get_focus_item();
 
 
 protected:
 protected:
+  void play_sound(const string &event);
   static void remove_all_children(Node *node);
   static void remove_all_children(Node *node);
 
 
 private:
 private:
@@ -154,6 +165,11 @@ private:
   typedef pvector<StateDef> StateDefs;
   typedef pvector<StateDef> StateDefs;
   StateDefs _state_defs;
   StateDefs _state_defs;
 
 
+#ifdef HAVE_AUDIO
+  typedef pmap<string, PT(AudioSound) > Sounds;
+  Sounds _sounds;
+#endif
+
   static PT(TextNode) _text_node;
   static PT(TextNode) _text_node;
   static PGItem *_focus_item;
   static PGItem *_focus_item;