Dawn/src/dawn/scene/components/ui/UIComponent.hpp

92 lines
2.5 KiB
C++

// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "UICanvas.hpp"
#include "util/mathutils.hpp"
namespace Dawn {
enum UIComponentAlign {
UI_COMPONENT_ALIGN_START,
UI_COMPONENT_ALIGN_MIDDLE,
UI_COMPONENT_ALIGN_END,
UI_COMPONENT_ALIGN_STRETCH
};
class UIComponentRenderable {
public:
/**
* Implemented UI Components that have rendering should implement this and
* return their rendering items. Items are passed back to the render
* pipeline.
*
* @param projection Camera projection, obtained from the canvas.
* @param view Camera view, obtained from the canvas.
* @return A list of renderable shader pass items for this renderable.
*/
virtual std::vector<struct ShaderPassItem> getPassItems(
glm::mat4 projection,
glm::mat4 view
) = 0;
};
class UIComponent : public SceneItemComponent, public UIComponentDimensional {
protected:
// @optional
float_t width = 1;
// @optional
float_t height = 1;
StateEvent<> eventAlignmentUpdated;
/**
* Simply returns this UI Components' dimensional parent, used for the
* alignment of this item.
*
* @return Pointer to the parent dimensional.
*/
UIComponentDimensional * getParentDimensional();
/**
* Internal method to update the alignment of this item.
*/
void updateAlignment();
public:
StateProperty<bool_t> alignmentNeedsUpdating;
/**
* Method used to calculate alignment dimensions.
*
* @param align Alignment value enumator.
* @param position Output position floating point.
* @param size Output size floating point.
* @param outerSize Outer size (of the parent).
* @param innerSize Inner size (of this element's content).
* @param alignment Alignment settings.
*/
static void calculateDimensions(
enum UIComponentAlign align,
float_t *position,
float_t *size,
float_t outerSize,
float_t innerSize,
glm::vec2 alignment
);
StateProperty<UIComponentAlign> alignX;
StateProperty<UIComponentAlign> alignY;
StateProperty<glm::vec4> alignment;
UIComponent(SceneItem *item);
float_t getWidth() override;
float_t getHeight() override;
void onStart() override;
friend class UICanvas;
};
}