graphics/ui/elements/controls deleted

This commit is contained in:
MihailRis 2024-04-20 22:52:26 +03:00
parent 85f7febd30
commit ed51751daa
34 changed files with 1506 additions and 1420 deletions

View File

@ -11,7 +11,6 @@
#include "../graphics/core/GfxContext.h"
#include "../graphics/core/Shader.h"
#include "../graphics/render/BlocksPreview.h"
#include "../graphics/ui/elements/controls.h"
#include "../items/Inventories.h"
#include "../items/Inventory.h"
#include "../items/ItemDef.h"

View File

@ -6,8 +6,7 @@
#include <glm/glm.hpp>
#include "../graphics/ui/elements/UINode.h"
#include "../graphics/ui/elements/Container.hpp"
#include "../graphics/ui/elements/controls.h"
#include "../graphics/ui/elements/layout/Container.hpp"
#include "../items/ItemStack.h"
#include "../typedefs.h"

View File

@ -6,7 +6,10 @@
#include "../delegates.h"
#include "../engine.h"
#include "../graphics/core/Mesh.h"
#include "../graphics/ui/elements/controls.h"
#include "../graphics/ui/elements/control/CheckBox.hpp"
#include "../graphics/ui/elements/control/TextBox.hpp"
#include "../graphics/ui/elements/control/TrackBar.hpp"
#include "../graphics/ui/elements/control/InputBindBox.hpp"
#include "../graphics/render/WorldRenderer.h"
#include "../objects/Player.h"
#include "../physics/Hitbox.h"

View File

@ -15,10 +15,10 @@
#include "../graphics/core/Texture.h"
#include "../graphics/render/BlocksPreview.h"
#include "../graphics/render/WorldRenderer.h"
#include "../graphics/ui/elements/controls.h"
#include "../graphics/ui/elements/UINode.h"
#include "../graphics/ui/elements/Menu.hpp"
#include "../graphics/ui/elements/Plotter.hpp"
#include "../graphics/ui/elements/layout/Menu.hpp"
#include "../graphics/ui/elements/layout/Panel.hpp"
#include "../graphics/ui/elements/display/Plotter.hpp"
#include "../graphics/ui/GUI.h"
#include "../items/Inventories.h"
#include "../items/Inventory.h"

View File

@ -11,8 +11,9 @@
#include "../../interfaces/Task.h"
#include "../../graphics/ui/GUI.h"
#include "../../graphics/ui/gui_util.h"
#include "../../graphics/ui/elements/Menu.hpp"
#include "../../graphics/ui/elements/controls.h"
#include "../../graphics/ui/elements/layout/Menu.hpp"
#include "../../graphics/ui/elements/display/Label.hpp"
#include "../../graphics/ui/elements/control/Button.hpp"
#include "../screens.h"
#include "../UiDocument.h"
#include "../../logic/scripting/scripting.h"

View File

@ -10,7 +10,7 @@
#include "../graphics/core/Shader.h"
#include "../graphics/core/TextureAnimation.h"
#include "../graphics/render/WorldRenderer.h"
#include "../graphics/ui/elements/Menu.hpp"
#include "../graphics/ui/elements/layout/Menu.hpp"
#include "../graphics/ui/GUI.h"
#include "../logic/ChunksController.h"
#include "../logic/LevelController.h"

View File

@ -1,6 +1,6 @@
#include "GUI.h"
#include "elements/UINode.h"
#include "elements/Menu.hpp"
#include "elements/layout/Menu.hpp"
#include <iostream>
#include <algorithm>

View File

@ -1,6 +1,6 @@
#include "UINode.h"
#include "Container.hpp"
#include "layout/Container.hpp"
#include "../../core/Batch2D.h"
using gui::UINode;

View File

@ -0,0 +1,98 @@
#include "Button.hpp"
#include "../display/Label.hpp"
#include "../../../core/GfxContext.h"
#include "../../../core/Batch2D.h"
using namespace gui;
Button::Button(std::shared_ptr<UINode> content, glm::vec4 padding)
: Panel(glm::vec2(), padding, 0) {
glm::vec4 margin = getMargin();
setSize(content->getSize()+
glm::vec2(padding[0]+padding[2]+margin[0]+margin[2],
padding[1]+padding[3]+margin[1]+margin[3]));
add(content);
setScrollable(false);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
setPressedColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.95f));
content->setInteractive(false);
}
Button::Button(
std::wstring text,
glm::vec4 padding,
onaction action,
glm::vec2 size
) : Panel(size, padding, 0)
{
if (size.y < 0.0f) {
size = glm::vec2(
glm::max(padding.x + padding.z + text.length()*8, size.x),
glm::max(padding.y + padding.w + 16, size.y)
);
}
setSize(size);
if (action) {
listenAction(action);
}
setScrollable(false);
label = std::make_shared<Label>(text);
label->setAlign(Align::center);
label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
label->setInteractive(false);
add(label);
setHoverColor(glm::vec4(0.05f, 0.1f, 0.15f, 0.75f));
setPressedColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.95f));
}
void Button::setText(std::wstring text) {
if (label) {
label->setText(text);
}
}
std::wstring Button::getText() const {
if (label) {
return label->getText();
}
return L"";
}
Button* Button::textSupplier(wstringsupplier supplier) {
if (label) {
label->textSupplier(supplier);
}
return this;
}
void Button::refresh() {
Panel::refresh();
if (label) {
label->setSize(size-glm::vec2(padding.z+padding.x, padding.w+padding.y));
}
}
void Button::drawBackground(const GfxContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->setColor(isPressed() ? pressedColor : (hover ? hoverColor : color));
batch->rect(pos.x, pos.y, size.x, size.y);
}
void Button::setTextAlign(Align align) {
if (label) {
label->setAlign(align);
refresh();
}
}
Align Button::getTextAlign() const {
if (label) {
return label->getAlign();
}
return Align::left;
}

