Button highlight behavior

This commit is contained in:
elasota
2020-03-14 05:04:51 -04:00
parent 3dcc787ca0
commit 3d37ded31d
2 changed files with 42 additions and 33 deletions

View File

@@ -8,7 +8,6 @@ namespace PortabilityLayer
{ {
ButtonWidget::ButtonWidget(const WidgetBasicState &state) ButtonWidget::ButtonWidget(const WidgetBasicState &state)
: WidgetSpec<ButtonWidget>(state) : WidgetSpec<ButtonWidget>(state)
, m_haveMouseDown(false)
, m_text(state.m_text) , m_text(state.m_text)
{ {
} }
@@ -18,36 +17,20 @@ namespace PortabilityLayer
if (!m_visible || !m_enabled) if (!m_visible || !m_enabled)
return WidgetHandleStates::kIgnored; return WidgetHandleStates::kIgnored;
if (m_haveMouseDown)
{
if (evt.IsLMouseUpEvent())
{
m_haveMouseDown = false;
const Point pt = m_window->MouseToLocal(evt.m_vosEvent.m_event.m_mouseInputEvent);
if (m_rect.Contains(pt))
return WidgetHandleStates::kActivated;
else
return WidgetHandleStates::kIgnored;
}
return WidgetHandleStates::kCaptured;
}
else
{
if (evt.IsLMouseDownEvent()) if (evt.IsLMouseDownEvent())
{ {
const Point pt = m_window->MouseToLocal(evt.m_vosEvent.m_event.m_mouseInputEvent); const Point pt = m_window->MouseToLocal(evt.m_vosEvent.m_event.m_mouseInputEvent);
if (m_rect.Contains(pt)) if (m_rect.Contains(pt))
{ {
m_haveMouseDown = true; if (Capture(pt, nullptr) == RegionIDs::kNone)
return WidgetHandleStates::kCaptured; return WidgetHandleStates::kDigested;
else
return WidgetHandleStates::kActivated;
} }
else else
return WidgetHandleStates::kIgnored; return WidgetHandleStates::kIgnored;
} }
}
return WidgetHandleStates::kIgnored; return WidgetHandleStates::kIgnored;
} }
@@ -59,16 +42,29 @@ namespace PortabilityLayer
int16_t ButtonWidget::Capture(const Point &pos, WidgetUpdateCallback_t callback) int16_t ButtonWidget::Capture(const Point &pos, WidgetUpdateCallback_t callback)
{ {
bool drawingDownState = false;
bool isInBounds = m_rect.Contains(pos);
for (;;) for (;;)
{ {
if (drawingDownState != isInBounds)
{
DrawControlInternal(m_window->GetDrawSurface(), isInBounds);
drawingDownState = isInBounds;
}
TimeTaggedVOSEvent evt; TimeTaggedVOSEvent evt;
if (WaitForEvent(&evt, 1)) if (WaitForEvent(&evt, 1))
{ {
if (evt.IsLMouseUpEvent()) if (evt.m_vosEvent.m_eventType == GpVOSEventTypes::kMouseInput)
{ {
const Point pt = m_window->MouseToLocal(evt.m_vosEvent.m_event.m_mouseInputEvent); const Point pt = m_window->MouseToLocal(evt.m_vosEvent.m_event.m_mouseInputEvent);
isInBounds = m_rect.Contains(pt);
if (m_rect.Contains(pt)) if (evt.IsLMouseUpEvent())
{
DrawControlInternal(m_window->GetDrawSurface(), false);
if (isInBounds)
return RegionIDs::kContent; return RegionIDs::kContent;
else else
return RegionIDs::kNone; return RegionIDs::kNone;
@@ -76,6 +72,7 @@ namespace PortabilityLayer
} }
} }
} }
}
bool ButtonWidget::Init(const WidgetBasicState &state) bool ButtonWidget::Init(const WidgetBasicState &state)
{ {
@@ -85,7 +82,12 @@ namespace PortabilityLayer
void ButtonWidget::DrawControl(DrawSurface *surface) void ButtonWidget::DrawControl(DrawSurface *surface)
{ {
surface->SetForeColor(StdColors::White()); DrawControlInternal(surface, false);
}
void ButtonWidget::DrawControlInternal(DrawSurface *surface, bool inverted)
{
surface->SetForeColor(inverted ? StdColors::Black() : StdColors::White());
surface->FillRect(this->m_rect.Inset(1, 1)); surface->FillRect(this->m_rect.Inset(1, 1));
if (m_enabled) if (m_enabled)
@@ -94,6 +96,12 @@ namespace PortabilityLayer
surface->SetForeColor(RGBAColor::Create(136, 136, 136, 255)); surface->SetForeColor(RGBAColor::Create(136, 136, 136, 255));
surface->FrameRect(this->m_rect); surface->FrameRect(this->m_rect);
if (m_enabled)
surface->SetForeColor(inverted ? StdColors::White() : StdColors::Black());
else
surface->SetForeColor(RGBAColor::Create(136, 136, 136, 255));
surface->SetSystemFont(12, PortabilityLayer::FontFamilyFlag_Bold); surface->SetSystemFont(12, PortabilityLayer::FontFamilyFlag_Bold);
int32_t x = (m_rect.left + m_rect.right - static_cast<int32_t>(surface->MeasureString(m_text.ToShortStr()))) / 2; int32_t x = (m_rect.left + m_rect.right - static_cast<int32_t>(surface->MeasureString(m_text.ToShortStr()))) / 2;
int32_t y = (m_rect.top + m_rect.bottom + static_cast<int32_t>(surface->MeasureFontAscender())) / 2; int32_t y = (m_rect.top + m_rect.bottom + static_cast<int32_t>(surface->MeasureFontAscender())) / 2;

View File

@@ -21,7 +21,8 @@ namespace PortabilityLayer
int16_t Capture(const Point &pos, WidgetUpdateCallback_t callback) override; int16_t Capture(const Point &pos, WidgetUpdateCallback_t callback) override;
private: private:
bool m_haveMouseDown; void DrawControlInternal(DrawSurface *surface, bool inverted);
PascalStr<255> m_text; PascalStr<255> m_text;
}; };
} }