133 lines
3.4 KiB
C++
133 lines
3.4 KiB
C++
#pragma once
|
|
|
|
#include "Renderer.h"
|
|
#include "TextureManager.h"
|
|
#include "Environment.h"
|
|
#include "external/nlohmann/json.hpp"
|
|
#include <string>
|
|
#include <vector>
|
|
#include <memory>
|
|
#include <functional>
|
|
|
|
namespace ZL {
|
|
|
|
using json = nlohmann::json;
|
|
|
|
struct UiRect {
|
|
float x = 0;
|
|
float y = 0;
|
|
float w = 0;
|
|
float h = 0;
|
|
bool contains(float px, float py) const {
|
|
return px >= x && px <= x + w && py >= y && py <= y + h;
|
|
}
|
|
};
|
|
|
|
enum class ButtonState {
|
|
Normal,
|
|
Hover,
|
|
Pressed
|
|
};
|
|
|
|
struct UiButton {
|
|
std::string name;
|
|
UiRect rect;
|
|
std::shared_ptr<Texture> texNormal;
|
|
std::shared_ptr<Texture> texHover;
|
|
std::shared_ptr<Texture> texPressed;
|
|
ButtonState state = ButtonState::Normal;
|
|
|
|
VertexRenderStruct mesh;
|
|
|
|
std::function<void(const std::string&)> onClick;
|
|
|
|
void buildMesh();
|
|
void draw(Renderer& renderer) const;
|
|
};
|
|
|
|
struct UiSlider {
|
|
std::string name;
|
|
UiRect rect;
|
|
std::shared_ptr<Texture> texTrack;
|
|
std::shared_ptr<Texture> texKnob;
|
|
|
|
VertexRenderStruct trackMesh;
|
|
VertexRenderStruct knobMesh;
|
|
|
|
float value = 0.0f;
|
|
bool vertical = true;
|
|
|
|
std::function<void(const std::string&, float)> onValueChanged;
|
|
|
|
void buildTrackMesh();
|
|
void buildKnobMesh();
|
|
void draw(Renderer& renderer) const;
|
|
};
|
|
|
|
struct UiNode {
|
|
std::string type;
|
|
UiRect rect;
|
|
std::string name;
|
|
std::vector<std::shared_ptr<UiNode>> children;
|
|
std::shared_ptr<UiButton> button;
|
|
std::shared_ptr<UiSlider> slider;
|
|
std::string orientation = "vertical";
|
|
float spacing = 0.0f;
|
|
};
|
|
|
|
class UiManager {
|
|
public:
|
|
UiManager() = default;
|
|
|
|
void loadFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile = "");
|
|
|
|
void draw(Renderer& renderer);
|
|
|
|
void onMouseMove(int x, int y);
|
|
void onMouseDown(int x, int y);
|
|
void onMouseUp(int x, int y);
|
|
|
|
bool isUiInteraction() const {
|
|
return pressedButton != nullptr || pressedSlider != nullptr;
|
|
}
|
|
|
|
std::shared_ptr<UiButton> findButton(const std::string& name);
|
|
|
|
bool setButtonCallback(const std::string& name, std::function<void(const std::string&)> cb);
|
|
|
|
bool addSlider(const std::string& name, const UiRect& rect, Renderer& renderer, const std::string& zipFile,
|
|
const std::string& trackPath, const std::string& knobPath, float initialValue = 0.0f, bool vertical = true);
|
|
|
|
std::shared_ptr<UiSlider> findSlider(const std::string& name);
|
|
bool setSliderCallback(const std::string& name, std::function<void(const std::string&, float)> cb);
|
|
bool setSliderValue(const std::string& name, float value); // programmatic set (clamped 0..1)
|
|
|
|
bool pushMenuFromFile(const std::string& path, Renderer& renderer, const std::string& zipFile = "");
|
|
bool popMenu();
|
|
void clearMenuStack();
|
|
|
|
private:
|
|
std::shared_ptr<UiNode> parseNode(const json& j, Renderer& renderer, const std::string& zipFile);
|
|
void layoutNode(const std::shared_ptr<UiNode>& node);
|
|
void collectButtonsAndSliders(const std::shared_ptr<UiNode>& node);
|
|
|
|
std::shared_ptr<UiNode> root;
|
|
std::vector<std::shared_ptr<UiButton>> buttons;
|
|
std::vector<std::shared_ptr<UiSlider>> sliders;
|
|
|
|
std::shared_ptr<UiButton> pressedButton;
|
|
std::shared_ptr<UiSlider> pressedSlider;
|
|
|
|
struct MenuState {
|
|
std::shared_ptr<UiNode> root;
|
|
std::vector<std::shared_ptr<UiButton>> buttons;
|
|
std::vector<std::shared_ptr<UiSlider>> sliders;
|
|
std::shared_ptr<UiButton> pressedButton;
|
|
std::shared_ptr<UiSlider> pressedSlider;
|
|
std::string path;
|
|
};
|
|
|
|
std::vector<MenuState> menuStack;
|
|
};
|
|
|
|
} // namespace ZL
|