Skip to content

Commit

Permalink
widget state constructor and compare
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Dec 5, 2021
1 parent 1e213e8 commit 6c3b15f
Show file tree
Hide file tree
Showing 55 changed files with 547 additions and 405 deletions.
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,16 @@ file(GLOB header_eez_gui_widgets
list (APPEND header_files ${header_eez_gui_widgets})
source_group("eez\\gui\\widgets" FILES ${src_eez_gui_widgets} ${header_eez_gui_widgets})

file(GLOB src_eez_gui_widgets_containers
"src/eez/gui/widgets/containers/*.cpp"
)
list (APPEND src_files ${src_eez_gui_widgets_containers})
file(GLOB header_eez_gui_widgets_containers
"src/eez/gui/widgets/containers/*.h"
)
list (APPEND header_files ${header_eez_gui_widgets_containers})
source_group("eez\\gui\\widgets\\containers" FILES ${src_eez_gui_widgets_containers} ${header_eez_gui_widgets_containers})

file(GLOB src_bb3_flow
"src/bb3/flow/*.cpp"
)
Expand Down
2 changes: 1 addition & 1 deletion src/bb3/psu/gui/data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
#include <bb3/fs_driver.h>

#include <eez/gui/gui.h>
#include <eez/gui/widgets/container.h>
#include <eez/gui/widgets/containers/container.h>
#include <eez/gui/widgets/yt_graph.h>

#if OPTION_FAN
Expand Down
9 changes: 3 additions & 6 deletions src/bb3/psu/gui/page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,16 +674,14 @@ void MenuWithButtonsPage::updateInternalPage(const WidgetCursor &widgetCursor2,
widgetCursor.y = y + m_containerRectangleWidget.y;
currentState->flags.active = 0;

RectangleWidgetState rectangleWidgetState;
rectangleWidgetState.widgetCursor = widgetCursor;
RectangleWidgetState rectangleWidgetState(widgetCursor);
rectangleWidgetState.draw(previousState);

widgetCursor.widget = &m_messageTextWidget;
widgetCursor.x = x + m_messageTextWidget.x;
widgetCursor.y = y + m_messageTextWidget.y;
currentState->flags.active = 0;
TextWidgetState textWidgetState;
textWidgetState.widgetCursor = widgetCursor;
TextWidgetState textWidgetState(widgetCursor);
textWidgetState.draw(previousState);
}

Expand All @@ -693,8 +691,7 @@ void MenuWithButtonsPage::updateInternalPage(const WidgetCursor &widgetCursor2,
widgetCursor.y = y + m_buttonTextWidgets[i].y;
widgetCursor.cursor = i;
currentState->flags.active = isActiveWidget(widgetCursor);
TextWidgetState textWidgetState;
textWidgetState.widgetCursor = widgetCursor;
TextWidgetState textWidgetState(widgetCursor);
textWidgetState.draw(previousState);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/eez/flow/flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
#include <eez/gui/gui.h>
#include <eez/gui/widgets/input.h>

#include <eez/gui/widgets/layout_view.h>
#include <eez/gui/widgets/containers/layout_view.h>

#include <eez/flow/flow.h>
#include <eez/flow/components.h>
Expand Down
2 changes: 1 addition & 1 deletion src/eez/gui/overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/

#include <eez/gui/gui.h>
#include <eez/gui/widgets/container.h>
#include <eez/gui/widgets/containers/container.h>

namespace eez {
namespace gui {
Expand Down
4 changes: 0 additions & 4 deletions src/eez/gui/page.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@

#pragma once

#include <eez/gui/widgets/text.h>
#include <eez/gui/widgets/rectangle.h>

namespace eez {
namespace gui {

Expand Down Expand Up @@ -69,7 +66,6 @@ class InternalPage : public Page {

protected:
Widget widget;
WidgetState widgetState;
};

} // namespace gui
Expand Down
21 changes: 13 additions & 8 deletions src/eez/gui/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,22 @@ bool g_isActiveWidget;

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

struct NoneWidgetState : public WidgetState {};
struct ReservedWidgetState : public WidgetState {};
struct NoneWidgetState : public WidgetState {
NoneWidgetState(const WidgetCursor &widgetCursor) : WidgetState(widgetCursor) {
}
};

struct ReservedWidgetState : public WidgetState {
ReservedWidgetState(const WidgetCursor &widgetCursor) : WidgetState(widgetCursor) {
}
};

#define WIDGET_TYPE(NAME_PASCAL_CASE, NAME, ID) \
void NAME##placementNew(void *ptr) { new (ptr) NAME_PASCAL_CASE##WidgetState; }
void NAME##placementNew(void *ptr, const WidgetCursor& widgetCursor) { new (ptr) NAME_PASCAL_CASE##WidgetState(widgetCursor); }
WIDGET_TYPES
#undef WIDGET_TYPE

typedef void (*WidgetStatePlacementNewFunctionType)(void *ptr);
typedef void (*WidgetStatePlacementNewFunctionType)(void *ptr, const WidgetCursor& widgetCursor);

#define WIDGET_TYPE(NAME_PASCAL_CASE, NAME, ID) NAME##placementNew,
static WidgetStatePlacementNewFunctionType g_widgetStatePlacementNewFunctions[] = {
Expand Down Expand Up @@ -140,11 +147,9 @@ void enumWidget(WidgetCursor &widgetCursor, WidgetState *currentState, WidgetSta

auto widgetState = currentState;

g_widgetStatePlacementNewFunctions[widget->type](widgetState);
g_widgetStatePlacementNewFunctions[widget->type](widgetState, widgetCursor);

widgetState->widgetCursor = widgetCursor;
widgetState->widgetStateSize = g_widgetStateSize[widget->type];
widgetState->flags.active = g_isActiveWidget;
widgetState->widgetCursor.x += widget->x;
widgetState->widgetCursor.y += widget->y;

Expand All @@ -159,7 +164,7 @@ void enumNoneWidget(WidgetCursor &widgetCursor, WidgetState *currentState, Widge

auto widgetState = currentState;

g_widgetStatePlacementNewFunctions[widget->type](widgetState);
g_widgetStatePlacementNewFunctions[widget->type](widgetState, widgetCursor);

widgetState->widgetCursor = widgetCursor;
widgetState->widgetCursor.widget = widget;
Expand Down
8 changes: 4 additions & 4 deletions src/eez/gui/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,14 +171,16 @@ struct WidgetCursor {
bool isPage() const;
};

extern bool g_isActiveWidget;

struct WidgetState {
WidgetCursor widgetCursor;
size_t widgetStateSize;
WidgetStateFlags flags;
Value data;

WidgetState() {
flags.active = 0;
WidgetState(const WidgetCursor& widgetCursor_) : widgetCursor(widgetCursor_) {
flags.active = g_isActiveWidget;
flags.focused = 0;
flags.blinking = 0;
flags.enabled = 0;
Expand All @@ -199,8 +201,6 @@ struct WidgetState {

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

extern bool g_isActiveWidget;

#define nextWidgetState(p) (WidgetState *)(((uint8_t *)p) + p->widgetStateSize)

void enumWidget(WidgetCursor &widgetCursor, WidgetState *currentState, WidgetState *previousState);
Expand Down
61 changes: 16 additions & 45 deletions src/eez/gui/widgets/bar_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,56 +66,27 @@ void drawLineInBarGraphWidget(const BarGraphWidget *barGraphWidget, int p, uint1
}
}

void BarGraphWidgetState::draw(WidgetState *previousStateBase) {
auto previousState = (BarGraphWidgetState *)previousStateBase;
auto widget = (const BarGraphWidget *)widgetCursor.widget;
const Style* style = getStyle(overrideStyleHook(widgetCursor, widget->style));

flags.blinking = g_isBlinkTime && isBlinking(widgetCursor, widget->data);
data = get(widgetCursor, widget->data);

color = getColor(widgetCursor, widget->data, style);
backgroundColor = getBackgroundColor(widgetCursor, widget->data, style);
activeColor = getActiveColor(widgetCursor, widget->data, style);
activeBackgroundColor = getActiveBackgroundColor(widgetCursor, widget->data, style);

line1Data = get(widgetCursor, widget->line1Data);

line2Data = get(widgetCursor, widget->line2Data);

uint32_t currentTime = millis();
textData = data;
bool refreshTextData;
if (previousState) {
refreshTextData = textData != previousState->textData;
if (refreshTextData) {
uint32_t refreshRate = getTextRefreshRate(widgetCursor, widget->data);
if (refreshRate != 0) {
refreshTextData = (currentTime - previousState->textDataRefreshLastTime) > refreshRate;
if (!refreshTextData) {
textData = previousState->textData;
}
void BarGraphWidgetState::refreshTextData(BarGraphWidgetState *previousState) {
if (previousState && textData != previousState->textData) {
auto widget = (const BarGraphWidget *)widgetCursor.widget;
uint32_t refreshRate = getTextRefreshRate(widgetCursor, widget->data);
if (refreshRate != 0) {
if (textDataRefreshLastTime - previousState->textDataRefreshLastTime < refreshRate) {
textData = previousState->textData;
textDataRefreshLastTime = previousState->textDataRefreshLastTime;
}
}
} else {
refreshTextData = true;
}
textDataRefreshLastTime = refreshTextData ? currentTime : previousState->textDataRefreshLastTime;

bool refresh =
!previousState ||
previousState->flags.active != flags.active ||
previousState->flags.blinking != flags.blinking ||
previousState->data != data ||
color != previousState->color ||
backgroundColor != previousState->backgroundColor ||
activeColor != previousState->activeColor ||
activeBackgroundColor != previousState->activeBackgroundColor ||
previousState->line1Data != line1Data ||
previousState->line2Data != line2Data ||
refreshTextData;
}

void BarGraphWidgetState::draw(WidgetState *previousStateBase) {
auto previousState = (BarGraphWidgetState *)previousStateBase;
refreshTextData(previousState);
bool refresh = !previousState || *this != *previousState;
if (refresh) {
auto widget = (const BarGraphWidget *)widgetCursor.widget;
const Style* style = getStyle(overrideStyleHook(widgetCursor, widget->style));

int x = widgetCursor.x;
int y = widgetCursor.y;
const int w = widget->w;
Expand Down
37 changes: 37 additions & 0 deletions src/eez/gui/widgets/bar_graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,43 @@ struct BarGraphWidgetState : public WidgetState {
Value textData;
uint32_t textDataRefreshLastTime;

BarGraphWidgetState(const WidgetCursor &widgetCursor) : WidgetState(widgetCursor) {
auto widget = (const BarGraphWidget *)widgetCursor.widget;

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

flags.blinking = g_isBlinkTime && isBlinking(widgetCursor, widget->data);
data = get(widgetCursor, widget->data);

color = getColor(widgetCursor, widget->data, style);
backgroundColor = getBackgroundColor(widgetCursor, widget->data, style);
activeColor = getActiveColor(widgetCursor, widget->data, style);
activeBackgroundColor = getActiveBackgroundColor(widgetCursor, widget->data, style);

line1Data = get(widgetCursor, widget->line1Data);

line2Data = get(widgetCursor, widget->line2Data);

textData = data;
textDataRefreshLastTime = millis();
}

bool operator!=(const BarGraphWidgetState& previousState) {
return
flags.active != previousState.flags.active ||
flags.blinking != previousState.flags.blinking ||
data != previousState.data ||
color != previousState.color ||
backgroundColor != previousState.backgroundColor ||
activeColor != previousState.activeColor ||
activeBackgroundColor != previousState.activeBackgroundColor ||
line1Data != previousState.line1Data ||
line2Data != previousState.line2Data ||
textData != previousState.textData;
}

void refreshTextData(BarGraphWidgetState *previousState);

void draw(WidgetState *previousState) override;
};

Expand Down
13 changes: 4 additions & 9 deletions src/eez/gui/widgets/bitmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,11 @@
namespace eez {
namespace gui {

void BitmapWidgetState::draw(WidgetState *previousState) {
auto widget = (const BitmapWidget *)widgetCursor.widget;

data = widget->data ? getBitmapImage(widgetCursor, widget->data) : 0;

bool refresh = !previousState ||
previousState->flags.active != flags.active ||
data != previousState->data;

void BitmapWidgetState::draw(WidgetState *previousStateBase) {
auto previousState = (BitmapWidgetState *)previousStateBase;
bool refresh = !previousState || *this != *previousState;
if (refresh) {
auto widget = (const BitmapWidget *)widgetCursor.widget;
const Style* style = getStyle(widget->style);

const Bitmap *bitmap = nullptr;
Expand Down
12 changes: 12 additions & 0 deletions src/eez/gui/widgets/bitmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ struct BitmapWidget : public Widget {
};

struct BitmapWidgetState : public WidgetState {
BitmapWidgetState(const WidgetCursor &widgetCursor) : WidgetState(widgetCursor) {
auto widget = (const BitmapWidget *)widgetCursor.widget;

data = widget->data ? getBitmapImage(widgetCursor, widget->data) : 0;
}

bool operator!=(const BitmapWidgetState& previousState) {
return
flags.active != previousState.flags.active ||
data != previousState.data;
}

void draw(WidgetState *previousState) override;
};

Expand Down
28 changes: 8 additions & 20 deletions src/eez/gui/widgets/button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,18 @@
#include <eez/gui/gui.h>
#include <eez/gui/widgets/button.h>

static const size_t MAX_TEXT_LEN = 128;

namespace eez {
namespace gui {

void ButtonWidgetState::draw(WidgetState *previousState) {
auto widget = (const ButtonWidget *)widgetCursor.widget;

auto enabled = get(widgetCursor, widget->enabled);
flags.enabled = enabled.getType() == VALUE_TYPE_UNDEFINED || get(widgetCursor, widget->enabled).getInt() ? 1 : 0;

const Style *style = getStyle(flags.enabled ? widget->style : widget->disabledStyle);

flags.blinking = g_isBlinkTime && (isBlinking(widgetCursor, widget->data) || styleIsBlink(style));
data = widget->data ? get(widgetCursor, widget->data) : 0;

bool refresh =
!previousState ||
previousState->flags.active != flags.active ||
previousState->flags.enabled != flags.enabled ||
previousState->flags.blinking != flags.blinking ||
previousState->data != data;

static const size_t MAX_TEXT_LEN = 128;

void ButtonWidgetState::draw(WidgetState *previousStateBase) {
auto previousState = (ButtonWidgetState *)previousStateBase;
bool refresh = !previousState || *this != *previousState;
if (refresh) {
auto widget = (const ButtonWidget *)widgetCursor.widget;
const Style *style = getStyle(flags.enabled ? widget->style : widget->disabledStyle);

if (widget->data) {
if (data.isString()) {
drawText(data.getString(), -1, widgetCursor.x,
Expand Down
20 changes: 20 additions & 0 deletions src/eez/gui/widgets/button.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ struct ButtonWidget : public Widget {
};

struct ButtonWidgetState : public WidgetState {
ButtonWidgetState(const WidgetCursor &widgetCursor) : WidgetState(widgetCursor) {
auto widget = (const ButtonWidget *)widgetCursor.widget;

auto enabled = get(widgetCursor, widget->enabled);
flags.enabled = enabled.getType() == VALUE_TYPE_UNDEFINED || get(widgetCursor, widget->enabled).getInt() ? 1 : 0;

const Style *style = getStyle(flags.enabled ? widget->style : widget->disabledStyle);
flags.blinking = g_isBlinkTime && (isBlinking(widgetCursor, widget->data) || styleIsBlink(style));

data = widget->data ? get(widgetCursor, widget->data) : 0;
}

bool operator!=(const ButtonWidgetState& previousState) {
return
flags.active != previousState.flags.active ||
flags.enabled != previousState.flags.enabled ||
flags.blinking != previousState.flags.blinking ||
data != previousState.data;
}

void draw(WidgetState *previousState) override;
};

Expand Down
Loading

0 comments on commit 6c3b15f

Please sign in to comment.