Compare commits

..

No commits in common. "669bce546d821333d76287cbb656eecdd048b43b" and "eae595ec4a125effe40a459f676c74f8a4b6faee" have entirely different histories.

7 changed files with 55 additions and 118 deletions

View File

@ -76,8 +76,7 @@ void UIApp::init(const AppInitArgs& args)
.text = "Test-Text!\nSecond $f00line$fff?", .text = "Test-Text!\nSecond $f00line$fff?",
.color = glm::vec4(0.f, 1.f, 0.f, 1.f), .color = glm::vec4(0.f, 1.f, 0.f, 1.f),
.posX = 100, .posX = 100,
.posY = 100, .posY = 100
.textHeight = 20
}); });
mButton = mWidgetTree.getRootWidget().emplaceChild<Button>({ mButton = mWidgetTree.getRootWidget().emplaceChild<Button>({
.text = "Click Me!", .text = "Click Me!",
@ -95,7 +94,7 @@ void UIApp::update(const AppUpdateArgs& args)
Application::update(args); Application::update(args);
processInput(args); processInput(args);
mLabel->setText(std::format("Rotation: {}{}\n$rUI vertices: $f8c{}", mRotation > 180.f ? "$8cf": "$fff" , mRotation, mUIRenderer.getNumVertices())); mLabel->setText(std::format("Rotation: {}{}\n$rUI vertices: $f00{}", mRotation > 180.f ? "$00f": "$fff" , mRotation, mNumVertices));
mWidgetTree.revalidateWidgets(); mWidgetTree.revalidateWidgets();
// begin rendering // begin rendering

View File

@ -52,7 +52,7 @@ void Button::setHoveredColor(const glm::vec4& color)
} }
} }
void Button::setPosX(float posX) void Button::setPosX(int posX)
{ {
if (posX != mPosX) if (posX != mPosX)
{ {
@ -61,7 +61,7 @@ void Button::setPosX(float posX)
} }
} }
void Button::setPosY(float posY) void Button::setPosY(int posY)
{ {
if (posY != mPosY) if (posY != mPosY)
{ {
@ -70,26 +70,6 @@ void Button::setPosY(float posY)
} }
} }
void Button::setWidth(float width)
{
if (width != mWidth)
{
mWidth = width;
invalidate();
}
}
void Button::setHeight(float height)
{
if (height != mHeight)
{
mHeight = height;
invalidate();
}
}
void Button::handleEnteredTree() void Button::handleEnteredTree()
{ {
update(); update();

View File

@ -17,10 +17,10 @@ struct ButtonCreateArgs
glm::vec4 textColor = {1.f, 1.f, 1.f, 1.f}; glm::vec4 textColor = {1.f, 1.f, 1.f, 1.f};
glm::vec4 backgroundColor = {1.f, 0.f, 0.f, 1.f}; glm::vec4 backgroundColor = {1.f, 0.f, 0.f, 1.f};
glm::vec4 hoveredColor = {1.f, 0.9f, 0.9f, 1.f}; glm::vec4 hoveredColor = {1.f, 0.9f, 0.9f, 1.f};
float posX = 0; int posX = 0;
float posY = 0; int posY = 0;
float width = 256; int width = 256;
float height = 84; int height = 84;
}; };
class Button : public Widget class Button : public Widget
@ -32,10 +32,10 @@ private:
glm::vec4 mTextColor; glm::vec4 mTextColor;
glm::vec4 mBackgroundColor; glm::vec4 mBackgroundColor;
glm::vec4 mHoveredColor; glm::vec4 mHoveredColor;
float mPosX = 0; int mPosX = 0;
float mPosY = 0; int mPosY = 0;
float mWidth = 0; int mWidth = 0;
float mHeight = 0; int mHeight = 0;
bool mHovered = false; bool mHovered = false;
UIRenderer::primitive_id_t mPrimitiveID = UIRenderer::UNSET_PRIMITIVE_ID; UIRenderer::primitive_id_t mPrimitiveID = UIRenderer::UNSET_PRIMITIVE_ID;
public: public:
@ -54,25 +54,17 @@ public:
const glm::vec4& getHoveredColor() const noexcept { return mHoveredColor; } const glm::vec4& getHoveredColor() const noexcept { return mHoveredColor; }
[[nodiscard]] [[nodiscard]]
float getPosX() const noexcept { return mPosX; } int getPosX() const noexcept { return mPosX; }
[[nodiscard]] [[nodiscard]]
float getPosY() const noexcept { return mPosY; } int getPosY() const noexcept { return mPosY; }
[[nodiscard]]
float getWidth() const noexcept { return mWidth; }
[[nodiscard]]
float getHeight() const noexcept { return mHeight; }
void setText(std::string text); void setText(std::string text);
void setTextColor(const glm::vec4& color); void setTextColor(const glm::vec4& color);
void setBackgroundColor(const glm::vec4& color); void setBackgroundColor(const glm::vec4& color);
void setHoveredColor(const glm::vec4& color); void setHoveredColor(const glm::vec4& color);
void setPosX(float posX); void setPosX(int posX);
void setPosY(float posY); void setPosY(int posY);
void setWidth(float width);
void setHeight(float height);
void handleEnteredTree() override; void handleEnteredTree() override;
void handleMouseMotion(const sdlpp::MouseMotionEvent& event) override; void handleMouseMotion(const sdlpp::MouseMotionEvent& event) override;

