Add some widget layout and ability event handling.

This commit is contained in:
jmsgrogan 2020-06-20 19:00:06 +01:00
parent b99708e7d3
commit 4e85edacc8
24 changed files with 285 additions and 31 deletions

View file

@ -3,7 +3,8 @@
Button::Button()
: Widget(),
mLabel()
mLabel(),
mClickFunc()
{
}
@ -13,6 +14,11 @@ std::unique_ptr<Button> Button::Create()
return std::make_unique<Button>();
}
void Button::SetOnClickFunction(clickFunc func)
{
mClickFunc = func;
}
void Button::SetLabel(const std::string& text)
{
mLabel = text;
@ -22,8 +28,15 @@ void Button::OnMyMouseEvent(const MouseEvent* event)
{
if(event->GetAction() == MouseEvent::Action::Pressed)
{
std::cout << "Clicked !!" << std::endl;
SetBackgroundColor(Color::Create(0, 255, 0));
if(mClickFunc)
{
mClickFunc();
}
}
else if(event->GetAction() == MouseEvent::Action::Released)
{
SetBackgroundColor(Color::Create(0, 255, 255));
}
}

View file

@ -1,11 +1,14 @@
#pragma once
#include "Widget.h"
#include <functional>
class Button : public Widget
{
private:
using clickFunc = std::function<void()>;
std::string mLabel;
clickFunc mClickFunc;
public:
Button();
@ -14,6 +17,8 @@ public:
void SetLabel(const std::string& text);
void SetOnClickFunction(clickFunc func);
void OnPaintEvent(const PaintEvent* event) override;
protected:

View file

@ -1,9 +1,11 @@
#include "HorizontalSpacer.h"
#include <algorithm>
#include <numeric>
HorizontalSpacer::HorizontalSpacer()
: Widget()
: Widget(),
mScales()
{
}
@ -13,18 +15,42 @@ std::unique_ptr<HorizontalSpacer> HorizontalSpacer::Create()
return std::make_unique<HorizontalSpacer>();
}
void HorizontalSpacer::AddWidget(WidgetUPtr widget)
{
AddWidgetWithScale(std::move(widget), 1.0);
}
void HorizontalSpacer::AddWidgetWithScale(WidgetUPtr widget, double scale)
{
Widget::AddWidget(std::move(widget));
mScales.push_back(scale);
}
void HorizontalSpacer::AddChildLayers(const PaintEvent* event)
{
mLayers.clear();
unsigned delta = mHeight / mChildren.size();
double scaleSum = std::accumulate(mScales.begin(), mScales.end(), 0.0);
double offset = 0;
double height = mHeight;
if (mMaxHeight > 0 && height > mMaxHeight)
{
height = mMaxHeight;
}
for(std::size_t idx=0; idx<mChildren.size(); idx++)
{
auto& child = mChildren[idx];
double scale = mScales[idx];
double delta = height * (scale/scaleSum);
if (child->GetMaxHeight() > 0 && delta > child->GetMaxHeight())
{
delta = child->GetMaxHeight();
}
child->SetSize(mWidth, delta);
child->SetLocation(DiscretePoint(mLocation.GetX(), mLocation.GetY() + delta*idx));
child->SetLocation(DiscretePoint(mLocation.GetX(), mLocation.GetY() + offset));
child->OnPaintEvent(event);
auto layers = child->GetLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end());
offset += delta;
}
}

View file

