performance.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. // SPDX-FileCopyrightText: 2012-2023 MOD Audio UG
  2. // SPDX-License-Identifier: AGPL-3.0-or-later
  3. /*
  4. * performance view behaviour
  5. *
  6. * The interface for managing your pedal board parameters on a touch device
  7. *
  8. * Properties:
  9. */
  10. JqueryClass('performanceBox', {
  11. init: function (options) {
  12. var self = $(this)
  13. options = $.extend({
  14. pedalboard: undefined,
  15. pedalPresets: undefined,
  16. resultCanvasPlugins: self.find('.js-performance-plugins'),
  17. resultCanvasPluginSettings: self.find('.js-performance-plugin-settings'),
  18. visibleFilter: true,
  19. selectedElement: undefined,
  20. selectedIndex: -1,
  21. plugins: [], // filtered plugins
  22. nodes: [], // rendered plugins
  23. inDesign: false,
  24. isMainWindow: true,
  25. windowName: "Performance",
  26. selectElement: function (element, callback) {
  27. const pedalboard = self.data("pedalboard")
  28. if (pedalboard) {
  29. let selectedIndex = -1
  30. const displaySelectedElementSettings = self.data("displaySelectedElementSettings")
  31. const currSelectedIndex = self.data("selectedIndex")
  32. // remove selected from the current selected element
  33. if (currSelectedIndex >= 0) {
  34. $("#mod-performance-plugin-" + currSelectedIndex.toString())?.removeClass("selected");
  35. }
  36. if (element === ":presets") {
  37. const pedalPresets = self.data("pedalPresets")
  38. let div = document.createElement("div")
  39. pedalPresets.getPedalPresetList(function(presets) {
  40. let data = []
  41. Object.keys(presets).forEach(key => data.push({index: data.length + 1, label: presets[key]}) )
  42. div.innerHTML = Mustache.render(TEMPLATES.performance_snapshots, {presets: data})
  43. let rendered = $(Array.prototype.slice.call(div.childNodes, 0))
  44. const settings = rendered[0];
  45. // add the selected class
  46. selectedIndex = 0;
  47. displaySelectedElementSettings(element, selectedIndex, settings, function() {
  48. self.data('scrollAndSelectElement')("#mod-performance-plugin-0")
  49. // attach events
  50. $(".performance .snapshot").each(function(index, snapshotDiv) {
  51. snapshotDiv.onclick = function (e) {
  52. pedalPresets.loadPreset(index, e.target.innerText)
  53. }
  54. });
  55. })
  56. });
  57. } else {
  58. //TODO: if element is plugin, show plugin settings
  59. const plugin = element
  60. const plugins = self.data("plugins")
  61. const settings = plugin.settingsPerformance[0];
  62. selectedIndex = plugins.indexOf(plugin) + 1 // +1 'cause 0 is the snapshot page
  63. displaySelectedElementSettings(element, selectedIndex, settings)
  64. self.data('scrollAndSelectElement')("#mod-performance-plugin-" + selectedIndex.toString())
  65. }
  66. }
  67. },
  68. scrollAndSelectElement: function(elementSelector) {
  69. let element = $(elementSelector)
  70. if (element && element[0]) {
  71. element.addClass("selected")
  72. element[0].scrollIntoView({
  73. "behavior" : "smooth",
  74. "block": "start",
  75. "container": "nearest",
  76. "inline": "nearest"
  77. });
  78. }
  79. },
  80. // show the settings for the selected plugin or snapshots
  81. displaySelectedElementSettings: function(selectedElement, selectedIndex, settings, callback) {
  82. const settingsContainer = self.data('resultCanvasPluginSettings')
  83. const settingsDiv = settingsContainer[0];
  84. if (settingsDiv) {
  85. $(settingsDiv).fadeOut(200, function() {
  86. settingsDiv.innerHTML = ""
  87. if (settings) {
  88. settingsDiv.appendChild(settings);
  89. $(settingsDiv).fadeIn(200);
  90. }
  91. self.data("selectedElement", selectedElement)
  92. self.data("selectedIndex", selectedIndex)
  93. if (callback)
  94. callback([])
  95. })
  96. }
  97. },
  98. /*
  99. * Update the plugin effect list after a change made by another client,
  100. * keeping the selected plugin when it is still listed
  101. */
  102. refreshPlugins: function() {
  103. if (!self.is(':visible'))
  104. return
  105. const selectedElement = self.data("selectedElement")
  106. self.data('updatePlugins')()
  107. const selectedIndex = self.data("plugins").indexOf(selectedElement) + 1 // +1 'cause 0 is the snapshot page
  108. if (selectedElement === ":presets") {
  109. $("#mod-performance-plugin-0").addClass("selected")
  110. } else if (selectedIndex > 0) {
  111. self.data("selectedIndex", selectedIndex)
  112. $("#mod-performance-plugin-" + selectedIndex.toString()).addClass("selected")
  113. } else {
  114. self.data('selectElement')(":presets")
  115. }
  116. },
  117. /*
  118. * Update the plugin effect list
  119. */
  120. updatePlugins: function() {
  121. const pedalboard = self.data("pedalboard")
  122. if (!pedalboard)
  123. return
  124. const plugins = pedalboard.data("plugins")
  125. const canvas = self.data("resultCanvasPlugins")
  126. canvas[0].innerHTML = ""
  127. // append snapshot view
  128. var div = document.createElement("div");
  129. div.innerHTML = Mustache.render(TEMPLATES.performance_plugin, {
  130. uri : ":presets",
  131. brand : " ",
  132. label : "Snapshots",
  133. thumbnail_href: "/img/presets.png"
  134. });
  135. var rendered = $(Array.prototype.slice.call(div.childNodes, 0))
  136. rendered[0].id = "mod-performance-plugin-0"
  137. rendered.click(function () {
  138. self.data("selectElement")(":presets")
  139. })
  140. canvas.append(rendered);
  141. // TODO: controls assigned to phisical mod
  142. // append effects controls one by one
  143. const visibleFilter = self.data("visibleFilter")
  144. var guis = []
  145. for (pluginKey in plugins) {
  146. guis.push(plugins[pluginKey].data("gui"))
  147. }
  148. guis = guis
  149. .filter(item => !visibleFilter || item.getPerformanceOptions()?.visible === visibleFilter)
  150. .sort(function(a,b) {
  151. const pa = a.getPerformanceOptions()
  152. const pb = b.getPerformanceOptions()
  153. const labela = a.label || a.effect.name
  154. const labelb = b.label || b.effect.name
  155. //console.log(`${labela}-${pa.index} vs ${labelb}-${pb.index}`)
  156. if (pa.index < pb.index)
  157. return -1
  158. else if (pa.index > pb.index)
  159. return 1
  160. else {
  161. if (labela < labelb)
  162. return -1
  163. else if (labela > labelb)
  164. return 1
  165. else
  166. return 0
  167. }
  168. })
  169. let index = 1
  170. self.data('plugins', guis)
  171. let nodes = []
  172. for (key in guis) {
  173. const gui = guis[key];
  174. const node = self.performanceBox("renderPlugin", index, gui, canvas)
  175. nodes.push(node)
  176. index += 1
  177. }
  178. self.data('nodes', nodes)
  179. const saveButton = $('#performance-save-button')
  180. if (saveButton) {
  181. if (self.data("inDesign")) {
  182. saveButton.removeClass('mod-hidden')
  183. } else {
  184. saveButton.addClass('mod-hidden')
  185. }
  186. }
  187. },
  188. savePedalboard: function() {
  189. desktop.saveCurrentPedalboard(false)
  190. }
  191. }, options)
  192. self.data(options)
  193. options.open = function () {
  194. // check the default for the visibleFilter: if any plugin is favorite the filter will be true on oper
  195. const pedalboard = self.data("pedalboard")
  196. if (pedalboard) {
  197. let defaultvisibleFilter = false;
  198. const plugins = pedalboard.data("plugins")
  199. for (pluginKey in plugins) {
  200. const gui = plugins[pluginKey].data("gui")
  201. if (gui.getPerformanceOptions().visible) {
  202. defaultvisibleFilter = true;
  203. break;
  204. }
  205. }
  206. self.data('visibleFilter', defaultvisibleFilter)
  207. }
  208. self.data('updatePlugins')()
  209. self.data("selectElement")(":presets")
  210. return false
  211. }
  212. const canvas = self.data("resultCanvasPlugins")
  213. canvas.bind("mousewheel", function(event, delta, deltaX, deltaY) {
  214. let index = self.data("selectedIndex")
  215. index -= deltaY;
  216. if (index < 0)
  217. index = 0;
  218. const pluginDiv = $("#mod-performance-plugin-" + index.toString())
  219. if (pluginDiv && pluginDiv[0]) {
  220. let selectedElement;
  221. if (index == 0) { // snapshots
  222. selectedElement = ":presets";
  223. } else {
  224. const plugin = pluginDiv.data("plugin")
  225. if (plugin) {
  226. selectedElement = plugin
  227. }
  228. }
  229. self.data('selectElement')(selectedElement)
  230. }
  231. event.preventDefault();
  232. });
  233. canvas.bind("swipe", function(e) {
  234. console.log(`swipe ${e}`)
  235. })
  236. const visibleFilterButton = self.find('#performance-filter-visibility')
  237. visibleFilterButton.click(function() {
  238. const newValue = !self.data('visibleFilter')
  239. const icon = self.find('#performance-filter-visibility')
  240. if (newValue) {
  241. visibleFilterButton.addClass("active")
  242. icon.removeClass('icon-eye-off')
  243. icon.addClass('icon-eye')
  244. } else {
  245. visibleFilterButton.removeClass("active")
  246. icon.removeClass('icon-eye')
  247. icon.addClass('icon-eye-off')
  248. }
  249. self.data('visibleFilter', newValue)
  250. self.data('updatePlugins')()
  251. self.data("selectElement")(":presets")
  252. });
  253. // enable drag & drop on mobile
  254. const settingsButton = self.find("#performance-design-button")
  255. settingsButton.click(function() {
  256. const newInDesign = !self.data("inDesign")
  257. if (newInDesign) {
  258. visibleFilterButton.removeClass("mod-hidden")
  259. } else {
  260. visibleFilterButton.addClass("mod-hidden")
  261. }
  262. self.data("inDesign", newInDesign)
  263. self.data('updatePlugins')()
  264. self.data("selectElement")(":presets")
  265. });
  266. // display save pedalboard button
  267. const saveButton = $('#performance-save-button')
  268. saveButton?.click(function() {
  269. self.data('savePedalboard')()
  270. })
  271. self.window(options)
  272. return self
  273. },
  274. renderPlugin: function (index, plugin, container) {
  275. var self = $(this)
  276. if (container.length == 0)
  277. return
  278. var uri = escape(plugin.effect.uri)
  279. var ver = [plugin.effect.builder, plugin.effect.microVersion, plugin.effect.minorVersion, plugin.effect.release].join('_')
  280. var plugin_data = {
  281. uri : uri,
  282. brand : !plugin.effect.brand ? "&nbsp;": plugin.effect.brand,
  283. label : plugin.label || plugin.effect.label,
  284. thumbnail_href: (plugin.effect.gui && plugin.effect.gui.thumbnail)
  285. ? ("/effect/image/thumbnail.png?uri=" + uri + "&v=" + ver)
  286. : "/resources/pedals/default-thumbnail.png",
  287. }
  288. if (window.devicePixelRatio && window.devicePixelRatio >= 2) {
  289. plugin_data.thumbnail_href = plugin_data.thumbnail_href.replace("thumbnail","screenshot")
  290. }
  291. const visibleFilter = self.data('visibleFilter')
  292. var div = document.createElement("div");
  293. div.innerHTML = Mustache.render(TEMPLATES.performance_plugin, plugin_data);
  294. var rendered = $(Array.prototype.slice.call(div.childNodes, 0));
  295. rendered[0].id = "mod-performance-plugin-" + index.toString()
  296. $(rendered).data('plugin', plugin)
  297. rendered[0].setAttribute('mod-instance', plugin.instance)
  298. if (self.data("inDesign")) {
  299. // enable drag & drop in design mode
  300. rendered.draggable({
  301. axis: desktop.isMobile ? "y" : "x",
  302. scroll: true,
  303. revert: true,
  304. handle: desktop.isMobile ? "img" : null,
  305. start: function() {
  306. rendered.zIndex(1)
  307. rendered.css('opacity', '0.8')
  308. },
  309. stop: function() {
  310. rendered.zIndex(0)
  311. rendered.css('opacity', '1')
  312. }
  313. })
  314. rendered.droppable({
  315. drop: function( event, ui ) {
  316. // swap position rendered with ui using jquery
  317. var target = plugin.getPerformanceOptions()
  318. var draggedPlugin = ui.draggable.data('plugin')
  319. var source = draggedPlugin.getPerformanceOptions();
  320. const tmp = target.index
  321. target.index = source.index
  322. source.index = tmp
  323. self.data('updatePlugins')()
  324. self.data("selectElement")(draggedPlugin)
  325. // let the host know about this change
  326. desktop.pedalboard.data('performancePluginIndexSet')(plugin.instance, target.index)
  327. desktop.pedalboard.data('performancePluginIndexSet')(draggedPlugin.instance, source.index)
  328. }
  329. })
  330. const iconFavorite = rendered.find('.icon-visible')
  331. if (iconFavorite) {
  332. if (visibleFilter === false) {
  333. iconFavorite.removeClass('hidden')
  334. if (plugin.getPerformanceOptions()?.visible ?? false) {
  335. iconFavorite.addClass("icon-eye")
  336. iconFavorite.addClass("is-visible")
  337. } else {
  338. iconFavorite.addClass("icon-eye-off")
  339. }
  340. iconFavorite.click(function(e) {
  341. const performanceOptions = plugin.getPerformanceOptions()
  342. const visible = !(performanceOptions.visible ?? false);
  343. if (visible) {
  344. iconFavorite.removeClass('icon-eye-off')
  345. iconFavorite.addClass("icon-eye")
  346. iconFavorite.addClass("is-visible")
  347. } else {
  348. iconFavorite.addClass('icon-eye-off')
  349. iconFavorite.removeClass("icon-eye")
  350. iconFavorite.removeClass("is-visible")
  351. }
  352. //console.log(`plugin ${plugin.instance} toggle visible: ${visible}`)
  353. performanceOptions.visible = visible
  354. // let the host know about this change
  355. desktop.pedalboard.data('performancePluginVisibilitySet')(plugin.instance, visible)
  356. e.stopPropagation()
  357. })
  358. } else {
  359. iconFavorite.addClass('hidden')
  360. }
  361. }
  362. const renameBox = rendered.find('.label-container')
  363. renameBox?.click(function() {
  364. desktop.openInputBoxWindow('Rename', plugin.label || plugin.effect.name, function(newName, cancelled) {
  365. if (cancelled)
  366. return
  367. plugin.setLabel(newName)
  368. // update the UI label & settings label
  369. rendered?.find('.title')?.text(plugin.label || plugin.effect.label)
  370. },
  371. 'Rename',
  372. function(value) {
  373. return true; // always valid
  374. })
  375. })
  376. renameBox?.find('.icon')?.removeClass('mod-hidden')
  377. }
  378. rendered.click(function () {
  379. self.data('selectElement')(plugin)
  380. })
  381. container.append(rendered)
  382. return rendered
  383. }
  384. })