Stripped back old shader code for now.
This commit is contained in:
10
archive/ui/elements/CMakeLists.txt
Normal file
10
archive/ui/elements/CMakeLists.txt
Normal file
@ -0,0 +1,10 @@
|
||||
# Copyright (c) 2023 Dominic Masters
|
||||
#
|
||||
# This software is released under the MIT License.
|
||||
# https://opensource.org/licenses/MIT
|
||||
|
||||
target_sources(${DAWN_TARGET_NAME}
|
||||
PRIVATE
|
||||
UILabel.cpp
|
||||
UIRectangle.cpp
|
||||
)
|
175
archive/ui/elements/UILabel.cpp
Normal file
175
archive/ui/elements/UILabel.cpp
Normal file
@ -0,0 +1,175 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "UILabel.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void UILabel::getSelfQuads(UICanvas &ctx) {
|
||||
std::vector<struct UIShaderQuad> quads;
|
||||
if(this->texture == nullptr || this->text.empty()) return;
|
||||
|
||||
glm::vec4 quad;
|
||||
glm::vec2 pos = glm::vec2(0, this->texture->fontSize);
|
||||
bool_t lastCharWasSpace = false;
|
||||
|
||||
for(size_t i = 0; i < text.size(); i++) {
|
||||
wchar_t c = text[i];
|
||||
auto info = texture->getCharacterData(c);
|
||||
|
||||
// Newline(s)
|
||||
if(c == L'\n') {
|
||||
pos.x = 0;
|
||||
pos.y += this->texture->fontSize;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Spaces
|
||||
if(c == L' ') {
|
||||
pos.x += info.advance.x;
|
||||
lastCharWasSpace = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
// Word Wrap
|
||||
if(wordWrap) {
|
||||
if(lastCharWasSpace) {
|
||||
// Scan ahead to next space
|
||||
float_t wordWidth = pos.x;// Start at current position and scan ahead.
|
||||
for(size_t j = i; j < text.size(); j++) {
|
||||
wchar_t c2 = text[j];
|
||||
if(c2 == L' ' || c2 == L'\n') {
|
||||
break;// If we hit another space, we are OK.
|
||||
}
|
||||
|
||||
// Will this character fit on the row? If not the whole word will wrap.
|
||||
auto info2 = texture->getCharacterData(c);
|
||||
wordWidth += info.advance.x;
|
||||
if(wordWidth > size.x) {
|
||||
pos.x = 0;
|
||||
pos.y += this->texture->fontSize;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
lastCharWasSpace = false;
|
||||
}
|
||||
// } else if(pos.x + info.size.x > subAlignedPosition.x + size.x) {
|
||||
// // Not word wrap, but instead just overflow characters.
|
||||
// pos.x = 0;
|
||||
// pos.y += this->texture->fontSize;
|
||||
}
|
||||
|
||||
ctx.addQuad(
|
||||
{
|
||||
subAlignedPosition.x + pos.x + info.offset.x,
|
||||
subAlignedPosition.y + pos.y + info.offset.y,
|
||||
subAlignedPosition.x + pos.x + info.size.x + info.offset.x,
|
||||
subAlignedPosition.y + pos.y + info.size.y + info.offset.y
|
||||
},
|
||||
{
|
||||
info.quad.x,
|
||||
info.quad.y,
|
||||
info.quad.z,
|
||||
info.quad.w
|
||||
},
|
||||
this->color,
|
||||
UIShaderQuadStyle::FONT,
|
||||
texture->texture
|
||||
);
|
||||
pos += info.advance;
|
||||
}
|
||||
}
|
||||
|
||||
float_t UILabel::getContentWidth() {
|
||||
if(this->texture == nullptr || this->text.empty()) return 0.0f;
|
||||
|
||||
float_t lineWidth = 0.0f;
|
||||
float_t width = 0.0f;
|
||||
for(wchar_t c : text) {
|
||||
if(c == L'\n') {
|
||||
width = Math::max<float_t>(width, lineWidth);
|
||||
lineWidth = 0.0f;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto info = texture->getCharacterData(c);
|
||||
lineWidth += info.advance.x;
|
||||
if(
|
||||
this->hasExplicitWidth() &&
|
||||
lineWidth >= size.x
|
||||
) return size.x;
|
||||
}
|
||||
width = Math::max<float_t>(width, lineWidth);
|
||||
return width;
|
||||
}
|
||||
|
||||
float_t UILabel::getContentHeight() {
|
||||
if(this->texture == nullptr || this->text.empty()) return 0.0f;
|
||||
|
||||
float_t height = this->texture->fontSize;
|
||||
float_t lineWidth = 0.0f;
|
||||
bool_t lastCharWasSpace = false;
|
||||
|
||||
for(wchar_t c : text) {
|
||||
if(c == L'\n') {
|
||||
height += this->texture->fontSize;
|
||||
continue;
|
||||
}
|
||||
|
||||
auto info = texture->getCharacterData(c);
|
||||
|
||||
if(c == L' ') {
|
||||
lineWidth += info.advance.x;
|
||||
lastCharWasSpace = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if(wordWrap) {
|
||||
if(lastCharWasSpace) {
|
||||
// Scan ahead to next space
|
||||
float_t wordWidth = lineWidth;// Start at current position and scan ahead.
|
||||
for(size_t j = 0; j < text.size(); j++) {
|
||||
wchar_t c2 = text[j];
|
||||
if(c2 == L' ' || c2 == L'\n') {
|
||||
break;// If we hit another space, we are OK.
|
||||
}
|
||||
|
||||
// Will this character fit on the row? If not the whole word will wrap.
|
||||
auto info2 = texture->getCharacterData(c);
|
||||
wordWidth += info.advance.x;
|
||||
if(wordWidth > size.x) {
|
||||
height += this->texture->fontSize;
|
||||
lineWidth = 0.0f;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
lastCharWasSpace = false;
|
||||
}
|
||||
// } else if(lineWidth + info.size.x > size.x) {
|
||||
// height += this->texture->fontSize;
|
||||
// lineWidth = 0.0f;
|
||||
}
|
||||
}
|
||||
|
||||
return height;
|
||||
}
|
||||
|
||||
std::shared_ptr<TrueTypeTexture> UILabel::getFont() {
|
||||
return this->texture;
|
||||
}
|
||||
|
||||
std::wstring UILabel::getText() {
|
||||
return this->text;
|
||||
}
|
||||
|
||||
void UILabel::setFont(std::shared_ptr<TrueTypeTexture> texture) {
|
||||
this->texture = texture;
|
||||
}
|
||||
|
||||
void UILabel::setText(const std::wstring &text) {
|
||||
this->text = text;
|
||||
}
|
54
archive/ui/elements/UILabel.hpp
Normal file
54
archive/ui/elements/UILabel.hpp
Normal file
@ -0,0 +1,54 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "ui/UISubAlignableElement.hpp"
|
||||
#include "display/font/TrueTypeTexture.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class UILabel final : public UISubAlignableElement {
|
||||
private:
|
||||
std::shared_ptr<TrueTypeTexture> texture = nullptr;
|
||||
std::wstring text = L"Hello World";
|
||||
|
||||
protected:
|
||||
void getSelfQuads(UICanvas &ctx) override;
|
||||
|
||||
public:
|
||||
bool_t wordWrap = true;
|
||||
struct Color color = COLOR_WHITE;
|
||||
|
||||
float_t getContentWidth() override;
|
||||
float_t getContentHeight() override;
|
||||
|
||||
/**
|
||||
* Returns the font used for this label.
|
||||
*
|
||||
* @return The font used for this label.
|
||||
*/
|
||||
std::shared_ptr<TrueTypeTexture> getFont();
|
||||
|
||||
/**
|
||||
* Returns the text used for this label.
|
||||
*
|
||||
* @return The text used for this label.
|
||||
*/
|
||||
std::wstring getText();
|
||||
|
||||
/**
|
||||
* Sets the font to use for this label.
|
||||
*
|
||||
* @param texture TrueType texture to use for this label.
|
||||
*/
|
||||
void setFont(std::shared_ptr<TrueTypeTexture> texture);
|
||||
|
||||
/**
|
||||
* Sets the text to use for this label.
|
||||
*
|
||||
* @param text The text to use for this label.
|
||||
*/
|
||||
void setText(const std::wstring &text);
|
||||
};
|
||||
}
|
19
archive/ui/elements/UIRectangle.cpp
Normal file
19
archive/ui/elements/UIRectangle.cpp
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#include "UIRectangle.hpp"
|
||||
|
||||
using namespace Dawn;
|
||||
|
||||
void UIRectangle::getSelfQuads(UICanvas &ctx) {
|
||||
std::vector<struct UIShaderQuad> quads;
|
||||
ctx.addQuad(
|
||||
glm::vec4(position, position + size),
|
||||
uv,
|
||||
color,
|
||||
UIShaderQuadStyle::TEXTURED,
|
||||
texture
|
||||
);
|
||||
}
|
19
archive/ui/elements/UIRectangle.hpp
Normal file
19
archive/ui/elements/UIRectangle.hpp
Normal file
@ -0,0 +1,19 @@
|
||||
// Copyright (c) 2023 Dominic Masters
|
||||
//
|
||||
// This software is released under the MIT License.
|
||||
// https://opensource.org/licenses/MIT
|
||||
|
||||
#pragma once
|
||||
#include "ui/UIAlignableElement.hpp"
|
||||
|
||||
namespace Dawn {
|
||||
class UIRectangle final : public UIAlignableElement {
|
||||
protected:
|
||||
void getSelfQuads(UICanvas &ctx) override;
|
||||
|
||||
public:
|
||||
struct Color color = COLOR_WHITE;
|
||||
std::shared_ptr<Texture> texture = nullptr;
|
||||
glm::vec4 uv = glm::vec4(0,0,1,1);
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user