From ff85e8734cd3f1451139f29e23f9d4d5eb840a01 Mon Sep 17 00:00:00 2001 From: tobias <thinkdifferent055@gmail.com> Date: Sat, 7 Oct 2023 10:55:49 +0200 Subject: [PATCH] #187 - Update feedback on page change, add option for pad index based feedback --- feedbacks.js | 18 ++++++++++++-- main.js | 4 ++- project.js | 45 ++++++++++++++++++++++++++++++++++ receive/current_page_update.js | 9 +++++++ 4 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 receive/current_page_update.js diff --git a/feedbacks.js b/feedbacks.js index 531f879..f713107 100644 --- a/feedbacks.js +++ b/feedbacks.js @@ -12,11 +12,25 @@ module.exports = async function (self) { type: 'textinput', label: 'Pad Name', default: '', + required: false + }, + { + id: 'index', + type: 'number', + label: 'Pad ID', + default: '', + required: false }, ], callback: (feedback, context) => { - self.log('debug', `Update feedback for pad name ${feedback.options.name}`); - const pad = self.currentProject.findPadByName(feedback.options.name); + let pad; + if (feedback.options.name) { + self.log('debug', `Update feedback for pad name ${feedback.options.name}`); + pad = self.currentProject.findPadByName(feedback.options.name); + } else { + self.log('debug', `Update feedback for pad index ${feedback.options.index}`); + pad = self.currentProject.findPadByIndexInCurrentPage(feedback.options.index - 1); + } if (pad != null) { self.log('debug', `Update feedback for pad id ${pad.id} with status ${pad.status}`); if (pad.status === 'PLAY') { diff --git a/main.js b/main.js index 7763ee3..e3233ca 100644 --- a/main.js +++ b/main.js @@ -7,6 +7,7 @@ const ProjectUpdate = require("./receive/project_update"); const Project = require("./project"); const PadNameUpdate = require("./receive/pad_name_update"); const PadStatusUpdate = require("./receive/pad_status_update"); +const CurrentPageUpdate = require("./receive/current_page_update"); const uuid = require('uuid'); class ModuleInstance extends InstanceBase { @@ -17,7 +18,8 @@ class ModuleInstance extends InstanceBase { messageHandlers = { 'project-current': new ProjectUpdate(), 'pad-name-changed': new PadNameUpdate(), - 'pad-status-changed': new PadStatusUpdate() + 'pad-status-changed': new PadStatusUpdate(), + 'current-page-changed': new CurrentPageUpdate() }; currentProject = new Project({}); diff --git a/project.js b/project.js index 57530b3..603ba84 100644 --- a/project.js +++ b/project.js @@ -1,9 +1,11 @@ module.exports = class Project { constructor(data) { this.padCache = {} + this.currentPage = 0; // Prefill with current project if (data) { + this.data = data; console.log('Create pad cache') for (const pageId in data.pages) { const page = data.pages[pageId] @@ -18,6 +20,30 @@ module.exports = class Project { } } + /* + Page + */ + + /** + * + * @returns {number} + */ + getCurrentPage() { + return this.currentPage; + } + + /** + * + * @param {number} newPage + */ + updateCurrentPage(newPage) { + this.currentPage = newPage; + } + + /* + Pads + */ + /** * * @param {string} oldName @@ -49,6 +75,25 @@ module.exports = class Project { return this.padCache[name]; } + /** + * + * @param {number} index + */ + findPadByIndexInCurrentPage(index) { + 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]; + if (pad.position === index) { + return pad; + } + } + } + } + return null; + } + /** * * @param {string} padId diff --git a/receive/current_page_update.js b/receive/current_page_update.js new file mode 100644 index 0000000..a03d936 --- /dev/null +++ b/receive/current_page_update.js @@ -0,0 +1,9 @@ +const MessageExecutable = require("./message_executable"); + +module.exports = class CurrentPageUpdate extends MessageExecutable { + + handleMessage(plugin, message) { + plugin.currentProject.updateCurrentPage(message.newPage); + plugin.checkFeedbacks(); + } +} \ No newline at end of file -- GitLab