frontend/gui refactor
This commit is contained in:
+104
-107
@@ -6,58 +6,53 @@
|
||||
#include "../../assets/Assets.h"
|
||||
#include "../../graphics/Batch2D.h"
|
||||
#include "../../graphics/Font.h"
|
||||
#include "../../graphics/GfxContext.h"
|
||||
#include "../../util/stringutil.h"
|
||||
#include "GUI.h"
|
||||
|
||||
using std::string;
|
||||
using std::wstring;
|
||||
using std::shared_ptr;
|
||||
using glm::vec2;
|
||||
using glm::vec3;
|
||||
using glm::vec4;
|
||||
|
||||
const uint KEY_ESCAPE = 256;
|
||||
const uint KEY_ENTER = 257;
|
||||
const uint KEY_BACKSPACE = 259;
|
||||
|
||||
using namespace gui;
|
||||
|
||||
Label::Label(string text, string fontName)
|
||||
Label::Label(std::string text, std::string fontName)
|
||||
: UINode(vec2(), vec2(text.length() * 8, 15)),
|
||||
text_(util::str2wstr_utf8(text)),
|
||||
text(util::str2wstr_utf8(text)),
|
||||
fontName_(fontName) {
|
||||
}
|
||||
|
||||
|
||||
Label::Label(wstring text, string fontName)
|
||||
Label::Label(std::wstring text, std::string fontName)
|
||||
: UINode(vec2(), vec2(text.length() * 8, 15)),
|
||||
text_(text),
|
||||
text(text),
|
||||
fontName_(fontName) {
|
||||
}
|
||||
|
||||
Label& Label::text(wstring text) {
|
||||
this->text_ = text;
|
||||
return *this;
|
||||
void Label::setText(std::wstring text) {
|
||||
this->text = text;
|
||||
}
|
||||
|
||||
wstring Label::text() const {
|
||||
return text_;
|
||||
std::wstring Label::getText() const {
|
||||
return text;
|
||||
}
|
||||
|
||||
void Label::draw(Batch2D* batch, Assets* assets) {
|
||||
void Label::draw(const GfxContext* pctx, Assets* assets) {
|
||||
if (supplier) {
|
||||
text(supplier());
|
||||
setText(supplier());
|
||||
}
|
||||
batch->color = color_;
|
||||
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->color = getColor();
|
||||
Font* font = assets->getFont(fontName_);
|
||||
vec2 size = UINode::size();
|
||||
vec2 newsize = vec2(font->calcWidth(text_), font->lineHeight());
|
||||
vec2 size = getSize();
|
||||
vec2 newsize = vec2(font->calcWidth(text), font->lineHeight());
|
||||
if (newsize.x > size.x) {
|
||||
this->size(newsize);
|
||||
setSize(newsize);
|
||||
size = newsize;
|
||||
}
|
||||
vec2 coord = calcCoord();
|
||||
font->draw(batch, text_, coord.x, coord.y);
|
||||
font->draw(batch, text, coord.x, coord.y);
|
||||
}
|
||||
|
||||
Label* Label::textSupplier(wstringsupplier supplier) {
|
||||
@@ -65,58 +60,61 @@ Label* Label::textSupplier(wstringsupplier supplier) {
|
||||
return this;
|
||||
}
|
||||
|
||||
void Label::size(vec2 sizenew) {
|
||||
UINode::size(vec2(UINode::size().x, sizenew.y));
|
||||
void Label::setSize(vec2 sizenew) {
|
||||
UINode::setSize(vec2(UINode::getSize().x, sizenew.y));
|
||||
}
|
||||
|
||||
// ================================= Image ====================================
|
||||
Image::Image(string texture, vec2 size) : UINode(vec2(), size), texture(texture) {
|
||||
Image::Image(std::string texture, vec2 size) : UINode(vec2(), size), texture(texture) {
|
||||
setInteractive(false);
|
||||
}
|
||||
|
||||
void Image::draw(Batch2D* batch, Assets* assets) {
|
||||
void Image::draw(const GfxContext* pctx, Assets* assets) {
|
||||
vec2 coord = calcCoord();
|
||||
vec4 color = getColor();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(assets->getTexture(texture));
|
||||
batch->color = color_;
|
||||
batch->rect(coord.x, coord.y, size_.x, size_.y, 0, 0, 0, UVRegion(), false, true, color_);
|
||||
batch->color = color;
|
||||
batch->rect(coord.x, coord.y, size.x, size.y,
|
||||
0, 0, 0, UVRegion(), false, true, color);
|
||||
}
|
||||
|
||||
// ================================= Button ===================================
|
||||
Button::Button(shared_ptr<UINode> content, glm::vec4 padding)
|
||||
: Panel(content->size()+vec2(padding[0]+padding[2]+content->margin()[0]+content->margin()[2],
|
||||
padding[1]+padding[3]+content->margin()[1]+content->margin()[3]), padding, 0) {
|
||||
Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
|
||||
: Panel(vec2(), padding, 0) {
|
||||
vec4 margin = getMargin();
|
||||
setSize(content->getSize()+vec2(padding[0]+padding[2]+margin[0]+margin[2],
|
||||
padding[1]+padding[3]+margin[1]+margin[3]));
|
||||
add(content);
|
||||
scrollable(false);
|
||||
}
|
||||
|
||||
Button::Button(wstring text, glm::vec4 padding, glm::vec4 margin)
|
||||
: Panel(vec2(32,32), padding, 0) {
|
||||
this->margin(margin);
|
||||
Label* label = new Label(text);
|
||||
label->align(Align::center);
|
||||
this->label = shared_ptr<UINode>(label);
|
||||
add(this->label);
|
||||
Button::Button(std::wstring text, glm::vec4 padding, glm::vec4 margin)
|
||||
: Panel(vec2(32,32), padding, 0)
|
||||
{
|
||||
setMargin(margin);
|
||||
scrollable(false);
|
||||
|
||||
label = std::make_shared<Label>(text);
|
||||
label->setAlign(Align::center);
|
||||
add(label);
|
||||
}
|
||||
|
||||
void Button::text(std::wstring text) {
|
||||
void Button::setText(std::wstring text) {
|
||||
if (label) {
|
||||
Label* label = (Label*)(this->label.get());
|
||||
label->text(text);
|
||||
label->setText(text);
|
||||
}
|
||||
}
|
||||
|
||||
wstring Button::text() const {
|
||||
std::wstring Button::getText() const {
|
||||
if (label) {
|
||||
Label* label = (Label*)(this->label.get());
|
||||
return label->text();
|
||||
return label->getText();
|
||||
}
|
||||
return L"";
|
||||
}
|
||||
|
||||
Button* Button::textSupplier(wstringsupplier supplier) {
|
||||
if (label) {
|
||||
Label* label = (Label*)(this->label.get());
|
||||
label->textSupplier(supplier);
|
||||
}
|
||||
return this;
|
||||
@@ -126,14 +124,15 @@ void Button::setHoverColor(glm::vec4 color) {
|
||||
hoverColor = color;
|
||||
}
|
||||
|
||||
void Button::drawBackground(Batch2D* batch, Assets* assets) {
|
||||
void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
vec2 coord = calcCoord();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->color = (ispressed() ? pressedColor : (hover_ ? hoverColor : color_));
|
||||
batch->rect(coord.x, coord.y, size_.x, size_.y);
|
||||
batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
|
||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||
}
|
||||
|
||||
shared_ptr<UINode> Button::getAt(vec2 pos, shared_ptr<UINode> self) {
|
||||
std::shared_ptr<UINode> Button::getAt(vec2 pos, std::shared_ptr<UINode> self) {
|
||||
return UINode::getAt(pos, self);
|
||||
}
|
||||
|
||||
@@ -153,8 +152,7 @@ Button* Button::listenAction(onaction action) {
|
||||
|
||||
void Button::textAlign(Align align) {
|
||||
if (label) {
|
||||
Label* label = (Label*)(this->label.get());
|
||||
label->align(align);
|
||||
label->setAlign(align);
|
||||
refresh();
|
||||
}
|
||||
}
|
||||
@@ -181,55 +179,58 @@ void RichButton::setHoverColor(glm::vec4 color) {
|
||||
hoverColor = color;
|
||||
}
|
||||
|
||||
void RichButton::drawBackground(Batch2D* batch, Assets* assets) {
|
||||
void RichButton::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
vec2 coord = calcCoord();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->color = (ispressed() ? pressedColor : (hover_ ? hoverColor : color_));
|
||||
batch->rect(coord.x, coord.y, size_.x, size_.y);
|
||||
batch->color = (isPressed() ? pressedColor : (hover ? hoverColor : color));
|
||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||
}
|
||||
|
||||
// ================================ TextBox ===================================
|
||||
TextBox::TextBox(wstring placeholder, vec4 padding)
|
||||
TextBox::TextBox(std::wstring placeholder, vec4 padding)
|
||||
: Panel(vec2(200,32), padding, 0, false),
|
||||
input(L""),
|
||||
placeholder(placeholder) {
|
||||
label = new Label(L"");
|
||||
add(shared_ptr<UINode>(label));
|
||||
label = std::make_shared<Label>(L"");
|
||||
add(label);
|
||||
}
|
||||
|
||||
void TextBox::drawBackground(Batch2D* batch, Assets* assets) {
|
||||
void TextBox::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
vec2 coord = calcCoord();
|
||||
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
|
||||
|
||||
if (valid) {
|
||||
if (isfocused()) {
|
||||
if (isFocused()) {
|
||||
batch->color = focusedColor;
|
||||
} else if (hover_) {
|
||||
} else if (hover) {
|
||||
batch->color = hoverColor;
|
||||
} else {
|
||||
batch->color = color_;
|
||||
batch->color = color;
|
||||
}
|
||||
} else {
|
||||
batch->color = invalidColor;
|
||||
}
|
||||
|
||||
batch->rect(coord.x, coord.y, size_.x, size_.y);
|
||||
if (!focused_ && supplier) {
|
||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||
if (!isFocused() && supplier) {
|
||||
input = supplier();
|
||||
}
|
||||
|
||||
if (input.empty()) {
|
||||
label->color(vec4(0.5f));
|
||||
label->text(placeholder);
|
||||
label->setColor(vec4(0.5f));
|
||||
label->setText(placeholder);
|
||||
} else {
|
||||
label->color(vec4(1.0f));
|
||||
label->text(input);
|
||||
label->setColor(vec4(1.0f));
|
||||
label->setText(input);
|
||||
}
|
||||
scrollable(false);
|
||||
}
|
||||
|
||||
void TextBox::typed(unsigned int codepoint) {
|
||||
input += wstring({(wchar_t)codepoint});
|
||||
input += std::wstring({(wchar_t)codepoint});
|
||||
validate();
|
||||
}
|
||||
|
||||
@@ -262,19 +263,16 @@ void TextBox::focus(GUI* gui) {
|
||||
}
|
||||
|
||||
void TextBox::keyPressed(int key) {
|
||||
switch (key) {
|
||||
case KEY_BACKSPACE:
|
||||
if (!input.empty()){
|
||||
input = input.substr(0, input.length()-1);
|
||||
validate();
|
||||
}
|
||||
break;
|
||||
case KEY_ENTER:
|
||||
if (validate() && consumer) {
|
||||
consumer(label->text());
|
||||
}
|
||||
defocus();
|
||||
break;
|
||||
if (key == keycode::BACKSPACE) {
|
||||
if (!input.empty()){
|
||||
input = input.substr(0, input.length()-1);
|
||||
validate();
|
||||
}
|
||||
} else if (key == keycode::ENTER) {
|
||||
if (validate() && consumer) {
|
||||
consumer(label->getText());
|
||||
}
|
||||
defocus();
|
||||
}
|
||||
// Pasting text from clipboard
|
||||
if (key == keycode::V && Events::pressed(keycode::LEFT_CONTROL)) {
|
||||
@@ -286,7 +284,7 @@ void TextBox::keyPressed(int key) {
|
||||
}
|
||||
}
|
||||
|
||||
shared_ptr<UINode> TextBox::getAt(vec2 pos, shared_ptr<UINode> self) {
|
||||
std::shared_ptr<UINode> TextBox::getAt(vec2 pos, std::shared_ptr<UINode> self) {
|
||||
return UINode::getAt(pos, self);
|
||||
}
|
||||
|
||||
@@ -302,7 +300,7 @@ void TextBox::textValidator(wstringchecker validator) {
|
||||
this->validator = validator;
|
||||
}
|
||||
|
||||
wstring TextBox::text() const {
|
||||
std::wstring TextBox::text() const {
|
||||
if (input.empty())
|
||||
return placeholder;
|
||||
return input;
|
||||
@@ -321,16 +319,13 @@ InputBindBox::InputBindBox(Binding& binding, vec4 padding)
|
||||
scrollable(false);
|
||||
}
|
||||
|
||||
shared_ptr<UINode> InputBindBox::getAt(vec2 pos, shared_ptr<UINode> self) {
|
||||
return UINode::getAt(pos, self);
|
||||
}
|
||||
|
||||
void InputBindBox::drawBackground(Batch2D* batch, Assets* assets) {
|
||||
void InputBindBox::drawBackground(const GfxContext* pctx, Assets* assets) {
|
||||
vec2 coord = calcCoord();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->color = (isfocused() ? focusedColor : (hover_ ? hoverColor : color_));
|
||||
batch->rect(coord.x, coord.y, size_.x, size_.y);
|
||||
label->text(util::str2wstr_utf8(binding.text()));
|
||||
batch->color = (isFocused() ? focusedColor : (hover ? hoverColor : color));
|
||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||
label->setText(util::str2wstr_utf8(binding.text()));
|
||||
}
|
||||
|
||||
void InputBindBox::clicked(GUI*, int button) {
|
||||
@@ -359,24 +354,25 @@ TrackBar::TrackBar(double min,
|
||||
value(value),
|
||||
step(step),
|
||||
trackWidth(trackWidth) {
|
||||
color(vec4(0.f, 0.f, 0.f, 0.4f));
|
||||
setColor(vec4(0.f, 0.f, 0.f, 0.4f));
|
||||
}
|
||||
|
||||
void TrackBar::draw(Batch2D* batch, Assets* assets) {
|
||||
void TrackBar::draw(const GfxContext* pctx, Assets* assets) {
|
||||
if (supplier_) {
|
||||
value = supplier_();
|
||||
}
|
||||
vec2 coord = calcCoord();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->color = (hover_ ? hoverColor : color_);
|
||||
batch->rect(coord.x, coord.y, size_.x, size_.y);
|
||||
batch->color = (hover ? hoverColor : color);
|
||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||
|
||||
float width = size_.x;
|
||||
float width = size.x;
|
||||
float t = (value - min) / (max-min+trackWidth*step);
|
||||
|
||||
batch->color = trackColor;
|
||||
int actualWidth = size_.x * (trackWidth / (max-min+trackWidth*step) * step);
|
||||
batch->rect(coord.x + width * t, coord.y, actualWidth, size_.y);
|
||||
int actualWidth = size.x * (trackWidth / (max-min+trackWidth*step) * step);
|
||||
batch->rect(coord.x + width * t, coord.y, actualWidth, size.y);
|
||||
}
|
||||
|
||||
void TrackBar::supplier(doublesupplier supplier) {
|
||||
@@ -391,7 +387,7 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
|
||||
vec2 coord = calcCoord();
|
||||
value = x;
|
||||
value -= coord.x;
|
||||
value = (value)/size_.x * (max-min+trackWidth*step);
|
||||
value = (value)/size.x * (max-min+trackWidth*step);
|
||||
value += min;
|
||||
value = (value > max) ? max : value;
|
||||
value = (value < min) ? min : value;
|
||||
@@ -403,17 +399,18 @@ void TrackBar::mouseMove(GUI*, int x, int y) {
|
||||
|
||||
// ================================ CheckBox ==================================
|
||||
CheckBox::CheckBox(bool checked) : UINode(vec2(), vec2(32.0f)), checked_(checked) {
|
||||
color(vec4(0.0f, 0.0f, 0.0f, 0.5f));
|
||||
setColor(vec4(0.0f, 0.0f, 0.0f, 0.5f));
|
||||
}
|
||||
|
||||
void CheckBox::draw(Batch2D* batch, Assets* assets) {
|
||||
void CheckBox::draw(const GfxContext* pctx, Assets* assets) {
|
||||
if (supplier_) {
|
||||
checked_ = supplier_();
|
||||
}
|
||||
vec2 coord = calcCoord();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->color = checked_ ? checkColor : (hover_ ? hoverColor : color_);
|
||||
batch->rect(coord.x, coord.y, size_.x, size_.y);
|
||||
batch->color = checked_ ? checkColor : (hover ? hoverColor : color);
|
||||
batch->rect(coord.x, coord.y, size.x, size.y);
|
||||
}
|
||||
|
||||
void CheckBox::mouseRelease(GUI*, int x, int y) {
|
||||
@@ -439,12 +436,12 @@ CheckBox* CheckBox::checked(bool flag) {
|
||||
FullCheckBox::FullCheckBox(std::wstring text, glm::vec2 size, bool checked)
|
||||
: Panel(size),
|
||||
checkbox(std::make_shared<CheckBox>(checked)){
|
||||
color(vec4(0.0f));
|
||||
setColor(vec4(0.0f));
|
||||
orientation(Orientation::horizontal);
|
||||
|
||||
add(checkbox);
|
||||
|
||||
auto label = std::make_shared<Label>(text);
|
||||
label->margin(vec4(5.0f, 5.0f, 0.0f, 0.0f));
|
||||
label->setMargin(vec4(5.f, 5.f, 0.f, 0.f));
|
||||
add(label);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user