|
@@ -4,22 +4,33 @@ static const Color4 INPUT_BACKGROUND(0x00, 0x00, 0x00, 0xA0);
|
|
|
static const Color4 INPUT_BACKGROUND_2(0x00, 0x00, 0x00, 0x80);
|
|
|
const Vector2 BaseGUI::FIXED_SIZE(400.0f, 300.0f);
|
|
|
|
|
|
+BaseGUI::Base::Base() : hovered(false), pressed(false) {
|
|
|
+}
|
|
|
+
|
|
|
BaseGUI::BaseGUI(const Size& size, TextInput*& textInput,
|
|
|
const Controller& controller)
|
|
|
: size(size), textInput(textInput), controller(controller) {
|
|
|
}
|
|
|
|
|
|
+void BaseGUI::tickBase(Base& b) {
|
|
|
+ b.hovered = isIn(b.pos, b.size, controller.getMouse() / scale);
|
|
|
+ b.pressed = b.hovered && controller.leftClick.wasReleased();
|
|
|
+}
|
|
|
+
|
|
|
void BaseGUI::tick() {
|
|
|
- if(controller.leftClick.wasReleased()) {
|
|
|
- Vector2 mousePos = controller.getMouse() / scale;
|
|
|
- for(Input& input : inputs) {
|
|
|
- if(isIn(input.pos, input.size, mousePos)) {
|
|
|
- textInput = &input.text;
|
|
|
- textInput->setActive(true);
|
|
|
- break;
|
|
|
- }
|
|
|
+ for(Label& label : labels) {
|
|
|
+ tickBase(label.base);
|
|
|
+ }
|
|
|
+ for(Input& input : inputs) {
|
|
|
+ tickBase(input.base);
|
|
|
+ if(input.base.pressed) {
|
|
|
+ textInput = &input.text;
|
|
|
+ textInput->setActive(true);
|
|
|
}
|
|
|
}
|
|
|
+ for(Button& button : buttons) {
|
|
|
+ tickBase(button.base);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
void BaseGUI::updateScale(ShaderMatrix& sm) {
|
|
@@ -30,34 +41,58 @@ void BaseGUI::updateScale(ShaderMatrix& sm) {
|
|
|
sm.scale(scale).update();
|
|
|
}
|
|
|
|
|
|
-void BaseGUI::render(float, ShaderMatrix&, Renderer& r) {
|
|
|
- Vector2 mousePos = controller.getMouse() / scale;
|
|
|
+void BaseGUI::renderBase(Renderer& r, const Base& base) {
|
|
|
+ r.renderRectangle(base.pos, base.size,
|
|
|
+ base.hovered ? INPUT_BACKGROUND_2 : INPUT_BACKGROUND);
|
|
|
+}
|
|
|
+
|
|
|
+void BaseGUI::renderLabels(Renderer& r) {
|
|
|
+ for(const Label& label : labels) {
|
|
|
+ renderCenteredString(r, label.base, label.text);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void BaseGUI::renderInputs(Renderer& r) {
|
|
|
for(Input& input : inputs) {
|
|
|
- input.text.setLimit(r.charsInSpace(input.size[0] - 20.0f));
|
|
|
- if(isIn(input.pos, input.size, mousePos)) {
|
|
|
- r.drawRectangle(input.pos, input.size, INPUT_BACKGROUND_2);
|
|
|
- } else {
|
|
|
- r.drawRectangle(input.pos, input.size, INPUT_BACKGROUND);
|
|
|
- }
|
|
|
+ input.text.setLimit(r.charsInSpace(input.base.size[0] - 20.0f));
|
|
|
+ renderBase(r, input.base);
|
|
|
StringBuffer<256> text(input.text);
|
|
|
- Vector2 size = r.getStringSize(text);
|
|
|
- Vector2 pos = input.pos + (input.size - size) * 0.5f;
|
|
|
- drawString(r, pos, text);
|
|
|
+ Vector2 pos = renderCenteredString(r, input.base, text);
|
|
|
if(textInput == &input.text) {
|
|
|
Vector2 cursor = r.getStringSize(text, input.text.getCursor());
|
|
|
- drawString(r, Vector2(pos[0] + cursor[0], pos[1] + 2.0f), "&292_");
|
|
|
- drawString(r, Vector2(pos[0] + cursor[0], pos[1] + 3.0f), "&292_");
|
|
|
+ renderString(r, Vector2(pos[0] + cursor[0], pos[1] + 2.0f),
|
|
|
+ "&292_");
|
|
|
+ renderString(r, Vector2(pos[0] + cursor[0], pos[1] + 3.0f),
|
|
|
+ "&292_");
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+void BaseGUI::renderButtons(Renderer& r) {
|
|
|
+ for(const Button& button : buttons) {
|
|
|
+ renderBase(r, button.base);
|
|
|
+ renderCenteredString(r, button.base, button.text);
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void BaseGUI::render(float, ShaderMatrix&, Renderer& r) {
|
|
|
+ renderLabels(r);
|
|
|
+ renderInputs(r);
|
|
|
+ renderButtons(r);
|
|
|
+}
|
|
|
+
|
|
|
Vector2 BaseGUI::round(const Vector2& v) const {
|
|
|
return Vector2(static_cast<int>(v[0] * scale) / scale,
|
|
|
static_cast<int>(v[1] * scale) / scale);
|
|
|
}
|
|
|
|
|
|
-void BaseGUI::drawString(Renderer& r, const Vector2& pos, const char* s) {
|
|
|
- r.drawString(round(pos), s);
|
|
|
+void BaseGUI::renderString(Renderer& r, const Vector2& pos, const char* s) {
|
|
|
+ r.renderString(round(pos), s);
|
|
|
+}
|
|
|
+
|
|
|
+BaseGUI::Label& BaseGUI::addLabel(const char* text) {
|
|
|
+ labels.add({Base(), text});
|
|
|
+ return labels[labels.getLength() - 1];
|
|
|
}
|
|
|
|
|
|
BaseGUI::Input& BaseGUI::addInput() {
|
|
@@ -65,10 +100,16 @@ BaseGUI::Input& BaseGUI::addInput() {
|
|
|
return inputs[inputs.getLength() - 1];
|
|
|
}
|
|
|
|
|
|
-void BaseGUI::drawCenteredString(Renderer& r, const Vector2& pos,
|
|
|
- const Vector2& size, const char* text) {
|
|
|
- Vector2 textSize = r.getStringSize(text);
|
|
|
- drawString(r, pos + (size - textSize) * 0.5f, text);
|
|
|
+BaseGUI::Button& BaseGUI::addButton(const char* text) {
|
|
|
+ buttons.add({Base(), text});
|
|
|
+ return buttons[buttons.getLength() - 1];
|
|
|
+}
|
|
|
+
|
|
|
+Vector2 BaseGUI::renderCenteredString(Renderer& r, const Base& b,
|
|
|
+ const char* text) {
|
|
|
+ Vector2 pos = b.pos + (b.size - r.getStringSize(text)) * 0.5f;
|
|
|
+ renderString(r, pos, text);
|
|
|
+ return pos;
|
|
|
}
|
|
|
|
|
|
bool BaseGUI::isIn(const Vector2& pos, const Vector2& size,
|