Skip to content

Commit 82ed54c

Browse files
committed
Add rgbMapSetColors script API to be called on color change.
1 parent d02415f commit 82ed54c

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

engine/src/rgbscript.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ bool RGBScript::load(const QDir& dir, const QString& fileName)
117117
m_script = QScriptValue();
118118
m_rgbMap = QScriptValue();
119119
m_rgbMapStepCount = QScriptValue();
120+
m_rgbMapSetColors = QScriptValue();
120121
m_apiVersion = 0;
121122

122123
m_fileName = fileName;
@@ -154,6 +155,7 @@ bool RGBScript::evaluate()
154155

155156
m_rgbMap = QScriptValue();
156157
m_rgbMapStepCount = QScriptValue();
158+
m_rgbMapSetColors = QScriptValue();
157159
m_apiVersion = 0;
158160

159161
m_script = s_engine->evaluate(m_contents, m_fileName);
@@ -184,6 +186,15 @@ bool RGBScript::evaluate()
184186
m_apiVersion = m_script.property("apiVersion").toInteger();
185187
if (m_apiVersion > 0)
186188
{
189+
if (m_apiVersion >= 3)
190+
{
191+
m_rgbMapSetColors = m_script.property("rgbMapSetColors");
192+
if (m_rgbMapSetColors.isFunction() == false)
193+
{
194+
qWarning() << m_fileName << "is missing the rgbMapSetColors() function!";
195+
return false;
196+
}
197+
}
187198
if (m_apiVersion >= 2)
188199
return loadProperties();
189200
return true;
@@ -249,6 +260,28 @@ int RGBScript::rgbMapStepCount(const QSize& size)
249260
}
250261
}
251262