View File

@ -0,0 +1,35 @@
#ifndef GRAPHICS_UI_ELEMENTS_BUTTON_HPP_
#define GRAPHICS_UI_ELEMENTS_BUTTON_HPP_
#include "../layout/Panel.hpp"
namespace gui {
class Label;
class Button : public Panel {
protected:
std::shared_ptr<Label> label = nullptr;
public:
Button(std::shared_ptr<UINode> content,
glm::vec4 padding=glm::vec4(2.0f));
Button(std::wstring text,
glm::vec4 padding,
onaction action,
glm::vec2 size=glm::vec2(-1));
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual Align getTextAlign() const;
virtual void setTextAlign(Align align);
virtual void setText(std::wstring text);
virtual std::wstring getText() const;
virtual Button* textSupplier(wstringsupplier supplier);
virtual void refresh() override;
};
}
#endif // GRAPHICS_UI_ELEMENTS_BUTTON_HPP_

View File

@ -0,0 +1,55 @@
#include "CheckBox.hpp"
#include "../../../core/GfxContext.h"
#include "../../../core/Batch2D.h"
#include "../display/Label.hpp"
using namespace gui;
CheckBox::CheckBox(bool checked) : UINode(glm::vec2(32.0f)), checked(checked) {
setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.5f));
}
void CheckBox::draw(const GfxContext* pctx, Assets* assets) {
if (supplier) {
checked = supplier();
}
glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->setColor(checked ? checkColor : (hover ? hoverColor : color));
batch->rect(pos.x, pos.y, size.x, size.y);
}
void CheckBox::mouseRelease(GUI*, int x, int y) {
checked = !checked;
if (consumer) {
consumer(checked);
}
}
void CheckBox::setSupplier(boolsupplier supplier) {
this->supplier = supplier;
}
void CheckBox::setConsumer(boolconsumer consumer) {
this->consumer = consumer;
}
CheckBox* CheckBox::setChecked(bool flag) {
checked = flag;
return this;
}
FullCheckBox::FullCheckBox(std::wstring text, glm::vec2 size, bool checked)
: Panel(size),
checkbox(std::make_shared<CheckBox>(checked)){
setColor(glm::vec4(0.0f));
setOrientation(Orientation::horizontal);
add(checkbox);
auto label = std::make_shared<Label>(text);
label->setMargin(glm::vec4(5.f, 5.f, 0.f, 0.f));
add(label);
}

View File

@ -0,0 +1,57 @@
#ifndef GRAPHICS_UI_ELEMENTS_CHECKBOX_HPP_
#define GRAPHICS_UI_ELEMENTS_CHECKBOX_HPP_
#include "../layout/Panel.hpp"
namespace gui {
class CheckBox : public UINode {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 checkColor {1.0f, 1.0f, 1.0f, 0.4f};
boolsupplier supplier = nullptr;
boolconsumer consumer = nullptr;
bool checked = false;
public:
CheckBox(bool checked=false);
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void mouseRelease(GUI*, int x, int y) override;
virtual void setSupplier(boolsupplier supplier);
virtual void setConsumer(boolconsumer consumer);
virtual CheckBox* setChecked(bool flag);
virtual bool isChecked() const {
if (supplier)
return supplier();
return checked;
}
};
class FullCheckBox : public Panel {
protected:
std::shared_ptr<CheckBox> checkbox;
public:
FullCheckBox(std::wstring text, glm::vec2 size, bool checked=false);
virtual void setSupplier(boolsupplier supplier) {
checkbox->setSupplier(supplier);
}
virtual void setConsumer(boolconsumer consumer) {
checkbox->setConsumer(consumer);
}
virtual void setChecked(bool flag) {
checkbox->setChecked(flag);
}
virtual bool isChecked() const {
return checkbox->isChecked();
}
};
}
#endif // GRAPHICS_UI_ELEMENTS_CHECKBOX_HPP_

View File

@ -0,0 +1,37 @@
#include "InputBindBox.hpp"
#include "../display/Label.hpp"
#include "../../../core/GfxContext.h"
#include "../../../core/Batch2D.h"
#include "../../../../util/stringutil.h"
using namespace gui;
InputBindBox::InputBindBox(Binding& binding, glm::vec4 padding)
: Panel(glm::vec2(100,32), padding, 0),
binding(binding) {
label = std::make_shared<Label>(L"");
add(label);
setScrollable(false);
}
void InputBindBox::drawBackground(const GfxContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->setColor(isFocused() ? focusedColor : (hover ? hoverColor : color));
batch->rect(pos.x, pos.y, size.x, size.y);
label->setText(util::str2wstr_utf8(binding.text()));
}
void InputBindBox::clicked(GUI*, mousecode button) {
binding.reset(button);
defocus();
}
void InputBindBox::keyPressed(keycode key) {
if (key != keycode::ESCAPE) {
binding.reset(key);
}
defocus();
}

