refactor: move position property to UIDrawable base class (UICaption)
- Update UICaption to use base class position instead of text position - Synchronize text position with base class position for rendering - Add onPositionChanged() virtual method for position synchronization - Update all UICaption methods to use base position consistently - Add comprehensive test coverage for UICaption position handling This is part 2 of moving position to the base class. UISprite and UIGrid will be updated in subsequent commits.
This commit is contained in:
parent
c4b4f12758
commit
5d24ba6a85
Binary file not shown.
After Width: | Height: | Size: 39 KiB |
|
@ -11,7 +11,8 @@ UICaption::UICaption()
|
|||
{
|
||||
// Initialize text with safe defaults
|
||||
text.setString("");
|
||||
text.setPosition(0.0f, 0.0f);
|
||||
position = sf::Vector2f(0.0f, 0.0f); // Set base class position
|
||||
text.setPosition(position); // Sync text position
|
||||
text.setCharacterSize(12);
|
||||
text.setFillColor(sf::Color::White);
|
||||
text.setOutlineColor(sf::Color::Black);
|
||||
|
@ -60,7 +61,9 @@ sf::FloatRect UICaption::get_bounds() const
|
|||
|
||||
void UICaption::move(float dx, float dy)
|
||||
{
|
||||
text.move(dx, dy);
|
||||
position.x += dx;
|
||||
position.y += dy;
|
||||
text.setPosition(position); // Keep text in sync
|
||||
}
|
||||
|
||||
void UICaption::resize(float w, float h)
|
||||
|
@ -69,6 +72,12 @@ void UICaption::resize(float w, float h)
|
|||
// This is a no-op but required by the interface
|
||||
}
|
||||
|
||||
void UICaption::onPositionChanged()
|
||||
{
|
||||
// Sync text position with base class position
|
||||
text.setPosition(position);
|
||||
}
|
||||
|
||||
PyObject* UICaption::get_float_member(PyUICaptionObject* self, void* closure)
|
||||
{
|
||||
auto member_ptr = reinterpret_cast<long>(closure);
|
||||
|
@ -237,9 +246,9 @@ int UICaption::set_text(PyUICaptionObject* self, PyObject* value, void* closure)
|
|||
}
|
||||
|
||||
PyGetSetDef UICaption::getsetters[] = {
|
||||
{"x", (getter)UICaption::get_float_member, (setter)UICaption::set_float_member, "X coordinate of top-left corner", (void*)0},
|
||||
{"y", (getter)UICaption::get_float_member, (setter)UICaption::set_float_member, "Y coordinate of top-left corner", (void*)1},
|
||||
{"pos", (getter)UICaption::get_vec_member, (setter)UICaption::set_vec_member, "(x, y) vector", (void*)0},
|
||||
{"x", (getter)UIDrawable::get_float_member, (setter)UIDrawable::set_float_member, "X coordinate of top-left corner", (void*)((intptr_t)PyObjectsEnum::UICAPTION << 8 | 0)},
|
||||
{"y", (getter)UIDrawable::get_float_member, (setter)UIDrawable::set_float_member, "Y coordinate of top-left corner", (void*)((intptr_t)PyObjectsEnum::UICAPTION << 8 | 1)},
|
||||
{"pos", (getter)UIDrawable::get_pos, (setter)UIDrawable::set_pos, "(x, y) vector", (void*)PyObjectsEnum::UICAPTION},
|
||||
//{"w", (getter)PyUIFrame_get_float_member, (setter)PyUIFrame_set_float_member, "width of the rectangle", (void*)2},
|
||||
//{"h", (getter)PyUIFrame_get_float_member, (setter)PyUIFrame_set_float_member, "height of the rectangle", (void*)3},
|
||||
{"outline", (getter)UICaption::get_float_member, (setter)UICaption::set_float_member, "Thickness of the border", (void*)4},
|
||||
|
@ -362,7 +371,8 @@ int UICaption::init(PyUICaptionObject* self, PyObject* args, PyObject* kwds)
|
|||
}
|
||||
}
|
||||
|
||||
self->data->text.setPosition(x, y);
|
||||
self->data->position = sf::Vector2f(x, y); // Set base class position
|
||||
self->data->text.setPosition(self->data->position); // Sync text position
|
||||
// check types for font, fill_color, outline_color
|
||||
|
||||
//std::cout << PyUnicode_AsUTF8(PyObject_Repr(font)) << std::endl;
|
||||
|
@ -435,11 +445,13 @@ int UICaption::init(PyUICaptionObject* self, PyObject* args, PyObject* kwds)
|
|||
// Property system implementation for animations
|
||||
bool UICaption::setProperty(const std::string& name, float value) {
|
||||
if (name == "x") {
|
||||
text.setPosition(sf::Vector2f(value, text.getPosition().y));
|
||||
position.x = value;
|
||||
text.setPosition(position); // Keep text in sync
|
||||
return true;
|
||||
}
|
||||
else if (name == "y") {
|
||||
text.setPosition(sf::Vector2f(text.getPosition().x, value));
|
||||
position.y = value;
|
||||
text.setPosition(position); // Keep text in sync
|
||||
return true;
|
||||
}
|
||||
else if (name == "font_size" || name == "size") { // Support both for backward compatibility
|
||||
|
@ -527,11 +539,11 @@ bool UICaption::setProperty(const std::string& name, const std::string& value) {
|
|||
|
||||
bool UICaption::getProperty(const std::string& name, float& value) const {
|
||||
if (name == "x") {
|
||||
value = text.getPosition().x;
|
||||
value = position.x;
|
||||
return true;
|
||||
}
|
||||
else if (name == "y") {
|
||||
value = text.getPosition().y;
|
||||
value = position.y;
|
||||
return true;
|
||||
}
|
||||
else if (name == "font_size" || name == "size") { // Support both for backward compatibility
|
||||
|
|
|
@ -16,6 +16,7 @@ public:
|
|||
sf::FloatRect get_bounds() const override;
|
||||
void move(float dx, float dy) override;
|
||||
void resize(float w, float h) override;
|
||||
void onPositionChanged() override;
|
||||
|
||||
// Property system for animations
|
||||
bool setProperty(const std::string& name, float value) override;
|
||||
|
|
|
@ -349,9 +349,11 @@ int UIDrawable::set_float_member(PyObject* self, PyObject* value, void* closure)
|
|||
switch (member) {
|
||||
case 0: // x
|
||||
drawable->position.x = val;
|
||||
drawable->onPositionChanged();
|
||||
break;
|
||||
case 1: // y
|
||||
drawable->position.y = val;
|
||||
drawable->onPositionChanged();
|
||||
break;
|
||||
case 2: // w
|
||||
case 3: // h
|
||||
|
@ -474,5 +476,6 @@ int UIDrawable::set_pos(PyObject* self, PyObject* value, void* closure) {
|
|||
}
|
||||
|
||||
drawable->position = sf::Vector2f(x, y);
|
||||
drawable->onPositionChanged();
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -74,6 +74,9 @@ public:
|
|||
virtual void move(float dx, float dy) = 0; // #98 - move by offset
|
||||
virtual void resize(float w, float h) = 0; // #98 - resize to dimensions
|
||||
|
||||
// Called when position changes to allow derived classes to sync
|
||||
virtual void onPositionChanged() {}
|
||||
|
||||
// Animation support
|
||||
virtual bool setProperty(const std::string& name, float value) { return false; }
|
||||
virtual bool setProperty(const std::string& name, int value) { return false; }
|
||||
|
|
|
@ -85,6 +85,12 @@ void UIFrame::resize(float w, float h)
|
|||
box.setSize(sf::Vector2f(w, h));
|
||||
}
|
||||
|
||||
void UIFrame::onPositionChanged()
|
||||
{
|
||||
// Sync box position with base class position
|
||||
box.setPosition(position);
|
||||
}
|
||||
|
||||
void UIFrame::render(sf::Vector2f offset, sf::RenderTarget& target)
|
||||
{
|
||||
// Check visibility
|
||||
|
|
|
@ -39,6 +39,7 @@ public:
|
|||
sf::FloatRect get_bounds() const override;
|
||||
void move(float dx, float dy) override;
|
||||
void resize(float w, float h) override;
|
||||
void onPositionChanged() override;
|
||||
|
||||
static PyObject* get_children(PyUIFrameObject* self, void* closure);
|
||||
|
||||
|
|
Loading…
Reference in New Issue