Initial commit.

This commit is contained in:
jmsgrogan 2020-05-02 08:31:03 +01:00
commit 59c6161fdb
134 changed files with 4751 additions and 0 deletions

View file

@ -0,0 +1,29 @@
list(APPEND ui_elements_LIB_INCLUDES
desktop_elements/Keyboard.cpp
desktop_elements/Screen.cpp
desktop_elements/Window.cpp
ui_events/KeyboardEvent.cpp
ui_events/MouseEvent.cpp
ui_events/UiEvent.cpp
ui_events/PaintEvent.cpp
widgets/Widget.cpp
widgets/Button.cpp
widgets/Label.cpp
widgets/HorizontalSpacer.cpp
widgets/elements/GeometryElement.cpp
widgets/elements/RectangleElement.cpp
widgets/elements/TextElement.cpp
widgets/elements/VisualLayer.cpp)
add_library(ui_elements SHARED ${ui_elements_LIB_INCLUDES})
target_include_directories(ui_elements PUBLIC
"${PROJECT_SOURCE_DIR}/src/core/"
"${PROJECT_SOURCE_DIR}/src/geometry/"
"${CMAKE_CURRENT_SOURCE_DIR}/"
"${CMAKE_CURRENT_SOURCE_DIR}/widgets"
"${CMAKE_CURRENT_SOURCE_DIR}/widgets/elements"
"${CMAKE_CURRENT_SOURCE_DIR}/ui_events"
"${CMAKE_CURRENT_SOURCE_DIR}/desktop_elements"
)
target_link_libraries(ui_elements PUBLIC core geometry)

View file

@ -0,0 +1,13 @@
#include "Keyboard.h"
Keyboard::Keyboard()
:mKeyMap()
{
}
std::shared_ptr<Keyboard> Keyboard::Create()
{
return std::make_shared<Keyboard>();
}

View file

@ -0,0 +1,26 @@
#pragma once
#include <memory>
#include <map>
#include <string>
class Keyboard
{
public:
using KeyCode = unsigned;
protected:
std::map<KeyCode, std::string> mKeyMap;
public:
Keyboard();
virtual ~Keyboard() = default;
static std::shared_ptr<Keyboard> Create();
virtual std::string GetKeyString(KeyCode code)
{
return "";
}
};
using KeyboardPtr = std::shared_ptr<Keyboard>;

View file

@ -0,0 +1,18 @@
#include "Screen.h"
namespace mt{
Screen::Screen()
{
}
Screen::~Screen()
{
}
std::shared_ptr<Screen> Screen::Create()
{
return std::make_shared<Screen>();
}
}

View file

@ -0,0 +1,17 @@
#pragma once
#include <memory>
namespace mt{
class Screen
{
public:
Screen();
~Screen();
static std::shared_ptr<Screen> Create();
};
}
using ScreenPtr = std::shared_ptr<mt::Screen>;

View file

@ -0,0 +1,67 @@
#include "Window.h"
namespace mt{
Window::Window()
:mWidth(400),
mHeight(300),
mWidget()
{
}
Window::~Window()
{
}
std::vector<VisualLayerPtr> Window::GetLayers()
{
return mLayers;
}
void Window::OnMouseEvent(MouseEventPtr event)
{
mWidget->OnMouseEvent(event);
}
void Window::OnPaint(PaintEventPtr event)
{
mLayers.clear();
mWidget->SetSize(mWidth, mHeight);
mWidget->OnPaintEvent(event);
auto layers = mWidget->GetLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end());
}
std::shared_ptr<Window> Window::Create()
{
return std::make_shared<Window>();
}
void Window::AddWidget(WidgetPtr widget)
{
mWidget = widget;
}
WidgetPtr Window::GetWidget()
{
return mWidget;
}
unsigned Window::GetWidth()
{
return mWidth;
}
unsigned Window::GetHeight()
{
return mHeight;
}
void Window::SetSize(unsigned width, unsigned height)
{
mWidth = width;
mHeight = height;
}
}