View File

@ -0,0 +1,25 @@
#ifndef GRAPHICS_UI_ELEMENTS_INPUTBINDBOX_HPP_
#define GRAPHICS_UI_ELEMENTS_INPUTBINDBOX_HPP_
#include "../layout/Panel.hpp"
namespace gui {
class Label;
class InputBindBox : public Panel {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
std::shared_ptr<Label> label;
Binding& binding;
public:
InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.0f));
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void clicked(GUI*, mousecode button) override;
virtual void keyPressed(keycode key) override;
virtual bool isFocuskeeper() const override {return true;}
};
}
#endif // GRAPHICS_UI_ELEMENTS_INPUTBINDBOX_HPP_

View File

@ -0,0 +1,149 @@
#ifndef GRAPHICS_UI_ELEMENTS_TEXTBOX_HPP_
#define GRAPHICS_UI_ELEMENTS_TEXTBOX_HPP_
#include "../layout/Panel.hpp"
#include "../display/Label.hpp"
class Font;
namespace gui {
class Label;
class TextBox : public Panel {
protected:
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
glm::vec4 invalidColor {0.1f, 0.05f, 0.03f, 1.0f};
std::shared_ptr<Label> label;
std::wstring input;
std::wstring placeholder;
wstringsupplier supplier = nullptr;
wstringconsumer consumer = nullptr;
wstringchecker validator = nullptr;
runnable onEditStart = nullptr;
bool valid = true;
/// @brief text input pointer, value may be greather than text length
uint caret = 0;
/// @brief actual local (line) position of the caret on vertical move
uint maxLocalCaret = 0;
uint textOffset = 0;
int textInitX;
/// @brief last time of the caret was moved (used for blink animation)
double caretLastMove = 0.0;
Font* font = nullptr;
size_t selectionStart = 0;
size_t selectionEnd = 0;
size_t selectionOrigin = 0;
bool multiline = false;
bool editable = true;
size_t normalizeIndex(int index);
int calcIndexAt(int x, int y) const;
void paste(const std::wstring& text);
void setTextOffset(uint x);
void erase(size_t start, size_t length);
bool eraseSelected();
void resetSelection();
void extendSelection(int index);
size_t getLineLength(uint line) const;
/// @brief Get total length of the selection
size_t getSelectionLength() const;
/// @brief Set maxLocalCaret to local (line) caret position
void resetMaxLocalCaret();
void performEditingKeyboardEvents(keycode key);
public:
TextBox(
std::wstring placeholder,
glm::vec4 padding=glm::vec4(4.0f)
);
virtual void setTextSupplier(wstringsupplier supplier);
/// @brief Consumer called on stop editing text (textbox defocus)
/// @param consumer std::wstring consumer function
virtual void setTextConsumer(wstringconsumer consumer);
/// @brief Text validator called while text editing and returns true if
/// text is valid
/// @param validator std::wstring consumer returning boolean
virtual void setTextValidator(wstringchecker validator);
virtual void setFocusedColor(glm::vec4 color);
virtual glm::vec4 getFocusedColor() const;
/// @brief Set color of textbox marked by validator as invalid
virtual void setErrorColor(glm::vec4 color);
/// @brief Get color of textbox marked by validator as invalid
virtual glm::vec4 getErrorColor() const;
/// @brief Get TextBox content text or placeholder if empty
virtual std::wstring getText() const;
/// @brief Set TextBox content text
virtual void setText(std::wstring value);
/// @brief Get text placeholder
virtual std::wstring getPlaceholder() const;
/// @brief Set text placeholder
/// @param text will be used instead of empty
virtual void setPlaceholder(const std::wstring& text);
/// @brief Get selected text
virtual std::wstring getSelection() const;
/// @brief Get current caret position in text
/// @return integer in range [0, text.length()]
virtual uint getCaret() const;
/// @brief Set caret position in the text
/// @param position integer in range [0, text.length()]
virtual void setCaret(uint position);
/// @brief Select part of the text
/// @param start index of the first selected character
/// @param end index of the last selected character + 1
virtual void select(int start, int end);
/// @brief Check text with validator set with setTextValidator
/// @return true if text is valid
virtual bool validate();
virtual void setValid(bool valid);
virtual bool isValid() const;
/// @brief Enable/disable multiline mode
virtual void setMultiline(bool multiline);
/// @brief Check if multiline mode is enabled
virtual bool isMultiline() const;
/// @brief Enable/disable text editing feature
virtual void setEditable(bool editable);
/// @brief Check if text editing feature is enabled
virtual bool isEditable() const;
/// @brief Set runnable called on textbox focus
virtual void setOnEditStart(runnable oneditstart);
virtual void onFocus(GUI*) override;
virtual void refresh() override;
virtual void click(GUI*, int, int) override;
virtual void mouseMove(GUI*, int x, int y) override;
virtual bool isFocuskeeper() const override {return true;}
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void typed(unsigned int codepoint) override;
virtual void keyPressed(keycode key) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
};
}
#endif // GRAPHICS_UI_ELEMENTS_TEXTBOX_HPP_

