From b7362aa3f20161cc0016c028e604cbee49fd9ff1 Mon Sep 17 00:00:00 2001 From: Magnus Norddahl Date: Wed, 27 Dec 2023 04:46:14 +0100 Subject: [PATCH] Add a bit more functionality to the controls --- .../widgets/checkboxlabel/checkboxlabel.h | 6 ++ .../zwidget/widgets/listview/listview.h | 4 ++ .../zwidget/widgets/pushbutton/pushbutton.h | 13 ++++ libraries/ZWidget/src/core/widget.cpp | 4 +- .../widgets/checkboxlabel/checkboxlabel.cpp | 45 ++++++++++++- .../ZWidget/src/widgets/listview/listview.cpp | 31 ++++++++- .../src/widgets/pushbutton/pushbutton.cpp | 67 ++++++++++++++++++- 7 files changed, 164 insertions(+), 6 deletions(-) diff --git a/libraries/ZWidget/include/zwidget/widgets/checkboxlabel/checkboxlabel.h b/libraries/ZWidget/include/zwidget/widgets/checkboxlabel/checkboxlabel.h index 1c83f1cc45..8bcaf5efbe 100644 --- a/libraries/ZWidget/include/zwidget/widgets/checkboxlabel/checkboxlabel.h +++ b/libraries/ZWidget/include/zwidget/widgets/checkboxlabel/checkboxlabel.h @@ -13,13 +13,19 @@ public: void SetChecked(bool value); bool GetChecked() const; + void Toggle(); double GetPreferredHeight() const; protected: void OnPaint(Canvas* canvas) override; + void OnMouseDown(const Point& pos, int key) override; + void OnMouseUp(const Point& pos, int key) override; + void OnMouseLeave() override; + void OnKeyUp(EInputKey key) override; private: std::string text; bool checked = false; + bool mouseDownActive = false; }; diff --git a/libraries/ZWidget/include/zwidget/widgets/listview/listview.h b/libraries/ZWidget/include/zwidget/widgets/listview/listview.h index ca54c33e7c..52cd5a7a2c 100644 --- a/libraries/ZWidget/include/zwidget/widgets/listview/listview.h +++ b/libraries/ZWidget/include/zwidget/widgets/listview/listview.h @@ -14,6 +14,10 @@ public: protected: void OnPaint(Canvas* canvas) override; void OnPaintFrame(Canvas* canvas) override; + void OnMouseDown(const Point& pos, int key) override; + void OnMouseDoubleclick(const Point& pos, int key) override; + void OnKeyDown(EInputKey key) override; std::vector items; + int selectedItem = 0; }; diff --git a/libraries/ZWidget/include/zwidget/widgets/pushbutton/pushbutton.h b/libraries/ZWidget/include/zwidget/widgets/pushbutton/pushbutton.h index 511b949171..80d827d392 100644 --- a/libraries/ZWidget/include/zwidget/widgets/pushbutton/pushbutton.h +++ b/libraries/ZWidget/include/zwidget/widgets/pushbutton/pushbutton.h @@ -2,6 +2,7 @@ #pragma once #include "../../core/widget.h" +#include class PushButton : public Widget { @@ -13,10 +14,22 @@ public: double GetPreferredHeight() const; + void Click(); + + std::function OnClick; + protected: void OnPaintFrame(Canvas* canvas) override; void OnPaint(Canvas* canvas) override; + void OnMouseMove(const Point& pos) override; + void OnMouseDown(const Point& pos, int key) override; + void OnMouseUp(const Point& pos, int key) override; + void OnMouseLeave() override; + void OnKeyDown(EInputKey key) override; + void OnKeyUp(EInputKey key) override; private: std::string text; + bool buttonDown = false; + bool hot = false; }; diff --git a/libraries/ZWidget/src/core/widget.cpp b/libraries/ZWidget/src/core/widget.cpp index 4ef736d69d..d6647fce77 100644 --- a/libraries/ZWidget/src/core/widget.cpp +++ b/libraries/ZWidget/src/core/widget.cpp @@ -141,7 +141,7 @@ void Widget::SetFrameGeometry(const Rect& geometry) double right = FrameGeometry.right() - Noncontent.Right; double bottom = FrameGeometry.bottom() - Noncontent.Bottom; left = std::min(left, FrameGeometry.right()); - top = std::min(top, FrameGeometry.right()); + top = std::min(top, FrameGeometry.bottom()); right = std::max(right, FrameGeometry.left()); bottom = std::max(bottom, FrameGeometry.top()); ContentGeometry = Rect::ltrb(left, top, right, bottom); @@ -268,7 +268,7 @@ void Widget::Repaint() { Widget* w = Window(); w->DispCanvas->begin(WindowBackground); - w->Paint(DispCanvas.get()); + w->Paint(w->DispCanvas.get()); w->DispCanvas->end(); } diff --git a/libraries/ZWidget/src/widgets/checkboxlabel/checkboxlabel.cpp b/libraries/ZWidget/src/widgets/checkboxlabel/checkboxlabel.cpp index 0924d8156e..80963fc087 100644 --- a/libraries/ZWidget/src/widgets/checkboxlabel/checkboxlabel.cpp +++ b/libraries/ZWidget/src/widgets/checkboxlabel/checkboxlabel.cpp @@ -41,7 +41,48 @@ double CheckboxLabel::GetPreferredHeight() const void CheckboxLabel::OnPaint(Canvas* canvas) { if (checked) - canvas->drawText(Point(0.0, GetHeight() - 5.0), Colorf::fromRgba8(255, 255, 255), "[x] " + text); + { + canvas->fillRect(Rect::xywh(0.0, GetHeight() * 0.5 - 5.0, 10.0, 10.0), Colorf::fromRgba8(100, 100, 100)); + canvas->fillRect(Rect::xywh(1.0, GetHeight() * 0.5 - 4.0, 8.0, 8.0), Colorf::fromRgba8(51, 51, 51)); + canvas->fillRect(Rect::xywh(2.0, GetHeight() * 0.5 - 3.0, 6.0, 6.0), Colorf::fromRgba8(226, 223, 219)); + } else - canvas->drawText(Point(0.0, GetHeight() - 5.0), Colorf::fromRgba8(255, 255, 255), "[ ] " + text); + { + canvas->fillRect(Rect::xywh(0.0, GetHeight() * 0.5 - 5.0, 10.0, 10.0), Colorf::fromRgba8(68, 68, 68)); + canvas->fillRect(Rect::xywh(1.0, GetHeight() * 0.5 - 4.0, 8.0, 8.0), Colorf::fromRgba8(51, 51, 51)); + } + + canvas->drawText(Point(14.0, GetHeight() - 5.0), Colorf::fromRgba8(255, 255, 255), text); +} + +void CheckboxLabel::OnMouseDown(const Point& pos, int key) +{ + mouseDownActive = true; + SetFocus(); +} + +void CheckboxLabel::OnMouseUp(const Point& pos, int key) +{ + if (mouseDownActive) + { + Toggle(); + } + mouseDownActive = false; +} + +void CheckboxLabel::OnMouseLeave() +{ + mouseDownActive = false; +} + +void CheckboxLabel::OnKeyUp(EInputKey key) +{ + if (key == IK_Space) + Toggle(); +} + +void CheckboxLabel::Toggle() +{ + checked = !checked; + Update(); } diff --git a/libraries/ZWidget/src/widgets/listview/listview.cpp b/libraries/ZWidget/src/widgets/listview/listview.cpp index b047c07682..ca3a22e7a4 100644 --- a/libraries/ZWidget/src/widgets/listview/listview.cpp +++ b/libraries/ZWidget/src/widgets/listview/listview.cpp @@ -22,7 +22,7 @@ void ListView::OnPaint(Canvas* canvas) int index = 0; for (const std::string& item : items) { - if (index == 0) + if (index == selectedItem) { canvas->fillRect(Rect::xywh(x - 2.0, y + 5.0 - h, w, h), Colorf::fromRgba8(100, 100, 100)); } @@ -43,3 +43,32 @@ void ListView::OnPaintFrame(Canvas* canvas) canvas->fillRect(Rect::xywh(0.0, 0.0, 1.0, h - 0.0), bordercolor); canvas->fillRect(Rect::xywh(w - 1.0, 0.0, 1.0, h - 0.0), bordercolor); } + +void ListView::OnMouseDown(const Point& pos, int key) +{ + SetFocus(); +} + +void ListView::OnMouseDoubleclick(const Point& pos, int key) +{ +} + +void ListView::OnKeyDown(EInputKey key) +{ + if (key == IK_Down) + { + if (selectedItem + 1 < (int)items.size()) + { + selectedItem++; + Update(); + } + } + else if (key == IK_Up) + { + if (selectedItem > 0) + { + selectedItem--; + Update(); + } + } +} diff --git a/libraries/ZWidget/src/widgets/pushbutton/pushbutton.cpp b/libraries/ZWidget/src/widgets/pushbutton/pushbutton.cpp index deb7bc9513..2e2e348cb5 100644 --- a/libraries/ZWidget/src/widgets/pushbutton/pushbutton.cpp +++ b/libraries/ZWidget/src/widgets/pushbutton/pushbutton.cpp @@ -30,7 +30,12 @@ void PushButton::OnPaintFrame(Canvas* canvas) double w = GetFrameGeometry().width; double h = GetFrameGeometry().height; Colorf bordercolor = Colorf::fromRgba8(100, 100, 100); - canvas->fillRect(Rect::xywh(0.0, 0.0, w, h), Colorf::fromRgba8(68, 68, 68)); + Colorf buttoncolor = Colorf::fromRgba8(68, 68, 68); + if (buttonDown) + buttoncolor = Colorf::fromRgba8(88, 88, 88); + else if (hot) + buttoncolor = Colorf::fromRgba8(78, 78, 78); + canvas->fillRect(Rect::xywh(0.0, 0.0, w, h), buttoncolor); canvas->fillRect(Rect::xywh(0.0, 0.0, w, 1.0), bordercolor); canvas->fillRect(Rect::xywh(0.0, h - 1.0, w, 1.0), bordercolor); canvas->fillRect(Rect::xywh(0.0, 0.0, 1.0, h - 0.0), bordercolor); @@ -42,3 +47,63 @@ void PushButton::OnPaint(Canvas* canvas) Rect box = canvas->measureText(text); canvas->drawText(Point((GetWidth() - box.width) * 0.5, GetHeight() - 5.0), Colorf::fromRgba8(255, 255, 255), text); } + +void PushButton::OnMouseMove(const Point& pos) +{ + if (!hot) + { + hot = true; + Update(); + } +} + +void PushButton::OnMouseDown(const Point& pos, int key) +{ + SetFocus(); + buttonDown = true; + Update(); +} + +void PushButton::OnMouseUp(const Point& pos, int key) +{ + if (buttonDown) + { + buttonDown = false; + hot = false; + Repaint(); + Click(); + } +} + +void PushButton::OnMouseLeave() +{ + hot = false; + buttonDown = false; + Update(); +} + +void PushButton::OnKeyDown(EInputKey key) +{ + if (key == IK_Space || key == IK_Enter) + { + buttonDown = true; + Update(); + } +} + +void PushButton::OnKeyUp(EInputKey key) +{ + if (key == IK_Space || key == IK_Enter) + { + buttonDown = false; + hot = false; + Repaint(); + Click(); + } +} + +void PushButton::Click() +{ + if (OnClick) + OnClick(); +}