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)
: WidgetSpec<ButtonWidget>(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<int32_t>(surface->MeasureString(m_text.ToShortStr()))) / 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;
private:
bool m_haveMouseDown;
void DrawControlInternal(DrawSurface *surface, bool inverted);
PascalStr<255> m_text;
};
}