Skip to content

Commit

Permalink
Add WidgetCharset class for character selection
Browse files Browse the repository at this point in the history
with cycling through charset and callback handling.

- Introduces WidgetCharset class inheriting BaseWidgetValue<char>.
- Allows user to select characters from a specified charset.
- Implements methods for cycling through characters, clearing value, and triggering callbacks.
  • Loading branch information
forntoh committed Oct 23, 2024
1 parent d2f055e commit 0d382c0
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/ItemWidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ struct make_index_sequence<0, Is...> {
using type = index_sequence<Is...>;
};

// Updated ItemWidget class supporting zero or more widgets
template <typename... Ts>
class ItemWidget : public BaseItemManyWidgets {
public:
Expand Down
106 changes: 106 additions & 0 deletions src/widget/WidgetCharset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Creator: @ShishkinDmitriy
#pragma once

#include "BaseWidgetValue.h"

/**
* @class WidgetCharset
* @brief Widget that allows user to select a character from a charset.
* Manages a character value within a specified charset, allowing cycling through characters.
*/
class WidgetCharset : public BaseWidgetValue<char> {
friend class ItemCharsetWidgets;

protected:
const char*& charset;
const bool cycle;
uint8_t charsetPosition = 0;

public:
WidgetCharset(
const char value,
const char*& charset,
const char* format,
const uint8_t cursorOffset,
const bool cycle,
void (*callback)(char))
: BaseWidgetValue<char>(value, format, cursorOffset, callback), charset(charset), cycle(cycle) {}

protected:
uint8_t draw(char* buffer, const uint8_t start) override {
if (start >= ITEM_DRAW_BUFFER_SIZE) return 0;
return snprintf(buffer + start, ITEM_DRAW_BUFFER_SIZE - start, format, value == '\0' ? ' ' : value);
}

/**
* @brief Process command.
*
* Handle commands:
* - `UP` - go to the next character in the charset and trigger callback
* - `DOWN` - go to the previous character in the charset and trigger callback
* - `BACKSPACE` - clear the value
*/
bool process(LcdMenu* menu, const unsigned char command) override {
MenuRenderer* renderer = menu->getRenderer();
if (renderer->isInEditMode()) {
switch (command) {
case UP:
if (nextChar()) {
value = charset[charsetPosition];
BaseWidgetValue<char>::handleChange();
printLog(F("WidgetCharset::nextChar"), value);
}
return true;
case DOWN:
if (previousChar()) {
value = charset[charsetPosition];
BaseWidgetValue<char>::handleChange();
printLog(F("WidgetCharset::previousChar"), value);
}
return true;
case BACKSPACE:
if (backspace()) {
printLog(F("WidgetCharset::backspace"));
BaseWidgetValue<char>::handleChange();
}
return true;
default:
return false;
}
}
return false;
}

bool backspace() {
if (value != '\0') {
value = '\0';
charsetPosition = 0;
return true;
}
return false;
}

bool nextChar() {
if (charset[charsetPosition + 1] != '\0') {
charsetPosition++;
return true;
}
if (cycle) {
charsetPosition = 0;
return true;
}
return false;
}

bool previousChar() {
if (charsetPosition > 0) {
charsetPosition--;
return true;
}
if (cycle) {
charsetPosition = strlen(charset) - 1;
return true;
}
return false;
}
};

0 comments on commit 0d382c0

Please sign in to comment.