Skip to content

Commit

Permalink
fixed scroll bar thumb dragging
Browse files Browse the repository at this point in the history
  • Loading branch information
mvladic committed Dec 17, 2021
1 parent e1e79f6 commit 36677c8
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 65 deletions.
91 changes: 50 additions & 41 deletions src/eez/gui/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,29 @@
namespace eez {
namespace gui {

static WidgetCursor m_foundWidgetAtDown;
WidgetCursor g_activeWidget;
static bool m_touchActionExecuted;
static bool m_touchActionExecutedAtDown;
static OnTouchFunctionType m_onTouchFunction;
static bool m_longTouchGenerated;
static bool m_extraLongTouchGenerated;
////////////////////////////////////////////////////////////////////////////////

WidgetCursor g_activeWidget;
bool g_isLongTouch;

static WidgetCursor g_foundWidgetAtDown;

static bool g_touchActionExecuted;
static bool g_touchActionExecutedAtDown;

static OnTouchFunctionType g_onTouchFunction;

static bool g_longTouchGenerated;
static bool g_extraLongTouchGenerated;

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

static void processTouchEvent(EventType type, int x, int y);
static void onPageTouch(const WidgetCursor &foundWidget, Event &touchEvent);
static void onWidgetDefaultTouch(const WidgetCursor &widgetCursor, Event &touchEvent);
static void onWidgetTouch(const WidgetCursor &widgetCursor, Event &touchEvent);

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

void eventHandling() {
if (isEventHandlingDisabledHook()) {
Expand Down Expand Up @@ -91,19 +101,19 @@ void eventHandling() {
if (eventType == EVENT_TYPE_TOUCH_DOWN) {
m_touchDownTimeMs = tickCountMs;
m_lastAutoRepeatEventTimeMs = tickCountMs;
m_longTouchGenerated = false;
m_extraLongTouchGenerated = false;
g_longTouchGenerated = false;
g_extraLongTouchGenerated = false;
processTouchEvent(EVENT_TYPE_TOUCH_DOWN, eventX, eventY);
} else if (eventType == EVENT_TYPE_TOUCH_MOVE) {
processTouchEvent(EVENT_TYPE_TOUCH_MOVE, eventX, eventY);

if (!m_longTouchGenerated && int32_t(tickCountMs - m_touchDownTimeMs) >= CONF_GUI_LONG_TOUCH_TIMEOUT_MS) {
m_longTouchGenerated = true;
if (!g_longTouchGenerated && int32_t(tickCountMs - m_touchDownTimeMs) >= CONF_GUI_LONG_TOUCH_TIMEOUT_MS) {
g_longTouchGenerated = true;
processTouchEvent(EVENT_TYPE_LONG_TOUCH, eventX, eventY);
}

if (m_longTouchGenerated && !m_extraLongTouchGenerated && int32_t(tickCountMs - m_touchDownTimeMs) >= CONF_GUI_EXTRA_LONG_TOUCH_TIMEOUT_MS) {
m_extraLongTouchGenerated = true;
if (g_longTouchGenerated && !g_extraLongTouchGenerated && int32_t(tickCountMs - m_touchDownTimeMs) >= CONF_GUI_EXTRA_LONG_TOUCH_TIMEOUT_MS) {
g_extraLongTouchGenerated = true;
processTouchEvent(EVENT_TYPE_EXTRA_LONG_TOUCH, eventX, eventY);
}

Expand All @@ -119,31 +129,25 @@ void eventHandling() {

static void processTouchEvent(EventType type, int x, int y) {
if (type == EVENT_TYPE_TOUCH_DOWN) {
m_foundWidgetAtDown = findWidget(x, y);
m_onTouchFunction = getWidgetTouchFunction(m_foundWidgetAtDown);
if (!m_onTouchFunction) {
m_onTouchFunction = onPageTouch;
g_foundWidgetAtDown = findWidget(x, y);
g_onTouchFunction = getWidgetTouchFunction(g_foundWidgetAtDown);
if (!g_onTouchFunction) {
g_onTouchFunction = onPageTouch;
}
} else if (type == EVENT_TYPE_TOUCH_UP) {
g_activeWidget = 0;
}

if (m_onTouchFunction) {
if (g_onTouchFunction) {
Event event;
event.type = type;
event.x = x;
event.y = y;

m_onTouchFunction(m_foundWidgetAtDown, event);
g_onTouchFunction(g_foundWidgetAtDown, event);
}
}

static void onWidgetTouch(const WidgetCursor &widgetCursor, Event &touchEvent) {
if (widgetCursor) {
widgetCursor.currentState->onTouch(widgetCursor, touchEvent);
}
}

OnTouchFunctionType getWidgetTouchFunction(const WidgetCursor &widgetCursor) {
if (widgetCursor) {
auto action = getWidgetAction(widgetCursor);
Expand Down Expand Up @@ -181,15 +185,15 @@ static void onWidgetDefaultTouch(const WidgetCursor &widgetCursor, Event &touchE
auto action = getWidgetAction(widgetCursor);

if (touchEvent.type == EVENT_TYPE_TOUCH_DOWN) {
m_touchActionExecuted = false;
m_touchActionExecutedAtDown = false;
g_touchActionExecuted = false;
g_touchActionExecutedAtDown = false;

if (action == EEZ_CONF_ACTION_ID_DRAG_OVERLAY) {
dragOverlay(touchEvent);
g_activeWidget = widgetCursor;
} else if (widgetCursor.appContext->testExecuteActionOnTouchDown(action)) {
executeAction(widgetCursor, action);
m_touchActionExecutedAtDown = true;
g_touchActionExecutedAtDown = true;
if (widgetCursor.appContext->isAutoRepeatAction(action)) {
g_activeWidget = widgetCursor;
}
Expand All @@ -202,27 +206,26 @@ static void onWidgetDefaultTouch(const WidgetCursor &widgetCursor, Event &touchE
}
} else if (touchEvent.type == EVENT_TYPE_AUTO_REPEAT) {
if (widgetCursor.appContext->isWidgetActionEnabled(widgetCursor) && widgetCursor.appContext->isAutoRepeatAction(action)) {
m_touchActionExecuted = true;
g_touchActionExecuted = true;
executeAction(widgetCursor, action);
}
} else if (touchEvent.type == EVENT_TYPE_LONG_TOUCH) {
m_touchActionExecuted = true;
g_touchActionExecuted = true;
int action = widgetCursor.appContext->getLongTouchActionHook(widgetCursor);
if (action != ACTION_ID_NONE) {
g_isLongTouch = true;
executeAction(widgetCursor, action);
g_isLongTouch = false;
}
} else if (touchEvent.type == EVENT_TYPE_EXTRA_LONG_TOUCH) {
m_touchActionExecuted = true;
g_touchActionExecuted = true;
int action = widgetCursor.appContext->getExtraLongTouchActionHook(widgetCursor);
if (action != ACTION_ID_NONE) {
executeAction(widgetCursor, action);
}
} else if (touchEvent.type == EVENT_TYPE_TOUCH_UP) {
if (!m_touchActionExecutedAtDown) {
g_activeWidget = 0;
if (!m_touchActionExecuted) {
if (!g_touchActionExecutedAtDown) {
if (!g_touchActionExecuted) {
if (action == EEZ_CONF_ACTION_ID_DRAG_OVERLAY) {
dragOverlay(touchEvent);
} else {
Expand All @@ -233,22 +236,28 @@ static void onWidgetDefaultTouch(const WidgetCursor &widgetCursor, Event &touchE
}
}

static void onWidgetTouch(const WidgetCursor &widgetCursor, Event &touchEvent) {
if (widgetCursor) {
if (touchEvent.type == EVENT_TYPE_TOUCH_DOWN) {
g_activeWidget = g_foundWidgetAtDown;
}
widgetCursor.currentState->onTouch(widgetCursor, touchEvent);
}
}


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

WidgetCursor &getFoundWidgetAtDown() {
return m_foundWidgetAtDown;
return g_foundWidgetAtDown;
}

void setFoundWidgetAtDown(WidgetCursor &widgetCursor) {
m_foundWidgetAtDown = widgetCursor;
g_foundWidgetAtDown = widgetCursor;
}

void clearFoundWidgetAtDown() {
m_foundWidgetAtDown = 0;
}

bool isFocusWidget(const WidgetCursor &widgetCursor) {
return widgetCursor.appContext->isFocusWidget(widgetCursor);
g_foundWidgetAtDown = 0;
}

} // namespace gui
Expand Down
4 changes: 4 additions & 0 deletions src/eez/gui/gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -458,5 +458,9 @@ void animateRects(AppContext *appContext, Buffer startBuffer, int numRects, floa
}
}

bool isFocusWidget(const WidgetCursor &widgetCursor) {
return widgetCursor.appContext->isFocusWidget(widgetCursor);
}

} // namespace gui
} // namespace eez
21 changes: 11 additions & 10 deletions src/eez/gui/widgets/scroll_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
namespace eez {
namespace gui {

ScrollBarWidgetSegment dragSegment;
int dragStartX;
int dragStartPosition;

int getSize(const WidgetCursor &widgetCursor) {
return ytDataGetSize(widgetCursor, widgetCursor.widget->data);
}
Expand Down Expand Up @@ -60,19 +64,16 @@ void getThumbGeometry(int size, int position, int pageSize, int xTrack, int wTra
xThumb = xTrack + (int)round(remap(position, 0, 0, size - pageSize, wTrack - widthThumb));
}

WidgetCursor ScrollBarWidgetState::g_selectedWidget;

bool ScrollBarWidgetState::updateState() {
const WidgetCursor &widgetCursor = g_widgetCursor;

bool hasPreviousState = widgetCursor.hasPreviousState;

WIDGET_STATE(flags.active, g_selectedWidget == widgetCursor);
WIDGET_STATE(flags.active, g_isActiveWidget);
WIDGET_STATE(flags.focused, isFocusWidget(widgetCursor));
WIDGET_STATE(size, getSize(widgetCursor));
WIDGET_STATE(position, getPosition(widgetCursor));
WIDGET_STATE(pageSize, getPageSize(widgetCursor));
WIDGET_STATE(segment, dragSegment);

return !hasPreviousState;
}
Expand All @@ -95,7 +96,7 @@ void ScrollBarWidgetState::render() {
widgetCursor.y,
isHorizontal ? buttonSize : (int)widget->w,
isHorizontal ? (int)widget->h : buttonSize, buttonsStyle,
segment == SCROLL_BAR_WIDGET_SEGMENT_LEFT_BUTTON
flags.active && dragSegment == SCROLL_BAR_WIDGET_SEGMENT_LEFT_BUTTON
);
}

Expand Down Expand Up @@ -143,7 +144,7 @@ void ScrollBarWidgetState::render() {
isHorizontal ? buttonSize : (int)widget->w,
isHorizontal ? (int)widget->h : buttonSize,
buttonsStyle,
segment == SCROLL_BAR_WIDGET_SEGMENT_RIGHT_BUTTON
flags.active && dragSegment == SCROLL_BAR_WIDGET_SEGMENT_RIGHT_BUTTON
);
}

Expand Down Expand Up @@ -190,9 +191,8 @@ void ScrollBarWidgetState::onTouch(const WidgetCursor &widgetCursor, Event &touc
wTrack = widget->h - 2 * buttonSize;
}

if (touchEvent.type == EVENT_TYPE_TOUCH_DOWN || touchEvent.type == EVENT_TYPE_AUTO_REPEAT) {
if (touchEvent.type == EVENT_TYPE_TOUCH_DOWN || touchEvent.type == EVENT_TYPE_AUTO_REPEAT || dragSegment == SCROLL_BAR_WIDGET_SEGMENT_UNINITIALIZED) {
if (touchEvent.type == EVENT_TYPE_TOUCH_DOWN) {
g_selectedWidget = widgetCursor;
dragSegment = SCROLL_BAR_WIDGET_SEGMENT_NONE;
}

Expand Down Expand Up @@ -237,13 +237,14 @@ void ScrollBarWidgetState::onTouch(const WidgetCursor &widgetCursor, Event &touc
dragStartPosition = getPosition(widgetCursor);
}
}
} else if (touchEvent.type == EVENT_TYPE_TOUCH_MOVE) {
}

if (touchEvent.type == EVENT_TYPE_TOUCH_MOVE) {
if (dragSegment == SCROLL_BAR_WIDGET_SEGMENT_THUMB) {
int size = getSize(widgetCursor);
setPosition(widgetCursor, dragStartPosition + (int)round(1.0 * (x - dragStartX) * size / wTrack));
}
} else if (touchEvent.type == EVENT_TYPE_TOUCH_UP) {
g_selectedWidget = 0;
dragSegment = SCROLL_BAR_WIDGET_SEGMENT_NONE;
}

Expand Down
6 changes: 1 addition & 5 deletions src/eez/gui/widgets/scroll_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct ScrollBarWidget : public Widget {
};

enum ScrollBarWidgetSegment {
SCROLL_BAR_WIDGET_SEGMENT_UNINITIALIZED,
SCROLL_BAR_WIDGET_SEGMENT_NONE,
SCROLL_BAR_WIDGET_SEGMENT_TRACK_LEFT,
SCROLL_BAR_WIDGET_SEGMENT_TRACK_RIGHT,
Expand All @@ -42,11 +43,6 @@ struct ScrollBarWidgetState : public WidgetState {
int size;
int position;
int pageSize;
ScrollBarWidgetSegment segment;

ScrollBarWidgetSegment dragSegment;
int dragStartX;
int dragStartPosition;

static WidgetCursor g_selectedWidget;

Expand Down
10 changes: 1 addition & 9 deletions src/eez/gui/widgets/up_down.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@
namespace eez {
namespace gui {

WidgetCursor UpDownWidgetState::g_selectedWidget;

bool UpDownWidgetState::updateState() {
const WidgetCursor &widgetCursor = g_widgetCursor;

bool hasPreviousState = widgetCursor.hasPreviousState;
auto widget = (const UpDownWidget *)widgetCursor.widget;

WIDGET_STATE(flags.active, g_selectedWidget == widgetCursor);
WIDGET_STATE(flags.active, g_isActiveWidget);
WIDGET_STATE(data, get(widgetCursor, widget->data));

return !hasPreviousState;
Expand Down Expand Up @@ -119,19 +117,13 @@ void UpDownWidgetState::onTouch(const WidgetCursor &widgetCursor, Event &touchEv
const Widget *widget = widgetCursor.widget;

if (touchEvent.type == EVENT_TYPE_TOUCH_DOWN || touchEvent.type == EVENT_TYPE_AUTO_REPEAT) {
if (touchEvent.type == EVENT_TYPE_TOUCH_DOWN) {
g_selectedWidget = widgetCursor;
}

if (touchEvent.x < widgetCursor.x + widget->w / 2) {
upDown(widgetCursor, UP_DOWN_WIDGET_SEGMENT_DOWN_BUTTON);
} else {
upDown(widgetCursor, UP_DOWN_WIDGET_SEGMENT_UP_BUTTON);
}

sound::playClick();
} else if (touchEvent.type == EVENT_TYPE_TOUCH_UP) {
g_selectedWidget = 0;
}
}

Expand Down

0 comments on commit 36677c8

Please sign in to comment.