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

@ -1,4 +1,4 @@
list(APPEND client_LIB_INCLUDES GuiApplication.cpp) list(APPEND client_LIB_INCLUDES GuiApplication.cpp TextEditorPanel.cpp)
add_library(client SHARED ${client_LIB_INCLUDES}) add_library(client SHARED ${client_LIB_INCLUDES})

View file

@ -2,6 +2,7 @@
#include "Widget.h" #include "Widget.h"
#include "HorizontalSpacer.h" #include "HorizontalSpacer.h"
#include "VerticalSpacer.h"
#include "TextBox.h" #include "TextBox.h"
#include "Button.h" #include "Button.h"
#include "Label.h" #include "Label.h"
@ -10,6 +11,8 @@
#include "Window.h" #include "Window.h"
#include "TextElement.h" #include "TextElement.h"
#include "WindowManager.h" #include "WindowManager.h"
#include "TextEditorPanel.h"
#include <iostream>
GuiApplication::GuiApplication() GuiApplication::GuiApplication()
: AbstractDesktopApp(), : AbstractDesktopApp(),
@ -29,28 +32,55 @@ void GuiApplication::SetMainApplication(MainApplicationPtr app)
mMainApplication = app; mMainApplication = app;
} }
void GuiApplication::SetUpWidget()
{
auto mainWindow = mDesktopManager->GetWindowManager()->GetMainWindow();
mainWindow->SetSize(800, 600);
// Left panel
auto textEditorButton = Button::Create();
textEditorButton->SetLabel("Text Editor");
textEditorButton->SetBackgroundColor(Color::Create(156, 156, 156));
auto imageEditorButton = Button::Create();
imageEditorButton->SetLabel("Image Editor");
imageEditorButton->SetBackgroundColor(Color::Create(156, 156, 156));
auto audioEditorButton = Button::Create();
audioEditorButton->SetLabel("Audio Editor");
audioEditorButton->SetBackgroundColor(Color::Create(156, 156, 156));
auto webClientButton = Button::Create();
webClientButton->SetLabel("Web Client");
webClientButton->SetBackgroundColor(Color::Create(156, 156, 156));
auto leftSpacer = HorizontalSpacer::Create();
leftSpacer->AddWidget(std::move(textEditorButton));
leftSpacer->AddWidget(std::move(imageEditorButton));
leftSpacer->AddWidget(std::move(audioEditorButton));
leftSpacer->AddWidget(std::move(webClientButton));
leftSpacer->SetMaxHeight(200);
// Right panel
auto textEditor = TextEditorPanel::Create();
textEditor->SetOnSaveFunction([](const std::string& content)
{
std::cout << content << std::endl;
});
textEditor->Initialize();
auto vertSpacer = VerticalSpacer::Create();
vertSpacer->AddWidgetWithScale(std::move(leftSpacer), 1);
vertSpacer->AddWidgetWithScale(std::move(textEditor), 4);
mainWindow->AddWidget(std::move(vertSpacer));
}
void GuiApplication::Run() void GuiApplication::Run()
{ {
auto mainWindow = mDesktopManager->GetWindowManager()->GetMainWindow(); auto mainWindow = mDesktopManager->GetWindowManager()->GetMainWindow();
SetUpWidget();
mainWindow->SetSize(800, 600);
auto label = Label::Create();
label->SetLabel("Type text!!");
label->SetBackgroundColor(Color::Create(0, 200, 200));
auto textBox = TextBox::Create();
auto button = Button::Create();
button->SetLabel("Save");
button->SetBackgroundColor(Color::Create(0, 0, 200));
auto spacer = HorizontalSpacer::Create();
spacer->AddWidget(std::move(label));
spacer->AddWidget(std::move(textBox));
spacer->AddWidget(std::move(button));
mainWindow->AddWidget(std::move(spacer));
mDesktopManager->SetKeyboard(XcbKeyboard::Create()); mDesktopManager->SetKeyboard(XcbKeyboard::Create());
bool useOpenGl = false; bool useOpenGl = false;
@ -63,9 +93,6 @@ void GuiApplication::Run()
{ {
window_interface.CreateOpenGlDrawable(mainWindow); window_interface.CreateOpenGlDrawable(mainWindow);
} }
window_interface.Loop(mDesktopManager.get()); window_interface.Loop(mDesktopManager.get());
window_interface.ShutDown(); window_interface.ShutDown();
} }

