DeltaGrapher is an UI element now
This commit is contained in:
+31
-28
@@ -51,7 +51,7 @@ namespace gui {
|
|||||||
class Container;
|
class Container;
|
||||||
class PagesControl;
|
class PagesControl;
|
||||||
|
|
||||||
/** The main UI controller */
|
/// @brief The main UI controller
|
||||||
class GUI {
|
class GUI {
|
||||||
std::shared_ptr<Container> container;
|
std::shared_ptr<Container> container;
|
||||||
std::shared_ptr<UINode> hover = nullptr;
|
std::shared_ptr<UINode> hover = nullptr;
|
||||||
@@ -67,55 +67,58 @@ namespace gui {
|
|||||||
GUI();
|
GUI();
|
||||||
~GUI();
|
~GUI();
|
||||||
|
|
||||||
/** Get the main menu (PagesControl) node */
|
/// @brief Get the main menu (PagesControl) node
|
||||||
std::shared_ptr<PagesControl> getMenu();
|
std::shared_ptr<PagesControl> getMenu();
|
||||||
|
|
||||||
/** Get current focused node
|
/// @brief Get current focused node
|
||||||
* @return focused node or nullptr */
|
/// @return focused node or nullptr
|
||||||
std::shared_ptr<UINode> getFocused() const;
|
std::shared_ptr<UINode> getFocused() const;
|
||||||
|
|
||||||
/** Check if all user input is caught by some element like TextBox */
|
/// @brief Check if all user input is caught by some element like TextBox
|
||||||
bool isFocusCaught() const;
|
bool isFocusCaught() const;
|
||||||
|
|
||||||
/** Main input handling and logic update method
|
/// @brief Main input handling and logic update method
|
||||||
* @param delta delta time */
|
/// @param delta delta time
|
||||||
void act(float delta);
|
void act(float delta);
|
||||||
|
|
||||||
/** Draw all visible elements on main container
|
/// @brief Draw all visible elements on main container
|
||||||
* @param pctx parent graphics context
|
/// @param pctx parent graphics context
|
||||||
* @param assets active assets storage */
|
/// @param assets active assets storage
|
||||||
void draw(const GfxContext* pctx, Assets* assets);
|
void draw(const GfxContext* pctx, Assets* assets);
|
||||||
|
|
||||||
/** Add node to the main container */
|
/// @brief Add element to the main container
|
||||||
|
/// @param node UI element
|
||||||
void add(std::shared_ptr<UINode> node);
|
void add(std::shared_ptr<UINode> node);
|
||||||
|
|
||||||
/** Remove node from the main container */
|
/// @brief Add element to the main container
|
||||||
|
/// @param node UI element
|
||||||
|
/// @param coord element position within the main container
|
||||||
|
void add(std::shared_ptr<UINode> node, glm::vec2 coord);
|
||||||
|
|
||||||
|
/// @brief Remove node from the main container
|
||||||
void remove(std::shared_ptr<UINode> node) noexcept;
|
void remove(std::shared_ptr<UINode> node) noexcept;
|
||||||
|
|
||||||
/** Store node in the GUI nodes dictionary
|
/// @brief Store node in the GUI nodes dictionary
|
||||||
* (does not add node to the main container)
|
/// (does not add node to the main container)
|
||||||
* @param name node key
|
/// @param name node key
|
||||||
* @param node target node
|
/// @param node target node
|
||||||
*/
|
|
||||||
void store(std::string name, std::shared_ptr<UINode> node);
|
void store(std::string name, std::shared_ptr<UINode> node);
|
||||||
|
|
||||||
/** Get node from the GUI nodes dictionary
|
/// @brief Get node from the GUI nodes dictionary
|
||||||
* @param name node key
|
/// @param name node key
|
||||||
* @return stored node or nullptr
|
/// @return stored node or nullptr
|
||||||
*/
|
|
||||||
std::shared_ptr<UINode> get(std::string name) noexcept;
|
std::shared_ptr<UINode> get(std::string name) noexcept;
|
||||||
|
|
||||||
/** Remove node from the GUI nodes dictionary
|
/// @brief Remove node from the GUI nodes dictionary
|
||||||
* @param name node key
|
/// @param name node key
|
||||||
*/
|
|
||||||
void remove(std::string name) noexcept;
|
void remove(std::string name) noexcept;
|
||||||
|
|
||||||
/** Set node as focused
|
/// @brief Set node as focused
|
||||||
* @param node new focused node or nullptr to remove focus
|
/// @param node new focused node or nullptr to remove focus
|
||||||
*/
|
|
||||||
void setFocus(std::shared_ptr<UINode> node);
|
void setFocus(std::shared_ptr<UINode> node);
|
||||||
|
|
||||||
/** Get the main container */
|
/// @brief Get the main container
|
||||||
|
/// @deprecated
|
||||||
std::shared_ptr<Container> getContainer() const;
|
std::shared_ptr<Container> getContainer() const;
|
||||||
|
|
||||||
void postRunnable(runnable callback);
|
void postRunnable(runnable callback);
|
||||||
|
|||||||
+45
-25
@@ -59,6 +59,43 @@ extern std::shared_ptr<gui::UINode> create_debug_panel(
|
|||||||
Player* player
|
Player* player
|
||||||
);
|
);
|
||||||
|
|
||||||
|
class DeltaGrapher : public gui::UINode {
|
||||||
|
std::unique_ptr<int[]> points;
|
||||||
|
float multiplier;
|
||||||
|
int index = 0;
|
||||||
|
int dmwidth;
|
||||||
|
int dmheight;
|
||||||
|
public:
|
||||||
|
DeltaGrapher(uint width, uint height, float multiplier)
|
||||||
|
: gui::UINode(glm::vec2(width, height)),
|
||||||
|
multiplier(multiplier),
|
||||||
|
dmwidth(width),
|
||||||
|
dmheight(height)
|
||||||
|
{
|
||||||
|
points = std::make_unique<int[]>(width);
|
||||||
|
}
|
||||||
|
|
||||||
|
void act(float delta) override {
|
||||||
|
index = index + 1 % dmwidth;
|
||||||
|
int value = static_cast<int>(delta * multiplier);
|
||||||
|
points[index % dmwidth] = std::min(value, dmheight);
|
||||||
|
}
|
||||||
|
|
||||||
|
void draw(const GfxContext* pctx, Assets* assets) override {
|
||||||
|
glm::vec2 pos = calcPos();
|
||||||
|
auto batch = pctx->getBatch2D();
|
||||||
|
batch->texture(nullptr);
|
||||||
|
batch->lineWidth(1);
|
||||||
|
for (int i = index+1; i < index+dmwidth; i++) {
|
||||||
|
int j = i % dmwidth;
|
||||||
|
batch->line(
|
||||||
|
pos.x + i - index, pos.y + size.y - points[j],
|
||||||
|
pos.x + i - index, pos.y + size.y, 1.0f, 1.0f, 1.0f, 0.2f
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
HudElement::HudElement(
|
HudElement::HudElement(
|
||||||
hud_element_mode mode,
|
hud_element_mode mode,
|
||||||
UiDocument* document,
|
UiDocument* document,
|
||||||
@@ -70,6 +107,7 @@ HudElement::HudElement(
|
|||||||
void HudElement::update(bool pause, bool inventoryOpen, bool debugMode) {
|
void HudElement::update(bool pause, bool inventoryOpen, bool debugMode) {
|
||||||
if (debug && !debugMode) {
|
if (debug && !debugMode) {
|
||||||
node->setVisible(false);
|
node->setVisible(false);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
switch (mode) {
|
switch (mode) {
|
||||||
case hud_element_mode::permanent:
|
case hud_element_mode::permanent:
|
||||||
@@ -189,6 +227,13 @@ Hud::Hud(Engine* engine, LevelFrontend* frontend, Player* player)
|
|||||||
gui->add(debugPanel);
|
gui->add(debugPanel);
|
||||||
gui->add(contentAccessPanel);
|
gui->add(contentAccessPanel);
|
||||||
gui->add(grabbedItemView);
|
gui->add(grabbedItemView);
|
||||||
|
|
||||||
|
auto dgrapher = std::make_shared<DeltaGrapher>(350, 250, 2000);
|
||||||
|
dgrapher->setPositionFunc([=]() {
|
||||||
|
glm::vec2 size = dgrapher->getSize();
|
||||||
|
return glm::vec2(Window::width-size.x, Window::height-size.y);
|
||||||
|
});
|
||||||
|
add(HudElement(hud_element_mode::permanent, nullptr, dgrapher, true));
|
||||||
}
|
}
|
||||||
|
|
||||||
Hud::~Hud() {
|
Hud::~Hud() {
|
||||||
@@ -428,11 +473,6 @@ void Hud::remove(std::shared_ptr<gui::UINode> node) {
|
|||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
|
|
||||||
class DeltaGrapher : gui::UINode {
|
|
||||||
|
|
||||||
public:
|
|
||||||
};
|
|
||||||
|
|
||||||
void Hud::draw(const GfxContext& ctx){
|
void Hud::draw(const GfxContext& ctx){
|
||||||
const Viewport& viewport = ctx.getViewport();
|
const Viewport& viewport = ctx.getViewport();
|
||||||
const uint width = viewport.getWidth();
|
const uint width = viewport.getWidth();
|
||||||
@@ -465,26 +505,6 @@ void Hud::draw(const GfxContext& ctx){
|
|||||||
batch->flush();
|
batch->flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delta-time visualizer
|
|
||||||
if (player->debug) {
|
|
||||||
batch->texture(nullptr);
|
|
||||||
const int dmwidth = 256;
|
|
||||||
const float dmscale = 4000.0f;
|
|
||||||
static float deltameter[dmwidth]{};
|
|
||||||
static int index = 0;
|
|
||||||
index = index + 1 % dmwidth;
|
|
||||||
float delta = static_cast<float>(engine->getDelta());
|
|
||||||
deltameter[index%dmwidth] = glm::min(0.2f, delta)*dmscale;
|
|
||||||
batch->lineWidth(1);
|
|
||||||
for (int i = index+1; i < index+dmwidth; i++) {
|
|
||||||
int j = i % dmwidth;
|
|
||||||
batch->line(
|
|
||||||
width-dmwidth+i-index, height-deltameter[j],
|
|
||||||
width-dmwidth+i-index, height, 1.0f, 1.0f, 1.0f, 0.2f
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (inventoryOpen) {
|
if (inventoryOpen) {
|
||||||
float caWidth = inventoryView ? contentAccess->getSize().x : 0.0f;
|
float caWidth = inventoryView ? contentAccess->getSize().x : 0.0f;
|
||||||
contentAccessPanel->setPos(glm::vec2(width-caWidth, 0));
|
contentAccessPanel->setPos(glm::vec2(width-caWidth, 0));
|
||||||
|
|||||||
Reference in New Issue
Block a user