Panels scrolling, scissors test fix

This commit is contained in:
MihailRis
2023-11-23 13:36:24 +03:00
parent e6c9a38f0c
commit be807535f5
10 changed files with 97 additions and 15 deletions
+5 -1
View File
@@ -49,6 +49,9 @@ void GUI::act(float delta) {
}
if (hover) {
hover->hover(true);
if (Events::scroll) {
hover->scrolled(Events::scroll);
}
}
this->hover = hover;
@@ -87,12 +90,13 @@ void GUI::act(float delta) {
if (Events::clicked(mousecode::BUTTON_1)) {
focus->mouseMove(this, mx, my);
}
if (prevfocus == focus)
if (prevfocus == focus){
for (int i = mousecode::BUTTON_1; i < mousecode::BUTTON_1+12; i++) {
if (Events::jclicked(i)) {
focus->clicked(this, i);
}
}
}
}
}
if (focus && !focus->isfocused()) {
+7 -6
View File
@@ -83,11 +83,17 @@ shared_ptr<UINode> UINode::getAt(vec2 pos, shared_ptr<UINode> self) {
vec2 UINode::calcCoord() const {
if (parent) {
return coord + parent->calcCoord();
return coord + parent->calcCoord() + parent->contentOffset();
}
return coord;
}
void UINode::scrolled(int value) {
if (parent) {
parent->scrolled(value);
}
}
void UINode::setCoord(vec2 coord) {
this->coord = coord;
}
@@ -100,11 +106,6 @@ void UINode::size(vec2 size) {
if (sizelock)
return;
this->size_ = size;
if (parent) {
sizelock = true;
//parent->refresh();
sizelock = false;
}
}
void UINode::_size(vec2 size) {
+2
View File
@@ -61,6 +61,7 @@ namespace gui {
virtual void clicked(GUI*, int button) {}
virtual void mouseMove(GUI*, int x, int y) {};
virtual void mouseRelease(GUI*, int x, int y);
virtual void scrolled(int value);
bool ispressed() const;
void defocus();
@@ -73,6 +74,7 @@ namespace gui {
virtual bool isInside(glm::vec2 pos);
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self);
virtual glm::vec2 contentOffset() {return glm::vec2(0.0f);};
glm::vec2 calcCoord() const;
void setCoord(glm::vec2 coord);
glm::vec2 size() const;
+4
View File
@@ -59,12 +59,14 @@ void Label::size(vec2 sizenew) {
// ================================= Button ===================================
Button::Button(shared_ptr<UINode> content, glm::vec4 padding) : Panel(vec2(32,32), padding, 0) {
add(content);
scrollable(false);
}
Button::Button(wstring text, glm::vec4 padding) : Panel(vec2(32,32), padding, 0) {
Label* label = new Label(text);
label->align(Align::center);
add(shared_ptr<UINode>(label));
scrollable(false);
}
void Button::drawBackground(Batch2D* batch, Assets* assets) {
@@ -117,6 +119,7 @@ void TextBox::drawBackground(Batch2D* batch, Assets* assets) {
label->color(vec4(1.0f));
label->text(input);
}
scrollable(false);
}
void TextBox::typed(unsigned int codepoint) {
@@ -163,6 +166,7 @@ InputBindBox::InputBindBox(Binding& binding, vec4 padding)
binding(binding) {
label = new Label(L"");
add(label);
scrollable(false);
}
shared_ptr<UINode> InputBindBox::getAt(vec2 pos, shared_ptr<UINode> self) {
+41 -4
View File
@@ -14,6 +14,7 @@ using glm::vec2;
using glm::vec4;
Container::Container(vec2 coord, vec2 size) : UINode(coord, size) {
actualLength = size.y;
}
shared_ptr<UINode> Container::getAt(vec2 pos, shared_ptr<UINode> self) {
@@ -53,6 +54,24 @@ void Container::act(float delta) {
}
}
void Container::scrolled(int value) {
int diff = (actualLength-size().y);
if (diff > 0 && scrollable_) {
scroll += value * 20;
if (scroll > 0)
scroll = 0;
if (-scroll > diff) {
scroll = -diff;
}
} else if (parent) {
parent->scrolled(value);
}
}
void Container::scrollable(bool flag) {
scrollable_ = flag;
}
void Container::draw(Batch2D* batch, Assets* assets) {
vec2 coord = calcCoord();
vec2 size = this->size();
@@ -110,6 +129,14 @@ void Panel::drawBackground(Batch2D* batch, Assets* assets) {
batch->rect(coord.x, coord.y, size_.x, size_.y);
}
void Panel::maxLength(int value) {
maxLength_ = value;
}
int Panel::maxLength() const {
return maxLength_;
}
void Panel::refresh() {
float x = padding.x;
float y = padding.y;
@@ -141,8 +168,13 @@ void Panel::refresh() {
node->refresh();
maxw = fmax(maxw, ex+node->size().x+margin.z+padding.z);
}
if (resizing_)
this->size(vec2(size.x, y+padding.w));
if (resizing_) {
if (maxLength_)
this->size(vec2(size.x, min(maxLength_, (int)(y+padding.w))));
else
this->size(vec2(size.x, y+padding.w));
}
actualLength = y + padding.w;
} else {
float maxh = size.y;
for (auto& node : nodes) {
@@ -158,10 +190,15 @@ void Panel::refresh() {
maxh = fmax(maxh, y+margin.y+node->size().y+margin.w+padding.w);
}
bool increased = maxh > size.y;
if (resizing_)
this->size(vec2(x+padding.z, size.y));
if (resizing_) {
if (maxLength_)
this->size(vec2(min(maxLength_, (int)(x+padding.z)), size.y));
else
this->size(vec2(x+padding.z, size.y));
}
if (increased)
refresh();
actualLength = size.y;
}
}
+10
View File
@@ -27,6 +27,9 @@ namespace gui {
protected:
std::vector<std::shared_ptr<UINode>> nodes;
std::vector<IntervalEvent> intervalEvents;
int scroll = 0;
int actualLength = 0;
bool scrollable_ = true;
public:
Container(glm::vec2 coord, glm::vec2 size);
@@ -37,7 +40,10 @@ namespace gui {
virtual void add(std::shared_ptr<UINode> node);
virtual void add(UINode* node);
virtual void remove(std::shared_ptr<UINode> node);
virtual void scrolled(int value) override;
virtual void scrollable(bool flag);
void listenInterval(float interval, ontimeout callback, int repeat=-1);
virtual glm::vec2 contentOffset() override {return glm::vec2(0.0f, scroll);};
};
class Panel : public Container {
@@ -46,6 +52,7 @@ namespace gui {
glm::vec4 padding {2.0f};
float interval = 2.0f;
bool resizing_;
int maxLength_ = 0;
public:
Panel(glm::vec2 size, glm::vec4 padding=glm::vec4(2.0f), float interval=2.0f, bool resizing=true);
virtual ~Panel();
@@ -57,6 +64,9 @@ namespace gui {
virtual void refresh() override;
virtual void lock() override;
virtual void maxLength(int value);
int maxLength() const;
};
struct Page {