add sound support

This commit is contained in:
David Rose 2001-07-12 16:24:17 +00:00
parent 208787c6a4
commit 75133e5e10
7 changed files with 119 additions and 20 deletions

View File

@ -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

View File

@ -165,8 +165,9 @@ release(const MouseWatcherParameter &param) {
void PGButton::
click(const MouseWatcherParameter &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));
}
////////////////////////////////////////////////////////////////////

View File

@ -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();
}

View File

@ -306,8 +306,9 @@ press(const MouseWatcherParameter &param) {
void PGEntry::
accept(const MouseWatcherParameter &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);
}
@ -322,8 +323,9 @@ accept(const MouseWatcherParameter &param) {
void PGEntry::
overflow(const MouseWatcherParameter &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));
}
////////////////////////////////////////////////////////////////////

View File

@ -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);

View File

@ -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 &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::
exit(const MouseWatcherParameter &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::
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 &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::
release(const MouseWatcherParameter &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;
}
#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

View File

@ -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<StateDef> StateDefs;
StateDefs _state_defs;
#ifdef HAVE_AUDIO
typedef pmap<string, PT(AudioSound) > Sounds;
Sounds _sounds;
#endif
static PT(TextNode) _text_node;
static PGItem *_focus_item;