From 1bbb0aa5b80042387f5d80e77fc6da166cb79b9d Mon Sep 17 00:00:00 2001 From: John McCardle Date: Thu, 7 Sep 2023 23:10:21 -0400 Subject: [PATCH] Caption object seems to be instantiable with a Font object now. Can't test actual rendering without a way to add objects to a collection. --- src/UI.h | 35 +++++++++++++++++++++++++++++++---- 1 file changed, 31 insertions(+), 4 deletions(-) diff --git a/src/UI.h b/src/UI.h index 35a0de2..f32628c 100644 --- a/src/UI.h +++ b/src/UI.h @@ -107,6 +107,7 @@ typedef struct { typedef struct { PyObject_HEAD std::shared_ptr data; + PyObject* font; } PyUICaptionObject; typedef struct { @@ -517,17 +518,41 @@ switch (target->derived_type()) \ static int PyUICaption_init(PyUICaptionObject* self, PyObject* args, PyObject* kwds) { //std::cout << "Init called\n"; - static const char* keywords[] = { "x", "y", "text", nullptr }; + static const char* keywords[] = { "x", "y", "text", "font", "fill_color", "outline_color", nullptr }; float x = 0.0f, y = 0.0f; char* text; + PyObject* font, fill_color, outline_color; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffz", const_cast(keywords), &x, &y, &text)) + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffzOOO", + const_cast(keywords), &x, &y, &text, &font, &fill_color, &outline_color)) { return -1; } - //self->data->x = x; - //self->data->y = y; + // check types for font, fill_color, outline_color + // + // Set Font + // + if (font != NULL && !PyObject_IsInstance(font, (PyObject*)&PyFontType)){ + PyErr_SetString(PyExc_TypeError, "font must be a mcrfpy.Font instance"); + return -1; + } else if (font != NULL) + { + auto font_obj = (PyFontObject*)font; + self->data->text.setFont(*font_obj->data); + self->font = font; + Py_INCREF(font); + } else + { + // default font + //self->data->text.setFont(Resources::game->getFont()); + } + + // + // Set Color + // + 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)); @@ -542,6 +567,8 @@ switch (target->derived_type()) \ .tp_dealloc = (destructor)[](PyObject* self) { PyUICaptionObject* obj = (PyUICaptionObject*)self; + // release reference to font object + if (obj->font) Py_DECREF(obj->font); obj->data.reset(); Py_TYPE(self)->tp_free(self); },