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();
button->setLabel("Click!");
button->setBounds(100, 200);
button->setBackgroundColor(Color(0, 0, 255, 0));
button->setBackgroundColor(Color(255, 0, 255, 1));
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();
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");
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);
glBindVertexArray(0);
}

View file

@ -54,17 +54,17 @@ void OpenGlPainter::paint(DrawingContext* context)
glClearColor(0.5, 0.5, 1.0, 0.0);
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)
{
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);
break;
}
}

View file

@ -120,9 +120,10 @@ void Window::doPaint(mt::Screen* 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();
@ -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;
bool isDirty() const;
private:
WidgetPtr mWidget {nullptr};
IPlatformWindowPtr mPlatformWindow {nullptr};

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -1,5 +1,7 @@
#include "WindowManager.h"
#include <iostream>
WindowManager::WindowManager()
: mWindows()
{
@ -60,3 +62,15 @@ mt::Window* WindowManager::getMainWindow() const
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();
}
void onLoopIteration(mt::Screen* screen);
private:
std::vector<WindowUPtr> mWindows;
};

View file

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