From 3d37ded31d681eba047e1fe11cf095ff1f2209cd Mon Sep 17 00:00:00 2001 From: elasota Date: Sat, 14 Mar 2020 05:04:51 -0400 Subject: [PATCH] Button highlight behavior --- PortabilityLayer/PLButtonWidget.cpp | 72 ++++++++++++++++------------- PortabilityLayer/PLButtonWidget.h | 3 +- 2 files changed, 42 insertions(+), 33 deletions(-) diff --git a/PortabilityLayer/PLButtonWidget.cpp b/PortabilityLayer/PLButtonWidget.cpp index 111ac5c..48a51d8 100644 --- a/PortabilityLayer/PLButtonWidget.cpp +++ b/PortabilityLayer/PLButtonWidget.cpp @@ -8,7 +8,6 @@ namespace PortabilityLayer { ButtonWidget::ButtonWidget(const WidgetBasicState &state) : WidgetSpec(state) - , m_haveMouseDown(false) , m_text(state.m_text) { } @@ -18,35 +17,19 @@ namespace PortabilityLayer if (!m_visible || !m_enabled) return WidgetHandleStates::kIgnored; - if (m_haveMouseDown) + if (evt.IsLMouseDownEvent()) { - if (evt.IsLMouseUpEvent()) - { - m_haveMouseDown = false; + 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)) + { + if (Capture(pt, nullptr) == RegionIDs::kNone) + return WidgetHandleStates::kDigested; + else return WidgetHandleStates::kActivated; - else - return WidgetHandleStates::kIgnored; - } - - return WidgetHandleStates::kCaptured; - } - else - { - if (evt.IsLMouseDownEvent()) - { - const Point pt = m_window->MouseToLocal(evt.m_vosEvent.m_event.m_mouseInputEvent); - - if (m_rect.Contains(pt)) - { - m_haveMouseDown = true; - return WidgetHandleStates::kCaptured; - } - else - return WidgetHandleStates::kIgnored; } + else + return WidgetHandleStates::kIgnored; } return WidgetHandleStates::kIgnored; @@ -59,19 +42,33 @@ namespace PortabilityLayer int16_t ButtonWidget::Capture(const Point &pos, WidgetUpdateCallback_t callback) { + bool drawingDownState = false; + bool isInBounds = m_rect.Contains(pos); for (;;) { + if (drawingDownState != isInBounds) + { + DrawControlInternal(m_window->GetDrawSurface(), isInBounds); + drawingDownState = isInBounds; + } + TimeTaggedVOSEvent evt; 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); + isInBounds = m_rect.Contains(pt); - if (m_rect.Contains(pt)) - return RegionIDs::kContent; - else - return RegionIDs::kNone; + if (evt.IsLMouseUpEvent()) + { + DrawControlInternal(m_window->GetDrawSurface(), false); + + if (isInBounds) + return RegionIDs::kContent; + else + return RegionIDs::kNone; + } } } } @@ -85,7 +82,12 @@ namespace PortabilityLayer 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)); if (m_enabled) @@ -94,6 +96,12 @@ namespace PortabilityLayer surface->SetForeColor(RGBAColor::Create(136, 136, 136, 255)); 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); int32_t x = (m_rect.left + m_rect.right - static_cast(surface->MeasureString(m_text.ToShortStr()))) / 2; int32_t y = (m_rect.top + m_rect.bottom + static_cast(surface->MeasureFontAscender())) / 2; diff --git a/PortabilityLayer/PLButtonWidget.h b/PortabilityLayer/PLButtonWidget.h index e585aca..092be49 100644 --- a/PortabilityLayer/PLButtonWidget.h +++ b/PortabilityLayer/PLButtonWidget.h @@ -21,7 +21,8 @@ namespace PortabilityLayer int16_t Capture(const Point &pos, WidgetUpdateCallback_t callback) override; private: - bool m_haveMouseDown; + void DrawControlInternal(DrawSurface *surface, bool inverted); + PascalStr<255> m_text; }; }