View file

@ -0,0 +1,50 @@
#pragma once
#include <memory>
#include <vector>
#include "PaintEvent.h"
#include "MouseEvent.h"
#include "VisualLayer.h"
#include "Widget.h"
namespace mt
{
class Window
{
private:
WidgetPtr mWidget;
unsigned mWidth;
unsigned mHeight;
std::vector<VisualLayerPtr> mLayers;
public:
Window();
~Window();
static std::shared_ptr<Window> Create();
void AddWidget(WidgetPtr widget);
WidgetPtr GetWidget();
std::vector<VisualLayerPtr> GetLayers();
unsigned GetWidth();
unsigned GetHeight();
void SetSize(unsigned width, unsigned height);
void OnPaint(PaintEventPtr event);
void OnMouseEvent(MouseEventPtr event);
};
}
using WindowPtr = std::shared_ptr<mt::Window>;

View file

View file

@ -0,0 +1 @@

View file

@ -0,0 +1,39 @@
#include "KeyboardEvent.h"
KeyboardEvent::KeyboardEvent()
: UiEvent(),
mAction(),
mKeyString()
{
mType = UiEvent::Type::Keyboard;
}
KeyboardEvent::~KeyboardEvent()
{
}
std::shared_ptr<KeyboardEvent> KeyboardEvent::Create()
{
return std::make_shared<KeyboardEvent>();
}
void KeyboardEvent::SetKeyString(const std::string& key)
{
mKeyString = key;
}
std::string KeyboardEvent::GetKeyString()
{
return mKeyString;
}
void KeyboardEvent::SetAction(Action action)
{
mAction = action;
}
KeyboardEvent::Action KeyboardEvent::GetAction()
{
return mAction;
}

View file

@ -0,0 +1,39 @@
#pragma once
#include <memory>
#include <string>
#include "UiEvent.h"
class KeyboardEvent : public UiEvent
{
public:
enum class Action
{
Pressed,
Released
};
private:
Action mAction;
std::string mKeyString;
public:
KeyboardEvent();
~KeyboardEvent();
static std::shared_ptr<KeyboardEvent> Create();
void SetKeyString(const std::string& key);
std::string GetKeyString();
void SetAction(Action action);
Action GetAction();
};
using KeyboardEventPtr = std::shared_ptr<KeyboardEvent>;

View file

@ -0,0 +1,51 @@
#include "MouseEvent.h"
MouseEvent::MouseEvent()
: UiEvent(),
mClientLocation(0, 0),
mScreenLocation(0, 0),
mAction()
{
mType = UiEvent::Type::Mouse;
}
MouseEvent::~MouseEvent()
{
}
std::shared_ptr<MouseEvent> MouseEvent::Create()
{
return std::make_shared<MouseEvent>();
}
void MouseEvent::SetClientLocation(Pixel location)
{
mClientLocation = location;
}
void MouseEvent::SetScreenLocation(Pixel location)
{
mScreenLocation = location;
}
void MouseEvent::SetAction(MouseEvent::Action action)
{
mAction = action;
}
Pixel MouseEvent::GetClientLocation()
{
return mClientLocation;
}
Pixel MouseEvent::GetScreenLocation()
{
return mScreenLocation;
}
MouseEvent::Action MouseEvent::GetAction()
{
return mAction;
}

View file

@ -0,0 +1,42 @@
#pragma once
#include <memory>
#include "DiscretePoint.h"
#include "UiEvent.h"
class MouseEvent : public UiEvent
{
public:
enum class Action
{
Pressed,
Released
};
private:
Pixel mClientLocation;
Pixel mScreenLocation;
Action mAction;
public:
MouseEvent();
~MouseEvent();
static std::shared_ptr<MouseEvent> Create();
void SetClientLocation(Pixel location);
void SetScreenLocation(Pixel location);
void SetAction(Action action);
Pixel GetClientLocation();
Pixel GetScreenLocation();
Action GetAction();
};
using MouseEventPtr = std::shared_ptr<MouseEvent>;