View File

@ -0,0 +1,114 @@
#include "TrackBar.hpp"
#include "../../../core/GfxContext.h"
#include "../../../core/Batch2D.h"
#include "../../../../assets/Assets.h"
using namespace gui;
TrackBar::TrackBar(
double min,
double max,
double value,
double step,
int trackWidth
) : UINode(glm::vec2(26)),
min(min),
max(max),
value(value),
step(step),
trackWidth(trackWidth)
{
setColor(glm::vec4(0.f, 0.f, 0.f, 0.4f));
setHoverColor(glm::vec4(0.01f, 0.02f, 0.03f, 0.5f));
}
void TrackBar::draw(const GfxContext* pctx, Assets* assets) {
if (supplier) {
value = supplier();
}
glm::vec2 pos = calcPos();
auto batch = pctx->getBatch2D();
batch->texture(nullptr);
batch->setColor(hover ? hoverColor : color);
batch->rect(pos.x, pos.y, size.x, size.y);
float width = size.x;
float t = (value - min) / (max-min+trackWidth*step);
batch->setColor(trackColor);
int actualWidth = size.x * (trackWidth / (max-min+trackWidth*step) * step);
batch->rect(pos.x + width * t, pos.y, actualWidth, size.y);
}
void TrackBar::setSupplier(doublesupplier supplier) {
this->supplier = supplier;
}
void TrackBar::setConsumer(doubleconsumer consumer) {
this->consumer = consumer;
}
void TrackBar::mouseMove(GUI*, int x, int y) {
glm::vec2 pos = calcPos();
value = x;
value -= pos.x;
value = (value)/size.x * (max-min+trackWidth*step);
value += min;
value = (value > max) ? max : value;
value = (value < min) ? min : value;
value = (int64_t)round(value / step) * step;
if (consumer) {
consumer(value);
}
}
double TrackBar::getValue() const {
return value;
}
double TrackBar::getMin() const {
return min;
}
double TrackBar::getMax() const {
return max;
}
double TrackBar::getStep() const {
return step;
}
int TrackBar::getTrackWidth() const {
return trackWidth;
}
glm::vec4 TrackBar::getTrackColor() const {
return trackColor;
}
void TrackBar::setValue(double x) {
value = x;
}
void TrackBar::setMin(double x) {
min = x;
}
void TrackBar::setMax(double x) {
max = x;
}
void TrackBar::setStep(double x) {
step = x;
}
void TrackBar::setTrackWidth(int width) {
trackWidth = width;
}
void TrackBar::setTrackColor(glm::vec4 color) {
trackColor = color;
}

View File

@ -0,0 +1,46 @@
#ifndef GRAPHICS_UI_ELEMENTS_TRACKBAR_HPP_
#define GRAPHICS_UI_ELEMENTS_TRACKBAR_HPP_
#include "../UINode.h"
namespace gui {
class TrackBar : public UINode {
protected:
glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f};
doublesupplier supplier = nullptr;
doubleconsumer consumer = nullptr;
double min;
double max;
double value;
double step;
int trackWidth;
public:
TrackBar(double min,
double max,
double value,
double step=1.0,
int trackWidth=1);
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void setSupplier(doublesupplier supplier);
virtual void setConsumer(doubleconsumer consumer);
virtual void mouseMove(GUI*, int x, int y) override;
virtual double getValue() const;
virtual double getMin() const;
virtual double getMax() const;
virtual double getStep() const;
virtual int getTrackWidth() const;
virtual glm::vec4 getTrackColor() const;
virtual void setValue(double);
virtual void setMin(double);
virtual void setMax(double);
virtual void setStep(double);
virtual void setTrackWidth(int);
virtual void setTrackColor(glm::vec4);
};
}
#endif // GRAPHICS_UI_ELEMENTS_TRACKBAR_HPP_

View File

