123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- #include "client/gui/BaseGUI.h"
- #include "client/Game.h"
- #include "client/rendering/Engine.h"
- 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) {
- }
- void BaseGUI::tickBase(Base& b) {
- b.hovered = isIn(b.pos, b.size, Game::controller.getMouse() / scale);
- b.pressed = b.hovered && Game::controller.leftClick.wasReleased();
- }
- void BaseGUI::tick() {
- for(Label& label : labels) {
- tickBase(label.base);
- }
- for(Input& input : inputs) {
- tickBase(input.base);
- if(input.base.pressed) {
- Engine::setTextInput(&input.text);
- }
- }
- for(Button& button : buttons) {
- tickBase(button.base);
- }
- }
- void BaseGUI::updateScale() {
- const Size& size = Engine::getSize();
- scale = static_cast<int>(
- std::min(size.width / FIXED_SIZE[0], size.height / FIXED_SIZE[1]));
- scale = scale < 1.0f ? 1.0f : scale;
- scaledSize = Vector2(size.width / scale, size.height / scale);
- Engine::matrix.scale(scale).update();
- }
- void BaseGUI::renderBase(const Base& base) {
- Engine::renderer.renderRectangle(base.pos, base.size,
- base.hovered ? INPUT_BACKGROUND_2
- : INPUT_BACKGROUND);
- }
- void BaseGUI::renderLabels() {
- for(const Label& label : labels) {
- renderCenteredString(label.base, label.text);
- }
- }
- void BaseGUI::renderInputs() {
- for(Input& i : inputs) {
- i.text.setLimit(Engine::renderer.charsInSpace(i.base.size[0] - 20.0f));
- renderBase(i.base);
- StringBuffer<256> text(i.text);
- Vector2 pos = renderCenteredString(i.base, text);
- if(Engine::isActiveTextInput(&i.text)) {
- Vector2 c =
- Engine::renderer.getStringSize(text, i.text.getCursor());
- renderString(Vector2(pos[0] + c[0], pos[1] + 2.0f), "&292_");
- renderString(Vector2(pos[0] + c[0], pos[1] + 3.0f), "&292_");
- }
- }
- }
- void BaseGUI::renderButtons() {
- for(const Button& button : buttons) {
- renderBase(button.base);
- renderCenteredString(button.base, button.text);
- }
- }
- void BaseGUI::render() {
- renderLabels();
- renderInputs();
- renderButtons();
- }
- 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::renderString(const Vector2& pos, const char* s) {
- Engine::renderer.renderString(round(pos), s);
- }
- BaseGUI::Label& BaseGUI::addLabel(const char* text) {
- labels.add({Base(), text});
- return labels[labels.getLength() - 1];
- }
- BaseGUI::Input& BaseGUI::addInput() {
- inputs.add();
- return inputs[inputs.getLength() - 1];
- }
- BaseGUI::Button& BaseGUI::addButton(const char* text) {
- buttons.add({Base(), text});
- return buttons[buttons.getLength() - 1];
- }
- Vector2 BaseGUI::renderCenteredString(const Base& b, const char* text) {
- Vector2 pos =
- b.pos + (b.size - Engine::renderer.getStringSize(text)) * 0.5f;
- renderString(pos, text);
- return pos;
- }
- bool BaseGUI::isIn(const Vector2& pos, const Vector2& size,
- const Vector2& point) const {
- Vector2 end = pos + size;
- return pos[0] <= point[0] && point[0] <= end[0] && pos[1] <= point[1] &&
- point[1] <= end[1];
- }
|