View file

@ -23,4 +23,6 @@ public:
void SetMainApplication(MainApplicationPtr app); void SetMainApplication(MainApplicationPtr app);
void Run(); void Run();
void SetUpWidget();
}; };

View file

View file

0
src/client/MainPanel.cpp Normal file
View file

0
src/client/MainPanel.h Normal file
View file

0
src/client/StatusBar.cpp Normal file
View file

0
src/client/StatusBar.h Normal file
View file

View file

@ -0,0 +1,59 @@
#include "TextEditorPanel.h"
#include "HorizontalSpacer.h"
#include "VerticalSpacer.h"
TextEditorPanel::TextEditorPanel()
: mSaveFunction(),
mTextBox()
{
}
void TextEditorPanel::Initialize()
{
auto textEditorLabel = Label::Create();
textEditorLabel->SetLabel("Text Editor");
textEditorLabel->SetBackgroundColor(Color::Create(81, 89, 102));
auto textEditorBox = TextBox::Create();
mTextBox = textEditorBox.get();
auto textEditorSaveButton = Button::Create();
textEditorSaveButton->SetLabel("Save");
textEditorSaveButton->SetBackgroundColor(Color::Create(156, 156, 156));
if (mSaveFunction)
{
auto onSave = [this]()
{
if(this && mSaveFunction && mTextBox)
{
mSaveFunction(mTextBox->GetContent());
};
};
textEditorSaveButton->SetOnClickFunction(onSave);
}
auto textEditorClearButton = Button::Create();
textEditorClearButton->SetLabel("Clear");
textEditorClearButton->SetBackgroundColor(Color::Create(156, 156, 156));
auto buttonSpacer = VerticalSpacer::Create();
buttonSpacer->AddWidgetWithScale(std::move(textEditorSaveButton), 1);
buttonSpacer->AddWidgetWithScale(std::move(textEditorClearButton), 1);
auto hSpacer = HorizontalSpacer::Create();
hSpacer->AddWidgetWithScale(std::move(textEditorLabel), 1);
hSpacer->AddWidgetWithScale(std::move(textEditorBox), 8);
hSpacer->AddWidgetWithScale(std::move(buttonSpacer), 1);
AddWidget(std::move(hSpacer));
}
std::unique_ptr<TextEditorPanel> TextEditorPanel::Create()
{
return std::make_unique<TextEditorPanel>();
}
void TextEditorPanel::SetOnSaveFunction(saveFunc func)
{
mSaveFunction = func;
}

View file

@ -0,0 +1,24 @@
#pragma once
#include "Widget.h"
#include "Button.h"
#include "Label.h"
#include "TextBox.h"
class TextEditorPanel : public Widget
{
using saveFunc = void (*)(const std::string& content);
saveFunc mSaveFunction;
TextBox* mTextBox;
public:
TextEditorPanel();
static std::unique_ptr<TextEditorPanel> Create();
void Initialize();
void SetOnSaveFunction(saveFunc func);
};
using TextEditorPanelUPtr = std::unique_ptr<TextEditorPanel>;

0
src/client/TopBar.cpp Normal file
View file

0
src/client/TopBar.h Normal file
View file

View file

