Skip to content

Commit

Permalink
widget data and action refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Jul 21, 2021
1 parent 78145d4 commit af2eea0
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 63 deletions.
10 changes: 10 additions & 0 deletions src/eez/flow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ using namespace eez::gui;
namespace eez {
namespace flow {

#if 0

static Assets *g_assets;

static const int UNDEFINED_VALUE_INDEX = 0;
Expand Down Expand Up @@ -290,6 +292,8 @@ void dataOperation(unsigned flowHandle, int16_t dataId, DataOperationEnum operat
}
}

#endif

void dumpFlow(FlowDefinition &flowDefinition) {
// printf("Flows:\n");
// for (unsigned i = 0; i < flowDefinition.flows.count; i++) {
Expand Down Expand Up @@ -321,5 +325,11 @@ void dumpFlow(FlowDefinition &flowDefinition) {
// }
}

unsigned start(eez::gui::Assets *assets) { return 0; }
void tick(unsigned flowHandle) {}

void executeFlowAction(unsigned flowHandle, int16_t actionId) {}
void dataOperation(unsigned flowHandle, int16_t dataId, gui::DataOperationEnum operation, gui::Cursor cursor, gui::Value &value) {}

} // namespace flow
} // namespace eez
1 change: 1 addition & 0 deletions src/eez/gui/app_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ void AppContext::updatePage(int i, WidgetCursor &widgetCursor) {

m_updatePageIndex = i;

widgetCursor.pageId = m_pageNavigationStack[i].pageId;
widgetCursor.cursor = -1;

int x;
Expand Down
31 changes: 16 additions & 15 deletions src/eez/gui/assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,23 +220,24 @@ int getExternalAssetsFirstPageId() {
return -1;
}

const char *getActionName(int16_t actionId) {
if (actionId == 0) {
return nullptr;
}
if (actionId < 0) {
actionId = -actionId;
}
actionId--;
return g_externalAssets->actionNames.item(g_externalAssets, actionId);
const char *getActionName(const WidgetCursor& widgetCursor, int16_t actionId) {
// if (actionId == 0) {
// return nullptr;
// }
// if (actionId < 0) {
// actionId = -actionId;
// }
// actionId--;
// return g_externalAssets->actionNames.item(g_externalAssets, actionId);
return nullptr;
}

int16_t getDataIdFromName(const char *name) {
for (uint32_t i = 0; i < g_externalAssets->variableNames.count; i++) {
if (strcmp(g_externalAssets->variableNames.item(g_externalAssets, i), name) == 0) {
return -((int16_t)i + 1);
}
}
int16_t getDataIdFromName(const WidgetCursor& widgetCursor, const char *name) {
// for (uint32_t i = 0; i < g_externalAssets->variableNames.count; i++) {
// if (strcmp(g_externalAssets->variableNames.item(g_externalAssets, i), name) == 0) {
// return -((int16_t)i + 1);
// }
// }
return 0;
}

Expand Down
34 changes: 15 additions & 19 deletions src/eez/gui/assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ struct ListOfFundamentalType {
#define CLOSE_PAGE_IF_TOUCHED_OUTSIDE_FLAG 2

struct Widget {
uint8_t type;
uint8_t reserved1;
uint16_t type;
int16_t data;
int16_t action;
int16_t x;
Expand Down Expand Up @@ -196,21 +195,18 @@ struct Colors {
// 011<input index> - push input value on stack (max. no. of component inputs is 8192)
// 100<operation type> - pop values from the stack, do operation and push result on the stack (max. no. of different operations is 8192)

static const uin16_t EXPR_EVAL_INSTRUCTION_TYPE_MASK = (7 << 13);
static const uin16_t EXPR_EVAL_INSTRUCTION_PARAM_MASK = ~EXPR_EVAL_INSTRUCTION_TYPE_MASK;
static const uint16_t EXPR_EVAL_INSTRUCTION_TYPE_MASK = (7 << 13);
static const uint16_t EXPR_EVAL_INSTRUCTION_PARAM_MASK = ~EXPR_EVAL_INSTRUCTION_TYPE_MASK;

static const uin16_t EXPR_EVAL_INSTRUCTION_TYPE_PUSH_CONSTANT = (0 << 13);
static const uin16_t EXPR_EVAL_INSTRUCTION_TYPE_PUSH_GLOBAL_VAR = (1 << 13);
static const uin16_t EXPR_EVAL_INSTRUCTION_TYPE_PUSH_LOCAL_VAR = (2 << 13);
static const uin16_t EXPR_EVAL_INSTRUCTION_TYPE_PUSH_INPUT = (3 << 13);
static const uin16_t EXPR_EVAL_INSTRUCTION_TYPE_DO_OPERATION = (4 << 13);
static const uin16_t EXPR_EVAL_INSTRUCTION_TYPE_END = (4 << 13);

static const uin16_t OPERATION_ADD 0
static const uin16_t OPERATION_SUB 0
static const uint16_t EXPR_EVAL_INSTRUCTION_TYPE_PUSH_CONSTANT = (0 << 13);
static const uint16_t EXPR_EVAL_INSTRUCTION_TYPE_PUSH_GLOBAL_VAR = (1 << 13);
static const uint16_t EXPR_EVAL_INSTRUCTION_TYPE_PUSH_LOCAL_VAR = (2 << 13);
static const uint16_t EXPR_EVAL_INSTRUCTION_TYPE_PUSH_INPUT = (3 << 13);
static const uint16_t EXPR_EVAL_INSTRUCTION_TYPE_DO_OPERATION = (4 << 13);
static const uint16_t EXPR_EVAL_INSTRUCTION_TYPE_END = (4 << 13);

struct PropertyValue {
uin16_t evalInstructions[1];
uint16_t evalInstructions[1];
};

struct ComponentOutput {
Expand All @@ -223,23 +219,23 @@ struct ComponentOutput {
struct Component {
uint16_t type;
uint16_t reserved;
ListOfFundamentalType<uin16_t> inputs;
ListOfFundamentalType<uint16_t> inputs;
ListOfAssetsPtr<PropertyValue> propertyValues;
ListOfAssetsPtr<ComponentOutput> outputs;
};

struct Flow {
ListOfAssetsPtr<Component> components;
ListOfAssetsPtr<Value> localVariables;
ListOfAssetsPtr<PropertyValue> widgetDataItems;
ListOfAssetsPtr<ComponentOutput> widgetActions;
uint16_t nInputValues;
};

struct FlowDefinition {
ListOfAssetsPtr<Flow> flows;
ListOfAssetsPtr<Value> constants;
ListOfAssetsPtr<Value> globalVariables;
ListOfAssetsPtr<PropertyValue> widgetDataItems;
ListOfAssetsPtr<ComponentOutput> widgetActions;
};

////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -281,8 +277,8 @@ const uint16_t *getColors();

int getExternalAssetsFirstPageId();

const char *getActionName(int16_t actionId);
int16_t getDataIdFromName(const char *name);
const char *getActionName(const WidgetCursor& widgetCursor, int16_t actionId);
int16_t getDataIdFromName(const WidgetCursor& widgetCursor, const char *name);

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

Expand Down
10 changes: 5 additions & 5 deletions src/eez/gui/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ static void onWidgetDefaultTouch(const WidgetCursor &widgetCursor, Event &touchE
dragOverlay(touchEvent);
m_activeWidget = widgetCursor;
} else if (widgetCursor.appContext->testExecuteActionOnTouchDown(action)) {
executeAction(action);
executeAction(widgetCursor, action);
m_touchActionExecutedAtDown = true;
if (widgetCursor.appContext->isAutoRepeatAction(action)) {
m_activeWidget = widgetCursor;
Expand All @@ -191,21 +191,21 @@ static void onWidgetDefaultTouch(const WidgetCursor &widgetCursor, Event &touchE
int action = widgetCursor.widget->action;
if (widgetCursor.appContext->isWidgetActionEnabled(widgetCursor) && widgetCursor.appContext->isAutoRepeatAction(action)) {
m_touchActionExecuted = true;
executeAction(action);
executeAction(widgetCursor, action);
}
} else if (touchEvent.type == EVENT_TYPE_LONG_TOUCH) {
m_touchActionExecuted = true;
int action = widgetCursor.appContext->getLongTouchActionHook(widgetCursor);
if (action != ACTION_ID_NONE) {
g_isLongTouch = true;
executeAction(action);
executeAction(widgetCursor, action);
g_isLongTouch = false;
}
} else if (touchEvent.type == EVENT_TYPE_EXTRA_LONG_TOUCH) {
m_touchActionExecuted = true;
int action = widgetCursor.appContext->getExtraLongTouchActionHook(widgetCursor);
if (action != ACTION_ID_NONE) {
executeAction(action);
executeAction(widgetCursor, action);
}
} else if (touchEvent.type == EVENT_TYPE_TOUCH_UP) {
if (!m_touchActionExecutedAtDown) {
Expand All @@ -215,7 +215,7 @@ static void onWidgetDefaultTouch(const WidgetCursor &widgetCursor, Event &touchE
if (action == ACTION_ID_DRAG_OVERLAY) {
dragOverlay(touchEvent);
} else {
executeAction(action);
executeAction(widgetCursor, action);
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/eez/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ bool isInternalAction(int actionId) {
return actionId > FIRST_INTERNAL_ACTION_ID;
}

void executeAction(int actionId) {
void executeAction(const WidgetCursor &widgetCursor, int actionId) {
if (actionId == ACTION_ID_NONE) {
return;
}
Expand All @@ -208,11 +208,15 @@ void executeAction(int actionId) {
if (actionId >= 0) {
g_actionExecFunctions[actionId]();
} else {
executeExternalActionHook(actionId);
executeExternalActionHook(widgetCursor, actionId);
}
}
}

void executeAction(int actionId) {
WidgetCursor widgetCursor;
executeAction(widgetCursor, actionId);
}
////////////////////////////////////////////////////////////////////////////////

using namespace mcu::display;
Expand Down
3 changes: 2 additions & 1 deletion src/eez/gui/gui.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ bool isActiveWidget(const WidgetCursor &widgetCursor);
bool isFocusWidget(const WidgetCursor &widgetCursor);
void refreshScreen();
bool isPageInternal(int pageId);
void executeAction(const WidgetCursor &widgetCursor, int actionId);
void executeAction(int actionId);

int16_t getAppContextId(AppContext *pAppContext);
Expand Down Expand Up @@ -153,7 +154,7 @@ void animateRects(AppContext *appContext, Buffer startBuffer, int numRects, floa

float getDefaultAnimationDurationHook();

void executeExternalActionHook(int16_t actionId);
void executeExternalActionHook(const WidgetCursor &widgetCursor, int16_t actionId);
void externalDataHook(int16_t id, DataOperationEnum operation, Cursor cursor, Value &value);

OnTouchFunctionType getWidgetTouchFunctionHook(const WidgetCursor &widgetCursor);
Expand Down
26 changes: 15 additions & 11 deletions src/eez/gui/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,42 +86,46 @@ struct WidgetCursor {
Assets *assets;
AppContext *appContext;
const Widget *widget;
int16_t x;
int16_t y;
Cursor cursor;
WidgetState *previousState;
WidgetState *currentState;
int16_t pageId;
int16_t x;
int16_t y;

WidgetCursor()
: assets(nullptr)
, appContext(nullptr)
, widget(nullptr)
, x(0)
, y(0)
, cursor(-1)
, previousState(nullptr)
, currentState(nullptr)
, pageId(0)
, x(0)
, y(0)
{
}

WidgetCursor(
Assets *assets_,
AppContext *appContext_,
const Widget *widget_,
int x_,
int y_,
const Cursor cursor_,
WidgetState *previousState_,
WidgetState *currentState_
WidgetState *currentState_,
int16_t pageId_,
int16_t x_,
int16_t y_
)
: assets(assets_)
, appContext(appContext_)
, widget(widget_)
, x(x_)
, y(y_)
, cursor(cursor_)
, cursor(cursor_)
, previousState(previousState_)
, currentState(currentState_)
, pageId(pageId_)
, x(x_)
, y(y_)
{
}

Expand All @@ -132,7 +136,7 @@ struct WidgetCursor {
}

bool operator!=(const WidgetCursor &rhs) const {
return appContext != rhs.appContext || widget != rhs.widget || cursor != rhs.cursor;
return widget != rhs.widget || cursor != rhs.cursor;
}

bool operator==(const WidgetCursor &rhs) const {
Expand Down
2 changes: 1 addition & 1 deletion src/eez/gui/widgets/yt_graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ OnTouchFunctionType YT_GRAPH_onTouch = [](const WidgetCursor &widgetCursor, Even
} else {
if (touchEvent.type == EVENT_TYPE_TOUCH_DOWN) {
if (widgetCursor.appContext->isWidgetActionEnabled(widgetCursor)) {
executeAction(widgetCursor.widget->action);
executeAction(widgetCursor, widgetCursor.widget->action);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/eez/keyboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ void onKeyDown(uint16_t param) {
if (g_focusWidgetCursor) {
if (g_focusWidgetCursor.widget->action) {
setFoundWidgetAtDown(g_focusWidgetCursor);
executeAction(g_focusWidgetCursor.widget->action);
executeAction(g_focusWidgetCursor, g_focusWidgetCursor.widget->action);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/eez/modules/psu/gui/page.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ void ToastMessagePage::refresh(const WidgetCursor& widgetCursor) {
}

void ToastMessagePage::updatePage(const WidgetCursor& widgetCursor) {
if (actionWidgetIsActive != isActiveWidget(WidgetCursor(widgetCursor.assets, appContext, &actionWidget, actionWidget.x, actionWidget.y, -1, 0, 0))) {
if (actionWidgetIsActive != isActiveWidget(WidgetCursor(widgetCursor.assets, appContext, &actionWidget, -1, 0, 0, INTERNAL_PAGE_ID_TOAST_MESSAGE, actionWidget.x, actionWidget.y))) {
actionWidgetIsActive = !actionWidgetIsActive;
refresh(widgetCursor);
}
Expand All @@ -307,10 +307,10 @@ WidgetCursor ToastMessagePage::findWidget(int x, int y, bool clicked) {
y >= (actionWidget.y - textHeight / 4) &&
y < (actionWidget.y + actionWidget.h - 1 + textHeight / 4)
) {
return WidgetCursor(g_mainAssets, appContext, &actionWidget, actionWidget.x, actionWidget.y, -1, 0, 0);
return WidgetCursor(g_mainAssets, appContext, &actionWidget, -1, 0, 0, INTERNAL_PAGE_ID_TOAST_MESSAGE, actionWidget.x, actionWidget.y);
}
widget.action = ACTION_ID_INTERNAL_DIALOG_CLOSE;
return WidgetCursor(g_mainAssets, appContext, &widget, x, y, -1, 0, 0);
return WidgetCursor(g_mainAssets, appContext, &widget, -1, 0, 0, INTERNAL_PAGE_ID_TOAST_MESSAGE, x, y);
}

return WidgetCursor();
Expand Down Expand Up @@ -538,7 +538,7 @@ WidgetCursor SelectFromEnumPage::findWidget(int x, int y, bool clicked) {

widget.action = ACTION_ID_INTERNAL_SELECT_ENUM_ITEM;
widget.data = (uint16_t)i;
return WidgetCursor(g_mainAssets, appContext, &widget, x, y, -1, 0, 0);
return WidgetCursor(g_mainAssets, appContext, &widget, -1, 0, 0, INTERNAL_PAGE_ID_SELECT_FROM_ENUM, x, y);
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/eez/modules/psu/gui/psu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ static mcu::Button g_userSwitch(USER_SW_GPIO_Port, USER_SW_Pin, true, true);

Value g_progress;

static WidgetCursor g_externalActionWidgetCursor;
static int16_t g_externalActionId = ACTION_ID_NONE;
static const size_t MAX_NUM_EXTERNAL_DATA_ITEM_VALUES = 20;
static struct {
Expand Down Expand Up @@ -992,7 +993,7 @@ DialogActionResult PsuAppContext::dialogAction(uint32_t timeoutMs, const char *&
}

if (g_externalActionId != ACTION_ID_NONE) {
selectedActionName = getActionName(g_externalActionId);
selectedActionName = getActionName(g_externalActionWidgetCursor, g_externalActionId);
g_externalActionId = ACTION_ID_NONE;
return DIALOG_ACTION_RESULT_SELECTED_ACTION;
}
Expand Down Expand Up @@ -2548,10 +2549,11 @@ float getDefaultAnimationDurationHook() {
return psu::persist_conf::devConf.animationsDuration;
}

void executeExternalActionHook(int16_t actionId) {
void executeExternalActionHook(const WidgetCursor &widgetCursor, int16_t actionId) {
if (scripting::isFlowRunning()) {
scripting::executeFlowAction(actionId);
} else {
g_externalActionWidgetCursor = widgetCursor;
g_externalActionId = actionId;
}
}
Expand Down
Loading

0 comments on commit af2eea0

Please sign in to comment.