diff --git a/base_position_uicaption_test.png b/base_position_uicaption_test.png new file mode 100644 index 0000000..2126533 Binary files /dev/null and b/base_position_uicaption_test.png differ diff --git a/src/UICaption.cpp b/src/UICaption.cpp index fc57d6e..b5e2ab7 100644 --- a/src/UICaption.cpp +++ b/src/UICaption.cpp @@ -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(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 diff --git a/src/UICaption.h b/src/UICaption.h index 7f00b22..e39d098 100644 --- a/src/UICaption.h +++ b/src/UICaption.h @@ -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; diff --git a/src/UIDrawable.cpp b/src/UIDrawable.cpp index 6921956..94b50ea 100644 --- a/src/UIDrawable.cpp +++ b/src/UIDrawable.cpp @@ -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; } diff --git a/src/UIDrawable.h b/src/UIDrawable.h index c3ba600..b18bf54 100644 --- a/src/UIDrawable.h +++ b/src/UIDrawable.h @@ -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; } diff --git a/src/UIFrame.cpp b/src/UIFrame.cpp index 7af139d..2ba73c2 100644 --- a/src/UIFrame.cpp +++ b/src/UIFrame.cpp @@ -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 diff --git a/src/UIFrame.h b/src/UIFrame.h index 2d4d23e..e1311d9 100644 --- a/src/UIFrame.h +++ b/src/UIFrame.h @@ -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);