feat: Phase 1 - safe constructors and _Drawable foundation

Closes #7 - Make all UI class constructors safe:
- Added safe default constructors for UISprite, UIGrid, UIEntity, UICaption
- Initialize all members to predictable values
- Made Python init functions accept no arguments
- Added x,y properties to UIEntity

Closes #71 - Create _Drawable Python base class:
- Created PyDrawable.h/cpp with base type (not yet inherited by UI types)
- Registered in module initialization

Closes #87 - Add visible property:
- Added bool visible=true to UIDrawable base class
- All render methods check visibility before drawing

Closes #88 - Add opacity property:
- Added float opacity=1.0 to UIDrawable base class
- UICaption and UISprite apply opacity to alpha channel

Closes #89 - Add get_bounds() method:
- Virtual method returns sf::FloatRect(x,y,w,h)
- Implemented in Frame, Caption, Sprite, Grid

Closes #98 - Add move() and resize() methods:
- move(dx,dy) for relative movement
- resize(w,h) for absolute sizing
- Caption resize is no-op (size controlled by font)

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
John McCardle 2025-07-06 00:13:39 -04:00
commit f1b354e47d
15 changed files with 531 additions and 161 deletions

View file

@ -11,7 +11,13 @@ UIDrawable* UISprite::click_at(sf::Vector2f point)
return NULL;
}
UISprite::UISprite() {}
UISprite::UISprite()
: sprite_index(0), ptex(nullptr)
{
// Initialize sprite to safe defaults
sprite.setPosition(0.0f, 0.0f);
sprite.setScale(1.0f, 1.0f);
}
UISprite::UISprite(std::shared_ptr<PyTexture> _ptex, int _sprite_index, sf::Vector2f _pos, float _scale)
: ptex(_ptex), sprite_index(_sprite_index)
@ -30,9 +36,21 @@ void UISprite::render(sf::Vector2f offset)
void UISprite::render(sf::Vector2f offset, sf::RenderTarget& target)
{
// Check visibility
if (!visible) return;
// Apply opacity
auto color = sprite.getColor();
color.a = static_cast<sf::Uint8>(255 * opacity);
sprite.setColor(color);
sprite.move(offset);
target.draw(sprite);
sprite.move(-offset);
// Restore original alpha
color.a = 255;
sprite.setColor(color);
}
void UISprite::setPosition(sf::Vector2f pos)
@ -84,6 +102,28 @@ PyObjectsEnum UISprite::derived_type()
return PyObjectsEnum::UISPRITE;
}
// Phase 1 implementations
sf::FloatRect UISprite::get_bounds() const
{
return sprite.getGlobalBounds();
}
void UISprite::move(float dx, float dy)
{
sprite.move(dx, dy);
}
void UISprite::resize(float w, float h)
{
// Calculate scale factors to achieve target size
auto bounds = sprite.getLocalBounds();
if (bounds.width > 0 && bounds.height > 0) {
float scaleX = w / bounds.width;
float scaleY = h / bounds.height;
sprite.setScale(scaleX, scaleY);
}
}
PyObject* UISprite::get_float_member(PyUISpriteObject* self, void* closure)
{
auto member_ptr = reinterpret_cast<long>(closure);