diff --git a/panda/src/pgui/Sources.pp b/panda/src/pgui/Sources.pp index 6d75d36114..b5aa4cd6a0 100644 --- a/panda/src/pgui/Sources.pp +++ b/panda/src/pgui/Sources.pp @@ -4,7 +4,7 @@ #begin lib_target #define TARGET pgui #define LOCAL_LIBS \ - grutil text tform graph linmath event putil gobj \ + audio grutil text tform graph linmath event putil gobj \ mathutil sgraph sgraphutil // #define COMBINED_SOURCES $[TARGET]_composite1.cxx $[TARGET]_composite2.cxx diff --git a/panda/src/pgui/pgButton.cxx b/panda/src/pgui/pgButton.cxx index 7ab06c13c1..ab892377c1 100644 --- a/panda/src/pgui/pgButton.cxx +++ b/panda/src/pgui/pgButton.cxx @@ -165,8 +165,9 @@ release(const MouseWatcherParameter ¶m) { void PGButton:: click(const MouseWatcherParameter ¶m) { 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)); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/pgui/pgEntry.I b/panda/src/pgui/pgEntry.I index 4b98083576..766300339d 100644 --- a/panda/src/pgui/pgEntry.I +++ b/panda/src/pgui/pgEntry.I @@ -246,6 +246,6 @@ get_overflow_prefix() { // set_max_chars() or via set_max_width(). //////////////////////////////////////////////////////////////////// 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(); } diff --git a/panda/src/pgui/pgEntry.cxx b/panda/src/pgui/pgEntry.cxx index 8548d05fd8..5dc6d45912 100644 --- a/panda/src/pgui/pgEntry.cxx +++ b/panda/src/pgui/pgEntry.cxx @@ -306,8 +306,9 @@ press(const MouseWatcherParameter ¶m) { void PGEntry:: accept(const MouseWatcherParameter ¶m) { 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); } @@ -322,8 +323,9 @@ accept(const MouseWatcherParameter ¶m) { void PGEntry:: overflow(const MouseWatcherParameter ¶m) { 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)); } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/pgui/pgEntry.h b/panda/src/pgui/pgEntry.h index 65975a0f1a..5925096595 100644 --- a/panda/src/pgui/pgEntry.h +++ b/panda/src/pgui/pgEntry.h @@ -90,7 +90,7 @@ PUBLISHED: INLINE static string get_accept_prefix(); INLINE string get_accept_event(const ButtonHandle &button) const; INLINE static string get_overflow_prefix(); - INLINE string get_overflow_event(const ButtonHandle &button) const; + INLINE string get_overflow_event() const; private: void slot_text_def(int state); diff --git a/panda/src/pgui/pgItem.cxx b/panda/src/pgui/pgItem.cxx index e2622d6872..877c75c2f7 100644 --- a/panda/src/pgui/pgItem.cxx +++ b/panda/src/pgui/pgItem.cxx @@ -29,6 +29,10 @@ #include "directRenderTraverser.h" #include "allTransitionsWrapper.h" +#ifdef HAVE_AUDIO +#include "audioSound.h" +#endif + TypeHandle PGItem::_type_handle; PT(TextNode) PGItem::_text_node; PGItem *PGItem::_focus_item = (PGItem *)NULL; @@ -216,8 +220,9 @@ draw_item(PGTop *top, GraphicsStateGuardian *gsg, void PGItem:: enter(const MouseWatcherParameter ¶m) { 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 ¶m) { void PGItem:: exit(const MouseWatcherParameter ¶m) { 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 ¶m) { //////////////////////////////////////////////////////////////////// void PGItem:: 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:: 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:: press(const MouseWatcherParameter ¶m) { 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 ¶m) { void PGItem:: release(const MouseWatcherParameter ¶m) { 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; } +#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 // Access: Published, Static @@ -504,6 +566,24 @@ get_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 // Access: Protected, Static diff --git a/panda/src/pgui/pgItem.h b/panda/src/pgui/pgItem.h index 99f79ddce4..8e80d2a81b 100644 --- a/panda/src/pgui/pgItem.h +++ b/panda/src/pgui/pgItem.h @@ -32,12 +32,15 @@ #include "pt_NodeRelation.h" #include "textNode.h" +#include "pmap.h" + class PGTop; class GraphicsStateGuardian; class AllAttributesWrapper; class AllTransitionsWrapper; class MouseWatcherParameter; class ArcChain; +class AudioSound; //////////////////////////////////////////////////////////////////// // Class : PGItem @@ -120,12 +123,20 @@ PUBLISHED: INLINE string get_press_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(); INLINE static void set_text_node(TextNode *node); INLINE static PGItem *get_focus_item(); protected: + void play_sound(const string &event); static void remove_all_children(Node *node); private: @@ -154,6 +165,11 @@ private: typedef pvector StateDefs; StateDefs _state_defs; +#ifdef HAVE_AUDIO + typedef pmap Sounds; + Sounds _sounds; +#endif + static PT(TextNode) _text_node; static PGItem *_focus_item;