View file

@ -0,0 +1,18 @@
#include "PaintEvent.h"
PaintEvent::PaintEvent()
: UiEvent()
{
mType = UiEvent::Type::Paint;
}
PaintEvent::~PaintEvent()
{
}
std::shared_ptr<PaintEvent> PaintEvent::Create()
{
return std::make_shared<PaintEvent>();
}

View file

@ -0,0 +1,17 @@
#pragma once
#include <memory>
#include "UiEvent.h"
class PaintEvent : public UiEvent
{
public:
PaintEvent();
~PaintEvent();
static std::shared_ptr<PaintEvent> Create();
};
using PaintEventPtr = std::shared_ptr<PaintEvent>;

View file

@ -0,0 +1,23 @@
#include "UiEvent.h"
UiEvent::UiEvent()
: mType(Type::Unknown)
{
}
UiEvent::~UiEvent()
{
}
std::shared_ptr<UiEvent> UiEvent::Create()
{
return std::make_shared<UiEvent>();
}
UiEvent::Type UiEvent::GetType()
{
return mType;
}

View file

@ -0,0 +1,29 @@
#pragma once
#include <memory>
class UiEvent
{
public:
enum class Type{
Unknown,
Paint,
Mouse,
Keyboard
};
protected:
UiEvent::Type mType;
public:
UiEvent();
virtual ~UiEvent();
static std::shared_ptr<UiEvent> Create();
Type GetType();
};
using UiEventPtr = std::shared_ptr<UiEvent>;

View file

@ -0,0 +1,44 @@
#include "Button.h"
#include <iostream>
Button::Button()
: Widget(),
mLabel()
{
}
std::shared_ptr<Button> Button::Create()
{
return std::make_shared<Button>();
}
void Button::SetLabel(const std::string& text)
{
mLabel = text;
}
void Button::OnMyMouseEvent(MouseEventPtr event)
{
if(event->GetAction() == MouseEvent::Action::Pressed)
{
std::cout << "Clicked !!" << std::endl;
SetBackgroundColor(Color::Create(0, 255, 0));
}
}
void Button::OnPaintEvent(PaintEventPtr event)
{
mLayers.clear();
AddBackground(event);
if(!mLabel.empty())
{
auto middle = DiscretePoint(mLocation.GetX() + mWidth/2,
mLocation.GetY() + mHeight/2);
auto text = TextElement::Create(mLabel, middle);
auto textLayer = VisualLayer::Create();
textLayer->SetText(text);
mLayers.push_back(textLayer);
}
}

View file

@ -0,0 +1,24 @@
#pragma once
#include "Widget.h"
class Button : public Widget
{
private:
std::string mLabel;
public:
Button();
void SetLabel(const std::string& text);
static std::shared_ptr<Button> Create();
void OnPaintEvent(PaintEventPtr event);
protected:
void OnMyMouseEvent(MouseEventPtr event);
};
using ButtonPtr = std::shared_ptr<Button>;

View file

@ -0,0 +1,33 @@
#include "HorizontalSpacer.h"
HorizontalSpacer::HorizontalSpacer()
: Widget()
{
}
std::shared_ptr<HorizontalSpacer> HorizontalSpacer::Create()
{
return std::make_shared<HorizontalSpacer>();
}
void HorizontalSpacer::AddChildLayers(PaintEventPtr event)
{
unsigned delta = mHeight / mChildren.size();
for(std::size_t idx=0; idx<mChildren.size(); idx++)
{
auto child = mChildren[idx];
child->SetSize(mWidth, delta);
child->SetLocation(DiscretePoint(mLocation.GetX(), mLocation.GetY() + delta*idx));
child->OnPaintEvent(event);
auto layers = child->GetLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end());
}
}
void HorizontalSpacer::OnPaintEvent(PaintEventPtr event)
{
mLayers.clear();
AddChildLayers(event);
}

