Back with clickable button.

This commit is contained in:
James Grogan 2022-11-16 17:00:55 +00:00
parent 3e53bd9e00
commit 70891ce7b4
20 changed files with 107 additions and 50 deletions

View file

@ -57,10 +57,11 @@ void MediaTool::initializeViews()
auto button = Button::Create(); auto button = Button::Create();
button->setLabel("Click!"); button->setLabel("Click!");
button->setBounds(100, 200); button->setBounds(100, 200);
button->setBackgroundColor(Color(0, 0, 255, 0)); button->setBackgroundColor(Color(255, 0, 255, 1));
auto background_widget = Widget::Create(); auto background_widget = Widget::Create();
background_widget->setBackgroundColor(Color(0, 255, 255, 0)); background_widget->setName("BackgroundWidget");
background_widget->setBackgroundColor(Color(0, 255, 255, 1));
auto horizontal_spacer = HorizontalSpacer::Create(); auto horizontal_spacer = HorizontalSpacer::Create();
horizontal_spacer->addWidgetWithScale(std::move(button), 1); horizontal_spacer->addWidgetWithScale(std::move(button), 1);

View file

@ -65,7 +65,7 @@ void OpenGlMeshPainter::paint(const std::vector<float>& verts, const std::vector
int vertexColorLocation = glGetUniformLocation(mShaderProgram->getHandle(), "ourColor"); int vertexColorLocation = glGetUniformLocation(mShaderProgram->getHandle(), "ourColor");
glUniform4f(vertexColorLocation, float(color[0]), float(color[1]), float(color[2]), float(color[3])); glUniform4f(vertexColorLocation, float(color[0]), float(color[1]), float(color[2]), float(color[3]));
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); //glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
glBindVertexArray(0); glBindVertexArray(0);
} }

View file

@ -54,17 +54,17 @@ void OpenGlPainter::paint(DrawingContext* context)
glClearColor(0.5, 0.5, 1.0, 0.0); glClearColor(0.5, 0.5, 1.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
for(unsigned idx=0; idx<context->getSurface()->getScene()->getNumItems(); idx++) auto scene = context->getSurface()->getScene();
for(unsigned idx=0; idx<scene->getNumItems(); idx++)
{ {
auto scene_item = context->getSurface()->getScene()->getItem(idx); auto scene_item = scene->getItem(idx);
if (scene_item->getType() == SceneItem::Type::MODEL) if (scene_item->getType() == SceneItem::Type::MODEL)
{ {
mMeshPainter->paint(dynamic_cast<SceneModel*>(scene_item), context); mMeshPainter->paint(dynamic_cast<SceneModel*>(scene_item), context);
} }
else else if (scene_item->getType() == SceneItem::Type::TEXT)
{ {
mTextPainter->paint(dynamic_cast<SceneText*>(scene_item), context); mTextPainter->paint(dynamic_cast<SceneText*>(scene_item), context);
break;
} }
} }

View file

@ -120,9 +120,10 @@ void Window::doPaint(mt::Screen* screen)
{ {
mPlatformWindow->beforePaint(screen); mPlatformWindow->beforePaint(screen);
if (mScene && mScene->getRootNode()->getNumChildren() == 0) auto scene = getScene();
if (scene->getRootNode()->getNumChildren() == 0)
{ {
mScene->getRootNode()->addChild(mWidget->getRootNode()); scene->getRootNode()->addChild(mWidget->getRootNode());
} }
mDrawingContext->paint(); mDrawingContext->paint();
@ -139,4 +140,9 @@ void Window::clear()
} }
} }
bool Window::isDirty() const
{
return mWidget->needsUpdate();
}
} }

View file

@ -59,6 +59,8 @@ public:
void setSize(unsigned width, unsigned height) override; void setSize(unsigned width, unsigned height) override;
bool isDirty() const;
private: private:
WidgetPtr mWidget {nullptr}; WidgetPtr mWidget {nullptr};
IPlatformWindowPtr mPlatformWindow {nullptr}; IPlatformWindowPtr mPlatformWindow {nullptr};

View file

