Add position tuple support and pos property to UI elements
closes #83, closes #84 - Issue #83: Add position tuple support to constructors - Frame and Sprite now accept both (x, y) and ((x, y)) forms - Also accept Vector objects as position arguments - Caption and Entity already supported tuple/Vector forms - Uses PyVector::from_arg for flexible position parsing - Issue #84: Add pos property to Frame and Sprite - Added pos getter that returns a Vector - Added pos setter that accepts Vector or tuple - Provides consistency with Caption and Entity which already had pos properties - All UI elements now have a uniform way to get/set positions as Vectors Both features improve API consistency and make it easier to work with positions.
This commit is contained in:
parent
2f2b488fb5
commit
99f301e3a0
6 changed files with 593 additions and 2 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#include "UIFrame.h"
|
||||
#include "UICollection.h"
|
||||
#include "GameEngine.h"
|
||||
#include "PyVector.h"
|
||||
|
||||
UIDrawable* UIFrame::click_at(sf::Vector2f point)
|
||||
{
|
||||
|
|
@ -214,6 +215,28 @@ int UIFrame::set_color_member(PyUIFrameObject* self, PyObject* value, void* clos
|
|||
return 0;
|
||||
}
|
||||
|
||||
PyObject* UIFrame::get_pos(PyUIFrameObject* self, void* closure)
|
||||
{
|
||||
auto type = (PyTypeObject*)PyObject_GetAttrString(McRFPy_API::mcrf_module, "Vector");
|
||||
auto obj = (PyVectorObject*)type->tp_alloc(type, 0);
|
||||
if (obj) {
|
||||
auto pos = self->data->box.getPosition();
|
||||
obj->data = sf::Vector2f(pos.x, pos.y);
|
||||
}
|
||||
return (PyObject*)obj;
|
||||
}
|
||||
|
||||
int UIFrame::set_pos(PyUIFrameObject* self, PyObject* value, void* closure)
|
||||
{
|
||||
PyVectorObject* vec = PyVector::from_arg(value);
|
||||
if (!vec) {
|
||||
PyErr_SetString(PyExc_TypeError, "pos must be a Vector or convertible to Vector");
|
||||
return -1;
|
||||
}
|
||||
self->data->box.setPosition(vec->data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyGetSetDef UIFrame::getsetters[] = {
|
||||
{"x", (getter)UIFrame::get_float_member, (setter)UIFrame::set_float_member, "X coordinate of top-left corner", (void*)0},
|
||||
{"y", (getter)UIFrame::get_float_member, (setter)UIFrame::set_float_member, "Y coordinate of top-left corner", (void*)1},
|
||||
|
|
@ -225,6 +248,7 @@ PyGetSetDef UIFrame::getsetters[] = {
|
|||
{"children", (getter)UIFrame::get_children, NULL, "UICollection of objects on top of this one", NULL},
|
||||
{"click", (getter)UIDrawable::get_click, (setter)UIDrawable::set_click, "Object called with (x, y, button) when clicked", (void*)PyObjectsEnum::UIFRAME},
|
||||
{"z_index", (getter)UIDrawable::get_int, (setter)UIDrawable::set_int, "Z-order for rendering (lower values rendered first)", (void*)PyObjectsEnum::UIFRAME},
|
||||
{"pos", (getter)UIFrame::get_pos, (setter)UIFrame::set_pos, "Position as a Vector", NULL},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
|
@ -256,9 +280,29 @@ int UIFrame::init(PyUIFrameObject* self, PyObject* args, PyObject* kwds)
|
|||
PyObject* fill_color = 0;
|
||||
PyObject* outline_color = 0;
|
||||
|
||||
// First try to parse as (x, y, w, h, ...)
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "ffff|OOf", const_cast<char**>(keywords), &x, &y, &w, &h, &fill_color, &outline_color, &outline))
|
||||
{
|
||||
return -1;
|
||||
PyErr_Clear(); // Clear the error
|
||||
|
||||
// Try to parse as ((x,y), w, h, ...) or (Vector, w, h, ...)
|
||||
PyObject* pos_obj = nullptr;
|
||||
const char* alt_keywords[] = { "pos", "w", "h", "fill_color", "outline_color", "outline", nullptr };
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "Off|OOf", const_cast<char**>(alt_keywords),
|
||||
&pos_obj, &w, &h, &fill_color, &outline_color, &outline))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Convert position argument to x, y
|
||||
PyVectorObject* vec = PyVector::from_arg(pos_obj);
|
||||
if (!vec) {
|
||||
PyErr_SetString(PyExc_TypeError, "First argument must be a tuple (x, y) or Vector when not providing x, y separately");
|
||||
return -1;
|
||||
}
|
||||
x = vec->data.x;
|
||||
y = vec->data.y;
|
||||
}
|
||||
|
||||
self->data->box.setPosition(sf::Vector2f(x, y));
|
||||
|
|
|
|||
|
|
@ -40,6 +40,8 @@ public:
|
|||
static int set_float_member(PyUIFrameObject* self, PyObject* value, void* closure);
|
||||
static PyObject* get_color_member(PyUIFrameObject* self, void* closure);
|
||||
static int set_color_member(PyUIFrameObject* self, PyObject* value, void* closure);
|
||||
static PyObject* get_pos(PyUIFrameObject* self, void* closure);
|
||||
static int set_pos(PyUIFrameObject* self, PyObject* value, void* closure);
|
||||
static PyGetSetDef getsetters[];
|
||||
static PyObject* repr(PyUIFrameObject* self);
|
||||
static int init(PyUIFrameObject* self, PyObject* args, PyObject* kwds);
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
#include "UISprite.h"
|
||||
#include "GameEngine.h"
|
||||
#include "PyVector.h"
|
||||
|
||||
UIDrawable* UISprite::click_at(sf::Vector2f point)
|
||||
{
|
||||
|
|
@ -203,6 +204,28 @@ int UISprite::set_texture(PyUISpriteObject* self, PyObject* value, void* closure
|
|||
return 0;
|
||||
}
|
||||
|
||||
PyObject* UISprite::get_pos(PyUISpriteObject* self, void* closure)
|
||||
{
|
||||
auto type = (PyTypeObject*)PyObject_GetAttrString(McRFPy_API::mcrf_module, "Vector");
|
||||
auto obj = (PyVectorObject*)type->tp_alloc(type, 0);
|
||||
if (obj) {
|
||||
auto pos = self->data->getPosition();
|
||||
obj->data = sf::Vector2f(pos.x, pos.y);
|
||||
}
|
||||
return (PyObject*)obj;
|
||||
}
|
||||
|
||||
int UISprite::set_pos(PyUISpriteObject* self, PyObject* value, void* closure)
|
||||
{
|
||||
PyVectorObject* vec = PyVector::from_arg(value);
|
||||
if (!vec) {
|
||||
PyErr_SetString(PyExc_TypeError, "pos must be a Vector or convertible to Vector");
|
||||
return -1;
|
||||
}
|
||||
self->data->setPosition(vec->data);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyGetSetDef UISprite::getsetters[] = {
|
||||
{"x", (getter)UISprite::get_float_member, (setter)UISprite::set_float_member, "X coordinate of top-left corner", (void*)0},
|
||||
{"y", (getter)UISprite::get_float_member, (setter)UISprite::set_float_member, "Y coordinate of top-left corner", (void*)1},
|
||||
|
|
@ -214,6 +237,7 @@ PyGetSetDef UISprite::getsetters[] = {
|
|||
{"texture", (getter)UISprite::get_texture, (setter)UISprite::set_texture, "Texture object", NULL},
|
||||
{"click", (getter)UIDrawable::get_click, (setter)UIDrawable::set_click, "Object called with (x, y, button) when clicked", (void*)PyObjectsEnum::UISPRITE},
|
||||
{"z_index", (getter)UIDrawable::get_int, (setter)UIDrawable::set_int, "Z-order for rendering (lower values rendered first)", (void*)PyObjectsEnum::UISPRITE},
|
||||
{"pos", (getter)UISprite::get_pos, (setter)UISprite::set_pos, "Position as a Vector", NULL},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
|
@ -239,10 +263,32 @@ int UISprite::init(PyUISpriteObject* self, PyObject* args, PyObject* kwds)
|
|||
int sprite_index = 0;
|
||||
PyObject* texture = NULL;
|
||||
|
||||
// First try to parse as (x, y, texture, ...)
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffOif",
|
||||
const_cast<char**>(keywords), &x, &y, &texture, &sprite_index, &scale))
|
||||
{
|
||||
return -1;
|
||||
PyErr_Clear(); // Clear the error
|
||||
|
||||
// Try to parse as ((x,y), texture, ...) or (Vector, texture, ...)
|
||||
PyObject* pos_obj = nullptr;
|
||||
const char* alt_keywords[] = { "pos", "texture", "sprite_index", "scale", nullptr };
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|OOif", const_cast<char**>(alt_keywords),
|
||||
&pos_obj, &texture, &sprite_index, &scale))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Convert position argument to x, y
|
||||
if (pos_obj) {
|
||||
PyVectorObject* vec = PyVector::from_arg(pos_obj);
|
||||
if (!vec) {
|
||||
PyErr_SetString(PyExc_TypeError, "First argument must be a tuple (x, y) or Vector when not providing x, y separately");
|
||||
return -1;
|
||||
}
|
||||
x = vec->data.x;
|
||||
y = vec->data.y;
|
||||
}
|
||||
}
|
||||
|
||||
// Handle texture - allow None or use default
|
||||
|
|
|
|||
|
|
@ -55,6 +55,8 @@ public:
|
|||
static int set_int_member(PyUISpriteObject* self, PyObject* value, void* closure);
|
||||
static PyObject* get_texture(PyUISpriteObject* self, void* closure);
|
||||
static int set_texture(PyUISpriteObject* self, PyObject* value, void* closure);
|
||||
static PyObject* get_pos(PyUISpriteObject* self, void* closure);
|
||||
static int set_pos(PyUISpriteObject* self, PyObject* value, void* closure);
|
||||
static PyGetSetDef getsetters[];
|
||||
static PyObject* repr(PyUISpriteObject* self);
|
||||
static int init(PyUISpriteObject* self, PyObject* args, PyObject* kwds);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue