From a6a2baeb01f4c39b9da400b772cc7db488d776b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Tue, 8 Jan 2019 10:53:34 +0100 Subject: [PATCH] [simulator] Process more keyboard event, such as ! ? ; --- ion/include/ion/events.h | 3 +- ion/src/simulator/keyboard/fltkkbd.cpp | 129 +++++++++++++++++++------ ion/src/simulator/keyboard/fltkkbd.h | 7 +- 3 files changed, 107 insertions(+), 32 deletions(-) diff --git a/ion/include/ion/events.h b/ion/include/ion/events.h index fc5270631..3ecbc5d2f 100644 --- a/ion/include/ion/events.h +++ b/ion/include/ion/events.h @@ -16,8 +16,9 @@ public: constexpr Event() : m_id(4*PageSize){} // Return Ion::Event::None by default constexpr Event(int i) : m_id(i){} // TODO: Assert here that i>=0 && i<255 -#if DEBUG + uint8_t id() const { return m_id; } +#if DEBUG const char * name() const; #endif Event(Keyboard::Key key, bool shift, bool alpha); diff --git a/ion/src/simulator/keyboard/fltkkbd.cpp b/ion/src/simulator/keyboard/fltkkbd.cpp index 22282a121..ad1c1988a 100644 --- a/ion/src/simulator/keyboard/fltkkbd.cpp +++ b/ion/src/simulator/keyboard/fltkkbd.cpp @@ -31,10 +31,6 @@ static const int kShortcutForKey[Ion::Keyboard::NumberOfKeys] = { FL_SHIFT | '0', '.', 0, 0, FL_KP_Enter, 0 }; -static bool shouldRepeatKey(Ion::Keyboard::Key k) { - return k <= Ion::Keyboard::Key::A4 || k == Ion::Keyboard::Key::A6; -} - static void keyHandler(Fl_Widget *, long key) { if (currentEvent == Ion::Events::None) { currentEvent = Ion::Events::Event((Ion::Keyboard::Key)key, @@ -44,32 +40,18 @@ static void keyHandler(Fl_Widget *, long key) { } } -static void alphabetEventHandler(Fl_Widget *, long c) { - static Ion::Events::Event lowerEvents[FltkKbd::sNumberOfLettersInAlphabet] = { - Ion::Events::LowerA, Ion::Events::LowerB, Ion::Events::LowerC, Ion::Events::LowerD, Ion::Events::LowerE, - Ion::Events::LowerF, Ion::Events::LowerG, Ion::Events::LowerH, Ion::Events::LowerI, Ion::Events::LowerJ, - Ion::Events::LowerK, Ion::Events::LowerL, Ion::Events::LowerM, Ion::Events::LowerN, Ion::Events::LowerO, - Ion::Events::LowerP, Ion::Events::LowerQ, Ion::Events::LowerR, Ion::Events::LowerS, Ion::Events::LowerT, - Ion::Events::LowerU, Ion::Events::LowerV, Ion::Events::LowerW, Ion::Events::LowerX, Ion::Events::LowerY, - Ion::Events::LowerZ }; - static Ion::Events::Event upperEvents[FltkKbd::sNumberOfLettersInAlphabet] = { - Ion::Events::UpperA, Ion::Events::UpperB, Ion::Events::UpperC, Ion::Events::UpperD, Ion::Events::UpperE, - Ion::Events::UpperF, Ion::Events::UpperG, Ion::Events::UpperH, Ion::Events::UpperI, Ion::Events::UpperJ, - Ion::Events::UpperK, Ion::Events::UpperL, Ion::Events::UpperM, Ion::Events::UpperN, Ion::Events::UpperO, - Ion::Events::UpperP, Ion::Events::UpperQ, Ion::Events::UpperR, Ion::Events::UpperS, Ion::Events::UpperT, - Ion::Events::UpperU, Ion::Events::UpperV, Ion::Events::UpperW, Ion::Events::UpperX, Ion::Events::UpperY, - Ion::Events::UpperZ - }; - if (currentEvent == Ion::Events::None) { - if (c >= 'a' && c <= 'z') { - currentEvent = lowerEvents[c - 'a']; - } else if (c >= 'A' && c <= 'Z') { - currentEvent = upperEvents[c - 'A']; - } - } +FltkKbd::FltkKbd(int x, int y, int w, int h) : Fl_Group(x, y, w, h) { + initButtons(x, y, w, h); + initAlphabetShortcuts(); + initOtherShortcuts(); + end(); } -FltkKbd::FltkKbd(int x, int y, int w, int h) : Fl_Group(x, y, w, h) { +static bool shouldRepeatKey(Ion::Keyboard::Key k) { + return k <= Ion::Keyboard::Key::A4 || k == Ion::Keyboard::Key::A6; +} + +void FltkKbd::initButtons(int x, int y, int w, int h) { assert(KeyboardRows * KeyboardColumns == Ion::Keyboard::NumberOfKeys); int keyWidth = w / KeyboardColumns; int keyHeight = h / KeyboardRows; @@ -94,12 +76,38 @@ FltkKbd::FltkKbd(int x, int y, int w, int h) : Fl_Group(x, y, w, h) { } m_buttons[k]->clear_visible_focus(); } +} +static void alphabetEventHandler(Fl_Widget *, long c) { + static Ion::Events::Event lowerEvents[FltkKbd::sNumberOfLettersInAlphabet] = { + Ion::Events::LowerA, Ion::Events::LowerB, Ion::Events::LowerC, Ion::Events::LowerD, Ion::Events::LowerE, + Ion::Events::LowerF, Ion::Events::LowerG, Ion::Events::LowerH, Ion::Events::LowerI, Ion::Events::LowerJ, + Ion::Events::LowerK, Ion::Events::LowerL, Ion::Events::LowerM, Ion::Events::LowerN, Ion::Events::LowerO, + Ion::Events::LowerP, Ion::Events::LowerQ, Ion::Events::LowerR, Ion::Events::LowerS, Ion::Events::LowerT, + Ion::Events::LowerU, Ion::Events::LowerV, Ion::Events::LowerW, Ion::Events::LowerX, Ion::Events::LowerY, + Ion::Events::LowerZ}; + static Ion::Events::Event upperEvents[FltkKbd::sNumberOfLettersInAlphabet] = { + Ion::Events::UpperA, Ion::Events::UpperB, Ion::Events::UpperC, Ion::Events::UpperD, Ion::Events::UpperE, + Ion::Events::UpperF, Ion::Events::UpperG, Ion::Events::UpperH, Ion::Events::UpperI, Ion::Events::UpperJ, + Ion::Events::UpperK, Ion::Events::UpperL, Ion::Events::UpperM, Ion::Events::UpperN, Ion::Events::UpperO, + Ion::Events::UpperP, Ion::Events::UpperQ, Ion::Events::UpperR, Ion::Events::UpperS, Ion::Events::UpperT, + Ion::Events::UpperU, Ion::Events::UpperV, Ion::Events::UpperW, Ion::Events::UpperX, Ion::Events::UpperY, + Ion::Events::UpperZ}; + if (currentEvent == Ion::Events::None) { + if (c >= 'a' && c <= 'z') { + currentEvent = lowerEvents[c - 'a']; + } else if (c >= 'A' && c <= 'Z') { + currentEvent = upperEvents[c - 'A']; + } + } +} + +void FltkKbd::initAlphabetShortcuts() { // Get keyboard letters event for (int k = 0; k < sNumberOfLettersInAlphabet; k++) { char l = 'a' + k; m_alphabetShortcuts[k] = new Fl_Button(0, 0, 0, 0, ""); - m_alphabetShortcuts[k]->callback(alphabetEventHandler,l); + m_alphabetShortcuts[k]->callback(alphabetEventHandler, l); m_alphabetShortcuts[k]->shortcut(l); m_alphabetShortcuts[k]->clear_visible_focus(); } @@ -111,10 +119,71 @@ FltkKbd::FltkKbd(int x, int y, int w, int h) : Fl_Group(x, y, w, h) { m_alphabetShortcuts[index]->shortcut(FL_SHIFT | ('a' + k)); m_alphabetShortcuts[index]->clear_visible_focus(); } +} - end(); +static void otherEventHandler(Fl_Widget *, long c) { + if (currentEvent == Ion::Events::None) { + currentEvent = Ion::Events::Event(c); + } +} + +void FltkKbd::initOtherShortcuts() { + // Get other events + for (int k = 0; k < sNumberOfOtherShortcuts; k++) { + m_otherShortcuts[k] = new Fl_Button(0, 0, 0, 0, ""); + } + int index = 0; + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Imaginary.id()); + m_otherShortcuts[index++]->shortcut(FL_ALT | 'i'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Comma.id()); + m_otherShortcuts[index++]->shortcut(','); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Power.id()); + m_otherShortcuts[index++]->shortcut('^'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::ShiftLeft.id()); + m_otherShortcuts[index++]->shortcut(FL_SHIFT | FL_Left); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::ShiftRight.id()); + m_otherShortcuts[index++]->shortcut(FL_SHIFT | FL_Right); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Cut.id()); + m_otherShortcuts[index++]->shortcut(FL_META | 'x'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Copy.id()); + m_otherShortcuts[index++]->shortcut(FL_META | 'c'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Paste.id()); + m_otherShortcuts[index++]->shortcut(FL_META | 'v'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Clear.id()); + m_otherShortcuts[index++]->shortcut(FL_META | FL_BackSpace); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::LeftBracket.id()); + m_otherShortcuts[index++]->shortcut(FL_SHIFT | FL_ALT | '('); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::RightBracket.id()); + m_otherShortcuts[index++]->shortcut(FL_SHIFT | FL_ALT | ')'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::LeftBrace.id()); + m_otherShortcuts[index++]->shortcut(FL_ALT | '('); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::RightBrace.id()); + m_otherShortcuts[index++]->shortcut(FL_ALT | ')'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Underscore.id()); + m_otherShortcuts[index++]->shortcut('_'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Equal.id()); + m_otherShortcuts[index++]->shortcut('='); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Lower.id()); + m_otherShortcuts[index++]->shortcut('<'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Greater.id()); + m_otherShortcuts[index++]->shortcut('>'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Colon.id()); + m_otherShortcuts[index++]->shortcut(':'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::SemiColon.id()); + m_otherShortcuts[index++]->shortcut(';'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::DoubleQuotes.id()); + m_otherShortcuts[index++]->shortcut('"'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Question.id()); + m_otherShortcuts[index++]->shortcut('?'); + m_otherShortcuts[index]->callback(otherEventHandler, Ion::Events::Exclamation.id()); + m_otherShortcuts[index++]->shortcut('!'); + assert(index == sNumberOfOtherShortcuts); + for (int k = 0; k < sNumberOfOtherShortcuts; k++) { + m_otherShortcuts[k]->clear_visible_focus(); + } } bool FltkKbd::key_down(Ion::Keyboard::Key key) { + assert((int)key >= 0 && (int)key < Ion::Keyboard::NumberOfKeys); return m_buttons[(int)key]->value(); } diff --git a/ion/src/simulator/keyboard/fltkkbd.h b/ion/src/simulator/keyboard/fltkkbd.h index dc30a76d3..8c5fd444d 100644 --- a/ion/src/simulator/keyboard/fltkkbd.h +++ b/ion/src/simulator/keyboard/fltkkbd.h @@ -8,11 +8,16 @@ class FltkKbd : public Fl_Group { public: constexpr static int sNumberOfLettersInAlphabet = 26; + constexpr static int sNumberOfOtherShortcuts = 22; FltkKbd(int x, int y, int w, int h); bool key_down(Ion::Keyboard::Key key); private: + void initButtons(int x, int y, int w, int h); + void initAlphabetShortcuts(); + void initOtherShortcuts(); Fl_Button * m_buttons[Ion::Keyboard::NumberOfKeys]; - Fl_Button * m_alphabetShortcuts[2*sNumberOfLettersInAlphabet]; + Fl_Button * m_alphabetShortcuts[2 * sNumberOfLettersInAlphabet]; + Fl_Button * m_otherShortcuts[sNumberOfOtherShortcuts]; }; #endif