@ -15,7 +15,7 @@ Button::Button()
mClickedColor(Color(180, 180, 180)), mClickedColor(Color(180, 180, 180)),
mClickFunc() mClickFunc()
{ {
mName = "Button";
} }
std::unique_ptr<Button> Button::Create() std::unique_ptr<Button> Button::Create()
@ -73,6 +73,8 @@ void Button::updateLabel(const PaintEvent* event)
if (!mTextNode) if (!mTextNode)
{ {
mTextNode = TextNode::Create(mLabel, middle); mTextNode = TextNode::Create(mLabel, middle);
mTextNode->setName(mName + "_TextNode");
mTextNode->setContent(mLabel);
mRootNode->addChild(mTextNode.get()); mRootNode->addChild(mTextNode.get());
} }

View file

@ -8,7 +8,7 @@ HorizontalSpacer::HorizontalSpacer()
: Widget(), : Widget(),
mScales() mScales()
{ {
mName = "HorizontalSpacer";
} }
std::unique_ptr<HorizontalSpacer> HorizontalSpacer::Create() std::unique_ptr<HorizontalSpacer> HorizontalSpacer::Create()

View file

@ -26,7 +26,7 @@ Widget::Widget()
mBorderColor(Color(0, 0, 0)), mBorderColor(Color(0, 0, 0)),
mVisible(true) mVisible(true)
{ {
mName = "Widget";
} }
Widget::~Widget() Widget::~Widget()
@ -111,7 +111,7 @@ void Widget::setBackgroundColor(const Color& color)
{ {
if (mBackgroundColor != color) if (mBackgroundColor != color)
{ {
mBackgroundColor = std::move(color); mBackgroundColor = color;
mMaterialDirty = true; mMaterialDirty = true;
} }
} }
@ -169,6 +169,8 @@ void Widget::onPaintEvent(const PaintEvent* event)
if (isDirty()) if (isDirty())
{ {
mRootNode->setName(mName);
doPaint(event); doPaint(event);
mTransformDirty = false; mTransformDirty = false;
@ -284,6 +286,8 @@ void Widget::updateBackground(const PaintEvent* event)
if (!mBackgroundNode) if (!mBackgroundNode)
{ {
mBackgroundNode = std::make_unique<RectangleNode>(DiscretePoint(locX, locY), deltaX, deltaY);
mBackgroundNode->setName(mName + "_BackgroundNode");
mRootNode->addChild(mBackgroundNode.get()); mRootNode->addChild(mBackgroundNode.get());
} }

View file

@ -5,6 +5,7 @@
#include <memory> #include <memory>
#include <vector> #include <vector>
#include <string>
class MouseEvent; class MouseEvent;
class KeyboardEvent; class KeyboardEvent;
@ -103,6 +104,13 @@ public:
void setVisible(bool visible); void setVisible(bool visible);
void setName(const std::string& name)
{
mName = name;
}
bool needsUpdate() const;
protected: protected:
virtual bool onMyKeyboardEvent(const KeyboardEvent* event); virtual bool onMyKeyboardEvent(const KeyboardEvent* event);
@ -114,11 +122,6 @@ protected:
virtual void updateChildLocations(); virtual void updateChildLocations();
void addMyLayers();
bool needsUpdate() const;
protected:
virtual bool isDirty() const; virtual bool isDirty() const;
DiscretePoint mLocation; DiscretePoint mLocation;
@ -139,6 +142,7 @@ protected:
bool mMaterialDirty{true}; bool mMaterialDirty{true};
bool mVisibilityDirty{true}; bool mVisibilityDirty{true};
std::string mName;
std::vector<TransformNode*> mPendingChildNodes; std::vector<TransformNode*> mPendingChildNodes;
}; };

View file

@ -13,8 +13,9 @@ class FontsManager;
class AbstractVisualNode class AbstractVisualNode
{ {
public: public:
AbstractVisualNode(const DiscretePoint& location) AbstractVisualNode(const DiscretePoint& location, const std::string& name = {})
: mLocation(location) : mLocation(location),
mName(name)
{ {
} }
@ -61,6 +62,16 @@ public:
return mChildren.size(); return mChildren.size();
} }
void setName(const std::string& name)
{
mName = name;
}
const std::string& getName() const
{
return mName;
}
protected: protected:
DiscretePoint mLocation; DiscretePoint mLocation;
std::unique_ptr<SceneItem> mSceneItem; std::unique_ptr<SceneItem> mSceneItem;
@ -70,4 +81,5 @@ protected:
bool mIsVisible{true}; bool mIsVisible{true};
bool mTransformIsDirty{true}; bool mTransformIsDirty{true};
std::string mName;
}; };

View file