@ -1,15 +1,21 @@
#pragma once
#include "Widget.h"
#include <vector>
class HorizontalSpacer : public Widget
{
std::vector<double> mScales;
public:
HorizontalSpacer();
static std::unique_ptr<HorizontalSpacer> Create();
void AddWidget(WidgetUPtr widget) override;
void AddWidgetWithScale(WidgetUPtr widget, double scale);
void AddChildLayers(const PaintEvent* event) override;
void OnPaintEvent(const PaintEvent* event) override;

View file

@ -8,7 +8,7 @@ TextBox::TextBox()
mContent(),
mCaps(false)
{
mBackgroundColor = Color::Create(200, 200, 200);
mBackgroundColor = Color::Create(240, 240, 220);
}
std::unique_ptr<TextBox> TextBox::Create()

View file

@ -0,0 +1,52 @@
#include "VerticalSpacer.h"
#include <algorithm>
#include <numeric>
VerticalSpacer::VerticalSpacer()
: Widget(),
mScales()
{
}
std::unique_ptr<VerticalSpacer> VerticalSpacer::Create()
{
return std::make_unique<VerticalSpacer>();
}
void VerticalSpacer::AddWidget(WidgetUPtr widget)
{
AddWidgetWithScale(std::move(widget), 1.0);
}
void VerticalSpacer::AddWidgetWithScale(WidgetUPtr widget, double scale)
{
Widget::AddWidget(std::move(widget));
mScales.push_back(scale);
}
void VerticalSpacer::AddChildLayers(const PaintEvent* event)
{
mLayers.clear();
double scaleSum = std::accumulate(mScales.begin(), mScales.end(), 0.0);
double offset = 0;
unsigned delta = mWidth / mChildren.size();
for(std::size_t idx=0; idx<mChildren.size(); idx++)
{
auto& child = mChildren[idx];
double scale = mScales[idx];
double delta = mWidth * (scale/scaleSum);
child->SetSize(delta, mHeight);
child->SetLocation(DiscretePoint(mLocation.GetX() + offset, mLocation.GetY()));
child->OnPaintEvent(event);
auto layers = child->GetLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end());
offset += delta;
}
}
void VerticalSpacer::OnPaintEvent(const PaintEvent* event)
{
AddChildLayers(event);
}

View file

@ -0,0 +1,23 @@
#pragma once
#include "Widget.h"
class VerticalSpacer : public Widget
{
std::vector<double> mScales;
public:
VerticalSpacer();
static std::unique_ptr<VerticalSpacer> Create();
void AddWidget(WidgetUPtr widget) override;
void AddWidgetWithScale(WidgetUPtr widget, double scale);
void AddChildLayers(const PaintEvent* event) override;
void OnPaintEvent(const PaintEvent* event) override;
};
using VerticalSpacerUPtr = std::unique_ptr<VerticalSpacer>;

View file

@ -7,6 +7,7 @@ Widget::Widget()
: mLocation(DiscretePoint(0, 0)),
mWidth(100),
mHeight(100),
mMaxHeight(0),
mMyLayers(),
mLayers(),
mChildren(),
@ -50,6 +51,16 @@ std::vector<VisualLayer*> Widget::GetLayers() const
return mLayers;
}
unsigned Widget::GetMaxHeight() const
{
return mMaxHeight;
}
void Widget::SetMaxHeight(unsigned maxHeight)
{
mMaxHeight = maxHeight;
}
void Widget::CopyMyLayers()
{
mLayers.clear();

View file

@ -17,6 +17,7 @@ protected:
DiscretePoint mLocation;
unsigned mWidth;
unsigned mHeight;
unsigned mMaxHeight;
std::vector<VisualLayerUPtr> mMyLayers;
std::vector<VisualLayer*> mLayers;
std::vector<std::unique_ptr<Widget> > mChildren;
@ -30,12 +31,14 @@ public:
static std::unique_ptr<Widget> Create();
void AddWidget(std::unique_ptr<Widget> widget);
virtual void AddWidget(std::unique_ptr<Widget> widget);
unsigned GetWidth() const;
unsigned GetHeight() const;
unsigned GetMaxHeight() const;
std::vector<VisualLayer*> GetLayers() const;
DiscretePoint GetLocation() const;
@ -54,6 +57,8 @@ public:
void SetLocation(const DiscretePoint& loc);
void SetMaxHeight(unsigned maxHeight);
protected:
virtual bool OnMyKeyboardEvent(const KeyboardEvent* event);

View file

@ -3,9 +3,9 @@
TextElement::TextElement(const std::string& content, const DiscretePoint& loc)
: mContent(content),
mLocation(loc),
mFontLabel("fixed")
mFontLabel("7x14")
{
// https://en.wikipedia.org/wiki/Fixed_(typeface)#:~:text=misc%2Dfixed%20is%20a%20collection,to%20a%20single%20font%20family.
}
TextElement::~TextElement()