00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_keyselectorbutton.hpp"
00012
00013 #include "xrb_inputstate.hpp"
00014 #include "xrb_input_events.hpp"
00015 #include "xrb_label.hpp"
00016 #include "xrb_layout.hpp"
00017 #include "xrb_screen.hpp"
00018
00019 namespace Xrb
00020 {
00021
00022 KeySelectorButton::KeySelectorButton (
00023 std::string const &input_action_name,
00024 Key::Code const key_code,
00025 ContainerWidget *const parent,
00026 std::string const &name)
00027 :
00028 Button(Singleton::InputState().KeyName(key_code), parent, name),
00029 m_input_action_name(input_action_name),
00030 m_internal_receiver_dialog_returned(&KeySelectorButton::DialogReturned, this)
00031 {
00032 m_key_code = key_code;
00033 m_key_selector_dialog = NULL;
00034 }
00035
00036 void KeySelectorButton::SetKeyCode (Key::Code const key_code)
00037 {
00038 if (Singleton::InputState().IsValidKeyCode(key_code))
00039 m_key_code = key_code;
00040 else
00041 m_key_code = Key::INVALID;
00042 SetText(Singleton::InputState().KeyName(m_key_code));
00043 }
00044
00045 void KeySelectorButton::HandleReleased ()
00046 {
00047 ASSERT1(m_key_selector_dialog == NULL);
00048 m_key_selector_dialog =
00049 new KeySelectorDialog(
00050 "Press new key/button for \"" + m_input_action_name + "\"",
00051 TopLevelParent());
00052 m_key_selector_dialog->CenterOnWidget(TopLevelParent());
00053 SignalHandler::Connect1(
00054 m_key_selector_dialog->SenderDialogReturned(),
00055 &m_internal_receiver_dialog_returned);
00056 }
00057
00058 void KeySelectorButton::DialogReturned (Dialog::ButtonID const button_id)
00059 {
00060 ASSERT1(m_key_selector_dialog != NULL);
00061 if (button_id == Dialog::ID_OK)
00062 SetKeyCode(m_key_selector_dialog->KeyCode());
00063 m_key_selector_dialog = NULL;
00064 }
00065
00066
00067
00068
00069
00070 KeySelectorButton::KeySelectorDialog::KeySelectorDialog (
00071 std::string const &message,
00072 ContainerWidget *const parent,
00073 std::string const &name)
00074 :
00075 Dialog(DT_CANCEL, parent, name)
00076 {
00077
00078
00079 m_children_get_input_events_first = true;
00080
00081
00082 GrabMouse();
00083
00084 m_key_code = Key::INVALID;
00085
00086 Label *l = new Label(message, DialogLayout());
00087 l->SetIsHeightFixedToTextHeight(true);
00088 }
00089
00090 bool KeySelectorButton::KeySelectorDialog::ProcessKeyEvent (EventKey const *const e)
00091 {
00092 ASSERT1(e != NULL);
00093 ASSERT1(Singleton::InputState().IsValidKeyCode(e->KeyCode()));
00094 m_key_code = e->KeyCode();
00095 OKButtonActivated();
00096 return true;
00097 }
00098
00099 bool KeySelectorButton::KeySelectorDialog::ProcessMouseButtonEvent (EventMouseButton const *const e)
00100 {
00101 ASSERT1(e != NULL);
00102 ASSERT1(Singleton::InputState().IsValidKeyCode(e->ButtonCode()));
00103 m_key_code = e->ButtonCode();
00104 OKButtonActivated();
00105 return true;
00106 }
00107
00108 bool KeySelectorButton::KeySelectorDialog::ProcessMouseWheelEvent (EventMouseWheel const *const e)
00109 {
00110 ASSERT1(e != NULL);
00111 ASSERT1(Singleton::InputState().IsValidKeyCode(e->ButtonCode()));
00112 m_key_code = e->ButtonCode();
00113 OKButtonActivated();
00114 return true;
00115 }
00116
00117 }
00118