Squashed commit of the following: [break_up_ui_h]
Closes #43 No segfault found in cos_play after completing the checklist. Maybe I accidentally fixed it...? commit6aa151aba3Author: John McCardle <mccardle.john@gmail.com> Date: Fri Apr 19 21:43:58 2024 -0400 UISprite.h/.cpp cleanup commitec0374ef50Author: John McCardle <mccardle.john@gmail.com> Date: Fri Apr 19 21:37:39 2024 -0400 UIGridPoint.h/.cpp reorganization commit2cb7339535Author: John McCardle <mccardle.john@gmail.com> Date: Fri Apr 19 21:19:25 2024 -0400 UIGrid.h/.cpp cleanup. I have reservations about the UIEntityCollection[Iter] classes + methods living there, but not enough to fix it right now. commit5d6af324bfAuthor: John McCardle <mccardle.john@gmail.com> Date: Thu Apr 18 22:14:57 2024 -0400 UIFrame - moving static method into class namespace; no type object access commit567218cd7bAuthor: John McCardle <mccardle.john@gmail.com> Date: Thu Apr 18 21:23:49 2024 -0400 UIEntity fixes for the UI.h split: There are segfaults in cos_play, I may have missed a type usage or something commit76693acd28Author: John McCardle <mccardle.john@gmail.com> Date: Sat Apr 13 00:18:37 2024 -0400 delete leftover comments commit9efe998a33Author: John McCardle <mccardle.john@gmail.com> Date: Sat Apr 13 00:17:43 2024 -0400 some work on UICaption and UICollection; fixing segfaults resulting from mcrfpydef namepace TypeObject usage commit714965da45Author: John McCardle <mccardle.john@gmail.com> Date: Fri Apr 12 14:15:00 2024 -0400 eliminate extra includes on UICaption commit8efa25878fAuthor: John McCardle <mccardle.john@gmail.com> Date: Wed Apr 10 23:41:14 2024 -0400 remove a lot of stuff commitc186d8c7f3Author: John McCardle <mccardle.john@gmail.com> Date: Wed Apr 10 23:10:15 2024 -0400 We are compiling again! Started refactoring UICaption to be more idiomatic commit1b6e2a709bAuthor: John McCardle <mccardle.john@gmail.com> Date: Tue Apr 9 22:42:02 2024 -0400 Still not quite compiling; as predicted, a lot of interdependency and definition order bugs to untangle commitaa7553a818Author: John McCardle <mccardle.john@gmail.com> Date: Tue Apr 9 22:41:20 2024 -0400 PyTexture clean up scribbles and experiments commitc0201d989aAuthor: John McCardle <mccardle.john@gmail.com> Date: Mon Apr 8 22:55:00 2024 -0400 additional unsaved changes commit83a63a3093Author: John McCardle <mccardle.john@gmail.com> Date: Mon Apr 8 22:45:00 2024 -0400 doesn't compile, but UI.h/.cpp code has been divvy'd up. refs #43 @2h
This commit is contained in:
parent
1a7186f745
commit
ac7f7052cd
20 changed files with 2814 additions and 3052 deletions
215
src/UISprite.cpp
Normal file
215
src/UISprite.cpp
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
#include "UISprite.h"
|
||||
#include "GameEngine.h"
|
||||
|
||||
UIDrawable* UISprite::click_at(sf::Vector2f point)
|
||||
{
|
||||
if (click_callable)
|
||||
{
|
||||
if(sprite.getGlobalBounds().contains(point)) return this;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
UISprite::UISprite() {}
|
||||
|
||||
UISprite::UISprite(std::shared_ptr<PyTexture> _ptex, int _sprite_index, sf::Vector2f _pos, float _scale)
|
||||
: ptex(_ptex), sprite_index(_sprite_index)
|
||||
{
|
||||
sprite = ptex->sprite(sprite_index, _pos, sf::Vector2f(_scale, _scale));
|
||||
}
|
||||
|
||||
void UISprite::render(sf::Vector2f offset)
|
||||
{
|
||||
sprite.move(offset);
|
||||
Resources::game->getWindow().draw(sprite);
|
||||
sprite.move(-offset);
|
||||
}
|
||||
|
||||
void UISprite::render(sf::Vector2f offset, sf::RenderTexture& target)
|
||||
{
|
||||
sprite.move(offset);
|
||||
target.draw(sprite);
|
||||
sprite.move(-offset);
|
||||
}
|
||||
|
||||
void UISprite::setPosition(sf::Vector2f pos)
|
||||
{
|
||||
sprite.setPosition(pos);
|
||||
}
|
||||
|
||||
void UISprite::setScale(sf::Vector2f s)
|
||||
{
|
||||
sprite.setScale(s);
|
||||
}
|
||||
|
||||
void UISprite::setTexture(std::shared_ptr<PyTexture> _ptex, int _sprite_index)
|
||||
{
|
||||
ptex = _ptex;
|
||||
if (_sprite_index != -1) // if you are changing textures, there's a good chance you need a new index too
|
||||
sprite_index = _sprite_index;
|
||||
sprite = ptex->sprite(sprite_index, sprite.getPosition(), sprite.getScale());
|
||||
}
|
||||
|
||||
void UISprite::setSpriteIndex(int _sprite_index)
|
||||
{
|
||||
sprite_index = _sprite_index;
|
||||
sprite = ptex->sprite(sprite_index, sprite.getPosition(), sprite.getScale());
|
||||
}
|
||||
|
||||
sf::Vector2f UISprite::getScale()
|
||||
{
|
||||
return sprite.getScale();
|
||||
}
|
||||
|
||||
sf::Vector2f UISprite::getPosition()
|
||||
{
|
||||
return sprite.getPosition();
|
||||
}
|
||||
|
||||
std::shared_ptr<PyTexture> UISprite::getTexture()
|
||||
{
|
||||
return ptex;
|
||||
}
|
||||
|
||||
int UISprite::getSpriteIndex()
|
||||
{
|
||||
return sprite_index;
|
||||
}
|
||||
|
||||
PyObjectsEnum UISprite::derived_type()
|
||||
{
|
||||
return PyObjectsEnum::UISPRITE;
|
||||
}
|
||||
|
||||
PyObject* UISprite::get_float_member(PyUISpriteObject* self, void* closure)
|
||||
{
|
||||
auto member_ptr = reinterpret_cast<long>(closure);
|
||||
if (member_ptr == 0)
|
||||
return PyFloat_FromDouble(self->data->getPosition().x);
|
||||
else if (member_ptr == 1)
|
||||
return PyFloat_FromDouble(self->data->getPosition().y);
|
||||
else if (member_ptr == 2)
|
||||
return PyFloat_FromDouble(self->data->getScale().x); // scale X and Y are identical, presently
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_AttributeError, "Invalid attribute");
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
int UISprite::set_float_member(PyUISpriteObject* self, PyObject* value, void* closure)
|
||||
{
|
||||
float val;
|
||||
auto member_ptr = reinterpret_cast<long>(closure);
|
||||
if (PyFloat_Check(value))
|
||||
{
|
||||
val = PyFloat_AsDouble(value);
|
||||
}
|
||||
else if (PyLong_Check(value))
|
||||
{
|
||||
val = PyLong_AsLong(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Value must be a floating point number.");
|
||||
return -1;
|
||||
}
|
||||
if (member_ptr == 0) //x
|
||||
self->data->setPosition(sf::Vector2f(val, self->data->getPosition().y));
|
||||
else if (member_ptr == 1) //y
|
||||
self->data->setPosition(sf::Vector2f(self->data->getPosition().x, val));
|
||||
else if (member_ptr == 2) // scale
|
||||
self->data->setScale(sf::Vector2f(val, val));
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* UISprite::get_int_member(PyUISpriteObject* self, void* closure)
|
||||
{
|
||||
auto member_ptr = reinterpret_cast<long>(closure);
|
||||
if (true) {}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_AttributeError, "Invalid attribute");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return PyLong_FromDouble(self->data->getSpriteIndex());
|
||||
}
|
||||
|
||||
int UISprite::set_int_member(PyUISpriteObject* self, PyObject* value, void* closure)
|
||||
{
|
||||
int val;
|
||||
auto member_ptr = reinterpret_cast<long>(closure);
|
||||
if (PyLong_Check(value))
|
||||
{
|
||||
val = PyLong_AsLong(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
PyErr_SetString(PyExc_TypeError, "Value must be an integer.");
|
||||
return -1;
|
||||
}
|
||||
self->data->setSpriteIndex(val);
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyObject* UISprite::get_texture(PyUISpriteObject* self, void* closure)
|
||||
{
|
||||
return self->data->getTexture()->pyObject();
|
||||
}
|
||||
|
||||
int UISprite::set_texture(PyUISpriteObject* self, PyObject* value, void* closure)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
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},
|
||||
{"scale", (getter)UISprite::get_float_member, (setter)UISprite::set_float_member, "Size factor", (void*)2},
|
||||
{"sprite_number", (getter)UISprite::get_int_member, (setter)UISprite::set_int_member, "Which sprite on the texture is shown", NULL},
|
||||
{"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},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
PyObject* UISprite::repr(PyUISpriteObject* self)
|
||||
{
|
||||
std::ostringstream ss;
|
||||
if (!self->data) ss << "<Sprite (invalid internal object)>";
|
||||
else {
|
||||
//auto sprite = self->data->sprite;
|
||||
ss << "<Sprite (x=" << self->data->getPosition().x << ", y=" << self->data->getPosition().y << ", " <<
|
||||
"scale=" << self->data->getScale().x << ", " <<
|
||||
"sprite_number=" << self->data->getSpriteIndex() << ")>";
|
||||
}
|
||||
std::string repr_str = ss.str();
|
||||
return PyUnicode_DecodeUTF8(repr_str.c_str(), repr_str.size(), "replace");
|
||||
}
|
||||
|
||||
int UISprite::init(PyUISpriteObject* self, PyObject* args, PyObject* kwds)
|
||||
{
|
||||
//std::cout << "Init called\n";
|
||||
static const char* keywords[] = { "x", "y", "texture", "sprite_index", "scale", nullptr };
|
||||
float x = 0.0f, y = 0.0f, scale = 1.0f;
|
||||
int sprite_index;
|
||||
PyObject* texture;
|
||||
|
||||
if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ffOif",
|
||||
const_cast<char**>(keywords), &x, &y, &texture, &sprite_index, &scale))
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
// check types for texture
|
||||
//if (texture != NULL && !PyObject_IsInstance(texture, (PyObject*)&PyTextureType)){
|
||||
if (texture != NULL && !PyObject_IsInstance(texture, PyObject_GetAttrString(McRFPy_API::mcrf_module, "Texture"))){
|
||||
PyErr_SetString(PyExc_TypeError, "texture must be a mcrfpy.Texture instance");
|
||||
return -1;
|
||||
}
|
||||
auto pytexture = (PyTextureObject*)texture;
|
||||
self->data = std::make_shared<UISprite>(pytexture->data, sprite_index, sf::Vector2f(x, y), scale);
|
||||
self->data->setPosition(sf::Vector2f(x, y));
|
||||
|
||||
return 0;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue