12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #include "client/gui/BaseGUI.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::BaseGUI(const Size& size, TextInput*& textInput,
- const Controller& controller)
- : size(size), textInput(textInput), controller(controller) {
- }
- 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;
- }
- }
- }
- }
- void BaseGUI::updateScale(ShaderMatrix& sm) {
- 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);
- sm.scale(scale).update();
- }
- void BaseGUI::render(float, ShaderMatrix&, Renderer& r) {
- Vector2 mousePos = controller.getMouse() / scale;
- 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);
- }
- StringBuffer<256> text(input.text);
- Vector2 size = r.getStringSize(text);
- Vector2 pos = input.pos + (input.size - size) * 0.5f;
- drawString(r, pos, 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_");
- }
- }
- }
- 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);
- }
- BaseGUI::Input& BaseGUI::addInput() {
- inputs.add();
- 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);
- }
- 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];
- }
|