Skip to content

Commit

Permalink
Improve pyImageLibMod interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Deledrius committed Jul 17, 2023
1 parent 3cbdc4d commit b5ead56
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 24 deletions.
18 changes: 18 additions & 0 deletions Scripts/Python/plasma/Plasma.py
Original file line number Diff line number Diff line change
Expand Up @@ -5261,6 +5261,24 @@ def saveAsJPEG(self,filename,quality=75):
"""Saves this image to disk as a JPEG file"""
pass

class ptImageLibMod:
"""Plasma ImageLibraryModifier class"""
def __init__(self,imlkey):
"""None"""
pass

def getImage(imageName):
"""Returns the named image, if present"""
pass

def getImages():
"""Returns a tuple of ptImages"""
pass

def getNames():
"""Returns a tuple of the image names"""
pass

class ptInputInterface:
"""Plasma input interface class"""
def __init__(self):
Expand Down
16 changes: 11 additions & 5 deletions Scripts/Python/stupStartUp.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,14 @@ def OnServerInitComplete(self):

###########################
def randomizeBackground(self):
if ilmList := BGObj.sceneobject.getImageLibMods():
ilm = ptImageLibMod(ilmList[0])
bgChoice = random.choice(ilm.getNames())
if newImage := ilm.getImage(bgChoice):
BGLayer.layer.texture = newImage
if not BGObj.sceneobject or not BGLayer.layer:
PtDebugPrint("stupStartUp: Missing ptAttribs for randomized backgrounds. Leaving default...")
return

# Get the first available ImageLibModifier on our Background SceneObject
if ilm := BGObj.sceneobject.getImageLibMods()[0]:
# Choose a random image from the available images
bgChoice = random.choice(ilm.getImages())
# Update the Background Layer's texture to our chosen image
BGLayer.layer.texture = bgChoice
PtDebugPrint("stupStartUp: Background has been randomized.")
25 changes: 22 additions & 3 deletions Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ You can contact Cyan Worlds, Inc. by email [email protected]
#include "hsResMgr.h"

#include "pnKeyedObject/plUoid.h"
#include "pyImage.h"
#include "pyImageLibMod.h"


void pyImageLibMod::setKey(pyKey& ilmKey) // only for python glue, do NOT call
{
if (fModifier && fModifierKey)
Expand All @@ -56,17 +58,34 @@ void pyImageLibMod::setKey(pyKey& ilmKey) // only for python glue, do NOT call
fModifierKey = ilmKey.getKey();
}

plBitmap* pyImageLibMod::GetImage(const ST::string& name) const
pyImage* pyImageLibMod::GetImage(const ST::string& name) const
{
plBitmap* image;

if (fModifier)
image = fModifier->GetImage(name);
else
image = plImageLibMod::ConvertNoRef(fModifierKey->ObjectIsLoaded())->GetImage(name);
return image;

return pyImage::ConvertFrom(pyImage::New(dynamic_cast<plMipmap*>(image)));
}

const std::vector<pyImage*> pyImageLibMod::GetImages() const
{
std::vector<pyImage*> imageList;
plImageLibMod* mod;

if (fModifier)
mod = fModifier;
else
mod = plImageLibMod::ConvertNoRef(fModifierKey->ObjectIsLoaded());

for (const auto& image : mod->GetImages())
imageList.push_back(pyImage::ConvertFrom(pyImage::New(dynamic_cast<plMipmap*>(image))));
return imageList;
}

std::vector<ST::string> pyImageLibMod::GetImageNames() const
const std::vector<ST::string> pyImageLibMod::GetImageNames() const
{
std::vector<ST::string> nameList;
plImageLibMod* mod;
Expand Down
7 changes: 5 additions & 2 deletions Sources/Plasma/FeatureLib/pfPython/pyImageLibMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ You can contact Cyan Worlds, Inc. by email [email protected]
#include "pnKeyedObject/plKey.h"
#include "plModifier/plImageLibMod.h"

class pyImage;

class pyImageLibMod
{
protected:
Expand Down Expand Up @@ -114,8 +116,9 @@ class pyImageLibMod
plKey GetKey() const { return fModifier ? fModifier->GetKey() : fModifierKey; }

// for python access
plBitmap* GetImage (const ST::string& name) const;
std::vector<ST::string> GetImageNames() const;
pyImage* GetImage (const ST::string& name) const;
const std::vector<ST::string> GetImageNames() const;
const std::vector<pyImage*> GetImages() const;
};

#endif // pyImageLibMod_h
14 changes: 12 additions & 2 deletions Sources/Plasma/FeatureLib/pfPython/pyImageLibModGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,22 @@ PYTHON_METHOD_DEFINITION(ptImageLibMod, getImage, args)
PYTHON_RETURN_ERROR;
}

plMipmap* image = plMipmap::ConvertNoRef(self->fThis->GetImage(name));
pyImage* image = self->fThis->GetImage(name);
if (image)
return pyImage::New(image);
return pyImage::New(image->GetKey());

PYTHON_RETURN_NONE;
}

PYTHON_METHOD_DEFINITION_NOARGS(ptImageLibMod, getImages)
{
const std::vector<pyImage*> imageList = self->fThis->GetImages();
PyObject* retVal = PyTuple_New(imageList.size());
for (size_t curKey = 0; curKey < imageList.size(); curKey++)
PyTuple_SET_ITEM(retVal, curKey, pyImage::New(imageList[curKey]->GetKey()));
return retVal;
}

PYTHON_METHOD_DEFINITION_NOARGS(ptImageLibMod, getNames)
{
std::vector<ST::string> nameList = self->fThis->GetImageNames();
Expand All @@ -131,6 +140,7 @@ PYTHON_METHOD_DEFINITION_NOARGS(ptImageLibMod, getNames)

PYTHON_START_METHODS_TABLE(ptImageLibMod)
PYTHON_METHOD(ptImageLibMod, getImage, "Params: name\nReturns the ptImage with the specified name"),
PYTHON_METHOD_NOARGS(ptImageLibMod, getImages, "Returns a tuple of the library's ptImages"),
PYTHON_METHOD_NOARGS(ptImageLibMod, getNames, "Returns the list of image names"),
PYTHON_END_METHODS_TABLE;

Expand Down
3 changes: 2 additions & 1 deletion Sources/Plasma/FeatureLib/pfPython/pySceneObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ You can contact Cyan Worlds, Inc. by email [email protected]
#include "pyKey.h"
#include "pyMatrix44.h"

#include "pyImageLibMod.h"
#include "pySceneObject.h"

#include "cyAvatar.h"
Expand Down Expand Up @@ -838,7 +839,7 @@ std::vector<PyObject*> pySceneObject::GetImageLibMods()
{
const plImageLibMod* ilm = plImageLibMod::ConvertNoRef(obj->GetModifierByType(plImageLibMod::Index()));
if (ilm)
pyPL.push_back(pyKey::New(ilm->GetKey()));
pyPL.push_back(pyImageLibMod::New(ilm->GetKey()));
}
}
return pyPL;
Expand Down
4 changes: 2 additions & 2 deletions Sources/Plasma/FeatureLib/pfPython/pySceneObjectGlue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,9 @@ PYTHON_METHOD_DEFINITION_NOARGS(ptSceneobject, getPythonMods)
PYTHON_METHOD_DEFINITION_NOARGS(ptSceneobject, getImageLibMods)
{
std::vector<PyObject*> vecList = self->fThis->GetImageLibMods();
PyObject* retVal = PyList_New(vecList.size());
PyObject* retVal = PyTuple_New(vecList.size());
for (int curKey = 0; curKey < vecList.size(); curKey++)
PyList_SetItem(retVal, curKey, vecList[curKey]); // steals the vecList ref
PyTuple_SetItem(retVal, curKey, vecList[curKey]);
return retVal;
}

Expand Down
15 changes: 8 additions & 7 deletions Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ You can contact Cyan Worlds, Inc. by email [email protected]
#include "hsStream.h"
#include "hsResMgr.h"

plImageLibMod::plImageLibMod()
{
}

plImageLibMod::~plImageLibMod()
{
}

bool plImageLibMod::MsgReceive(plMessage* msg)
{
Expand Down Expand Up @@ -106,6 +99,14 @@ plBitmap* plImageLibMod::GetImage(const ST::string& imageName) const
return nullptr;
}

const std::vector<plBitmap*> plImageLibMod::GetImages() const
{
std::vector<plBitmap*> images;
for (auto image : fImages)
images.emplace_back(image);
return images;
}

const std::vector<ST::string> plImageLibMod::GetImageNames() const
{
std::vector<ST::string> names;
Expand Down
5 changes: 3 additions & 2 deletions Sources/Plasma/PubUtilLib/plModifier/plImageLibMod.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ class plImageLibMod : public plSingleModifier
bool IEval(double secs, float del, uint32_t dirty) override { return false; }

public:
plImageLibMod();
virtual ~plImageLibMod();
plImageLibMod() {};
virtual ~plImageLibMod() {};

CLASSNAME_REGISTER( plImageLibMod );
GETINTERFACE_ANY( plImageLibMod, plSingleModifier );
Expand All @@ -76,6 +76,7 @@ class plImageLibMod : public plSingleModifier

size_t GetNumImages() const { return fImages.size(); }
plBitmap* GetImage(const ST::string&) const;
const std::vector<plBitmap*> GetImages() const;
const std::vector<ST::string> GetImageNames() const;
};

Expand Down

0 comments on commit b5ead56

Please sign in to comment.