diff --git a/main.js b/main.js index e3233ca1cc78aa6b3d7246ffa424b615af1f7863..349696bc8f9641943a24e22691acb67c6adf86e6 100644 --- a/main.js +++ b/main.js @@ -187,6 +187,21 @@ class ModuleInstance extends InstanceBase { this.log('debug', `Cannot handle incoming message ${data}`) } } + + updateVariables() { + this.setVariableDefinitions([...Array(this.currentProject.getPadCount()).keys()].map(index => { + return {variableId: `pad-${index}`, name: `Pad Index ${index + 1}`} + })) + } + + updateVariablesForCurrentPage() { + let pads = this.currentProject.getAllPadsOfCurrentPage(); + for (let i in pads) { + let pad = pads[i]; + this.log('debug', `Update variable 'pad-${pad.position}'`); + this.setVariableValues({ [`pad-${pad.position}`]: pad.name}); + } + } } runEntrypoint(ModuleInstance, UpgradeScripts) diff --git a/project.js b/project.js index 0f54e19d28ba92e7ea994fe76c517dc9aed0b228..bb3e2d7519e12b13d1c27ac7fed36b62f92c3162 100644 --- a/project.js +++ b/project.js @@ -44,6 +44,33 @@ module.exports = class Project { Pads */ + /** + * + * @returns {number} + */ + getPadCount() { + return this.data.settings.columns * this.data.settings.rows; + } + + /** + * + * @returns {*[]} + */ + getAllPadsOfCurrentPage() { + let pads = []; + + for (const pageId in this.data.pages) { + const page = this.data.pages[pageId] + if (page.position === this.currentPage) { + for (const padId in page.pads) { + const pad = page.pads[padId]; + pads.push(pad); + } + } + } + return pads; + } + /** * * @param {string} oldName diff --git a/receive/current_page_update.js b/receive/current_page_update.js index a03d93654e2cc6ffd2fe049444a3e3a7b23bff80..abcfe4219cc643f4fb56d15ee9b754004c74bafa 100644 --- a/receive/current_page_update.js +++ b/receive/current_page_update.js @@ -5,5 +5,7 @@ module.exports = class CurrentPageUpdate extends MessageExecutable { handleMessage(plugin, message) { plugin.currentProject.updateCurrentPage(message.newPage); plugin.checkFeedbacks(); + + plugin.updateVariablesForCurrentPage(); } } \ No newline at end of file diff --git a/receive/pad_name_update.js b/receive/pad_name_update.js index d7533f5e8ed0312732186edf0810105cef28e3ff..91f5f0f908f0b6b296ada8b6e269aa09808c3826 100644 --- a/receive/pad_name_update.js +++ b/receive/pad_name_update.js @@ -4,5 +4,11 @@ module.exports = class PadNameUpdate extends MessageExecutable { handleMessage(plugin, message) { plugin.currentProject.updatePadName(message.oldValue, message.newValue); + + let pad = plugin.currentProject.findPadById(message.pad); + if (pad.page === plugin.currentProject.getCurrentPage()) { + plugin.log('debug', `Update variable 'pad-${pad.position}'`); + plugin.setVariableValues({[`pad-${pad.position}`]: message.newValue}); + } } } \ No newline at end of file diff --git a/receive/project_update.js b/receive/project_update.js index 352005556680d300cca46bfde333e772585cb7bc..18518f6cb449b42c85519e08aff24f94567c0d2e 100644 --- a/receive/project_update.js +++ b/receive/project_update.js @@ -6,5 +6,8 @@ module.exports = class ProjectUpdate extends MessageExecutable { handleMessage(plugin, message) { plugin.currentProject = new Project(message) plugin.checkFeedbacks(); + + plugin.updateVariables() + plugin.updateVariablesForCurrentPage(); } } \ No newline at end of file