mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-08 12:31:51 +00:00
graphics/ui/elements/controls deleted
This commit is contained in:
parent
85f7febd30
commit
ed51751daa
34 changed files with 1506 additions and 1420 deletions
35
src/graphics/ui/elements/display/Image.cpp
Normal file
35
src/graphics/ui/elements/display/Image.cpp
Normal 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;
|
||||
}
|
||||
21
src/graphics/ui/elements/display/Image.hpp
Normal file
21
src/graphics/ui/elements/display/Image.hpp
Normal 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_
|
||||
185
src/graphics/ui/elements/display/Label.cpp
Normal file
185
src/graphics/ui/elements/display/Label.cpp
Normal 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;
|
||||
}
|
||||
78
src/graphics/ui/elements/display/Label.hpp
Normal file
78
src/graphics/ui/elements/display/Label.hpp
Normal 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_
|
||||
52
src/graphics/ui/elements/display/Plotter.cpp
Normal file
52
src/graphics/ui/elements/display/Plotter.cpp
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
#include "Plotter.hpp"
|
||||
|
||||
#include "../../../core/Batch2D.h"
|
||||
#include "../../../core/Font.h"
|
||||
#include "../../../core/GfxContext.h"
|
||||
#include "../../../../assets/Assets.h"
|
||||
#include "../../../../util/stringutil.h"
|
||||
|
||||
using namespace gui;
|
||||
|
||||
void Plotter::act(float delta) {
|
||||
index = index + 1 % dmwidth;
|
||||
int value = static_cast<int>(delta * multiplier);
|
||||
points[index % dmwidth] = std::min(value, dmheight);
|
||||
}
|
||||
|
||||
void Plotter::draw(const GfxContext* pctx, Assets* assets) {
|
||||
glm::vec2 pos = calcPos();
|
||||
auto batch = pctx->getBatch2D();
|
||||
batch->texture(nullptr);
|
||||
batch->lineWidth(1);
|
||||
for (int i = index+1; i < index+dmwidth; i++) {
|
||||
int j = i % dmwidth;
|
||||
batch->line(
|
||||
pos.x + i - index, pos.y + size.y - points[j],
|
||||
pos.x + i - index, pos.y + size.y, 1.0f, 1.0f, 1.0f, 0.2f
|
||||
);
|
||||
}
|
||||
batch->setColor({1,1,1,0.2f});
|
||||
batch->lineRect(pos.x, pos.y, dmwidth, dmheight);
|
||||
for (int y = 0; y < dmheight; y += 16) {
|
||||
batch->line(
|
||||
pos.x+dmwidth-4, pos.y+dmheight-y,
|
||||
pos.x+dmwidth+4, pos.y+dmheight-y,
|
||||
1.0f, 1.0f, 1.0f, 0.2f
|
||||
);
|
||||
}
|
||||
|
||||
int current_point = static_cast<int>(points[index % dmwidth]);
|
||||
auto font = assets->getFont("normal");
|
||||
for (int y = 0; y < dmheight; y += labelsInterval) {
|
||||
std::wstring string;
|
||||
if (current_point/16 == y/labelsInterval) {
|
||||
batch->setColor({1,1,1,0.5f});
|
||||
string = util::to_wstring(current_point / multiplier, 3);
|
||||
} else {
|
||||
batch->setColor({1,1,1,0.2f});
|
||||
string = util::to_wstring(y / multiplier, 3);
|
||||
}
|
||||
font->draw(batch, string, pos.x+dmwidth+2, pos.y+dmheight-y-labelsInterval);
|
||||
}
|
||||
}
|
||||
37
src/graphics/ui/elements/display/Plotter.hpp
Normal file
37
src/graphics/ui/elements/display/Plotter.hpp
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
#ifndef GRAPHICS_UI_ELEMENTS_PLOTTER_HPP_
|
||||
#define GRAPHICS_UI_ELEMENTS_PLOTTER_HPP_
|
||||
|
||||
#include "../UINode.h"
|
||||
#include "../../../../typedefs.h"
|
||||
|
||||
#include <memory>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
class Assets;
|
||||
class GfxContext;
|
||||
|
||||
namespace gui {
|
||||
class Plotter : public gui::UINode {
|
||||
std::unique_ptr<int[]> points;
|
||||
float multiplier;
|
||||
int index = 0;
|
||||
int dmwidth;
|
||||
int dmheight;
|
||||
int labelsInterval;
|
||||
public:
|
||||
Plotter(uint width, uint height, float multiplier, int labelsInterval)
|
||||
: gui::UINode(glm::vec2(width, height)),
|
||||
multiplier(multiplier),
|
||||
dmwidth(width-50),
|
||||
dmheight(height),
|
||||
labelsInterval(labelsInterval)
|
||||
{
|
||||
points = std::make_unique<int[]>(dmwidth);
|
||||
}
|
||||
|
||||
void act(float delta) override;
|
||||
void draw(const GfxContext* pctx, Assets* assets) override;
|
||||
};
|
||||
}
|
||||
|
||||
#endif // GRAPHICS_UI_ELEMENTS_PLOTTER_HPP_
|
||||
Loading…
Add table
Add a link
Reference in a new issue