00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "xrb_key.hpp"
00012
00013 #include "xrb_input_events.hpp"
00014
00015 namespace Xrb
00016 {
00017
00018 Key *Key::Create (Key::Code const code, std::string const &name)
00019 {
00020 ASSERT1(!name.empty());
00021
00022 Key *retval = new Key();
00023
00024 retval->m_code = code;
00025 retval->m_name = name;
00026
00027 return retval;
00028 }
00029
00030 bool Key::IsKeyRepeatable (Key::Code const code)
00031 {
00032
00033 if ((code >= Key::SPACE && code <= Key::DELETE)
00034 ||
00035 code == Key::BACKSPACE
00036 ||
00037 (code >= Key::KP0 && code <= Key::KP_PLUS)
00038 ||
00039 (code >= Key::UP && code <= Key::LEFT)
00040 ||
00041 code == Key::PAGEUP
00042 ||
00043 code == Key::PAGEDOWN
00044 ||
00045 (code >= Key::WORLD_0 && code <= Key::WORLD_95))
00046 return true;
00047
00048
00049 return false;
00050 }
00051
00052 bool Key::IsKeyAscii (Key::Code const code)
00053 {
00054 if ((code >= ZERO && code <= NINE) || (code >= A && code <= Z))
00055 return true;
00056
00057 switch (code)
00058 {
00059 case TAB:
00060 case RETURN:
00061 case ESCAPE:
00062 case SPACE:
00063 case QUOTE:
00064 case COMMA:
00065 case MINUS:
00066 case PERIOD:
00067 case SLASH:
00068 case SEMICOLON:
00069 case EQUALS:
00070 case LEFTBRACKET:
00071 case BACKSLASH:
00072 case RIGHTBRACKET:
00073 case BACKQUOTE:
00074 case DELETE:
00075 return true;
00076
00077 default:
00078 return false;
00079 }
00080 }
00081
00082 void Key::ResetPressed ()
00083 {
00084 m_is_pressed = false;
00085 }
00086
00087 Key::Key ()
00088 :
00089 EventHandler(NULL)
00090 {
00091 m_code = Key::INVALID;
00092 m_is_pressed = false;
00093 }
00094
00095 bool Key::HandleEvent (Event const *const e)
00096 {
00097 ASSERT1(e != NULL);
00098
00099 switch (e->GetEventType())
00100 {
00101 case Event::KEYDOWN:
00102
00103 ASSERT1(static_cast<EventKeyDown const *const>(e)->KeyCode() == m_code);
00104 m_is_pressed = true;
00105 break;
00106
00107 case Event::KEYUP:
00108
00109 ASSERT1(static_cast<EventKeyUp const *const>(e)->KeyCode() == m_code);
00110 m_is_pressed = false;
00111 break;
00112
00113 case Event::MOUSEBUTTONDOWN:
00114
00115 ASSERT1(static_cast<EventMouseButtonDown const *const>(e)->ButtonCode() == m_code);
00116 m_is_pressed = true;
00117 break;
00118
00119 case Event::MOUSEBUTTONUP:
00120
00121 ASSERT1(static_cast<EventMouseButtonUp const *const>(e)->ButtonCode() == m_code);
00122 m_is_pressed = false;
00123 break;
00124
00125 default:
00126 ASSERT1(false && "Events which are not of type KEYDOWN, KEYUP, MOUSEBUTTONDOWN, or MOUSEBUTTONUP should not be passed to Key::HandleEvent()");
00127 break;
00128 }
00129
00130 return true;
00131 }
00132
00133 }