00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_button.hpp"
00012
00013 #include "xrb_input_events.hpp"
00014
00015 namespace Xrb
00016 {
00017
00018 Button::Button (
00019 std::string const &text,
00020 ContainerWidget *const parent,
00021 std::string const &name)
00022 :
00023 Label(text, parent, name),
00024 m_sender_pressed_state_changed(this),
00025 m_sender_pressed(this),
00026 m_sender_released(this)
00027 {
00028 SetAlignment(Alignment2(CENTER, CENTER));
00029 Initialize();
00030 }
00031
00032 Button::Button (
00033 Resource<GLTexture> const &picture,
00034 ContainerWidget *const parent,
00035 std::string const &name)
00036 :
00037 Label(picture, parent, name),
00038 m_sender_pressed_state_changed(this),
00039 m_sender_pressed(this),
00040 m_sender_released(this)
00041 {
00042 Initialize();
00043 }
00044
00045 void Button::SetIsEnabled (bool const is_enabled)
00046 {
00047 Widget::SetIsEnabled(is_enabled);
00048 if (!IsEnabled())
00049 m_is_pressed = false;
00050 UpdateRenderBackground();
00051 }
00052
00053 bool Button::ProcessMouseButtonEvent (EventMouseButton const *const e)
00054 {
00055 if (e->ButtonCode() == Key::LEFTMOUSE)
00056 {
00057 if (e->IsMouseButtonDownEvent())
00058 {
00059 m_is_pressed = true;
00060 HandlePressed();
00061 }
00062 else if (e->IsMouseButtonUpEvent())
00063 {
00064 if (m_is_pressed)
00065 {
00066 m_is_pressed = false;
00067 HandleReleased();
00068 }
00069 }
00070 else
00071 {
00072 ASSERT1(false &&
00073 "IsMouseButtonDownEvent() and IsMouseButtonUpEvent() "
00074 "both failed. Something's wacky.");
00075 }
00076
00077
00078 UpdateRenderBackground();
00079 }
00080
00081 return true;
00082 }
00083
00084 void Button::HandleMouseoverOn ()
00085 {
00086 ASSERT1(IsMouseover());
00087 UpdateRenderBackground();
00088 }
00089
00090 void Button::HandleMouseoverOff ()
00091 {
00092 ASSERT1(!IsMouseover());
00093 m_is_pressed = false;
00094 UpdateRenderBackground();
00095 }
00096
00097 void Button::HandlePressed ()
00098 {
00099 m_sender_pressed_state_changed.Signal(m_is_pressed);
00100 m_sender_pressed.Signal();
00101 }
00102
00103 void Button::HandleReleased ()
00104 {
00105 m_sender_pressed_state_changed.Signal(m_is_pressed);
00106 m_sender_released.Signal();
00107 }
00108
00109 void Button::UpdateRenderBackground ()
00110 {
00111
00112 if (!IsEnabled())
00113 SetRenderBackground(
00114 WidgetSkinWidgetBackground(WidgetSkin::BUTTON_BACKGROUND));
00115 else if (IsPressed())
00116 SetRenderBackground(
00117 WidgetSkinWidgetBackground(WidgetSkin::BUTTON_PRESSED_BACKGROUND));
00118 else if (IsMouseover() && AcceptsMouseover())
00119 SetRenderBackground(
00120 WidgetSkinWidgetBackground(WidgetSkin::BUTTON_MOUSEOVER_BACKGROUND));
00121 else
00122 SetRenderBackground(
00123 WidgetSkinWidgetBackground(WidgetSkin::BUTTON_BACKGROUND));
00124 }
00125
00126 void Button::HandleChangedWidgetSkinWidgetBackground (
00127 WidgetSkin::WidgetBackgroundType const widget_background_type)
00128 {
00129
00130 UpdateRenderBackground();
00131 }
00132
00133 void Button::Initialize ()
00134 {
00135 m_accepts_focus = true;
00136 m_accepts_mouseover = true;
00137 m_is_pressed = false;
00138
00139 Button::UpdateRenderBackground();
00140 }
00141
00142 }