263+
void RGBScript::rgbMapSetColors(QVector<uint> colors)
264+
{
265+
QMutexLocker engineLocker(s_engineMutex);
266+
if (m_apiVersion <= 2)
267+
return;
268+
if (m_rgbMapSetColors.isValid() == false)
269+
return;
270+
271+
int accColors = acceptColors();
272+
int rawColorCount = colors.count();
273+
QScriptValue jsRawColors = s_engine->newArray(accColors);
274+
for (int i = 0; i < rawColorCount && i < accColors; i++)
275+
jsRawColors.setProperty(i, QScriptValue(colors.at(i)));
276+
277+
QScriptValueList args;
278+
args << jsRawColors;
279+
280+
QScriptValue value = m_rgbMapSetColors.call(QScriptValue(), args);
281+
if (value.isError())
282+
displayError(value, m_fileName);
283+
}
284+
252285
void RGBScript::rgbMap(const QSize& size, uint rgb, int step, RGBMap &map, QVector<uint> &rawColors)
253286
{
254287
QMutexLocker engineLocker(s_engineMutex);

engine/src/rgbscript.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ class RGBScript : public RGBAlgorithm
9090
/** @reimp */
9191
int rgbMapStepCount(const QSize& size);
9292

93+
/** @reimp */
94+
void rgbMapSetColors(QVector<uint> colors);
95+
9396
/** @reimp */
9497
void rgbMap(const QSize& size, uint rgb, int step, RGBMap &map, QVector<uint> &rawColors);
9598

@@ -119,6 +122,7 @@ class RGBScript : public RGBAlgorithm
119122
QScriptValue m_script; //! The script itself
120123
QScriptValue m_rgbMap; //! rgbMap() function
121124
QScriptValue m_rgbMapStepCount; //! rgbMapStepCount() function
125+
QScriptValue m_rgbMapSetColors; //! rgbMapSetColors() function
122126

123127
/************************************************************************
124128
* Properties

engine/src/rgbscriptv4.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ bool RGBScript::load(const QDir& dir, const QString& fileName)
109109
m_script = QJSValue();
110110
m_rgbMap = QJSValue();
111111
m_rgbMapStepCount = QJSValue();
112+
m_rgbMapSetColors = QJSValue();
112113
m_apiVersion = 0;
113114

114115
m_fileName = fileName;
@@ -137,6 +138,7 @@ bool RGBScript::evaluate()
137138

138139
m_rgbMap = QJSValue();
139140
m_rgbMapStepCount = QJSValue();
141+
m_rgbMapSetColors = QJSValue();
140142
m_apiVersion = 0;
141143

142144
if (m_fileName.isEmpty() || m_contents.isEmpty())
@@ -169,6 +171,15 @@ bool RGBScript::evaluate()
169171
m_apiVersion = m_script.property("apiVersion").toInt();
170172
if (m_apiVersion > 0)
171173
{
174+
if (m_apiVersion >= 3)
175+
{
176+
m_rgbMapSetColors = m_script.property("rgbMapSetColors");
177+
if (m_rgbMapSetColors.isCallable() == false)
178+
{
179+
qWarning() << m_fileName << "is missing the rgbMapSetColors() function!";
180+
return false;
181+
}
182+
}
172183
if (m_apiVersion >= 2)
173184
return loadProperties();
174185
return true;
@@ -233,13 +244,38 @@ int RGBScript::rgbMapStepCount(const QSize& size)
233244
}
234245
}
235246

247+
void RGBScript::rgbMapSetColors(QVector<uint> colors)
248+
{
249+
QMutexLocker engineLocker(s_engineMutex);
250+
if (m_apiVersion <= 2)
251+
return;
252+
if (m_rgbMap.isUndefined() == true)
253+
return;
254+
if (m_rgbMapSetColors.isCallable() == false)
255+
return;
256+
257+
int accColors = acceptColors();
258+
int rawColorCount = colors.count();
259+
QJSValue jsRawColors = s_engine->newArray(accColors);
260+
for (int i = 0; i < rawColorCount && i < accColors; i++)
261+
jsRawColors.setProperty(i, QJSValue(colors.at(i)));
262+
263+
QJSValueList args;
264+
args << jsRawColors;
265+
266+
QJSValue value = m_rgbMapSetColors.call(args);
267+
if (value.isError())
268+
displayError(value, m_fileName);
269+
}
270+
236271
void RGBScript::rgbMap(const QSize& size, uint rgb, int step, RGBMap &map, QVector<uint> &rawColors)
237272
{
238273
QMutexLocker engineLocker(s_engineMutex);
239274

240275
if (m_rgbMap.isUndefined() == true)
241276
return;
242277

278+
// Call the rgbMap function
243279
QJSValueList args;
244280
if (m_apiVersion <= 2) {
245281
args << size.width() << size.height() << rgb << step;
@@ -257,6 +293,7 @@ void RGBScript::rgbMap(const QSize& size, uint rgb, int step, RGBMap &map, QVect
257293
if (yarray.isError())
258294
displayError(yarray, m_fileName);
259295

296+
// Check the matrix to be a valid matrix
260297
if (yarray.isArray())
261298
{
262299
QVariantList yvArray = yarray.toVariant().toList();

engine/src/rgbscriptv4.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ class RGBScript : public RGBAlgorithm
9191
/** @reimp */
9292
int rgbMapStepCount(const QSize& size);
9393

94+
/** @reimp */
95+
void rgbMapSetColors(QVector<uint> colors);
96+
9497
/** @reimp */
9598
void rgbMap(const QSize& size, uint rgb, int step, RGBMap &map, QVector<uint> &rawColors);
9699

@@ -120,6 +123,7 @@ class RGBScript : public RGBAlgorithm
120123
QJSValue m_script; //! The script itself
121124
QJSValue m_rgbMap; //! rgbMap() function
122125
QJSValue m_rgbMapStepCount; //! rgbMapStepCount() function
126+
QJSValue m_rgbMapSetColors; //! rgbMapSetColors() function
123127

124128
/************************************************************************
125129
* Properties

0 commit comments

Comments
 (0)