View file

@ -0,0 +1,18 @@
#pragma once
#include "Widget.h"
class HorizontalSpacer : public Widget
{
public:
HorizontalSpacer();
static std::shared_ptr<HorizontalSpacer> Create();
void AddChildLayers(PaintEventPtr event) override;
void OnPaintEvent(PaintEventPtr event) override;
};
using HorizontalSpacerPtr = std::shared_ptr<HorizontalSpacer>;

View file

@ -0,0 +1,35 @@
#include "Label.h"
#include "TextElement.h"
Label::Label()
: Widget(),
mLabel()
{
}
std::shared_ptr<Label> Label::Create()
{
return std::make_shared<Label>();
}
void Label::SetLabel(const std::string& text)
{
mLabel = text;
}
void Label::OnPaintEvent(PaintEventPtr event)
{
mLayers.clear();
AddBackground(event);
if(!mLabel.empty())
{
auto middle = DiscretePoint(mLocation.GetX() + mWidth/2,
mLocation.GetY() + mHeight/2);
auto text = TextElement::Create(mLabel, middle);
auto textLayer = VisualLayer::Create();
textLayer->SetText(text);
mLayers.push_back(textLayer);
}
}

View file

@ -0,0 +1,22 @@
#pragma once
#include "Widget.h"
class Label : public Widget
{
private:
std::string mLabel;
public:
Label();
void SetLabel(const std::string& text);
static std::shared_ptr<Label> Create();
void OnPaintEvent(PaintEventPtr event);
};
using LabelPtr = std::shared_ptr<Label>;

View file

@ -0,0 +1,134 @@
#include "RectangleElement.h"
#include "Widget.h"
Widget::Widget()
: mLocation(DiscretePoint(0, 0)),
mWidth(100),
mHeight(100),
mLayers(),
mChildren(),
mBackgroundColor(Color::Create(200, 0, 0))
{
}
Widget::~Widget()
{
}
void Widget::AddWidget(WidgetPtr widget)
{
mChildren.push_back(widget);
}
void Widget::SetSize(unsigned width, unsigned height)
{
mWidth = width;
mHeight = height;
}
std::shared_ptr<Widget> Widget::Create()
{
return std::make_shared<Widget>();
}
DiscretePoint Widget::GetLocation()
{
return mLocation;
}
std::vector<VisualLayerPtr> Widget::GetLayers()
{
return mLayers;
}
void Widget::AddChildLayers(PaintEventPtr event)
{
for(std::size_t idx=0; idx<mChildren.size(); idx++)
{
auto child = mChildren[idx];
child->SetSize(mWidth, mHeight);
child->SetLocation(mLocation);
child->OnPaintEvent(event);
auto layers = child->GetLayers();
mLayers.insert(mLayers.end(), layers.begin(), layers.end());
}
}
void Widget::OnPaintEvent(PaintEventPtr event)
{
mLayers.clear();
AddBackground(event);
AddChildLayers(event);
}
bool Widget::Contains(const DiscretePoint& loc)
{
if(loc.GetX() < mLocation.GetX()) return false;
if(loc.GetX() > mLocation.GetX() + mWidth) return false;
if(loc.GetY() > mLocation.GetY() + mHeight) return false;
if(loc.GetY() < mLocation.GetY()) return false;
return true;
}
bool Widget::OnMouseEvent(MouseEventPtr event)
{
bool inChild = false;
for(const auto& child : mChildren)
{
if(child->OnMouseEvent(event))
{
inChild = true;
break;
}
}
if(!inChild)
{
if(Contains(event->GetClientLocation()))
{
OnMyMouseEvent(event);
return true;
}
else
{
return false;
}
}
return true;
}
void Widget::OnMyMouseEvent(MouseEventPtr event)
{
}
void Widget::AddBackground(PaintEventPtr event)
{
auto shape = RectangleElement::Create(mLocation, mWidth, mHeight);
shape->SetFillColor(mBackgroundColor);
auto shapeLayer = VisualLayer::Create();
shapeLayer->SetShape(shape);
mLayers.push_back(shapeLayer);
}
void Widget::SetBackgroundColor(ColorPtr color)
{
mBackgroundColor = color;
}
void Widget::SetLocation(const DiscretePoint& loc)
{
mLocation = loc;
}
unsigned Widget::GetWidth()
{
return mWidth;
}
unsigned Widget::GetHeight()
{
return mHeight;
}

