-
Notifications
You must be signed in to change notification settings - Fork 14
[Draft] Rework text vertex calculation #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -163,9 +163,9 @@ class OSRE_EXPORT CanvasRenderer : public IRenderPath { | |
| Rect2i mResolution; | ||
| i32 mActiveLayer; | ||
| i32 mNumLayers; | ||
| Font *mFont; | ||
| Mesh *mMesh; | ||
| Mesh *mText; | ||
| Font *mFont = nullptr; | ||
| Mesh *mMesh = nullptr; | ||
| Mesh *mText = nullptr; | ||
| }; | ||
|
|
||
| inline Font* CanvasRenderer::getActiveFont() const { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,12 +28,15 @@ namespace OSRE::Ui { | |
| using namespace ::OSRE::RenderBackend; | ||
|
|
||
| Button::Button(const String &label, const Rect2i &rect, WidgetBase *parent) : | ||
| WidgetBase(rect, parent), mLabel(label) { | ||
| WidgetBase(rect, parent), mText(nullptr) { | ||
| setDirty(); | ||
| if (!label.empty()) { | ||
| setLabel(label); | ||
| } | ||
| } | ||
|
|
||
| Button::~Button() { | ||
| // empty | ||
| delete mText; | ||
| } | ||
|
|
||
| void Button::onUpdate() { | ||
|
|
@@ -48,9 +51,6 @@ void Button::onRender(CanvasRenderer *renderer) { | |
| renderer->setColor(Color4(0.5f, 0.5f, 0.5f, 1.0f)); | ||
| const Rect2i &r = getRect(); | ||
| renderer->drawRect(r.x1, r.y1, r.width, r.height, true); | ||
| if (!mLabel.empty()) { | ||
| renderer->drawText(r.x1+1, r.y1+1, 40, mLabel); | ||
| } | ||
| setClean(); | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -21,8 +21,9 @@ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |||||||||||||||||||||||||||||||||
| CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||||||||||||||||||||||||||||||||||
| -----------------------------------------------------------------------------------------------*/ | ||||||||||||||||||||||||||||||||||
| #include "WidgetBase.h" | ||||||||||||||||||||||||||||||||||
| #include "UI/Text.h" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| namespace OSRE::Ui { | ||||||||||||||||||||||||||||||||||
| namespace OSRE::Ui { | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| //------------------------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||
| /// @ingroup Engine | ||||||||||||||||||||||||||||||||||
|
|
@@ -41,20 +42,24 @@ class OSRE_EXPORT Button : public WidgetBase { | |||||||||||||||||||||||||||||||||
| void onRender(RenderBackend::CanvasRenderer *renderer) override; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| private: | ||||||||||||||||||||||||||||||||||
| String mLabel; | ||||||||||||||||||||||||||||||||||
| Text *mText; | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| inline void Button::setLabel(const String &text) { | ||||||||||||||||||||||||||||||||||
| if (text == mLabel) { | ||||||||||||||||||||||||||||||||||
| if (mText == nullptr) { | ||||||||||||||||||||||||||||||||||
| mText = new Text(text, getRect(), this); | ||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| mLabel = text; | ||||||||||||||||||||||||||||||||||
| setDirty(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| mText->setLabel(text); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| inline const String &Button::getLabel() const { | ||||||||||||||||||||||||||||||||||
| return mLabel; | ||||||||||||||||||||||||||||||||||
| if (mText == nullptr) { | ||||||||||||||||||||||||||||||||||
| return String(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| return mText->getLabel(); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
Comment on lines
57
to
63
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential issue with returning reference to temporary object. The getLabel method returns a reference to a temporary String object when mText is nullptr, which is dangerous. The temporary object will be destroyed after the function returns, leaving the reference pointing to invalid memory. Fix this by creating a static empty string or returning by value: inline const String &Button::getLabel() const {
if (mText == nullptr) {
- return String();
+ static const String empty;
+ return empty;
}
return mText->getLabel();
}📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| } // namespace OSRE::Ui | ||||||||||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| /*----------------------------------------------------------------------------------------------- | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2015-2025 OSRE ( Open Source Render Engine ) by Kim Kulling | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| this software and associated documentation files (the "Software"), to deal in | ||
| the Software without restriction, including without limitation the rights to | ||
| use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| the Software, and to permit persons to whom the Software is furnished to do so, | ||
| subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| -----------------------------------------------------------------------------------------------*/ | ||
| #include "UI/Text.h" | ||
|
|
||
| namespace OSRE::Ui { | ||
|
|
||
| Text::Text(const String &label, const Rect2i &rect, WidgetBase *parent) : | ||
| WidgetBase(rect, parent), mLabel(label) { | ||
| setDirty(); | ||
| } | ||
|
|
||
| Text::~Text() { | ||
| // empty | ||
| } | ||
|
|
||
| void Text::setLabel(const String &text) { | ||
| if (text == mLabel) { | ||
| return; | ||
| } | ||
| mLabel = text; | ||
| setDirty(); | ||
| } | ||
|
|
||
| const String &Text::getLabel() const { | ||
| return mLabel; | ||
| } | ||
|
|
||
| void Text::onUpdate() { | ||
| // empty | ||
| } | ||
|
|
||
| void Text::onRender(RenderBackend::CanvasRenderer *renderer) { | ||
| osre_assert(renderer != nullptr); | ||
|
|
||
| if (isDirty()) { | ||
| renderer->selectLayer(1); | ||
| renderer->setColor(Color4(0.5f, 0.5f, 0.5f, 1.0f)); | ||
| const Rect2i &r = getRect(); | ||
| renderer->drawText(r.x1 + 1, r.y1 + 1, 40, mLabel); | ||
| setClean(); | ||
| } | ||
| } | ||
|
|
||
| } // namespace OSRE::Ui |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /*----------------------------------------------------------------------------------------------- | ||
| The MIT License (MIT) | ||
|
|
||
| Copyright (c) 2015-2025 OSRE ( Open Source Render Engine ) by Kim Kulling | ||
|
|
||
| Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| this software and associated documentation files (the "Software"), to deal in | ||
| the Software without restriction, including without limitation the rights to | ||
| use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| the Software, and to permit persons to whom the Software is furnished to do so, | ||
| subject to the following conditions: | ||
|
|
||
| The above copyright notice and this permission notice shall be included in all | ||
| copies or substantial portions of the Software. | ||
|
|
||
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | ||
| FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | ||
| COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | ||
| IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | ||
| CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
| -----------------------------------------------------------------------------------------------*/ | ||
| #pragma once | ||
|
|
||
| #include "UI/WidgetBase.h" | ||
| #include "RenderBackend/2D/CanvasRenderer.h" | ||
|
|
||
| namespace OSRE::Ui { | ||
|
|
||
| //------------------------------------------------------------------------------------------------- | ||
| /// @ingroup Engine | ||
| /// | ||
| /// @brief Todo! | ||
| //------------------------------------------------------------------------------------------------- | ||
| class OSRE_EXPORT Text : public WidgetBase { | ||
| public: | ||
| Text(const String &label, const Rect2i &rect, WidgetBase *parent = nullptr); | ||
| ~Text() override; | ||
| void setLabel(const String &text); | ||
| const String &getLabel() const; | ||
|
|
||
| protected: | ||
| void onUpdate() override; | ||
| void onRender(RenderBackend::CanvasRenderer *renderer) override; | ||
|
|
||
| private: | ||
| String mLabel; | ||
| }; | ||
|
|
||
| } // namespace OSRE::Ui |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
generateTextBoxVerticesAndIndices– eliminate unused arrays & potential overflowcolorsis completely filled but never used later. Remove it or use it to drivecolor0.ui16. For very long strings (> 6553 chars) this overflows. Either:• enforce
OSRE_ASSERT(text.size() < 6553)or• switch the public interface to
ui32.reserve()instead ofresize()+ manual assignment to avoid default-construction overhead.This keeps RAM the same but skips pointless writes.
🤖 Prompt for AI Agents