Add a bit more functionality to the controls

This commit is contained in:
Magnus Norddahl 2023-12-27 04:46:14 +01:00 committed by Christoph Oelckers
parent 787d18e12b
commit b7362aa3f2
7 changed files with 164 additions and 6 deletions

View file

@ -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;
};

View file

@ -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<std::string> items;
int selectedItem = 0;
};

View file

@ -2,6 +2,7 @@
#pragma once
#include "../../core/widget.h"
#include <functional>
class PushButton : public Widget
{
@ -13,10 +14,22 @@ public:
double GetPreferredHeight() const;
void Click();
std::function<void()> 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;
};

View file

@ -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();
}

View file

@ -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();
}

View file

@ -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();
}
}
}

View file

@ -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();
}