Color parsing for UICaption __init__ - closes #58

This commit is contained in:
John McCardle 2024-04-20 13:37:19 -04:00
parent ac7f7052cd
commit a465a9861d
3 changed files with 45 additions and 8 deletions

View File

@ -1,4 +1,5 @@
#include "PyColor.h"
#include "McRFPy_API.h"
PyGetSetDef PyColor::getsetters[] = {
{"r", (getter)PyColor::get_member, (setter)PyColor::set_member, "Red component", (void*)0},
@ -134,3 +135,16 @@ int PyColor::set_member(PyObject* obj, PyObject* value, void* closure)
// TODO
return 0;
}
PyColorObject* PyColor::from_arg(PyObject* args)
{
auto type = (PyTypeObject*)PyObject_GetAttrString(McRFPy_API::mcrf_module, "Color");
if (PyObject_IsInstance(args, (PyObject*)type)) return (PyColorObject*)args;
auto obj = (PyColorObject*)type->tp_alloc(type, 0);
int err = init(obj, args, NULL);
if (err) {
Py_DECREF(obj);
return NULL;
}
return obj;
}

View File

@ -29,6 +29,7 @@ public:
static int set_member(PyObject*, PyObject*, void*);
static PyGetSetDef getsetters[];
static PyColorObject* from_arg(PyObject*);
};
namespace mcrfpydef {

View File

@ -212,7 +212,7 @@ PyObject* UICaption::repr(PyUICaptionObject* self)
"text='" << (std::string)text.getString() << "', " <<
"outline=" << text.getOutlineThickness() << ", " <<
"fill_color=(" << (int)fc.r << ", " << (int)fc.g << ", " << (int)fc.b << ", " << (int)fc.a <<"), " <<
"outlinecolor=(" << (int)oc.r << ", " << (int)oc.g << ", " << (int)oc.b << ", " << (int)oc.a <<"), " <<
"outline_color=(" << (int)oc.r << ", " << (int)oc.g << ", " << (int)oc.b << ", " << (int)oc.a <<"), " <<
")>";
}
std::string repr_str = ss.str();
@ -222,13 +222,13 @@ PyObject* UICaption::repr(PyUICaptionObject* self)
int UICaption::init(PyUICaptionObject* self, PyObject* args, PyObject* kwds)
{
using namespace mcrfpydef;
static const char* keywords[] = { "x", "y", "text", "font", "fill_color", "outline_color", nullptr };
float x = 0.0f, y = 0.0f;
static const char* keywords[] = { "x", "y", "text", "font", "fill_color", "outline_color", "outline", nullptr };
float x = 0.0f, y = 0.0f, outline = 0.0f;
char* text;
PyObject* font, fill_color, outline_color;
PyObject* font=NULL, *fill_color=NULL, *outline_color=NULL;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffzOOO",
const_cast<char**>(keywords), &x, &y, &text, &font, &fill_color, &outline_color))
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffzOOOf",
const_cast<char**>(keywords), &x, &y, &text, &font, &fill_color, &outline_color, &outline))
{
return -1;
}
@ -253,8 +253,30 @@ int UICaption::init(PyUICaptionObject* self, PyObject* args, PyObject* kwds)
self->data->text.setPosition(sf::Vector2f(x, y));
self->data->text.setString((std::string)text);
self->data->text.setFillColor(sf::Color(0,0,0,255));
self->data->text.setOutlineColor(sf::Color(128,128,128,255));
self->data->text.setOutlineThickness(outline);
if (fill_color) {
auto fc = PyColor::from_arg(fill_color);
if (!fc) {
PyErr_SetString(PyExc_TypeError, "fill_color must be mcrfpy.Color or arguments to mcrfpy.Color.__init__");
return -1;
}
self->data->text.setFillColor(PyColor::fromPy(fc));
//Py_DECREF(fc);
} else {
self->data->text.setFillColor(sf::Color(0,0,0,255));
}
if (outline_color) {
auto oc = PyColor::from_arg(outline_color);
if (!oc) {
PyErr_SetString(PyExc_TypeError, "outline_color must be mcrfpy.Color or arguments to mcrfpy.Color.__init__");
return -1;
}
self->data->text.setOutlineColor(PyColor::fromPy(oc));
//Py_DECREF(oc);
} else {
self->data->text.setOutlineColor(sf::Color(128,128,128,255));
}
return 0;
}