Successful use of a copy-modify-paste template of Python type (UIFrame -> PyUIFrameObject)
This commit is contained in:
parent
50d926fe37
commit
6fe7b842ef
|
@ -124,14 +124,14 @@ PyObject* PyInit_mcrfpy()
|
||||||
//std::cout << "Adding UIFrame object to module\n";
|
//std::cout << "Adding UIFrame object to module\n";
|
||||||
PyModule_AddType(m, &mcrfpydef::PyColorType);
|
PyModule_AddType(m, &mcrfpydef::PyColorType);
|
||||||
|
|
||||||
/*
|
|
||||||
if (PyModule_AddType(m, &mcrfpydef::PyUIFrameType) < 0)
|
if (PyModule_AddType(m, &mcrfpydef::PyUIFrameType) < 0)
|
||||||
{
|
{
|
||||||
std::cout << "Error adding UIFrame type to module; aborting" << std::endl;
|
std::cout << "Error adding UIFrame type to module; aborting" << std::endl;
|
||||||
Py_DECREF(&mcrfpydef::PyUIFrameType);
|
Py_DECREF(&mcrfpydef::PyUIFrameType);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
|
|
211
src/UI.h
211
src/UI.h
|
@ -86,13 +86,10 @@ struct CPythonSharedObject {
|
||||||
typedef CPythonSharedObject<UIFrame> PyUIFrameObject;
|
typedef CPythonSharedObject<UIFrame> PyUIFrameObject;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
//equivalent
|
|
||||||
/*
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
std::shared_ptr<UIFrame> data;
|
std::shared_ptr<UIFrame> data;
|
||||||
} PyUIFrameObject;
|
} PyUIFrameObject;
|
||||||
*/
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
PyObject_HEAD
|
PyObject_HEAD
|
||||||
|
@ -215,117 +212,62 @@ namespace mcrfpydef {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
static PyTypeObject PyUIFrameType = {
|
*
|
||||||
.tp_name = "mcrfpy.Frame",
|
* Begin template generation for PyUIFrameType
|
||||||
.tp_basicsize = sizeof(PyUIFrameObject),
|
*
|
||||||
.tp_itemsize = 0,
|
|
||||||
.tp_dealloc = (destructor)[](PyObject* self) {
|
|
||||||
PyUIFrameObject* obj = (PyUIFrameObject*)self;
|
|
||||||
obj->data.reset();
|
|
||||||
Py_TYPE(self)->tp_free(self);
|
|
||||||
},
|
|
||||||
//.tp_init = (initproc)PyUIFrame_init, // needs implementation
|
|
||||||
//.tp_new = PyUIFrame_new, // needs implementation
|
|
||||||
};
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
static PyObject* PyUIFrame_get_member(PyUIFrameObject* self, void* closure)
|
||||||
|
{
|
||||||
|
auto member_ptr = reinterpret_cast<long>(closure);
|
||||||
|
if (member_ptr == 0)
|
||||||
|
return PyLong_FromLong(self->data->box.getPosition().x);
|
||||||
|
else if (member_ptr == 1)
|
||||||
|
return PyLong_FromLong(self->data->box.getPosition().y);
|
||||||
|
else
|
||||||
|
{
|
||||||
|
PyErr_SetString(PyExc_AttributeError, "Invalid attribute");
|
||||||
|
return nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int PyUIFrame_set_member(PyUIFrameObject* self, PyObject* value, void* closure)
|
||||||
|
{
|
||||||
|
if (PyLong_Check(value))
|
||||||
|
{
|
||||||
/*
|
/*
|
||||||
static PyTypeObject PyUIFrameType = {
|
long int_val = PyLong_AsLong(value);
|
||||||
//PyVarObject_HEAD_INIT(NULL, 0)
|
if (int_val < 0)
|
||||||
.tp_name = "mcrfpy.Frame",
|
int_val = 0;
|
||||||
.tp_basicsize = sizeof(PyColorObject),
|
else if (int_val > 255)
|
||||||
.tp_itemsize = 0,
|
int_val = 255;
|
||||||
//.tp_dealloc = [](PyObject* obj) { Py_TYPE(obj)->tp_free(obj); },
|
auto member_ptr = reinterpret_cast<long>(closure);
|
||||||
//.tp_repr = (reprfunc)PyUIFrame_repr,
|
if (member_ptr == 0)
|
||||||
//.tp_hash = NULL,
|
self->data->r = static_cast<sf::Uint8>(int_val);
|
||||||
//.tp_iter
|
else if (member_ptr == 1)
|
||||||
//.tp_iternext
|
self->data->g = static_cast<sf::Uint8>(int_val);
|
||||||
.tp_flags = Py_TPFLAGS_DEFAULT,
|
else if (member_ptr == 2)
|
||||||
.tp_doc = PyDoc_STR("SFML Color object (RGBA)"),
|
self->data->b = static_cast<sf::Uint8>(int_val);
|
||||||
//.tp_methods = PyUIFrame_methods,
|
else if (member_ptr == 3)
|
||||||
//.tp_members = PyColor_members,
|
self->data->a = static_cast<sf::Uint8>(int_val);
|
||||||
.tp_getset = PyColor_getsetters,
|
|
||||||
//.tp_base = NULL,
|
|
||||||
//.tp_init = (initproc)PyUIFrame_init,
|
|
||||||
.tp_new = PyColor_new, //PyType_GenericNew ?
|
|
||||||
};
|
|
||||||
|
|
||||||
static PyTypeObject PyCaptionType = {
|
|
||||||
//PyVarObject_HEAD_INIT(NULL, 0)
|
|
||||||
.tp_name = "mcrfpy.Caption",
|
|
||||||
.tp_basicsize = sizeof(PyColorObject),
|
|
||||||
.tp_itemsize = 0,
|
|
||||||
//.tp_dealloc = [](PyObject* obj) { Py_TYPE(obj)->tp_free(obj); },
|
|
||||||
//.tp_repr = (reprfunc)PyUIFrame_repr,
|
|
||||||
//.tp_hash = NULL,
|
|
||||||
//.tp_iter
|
|
||||||
//.tp_iternext
|
|
||||||
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
||||||
.tp_doc = PyDoc_STR("SFML Color object (RGBA)"),
|
|
||||||
//.tp_methods = PyUIFrame_methods,
|
|
||||||
//.tp_members = PyColor_members,
|
|
||||||
.tp_getset = PyColor_getsetters,
|
|
||||||
//.tp_base = NULL,
|
|
||||||
//.tp_init = (initproc)PyUIFrame_init,
|
|
||||||
.tp_new = PyColor_new, //PyType_GenericNew ?
|
|
||||||
};
|
|
||||||
|
|
||||||
static PyTypeObject PySpriteType = {
|
|
||||||
//PyVarObject_HEAD_INIT(NULL, 0)
|
|
||||||
.tp_name = "mcrfpy.Sprite",
|
|
||||||
.tp_basicsize = sizeof(PyColorObject),
|
|
||||||
.tp_itemsize = 0,
|
|
||||||
//.tp_dealloc = [](PyObject* obj) { Py_TYPE(obj)->tp_free(obj); },
|
|
||||||
//.tp_repr = (reprfunc)PyUIFrame_repr,
|
|
||||||
//.tp_hash = NULL,
|
|
||||||
//.tp_iter
|
|
||||||
//.tp_iternext
|
|
||||||
.tp_flags = Py_TPFLAGS_DEFAULT,
|
|
||||||
.tp_doc = PyDoc_STR("SFML Color object (RGBA)"),
|
|
||||||
//.tp_methods = PyUIFrame_methods,
|
|
||||||
//.tp_members = PyColor_members,
|
|
||||||
.tp_getset = PyColor_getsetters,
|
|
||||||
//.tp_base = NULL,
|
|
||||||
//.tp_init = (initproc)PyUIFrame_init,
|
|
||||||
.tp_new = PyColor_new, //PyType_GenericNew ?
|
|
||||||
};
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// UIFrame Definitions
|
|
||||||
// new, init, repr, set_size, methods, members, PyTypeObject
|
|
||||||
/*
|
|
||||||
static PyObject* PyUIFrame_new(PyTypeObject* type, PyObject* args, PyObject* kwds)
|
|
||||||
{
|
|
||||||
std::cout << "New called\n";
|
|
||||||
UIFrame* self;
|
|
||||||
self = (UIFrame*)type->tp_alloc(type, 0);
|
|
||||||
if (self != nullptr)
|
|
||||||
{
|
|
||||||
}
|
}
|
||||||
return (PyObject*)self;
|
else
|
||||||
}
|
|
||||||
|
|
||||||
static int PyUIFrame_init(UIFrame* self, PyObject* args, PyObject* kwds)
|
|
||||||
{
|
|
||||||
std::cout << "Init called\n";
|
|
||||||
static const char* keywords[] = { "x", "y", nullptr }; // TODO: keywords and other python objects to configure box (sf::RectangleShape)
|
|
||||||
float x = 0.0f, y = 0.0f;
|
|
||||||
|
|
||||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ff", const_cast<char**>(keywords), &x, &y))
|
|
||||||
{
|
{
|
||||||
|
PyErr_SetString(PyExc_TypeError, "Value must be an integer.");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
//self->x = x;
|
|
||||||
//self->y = y;
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject* PyUIFrame_repr(UIFrame* self)
|
static PyGetSetDef PyUIFrame_getsetters[] = {
|
||||||
|
{"x", (getter)PyUIFrame_get_member, (setter)PyUIFrame_set_member, "member desc", (void*)0},
|
||||||
|
{"y", (getter)PyUIFrame_get_member, (setter)PyUIFrame_set_member, "member desc", (void*)1},
|
||||||
|
{NULL}
|
||||||
|
};
|
||||||
|
|
||||||
|
static PyObject* PyUIFrame_repr(PyUIFrameObject* self)
|
||||||
{
|
{
|
||||||
std::ostringstream ss;
|
std::ostringstream ss;
|
||||||
ss << "<UIFrame ()>";
|
ss << "<UIFrame ()>";
|
||||||
|
@ -334,49 +276,58 @@ namespace mcrfpydef {
|
||||||
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
|
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyObject* PyUIFrame_setSize(UIFrame* self, PyObject* args)
|
static int PyUIFrame_init(PyUIFrameObject* self, PyObject* args, PyObject* kwds)
|
||||||
{
|
{
|
||||||
//float w, h;
|
//std::cout << "Init called\n";
|
||||||
if (!PyArg_ParseTuple(args, "ff", &self->w, &self->h)) return (PyObject*)-1;
|
static const char* keywords[] = { "x", "y", nullptr };
|
||||||
//self->box.setSize(sf::Vector2f(w, h));
|
float x = 0.0f, y = 0.0f;
|
||||||
Py_INCREF(Py_None);
|
|
||||||
//return PyFloat_FromDouble(mag);
|
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ff", const_cast<char**>(keywords), &x, &y))
|
||||||
return Py_None;
|
{
|
||||||
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static PyMethodDef PyUIFrame_methods[] = {
|
//self->data->x = x;
|
||||||
{"set_size", (PyCFunction)PyUIFrame_setSize, METH_VARARGS,
|
//self->data->y = y;
|
||||||
"Set the width and height of the UIFrame's visible box"},
|
|
||||||
{NULL, NULL, 0, NULL}
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
return 0;
|
||||||
static PyMemberDef PyUIFrame_members[] = {
|
}
|
||||||
{"box", T_OBJECT, offsetof(UIFrame, box), 0},
|
|
||||||
{NULL}
|
|
||||||
};
|
|
||||||
*/
|
|
||||||
|
|
||||||
/*
|
|
||||||
static PyTypeObject PyUIFrameType = {
|
static PyTypeObject PyUIFrameType = {
|
||||||
//PyVarObject_HEAD_INIT(NULL, 0)
|
//PyVarObject_HEAD_INIT(NULL, 0)
|
||||||
.tp_name = "mcrfpy.UIFrame",
|
.tp_name = "mcrfpy.Frame",
|
||||||
.tp_basicsize = sizeof(UIFrame),
|
.tp_basicsize = sizeof(PyUIFrameObject),
|
||||||
.tp_itemsize = 0,
|
.tp_itemsize = 0,
|
||||||
.tp_dealloc = [](PyObject* obj) { Py_TYPE(obj)->tp_free(obj); },
|
.tp_dealloc = (destructor)[](PyObject* self)
|
||||||
//.tp_repr = (reprfunc)PyUIFrame_repr,
|
{
|
||||||
|
PyUIFrameObject* obj = (PyUIFrameObject*)self;
|
||||||
|
obj->data.reset();
|
||||||
|
Py_TYPE(self)->tp_free(self);
|
||||||
|
},
|
||||||
|
.tp_repr = (reprfunc)PyUIFrame_repr,
|
||||||
//.tp_hash = NULL,
|
//.tp_hash = NULL,
|
||||||
//.tp_iter
|
//.tp_iter
|
||||||
//.tp_iternext
|
//.tp_iternext
|
||||||
.tp_flags = Py_TPFLAGS_DEFAULT,
|
.tp_flags = Py_TPFLAGS_DEFAULT,
|
||||||
.tp_doc = PyDoc_STR("Custom UIFrame object"),
|
.tp_doc = PyDoc_STR("docstring"),
|
||||||
.tp_methods = PyUIFrame_methods,
|
//.tp_methods = PyUIFrame_methods,
|
||||||
//.tp_members = PyUIFrame_members,
|
//.tp_members = PyUIFrame_members,
|
||||||
|
.tp_getset = PyUIFrame_getsetters,
|
||||||
//.tp_base = NULL,
|
//.tp_base = NULL,
|
||||||
.tp_init = (initproc)PyUIFrame_init,
|
.tp_init = (initproc)PyUIFrame_init,
|
||||||
.tp_new = PyUIFrame_new, //PyType_GenericNew ?
|
.tp_new = [](PyTypeObject* type, PyObject* args, PyObject* kwds) -> PyObject*
|
||||||
|
{
|
||||||
|
PyUIFrameObject* self = (PyUIFrameObject*)type->tp_alloc(type, 0);
|
||||||
|
if (self) self->data = std::make_shared<UIFrame>();
|
||||||
|
return (PyObject*)self;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* End auto-generated PyUIFrameType generation
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
}
|
|
||||||
|
} // namespace mcrfpydef
|
||||||
|
|
Loading…
Reference in New Issue