Zepha/src/client/gui/Root.h

66 lines
1.4 KiB
C
Raw Normal View History

2021-08-20 22:48:38 -07:00
#pragma once
2021-08-19 13:33:08 -07:00
#include "client/gui/Gui.h"
#include "Element.h"
#include "client/Window.h"
#include "game/atlas/TextureAtlas.h"
/**
* Handles rendering and processing events for a Gui tree.
* Holds a `body` Box that is always the screen's size.
*/
2021-08-19 13:33:08 -07:00
namespace Gui {
class Root {
public:
Root(Window& window, TextureAtlas& atlas);
2021-08-20 16:19:10 -07:00
~Root();
/**
* Creates an element from the props provided, optionally with children.
* Does not add it to the tree, use body->append() for that.
*/
2021-08-19 13:33:08 -07:00
template<typename E, std::enable_if_t<std::is_base_of_v<Element, E>, bool> = true>
2021-08-24 01:48:53 -07:00
sptr<E> create(const Props& props = {}, const vec<sptr<Element>>& children = {}) {
let elem = make_shared<E>(*this, stylesheets);
2021-08-19 13:33:08 -07:00
elem->setProps(props);
for (const let& child : children) elem->append(child);
return elem;
};
/**
* Adds a stylesheet to the tree, which can provide
* styles for elements with specific classes.
*/
2021-08-24 01:48:53 -07:00
void addStylesheet(const StyleSheet& sheet);
2021-08-19 13:33:08 -07:00
/** Processes mouse events. */
void update();
2021-08-19 13:33:08 -07:00
/** Renders the Gui tree. */
2021-08-19 13:33:08 -07:00
void draw(Renderer& renderer);
/** The list of stylesheets to apply to the document. */
vec<StyleSheet> stylesheets;
/** The body element, which is the root of the tree. */
2021-08-19 13:33:08 -07:00
const sptr<Element> body;
/** The clients texture atlas. */
2021-08-19 13:33:08 -07:00
TextureAtlas& atlas;
private:
2021-08-19 13:33:08 -07:00
Window& window;
vec<ListenerRef> callbacks {};
2021-08-19 13:33:08 -07:00
};
}