Skip to content

Instantly share code, notes, and snippets.

@jmglov
Created June 27, 2012 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmglov/3005315 to your computer and use it in GitHub Desktop.
Save jmglov/3005315 to your computer and use it in GitHub Desktop.
add audio feedback to button clicks
diff --git a/src/gui/CMakeLists.txt b/src/gui/CMakeLists.txt
index e391302..0f6aea2 100644
--- a/src/gui/CMakeLists.txt
+++ b/src/gui/CMakeLists.txt
@@ -55,6 +55,19 @@ target_link_libraries(adonthell_gui
adonthell_rpg
)
+################################
+# Unit tests
+IF(DEVBUILD)
+ add_executable(test_ui_event_manager test_ui_event_manager.cc)
+ set_target_properties(test_ui_event_manager PROPERTIES COMPILE_FLAGS ${LIBFT_CFLAGS})
+ target_link_libraries(test_ui_event_manager ${TEST_LIBRARIES} ${LIBFT_LIBRARIES} adonthell_gui)
+ add_test(NAME UiEventManager COMMAND test_ui_event_manager)
+
+ add_executable(test_ui_event test_ui_event.cc)
+ set_target_properties(test_ui_event PROPERTIES COMPILE_FLAGS ${LIBFT_CFLAGS})
+ target_link_libraries(test_ui_event ${TEST_LIBRARIES} adonthell_gui)
+ add_test(NAME UiEvent COMMAND test_ui_event)
+ENDIF(DEVBUILD)
#############################################
# Install Stuff
diff --git a/src/gui/Makefile.am b/src/gui/Makefile.am
index 352df4b..c6515eb 100644
--- a/src/gui/Makefile.am
+++ b/src/gui/Makefile.am
@@ -54,3 +54,16 @@ libadonthell_gui_la_LIBADD = $(PY_LIBS) \
$(top_builddir)/src/python/libadonthell_python.la \
$(top_builddir)/src/rpg/libadonthell_rpg.la \
-lstdc++ $(FT2_LIBS)
+
+## Unit tests
+test_CXXFLAGS = $(libgmock_CFLAGS) $(libgtest_CFLAGS)
+test_LDADD = $(libgmock_LIBS) $(libgtest_LIBS) $(top_builddir)/src/audio/libadonthell_gui.la
+
+test_ui_event_manager_SOURCES = test_ui_event_manager.cc
+test_ui_event_manager_CXXFLAGS = $(libadonthell_gui_la_CXXFLAGS) $(test_CXXFLAGS)
+test_ui_event_manager_LDADD = $(libadonthell_gui_la_LIBADD) $(test_LDADD)
+
+TESTS = \
+ test_ui_event_manager
+
+check_PROGRAMS = $(TESTS)
diff --git a/src/gui/test_ui_event.cc b/src/gui/test_ui_event.cc
new file mode 100644
index 0000000..d22c6e1
--- /dev/null
+++ b/src/gui/test_ui_event.cc
@@ -0,0 +1,69 @@
+/*
+ Copyright (C) 2010 Josh Glover <jmglov@jmglov.net>
+ Part of the Adonthell Project http://adonthell.linuxgames.com
+
+ Adonthell is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Adonthell is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Adonthell; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/**
+ * @file test/audio/audio_event.cc
+ * @author Josh Glover <jmglov@jmglov.net>
+ *
+ * @brief Unit tests for the audio_event class.
+ *
+ *
+ */
+
+
+#include "event/manager.h"
+#include "gui/button.h"
+#include "gui/ui_event.h"
+
+#include <gtest/gtest.h>
+
+namespace gui
+{
+ class ui_event_Test : public ::testing::Test {
+
+ protected:
+ // If the constructor and destructor are not enough for setting up
+ // and cleaning up each test, you can define the following methods:
+
+ virtual void SetUp() {
+ // Code here will be called immediately after the constructor (right
+ // before each test).
+ }
+
+ virtual void TearDown() {
+ // Code here will be called immediately after each test (right
+ // before the destructor).
+ }
+ }; // class{}
+
+ TEST_F(ui_event_Test, event_from_all_sources_equals_any_event) {
+ ui_event* event1 = new ui_event( (widget*)ui_event::ANY_SOURCE, "activate", NULL);
+ ui_event* event2 = new ui_event( (widget*)0x2, "activate", NULL);
+
+ EXPECT_TRUE(event1->equals(event2));
+ }
+
+} // namespace{}
+
+
+int main(int argc, char **argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+
+ return RUN_ALL_TESTS();
+}
diff --git a/src/gui/test_ui_event_manager.cc b/src/gui/test_ui_event_manager.cc
new file mode 100644
index 0000000..261f2de
--- /dev/null
+++ b/src/gui/test_ui_event_manager.cc
@@ -0,0 +1,85 @@
+/*
+ Copyright (C) 2010 Josh Glover <jmglov@jmglov.net>
+ Part of the Adonthell Project http://adonthell.linuxgames.com
+
+ Adonthell is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 2 of the License, or
+ (at your option) any later version.
+
+ Adonthell is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with Adonthell; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+*/
+
+/**
+ * @file test/audio/audio_event.cc
+ * @author Josh Glover <jmglov@jmglov.net>
+ *
+ * @brief Unit tests for the audio_event class.
+ *
+ *
+ */
+
+
+#include "event/manager.h"
+#include "gui/button.h"
+#include "gui/ui_event.h"
+#include "gui/ui_event_manager.h"
+
+#include <gtest/gtest.h>
+
+namespace gui
+{
+ class ui_event_manager_Test : public ::testing::Test {
+
+ protected:
+ // If the constructor and destructor are not enough for setting up
+ // and cleaning up each test, you can define the following methods:
+
+ virtual void SetUp() {
+ // Code here will be called immediately after the constructor (right
+ // before each test).
+ }
+
+ virtual void TearDown() {
+ // Code here will be called immediately after each test (right
+ // before the destructor).
+ }
+
+ public:
+ void increment_int(const events::event *event) {
+ const ui_event *e = (const ui_event *) event;
+ (*((int *)e->user_data()))++;
+ }
+ }; // class{}
+
+ TEST_F(ui_event_manager_Test, listen_for_events_from_any_source) {
+ int test_value = 0;
+ ui_event* event = new ui_event( (widget*)ui_event::ANY_SOURCE, "activate", &test_value);
+ events::factory event_factory;
+
+ event_factory.register_event(event, ::base::make_functor(*this, &ui_event_manager_Test::increment_int));
+
+ gui::button* button = new gui::button(0, 0);
+ button->activate();
+
+ events::manager_base *manager = events::event_type::get_manager (((events::event *) event)->type ());
+ ((ui_event_manager *)manager)->update();
+
+ EXPECT_EQ(1, test_value);
+ }
+
+} // namespace{}
+
+
+int main(int argc, char **argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+
+ return RUN_ALL_TESTS();
+}
diff --git a/src/gui/ui_event.h b/src/gui/ui_event.h
index 0a656e9..4339d4d 100644
--- a/src/gui/ui_event.h
+++ b/src/gui/ui_event.h
@@ -43,6 +43,11 @@ namespace gui
public:
/**
+ * Source for listeners that want to listen to events of a given type from any source.
+ */
+ static const int ANY_SOURCE = 0x1; // definitely not a valid pointer
+
+ /**
* @name Initialization
*/
//@{
@@ -75,7 +80,7 @@ namespace gui
bool equals (const events::event* e) const
{
const ui_event *evt = (const ui_event *) e;
- return Source == evt->source () && Action == evt->action();
+ return (Source == evt->source () || (uint64_t)Source == ANY_SOURCE) && Action == evt->action();
}
#ifndef SWIG
diff --git a/src/world/test_placeable.cc b/src/world/test_placeable.cc
index f0100b3..fc37583 100644
--- a/src/world/test_placeable.cc
+++ b/src/world/test_placeable.cc
@@ -44,7 +44,7 @@ namespace world
class placeable_Test : public ::testing::Test {
protected:
- placeable_Test() : object(nowhere, "unique_hash") {
+ placeable_Test() : object(nowhere, "") {
}
virtual ~placeable_Test() {
diff --git a/test/data/audio/click.ogg b/test/data/audio/click.ogg
new file mode 100644
index 0000000..fa167cd
Binary files /dev/null and b/test/data/audio/click.ogg differ
diff --git a/test/data/audio/worldtest.ogg b/test/data/audio/worldtest.ogg
new file mode 100644
index 0000000..ebcb799
Binary files /dev/null and b/test/data/audio/worldtest.ogg differ
diff --git a/test/worldtest.cc b/test/worldtest.cc
index aee3dc9..bbd3960 100644
--- a/test/worldtest.cc
+++ b/test/worldtest.cc
@@ -36,6 +36,7 @@
#include "world/character.h"
#include "world/object.h"
#include "world/area_manager.h"
+#include "gui/ui_event.h"
#include "gui/window_manager.h"
#include "base/logging.h"
@@ -114,6 +115,12 @@ public:
// do not consume event
return false;
}
+
+ void play_sound(const events::event *event) {
+ const gui::ui_event *e = (const gui::ui_event *) event;
+ audio::sound *sound = (audio::sound *) e->user_data();
+ sound->play(0);
+ }
};
class world_test : public adonthell::app
@@ -180,7 +187,7 @@ public:
npc->set_specie ("Human");
LOG(INFO) << " done!";
- std::string sound_filename = "at-demo-2.ogg";
+ std::string sound_filename = "worldtest.ogg";
LOG(INFO) << "Loading sound file '" << sound_filename << "'";
audio::sound *sound = new audio::sound(sound_filename);
LOG(INFO) << " done!";
@@ -189,6 +196,15 @@ public:
sound->fadein(3, -1);
LOG(INFO) << " done!";
+ std::string button_sound_filename = "click.ogg";
+ LOG(INFO) << "Loading button sound file '" << button_sound_filename << "'";
+ audio::sound *button_sound = new audio::sound(button_sound_filename);
+ LOG(INFO) << " done!";
+
+ gui::ui_event *activate_event = new gui::ui_event((gui::widget*)gui::ui_event::ANY_SOURCE, "activate", button_sound);
+ events::factory event_factory;
+ event_factory.register_event(activate_event, ::base::make_functor(gc, &game_client::play_sound));
+
// arguments to map view schedule
LOG(INFO) << "Adding player character to mapview schedule... ";
PyObject *args = PyTuple_New (1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment