Skip to content

Commit

Permalink
#71
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Nov 12, 2020
1 parent d48a963 commit 413fb45
Show file tree
Hide file tree
Showing 11 changed files with 181 additions and 23 deletions.
7 changes: 7 additions & 0 deletions src/eez/gui/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,13 @@ int getTextCursorPosition(Cursor cursor, int16_t id) {
return value.getType() == VALUE_TYPE_INT ? value.getInt() : -1;
}

int getXScroll(const WidgetCursor &widgetCursor) {
const Widget *widget = widgetCursor.widget;
Value value((void *)&widgetCursor, VALUE_TYPE_POINTER);
DATA_OPERATION_FUNCTION(widget->data, DATA_OPERATION_GET_X_SCROLL, widgetCursor.cursor, value);
return value.getType() == VALUE_TYPE_INT ? value.getInt() : 0;
}

} // namespace gui
} // namespace eez

Expand Down
4 changes: 3 additions & 1 deletion src/eez/gui/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,8 @@ enum DataOperationEnum {
DATA_OPERATION_YT_DATA_GET_CURSOR_X_VALUE,
DATA_OPERATION_YT_DATA_TOUCH_DRAG,
DATA_OPERATION_GET_CANVAS_DRAW_FUNCTION,
DATA_OPERATION_GET_TEXT_CURSOR_POSITION
DATA_OPERATION_GET_TEXT_CURSOR_POSITION,
DATA_OPERATION_GET_X_SCROLL
};

int count(int16_t id);
Expand Down Expand Up @@ -470,6 +471,7 @@ struct TouchDrag {
void ytDataTouchDrag(Cursor cursor, int16_t id, TouchDrag *touchDrag);

int getTextCursorPosition(Cursor cursor, int16_t id);
int getXScroll(const WidgetCursor &widgetCursor);

} // namespace gui
} // namespace eez
49 changes: 47 additions & 2 deletions src/eez/gui/draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void drawText(const char *text, int textLength, int x, int y, int w, int h, cons
bool active, bool blink, bool ignoreLuminocity,
uint16_t *overrideColor, uint16_t *overrideBackgroundColor,
uint16_t *overrideActiveColor, uint16_t *overrideActiveBackgroundColor,
bool useSmallerFontIfDoesNotFit, int cursorPosition) {
bool useSmallerFontIfDoesNotFit, int cursorPosition, int xScroll) {
int x1 = x;
int y1 = y;
int x2 = x + w - 1;
Expand Down Expand Up @@ -166,7 +166,7 @@ void drawText(const char *text, int textLength, int x, int y, int w, int h, cons
display::setColor(style->color, ignoreLuminocity);
}
}
display::drawStr(text, textLength, x_offset, y_offset, x1, y1, x2, y2, font, cursorPosition);
display::drawStr(text, textLength, x_offset - xScroll, y_offset, x1, y1, x2, y2, font, cursorPosition);
}

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -216,6 +216,51 @@ int getCharIndexAtPosition(int xPos, const char *text, int textLength, int x, in
return display::getCharIndexAtPosition(xPos, text, textLength, x_offset, y_offset, x1, y1, x2, y2, font);
}

int getCursorXPosition(int cursorPosition, const char *text, int textLength, int x, int y, int w, int h, const Style *style) {
int x1 = x;
int y1 = y;
int x2 = x + w - 1;
int y2 = y + h - 1;

if (style->border_size_top > 0 || style->border_size_right > 0 || style->border_size_bottom > 0 || style->border_size_left > 0) {
x1 += style->border_size_left;
y1 += style->border_size_top;
x2 -= style->border_size_right;
y2 -= style->border_size_bottom;
}

font::Font font = styleGetFont(style);

int width = display::measureStr(text, textLength, font, 0);
int height = font.getHeight();

int x_offset;
if (styleIsHorzAlignLeft(style)) {
x_offset = x1 + style->padding_left;
} else if (styleIsHorzAlignRight(style)) {
x_offset = x2 - style->padding_right - width;
} else {
x_offset = x1 + ((x2 - x1 + 1) - width) / 2;
if (x_offset < x1) {
x_offset = x1;
}
}

int y_offset;
if (styleIsVertAlignTop(style)) {
y_offset = y1 + style->padding_top;
} else if (styleIsVertAlignBottom(style)) {
y_offset = y2 - style->padding_bottom - height;
} else {
y_offset = y1 + ((y2 - y1 + 1) - height) / 2;
}
if (y_offset < 0) {
y_offset = y1;
}

return display::getCursorXPosition(cursorPosition, text, textLength, x_offset, y_offset, x1, y1, x2, y2, font);
}

////////////////////////////////////////////////////////////////////////////////