@ -1,371 +0,0 @@
#ifndef GRAPHICS_UI_ELEMENTS_CONTROLS_H_
#define GRAPHICS_UI_ELEMENTS_CONTROLS_H_
// TODO: move elements to different files
#include <string>
#include <memory>
#include <vector>
#include <functional>
#include <glm/glm.hpp>
#include "../GUI.h"
#include "UINode.h"
#include "Panel.hpp"
#include "../../../window/input.h"
#include "../../../delegates.h"
#include "../../../typedefs.h"
class Batch2D;
class Assets;
class Font;
namespace gui {
class Label : public UINode {
protected:
std::wstring text;
std::string fontName;
wstringsupplier supplier = nullptr;
uint lines = 1;
float lineInterval = 1.5f;
Align valign = Align::center;
bool multiline = false;
// runtime values
/// @brief Text Y offset relative to label position
/// (last calculated alignment)
int textYOffset = 0;
/// @brief Text line height multiplied by line interval
int totalLineHeight = 1;
public:
Label(std::string text, std::string fontName="normal");
Label(std::wstring text, std::string fontName="normal");
virtual void setText(std::wstring text);
const std::wstring& getText() const;
virtual void setFontName(std::string name);
virtual const std::string& getFontName() const;
/// @brief Set text vertical alignment (default value: center)
/// @param align Align::top / Align::center / Align::bottom
virtual void setVerticalAlign(Align align);
virtual Align getVerticalAlign() const;
/// @brief Get line height multiplier used for multiline labels
/// (default value: 1.5)
virtual float getLineInterval() const;
/// @brief Set line height multiplier used for multiline labels
virtual void setLineInterval(float interval);
/// @brief Get Y position of the text relative to label position
/// @return Y offset
virtual int getTextYOffset() const;
/// @brief Get Y position of the line relative to label position
/// @param line target line index
/// @return Y offset
virtual int getLineYOffset(uint line) const;
/// @brief Get position of line start in the text
/// @param line target line index
/// @return position in the text [0..length]
virtual size_t getTextLineOffset(uint line) const;
/// @brief Get line index by its Y offset relative to label position
/// @param offset target Y offset
/// @return line index [0..+]
virtual uint getLineByYOffset(int offset) const;
virtual uint getLineByTextIndex(size_t index) const;
virtual uint getLinesNumber() const;
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void textSupplier(wstringsupplier supplier);
virtual void setMultiline(bool multiline);
virtual bool isMultiline() const;
};
class Image : public UINode {
protected:
std::string texture;
bool autoresize = false;
public:
Image(std::string texture, glm::vec2 size=glm::vec2(32,32));
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void setAutoResize(bool flag);
virtual bool isAutoResize() const;
};
class Button : public Panel {
protected:
std::shared_ptr<Label> label = nullptr;
public:
Button(std::shared_ptr<UINode> content,
glm::vec4 padding=glm::vec4(2.0f));
Button(std::wstring text,
glm::vec4 padding,
onaction action,
glm::vec2 size=glm::vec2(-1));
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual Align getTextAlign() const;
virtual void setTextAlign(Align align);
virtual void setText(std::wstring text);
virtual std::wstring getText() const;
virtual Button* textSupplier(wstringsupplier supplier);
virtual void refresh() override;
};
class TextBox : public Panel {
protected:
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
glm::vec4 invalidColor {0.1f, 0.05f, 0.03f, 1.0f};
std::shared_ptr<Label> label;
std::wstring input;
std::wstring placeholder;
wstringsupplier supplier = nullptr;
wstringconsumer consumer = nullptr;
wstringchecker validator = nullptr;
runnable onEditStart = nullptr;
bool valid = true;
/// @brief text input pointer, value may be greather than text length
uint caret = 0;
/// @brief actual local (line) position of the caret on vertical move
uint maxLocalCaret = 0;
uint textOffset = 0;
int textInitX;
/// @brief last time of the caret was moved (used for blink animation)
double caretLastMove = 0.0;
Font* font = nullptr;
size_t selectionStart = 0;
size_t selectionEnd = 0;
size_t selectionOrigin = 0;
bool multiline = false;
bool editable = true;
size_t normalizeIndex(int index);
int calcIndexAt(int x, int y) const;
void paste(const std::wstring& text);
void setTextOffset(uint x);
void erase(size_t start, size_t length);
bool eraseSelected();
void resetSelection();
void extendSelection(int index);
size_t getLineLength(uint line) const;
/// @brief Get total length of the selection
size_t getSelectionLength() const;
/// @brief Set maxLocalCaret to local (line) caret position
void resetMaxLocalCaret();
void performEditingKeyboardEvents(keycode key);
public:
TextBox(
std::wstring placeholder,
glm::vec4 padding=glm::vec4(4.0f)
);
virtual void setTextSupplier(wstringsupplier supplier);
/// @brief Consumer called on stop editing text (textbox defocus)
/// @param consumer std::wstring consumer function
virtual void setTextConsumer(wstringconsumer consumer);
/// @brief Text validator called while text editing and returns true if
/// text is valid
/// @param validator std::wstring consumer returning boolean
virtual void setTextValidator(wstringchecker validator);
virtual void setFocusedColor(glm::vec4 color);
virtual glm::vec4 getFocusedColor() const;
/// @brief Set color of textbox marked by validator as invalid
virtual void setErrorColor(glm::vec4 color);
/// @brief Get color of textbox marked by validator as invalid
virtual glm::vec4 getErrorColor() const;
/// @brief Get TextBox content text or placeholder if empty
virtual std::wstring getText() const;
/// @brief Set TextBox content text
virtual void setText(std::wstring value);
/// @brief Get text placeholder
virtual std::wstring getPlaceholder() const;
/// @brief Set text placeholder
/// @param text will be used instead of empty
virtual void setPlaceholder(const std::wstring& text);
/// @brief Get selected text
virtual std::wstring getSelection() const;
/// @brief Get current caret position in text
/// @return integer in range [0, text.length()]
virtual uint getCaret() const;
/// @brief Set caret position in the text
/// @param position integer in range [0, text.length()]
virtual void setCaret(uint position);
/// @brief Select part of the text
/// @param start index of the first selected character
/// @param end index of the last selected character + 1
virtual void select(int start, int end);
/// @brief Check text with validator set with setTextValidator
/// @return true if text is valid
virtual bool validate();
virtual void setValid(bool valid);
virtual bool isValid() const;
/// @brief Enable/disable multiline mode
virtual void setMultiline(bool multiline);
/// @brief Check if multiline mode is enabled
virtual bool isMultiline() const;
/// @brief Enable/disable text editing feature
virtual void setEditable(bool editable);
/// @brief Check if text editing feature is enabled
virtual bool isEditable() const;
/// @brief Set runnable called on textbox focus
virtual void setOnEditStart(runnable oneditstart);
virtual void onFocus(GUI*) override;
virtual void refresh() override;
virtual void click(GUI*, int, int) override;
virtual void mouseMove(GUI*, int x, int y) override;
virtual bool isFocuskeeper() const override {return true;}
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void typed(unsigned int codepoint) override;
virtual void keyPressed(keycode key) override;
virtual std::shared_ptr<UINode> getAt(glm::vec2 pos, std::shared_ptr<UINode> self) override;
};
class InputBindBox : public Panel {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 focusedColor {0.0f, 0.0f, 0.0f, 1.0f};
std::shared_ptr<Label> label;
Binding& binding;
public:
InputBindBox(Binding& binding, glm::vec4 padding=glm::vec4(6.0f));
virtual void drawBackground(const GfxContext* pctx, Assets* assets) override;
virtual void clicked(GUI*, mousecode button) override;
virtual void keyPressed(keycode key) override;
virtual bool isFocuskeeper() const override {return true;}
};
class TrackBar : public UINode {
protected:
glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f};
doublesupplier supplier = nullptr;
doubleconsumer consumer = nullptr;
double min;
double max;
double value;
double step;
int trackWidth;
public:
TrackBar(double min,
double max,
double value,
double step=1.0,
int trackWidth=1);
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void setSupplier(doublesupplier supplier);
virtual void setConsumer(doubleconsumer consumer);
virtual void mouseMove(GUI*, int x, int y) override;
virtual double getValue() const;
virtual double getMin() const;
virtual double getMax() const;
virtual double getStep() const;
virtual int getTrackWidth() const;
virtual glm::vec4 getTrackColor() const;
virtual void setValue(double);
virtual void setMin(double);
virtual void setMax(double);
virtual void setStep(double);
virtual void setTrackWidth(int);
virtual void setTrackColor(glm::vec4);
};
class CheckBox : public UINode {
protected:
glm::vec4 hoverColor {0.05f, 0.1f, 0.2f, 0.75f};
glm::vec4 checkColor {1.0f, 1.0f, 1.0f, 0.4f};
boolsupplier supplier = nullptr;
boolconsumer consumer = nullptr;
bool checked = false;
public:
CheckBox(bool checked=false);
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void mouseRelease(GUI*, int x, int y) override;
virtual void setSupplier(boolsupplier supplier);
virtual void setConsumer(boolconsumer consumer);
virtual CheckBox* setChecked(bool flag);
virtual bool isChecked() const {
if (supplier)
return supplier();
return checked;
}
};
class FullCheckBox : public Panel {
protected:
std::shared_ptr<CheckBox> checkbox;
public:
FullCheckBox(std::wstring text, glm::vec2 size, bool checked=false);
virtual void setSupplier(boolsupplier supplier) {
checkbox->setSupplier(supplier);
}
virtual void setConsumer(boolconsumer consumer) {
checkbox->setConsumer(consumer);
}
virtual void setChecked(bool flag) {
checkbox->setChecked(flag);
}
virtual bool isChecked() const {
return checkbox->isChecked();
}
};
}
#endif // GRAPHICS_UI_ELEMENTS_CONTROLS_H_