@ -10,6 +10,7 @@ list(APPEND ui_elements_LIB_INCLUDES
widgets/Button.cpp widgets/Button.cpp
widgets/Label.cpp widgets/Label.cpp
widgets/HorizontalSpacer.cpp widgets/HorizontalSpacer.cpp
widgets/VerticalSpacer.cpp
widgets/TextBox.cpp widgets/TextBox.cpp
widgets/elements/GeometryElement.cpp widgets/elements/GeometryElement.cpp
widgets/elements/RectangleElement.cpp widgets/elements/RectangleElement.cpp

View file

@ -3,7 +3,8 @@
Button::Button() Button::Button()
: Widget(), : Widget(),
mLabel() mLabel(),
mClickFunc()
{ {
} }
@ -13,6 +14,11 @@ std::unique_ptr<Button> Button::Create()
return std::make_unique<Button>(); return std::make_unique<Button>();
} }
void Button::SetOnClickFunction(clickFunc func)
{
mClickFunc = func;
}
void Button::SetLabel(const std::string& text) void Button::SetLabel(const std::string& text)
{ {
mLabel = text; mLabel = text;
@ -22,8 +28,15 @@ void Button::OnMyMouseEvent(const MouseEvent* event)
{ {
if(event->GetAction() == MouseEvent::Action::Pressed) if(event->GetAction() == MouseEvent::Action::Pressed)
{ {
std::cout << "Clicked !!" << std::endl;
SetBackgroundColor(Color::Create(0, 255, 0)); 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 #pragma once
#include "Widget.h" #include "Widget.h"
#include <functional>
class Button : public Widget class Button : public Widget
{ {
private: private:
using clickFunc = std::function<void()>;
std::string mLabel; std::string mLabel;
clickFunc mClickFunc;
public: public:
Button(); Button();
@ -14,6 +17,8 @@ public:
void SetLabel(const std::string& text); void SetLabel(const std::string& text);
void SetOnClickFunction(clickFunc func);
void OnPaintEvent(const PaintEvent* event) override; void OnPaintEvent(const PaintEvent* event) override;
protected: protected:

View file

@ -1,9 +1,11 @@
#include "HorizontalSpacer.h" #include "HorizontalSpacer.h"
#include <algorithm> #include <algorithm>
#include <numeric>
HorizontalSpacer::HorizontalSpacer() HorizontalSpacer::HorizontalSpacer()
: Widget() : Widget(),
mScales()
{ {
} }
@ -13,18 +15,42 @@ std::unique_ptr<HorizontalSpacer> HorizontalSpacer::Create()
return std::make_unique<HorizontalSpacer>(); 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) void HorizontalSpacer::AddChildLayers(const PaintEvent* event)
{ {
mLayers.clear(); 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++) for(std::size_t idx=0; idx<mChildren.size(); idx++)
{ {
auto& child = mChildren[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->SetSize(mWidth, delta);
child->SetLocation(DiscretePoint(mLocation.GetX(), mLocation.GetY() + delta*idx)); child->SetLocation(DiscretePoint(mLocation.GetX(), mLocation.GetY() + offset));
child->OnPaintEvent(event); child->OnPaintEvent(event);
auto layers = child->GetLayers(); auto layers = child->GetLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end()); mLayers.insert(mLayers.end(), layers.begin(), layers.end());
offset += delta;
} }
} }

View file

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

View file

@ -8,7 +8,7 @@ TextBox::TextBox()
mContent(), mContent(),
mCaps(false) mCaps(false)
{ {
mBackgroundColor = Color::Create(200, 200, 200); mBackgroundColor = Color::Create(240, 240, 220);
} }
std::unique_ptr<TextBox> TextBox::Create() 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)), : mLocation(DiscretePoint(0, 0)),
mWidth(100), mWidth(100),
mHeight(100), mHeight(100),
mMaxHeight(0),
mMyLayers(), mMyLayers(),
mLayers(), mLayers(),
mChildren(), mChildren(),
@ -50,6 +51,16 @@ std::vector<VisualLayer*> Widget::GetLayers() const
return mLayers; return mLayers;
} }
unsigned Widget::GetMaxHeight() const
{
return mMaxHeight;
}
void Widget::SetMaxHeight(unsigned maxHeight)
{
mMaxHeight = maxHeight;
}
void Widget::CopyMyLayers() void Widget::CopyMyLayers()
{ {
mLayers.clear(); mLayers.clear();

View file

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

View file

@ -3,9 +3,9 @@
TextElement::TextElement(const std::string& content, const DiscretePoint& loc) TextElement::TextElement(const std::string& content, const DiscretePoint& loc)
: mContent(content), : mContent(content),
mLocation(loc), 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() TextElement::~TextElement()