add splitbox ui element

This commit is contained in:
MihailRis 2025-03-10 23:09:07 +03:00
parent c6492b3d25
commit 9a01b5ad2d
3 changed files with 96 additions and 0 deletions

View File

@ -0,0 +1,62 @@
#include "SplitBox.hpp"
using namespace gui;
SplitBox::SplitBox(const glm::vec2& size, float splitPos, Orientation orientation)
: Container(size), splitPos(splitPos), orientation(orientation) {
setCursor(
orientation == Orientation::vertical ? CursorShape::NS_RESIZE
: CursorShape::EW_RESIZE
);
}
void SplitBox::mouseMove(GUI*, int x, int y) {
auto pos = calcPos();
auto size = getSize();
glm::ivec2 cursor(x - pos.x, y - pos.y);
int axis = orientation == Orientation::vertical;
int v = cursor[axis];
v = std::max(std::min(static_cast<int>(size[axis]) - 10, v), 10);
float t = v / size[axis];
splitPos = t;
refresh();
}
void SplitBox::refresh() {
Container::refresh();
if (nodes.empty()) {
return;
}
glm::vec2 size = getSize();
if (nodes.size() == 1) {
auto node = nodes.at(0);
node->setPos(glm::vec2());
node->setSize(size);
return;
}
auto nodeA = nodes.at(0);
auto nodeB = nodes.at(1);
nodeA->setPos(glm::vec2());
if (orientation == Orientation::vertical) {
float splitPos = this->splitPos * size.y;
nodeA->setSize(glm::vec2(size.x, splitPos - splitRadius));
nodeB->setSize(glm::vec2(size.x, size.y - splitPos - splitRadius));
nodeB->setPos(glm::vec2(0.0f, splitPos + splitRadius));
} else {
float splitPos = this->splitPos * size.x;
nodeA->setSize(glm::vec2(splitPos - splitRadius, size.y));
nodeB->setSize(glm::vec2(size.x - splitPos - splitRadius, size.y));
nodeB->setPos(glm::vec2(splitPos + splitRadius, 0.0f));
}
}
void SplitBox::fullRefresh() {
refresh();
reposition();
Container::fullRefresh();
}

View File

@ -0,0 +1,18 @@
#pragma once
#include "Container.hpp"
namespace gui {
class SplitBox : public Container {
public:
SplitBox(const glm::vec2& size, float splitPos, Orientation orientation);
virtual void mouseMove(GUI*, int x, int y) override;
virtual void refresh() override;
virtual void fullRefresh() override;
private:
float splitPos;
int splitRadius = 2;
Orientation orientation;
};
}

View File

@ -7,6 +7,7 @@
#include "elements/Canvas.hpp"
#include "elements/CheckBox.hpp"
#include "elements/TextBox.hpp"
#include "elements/SplitBox.hpp"
#include "elements/TrackBar.hpp"
#include "elements/InputBindBox.hpp"
#include "elements/InventoryView.hpp"
@ -313,6 +314,20 @@ static std::shared_ptr<UINode> read_container(
return container;
}
static std::shared_ptr<UINode> read_split_box(
UiXmlReader& reader, const xml::xmlelement& element
) {
float splitPos = element.attr("split-pos", "0.5").asFloat();
Orientation orientation =
element.attr("orientation", "vertical").getText() == "horizontal"
? Orientation::horizontal
: Orientation::vertical;
auto splitBox =
std::make_shared<SplitBox>(glm::vec2(), splitPos, orientation);
read_container_impl(reader, element, *splitBox);
return splitBox;
}
static std::shared_ptr<UINode> read_panel(
UiXmlReader& reader, const xml::xmlelement& element
) {
@ -677,6 +692,7 @@ UiXmlReader::UiXmlReader(const scriptenv& env) : env(env) {
add("button", read_button);
add("textbox", read_text_box);
add("pagebox", read_page_box);
add("splitbox", read_split_box);
add("checkbox", read_check_box);
add("trackbar", read_track_bar);
add("container", read_container);