View file

@ -0,0 +1,62 @@
#pragma once
#include <memory>
#include <vector>
#include "DiscretePoint.h"
#include "VisualLayer.h"
#include "PaintEvent.h"
#include "Color.h"
#include "MouseEvent.h"
class Widget
{
protected:
DiscretePoint mLocation;
unsigned mWidth;
unsigned mHeight;
std::vector<VisualLayerPtr> mLayers;
std::vector<std::shared_ptr<Widget> > mChildren;
ColorPtr mBackgroundColor;
public:
Widget();
virtual ~Widget();
void AddWidget(std::shared_ptr<Widget> widget);
void SetBackgroundColor(ColorPtr color);
void SetSize(unsigned mWidth, unsigned mHeight);
DiscretePoint GetLocation();
void SetLocation(const DiscretePoint& loc);
unsigned GetWidth();
unsigned GetHeight();
std::vector<VisualLayerPtr> GetLayers();
static std::shared_ptr<Widget> Create();
virtual void OnPaintEvent(PaintEventPtr event);
virtual bool OnMouseEvent(MouseEventPtr event);
bool Contains(const DiscretePoint& loc);
protected:
virtual void OnMyMouseEvent(MouseEventPtr event);
virtual void AddChildLayers(PaintEventPtr event);
virtual void AddBackground(PaintEventPtr event);
};
using WidgetPtr = std::shared_ptr<Widget>;

View file

@ -0,0 +1,36 @@
#include "GeometryElement.h"
GeometryElement::GeometryElement()
: mFillColor(Color::Create(255, 255, 255)),
mStrokeColor(Color::Create(0, 0, 0)),
mStrokeThickness(1),
mType(Type::Path)
{
}
ColorPtr GeometryElement::GetFillColor()
{
return mFillColor;
}
ColorPtr GeometryElement::GetStrokeColor()
{
return mStrokeColor;
}
unsigned GeometryElement::GetStrokeThickness()
{
return mStrokeThickness;
}
void GeometryElement::SetFillColor(ColorPtr color)
{
mFillColor = color;
}
void GeometryElement::SetStrokeColor(ColorPtr color)
{
mStrokeColor = color;
}
void GeometryElement::SetStrokeThickness(unsigned thickness)
{
mStrokeThickness = thickness;
}

View file

@ -0,0 +1,39 @@
#pragma once
#include "Color.h"
class GeometryElement
{
public:
enum class Type
{
Path,
Rectangle,
Circle,
Arc
};
private:
ColorPtr mFillColor;
ColorPtr mStrokeColor;
unsigned mStrokeThickness;
Type mType;
public:
GeometryElement();
virtual ~GeometryElement() = default;
ColorPtr GetFillColor();
ColorPtr GetStrokeColor();
unsigned GetStrokeThickness();
virtual Type GetType() = 0;
void SetFillColor(ColorPtr color);
void SetStrokeColor(ColorPtr color);
void SetStrokeThickness(unsigned thickness);
};
using GeometryElementPtr = std::shared_ptr<GeometryElement>;

View file