View File

@ -3,8 +3,7 @@
namespace sdl_gpu_test namespace sdl_gpu_test
{ {
Label::Label(LabelCreateArgs args) : mText(std::move(args.text)), mColor(args.color), mPosX(args.posX), Label::Label(LabelCreateArgs args) : mText(std::move(args.text)), mColor(args.color), mPosX(args.posX), mPosY(args.posY)
mPosY(args.posY), mTextHeight(args.textHeight)
{ {
} }
@ -27,7 +26,7 @@ void Label::setColor(const glm::vec4& color)
} }
} }
void Label::setPosX(float posX) void Label::setPosX(int posX)
{ {
if (posX != mPosX) if (posX != mPosX)
{ {
@ -36,7 +35,7 @@ void Label::setPosX(float posX)
} }
} }
void Label::setPosY(float posY) void Label::setPosY(int posY)
{ {
if (posY != mPosY) if (posY != mPosY)
{ {
@ -45,16 +44,6 @@ void Label::setPosY(float posY)
} }
} }
void Label::setTextHeight(float textHeight)
{
if (textHeight != mTextHeight)
{
mTextHeight = textHeight;
invalidate();
}
}
void Label::handleEnteredTree() void Label::handleEnteredTree()
{ {
update(); update();
@ -79,7 +68,6 @@ void Label::update()
mTree->getRenderer().drawText({ mTree->getRenderer().drawText({
.x = mPosX, .x = mPosX,
.y = mPosY, .y = mPosY,
.height = mTextHeight,
.text = mText, .text = mText,
.color = mColor .color = mColor
}, &mPrimitiveID); }, &mPrimitiveID);

View File

@ -14,9 +14,8 @@ struct LabelCreateArgs
{ {
std::string text; std::string text;
glm::vec4 color = {1.f, 1.f, 1.f, 1.f}; glm::vec4 color = {1.f, 1.f, 1.f, 1.f};
float posX = 0.f; int posX = 0;
float posY = 0.f; int posY = 0;
float textHeight = 38.f;
}; };
class Label : public Widget class Label : public Widget
@ -26,9 +25,8 @@ public:
private: private:
std::string mText; std::string mText;
glm::vec4 mColor = {1.f, 1.f, 1.f, 1.f}; glm::vec4 mColor = {1.f, 1.f, 1.f, 1.f};
float mPosX = 0.f; int mPosX = 0;
float mPosY = 0.f; int mPosY = 0;
float mTextHeight = 38.f;
UIRenderer::primitive_id_t mPrimitiveID = UIRenderer::UNSET_PRIMITIVE_ID; UIRenderer::primitive_id_t mPrimitiveID = UIRenderer::UNSET_PRIMITIVE_ID;
public: public:
explicit Label(LabelCreateArgs args); explicit Label(LabelCreateArgs args);
@ -40,19 +38,15 @@ public:
const glm::vec4& getColor() const noexcept { return mColor; } const glm::vec4& getColor() const noexcept { return mColor; }
[[nodiscard]] [[nodiscard]]
float getPosX() const noexcept { return mPosX; } int getPosX() const noexcept { return mPosX; }
[[nodiscard]] [[nodiscard]]
float getPosY() const noexcept { return mPosY; } int getPosY() const noexcept { return mPosY; }
[[nodiscard]]
float getTextHeight() const noexcept { return mTextHeight; }
void setText(std::string text); void setText(std::string text);
void setColor(const glm::vec4& color); void setColor(const glm::vec4& color);
void setPosX(float posX); void setPosX(int posX);
void setPosY(float posY); void setPosY(int posY);
void setTextHeight(float textHeight);
void handleEnteredTree() override; void handleEnteredTree() override;
void revalidate() override; void revalidate() override;

View File

@ -153,13 +153,11 @@ void UIRenderer::render(const UIRendererRenderArgs& args)
} }
} }
std::pair<float, float> UIRenderer::measureText(const MeasureTextArgs& args) const std::pair<unsigned, unsigned> UIRenderer::measureText(const MeasureTextArgs& args) const
{ {
const float lineHeight = static_cast<float>(mFontMap.lineHeight) * static_cast<float>(args.height) / static_cast<float>(mFontMap.base); unsigned width = 0;
unsigned height = mFontMap.lineHeight;
float width = 0; unsigned lineWidth = 0;
float height = lineHeight;
float lineWidth = 0;
for (std::size_t pos = 0; pos < args.text.size(); ++pos) for (std::size_t pos = 0; pos < args.text.size(); ++pos)
{ {
@ -169,7 +167,7 @@ std::pair<float, float> UIRenderer::measureText(const MeasureTextArgs& args) con
case '\n': case '\n':
width = std::max(lineWidth, width); width = std::max(lineWidth, width);
lineWidth = 0; lineWidth = 0;
height += lineHeight; height += mFontMap.lineHeight;
break; break;
case '$': case '$':
++pos; ++pos;
@ -207,9 +205,8 @@ std::pair<float, float> UIRenderer::measureText(const MeasureTextArgs& args) con
[[fallthrough]]; [[fallthrough]];
default: default:
{ {
const float factor = static_cast<float>(args.height) / static_cast<float>(mFontMap.base);
const UVFontMapEntry& entry = mFontMap.entries[chr < 0 ? '_' : chr]; // TODO: more chars const UVFontMapEntry& entry = mFontMap.entries[chr < 0 ? '_' : chr]; // TODO: more chars
lineWidth += factor * static_cast<float>(entry.xAdvance); lineWidth += entry.xAdvance;
break; break;
} }
} }
@ -221,11 +218,9 @@ std::pair<float, float> UIRenderer::measureText(const MeasureTextArgs& args) con
void UIRenderer::drawText(const DrawTextArgs& args, primitive_id_t* outPrimitiveId) void UIRenderer::drawText(const DrawTextArgs& args, primitive_id_t* outPrimitiveId)
{ {
const float lineHeight = static_cast<float>(mFontMap.lineHeight) * static_cast<float>(args.height) / static_cast<float>(mFontMap.base);
glm::vec4 color = args.color; glm::vec4 color = args.color;
float posX = args.x; int posX = args.x;
float posY = args.y; int posY = args.y;
if (outPrimitiveId != nullptr && *outPrimitiveId == UNSET_PRIMITIVE_ID) if (outPrimitiveId != nullptr && *outPrimitiveId == UNSET_PRIMITIVE_ID)
{ {
@ -239,7 +234,7 @@ void UIRenderer::drawText(const DrawTextArgs& args, primitive_id_t* outPrimitive
{ {
case '\n': case '\n':
posX = args.x; posX = args.x;
posY += lineHeight; posY += static_cast<int>(mFontMap.lineHeight);
break; break;
case '$': case '$':
++pos; ++pos;
@ -285,13 +280,12 @@ void UIRenderer::drawText(const DrawTextArgs& args, primitive_id_t* outPrimitive
chr = args.text[pos]; chr = args.text[pos];
[[fallthrough]]; [[fallthrough]];
default: default:
posX += drawChar({ posX += static_cast<int>(drawChar({
.x = posX, .x = posX,
.y = posY, .y = posY,
.height = args.height,
.chr = chr, .chr = chr,
.color = color .color = color
}, outPrimitiveId); }, outPrimitiveId));
break; break;
} }
} }
@ -299,37 +293,33 @@ void UIRenderer::drawText(const DrawTextArgs& args, primitive_id_t* outPrimitive
void UIRenderer::drawTextCentered(DrawTextArgs args, primitive_id_t* outPrimitiveId) void UIRenderer::drawTextCentered(DrawTextArgs args, primitive_id_t* outPrimitiveId)
{ {
const float lineHeight = static_cast<float>(mFontMap.lineHeight) * args.height / static_cast<float>(mFontMap.base);
const auto [width, height] = measureText({ const auto [width, height] = measureText({
.text = args.text, .text = args.text
.height = args.height
}); });
args.x -= width / 2; args.x -= static_cast<int>(width / 2);
args.y -= (height + lineHeight - args.height) / 2.f; args.y -= static_cast<int>(height + mFontMap.lineHeight - mFontMap.base) / 2;
drawText(args, outPrimitiveId); drawText(args, outPrimitiveId);
} }
float UIRenderer::drawChar(const DrawCharArgs& args, primitive_id_t* outPrimitiveId) unsigned UIRenderer::drawChar(const DrawCharArgs& args, primitive_id_t* outPrimitiveId)
{ {
if (outPrimitiveId != nullptr && *outPrimitiveId == UNSET_PRIMITIVE_ID) if (outPrimitiveId != nullptr && *outPrimitiveId == UNSET_PRIMITIVE_ID)
{ {
*outPrimitiveId = mNextOwner++; *outPrimitiveId = mNextOwner++;
} }
const float factor = static_cast<float>(args.height) / static_cast<float>(mFontMap.base);
const UVFontMapEntry& entry = mFontMap.entries[args.chr < 0 ? '_' : args.chr]; // TODO: more chars const UVFontMapEntry& entry = mFontMap.entries[args.chr < 0 ? '_' : args.chr]; // TODO: more chars
const glm::vec2 topLeft = {args.x + factor * entry.xOffset, args.y + factor * entry.yOffset}; const glm::vec2 topLeft = {args.x + entry.xOffset, args.y + entry.yOffset};
drawQuadInternal({ drawQuadInternal({
.topLeft = topLeft, .topLeft = topLeft,
.bottomRight = {topLeft.x + factor * static_cast<float>(entry.width), topLeft.y + factor * static_cast<float>(entry.height)}, .bottomRight = {topLeft.x + static_cast<float>(entry.width), topLeft.y + static_cast<float>(entry.height)},
.uvTopLeft = {entry.uvX, entry.uvY}, .uvTopLeft = {entry.uvX, entry.uvY},
.uvBottomRight = {entry.uvX + entry.uvWidth, entry.uvY + entry.uvHeight}, .uvBottomRight = {entry.uvX + entry.uvWidth, entry.uvY + entry.uvHeight},
.color = args.color .color = args.color
}, outPrimitiveId == nullptr ? UNSET_PRIMITIVE_ID : *outPrimitiveId); }, outPrimitiveId == nullptr ? UNSET_PRIMITIVE_ID : *outPrimitiveId);
return factor * static_cast<float>(entry.xAdvance); return entry.xAdvance;
} }
void UIRenderer::drawQuad(const DrawQuadArgs& args, primitive_id_t* outPrimitiveId) void UIRenderer::drawQuad(const DrawQuadArgs& args, primitive_id_t* outPrimitiveId)

View File

@ -23,23 +23,20 @@ struct UIVertex
struct MeasureTextArgs struct MeasureTextArgs
{ {
std::string_view text; std::string_view text;
float height = 38;
}; };
struct DrawTextArgs struct DrawTextArgs
{ {
float x = 0.f; int x = 0;
float y = 0.f; int y = 0;
float height = 38.f;
std::string_view text; std::string_view text;
glm::vec4 color = glm::vec4(1.f); glm::vec4 color = glm::vec4(1.f);
}; };
struct DrawCharArgs struct DrawCharArgs
{ {
float x = 0.f; int x = 0;
float y = 0.f; int y = 0;
float height = 38.f;
char chr; char chr;
glm::vec4 color = glm::vec4(1.f); glm::vec4 color = glm::vec4(1.f);
}; };
@ -47,10 +44,10 @@ struct DrawCharArgs
struct DrawQuadArgs struct DrawQuadArgs
{ {
std::string texture; std::string texture;
float x = 0.f; int x = 0;
float y = 0.f; int y = 0;
float width; int width;
float height; int height;
glm::vec4 color = glm::vec4(1.f); glm::vec4 color = glm::vec4(1.f);
}; };
@ -82,17 +79,14 @@ private:
bool mVerticesDirty = true; bool mVerticesDirty = true;
std::size_t mVertexBufferSize = 0; std::size_t mVertexBufferSize = 0;
public: public:
[[nodiscard]]
std::size_t getNumVertices() const noexcept { return mVertices.size(); }
void init(Application& application); void init(Application& application);
void render(const UIRendererRenderArgs& args); void render(const UIRendererRenderArgs& args);
[[nodiscard]] [[nodiscard]]
std::pair<float, float> measureText(const MeasureTextArgs& args) const; std::pair<unsigned, unsigned> measureText(const MeasureTextArgs& args) const;
void drawText(const DrawTextArgs& args, primitive_id_t* outPrimitiveId = nullptr); void drawText(const DrawTextArgs& args, primitive_id_t* outPrimitiveId = nullptr);
void drawTextCentered(DrawTextArgs args, primitive_id_t* outPrimitiveId = nullptr); void drawTextCentered(DrawTextArgs args, primitive_id_t* outPrimitiveId = nullptr);
float drawChar(const DrawCharArgs& args, primitive_id_t* outPrimitiveId = nullptr); unsigned drawChar(const DrawCharArgs& args, primitive_id_t* outPrimitiveId = nullptr);
void drawQuad(const DrawQuadArgs& args, primitive_id_t* outPrimitiveId = nullptr); void drawQuad(const DrawQuadArgs& args, primitive_id_t* outPrimitiveId = nullptr);
bool removePrimitive(primitive_id_t primitiveId); bool removePrimitive(primitive_id_t primitiveId);
private: private: