Skip to content

Commit

Permalink
W_BUTTON: Add clickHeld event
Browse files Browse the repository at this point in the history
  • Loading branch information
past-due committed Oct 11, 2023
1 parent 9241f59 commit fceb78e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/widget/button.cpp
Expand Up @@ -112,6 +112,32 @@ void W_BUTTON::setHelp(optional<WidgetHelp> _help)
help = _help;
}

void W_BUTTON::run(W_CONTEXT *psContext)
{
if (clickDownStart.has_value())
{
const std::chrono::steady_clock::time_point now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::milliseconds>(now - clickDownStart.value()) >= widgGetClickHoldMS())
{
if (clickDownKey.has_value())
{
if (clickHeld(psContext, clickDownKey.value()))
{
// clear button down state, as the clickHeld event "consumed" this click
state &= ~WBUT_DOWN;
}
}
clickDownStart.reset();
}
}
}

// Returns true if "consumed" held click
bool W_BUTTON::clickHeld(W_CONTEXT *psContext, WIDGET_KEY key)
{
return false;
}

void W_BUTTON::clicked(W_CONTEXT *, WIDGET_KEY key)
{
if ((minClickInterval > 0) && (realTime - lastClickTime < minClickInterval))
Expand All @@ -135,6 +161,9 @@ void W_BUTTON::clicked(W_CONTEXT *, WIDGET_KEY key)
}
state &= ~WBUT_FLASH; // Stop it flashing
state |= WBUT_DOWN;

clickDownStart = std::chrono::steady_clock::now();
clickDownKey = key;
}
}
}
Expand Down Expand Up @@ -170,6 +199,9 @@ void W_BUTTON::released(W_CONTEXT *, WIDGET_KEY key)
dirty = true;
}
}

clickDownStart = nullopt;
clickDownKey = nullopt;
}

WIDGET_KEY W_BUTTON::getOnClickButtonPressed() const
Expand Down Expand Up @@ -202,6 +234,9 @@ void W_BUTTON::highlightLost()
{
state &= ~(WBUT_DOWN | WBUT_HIGHLIGHT);
dirty = true;

clickDownStart = nullopt;
clickDownKey = nullopt;
}

void W_BUTTON::display(int xOffset, int yOffset)
Expand Down
4 changes: 4 additions & 0 deletions lib/widget/button.h
Expand Up @@ -53,9 +53,11 @@ class W_BUTTON : public WIDGET
W_BUTTON();

void clicked(W_CONTEXT *psContext, WIDGET_KEY key) override;
virtual bool clickHeld(W_CONTEXT *psContext, WIDGET_KEY key);
void released(W_CONTEXT *psContext, WIDGET_KEY key) override;
void highlight(W_CONTEXT *psContext) override;
void highlightLost() override;
void run(W_CONTEXT *psContext) override;
void display(int xOffset, int yOffset) override;
void displayRecursive(WidgetGraphicsContext const &context) override; // for handling progress border overlay

Expand Down Expand Up @@ -160,6 +162,8 @@ class W_BUTTON : public WIDGET
PIELIGHT progressBorderColour;
WIDGET_KEY lastClickButton = WKEY_NONE;
optional<WidgetHelp> help;
optional<std::chrono::steady_clock::time_point> clickDownStart; // the start time of click down on this button
optional<WIDGET_KEY> clickDownKey;
};

class MultipleChoiceButton : public W_BUTTON
Expand Down

0 comments on commit fceb78e

Please sign in to comment.