View File

@ -0,0 +1,35 @@
#include "Image.hpp"
#include "../../../core/GfxContext.h"
#include "../../../core/Batch2D.h"
#include "../../../core/Texture.h"
#include "../../../../assets/Assets.h"
#include "../../../../maths/UVRegion.h"
using namespace gui;
Image::Image(std::string texture, glm::vec2 size) : UINode(size), texture(texture) {
setInteractive(false);
}
void Image::draw(const GfxContext* pctx, Assets* assets) {
glm::vec2 pos = calcPos();
glm::vec4 color = getColor();
auto batch = pctx->getBatch2D();
auto texture = assets->getTexture(this->texture);
if (texture && autoresize) {
setSize(glm::vec2(texture->getWidth(), texture->getHeight()));
}
batch->texture(texture);
batch->setColor(color);
batch->rect(pos.x, pos.y, size.x, size.y,
0, 0, 0, UVRegion(), false, true, color);
}
void Image::setAutoResize(bool flag) {
autoresize = flag;
}
bool Image::isAutoResize() const {
return autoresize;
}

View File

@ -0,0 +1,21 @@
#ifndef GRAPHICS_UI_ELEMENTS_IMAGE_HPP_
#define GRAPHICS_UI_ELEMENTS_IMAGE_HPP_
#include "../UINode.h"
namespace gui {
class Image : public UINode {
protected:
std::string texture;
bool autoresize = false;
public:
Image(std::string texture, glm::vec2 size=glm::vec2(32,32));
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void setAutoResize(bool flag);
virtual bool isAutoResize() const;
};
}
#endif // GRAPHICS_UI_ELEMENTS_IMAGE_HPP_

