mirror of
https://github.com/huderlem/porymap.git
synced 2026-08-27 02:56:17 -05:00
Add basic image cache to scripting API
This commit is contained in:
@@ -25,6 +25,7 @@ public:
|
||||
static Tile toTile(QJSValue obj);
|
||||
static QJSValue dimensions(int width, int height);
|
||||
static QJSEngine *getEngine();
|
||||
static QImage getImage(QString filepath);
|
||||
static void init(MainWindow *mainWindow);
|
||||
static void registerAction(QString functionName, QString actionName);
|
||||
static int numRegisteredActions();
|
||||
@@ -41,6 +42,7 @@ private:
|
||||
QStringList filepaths;
|
||||
QList<QJSValue> modules;
|
||||
QMap<QString, QString> registeredActions;
|
||||
QMap<QString, const QImage*> imageCache;
|
||||
|
||||
void loadModules(QStringList moduleFiles);
|
||||
void invokeCallback(CallbackType type, QJSValueList args);
|
||||
|
||||
@@ -193,3 +193,12 @@ QJSValue Scripting::fromTile(Tile tile) {
|
||||
QJSEngine *Scripting::getEngine() {
|
||||
return instance->engine;
|
||||
}
|
||||
|
||||
QImage Scripting::getImage(QString filepath) {
|
||||
const QImage * image = instance->imageCache.value(filepath, nullptr);
|
||||
if (!image) {
|
||||
image = new QImage(filepath);
|
||||
instance->imageCache.insert(filepath, image);
|
||||
}
|
||||
return QImage(*image);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "overlay.h"
|
||||
#include "scripting.h"
|
||||
#include "log.h"
|
||||
|
||||
void OverlayText::render(QPainter *painter) {
|
||||
@@ -53,7 +54,7 @@ void Overlay::addRect(int x, int y, int width, int height, QString color, bool f
|
||||
}
|
||||
|
||||
bool Overlay::addImage(int x, int y, QString filepath, int width, int height, unsigned offset, bool hflip, bool vflip, bool setTransparency) {
|
||||
QImage image = QImage(filepath);
|
||||
QImage image = Scripting::getImage(filepath);
|
||||
if (image.isNull()) {
|
||||
logError(QString("Failed to load image '%1'").arg(filepath));
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user