@ -0,0 +1,36 @@
#include "RectangleElement.h"
RectangleElement::RectangleElement(const DiscretePoint& loc,
unsigned width, unsigned height)
: mLocation(loc),
mWidth(width),
mHeight(height)
{
}
std::shared_ptr<RectangleElement> RectangleElement::Create(const DiscretePoint& loc,
unsigned width, unsigned height)
{
return std::make_shared<RectangleElement>(loc, width, height);
}
GeometryElement::Type RectangleElement::GetType()
{
return GeometryElement::Type::Rectangle;
}
DiscretePoint RectangleElement::GetLocation()
{
return mLocation;
}
unsigned RectangleElement::GetWidth()
{
return mWidth;
}
unsigned RectangleElement::GetHeight()
{
return mHeight;
}

View file

@ -0,0 +1,29 @@
#pragma once
#include <memory>
#include "GeometryElement.h"
#include "DiscretePoint.h"
class RectangleElement : public GeometryElement
{
DiscretePoint mLocation;
unsigned mWidth;
unsigned mHeight;
public:
RectangleElement(const DiscretePoint& loc,
unsigned width, unsigned height);
static std::shared_ptr<RectangleElement> Create(const DiscretePoint& loc,
unsigned width, unsigned height);
GeometryElement::Type GetType() override;
DiscretePoint GetLocation();
unsigned GetWidth();
unsigned GetHeight();
};
using RectangleElementPtr = std::shared_ptr<RectangleElement>;

View file

@ -0,0 +1,39 @@
#include "TextElement.h"
TextElement::TextElement(const std::string& content, const DiscretePoint& loc)
: mContent(content),
mLocation(loc),
mFontLabel("fixed")
{
}
TextElement::~TextElement()
{
}
std::shared_ptr<TextElement> TextElement::Create(const std::string& content, const DiscretePoint& loc)
{
return std::make_shared<TextElement>(content, loc);
}
DiscretePoint TextElement::GetLocation()
{
return mLocation;
}
void TextElement::SetContent(const std::string& content)
{
mContent = content;
}
std::string TextElement::GetFontLabel()
{
return mFontLabel;
}
std::string TextElement::GetContent()
{
return mContent;
}

View file

@ -0,0 +1,31 @@
#pragma once
#include <memory>
#include <string>
#include "DiscretePoint.h"
class TextElement
{
std::string mContent;
DiscretePoint mLocation;
std::string mFontLabel;
public:
TextElement(const std::string& content, const DiscretePoint& loc);
~TextElement();
static std::shared_ptr<TextElement> Create(const std::string& content, const DiscretePoint& loc);
DiscretePoint GetLocation();
std::string GetContent();
void SetContent(const std::string& content);
std::string GetFontLabel();
};
using TextElementPtr = std::shared_ptr<TextElement>;

View file

@ -0,0 +1,38 @@
#include "VisualLayer.h"
VisualLayer::VisualLayer()
: mShape(),
mText()
{
}
std::shared_ptr<VisualLayer> VisualLayer::Create()
{
return std::make_shared<VisualLayer>();
}
void VisualLayer::SetShape(GeometryElementPtr shape)
{
mShape = shape;
}
void VisualLayer::SetText(TextElementPtr text)
{
mText = text;
}
bool VisualLayer::HasShape()
{
return bool(mShape);
}
bool VisualLayer::HasText()
{
return bool(mText);
}
GeometryElementPtr VisualLayer::GetShape()
{
return mShape;
}
TextElementPtr VisualLayer::GetText()
{
return mText;
}

View file

@ -0,0 +1,26 @@
#pragma once
#include <memory>
#include "GeometryElement.h"
#include "TextElement.h"
class VisualLayer
{
GeometryElementPtr mShape;
TextElementPtr mText;
public:
VisualLayer();
static std::shared_ptr<VisualLayer> Create();
void SetShape(GeometryElementPtr shape);
void SetText(TextElementPtr text);
bool HasShape();
bool HasText();
GeometryElementPtr GetShape();
TextElementPtr GetText();
};
using VisualLayerPtr = std::shared_ptr<VisualLayer>;