]> git.tdb.fi Git - libs/gltk.git/commitdiff
Allow Root widgets to be created with custom input devices
authorMikko Rasa <tdb@tdb.fi>
Fri, 7 Dec 2012 18:59:43 +0000 (20:59 +0200)
committerMikko Rasa <tdb@tdb.fi>
Fri, 7 Dec 2012 18:59:43 +0000 (20:59 +0200)
source/root.cpp
source/root.h

index e7c0a9251eadebfe8863e3549e4571737c0b24aa..89db305ea5bd291e7cfc9823c7a5a2964a837ec6 100644 (file)
@@ -9,24 +9,48 @@
 namespace Msp {
 namespace GLtk {
 
-Root::Root(const Resources &r, Graphics::Window &w):
+Root::Root(const Resources &r, Graphics::Window &window):
        resources(r),
-       window(w),
-       keyboard(window),
-       mouse(window),
-       lbl_tooltip(0),
-       tooltip_target(0)
+       keyboard(new Input::Keyboard(window)),
+       mouse(new Input::Mouse(window)),
+       own_input(true)
 {
        set_geometry(Geometry(0, 0, window.get_width(), window.get_height()));
 
+       init();
+}
+
+Root::Root(const Resources &r, Input::Keyboard *k, Input::Mouse *m):
+       resources(r),
+       keyboard(k),
+       mouse(m),
+       own_input(false)
+{
+       init();
+}
+
+void Root::init()
+{
+       lbl_tooltip = 0;
+       tooltip_target = 0;
+
        update_style();
 
-       mouse.signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
-       mouse.signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
-       mouse.signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
-       keyboard.signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
-       keyboard.signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
-       keyboard.signal_character.connect(sigc::mem_fun(this, &Root::character_event));
+       mouse->signal_button_press.connect(sigc::mem_fun(this, &Root::button_press_event));
+       mouse->signal_button_release.connect(sigc::mem_fun(this, &Root::button_release_event));
+       mouse->signal_axis_motion.connect(sigc::mem_fun(this, &Root::axis_motion_event));
+       keyboard->signal_button_press.connect(sigc::mem_fun(this, &Root::key_press_event));
+       keyboard->signal_button_release.connect(sigc::mem_fun(this, &Root::key_release_event));
+       keyboard->signal_character.connect(sigc::mem_fun(this, &Root::character_event));
+}
+
+Root::~Root()
+{
+       if(own_input)
+       {
+               delete keyboard;
+               delete mouse;
+       }
 }
 
 void Root::tick()
@@ -149,8 +173,8 @@ void Root::character_event(StringCodec::unichar ch)
 
 void Root::get_pointer(int &x, int &y)
 {
-       x = (mouse.get_axis_value(0)*0.5+0.5)*geom.w;
-       y = (mouse.get_axis_value(1)*0.5+0.5)*geom.h;
+       x = (mouse->get_axis_value(0)*0.5+0.5)*geom.w;
+       y = (mouse->get_axis_value(1)*0.5+0.5)*geom.h;
 }
 
 } // namespace GLtk
index 53f8ff3d74c37aa81a59478d37371719ad0e85c9..4e5309fe888f23234ca4d1d976f17fb0b596a742 100644 (file)
@@ -14,10 +14,8 @@ namespace GLtk {
 class Label;
 
 /**
-A Root is a special type of Panel that covers and entire Window and accepts
-input from it.  When created, a Root widget will take its size from the window
-it is created for.  The size can be changed, but a Root should always be
-rendered to fill the window in order to get coordinates mapped correctly.
+A Root is a special type of Panel that covers the entire viewport and receives
+input from keyboard and mouse.
 */
 class Root: public Panel, public sigc::trackable
 {
@@ -26,9 +24,9 @@ public:
 
 private:
        const Resources &resources;
-       Graphics::Window &window;
-       Input::Keyboard keyboard;
-       Input::Mouse mouse;
+       Input::Keyboard *keyboard;
+       Input::Mouse *mouse;
+       bool own_input;
        Label *lbl_tooltip;
        int pointer_x;
        int pointer_y;
@@ -36,8 +34,16 @@ private:
        Widget *tooltip_target;
 
 public:
+       /** Creates a Root widget for a window.  The geometry is set to match the
+       window's size, and input devices are created automatically. */
        Root(const Resources &, Graphics::Window &);
 
+       Root(const Resources &, Input::Keyboard *, Input::Mouse *);
+private:
+       void init();
+public:
+       virtual ~Root();
+
        virtual const char *get_class() const { return "root"; }
 
        const Resources &get_resources() const { return resources; }