[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
This commit is contained in:
Léa Saviot
2017-10-30 15:10:46 +01:00
committed by Romain Goyet
parent 681be23c30
commit b7baec4a40
10 changed files with 100 additions and 7 deletions

View File

@@ -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

View File

@@ -2,6 +2,7 @@
#define CODE_APP_H
#include <escher.h>
#include <ion/events.h>
#include "../shared/message_controller.h"
#include "menu_controller.h"
#include "script_store.h"

View File

@@ -0,0 +1,4 @@
CodeApp = "Python"
CodeAppCapital = "PYTHON"
ConsolePrompt = ">>> "
ConsoleError = "Error"

View File

@@ -1,6 +1,6 @@
#include "console_controller.h"
#include "script.h"
#include "app.h"
#include "helpers.h"
#include <apps/i18n.h>
#include <assert.h>
@@ -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) {

View File

@@ -1,5 +1,7 @@
#include "editor_controller.h"
#include "script_parameter_controller.h"
#include "helpers.h"
#include <apps/code/app.h>
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<char *>(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<char *>(textArea->text());
@@ -95,6 +108,7 @@ bool EditorController::textAreaDidReceiveEvent(TextArea * textArea, Ion::Events:
return true;
}
}
return false;
}

46
apps/code/helpers.cpp Normal file
View File

@@ -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<sizeof(sEventTextMap)/sizeof(sEventTextMap[0]); i++) {
if (event == sEventTextMap[i].event()) {
return sEventTextMap[i].text();
}
}
return nullptr;
}
}
}

14
apps/code/helpers.h Normal file
View File

@@ -0,0 +1,14 @@
#ifndef CODE_HELPERS_H
#define CODE_HELPERS_H
#include <ion/events.h>
namespace Code {
namespace Helpers {
const char * PythonTextForEvent(Ion::Events::Event event);
}
}
#endif

View File

@@ -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"

View File

@@ -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:

View File

@@ -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);
}