Files
Dawn/src/dawn/scene/components/ui/UICanvas.hpp

119 lines
3.3 KiB
C++

// Copyright (c) 2022 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "scene/SceneItemComponent.hpp"
#include "display/RenderTarget.hpp"
#include "scene/components/display/Camera.hpp"
namespace Dawn {
enum UIDrawType {
UI_DRAW_TYPE_WORLD_ABSOLUTE,
UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE,
UI_DRAW_TYPE_CAMERA_OVERLAY
};
class UIComponent;
struct UIMenu;
class UICanvas : public SceneItemComponent {
protected:
Camera *camera = nullptr;
struct UIMenu *currentMenu = nullptr;
void onRenderTargetResize(float_t w, float_t h);
void onSceneUpdate();
public:
/**
* Creates a UI Canvas Scene Item Element, and attaches it to the provided
* scene.
*
* @param scene Scene to create the UI Canvas for.
* @return Created UI Canvas.
*/
static UICanvas * create(Scene *scene);
//======================================================================//
std::vector<UIComponent*> children;
UIDrawType drawType = UI_DRAW_TYPE_WORLD_CAMERA_RELATIVE;
/**
* Constructs the UI Canvas Scene Item Component.
*
* @param item Item that this canvas item belongs to.
*/
UICanvas(SceneItem *item);
/**
* Sets the camera used by the UI canvas.
*
* @param camera Camera to set for the UI canvas.
*/
void setCamera(Camera *camera);
/**
* Construct and append a UI item to this UI Canvas.
*
* @tparam Type of the UI Item.
* @return Pointer to the created UI Item.
*/
template<class T>
T * addElement() {
auto item = new T(this);
this->children.push_back(item);
return item;
}
/**
* Find a UI Element attached to this canvas.
*
* @tparam Type of the UI item to find.
* @return Pointer to first matching element of type T.
*/
template<class T>
T * findElement() {
auto it = this->children.begin();
while(it != this->children.end()) {
auto castedAs = dynamic_cast<T*>(*it);
if(castedAs != nullptr) return castedAs;
++it;
}
return nullptr;
}
/**
* Returns the width of the root UI Canvas size. In future I may allow
* this to be dynamic, right now it uses the render canvas however.
*
* @return Width of the UI Canvas.
*/
float_t getWidth();
/**
* Returns the height of this UI Canvas element.
*
* @return Height of the UI Canvas.
*/
float_t getHeight();
/**
* Returns the currently active menu for this UI Canvas.
*
* @return The currently active menu, or nullptr if there is none.
*/
struct UIMenu * getCurrentMenu();
/**
* Sets the currently active menu, and ticks it appropriately.
*
* @param menu Menu to set as the current active menu.
*/
void setCurrentMenu(struct UIMenu *menu);
void onStart() override;
void onDispose() override;
};
}