View File

@ -0,0 +1,185 @@
#include "Label.hpp"
#include "../../../core/GfxContext.h"
#include "../../../core/Batch2D.h"
#include "../../../core/Font.h"
#include "../../../../assets/Assets.h"
#include "../../../../util/stringutil.h"
using namespace gui;
Label::Label(std::string text, std::string fontName)
: UINode(glm::vec2(text.length() * 8, 15)),
text(util::str2wstr_utf8(text)),
fontName(fontName)
{
setInteractive(false);
}
Label::Label(std::wstring text, std::string fontName)
: UINode(glm::vec2(text.length() * 8, 15)),
text(text),
fontName(fontName)
{
setInteractive(false);
}
void Label::setText(std::wstring text) {
this->text = text;
lines = 1;
for (size_t i = 0; i < text.length(); i++) {
if (text[i] == L'\n') {
lines++;
}
}
lines = std::max(lines, 1U);
}
const std::wstring& Label::getText() const {
return text;
}
void Label::setFontName(std::string name) {
this->fontName = name;
}
const std::string& Label::getFontName() const {
return fontName;
}
void Label::setVerticalAlign(Align align) {
this->valign = align;
}
Align Label::getVerticalAlign() const {
return valign;
}
float Label::getLineInterval() const {
return lineInterval;
}
void Label::setLineInterval(float interval) {
lineInterval = interval;
}
int Label::getTextYOffset() const {
return textYOffset;
}
size_t Label::getTextLineOffset(uint line) const {
size_t offset = 0;
size_t linesCount = 0;
while (linesCount < line && offset < text.length()) {
size_t endline = text.find(L'\n', offset);
if (endline == std::wstring::npos) {
break;
}
offset = endline+1;
linesCount++;
}
return offset;
}
int Label::getLineYOffset(uint line) const {
return line * totalLineHeight + textYOffset;
}
uint Label::getLineByYOffset(int offset) const {
if (offset < textYOffset) {
return 0;
}
return (offset - textYOffset) / totalLineHeight;
}
uint Label::getLineByTextIndex(size_t index) const {
size_t offset = 0;
size_t linesCount = 0;
while (offset < index && offset < text.length()) {
size_t endline = text.find(L'\n', offset);
if (endline == std::wstring::npos) {
break;
}
if (endline+1 > index) {
break;
}
offset = endline+1;
linesCount++;
}
return linesCount;
}
uint Label::getLinesNumber() const {
return lines;
}
void Label::draw(const GfxContext* pctx, Assets* assets) {
if (supplier) {
setText(supplier());
}
auto batch = pctx->getBatch2D();
auto font = assets->getFont(fontName);
batch->setColor(getColor());
uint lineHeight = font->getLineHeight();
glm::vec2 size = getSize();
glm::vec2 newsize (
font->calcWidth(text),
(lines == 1 ? lineHeight : lineHeight*lineInterval)*lines + font->getYOffset()
);
glm::vec2 pos = calcPos();
switch (align) {
case Align::left:
break;
case Align::center:
pos.x += (size.x-newsize.x)*0.5f;
break;
case Align::right:
pos.x += size.x-newsize.x;
break;
}
switch (valign) {
case Align::top:
break;
case Align::center:
pos.y += (size.y-newsize.y)*0.5f;
break;
case Align::bottom:
pos.y += size.y-newsize.y;
break;
}
textYOffset = pos.y-calcPos().y;
totalLineHeight = lineHeight * lineInterval;
if (multiline) {
size_t offset = 0;
for (uint i = 0; i < lines; i++) {
std::wstring_view view(text.c_str()+offset, text.length()-offset);
size_t end = view.find(L'\n');
if (end != std::wstring::npos) {
view = std::wstring_view(text.c_str()+offset, end);
offset += end + 1;
}
font->draw(batch, view, pos.x, pos.y + i * totalLineHeight, FontStyle::none);
}
} else {
font->draw(batch, text, pos.x, pos.y, FontStyle::none);
}
}
void Label::textSupplier(wstringsupplier supplier) {
this->supplier = supplier;
}
void Label::setMultiline(bool multiline) {
this->multiline = multiline;
}
bool Label::isMultiline() const {
return multiline;
}

View File