@ -1,5 +1,7 @@
#include "MaterialNode.h" #include "MaterialNode.h"
#include <iostream>
MaterialNode::MaterialNode(const DiscretePoint& location) MaterialNode::MaterialNode(const DiscretePoint& location)
: AbstractVisualNode(location), : AbstractVisualNode(location),
mFillColor(Color(255, 255, 255)), mFillColor(Color(255, 255, 255)),

View file

@ -71,6 +71,7 @@ void RectangleNode::update(FontsManager* fontsManager)
if (!mSceneItem) if (!mSceneItem)
{ {
mSceneItem = std::make_unique<SceneModel>(std::move(mesh)); mSceneItem = std::make_unique<SceneModel>(std::move(mesh));
mSceneItem->setName(mName + "_Model");
} }
else else
{ {

View file

@ -4,10 +4,12 @@
#include "FontsManager.h" #include "FontsManager.h"
#include "SceneItem.h" #include "SceneItem.h"
#include <iostream>
Scene::Scene() Scene::Scene()
: mRootNode(std::make_unique<RootNode>()) : mRootNode(std::make_unique<RootNode>())
{ {
mRootNode->setName("Scene_RootNode");
} }
void Scene::update(FontsManager* fontsManager) void Scene::update(FontsManager* fontsManager)
@ -23,8 +25,12 @@ void Scene::updateNode(AbstractVisualNode* node, FontsManager* fontsManager)
} }
node->update(fontsManager); node->update(fontsManager);
if(auto item = node->getSceneItem())
{
mSceneItems.push_back(node->getSceneItem()); mSceneItems.push_back(node->getSceneItem());
} }
}
RootNode* Scene::getRootNode() const RootNode* Scene::getRootNode() const
{ {

View file

@ -3,12 +3,15 @@
#include "Color.h" #include "Color.h"
#include "Transform.h" #include "Transform.h"
#include <string>
class SceneItem class SceneItem
{ {
public: public:
enum class Type enum class Type
{ {
UNSET,
MODEL, MODEL,
TEXT TEXT
}; };
@ -31,6 +34,16 @@ public:
void updateTransform(const Transform& transform); void updateTransform(const Transform& transform);
void setName(const std::string& name)
{
mName = name;
}
const std::string& getName() const
{
return mName;
}
protected: protected:
Transform mTransform; Transform mTransform;
Color mUniformColor; Color mUniformColor;
@ -38,4 +51,6 @@ protected:
bool mColorIsDirty{true}; bool mColorIsDirty{true};
bool mTransformIsDirty{true}; bool mTransformIsDirty{true};
bool mIsVisible{true}; bool mIsVisible{true};
std::string mName;
}; };

View file

@ -10,6 +10,8 @@
#include "Color.h" #include "Color.h"
#include <iostream>
TextNode::TextNode(const std::string& content, const DiscretePoint& loc) TextNode::TextNode(const std::string& content, const DiscretePoint& loc)
: MaterialNode(loc) : MaterialNode(loc)
{ {
@ -50,9 +52,10 @@ void TextNode::update(FontsManager* fontsManager)
if (!mSceneItem) if (!mSceneItem)
{ {
mSceneItem = std::make_unique<SceneText>(); mSceneItem = std::make_unique<SceneText>();
mSceneItem->setName(mName + "_SceneText");
} }
if (!mContentIsDirty) if (mContentIsDirty)
{ {
dynamic_cast<SceneText*>(mSceneItem.get())->setContent(mContent); dynamic_cast<SceneText*>(mSceneItem.get())->setContent(mContent);
mContentIsDirty = false; mContentIsDirty = false;

View file

@ -9,7 +9,6 @@ DesktopManager::DesktopManager(AbstractDesktopApp* application)
mWindowManager(WindowManager::Create()), mWindowManager(WindowManager::Create()),
mKeyboard(Keyboard::Create()), mKeyboard(Keyboard::Create()),
mUiApplication(application), mUiApplication(application),
mModified(false),
mEventManager(EventManager::Create()) mEventManager(EventManager::Create())
{ {
@ -45,14 +44,8 @@ void DesktopManager::onMouseEvent(const MouseEvent* event)
mWindowManager->onMouseEvent(event); mWindowManager->onMouseEvent(event);
} }
bool DesktopManager::isModified() const
{
return mModified;
}
void DesktopManager::onUiEvent(UiEventUPtr eventUPtr) void DesktopManager::onUiEvent(UiEventUPtr eventUPtr)
{ {
mModified = false;
const auto event = mEventManager->AddEvent(std::move(eventUPtr)); const auto event = mEventManager->AddEvent(std::move(eventUPtr));
switch (event->GetType()) switch (event->GetType())
{ {
@ -64,28 +57,18 @@ void DesktopManager::onUiEvent(UiEventUPtr eventUPtr)
case (UiEvent::Type::Keyboard): case (UiEvent::Type::Keyboard):
{ {
onKeyboardEvent(dynamic_cast<const KeyboardEvent*>(event)); onKeyboardEvent(dynamic_cast<const KeyboardEvent*>(event));
mModified = true;
break; break;
} }
case (UiEvent::Type::Resize): case (UiEvent::Type::Resize):
{ {
auto resize_event = dynamic_cast<const ResizeEvent*>(event); auto resize_event = dynamic_cast<const ResizeEvent*>(event);
mWindowManager->onResizeEvent(resize_event); mWindowManager->onResizeEvent(resize_event);
mModified = true;
break; break;
} }
case (UiEvent::Type::Mouse): case (UiEvent::Type::Mouse):
{ {
auto mouseEvent = dynamic_cast<const MouseEvent*>(event); auto mouseEvent = dynamic_cast<const MouseEvent*>(event);
onMouseEvent(mouseEvent); onMouseEvent(mouseEvent);
if (mouseEvent->GetAction() == MouseEvent::Action::Pressed)
{
mModified = true;
}
else if(mouseEvent->GetAction() == MouseEvent::Action::Released)
{
mModified = true;
}
break; break;
} }
default: default:
@ -93,11 +76,6 @@ void DesktopManager::onUiEvent(UiEventUPtr eventUPtr)
} }
} }
void DesktopManager::setIsModified(bool modified)
{
mModified = modified;
}
Keyboard* DesktopManager::getKeyboard() const Keyboard* DesktopManager::getKeyboard() const
{ {
return mKeyboard.get(); return mKeyboard.get();
@ -136,3 +114,10 @@ WindowManager* DesktopManager::getWindowManager() const
{ {
return mWindowManager.get(); return mWindowManager.get();
} }
void DesktopManager::onLoopIteration()
{
auto defaultScreen = getDefaultScreen();
mWindowManager->onLoopIteration(defaultScreen);
}

View file

@ -37,10 +37,6 @@ public:
mt::Screen* getDefaultScreen() const; mt::Screen* getDefaultScreen() const;
bool isModified() const;
void setIsModified(bool modified);
void setKeyboard(KeyboardUPtr keyboard); void setKeyboard(KeyboardUPtr keyboard);
void onUiEvent(UiEventUPtr event); void onUiEvent(UiEventUPtr event);
@ -53,13 +49,14 @@ public:
void clearEvents(); void clearEvents();
void onLoopIteration();
private: private:
std::vector<ScreenPtr> mScreens; std::vector<ScreenPtr> mScreens;
WindowManagerUPtr mWindowManager; WindowManagerUPtr mWindowManager;
KeyboardUPtr mKeyboard; KeyboardUPtr mKeyboard;
EventManagerUPtr mEventManager; EventManagerUPtr mEventManager;
AbstractDesktopApp* mUiApplication; AbstractDesktopApp* mUiApplication;
bool mModified;
}; };
using DesktopManagerUPtr = std::unique_ptr<DesktopManager>; using DesktopManagerUPtr = std::unique_ptr<DesktopManager>;

View file

@ -1,5 +1,7 @@
#include "WindowManager.h" #include "WindowManager.h"
#include <iostream>
WindowManager::WindowManager() WindowManager::WindowManager()
: mWindows() : mWindows()
{ {
@ -60,3 +62,15 @@ mt::Window* WindowManager::getMainWindow() const
return nullptr; return nullptr;
} }
} }
void WindowManager::onLoopIteration(mt::Screen* screen)
{
for (auto& window : mWindows)
{
if (window->isDirty())
{
window->onPaint(nullptr);
window->doPaint(screen);
}
}
}

View file

@ -42,6 +42,8 @@ public:
return mWindows[idx].get(); return mWindows[idx].get();
} }
void onLoopIteration(mt::Screen* screen);
private: private:
std::vector<WindowUPtr> mWindows; std::vector<WindowUPtr> mWindows;
}; };

View file

@ -239,6 +239,7 @@ void XcbInterface::loop()
break; break;
} }
free(event); free(event);
mDesktopManager->onLoopIteration();
} }
} }