diff --git a/Scripts/Python/plasma/Plasma.py b/Scripts/Python/plasma/Plasma.py index cdb52b4206..5d7c0421d5 100644 --- a/Scripts/Python/plasma/Plasma.py +++ b/Scripts/Python/plasma/Plasma.py @@ -306,6 +306,10 @@ def PtFileExists(filename): """Returns true if the specified file exists""" pass +def PtFindImage(name: str) -> Tuple[ptImage]: + """Find an already loaded image by name""" + ... + def PtFindSceneobject(name,ageName): """This will try to find a sceneobject based on its name and what age its in - it will return a ptSceneObject if found- if not found then a NameError exception will happen""" diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp b/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp index 4ba2c0d012..43b291fb33 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyImage.cpp @@ -42,6 +42,9 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "pyImage.h" +#include +#include + #include #include "hsResMgr.h" @@ -52,6 +55,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "plGImage/plJPEG.h" #include "plGImage/plMipmap.h" #include "plGImage/plPNG.h" +#include "plResMgr/plKeyFinder.h" #include "pyColor.h" #include "pyGeometry3.h" @@ -201,6 +205,23 @@ void pyImage::SaveAsPNG(const plFileName& fileName, const std::multimapGetImage(), textFields); } +PyObject* pyImage::Find(const ST::string& name) +{ + std::vector foundKeys; + plKeyFinder::Instance().ReallyStupidSubstringSearch(name, plMipmap::Index(), foundKeys); + // Remove anything that isn't loaded - they aren't useful in Python code + std::remove_if( + foundKeys.begin(), + foundKeys.end(), + [](const plKey& key) { return key->ObjectIsLoaded() == nullptr; } + ); + + PyObject* tup = PyTuple_New(foundKeys.size()); + for (size_t i = 0; i < foundKeys.size(); ++i) + PyTuple_SET_ITEM(tup, i, pyImage::New(foundKeys[i])); + return tup; +} + PyObject* pyImage::LoadJPEGFromDisk(const plFileName& filename, uint16_t width, uint16_t height) { plMipmap* theMipmap = plJPEG::Instance().ReadFromFile(filename); diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImage.h b/Sources/Plasma/FeatureLib/pfPython/pyImage.h index 73fe2559e4..3dac7ce584 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImage.h +++ b/Sources/Plasma/FeatureLib/pfPython/pyImage.h @@ -150,6 +150,8 @@ class pyImage uint32_t GetHeight(); // returns the height of the image void SaveAsJPEG(const plFileName& fileName, uint8_t quality = 75); void SaveAsPNG(const plFileName& fileName, const std::multimap& textFields = std::multimap()); + + static PyObject* Find(const ST::string& name); static PyObject* LoadJPEGFromDisk(const plFileName& filename, uint16_t width, uint16_t height); // returns pyImage static PyObject* LoadPNGFromDisk(const plFileName& filename, uint16_t width, uint16_t height); // returns pyImage #endif diff --git a/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp b/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp index a0183fc0f0..3b75c9733c 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyImageGlue.cpp @@ -252,6 +252,17 @@ void pyImage::AddPlasmaClasses(PyObject *m) } #ifndef BUILDING_PYPLASMA +PYTHON_GLOBAL_METHOD_DEFINITION(PtFindImage, args, "Params: name\nFind an already loaded image by by name.") +{ + ST::string name; + if (!PyArg_ParseTuple(args, "O&", PyUnicode_STStringConverter, &name)) { + PyErr_SetString(PyExc_TypeError, "PtFindImage expects a string"); + PYTHON_RETURN_ERROR; + } + + return pyImage::Find(name); +} + PYTHON_GLOBAL_METHOD_DEFINITION(PtLoadJPEGFromDisk, args, "Params: filename,width,height\nThe image will be resized to fit the width and height arguments. Set to 0 if resizing is not desired.\nReturns a pyImage of the specified file.") { plFileName filename; @@ -283,6 +294,7 @@ void pyImage::AddPlasmaMethods(PyObject* m) { #ifndef BUILDING_PYPLASMA PYTHON_START_GLOBAL_METHOD_TABLE(ptImage) + PYTHON_GLOBAL_METHOD(PtFindImage) PYTHON_GLOBAL_METHOD(PtLoadJPEGFromDisk) PYTHON_GLOBAL_METHOD(PtLoadPNGFromDisk) PYTHON_END_GLOBAL_METHOD_TABLE(m, ptImage)