Compare commits
No commits in common. "669bce546d821333d76287cbb656eecdd048b43b" and "eae595ec4a125effe40a459f676c74f8a4b6faee" have entirely different histories.
669bce546d
...
eae595ec4a
@ -76,8 +76,7 @@ void UIApp::init(const AppInitArgs& args)
|
||||
.text = "Test-Text!\nSecond $f00line$fff?",
|
||||
.color = glm::vec4(0.f, 1.f, 0.f, 1.f),
|
||||
.posX = 100,
|
||||
.posY = 100,
|
||||
.textHeight = 20
|
||||
.posY = 100
|
||||
});
|
||||
mButton = mWidgetTree.getRootWidget().emplaceChild<Button>({
|
||||
.text = "Click Me!",
|
||||
@ -95,7 +94,7 @@ void UIApp::update(const AppUpdateArgs& args)
|
||||
Application::update(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();
|
||||
|
||||
// begin rendering
|
||||
|
||||
@ -52,7 +52,7 @@ void Button::setHoveredColor(const glm::vec4& color)
|
||||
}
|
||||
}
|
||||
|
||||
void Button::setPosX(float posX)
|
||||
void Button::setPosX(int posX)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@ -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()
|
||||
{
|
||||
update();
|
||||
|
||||
@ -17,10 +17,10 @@ struct ButtonCreateArgs
|
||||
glm::vec4 textColor = {1.f, 1.f, 1.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};
|
||||
float posX = 0;
|
||||
float posY = 0;
|
||||
float width = 256;
|
||||
float height = 84;
|
||||
int posX = 0;
|
||||
int posY = 0;
|
||||
int width = 256;
|
||||
int height = 84;
|
||||
};
|
||||
|
||||
class Button : public Widget
|
||||
@ -32,10 +32,10 @@ private:
|
||||
glm::vec4 mTextColor;
|
||||
glm::vec4 mBackgroundColor;
|
||||
glm::vec4 mHoveredColor;
|
||||
float mPosX = 0;
|
||||
float mPosY = 0;
|
||||
float mWidth = 0;
|
||||
float mHeight = 0;
|
||||
int mPosX = 0;
|
||||
int mPosY = 0;
|
||||
int mWidth = 0;
|
||||
int mHeight = 0;
|
||||
bool mHovered = false;
|
||||
UIRenderer::primitive_id_t mPrimitiveID = UIRenderer::UNSET_PRIMITIVE_ID;
|
||||
public:
|
||||
@ -54,25 +54,17 @@ public:
|
||||
const glm::vec4& getHoveredColor() const noexcept { return mHoveredColor; }
|
||||
|
||||
[[nodiscard]]
|
||||
float getPosX() const noexcept { return mPosX; }
|
||||
int getPosX() const noexcept { return mPosX; }
|
||||
|
||||
[[nodiscard]]
|
||||
float getPosY() const noexcept { return mPosY; }
|
||||
|
||||
[[nodiscard]]
|
||||
float getWidth() const noexcept { return mWidth; }
|
||||
|
||||
[[nodiscard]]
|
||||
float getHeight() const noexcept { return mHeight; }
|
||||
int getPosY() const noexcept { return mPosY; }
|
||||
|
||||
void setText(std::string text);
|
||||
void setTextColor(const glm::vec4& color);
|
||||
void setBackgroundColor(const glm::vec4& color);
|
||||
void setHoveredColor(const glm::vec4& color);
|
||||
void setPosX(float posX);
|
||||
void setPosY(float posY);
|
||||
void setWidth(float width);
|
||||
void setHeight(float height);
|
||||
void setPosX(int posX);
|
||||
void setPosY(int posY);
|
||||
|
||||
void handleEnteredTree() override;
|
||||
void handleMouseMotion(const sdlpp::MouseMotionEvent& event) override;
|
||||
|
||||
@ -3,8 +3,7 @@
|
||||
|
||||
namespace sdl_gpu_test
|
||||
{
|
||||
Label::Label(LabelCreateArgs args) : mText(std::move(args.text)), mColor(args.color), mPosX(args.posX),
|
||||
mPosY(args.posY), mTextHeight(args.textHeight)
|
||||
Label::Label(LabelCreateArgs args) : mText(std::move(args.text)), mColor(args.color), mPosX(args.posX), mPosY(args.posY)
|
||||
{
|
||||
|
||||
}
|
||||
@ -27,7 +26,7 @@ void Label::setColor(const glm::vec4& color)
|
||||
}
|
||||
}
|
||||
|
||||
void Label::setPosX(float posX)
|
||||
void Label::setPosX(int posX)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@ -45,16 +44,6 @@ void Label::setPosY(float posY)
|
||||
}
|
||||
}
|
||||
|
||||
void Label::setTextHeight(float textHeight)
|
||||
{
|
||||
if (textHeight != mTextHeight)
|
||||
{
|
||||
mTextHeight = textHeight;
|
||||
invalidate();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Label::handleEnteredTree()
|
||||
{
|
||||
update();
|
||||
@ -79,7 +68,6 @@ void Label::update()
|
||||
mTree->getRenderer().drawText({
|
||||
.x = mPosX,
|
||||
.y = mPosY,
|
||||
.height = mTextHeight,
|
||||
.text = mText,
|
||||
.color = mColor
|
||||
}, &mPrimitiveID);
|
||||
|
||||
@ -14,9 +14,8 @@ struct LabelCreateArgs
|
||||
{
|
||||
std::string text;
|
||||
glm::vec4 color = {1.f, 1.f, 1.f, 1.f};
|
||||
float posX = 0.f;
|
||||
float posY = 0.f;
|
||||
float textHeight = 38.f;
|
||||
int posX = 0;
|
||||
int posY = 0;
|
||||
};
|
||||
|
||||
class Label : public Widget
|
||||
@ -26,9 +25,8 @@ public:
|
||||
private:
|
||||
std::string mText;
|
||||
glm::vec4 mColor = {1.f, 1.f, 1.f, 1.f};
|
||||
float mPosX = 0.f;
|
||||
float mPosY = 0.f;
|
||||
float mTextHeight = 38.f;
|
||||
int mPosX = 0;
|
||||
int mPosY = 0;
|
||||
UIRenderer::primitive_id_t mPrimitiveID = UIRenderer::UNSET_PRIMITIVE_ID;
|
||||
public:
|
||||
explicit Label(LabelCreateArgs args);
|
||||
@ -40,19 +38,15 @@ public:
|
||||
const glm::vec4& getColor() const noexcept { return mColor; }
|
||||
|
||||
[[nodiscard]]
|
||||
float getPosX() const noexcept { return mPosX; }
|
||||
int getPosX() const noexcept { return mPosX; }
|
||||
|
||||
[[nodiscard]]
|
||||
float getPosY() const noexcept { return mPosY; }
|
||||
|
||||
[[nodiscard]]
|
||||
float getTextHeight() const noexcept { return mTextHeight; }
|
||||
int getPosY() const noexcept { return mPosY; }
|
||||
|
||||
void setText(std::string text);
|
||||
void setColor(const glm::vec4& color);
|
||||
void setPosX(float posX);
|
||||
void setPosY(float posY);
|
||||
void setTextHeight(float textHeight);
|
||||
void setPosX(int posX);
|
||||
void setPosY(int posY);
|
||||
|
||||
void handleEnteredTree() override;
|
||||
void revalidate() override;
|
||||
|
||||
@ -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);
|
||||
|
||||
float width = 0;
|
||||
float height = lineHeight;
|
||||
float lineWidth = 0;
|
||||
unsigned width = 0;
|
||||
unsigned height = mFontMap.lineHeight;
|
||||
unsigned lineWidth = 0;
|
||||
|
||||
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':
|
||||
width = std::max(lineWidth, width);
|
||||
lineWidth = 0;
|
||||
height += lineHeight;
|
||||
height += mFontMap.lineHeight;
|
||||
break;
|
||||
case '$':
|
||||
++pos;
|
||||
@ -207,9 +205,8 @@ std::pair<float, float> UIRenderer::measureText(const MeasureTextArgs& args) con
|
||||
[[fallthrough]];
|
||||
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
|
||||
lineWidth += factor * static_cast<float>(entry.xAdvance);
|
||||
lineWidth += entry.xAdvance;
|
||||
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)
|
||||
{
|
||||
const float lineHeight = static_cast<float>(mFontMap.lineHeight) * static_cast<float>(args.height) / static_cast<float>(mFontMap.base);
|
||||
|
||||
glm::vec4 color = args.color;
|
||||
float posX = args.x;
|
||||
float posY = args.y;
|
||||
int posX = args.x;
|
||||
int posY = args.y;
|
||||
|
||||
if (outPrimitiveId != nullptr && *outPrimitiveId == UNSET_PRIMITIVE_ID)
|
||||
{
|
||||
@ -239,7 +234,7 @@ void UIRenderer::drawText(const DrawTextArgs& args, primitive_id_t* outPrimitive
|
||||
{
|
||||
case '\n':
|
||||
posX = args.x;
|
||||
posY += lineHeight;
|
||||
posY += static_cast<int>(mFontMap.lineHeight);
|
||||
break;
|
||||
case '$':
|
||||
++pos;
|
||||
@ -285,13 +280,12 @@ void UIRenderer::drawText(const DrawTextArgs& args, primitive_id_t* outPrimitive
|
||||
chr = args.text[pos];
|
||||
[[fallthrough]];
|
||||
default:
|
||||
posX += drawChar({
|
||||
posX += static_cast<int>(drawChar({
|
||||
.x = posX,
|
||||
.y = posY,
|
||||
.height = args.height,
|
||||
.chr = chr,
|
||||
.color = color
|
||||
}, outPrimitiveId);
|
||||
}, outPrimitiveId));
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -299,37 +293,33 @@ void UIRenderer::drawText(const DrawTextArgs& args, primitive_id_t* outPrimitive
|
||||
|
||||
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({
|
||||
.text = args.text,
|
||||
.height = args.height
|
||||
.text = args.text
|
||||
});
|
||||
args.x -= width / 2;
|
||||
args.y -= (height + lineHeight - args.height) / 2.f;
|
||||
args.x -= static_cast<int>(width / 2);
|
||||
args.y -= static_cast<int>(height + mFontMap.lineHeight - mFontMap.base) / 2;
|
||||
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)
|
||||
{
|
||||
*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 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({
|
||||
.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},
|
||||
.uvBottomRight = {entry.uvX + entry.uvWidth, entry.uvY + entry.uvHeight},
|
||||
.color = args.color
|
||||
}, 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)
|
||||
|
||||
@ -23,23 +23,20 @@ struct UIVertex
|
||||
struct MeasureTextArgs
|
||||
{
|
||||
std::string_view text;
|
||||
float height = 38;
|
||||
};
|
||||
|
||||
struct DrawTextArgs
|
||||
{
|
||||
float x = 0.f;
|
||||
float y = 0.f;
|
||||
float height = 38.f;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
std::string_view text;
|
||||
glm::vec4 color = glm::vec4(1.f);
|
||||
};
|
||||
|
||||
struct DrawCharArgs
|
||||
{
|
||||
float x = 0.f;
|
||||
float y = 0.f;
|
||||
float height = 38.f;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
char chr;
|
||||
glm::vec4 color = glm::vec4(1.f);
|
||||
};
|
||||
@ -47,10 +44,10 @@ struct DrawCharArgs
|
||||
struct DrawQuadArgs
|
||||
{
|
||||
std::string texture;
|
||||
float x = 0.f;
|
||||
float y = 0.f;
|
||||
float width;
|
||||
float height;
|
||||
int x = 0;
|
||||
int y = 0;
|
||||
int width;
|
||||
int height;
|
||||
glm::vec4 color = glm::vec4(1.f);
|
||||
};
|
||||
|
||||
@ -82,17 +79,14 @@ private:
|
||||
bool mVerticesDirty = true;
|
||||
std::size_t mVertexBufferSize = 0;
|
||||
public:
|
||||
[[nodiscard]]
|
||||
std::size_t getNumVertices() const noexcept { return mVertices.size(); }
|
||||
|
||||
void init(Application& application);
|
||||
void render(const UIRendererRenderArgs& args);
|
||||
|
||||
[[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 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);
|
||||
bool removePrimitive(primitive_id_t primitiveId);
|
||||
private:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user