Add basic image cache to scripting API
This commit is contained in:
parent
1adc489bc0
commit
eca84beae0
3 changed files with 13 additions and 1 deletions
|
@ -25,6 +25,7 @@ public:
|
||||||
static Tile toTile(QJSValue obj);
|
static Tile toTile(QJSValue obj);
|
||||||
static QJSValue dimensions(int width, int height);
|
static QJSValue dimensions(int width, int height);
|
||||||
static QJSEngine *getEngine();
|
static QJSEngine *getEngine();
|
||||||
|
static QImage getImage(QString filepath);
|
||||||
static void init(MainWindow *mainWindow);
|
static void init(MainWindow *mainWindow);
|
||||||
static void registerAction(QString functionName, QString actionName);
|
static void registerAction(QString functionName, QString actionName);
|
||||||
static int numRegisteredActions();
|
static int numRegisteredActions();
|
||||||
|
@ -41,6 +42,7 @@ private:
|
||||||
QStringList filepaths;
|
QStringList filepaths;
|
||||||
QList<QJSValue> modules;
|
QList<QJSValue> modules;
|
||||||
QMap<QString, QString> registeredActions;
|
QMap<QString, QString> registeredActions;
|
||||||
|
QMap<QString, const QImage*> imageCache;
|
||||||
|
|
||||||
void loadModules(QStringList moduleFiles);
|
void loadModules(QStringList moduleFiles);
|
||||||
void invokeCallback(CallbackType type, QJSValueList args);
|
void invokeCallback(CallbackType type, QJSValueList args);
|
||||||
|
|
|
@ -193,3 +193,12 @@ QJSValue Scripting::fromTile(Tile tile) {
|
||||||
QJSEngine *Scripting::getEngine() {
|
QJSEngine *Scripting::getEngine() {
|
||||||
return instance->engine;
|
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 "overlay.h"
|
||||||
|
#include "scripting.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
void OverlayText::render(QPainter *painter) {
|
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) {
|
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()) {
|
if (image.isNull()) {
|
||||||
logError(QString("Failed to load image '%1'").arg(filepath));
|
logError(QString("Failed to load image '%1'").arg(filepath));
|
||||||
return false;
|
return false;
|
||||||
|
|
Loading…
Reference in a new issue