Skip to content

Commit 9030a73

Browse files
committed
#173 on screen touch keyboard working for most cases now.
1 parent 88d24c0 commit 9030a73

7 files changed

Lines changed: 138 additions & 48 deletions

‎src/EditableLargeNumberMenuItem.cpp‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,14 @@ void EditableLargeNumberMenuItem::setLargeNumberFromString(const char* val) {
109109
setChanged(true);
110110
}
111111

112+
void EditableLargeNumberMenuItem::setNegativeFlagValue(int neg) {
113+
uint8_t sz[2];
114+
sz[0] = lowByte(neg);
115+
sz[1] = highByte(0);
116+
renderFn(this, 1, RENDERFN_SET_VALUE, reinterpret_cast<char*>(sz), sizeof(sz));
117+
changeOccurred(false);
118+
}
119+
112120
void wrapEditor(bool editRow, uint8_t row, char val, char* buffer, int bufferSize) {
113121
if (editRow) {
114122
menuMgr.setEditorHints(CurrentEditorRenderingHints::EDITOR_RUNTIME_TEXT, row, row+1);

‎src/EditableLargeNumberMenuItem.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ class EditableLargeNumberMenuItem : public EditableMultiPartMenuItem {
221221
void setLargeNumberFromString(const char* largeNum);
222222

223223
bool isNegativeAllowed() const { return negativeAllowed; }
224+
void setNegativeFlagValue(int neg);
225+
224226
};
225227

226228
#endif //_EDITABLE_LARGE_NUMBER_MENU_ITEM_H_

‎src/graphics/BaseGraphicalRenderer.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ GridPositionRowCacheEntry* BaseGraphicalRenderer::firstItemOnRow(int row) {
365365

366366
int BaseGraphicalRenderer::totalGridHeight() const {
367367
int totalItemHeights = 0;
368-
for(int i=0; i<itemOrderByRow.count(); i++) {
368+
for(size_t i=0; i<itemOrderByRow.count(); i++) {
369369
auto rowEntry = itemOrderByRow.itemAtIndex(i);
370370
if(rowEntry == nullptr || rowEntry->getPosition().getGridPosition() > 1) continue;
371371
auto itemHeight = rowEntry->getPosition().getGridHeight();
@@ -492,7 +492,7 @@ int BaseGraphicalRenderer::skipRow(int idx) {
492492
while(currentRow == startingRow && toSkip < 5) {
493493
toSkip++;
494494
idx++;
495-
if(idx >= itemOrderByRow.count()) break;
495+
if(static_cast<size_t>(idx) >= itemOrderByRow.count()) break;
496496
currentRow = itemOrderByRow.itemAtIndex(idx)->getPosition().getRow();
497497
}
498498
return toSkip;

‎src/graphics/GraphicsDeviceRenderer.cpp‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ namespace tcgfx {
5858
void GraphicsDeviceRenderer::setHasTouchInterface(bool hasTouch) {
5959
bitWrite(flags, GFX_USING_TOUCH_INTERFACE, hasTouch);
6060
if (!touchKeyboardController) {
61-
const auto props = getGraphicsPropertiesFactory().configFor(nullptr,ItemDisplayProperties::COMPTYPE_ACTION);
62-
touchKeyboardController = defaultTouchKeyboardController(rootDrawable->getDisplayDimensions(),*this, *props);
61+
touchKeyboardController = defaultTouchKeyboardController(rootDrawable->getDisplayDimensions(),*this);
6362
}
6463
}
6564

‎src/graphics/MenuTouchScreenEncoder.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ void MenuTouchScreenEncoder::touched(const TouchNotification &evt) {
120120
}
121121
else if(isMenuRuntimeMultiEdit(theItem) && !theItem->isReadOnly()) {
122122
serlogF(SER_TCMENU_DEBUG, "TMPart");
123-
renderer->getTouchKeyboardController()->startEditing(theItem, nullptr);
123+
renderer->getTouchKeyboardController()->startEditing(theItem);
124124
}
125125
else if(!theItem->isReadOnly()){
126126
GridPosition::GridDrawingMode drawingMode = evt.getEntry()->getPosition().getDrawingMode();

‎src/graphics/TcTouchKeyboardController.cpp‎

Lines changed: 102 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,76 @@ void TouchKeyboardLayout::calculateKeyHeight(DeviceDrawableHelper& helper) {
5252
if (actualRows && eachKeyHeight > size.y / actualRows) eachKeyHeight = size.y / actualRows;
5353
}
5454

55+
SingleValuePerFieldTouchEditor* SingleValuePerFieldTouchEditor::initSingle(MenuItem* item) {
56+
initFor(item);
57+
if (item->getMenuType() == MENUTYPE_LARGENUM_VALUE) {
58+
if (auto numItem = reinterpret_cast<EditableLargeNumberMenuItem*>(item); numItem->isNegativeAllowed()) {
59+
numItem->nextPart();
60+
}
61+
}
62+
63+
return this;
64+
}
65+
5566
void SingleValuePerFieldTouchEditor::keypress(TcTouchKeyboardController* controller, char ch) {
67+
bool moreToGo = true;
5668
if (menuItem->getMenuType() == MENUTYPE_TEXT_VALUE) {
57-
const auto txtItem = reinterpret_cast<TextMenuItem*>(menuItem);
58-
txtItem->valueChangedFromKeyboard(ch);
59-
} else {
60-
menuItem->valueChanged(ch);
69+
if (ch == 0xFE) {
70+
capsLock = true;
71+
} else {
72+
const auto txtItem = reinterpret_cast<TextMenuItem*>(menuItem);
73+
if (capsLock) {
74+
ch = toupper(ch);
75+
}
76+
txtItem->valueChangedFromKeyboard(ch);
77+
moreToGo = menuItem->nextPart() != 0;
78+
}
79+
} else if (menuItem->getMenuType() == MENUTYPE_LARGENUM_VALUE) {
80+
const auto lgeItem = reinterpret_cast<EditableLargeNumberMenuItem*>(menuItem);
81+
if (ch == '+' && lgeItem->isNegativeAllowed()) {
82+
const auto neg = lgeItem->getLargeNumber()->isNegative() ? 0 : 1;
83+
lgeItem->setNegativeFlagValue(neg);
84+
} else if (ch == '>') {
85+
moreToGo = menuItem->nextPart() != 0;
86+
} else if (ch >= '0' && ch <= '9') {
87+
menuItem->valueChanged(ch);
88+
moreToGo = menuItem->nextPart() != 0;
89+
}
6190
}
62-
bool moreToGo = menuItem->nextPart() != 0;
6391
controller->needsRedraw(moreToGo);
6492
}
6593

94+
SummingValueKeyboardTouchEditor* SummingValueKeyboardTouchEditor::initSumming(MenuItem* item) {
95+
initFor(item);
96+
currentValueBeingEdited = 0;
97+
return this;
98+
}
99+
100+
void SummingValueKeyboardTouchEditor::keypress(TcTouchKeyboardController* controller, char ch) {
101+
const auto range = menuItem->getCurrentRange();
102+
if (ch == '<') {
103+
currentValueBeingEdited = 0;
104+
menuItem->valueChanged(0);
105+
controller->needsRedraw(true);
106+
return;
107+
} else if (ch == '>' || ch == '.') {
108+
currentValueBeingEdited = 0;
109+
const bool moreToGo = menuItem->nextPart() != 0;
110+
controller->needsRedraw(moreToGo);
111+
return;
112+
} else if (ch < '0' || ch > '9') return;
113+
114+
const auto num = ch - '0';
115+
currentValueBeingEdited = currentValueBeingEdited * 10 + num;
116+
if (range < currentValueBeingEdited) {
117+
currentValueBeingEdited = 0;
118+
controller->needsRedraw(true);
119+
} else {
120+
menuItem->valueChanged(currentValueBeingEdited);
121+
controller->needsRedraw(true);
122+
}
123+
}
124+
66125
void TouchKeyboardRow::drawRow(GraphicsDeviceRenderer& renderer, ItemDisplayProperties& properties, int yLocationStart, int height) {
67126
auto* drawable = renderer.getDeviceDrawable();
68127
DeviceDrawableHelper helper(drawable);
@@ -82,9 +141,9 @@ void TouchKeyboardRow::drawRow(GraphicsDeviceRenderer& renderer, ItemDisplayProp
82141
}
83142

84143

85-
TcTouchKeyboardController::TcTouchKeyboardController(TouchKeyboardLayout* defKeyboard, GraphicsDeviceRenderer& renderer, ItemDisplayProperties& properties) :
144+
TcTouchKeyboardController::TcTouchKeyboardController(TouchKeyboardLayout* numKeyboard, TouchKeyboardLayout* txtKeyboard, GraphicsDeviceRenderer& renderer) :
86145
upButton(&drawUpArrowLgeBitmap), downButton(&drawDownArrowLgeBitmap), keyboardLayout(nullptr),
87-
touchRenderer(renderer), properties(properties), defKeyboard(defKeyboard) {
146+
touchRenderer(renderer), defNumKeyboard(numKeyboard), defTxtKeyboard(txtKeyboard) {
88147
}
89148

90149
void TcTouchKeyboardController::started(BaseMenuRenderer* currentRenderer) {
@@ -100,6 +159,7 @@ void TcTouchKeyboardController::started(BaseMenuRenderer* currentRenderer) {
100159
}
101160

102161
void TcTouchKeyboardController::reset() {
162+
stopEditing();
103163
}
104164

105165
void TcTouchKeyboardController::renderLoop(unsigned int currentValue, RenderPressMode userClick) {
@@ -113,16 +173,14 @@ void TcTouchKeyboardController::renderLoop(unsigned int currentValue, RenderPres
113173
}
114174
}
115175

116-
void TcTouchKeyboardController::startEditing(MenuItem* menuItem, KeyboardTouchEditor* actEditor) {
176+
void TcTouchKeyboardController::startEditing(MenuItem* menuItem) {
117177
if (menuItem == nullptr) {
118178
serlogF(SER_WARNING, "TchEd-null");
119179
return;
120-
} else if (actEditor) {
121-
itemEditor = actEditor;
122180
} else if (menuItem->getMenuType() == MENUTYPE_TEXT_VALUE || menuItem->getMenuType() == MENUTYPE_LARGENUM_VALUE) {
123-
itemEditor = perCharEditor.initFor(menuItem);
181+
itemEditor = perCharEditor.initSingle(menuItem);
124182
} else if (menuItem->getMenuType() == MENUTYPE_DATE || menuItem->getMenuType() == MENUTYPE_IPADDRESS || menuItem->getMenuType() == MENUTYPE_COLOR_VALUE || menuItem->getMenuType() == MENUTYPE_TIME) {
125-
itemEditor = summingEditor.initFor(menuItem);
183+
itemEditor = summingEditor.initSumming(menuItem);
126184
} else {
127185
// it has been called with an unsupported menu type, only runtime editable items can use this path
128186
return;
@@ -133,7 +191,7 @@ void TcTouchKeyboardController::startEditing(MenuItem* menuItem, KeyboardTouchEd
133191
menuMgr.setEditorHintsLocked(false);
134192
menuMgr.setCurrentEditor(menuItem);
135193
itemBeingEdited = multi;
136-
keyboardLayout = defKeyboard;
194+
keyboardLayout = (menuItem->getMenuType() == MENUTYPE_TEXT_VALUE) ? defTxtKeyboard : defNumKeyboard;
137195

138196
// here we take over the display to render a keyboard
139197
touchRenderer.setCustomDrawingHandler(this);
@@ -149,6 +207,9 @@ void TcTouchKeyboardController::renderItem() {
149207
DeviceDrawableHelper helper(touchRenderer.getDeviceDrawable());
150208
char buffer[64] = "Edit ";
151209
itemBeingEdited->copyNameToBuffer(buffer, 5, sizeof buffer);
210+
if (itemEditor->isCapsDown()) {
211+
appendChar(buffer, '^', sizeof buffer);
212+
}
152213
helper.setFontFromParameters(titleProps->getFont(), titleProps->getFontMagnification());
153214
int baseline;
154215
int titleHeight = helper.textExtents(buffer, &baseline).y;
@@ -161,8 +222,8 @@ void TcTouchKeyboardController::renderItem() {
161222
auto& hints = menuMgr.getEditorHints();
162223
if (hints.getEditorRenderingType() == CurrentEditorRenderingHints::EDITOR_RUNTIME_TEXT) {
163224
auto length = strlen(buffer);
164-
const int start = internal_min(length, internal_max(0, hints.getStartIndex()));
165-
const int end = internal_min(length, internal_max(start, hints.getEndIndex()));
225+
const int start = internal_min((int)length, internal_max(0, hints.getStartIndex()));
226+
const int end = internal_min((int)length, internal_max(start, hints.getEndIndex()));
166227
char saved = buffer[start];
167228
buffer[start] = 0;
168229
int startX = helper.textExtents(buffer, &baseline).x;
@@ -200,24 +261,31 @@ void TcTouchKeyboardController::needsRedraw(bool moreToGo) {
200261
}
201262
}
202263

203-
void TcTouchKeyboardController::renderKeyboard() {
204-
auto* layout = keyboardLayout ? keyboardLayout : defKeyboard;
264+
void TcTouchKeyboardController::renderKeyboard() const {
265+
auto* layout = keyboardLayout ? keyboardLayout : defNumKeyboard;
205266
if (layout && touchRenderer.getDeviceDrawable()) {
267+
auto& factory = touchRenderer.getGraphicsPropertiesFactory();
268+
const auto location = keyboardLayout->getKeysPerRow() > 5 ? ItemDisplayProperties::COMPTYPE_ITEM : ItemDisplayProperties::COMPTYPE_TITLE;
269+
auto* properties = factory.configFor(itemBeingEdited, location);
270+
206271
DeviceDrawableHelper helper(touchRenderer.getDeviceDrawable());
207-
helper.setFontFromParameters(properties.getFont(), properties.getFontMagnification());
272+
helper.setFontFromParameters(properties->getFont(), properties->getFontMagnification());
208273
layout->calculateKeyHeight(helper);
209-
layout->drawRows(touchRenderer, properties, touchRenderer.getHeight() - layout->getRequiredHeight());
274+
layout->drawRows(touchRenderer, *properties, touchRenderer.getHeight() - layout->getRequiredHeight());
210275
}
211276
}
212277

213278
void TcTouchKeyboardController::drawScrollbar(DeviceDrawable* drawable, const Coord& displayDims, int titleHeight) {
279+
auto& factory = touchRenderer.getGraphicsPropertiesFactory();
280+
auto* properties = factory.configFor(nullptr, ItemDisplayProperties::COMPTYPE_TITLE);
281+
214282
const auto yHei = displayDims.y - titleHeight;
215283
const auto xLoc = displayDims.x - TC_KEYBOARD_BUTTON_SIZE;
216284
upButton.setPositionAndSize(Coord(xLoc, titleHeight), Coord(TC_KEYBOARD_BUTTON_SIZE, yHei / 2));
217-
upButton.setColors(properties.getColor(ItemDisplayProperties::TEXT), properties.getColor(ItemDisplayProperties::BACKGROUND), properties.getColor(ItemDisplayProperties::HIGHLIGHT1));
285+
upButton.setColors(properties->getColor(ItemDisplayProperties::TEXT), properties->getColor(ItemDisplayProperties::BACKGROUND), properties->getColor(ItemDisplayProperties::HIGHLIGHT1));
218286
upButton.paintButton(drawable);
219287
downButton.setPositionAndSize(Coord(xLoc, titleHeight + yHei / 2), Coord(TC_KEYBOARD_BUTTON_SIZE, yHei / 2));
220-
downButton.setColors(properties.getColor(ItemDisplayProperties::TEXT), properties.getColor(ItemDisplayProperties::BACKGROUND), properties.getColor(ItemDisplayProperties::HIGHLIGHT1));
288+
downButton.setColors(properties->getColor(ItemDisplayProperties::TEXT), properties->getColor(ItemDisplayProperties::BACKGROUND), properties->getColor(ItemDisplayProperties::HIGHLIGHT1));
221289
downButton.paintButton(drawable);
222290
}
223291

@@ -241,9 +309,19 @@ void TcTouchKeyboardController::stopEditing() {
241309
itemBeingEdited = nullptr;
242310
}
243311

244-
TcTouchKeyboardController* tcgfx::defaultTouchKeyboardController(const Coord& size, GraphicsDeviceRenderer& renderer, ItemDisplayProperties& properties) {
312+
TcTouchKeyboardController* tcgfx::defaultTouchKeyboardController(const Coord& size, GraphicsDeviceRenderer& renderer) {
245313
auto* layout = new TouchKeyboardLayout(size);
246-
layout->withRow("789>", 4)->withRow("456<", 4)->withRow("123X", 4)->withRow(".0+", 3);
247-
auto* touchKeyboard = new TcTouchKeyboardController(layout, renderer, properties);
314+
auto* layoutText = new TouchKeyboardLayout(size);
315+
layout->withRow("789>", 4)
316+
->withRow("456<", 4)
317+
->withRow("123X", 4)
318+
->withRow(".0+", 3);
319+
layoutText->withRow("1234567890", 10)
320+
->withRow("qwertyuiop", 10)
321+
->withRow("asdfghjkl!", 10)
322+
->withRow("zxcvbnm,.?", 10)
323+
->withRow(" *#@$\0xFE", 6);
324+
325+
auto* touchKeyboard = new TcTouchKeyboardController(layout, layoutText, renderer);
248326
return touchKeyboard;
249327
}

‎src/graphics/TcTouchKeyboardController.h‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@
2020
namespace tcgfx {
2121
class TcTouchKeyboardController;
2222

23+
constexpr uint8_t noKeyPressed = 0xFF;
24+
constexpr uint8_t capsKey = 0xFE;
25+
2326
class TouchKeyboardRow {
24-
char column[12] = {};
27+
char column[10] = {};
2528
uint8_t actualColumns = 0;
2629
int eachKeySize = 0;
2730
uint8_t pressedKey = 0;
@@ -41,10 +44,11 @@ namespace tcgfx {
4144
int getTextHeight(DeviceDrawableHelper& helper) const;
4245

4346
uint8_t touchInRow(const Coord& touch, bool touched);
47+
[[nodiscard]] uint8_t getNumKeys() const { return actualColumns; }
4448
};
4549

4650
class TouchKeyboardLayout {
47-
TouchKeyboardRow rows[4];
51+
TouchKeyboardRow rows[5];
4852
Coord size;
4953
int16_t eachKeyHeight;
5054
int16_t screenHeight;
@@ -53,7 +57,7 @@ namespace tcgfx {
5357
explicit TouchKeyboardLayout(const Coord& size) : rows{}, size(size), eachKeyHeight(), screenHeight(size.y),actualRows(0) {}
5458

5559
TouchKeyboardLayout* withRow(const char* rowKeys, uint8_t keys) {
56-
if (actualRows < 4 && keys > 0 && keys <= 12) {
60+
if (actualRows < 5 && keys > 0 && keys <= 12) {
5761
rows[actualRows] = TouchKeyboardRow(rowKeys, keys, size.x);
5862
actualRows++;
5963
}
@@ -81,39 +85,39 @@ namespace tcgfx {
8185
[[nodiscard]] int getRequiredHeight() const {
8286
return actualRows * eachKeyHeight;
8387
}
88+
89+
[[nodiscard]] int getKeysPerRow() const {
90+
return rows[0].getNumKeys();
91+
}
8492
};
8593

8694
class KeyboardTouchEditor {
8795
protected:
8896
EditableMultiPartMenuItem* menuItem = nullptr;
89-
int editIndex = 0;
97+
bool capsLock = false;
9098
public:
9199
virtual void keypress(TcTouchKeyboardController* controller, char ch) = 0;
92-
KeyboardTouchEditor* initFor(MenuItem* m) {
100+
void initFor(MenuItem* m) {
93101
menuItem = reinterpret_cast<EditableMultiPartMenuItem*>(m);
94-
editIndex = 0;
95102
menuItem->beginMultiEdit();
96103
menuItem->nextPart();
97-
return this;
98104
}
99105

100106
virtual ~KeyboardTouchEditor() = default;
107+
[[nodiscard]] bool isCapsDown() const { return capsLock; }
101108
};
102109

103110
class SingleValuePerFieldTouchEditor : public KeyboardTouchEditor {
104111
public:
105-
void initSingle(MenuItem* item) { initFor(item); }
112+
SingleValuePerFieldTouchEditor* initSingle(MenuItem* item);
106113
void keypress(TcTouchKeyboardController* controller, char ch) override;
107114
};
108115

109116
class SummingValueKeyboardTouchEditor : public KeyboardTouchEditor {
110117
int currentValueBeingEdited = 0;
111118
public:
112-
void initSumming(MenuItem* item) {
113-
initFor(item);
114-
currentValueBeingEdited = 0;
115-
}
116-
void keypress(TcTouchKeyboardController* controller, char ch) override {}
119+
SummingValueKeyboardTouchEditor* initSumming(MenuItem* item);
120+
void keypress(TcTouchKeyboardController* controller, char ch) override;
117121
};
118122

119123
class TcTouchKeyboardController : public CustomDrawing, public TouchObserver {
@@ -124,20 +128,19 @@ namespace tcgfx {
124128
KeyboardTouchEditor* itemEditor{};
125129
EditableMultiPartMenuItem* itemBeingEdited{};
126130
GraphicsDeviceRenderer& touchRenderer;
127-
ItemDisplayProperties& properties;
128131

129132
// these are the default keyboard and editor layouts that are built in
130-
TouchKeyboardLayout* defKeyboard;
133+
TouchKeyboardLayout *defNumKeyboard, *defTxtKeyboard;
131134
SingleValuePerFieldTouchEditor perCharEditor;
132135
SummingValueKeyboardTouchEditor summingEditor;
133136
bool dirty = false;
134137
public:
135-
TcTouchKeyboardController(TouchKeyboardLayout* defKeyboard, GraphicsDeviceRenderer& renderer, ItemDisplayProperties& properties);
138+
TcTouchKeyboardController(TouchKeyboardLayout* defKeyboard, TouchKeyboardLayout* txtKeyboard, GraphicsDeviceRenderer& renderer);
136139
void setTouchScreenManager(MenuTouchScreenManager* touchScreenManager) { this->touchScreenManager = touchScreenManager; }
137140
void started(BaseMenuRenderer* currentRenderer) override;
138141
void reset() override;
139142
void renderLoop(unsigned int currentValue, RenderPressMode userClick) override;
140-
void startEditing(MenuItem* menuItem, KeyboardTouchEditor* actEditor);
143+
void startEditing(MenuItem* menuItem);
141144
void drawScrollbar(DeviceDrawable* drawable, const Coord& displayDims, int titleHeight);
142145
void reportTouchUpDown(const Coord& touch) const;
143146
void stopEditing();
@@ -147,11 +150,11 @@ namespace tcgfx {
147150
void needsRedraw(bool moreToGo);
148151

149152
protected:
150-
void renderKeyboard();
153+
void renderKeyboard() const;
151154
void renderItem();
152155
};
153156

154-
TcTouchKeyboardController* defaultTouchKeyboardController(const Coord& size, GraphicsDeviceRenderer& renderer, ItemDisplayProperties& properties);
157+
TcTouchKeyboardController* defaultTouchKeyboardController(const Coord& size, GraphicsDeviceRenderer& renderer);
155158

156159
}
157160
#endif

0 commit comments

Comments
 (0)