static const unsigned int CONF_MULTILINE_TEXT_MAX_LINE_LENGTH = 100;
Expand Down
3 changes: 2 additions & 1 deletion src/eez/gui/draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ namespace gui {
font::Font styleGetFont(const Style *style);
bool styleIsBlink(const Style *style);

void drawText(const char *text, int textLength, int x, int y, int w, int h, const Style *style, bool active, bool blink, bool ignoreLuminocity, uint16_t *overrideColor, uint16_t *overrideBackgroundColor, uint16_t *overrideActiveColor, uint16_t *overrideActiveBackgroundColor, bool useSmallerFontIfDoesNotFit = false, int cursorPosition = -1);
void drawText(const char *text, int textLength, int x, int y, int w, int h, const Style *style, bool active, bool blink, bool ignoreLuminocity, uint16_t *overrideColor, uint16_t *overrideBackgroundColor, uint16_t *overrideActiveColor, uint16_t *overrideActiveBackgroundColor, bool useSmallerFontIfDoesNotFit = false, int cursorPosition = -1, int xScroll = 0);
int getCharIndexAtPosition(int xPos, const char *text, int textLength, int x, int y, int w, int h, const Style *style);
int getCursorXPosition(int cursorPosition, const char *text, int textLength, int x, int y, int w, int h, const Style *style);
void drawMultilineText(const char *text, int x, int y, int w, int h, const Style *style, bool active, int firstLineIndent, int hangingIndent);
int measureMultilineText(const char *text, int x, int y, int w, int h, const Style *style, int firstLineIndent, int hangingIndent);
void drawBitmap(Image *image, int x, int y, int w, int h, const Style *style, bool active);
Expand Down
68 changes: 62 additions & 6 deletions src/eez/gui/widgets/display_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct DisplayDataState {
int16_t cursorPosition;
uint32_t cursorBlinkStartTime;
uint8_t cursorVisible;
uint8_t xScroll;
};

FixPointersFunctionType DISPLAY_DATA_fixPointers = nullptr;
Expand Down Expand Up @@ -109,6 +110,8 @@ DrawFunctionType DISPLAY_DATA_draw = [](const WidgetCursor &widgetCursor) {
currentState->activeBackgroundColor = widgetCursor.currentState->flags.focused ? style->focus_color : getActiveBackgroundColor(widgetCursor.cursor, widget->data, style);

currentState->cursorPosition = getTextCursorPosition(widgetCursor.cursor, widget->data);

currentState->xScroll = getXScroll(widgetCursor);

bool refresh =
!widgetCursor.previousState ||
Expand All @@ -120,7 +123,8 @@ DrawFunctionType DISPLAY_DATA_draw = [](const WidgetCursor &widgetCursor) {
currentState->backgroundColor != previousState->backgroundColor ||
currentState->activeColor != previousState->activeColor ||
currentState->activeBackgroundColor != previousState->activeBackgroundColor ||
currentState->cursorPosition != previousState->cursorPosition;
currentState->cursorPosition != previousState->cursorPosition ||
currentState->xScroll != previousState->xScroll;

if (currentState->cursorPosition != -1) {
if (refresh) {
Expand Down Expand Up @@ -174,10 +178,11 @@ DrawFunctionType DISPLAY_DATA_draw = [](const WidgetCursor &widgetCursor) {
}

drawText(start, length, widgetCursor.x, widgetCursor.y, (int)widget->w, (int)widget->h,
style, widgetCursor.currentState->flags.active,
widgetCursor.currentState->flags.blinking, false,
&currentState->color, &currentState->backgroundColor, &currentState->activeColor, &currentState->activeBackgroundColor,
widgetCursor.currentState->data.getType() == VALUE_TYPE_FLOAT, currentState->cursorVisible ? currentState->cursorPosition : -1);
style, widgetCursor.currentState->flags.active,
widgetCursor.currentState->flags.blinking, false,
&currentState->color, &currentState->backgroundColor, &currentState->activeColor, &currentState->activeBackgroundColor,
widgetCursor.currentState->data.getType() == VALUE_TYPE_FLOAT,
currentState->cursorVisible ? currentState->cursorPosition : -1, currentState->xScroll);
}
};

Expand Down Expand Up @@ -227,7 +232,58 @@ int DISPLAY_DATA_getCharIndexAtPosition(int xPos, const WidgetCursor &widgetCurs
length--;
}

return getCharIndexAtPosition(xPos, start, length, widgetCursor.x, widgetCursor.y, (int)widget->w, (int)widget->h, style);
int xScroll = getXScroll(widgetCursor);

return getCharIndexAtPosition(xPos, start, length, widgetCursor.x - xScroll, widgetCursor.y, (int)widget->w, (int)widget->h, style);
}

int DISPLAY_DATA_getCursorXPosition(int cursorPosition, const WidgetCursor &widgetCursor) {
const Widget *widget = widgetCursor.widget;
const DisplayDataWidget *display_data_widget = GET_WIDGET_PROPERTY(widget, specific, const DisplayDataWidget *);

const Style *style = getStyle(overrideStyleHook(widgetCursor, widget->style));

char text[64];
Value data = get(widgetCursor.cursor, widget->data);
data.toText(text, sizeof(text));

char *start = text;

if (display_data_widget->displayOption == DISPLAY_OPTION_INTEGER) {
int i = findStartOfFraction(text);
text[i] = 0;
} else if (display_data_widget->displayOption == DISPLAY_OPTION_FRACTION) {
int i = findStartOfFraction(text);
start = text + i;
} else if (display_data_widget->displayOption == DISPLAY_OPTION_FRACTION_AND_UNIT) {
int i = findStartOfFraction(text);
int k = findStartOfUnit(text, i);
if (i < k) {
start = text + i;
text[k] = 0;
} else {
strcpy(text, ".0");
}
} else if (display_data_widget->displayOption == DISPLAY_OPTION_UNIT) {
int i = findStartOfUnit(text, 0);
start = text + i;
} else if (display_data_widget->displayOption == DISPLAY_OPTION_INTEGER_AND_FRACTION) {
int i = findStartOfUnit(text, 0);
text[i] = 0;
}

// trim left
while (*start && *start == ' ') {
start++;
}

// trim right
int length = strlen(start);
while (length > 0 && start[length - 1] == ' ') {
length--;
}

return getCursorXPosition(cursorPosition, start, length, widgetCursor.x, widgetCursor.y, (int)widget->w, (int)widget->h, style);
}

OnTouchFunctionType DISPLAY_DATA_onTouch = nullptr;
Expand Down
1 change: 1 addition & 0 deletions src/eez/gui/widgets/display_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ namespace eez {
namespace gui {

int DISPLAY_DATA_getCharIndexAtPosition(int xPos, const WidgetCursor &widgetCursor);
int DISPLAY_DATA_getCursorXPosition(int cursorPosition, const WidgetCursor &widgetCursor);

} // namespace gui
} // namespace eez
25 changes: 22 additions & 3 deletions src/eez/modules/mcu/display.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,6 @@ void endBuffersDrawing() {
}

int getCharIndexAtPosition(int xPos, const char *text, int textLength, int x, int y, int clip_x1, int clip_y1, int clip_x2,int clip_y2, gui::font::Font &font) {
g_font = font;

if (textLength == -1) {
textLength = strlen(text);
}
Expand All @@ -611,7 +609,7 @@ int getCharIndexAtPosition(int xPos, const char *text, int textLength, int x, in
for (i = 0; i < textLength && text[i]; ++i) {
char encoding = text[i];
gui::font::Glyph glyph;
g_font.getGlyph(encoding, glyph);
font.getGlyph(encoding, glyph);
auto dx = 0;
if (glyph) {
dx = glyph.dx;
Expand All @@ -625,6 +623,27 @@ int getCharIndexAtPosition(int xPos, const char *text, int textLength, int x, in
return i;
}

int getCursorXPosition(int cursorPosition, const char *text, int textLength, int x, int y, int clip_x1, int clip_y1, int clip_x2,int clip_y2, gui::font::Font &font) {
if (textLength == -1) {
textLength = strlen(text);
}

for (int i = 0; i < textLength && text[i]; ++i) {
if (i == cursorPosition) {
return x;
}
char encoding = text[i];
gui::font::Glyph glyph;
font.getGlyph(encoding, glyph);
if (glyph) {
x += glyph.dx;
}
}

return x;
}


} // namespace display
} // namespace mcu
} // namespace eez
Expand Down
1 change: 1 addition & 0 deletions src/eez/modules/mcu/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ void bitBlt(void *src, void *dst, int sx, int sy, int sw, int sh, int dx, int dy
void drawBitmap(Image *image, int x, int y);
void drawStr(const char *text, int textLength, int x, int y, int clip_x1, int clip_y1, int clip_x2,int clip_y2, gui::font::Font &font, int cursorPosition);
int getCharIndexAtPosition(int xPos, const char *text, int textLength, int x, int y, int clip_x1, int clip_y1, int clip_x2,int clip_y2, gui::font::Font &font);
int getCursorXPosition(int cursorPosition, const char *text, int textLength, int x, int y, int clip_x1, int clip_y1, int clip_x2,int clip_y2, gui::font::Font &font);
int8_t measureGlyph(uint8_t encoding, gui::font::Font &font);
int measureStr(const char *text, int textLength, gui::font::Font &font, int max_width = 0);

Expand Down
5 changes: 5 additions & 0 deletions src/eez/modules/psu/gui/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1944,6 +1944,11 @@ void data_keypad_text(DataOperationEnum operation, Cursor cursor, Value &value)
if (keypad) {
value = keypad->getCursorPostion();
}
} else if (operation == DATA_OPERATION_GET_X_SCROLL) {
Keypad *keypad = getActiveKeypad();
if (keypad) {
value = keypad->getXScroll(*(WidgetCursor *)value.getVoidPointer());
}
}
}

Expand Down
36 changes: 27 additions & 9 deletions src/eez/modules/psu/gui/keypad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void onKeypadTextTouch(const WidgetCursor &widgetCursor, Event &touchEvent) {

Keypad *keypad = getActiveKeypad();
if (keypad) {
keypad->setCursorPostion(DISPLAY_DATA_getCharIndexAtPosition(touchEvent.x, widgetCursor));
keypad->setCursorPosition(DISPLAY_DATA_getCharIndexAtPosition(touchEvent.x, widgetCursor));
}
}

Expand All @@ -122,6 +122,7 @@ void Keypad::init(AppContext *appContext, const char *label_) {
m_label[0] = 0;
m_keypadText[0] = 0;
m_cursorPosition = 0;
m_xScroll = 0;
m_okCallback = 0;
m_cancelCallback = 0;
m_keypadMode = KEYPAD_MODE_LOWERCASE;
Expand Down Expand Up @@ -187,10 +188,10 @@ void Keypad::start(AppContext *appContext, const char *label, const char *text,

if (text) {
strcpy(m_keypadText, text);
m_cursorPosition = strlen(m_keypadText);
setCursorPosition(strlen(m_keypadText));
} else {
m_keypadText[0] = 0;
m_cursorPosition = 0;
setCursorPosition(0);
}
m_keypadMode = KEYPAD_MODE_LOWERCASE;
}
Expand All @@ -211,7 +212,8 @@ void Keypad::insertChar(char c) {
for (int i = n; i >= m_cursorPosition; i--) {
m_keypadText[i + 1] = m_keypadText[i];
}
m_keypadText[m_cursorPosition++] = c;
m_keypadText[m_cursorPosition] = c;
setCursorPosition(m_cursorPosition + 1);
m_lastKeyAppendTime = micros();
} else {
sound::playBeep();
Expand All @@ -237,7 +239,7 @@ void Keypad::back() {
for (int i = m_cursorPosition; i < n; i++) {
m_keypadText[i - 1] = m_keypadText[i];
}
m_cursorPosition--;
setCursorPosition(m_cursorPosition - 1);
m_keypadText[n - 1] = 0;
} else {
sound::playBeep();
Expand All @@ -246,7 +248,7 @@ void Keypad::back() {

void Keypad::clear() {
m_keypadText[0] = 0;
m_cursorPosition = 0;
setCursorPosition(0);
}

void Keypad::sign() {
Expand Down Expand Up @@ -294,10 +296,26 @@ int Keypad::getCursorPostion() {
return m_cursorPosition;
}

void Keypad::setCursorPostion(int cursorPosition) {
void Keypad::setCursorPosition(int cursorPosition) {
m_cursorPosition = cursorPosition;
}

int Keypad::getXScroll(const WidgetCursor &widgetCursor) {
int x = DISPLAY_DATA_getCursorXPosition(m_cursorPosition, widgetCursor);

x -= widgetCursor.x;

static const int CURSOR_WIDTH = 2;

if (x < m_xScroll) {
m_xScroll = MAX(x - widgetCursor.widget->w / 2, 0);
} else if (m_xScroll + widgetCursor.widget->w < x + CURSOR_WIDTH) {
m_xScroll = x + CURSOR_WIDTH - widgetCursor.widget->w;
}

return m_xScroll;
}

////////////////////////////////////////////////////////////////////////////////

NumericKeypadOptions::NumericKeypadOptions() {
Expand Down Expand Up @@ -795,7 +813,7 @@ void NumericKeypad::dot() {
void NumericKeypad::reset() {
m_state = m_startValue.getType() != VALUE_TYPE_NONE ? START : EMPTY;
m_keypadText[0] = 0;
m_cursorPosition = 0;
setCursorPosition(0);
}

void NumericKeypad::key(char ch) {
Expand Down Expand Up @@ -939,7 +957,7 @@ void NumericKeypad::ok() {
m_okUint32Callback(ipAddress);
m_state = START;
m_keypadText[0] = 0;
m_cursorPosition = 0;
setCursorPosition(0);
} else {
errorMessage("Invalid IP address format!");
}
Expand Down
Loading

0 comments on commit 413fb45

Please sign in to comment.