@ -0,0 +1,78 @@
#ifndef GRAPHICS_UI_ELEMENTS_LABEL_HPP_
#define GRAPHICS_UI_ELEMENTS_LABEL_HPP_
#include "../UINode.h"
namespace gui {
class Label : public UINode {
protected:
std::wstring text;
std::string fontName;
wstringsupplier supplier = nullptr;
uint lines = 1;
float lineInterval = 1.5f;
Align valign = Align::center;
bool multiline = false;
// runtime values
/// @brief Text Y offset relative to label position
/// (last calculated alignment)
int textYOffset = 0;
/// @brief Text line height multiplied by line interval
int totalLineHeight = 1;
public:
Label(std::string text, std::string fontName="normal");
Label(std::wstring text, std::string fontName="normal");
virtual void setText(std::wstring text);
const std::wstring& getText() const;
virtual void setFontName(std::string name);
virtual const std::string& getFontName() const;
/// @brief Set text vertical alignment (default value: center)
/// @param align Align::top / Align::center / Align::bottom
virtual void setVerticalAlign(Align align);
virtual Align getVerticalAlign() const;
/// @brief Get line height multiplier used for multiline labels
/// (default value: 1.5)
virtual float getLineInterval() const;
/// @brief Set line height multiplier used for multiline labels
virtual void setLineInterval(float interval);
/// @brief Get Y position of the text relative to label position
/// @return Y offset
virtual int getTextYOffset() const;
/// @brief Get Y position of the line relative to label position
/// @param line target line index
/// @return Y offset
virtual int getLineYOffset(uint line) const;
/// @brief Get position of line start in the text
/// @param line target line index
/// @return position in the text [0..length]
virtual size_t getTextLineOffset(uint line) const;
/// @brief Get line index by its Y offset relative to label position
/// @param offset target Y offset
/// @return line index [0..+]
virtual uint getLineByYOffset(int offset) const;
virtual uint getLineByTextIndex(size_t index) const;
virtual uint getLinesNumber() const;
virtual void draw(const GfxContext* pctx, Assets* assets) override;
virtual void textSupplier(wstringsupplier supplier);
virtual void setMultiline(bool multiline);
virtual bool isMultiline() const;
};
}
#endif // GRAPHICS_UI_ELEMENTS_LABEL_HPP_

View File

@ -1,10 +1,10 @@
#include "Plotter.hpp"
#include "../../core/Batch2D.h"
#include "../../core/Font.h"
#include "../../core/GfxContext.h"
#include "../../../assets/Assets.h"
#include "../../../util/stringutil.h"
#include "../../../core/Batch2D.h"
#include "../../../core/Font.h"
#include "../../../core/GfxContext.h"
#include "../../../../assets/Assets.h"
#include "../../../../util/stringutil.h"
using namespace gui;

View File

@ -1,8 +1,8 @@
#ifndef GRAPHICS_UI_ELEMENTS_PLOTTER_HPP_
#define GRAPHICS_UI_ELEMENTS_PLOTTER_HPP_
#include "UINode.h"
#include "../../../typedefs.h"
#include "../UINode.h"
#include "../../../../typedefs.h"
#include <memory>
#include <glm/glm.hpp>

View File

@ -1,7 +1,7 @@
#include "Container.hpp"
#include "../../core/GfxContext.h"
#include "../../core/Batch2D.h"
#include "../../../core/GfxContext.h"
#include "../../../core/Batch2D.h"
using namespace gui;

View File

@ -1,8 +1,8 @@
#ifndef GRAPHICS_UI_ELEMENTS_CONTAINER_HPP_
#define GRAPHICS_UI_ELEMENTS_CONTAINER_HPP_
#include "UINode.h"
#include "commons.hpp"
#include "../UINode.h"
#include "../commons.hpp"
#include <vector>

View File

@ -1,7 +1,7 @@
#ifndef GRAPHICS_UI_ELEMENTS_PANEL_HPP_
#define GRAPHICS_UI_ELEMENTS_PANEL_HPP_
#include "commons.hpp"
#include "../commons.hpp"
#include "Container.hpp"
namespace gui {

View File

@ -1,6 +1,7 @@
#include "gui_util.h"
#include "elements/controls.h"
#include "elements/Menu.hpp"
#include "elements/display/Label.hpp"
#include "elements/layout/Menu.hpp"
#include "elements/control/Button.hpp"
#include "gui_xml.h"
#include <glm/glm.hpp>

View File

@ -3,8 +3,13 @@
#include <charconv>
#include <stdexcept>
#include "elements/Panel.hpp"
#include "elements/controls.h"
#include "elements/layout/Panel.hpp"
#include "elements/display/Image.hpp"
#include "elements/control/Button.hpp"
#include "elements/control/CheckBox.hpp"
#include "elements/control/TextBox.hpp"
#include "elements/control/TrackBar.hpp"
#include "elements/control/InputBindBox.hpp"
#include "../../frontend/locale/langs.h"
#include "../../logic/scripting/scripting.h"

View File

@ -10,9 +10,12 @@
#include "../../../assets/Assets.h"
#include "../../../graphics/ui/gui_util.h"
#include "../../../graphics/ui/elements/UINode.h"
#include "../../../graphics/ui/elements/Panel.hpp"
#include "../../../graphics/ui/elements/Menu.hpp"
#include "../../../graphics/ui/elements/controls.h"
#include "../../../graphics/ui/elements/control/Button.hpp"
#include "../../../graphics/ui/elements/control/CheckBox.hpp"
#include "../../../graphics/ui/elements/control/TextBox.hpp"
#include "../../../graphics/ui/elements/control/TrackBar.hpp"
#include "../../../graphics/ui/elements/layout/Panel.hpp"
#include "../../../graphics/ui/elements/layout/Menu.hpp"
#include "../../../frontend/UiDocument.h"
#include "../../../frontend/locale/langs.h"
#include "../../../util/stringutil.h"