diff options
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/button.cc | 17 | ||||
-rw-r--r-- | src/ui/button.h | 12 | ||||
-rw-r--r-- | src/ui/console.cc | 36 | ||||
-rw-r--r-- | src/ui/console.h | 3 | ||||
-rw-r--r-- | src/ui/iconbutton.cc | 9 | ||||
-rw-r--r-- | src/ui/iconbutton.h | 13 | ||||
-rw-r--r-- | src/ui/listitem.cc | 4 | ||||
-rw-r--r-- | src/ui/listitem.h | 19 | ||||
-rw-r--r-- | src/ui/listview.cc | 31 | ||||
-rw-r--r-- | src/ui/listview.h | 4 | ||||
-rwxr-xr-x | src/ui/modelview.cc | 26 | ||||
-rwxr-xr-x | src/ui/modelview.h | 26 | ||||
-rw-r--r-- | src/ui/scrollbar.cc | 63 | ||||
-rw-r--r-- | src/ui/scrollbar.h | 26 | ||||
-rw-r--r-- | src/ui/scrollpane.cc | 23 | ||||
-rw-r--r-- | src/ui/scrollpane.h | 7 | ||||
-rw-r--r-- | src/ui/slider.cc | 66 | ||||
-rw-r--r-- | src/ui/slider.h | 43 | ||||
-rw-r--r-- | src/ui/ui.cc | 72 | ||||
-rw-r--r-- | src/ui/ui.h | 8 | ||||
-rw-r--r-- | src/ui/widget.cc | 56 | ||||
-rw-r--r-- | src/ui/widget.h | 30 |
22 files changed, 382 insertions, 212 deletions
diff --git a/src/ui/button.cc b/src/ui/button.cc index 6c34a4f..d356db9 100644 --- a/src/ui/button.cc +++ b/src/ui/button.cc @@ -93,15 +93,14 @@ void Button::draw() // } } -bool Button::on_keypress(const int key, const unsigned int modifier) +bool Button::on_mousepress(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) { if (enabled()) { if (button_command.size()) { core::cmd() << button_command << std::endl; } audio::play("ui/clicked"); - emit(EventButtonClicked); } return true; @@ -110,16 +109,4 @@ bool Button::on_keypress(const int key, const unsigned int modifier) return false; } -bool Button::on_keyrelease(const int key, const unsigned int modifier) -{ - return false; -} - -void Button::on_mouseover(const math::Vector2f &cursor) -{ - if (enabled()) { - //audio::play("ui/select"); - } -} - } diff --git a/src/ui/button.h b/src/ui/button.h index 7d264a9..15d5d05 100644 --- a/src/ui/button.h +++ b/src/ui/button.h @@ -33,14 +33,10 @@ public: /// print button description virtual void print(const size_t indent) const; - /// called when the mouse enters the widget - virtual void on_mouseover(const math::Vector2f &cursor); - - /// called when the widget receives a key press - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// called when the widget receives a key release - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); protected: /// draw the button background diff --git a/src/ui/console.cc b/src/ui/console.cc index 3799392..b683778 100644 --- a/src/ui/console.cc +++ b/src/ui/console.cc @@ -25,6 +25,8 @@ namespace ui const float DEFAULT_CONSOLE_HEIGHT = 0.7f; const size_t DEFAULT_MAX_HISTO_LINES = 512; +// number of lines to scroll +const size_t SCROLL_LINES = 3; // the global console buffer object ConsoleBuffer Console::con_buffer; @@ -87,8 +89,8 @@ void Console::show() { ui::Window::show(); raise(); - SDL_WM_GrabInput(SDL_GRAB_OFF); - SDL_ShowCursor(SDL_ENABLE); + + SDL_SetRelativeMouseMode(SDL_FALSE); console_scrollpane->set_scroll(0); console_scrollpane->set_offset(3); @@ -106,12 +108,10 @@ void Console::hide() ui::Window::hide(); core::Cvar *input_grab = core::Cvar::find("input_grab"); - if (!input_grab || input_grab->value()) { - SDL_WM_GrabInput(SDL_GRAB_ON); - SDL_ShowCursor(SDL_DISABLE); + if (input_grab && input_grab->value()) { + SDL_SetRelativeMouseMode(SDL_TRUE); } else { - SDL_WM_GrabInput(SDL_GRAB_OFF); - SDL_ShowCursor(SDL_ENABLE); + SDL_SetRelativeMouseMode(SDL_FALSE); } audio::play("ui/console"); @@ -139,9 +139,6 @@ bool Console::on_emit(Widget *sender, const Event event, void *data) bool Console::on_keypress(const int key, const unsigned int modifier) { - // number of lines to scroll - const size_t scroll_offset = 3; - ui::Text::reverse_iterator upit; switch (key) { @@ -191,14 +188,12 @@ bool Console::on_keypress(const int key, const unsigned int modifier) } return true; break; - case 512 + SDL_BUTTON_WHEELUP: case SDLK_PAGEUP: - console_scrollpane->inc_scroll(scroll_offset); + console_scrollpane->inc_scroll(SCROLL_LINES); return true; break; - case 512 + SDL_BUTTON_WHEELDOWN: case SDLK_PAGEDOWN: - console_scrollpane->dec_scroll(scroll_offset); + console_scrollpane->dec_scroll(SCROLL_LINES); return true; break; } @@ -206,6 +201,19 @@ bool Console::on_keypress(const int key, const unsigned int modifier) return false; } +bool Console::on_mousewheel(const math::Vector2f & direction) +{ + if (direction.y() > 0 ) + { + console_scrollpane->inc_scroll(SCROLL_LINES); + return true; + } else if (direction.y() < 0) { + console_scrollpane->dec_scroll(SCROLL_LINES); + return true; + } + return false; +} + void Console::draw() { console_scrollbar->set_range(0, (float) con_buffer.log().size()); diff --git a/src/ui/console.h b/src/ui/console.h index 93b6934..ea17e8b 100644 --- a/src/ui/console.h +++ b/src/ui/console.h @@ -63,6 +63,9 @@ protected: /// handle keypress events virtual bool on_keypress(const int key, const unsigned int modifier); + /// handle mousewheel events + virtual bool on_mousewheel(const math::Vector2f & direction); + /// handle emit events virtual bool on_emit(Widget *sender, const Event event, void *data); diff --git a/src/ui/iconbutton.cc b/src/ui/iconbutton.cc index b8dda87..944ae6c 100644 --- a/src/ui/iconbutton.cc +++ b/src/ui/iconbutton.cc @@ -105,9 +105,9 @@ void IconButton::draw_border() } } -bool IconButton::on_keypress(const int key, const unsigned int modifier) +bool IconButton::on_mousepress(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) { if (enabled()) { if (iconbutton_command.size()) { core::cmd() << iconbutton_command << std::endl; @@ -121,11 +121,6 @@ bool IconButton::on_keypress(const int key, const unsigned int modifier) return false; } -bool IconButton::on_keyrelease(const int key, const unsigned int modifier) -{ - return false; -} - void IconButton::on_mouseover(const math::Vector2f &cursor) { if (enabled()) { diff --git a/src/ui/iconbutton.h b/src/ui/iconbutton.h index c9a7c5b..a7aee39 100644 --- a/src/ui/iconbutton.h +++ b/src/ui/iconbutton.h @@ -51,14 +51,15 @@ public: /// print button description virtual void print(const size_t indent) const; - /// called when the mouse enters the widget + /** + * @brief mouse over event handler + * */ virtual void on_mouseover(const math::Vector2f &cursor); - /// called when the widget receives a key press - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// called when the widget receives a key release - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); protected: /// draw the button border diff --git a/src/ui/listitem.cc b/src/ui/listitem.cc index d7c3f1e..ad821ed 100644 --- a/src/ui/listitem.cc +++ b/src/ui/listitem.cc @@ -70,9 +70,9 @@ void ListItem::on_mouseover(const math::Vector2f &cursor) } } -bool ListItem::on_keypress(const int key, const unsigned int modifier) +bool ListItem::on_mousepress(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) { //audio::play("ui/button"); emit(EventListItemClicked); diff --git a/src/ui/listitem.h b/src/ui/listitem.h index a0f4d5e..c2ccf6f 100644 --- a/src/ui/listitem.h +++ b/src/ui/listitem.h @@ -65,14 +65,21 @@ public: void select(); protected: - /// keypress event handler - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// draw the button border + /** + * @brief mouseover event handler + * */ + virtual void on_mouseover(const math::Vector2f &cursor); + + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); + + /** + * @brief draw the button border + * */ virtual void draw_border(); - virtual void on_mouseover(const math::Vector2f &cursor); - virtual void draw(); private: diff --git a/src/ui/listview.cc b/src/ui/listview.cc index 0c01596..c72cd59 100644 --- a/src/ui/listview.cc +++ b/src/ui/listview.cc @@ -155,26 +155,21 @@ bool ListView::on_emit(Widget *sender, const Event event, void *data) return false; } -bool ListView::on_keypress(const int key, const unsigned int modifier) +bool ListView::on_mousewheel(const math::Vector2f & direction) { - switch (key) { - - case 512 + SDL_BUTTON_WHEELDOWN: - inc_scroll(1.0f); - resize(); - return true; - break; - - case 512 + SDL_BUTTON_WHEELUP: - dec_scroll(1.0f); - resize(); - return true; - break; - - default: - break; - } + if (direction.y() > 0 ) + { + dec_scroll(1.0f); + resize(); + return true; + } + else if (direction.y() < 0) + { + inc_scroll(1.0f); + resize(); + return true; + } return false; } diff --git a/src/ui/listview.h b/src/ui/listview.h index 7678b8b..616233d 100644 --- a/src/ui/listview.h +++ b/src/ui/listview.h @@ -79,9 +79,9 @@ protected: virtual bool on_emit(Widget *sender, const Event event, void *data); /** - * @brief keypress event handler + * @brief mouse hweel event handler * */ - virtual bool on_keypress(const int key, const unsigned int modifier); + virtual bool on_mousewheel(const math::Vector2f & direction); private: float listview_scroll; diff --git a/src/ui/modelview.cc b/src/ui/modelview.cc index 679c7b6..f1676b1 100755 --- a/src/ui/modelview.cc +++ b/src/ui/modelview.cc @@ -100,19 +100,29 @@ void ModelView::set_zoom(const float zoom) math::clamp(modelview_zoom, 1.0f, 5.0f); } -bool ModelView::on_keypress(const int key, const unsigned int modifier) +bool ModelView::on_mousewheel(const math::Vector2f & direction) { - if (key == 512 + SDL_BUTTON_WHEELUP) { + if (direction.y() > 0 ) + { modelview_zoom -= 0.25f; if (modelview_zoom < 1.0f) modelview_zoom = 1.0f; return true; - } else if (key == 512 + SDL_BUTTON_WHEELDOWN) { + } else if (direction.y() < 0 ) + { modelview_zoom += 0.25f; if (modelview_zoom > 5.0f) modelview_zoom = 5.0f; return true; - } else if (key == 512 + SDL_BUTTON_LEFT) { + } + + return false; +} + +bool ModelView::on_mousepress(const unsigned int button) +{ + if (button == SDL_BUTTON_LEFT) + { modelview_dragging = true; return true; } @@ -120,9 +130,10 @@ bool ModelView::on_keypress(const int key, const unsigned int modifier) return false; } -bool ModelView::on_keyrelease(const int key, const unsigned int modifier) +bool ModelView::on_mouserelease(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) + { modelview_dragging = false; return true; } @@ -132,7 +143,8 @@ bool ModelView::on_keyrelease(const int key, const unsigned int modifier) void ModelView::on_mousemove(const math::Vector2f &cursor) { - if ((width() <= 0) || (height() <= 0)) { + if ((width() <= 0) || (height() <= 0)) + { return; } diff --git a/src/ui/modelview.h b/src/ui/modelview.h index e23fd1c..f9566cd 100755 --- a/src/ui/modelview.h +++ b/src/ui/modelview.h @@ -88,16 +88,30 @@ protected: /// draw border virtual void draw_background(); - /// keypress event handler - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// keyrelease event handler - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); + + /** + * @brief mouse button release event handler + * */ + virtual bool on_mouserelease(const unsigned int button); + /** + * @brief mouse over event handler + * */ virtual void on_mouseover(const math::Vector2f &cursor); - /// mouse movement handler + /** + * @brief mouse movement event handler + * */ virtual void on_mousemove(const math::Vector2f &cursor); + + /** + * @brief mouse hweel event handler + * */ + virtual bool on_mousewheel(const math::Vector2f & direction); private: void reset(); diff --git a/src/ui/scrollbar.cc b/src/ui/scrollbar.cc index a8826a5..a766b38 100644 --- a/src/ui/scrollbar.cc +++ b/src/ui/scrollbar.cc @@ -108,44 +108,45 @@ bool ScrollBar::on_emit(Widget *sender, const Event event, void *data) return false; } -bool ScrollBar::on_keypress(const int key, const unsigned int modifier) +bool ScrollBar::on_mousewheel(const math::Vector2f & direction) { - switch (key) { - - case 512 + SDL_BUTTON_WHEELDOWN: - if (scrollbar_value < scrollbar_maximum) { - scrollbar_value++; - emit(EventScrollBarChanged, this); - } - return true; - break; - - case 512 + SDL_BUTTON_WHEELUP: - if (scrollbar_value > scrollbar_minimum) { - scrollbar_value--; - emit(EventScrollBarChanged, this); - } - return true; - break; - - case 512 + SDL_BUTTON_LEFT: - if (scrollbar_maximum > scrollbar_minimum) { - // TODO position hit test - scrollbar_dragging = true; - } - return true; - break; - - default: - break; + if (direction.y() > 0 ) + { + if (scrollbar_value < scrollbar_maximum) { + scrollbar_value--; + emit(EventScrollBarChanged, this); + } + return true; + } else if (direction.y() < 0 ) + { + if (scrollbar_value < scrollbar_maximum) { + scrollbar_value++; + emit(EventScrollBarChanged, this); + } + return true; + } + + return false; +} + +bool ScrollBar::on_mousepress(const unsigned int button) +{ + if (button == SDL_BUTTON_LEFT) + { + if (scrollbar_maximum > scrollbar_minimum) { + // TODO position hit test + scrollbar_dragging = true; + } + return true; } return false; } -bool ScrollBar::on_keyrelease(const int key, const unsigned int modifier) +bool ScrollBar::on_mouserelease(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) + { scrollbar_dragging = false; return true; } diff --git a/src/ui/scrollbar.h b/src/ui/scrollbar.h index 0689969..c8eccf9 100644 --- a/src/ui/scrollbar.h +++ b/src/ui/scrollbar.h @@ -89,17 +89,31 @@ protected: /// emit event handler virtual bool on_emit(Widget *sender, const Event event, void *data=0); - /// keypress event handler - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// keyrelease event handler - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); + + /** + * @brief mouse button release event handler + * */ + virtual bool on_mouserelease(const unsigned int button); + /** + * @brief mouseover event handler + * */ virtual void on_mouseover(const math::Vector2f &cursor); - /// mouse movement handler + /** + * @brief mouse movement event handler + * */ virtual void on_mousemove(const math::Vector2f &cursor); + /** + * @brief mousehweel event handler + * */ + virtual bool on_mousewheel(const math::Vector2f & direction); + private: /// validate slider value void validate(); diff --git a/src/ui/scrollpane.cc b/src/ui/scrollpane.cc index 48391c5..57c957f 100644 --- a/src/ui/scrollpane.cc +++ b/src/ui/scrollpane.cc @@ -65,24 +65,21 @@ void ScrollPane::set_offset(const int offset) scrollpane_offset = offset; } -bool ScrollPane::on_keypress(const int key, const unsigned int modifier) +bool ScrollPane::on_mousewheel(const math::Vector2f & direction) { // number of lines to scroll int alignmentmodifier =( (alignment() & AlignTop) == AlignTop) ? -1 : 1; - switch (key) { - - case 512 + SDL_BUTTON_WHEELUP: - inc_scroll(alignmentmodifier * scrollpane_offset); - return true; - break; - - case 512 + SDL_BUTTON_WHEELDOWN: - dec_scroll(alignmentmodifier * scrollpane_offset); - return true; - break; + if (direction.y() > 0 ) + { + inc_scroll(alignmentmodifier * scrollpane_offset); + return true; + } else if (direction.y() < 0 ) + { + dec_scroll(alignmentmodifier * scrollpane_offset); + return true; } - + return false; } diff --git a/src/ui/scrollpane.h b/src/ui/scrollpane.h index 8fe8395..eac2b17 100644 --- a/src/ui/scrollpane.h +++ b/src/ui/scrollpane.h @@ -58,8 +58,11 @@ protected: /// draw the scroll pane virtual void draw(); - /// key event handler provides mouse scrolling - virtual bool on_keypress(const int key, const unsigned int modifier); + + /** + * @brief mousehweel event handler + * */ + virtual bool on_mousewheel(const math::Vector2f & direction); private: ui::Text &scrollpane_text; diff --git a/src/ui/slider.cc b/src/ui/slider.cc index 9166656..ee29f05 100644 --- a/src/ui/slider.cc +++ b/src/ui/slider.cc @@ -108,44 +108,48 @@ bool Slider::on_emit(Widget *sender, const Event event, void *data) return false; } -bool Slider::on_keypress(const int key, const unsigned int modifier) +bool Slider::on_mousewheel(const math::Vector2f & direction) { - switch (key) { - - case 512 + SDL_BUTTON_WHEELUP: - if (slider_value < slider_maximum) { - slider_value++; - emit(EventSliderChanged, this); - } - return true; - break; - - case 512 + SDL_BUTTON_WHEELDOWN: - if (slider_value > slider_minimum) { - slider_value--; - emit(EventSliderChanged, this); - } - return true; - break; - - case 512 + SDL_BUTTON_LEFT: - if (slider_maximum > slider_minimum) { - // TODO position hit test - slider_dragging = true; - } - return true; - break; - - default: - break; + if (direction.y() > 0 ) + { + if (slider_value < slider_maximum) + { + slider_value++; + emit(EventSliderChanged, this); + } + return true; + } else if (direction.y() < 0 ) + { + if (slider_value > slider_minimum) + { + slider_value--; + emit(EventSliderChanged, this); + } + return true; + } + + return false; +} + +bool Slider::on_mousepress(const unsigned int button) +{ + if (button == SDL_BUTTON_LEFT) + { + if (slider_maximum > slider_minimum) + { + // TODO position hit test + slider_dragging = true; + } + return true; } return false; } -bool Slider::on_keyrelease(const int key, const unsigned int modifier) +bool Slider::on_mouserelease(const unsigned int button) { - if (key == 512 + SDL_BUTTON_LEFT) { + if (button == SDL_BUTTON_LEFT) + { slider_dragging = false; return true; } diff --git a/src/ui/slider.h b/src/ui/slider.h index 70a57ac..2f71c39 100644 --- a/src/ui/slider.h +++ b/src/ui/slider.h @@ -74,30 +74,53 @@ public: */ void set_range(const float minimum, const float maximum); - /// show the widget + /** + * @brief show the widget + * */ virtual void show(); protected: - /// resize event handler + /** + * @brief resize event handler + * */ virtual void resize(); - /// draw event handler + /** + * @brief draw event handler + * */ virtual void draw(); - /// emit event handler + /** + * @brief emit event handler + * */ virtual bool on_emit(Widget *sender, const Event event, void *data=0); - /// keypress event handler - virtual bool on_keypress(const int key, const unsigned int modifier); - - /// keyrelease event handler - virtual bool on_keyrelease(const int key, const unsigned int modifier); + /** + * @brief mouse button press event handler + * */ + virtual bool on_mousepress(const unsigned int button); + + /** + * @brief mouse button release event handler + * */ + virtual bool on_mouserelease(const unsigned int button); + /** + * @brief mouseover event handler + * */ virtual void on_mouseover(const math::Vector2f &cursor); - /// mouse movement handler + /** + * @brief mouse movement event handler + * */ virtual void on_mousemove(const math::Vector2f &cursor); + + /** + * @brief mousehweel event handler + * */ + virtual bool on_mousewheel(const math::Vector2f & direction); + private: /// validate slider value void validate(); diff --git a/src/ui/ui.cc b/src/ui/ui.cc index e10d77a..e3aeb31 100644 --- a/src/ui/ui.cc +++ b/src/ui/ui.cc @@ -284,31 +284,63 @@ void UI::input_mouse(const float x, const float y) mouse_cursor.assign(x, y); } +bool UI::input_mouse_button(const bool pressed, unsigned int button) +{ + bool handled = false; + + if (button == SDL_BUTTON_LEFT) + { + mouse_buttonleft_pressed = pressed; + } + + // set mouse focus + Widget *f = find_mouse_focus(mouse_cursor); + if (f) + { + f->event_mouse(mouse_cursor); + } + ui_mouse_focus = f; + + // send mouse button events + if (ui_mouse_focus) + { + handled = ui_mouse_focus->event_mouse_button(pressed, button); + } + return handled; +} + +bool UI::input_mouse_wheel(const math::Vector2f & direction) +{ + bool handled = false; + + // set mouse focus + Widget *f = find_mouse_focus(mouse_cursor); + if (f) + { + f->event_mouse(mouse_cursor); + } + ui_mouse_focus = f; + + // send mouse wheel events + if (ui_mouse_focus) + { + handled = ui_mouse_focus->event_mouse_wheel(direction); + } + return handled; +} + bool UI::input_key(const bool pressed, const int key, const unsigned int modifier) { bool handled = false; - if (key < 512) { - // keyboard keys - Widget *f = find_input_focus(); - if (f) { - handled = f->event_key(pressed, key, modifier); - } - ui_input_focus = f; - - } else if (key < 564) { - if ( key == 512 + SDL_BUTTON_LEFT) { - mouse_buttonleft_pressed = pressed; - } - // mouse buttons - Widget *f = find_mouse_focus(mouse_cursor); - if (f) { - f->event_mouse(mouse_cursor); - } - ui_mouse_focus = f; - if (ui_mouse_focus) - handled = ui_mouse_focus->event_key(pressed, key, modifier); + // send keyboard events + Widget *f = find_input_focus(); + if (f) + { + handled = f->event_key(pressed, key, modifier); } + ui_input_focus = f; + return handled; } diff --git a/src/ui/ui.h b/src/ui/ui.h index 77b04f5..5c44643 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -48,8 +48,14 @@ public: /// receive global mouse movement void input_mouse(const float x, const float y); + + /// receive global mouse button events + bool input_mouse_button(const bool pressed, unsigned int button); + + /// receive global mouse wheel events + bool input_mouse_wheel(const math::Vector2f & direction); - /// receive global key input + /// receive global keyboard events bool input_key(const bool pressed, const int key, const unsigned int modifier); /// run a user interface frame diff --git a/src/ui/widget.cc b/src/ui/widget.cc index e931147..982b792 100644 --- a/src/ui/widget.cc +++ b/src/ui/widget.cc @@ -438,7 +438,8 @@ bool Widget::event_key(const bool pressed, const int key, const unsigned int mod { bool handled = false; - if (enabled()) { + if (enabled()) + { if (pressed) { handled = on_keypress(key, modifier); } else { @@ -452,19 +453,51 @@ bool Widget::event_key(const bool pressed, const int key, const unsigned int mod return handled; } -bool Widget::event_mouse(const math::Vector2f &cursor) +void Widget::event_mouse(const math::Vector2f & cursor) { if (disabled()) - return false; + return; math::Vector2f local_cursor = to_local_coords(cursor); - bool handled = false; if (root()->mouse_focus() != this) { on_mouseover(local_cursor); } on_mousemove(local_cursor); +} + +bool Widget::event_mouse_button(const bool pressed, const unsigned int button) +{ + bool handled = false; + + if (enabled()) + { + if (pressed) { + handled = on_mousepress(button); + } else { + handled = on_mouserelease(button); + } + } + + if (!handled && parent()) + handled = parent()->event_mouse_button(pressed, button); + + return handled; +} + +bool Widget::event_mouse_wheel(const math::Vector2f & direction) +{ + bool handled = false; + + if (enabled()) + { + handled = on_mousewheel(direction); + } + + if (!handled && parent()) + handled = parent()->event_mouse_wheel(direction); + return handled; } @@ -512,6 +545,21 @@ void Widget::on_mousemove(const math::Vector2f &cursor) return; } +bool Widget::on_mousepress(const unsigned int button) +{ + return false; +} + +bool Widget::on_mouserelease(const unsigned int button) +{ + return false; +} + +bool Widget::on_mousewheel(const math::Vector2f & direction) +{ + return false; +} + bool Widget::on_keypress(const int key, const unsigned int modifier) { return false; diff --git a/src/ui/widget.h b/src/ui/widget.h index fafdf58..a3994af 100644 --- a/src/ui/widget.h +++ b/src/ui/widget.h @@ -7,7 +7,7 @@ #ifndef __INCLUDED_UI_WIDGET_H__ #define __INCLUDED_UI_WIDGET_H__ -#include "SDL/SDL.h" +#include "SDL2/SDL.h" #include <string> #include <list> @@ -270,13 +270,26 @@ public: * @see on_keyrelease **/ bool event_key(const bool pressed, const int key, const unsigned int modifier); + + /** + * @brief calls the on_mousepress and on_mouserelease event handlers and sends undhandled events to the parent widget + * @see on_mousepress + * @see on_mouserelease + * */ + bool event_mouse_button(const bool pressed, const unsigned int button); + + /** + * @brief calls the on_mousewheel event handlers and sends unhandled events to the parent widget + * @see on_mousewheel + * */ + bool event_mouse_wheel(const math::Vector2f & direction); /** * @brief calls the mouse event handlers and sends unhandled keys to the parent widget * @see on_mousemove * @see on_mouseover **/ - bool event_mouse(const math::Vector2f &cursor); + void event_mouse(const math::Vector2f & cursor); /** * @brief calls the custom event handler and sends unhandled events to the parent widget @@ -284,7 +297,9 @@ public: **/ bool event_emit(Widget *sender, const Event event, void *data = 0); - /// emit a custom event + /** + * @brief emit a custom event + * */ inline void emit(const Event event, void *data=0) { event_emit(this, event, data); } @@ -355,6 +370,15 @@ protected: /// called when the mouse enters the widget virtual void on_mouseover(const math::Vector2f &cursor); + + /// called when a mouse button is pressed + virtual bool on_mousepress(const unsigned int button); + + /// called when a mouse button is released + virtual bool on_mouserelease(const unsigned int button); + + /// called when the scrollwheel is used + virtual bool on_mousewheel(const math::Vector2f & direction); /// called when the widget receives a key press virtual bool on_keypress(const int key, const unsigned int modifier); |