From b7baec4a40d33ba2c673376f0e36ca8ef1da7e17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9a=20Saviot?= Date: Mon, 30 Oct 2017 15:10:46 +0100 Subject: [PATCH] [code] Keyboard events translation, for script or command edition. For instance, the "power" event should insert "**" in the text, not "^". Added a universal i18n file in apps/code. Change-Id: I5d067b0c5b86590af95aa98856522d54e42acba6 --- apps/code/Makefile | 2 ++ apps/code/app.h | 1 + apps/code/base.universal.i18n | 4 +++ apps/code/console_controller.cpp | 15 ++++++++-- apps/code/editor_controller.cpp | 16 ++++++++++- apps/code/helpers.cpp | 46 +++++++++++++++++++++++++++++++ apps/code/helpers.h | 14 ++++++++++ apps/shared.universal.i18n | 4 --- escher/include/escher/text_area.h | 1 + escher/src/text_area.cpp | 4 +++ 10 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 apps/code/base.universal.i18n create mode 100644 apps/code/helpers.cpp create mode 100644 apps/code/helpers.h diff --git a/apps/code/Makefile b/apps/code/Makefile index 85a120bae..536375719 100644 --- a/apps/code/Makefile +++ b/apps/code/Makefile @@ -8,6 +8,7 @@ app_objs += $(addprefix apps/code/,\ console_line_cell.o\ console_store.o\ editor_controller.o\ + helpers.o\ menu_controller.o\ script.o\ script_parameter_controller.o\ @@ -21,6 +22,7 @@ i18n_files += $(addprefix apps/code/,\ base.es.i18n\ base.fr.i18n\ base.pt.i18n\ + base.universal.i18n\ ) app_images += apps/code/code_icon.png diff --git a/apps/code/app.h b/apps/code/app.h index 5f5e05767..616680173 100644 --- a/apps/code/app.h +++ b/apps/code/app.h @@ -2,6 +2,7 @@ #define CODE_APP_H #include +#include #include "../shared/message_controller.h" #include "menu_controller.h" #include "script_store.h" diff --git a/apps/code/base.universal.i18n b/apps/code/base.universal.i18n new file mode 100644 index 000000000..7fca324ab --- /dev/null +++ b/apps/code/base.universal.i18n @@ -0,0 +1,4 @@ +CodeApp = "Python" +CodeAppCapital = "PYTHON" +ConsolePrompt = ">>> " +ConsoleError = "Error" diff --git a/apps/code/console_controller.cpp b/apps/code/console_controller.cpp index a6cf81563..68093dc6b 100644 --- a/apps/code/console_controller.cpp +++ b/apps/code/console_controller.cpp @@ -1,6 +1,6 @@ #include "console_controller.h" #include "script.h" -#include "app.h" +#include "helpers.h" #include #include @@ -187,7 +187,18 @@ bool ConsoleController::textFieldShouldFinishEditing(TextField * textField, Ion: } bool ConsoleController::textFieldDidReceiveEvent(TextField * textField, Ion::Events::Event event) { - return false; + const char * pythonText = Helpers::PythonTextForEvent(event); + if (pythonText == nullptr) { + return false; + } + textField->setEditing(true, false); + if (textField->insertTextAtLocation(pythonText, textField->cursorLocation())) { + textField->setCursorLocation(textField->cursorLocation()+strlen(pythonText)); + if (pythonText[strlen(pythonText)-1] == ')') { + textField->setCursorLocation(textField->cursorLocation()-1); + } + } + return true; } bool ConsoleController::textFieldDidFinishEditing(TextField * textField, const char * text, Ion::Events::Event event) { diff --git a/apps/code/editor_controller.cpp b/apps/code/editor_controller.cpp index 11341f256..ef75805d2 100644 --- a/apps/code/editor_controller.cpp +++ b/apps/code/editor_controller.cpp @@ -1,5 +1,7 @@ #include "editor_controller.h" #include "script_parameter_controller.h" +#include "helpers.h" +#include namespace Code { @@ -36,6 +38,15 @@ bool EditorController::textAreaShouldFinishEditing(TextArea * textArea, Ion::Eve } bool EditorController::textAreaDidReceiveEvent(TextArea * textArea, Ion::Events::Event event) { + const char * pythonText = Helpers::PythonTextForEvent(event); + if (pythonText != nullptr) { + textArea->insertText(pythonText); + if (pythonText[strlen(pythonText)-1] == ')') { + textArea->moveCursor(-1); + } + return true; + } + if (event == Ion::Events::EXE) { // Auto-Indent char * text = const_cast(textArea->text()); @@ -62,7 +73,9 @@ bool EditorController::textAreaDidReceiveEvent(TextArea * textArea, Ion::Events: textArea->insertText(" "); } return true; - } else if (event == Ion::Events::Backspace) { + } + + if (event == Ion::Events::Backspace) { // If the cursor is on the left of the text of a line, // backspace one intentation space at a time. char * text = const_cast(textArea->text()); @@ -95,6 +108,7 @@ bool EditorController::textAreaDidReceiveEvent(TextArea * textArea, Ion::Events: return true; } } + return false; } diff --git a/apps/code/helpers.cpp b/apps/code/helpers.cpp new file mode 100644 index 000000000..40cb4697a --- /dev/null +++ b/apps/code/helpers.cpp @@ -0,0 +1,46 @@ +#include "helpers.h" + +namespace Code { +namespace Helpers { + +class EventTextPair { +public: + constexpr EventTextPair(Ion::Events::Event event, const char * text) : m_event(event), m_text(text) {} + Ion::Events::Event event() const { return m_event; } + const char * text() const { return m_text; } +private: + const Ion::Events::Event m_event; + const char * m_text; +}; + +static constexpr EventTextPair sEventTextMap[] = { + EventTextPair(Ion::Events::XNT, "x"), + EventTextPair(Ion::Events::Exp, "exp()"), + EventTextPair(Ion::Events::Ln, "log()"), + EventTextPair(Ion::Events::Log, "log10()"), + EventTextPair(Ion::Events::Sine, "sin()"), + EventTextPair(Ion::Events::Cosine, "cos()"), + EventTextPair(Ion::Events::Tangent, "tan()"), + EventTextPair(Ion::Events::Imaginary, "1j"), + EventTextPair(Ion::Events::Power, "**"), + EventTextPair(Ion::Events::Pi, "pi"), + EventTextPair(Ion::Events::Sqrt, "sqrt()"), + EventTextPair(Ion::Events::Square, "**2"), + EventTextPair(Ion::Events::Multiplication, "*"), + EventTextPair(Ion::Events::EE, "e"), + EventTextPair(Ion::Events::Arcsine, "asin()"), + EventTextPair(Ion::Events::Arccosine, "acos()"), + EventTextPair(Ion::Events::Arctangent, "atan()") +}; + +const char * PythonTextForEvent(Ion::Events::Event event) { + for (int i=0; i + +namespace Code { +namespace Helpers { + +const char * PythonTextForEvent(Ion::Events::Event event); + +} +} + +#endif diff --git a/apps/shared.universal.i18n b/apps/shared.universal.i18n index 3a5c60247..e18f32319 100644 --- a/apps/shared.universal.i18n +++ b/apps/shared.universal.i18n @@ -1,6 +1,4 @@ Default = "" -CodeApp = "Python" -CodeAppCapital = "PYTHON" Alpha = "alpha" CapitalAlpha = "ALPHA" Shift = "shift" @@ -106,5 +104,3 @@ AtanhCommandWithArg = "atanh(x)" Prediction95CommandWithArg = "prediction95(p,n)" PredictionCommandWithArg = "prediction(p,n)" ConfidenceCommandWithArg = "confidence(f,n)" -ConsolePrompt = ">>> " -ConsoleError = "Error" diff --git a/escher/include/escher/text_area.h b/escher/include/escher/text_area.h index 82ef02851..d371c8465 100644 --- a/escher/include/escher/text_area.h +++ b/escher/include/escher/text_area.h @@ -20,6 +20,7 @@ public: void removeChar() { m_contentView.removeChar(); } const char * text() const { return m_contentView.text(); } int cursorLocation() const { return m_contentView.cursorLocation(); } + void moveCursor(int deltaX); private: class Text { public: diff --git a/escher/src/text_area.cpp b/escher/src/text_area.cpp index 9993ce82f..a5a8bdfe6 100644 --- a/escher/src/text_area.cpp +++ b/escher/src/text_area.cpp @@ -368,3 +368,7 @@ void TextArea::setText(char * textBuffer, size_t textBufferSize) { m_contentView.setText(textBuffer, textBufferSize); m_contentView.moveCursorGeo(0, 0); } + +void TextArea::moveCursor(int deltaX) { + m_contentView.moveCursorIndex(deltaX); +}