From d1da38ea8d1074944002ff3191b7867e40fb690a Mon Sep 17 00:00:00 2001 From: Adam Johnson Date: Sun, 10 Nov 2024 19:14:10 -0500 Subject: [PATCH] Add `PtFindLayer`. Yas, another helper. --- Scripts/Python/plasma/Plasma.py | 4 ++++ .../FeatureLib/pfPython/cyPythonInterface.cpp | 1 + .../Plasma/FeatureLib/pfPython/pyLayer.cpp | 9 ++++++++ Sources/Plasma/FeatureLib/pfPython/pyLayer.h | 3 +++ .../FeatureLib/pfPython/pyLayerGlue.cpp | 22 +++++++++++++++++++ 5 files changed, 39 insertions(+) diff --git a/Scripts/Python/plasma/Plasma.py b/Scripts/Python/plasma/Plasma.py index 37af826a77..53f2f4110f 100644 --- a/Scripts/Python/plasma/Plasma.py +++ b/Scripts/Python/plasma/Plasma.py @@ -310,6 +310,10 @@ def PtFindImage(name: str) -> Iterable[ptImage]: """Find an already loaded image by name""" ... +def PtFindLayer(name: str, age: str = "", page: str = "") -> Optional[ptLayer]: + """Find a layer 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/cyPythonInterface.cpp b/Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.cpp index 9b93116453..703d86b15f 100644 --- a/Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/cyPythonInterface.cpp @@ -1039,6 +1039,7 @@ void PythonInterface::AddPlasmaMethods(PyObject* m) pyGUIDialog::AddPlasmaMethods(m); pyImage::AddPlasmaMethods(m); pyJournalBook::AddPlasmaMethods(m); + pyLayer::AddPlasmaMethods(m); pySDLModifier::AddPlasmaMethods(m); pySpawnPointInfo::AddPlasmaMethods(m); } diff --git a/Sources/Plasma/FeatureLib/pfPython/pyLayer.cpp b/Sources/Plasma/FeatureLib/pfPython/pyLayer.cpp index 5c441786d8..9ecc390758 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyLayer.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyLayer.cpp @@ -48,6 +48,7 @@ You can contact Cyan Worlds, Inc. by email legal@cyan.com #include "pyImage.h" #include "plMessage/plLayRefMsg.h" +#include "plResMgr/plKeyFinder.h" void pyLayer::setKey(pyKey& layerKey) // only for python glue, do NOT call { @@ -85,3 +86,11 @@ PyObject* pyLayer::GetTexture() const PYTHON_RETURN_NONE; } + +PyObject* pyLayer::Find(const ST::string& name, const ST::string& age, const ST::string& page) +{ + plKey foundKey = plKeyFinder::Instance().StupidSearch(age, page, plLayer::Index(), name); + if (foundKey) + return pyLayer::New(std::move(foundKey)); + PYTHON_RETURN_NONE; +} diff --git a/Sources/Plasma/FeatureLib/pfPython/pyLayer.h b/Sources/Plasma/FeatureLib/pfPython/pyLayer.h index ccc39b0a37..3768ba387c 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyLayer.h +++ b/Sources/Plasma/FeatureLib/pfPython/pyLayer.h @@ -99,6 +99,7 @@ class pyLayer PYTHON_CLASS_CONVERT_FROM_DEFINITION(pyLayer); // converts a PyObject to a pyLayer (throws error if not correct type) static void AddPlasmaClasses(PyObject* m); + static void AddPlasmaMethods(PyObject* m); void setKey(pyKey& layerKey); @@ -120,6 +121,8 @@ class pyLayer // For Python access void SetTexture(plBitmap* image); PyObject* GetTexture() const; + + static PyObject* Find(const ST::string& name, const ST::string& age, const ST::string& page); }; #endif // pyLayer_h diff --git a/Sources/Plasma/FeatureLib/pfPython/pyLayerGlue.cpp b/Sources/Plasma/FeatureLib/pfPython/pyLayerGlue.cpp index 79e2214e93..dccdcffd31 100644 --- a/Sources/Plasma/FeatureLib/pfPython/pyLayerGlue.cpp +++ b/Sources/Plasma/FeatureLib/pfPython/pyLayerGlue.cpp @@ -146,3 +146,25 @@ void pyLayer::AddPlasmaClasses(PyObject* m) PYTHON_CLASS_IMPORT(m, ptLayer); PYTHON_CLASS_IMPORT_END(m); } + +PYTHON_GLOBAL_METHOD_DEFINITION_WKEY(PtFindLayer, args, kwds, "Params: name\nFind a layer by name.") +{ + const char* kwdlist[]{"name", "age", "page", nullptr}; + ST::string name, age, page; + if (!PyArg_ParseTupleAndKeywords(args, kwds, "O&|O&O&", const_cast(kwdlist), + PyUnicode_STStringConverter, &name, + PyUnicode_STStringConverter, &age, + PyUnicode_STStringConverter, &page)) { + PyErr_SetString(PyExc_TypeError, "PtFindLayer expects a string and two optional strings"); + PYTHON_RETURN_ERROR; + } + + return pyLayer::Find(name, age, page); +} + +void pyLayer::AddPlasmaMethods(PyObject* m) +{ + PYTHON_START_GLOBAL_METHOD_TABLE(ptLayer) + PYTHON_GLOBAL_METHOD_WKEY(PtFindLayer) + PYTHON_END_GLOBAL_METHOD_TABLE(m, ptLayer) +}