Dawn/src/dawn/ui/UIComponent.hpp

82 lines
2.4 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/components/ui/UICanvas.hpp"
#include "scene/Scene.hpp"
#include "display/Color.hpp"
#include "display/shader/UIShader.hpp"
namespace Dawn {
enum UIComponentAlign {
UI_COMPONENT_ALIGN_START,
UI_COMPONENT_ALIGN_MIDDLE,
UI_COMPONENT_ALIGN_END,
UI_COMPONENT_ALIGN_STRETCH
};
class UIComponent {
protected:
// Calculated (and cached) values
float_t width = 1;
float_t height = 1;
float_t relativeX = 0;
float_t relativeY = 0;
// Setting values
UIComponentAlign alignX = UI_COMPONENT_ALIGN_START;
UIComponentAlign alignY = UI_COMPONENT_ALIGN_START;
glm::vec4 alignment = glm::vec4(0, 0, 32, 32);
float_t z = 0;
std::vector<UIComponent*> children;
UIComponent *parent = nullptr;
// I currently don't support rotation or scale. Not because I can't but
// because it's basically un-necessary. Unity does support rotation but
// it doesn't affect how the alignment side of things work (similar to how
// CSS would handle things) When I need to support these I will add the
// code but right now it's not necessary
/**
* Updates the cached/stored values based on the setting internal values.
* You should watchdog this if you intend to do something when values are
* updated, e.g. if you need to resize a quad, or something.
*/
virtual void updatePositions();
public:
UICanvas &canvas;
UIComponent(UICanvas &canvas);
/**
* Returns the calculated width, based on the internal alignment values.
*
* @return Width of the component.
*/
float_t getWidth();
float_t getHeight();
float_t getRelativeX();
float_t getRelativeY();
void setTransform(
UIComponentAlign xAlign,
UIComponentAlign yAlign,
glm::vec4 alignment,
float_t z
);
// virtual void update() = 0;
void draw(UIShader &uiShader, glm::mat4 parentTransform);
virtual void drawSelf(UIShader &uiShader, glm::mat4 selfTransform) = 0;
void addChild(UIComponent *child);
void removeChild(UIComponent *child);
virtual ~UIComponent();
friend class UICanvas;
};
}