| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | local lub = require 'lub' |
| | local yaml = require 'yaml' |
| |
|
| | |
| | |
| | yaml.configure { |
| | sort_table_keys = true, |
| | } |
| |
|
| | local lib = lub.class('lub.Param') |
| |
|
| | |
| | local ProxyMt = {} |
| |
|
| | |
| | local CtrlMt = {} |
| |
|
| | local private = {} |
| |
|
| | |
| | |
| | |
| |
|
| | |
| |
|
| | |
| | |
| | |
| | function lib.new(filepath) |
| | if not filepath then |
| | filepath = lub.path('&', 3) |
| | if not filepath then |
| | |
| | filepath = lub.path('&', 4) |
| | end |
| | filepath = filepath..'.yml' |
| | end |
| |
|
| | local self = { |
| | preset = 'p1', |
| | presets = {}, |
| | filepath = filepath, |
| | proxies = {}, |
| | controls = {}, |
| | mappings = {}, |
| | rmappings = {}, |
| | } |
| | setmetatable(self, lib) |
| | private.loadpath(self, self.filepath) |
| | return self |
| | end |
| |
|
| | |
| |
|
| | |
| | |
| | |
| | |
| | |
| | function lib:proxy(original_table, proxy_name, settings) |
| | local settings = settings or {} |
| | local proxy = setmetatable({ |
| | __param = self, |
| | __storage = {}, |
| | __original = original_table, |
| | __name = proxy_name or 'main', |
| | }, ProxyMt) |
| | self.proxies[proxy.__name] = proxy |
| | local proxy_data = (self.presets[self.preset] or {})[proxy.__name] |
| | if proxy_data then |
| | for key, value in pairs(proxy_data) do |
| | self:setValue(proxy, key, value) |
| | end |
| | end |
| | return proxy |
| | end |
| |
|
| | |
| | function lib:setValue(proxy, key, value, controller) |
| | local storage = proxy.__storage |
| | local old_value = storage[key] |
| | |
| | |
| | proxy.__original[key] = value |
| |
|
| | |
| | if old_value ~= value then |
| | |
| | rawset(storage, key, value) |
| | if self.learn_next then |
| | self.lchange = {proxy_name = proxy.__name, key = key} |
| | self.learn_next = false |
| | end |
| | private.notifyControls(self, proxy.__name, key, value, controller) |
| | end |
| | end |
| |
|
| | |
| | |
| | function lib:control(ctrl, key, value) |
| | |
| | local rmapping = (self.rmappings[ctrl.name] or {}) |
| | if not rmapping then return end |
| |
|
| | local rmap = rmapping[key] |
| | if not rmap then return end |
| |
|
| | local proxy = self.proxies[rmap.proxy_name] |
| | if not rmap then return end |
| |
|
| | self:setValue(proxy, rmap.key, value, ctrl) |
| | end |
| |
|
| | |
| | |
| | function lib:mapParameter(ctrl_name, ctrl_key, proxy_name, key) |
| |
|
| | if not proxy_name then |
| | local lchange = self.lchange |
| | if not lchange then |
| | print("missing 'last changed' parameter. Cannot learn mapping.") |
| | return |
| | end |
| |
|
| | proxy_name = lchange.proxy_name |
| | key = lchange.key |
| | end |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| | |
| | lub.deepMerge(self.mappings, proxy_name, { |
| | [key] = { |
| | [ctrl_name] = ctrl_key |
| | } |
| | }) |
| |
|
| | |
| | |
| | |
| | |
| | |
| | lub.deepMerge(self.rmappings, ctrl_name, { |
| | [ctrl_key] = { |
| | proxy_name = proxy_name, |
| | key = key, |
| | } |
| | }) |
| |
|
| | private.save(self) |
| | end |
| |
|
| | |
| | function lib:dump() |
| | return yaml.dump({ |
| | preset = self.preset, |
| | presets = self.presets, |
| | mappings = self.mappings, |
| | }) |
| | end |
| |
|
| | |
| | function lib:savePreset() |
| | local preset = self.presets[self.preset] |
| | if not preset then |
| | preset = {} |
| | self.presets[self.preset] = preset |
| | end |
| | for proxy_name, proxy in pairs(self.proxies) do |
| | local tbl = preset[proxy_name] |
| | if not tbl then |
| | tbl = {} |
| | preset[proxy_name] = tbl |
| | end |
| | for k, value in pairs(proxy.__storage) do |
| | tbl[k] = value |
| | end |
| | end |
| |
|
| | |
| | private.save(self) |
| | end |
| |
|
| | |
| | |
| | |
| | function lib:selectPreset(preset_name) |
| | if not preset_name then |
| | print('Cannot use nil as preset name !') |
| | return |
| | end |
| | if type(preset_name) == 'number' then |
| | preset_name = 'p'..preset_name |
| | end |
| | local preset_data = self.presets[preset_name] |
| | if preset_data then |
| | |
| | for proxy_name, proxy_data in pairs(preset_data) do |
| | local proxy = self.proxies[proxy_name] |
| | if proxy then |
| | for key, value in pairs(proxy_data) do |
| | self:setValue(proxy, key, value) |
| | end |
| | end |
| | end |
| |
|
| | else |
| | |
| | end |
| | self.preset = preset_name |
| | end |
| |
|
| | |
| | |
| | function lib:copyToPreset(preset_name) |
| | if type(preset_name) == 'number' then |
| | preset_name = 'p'..preset_name |
| | end |
| | self.preset = preset_name |
| | self:savePreset() |
| | end |
| |
|
| |
|
| | |
| |
|
| | |
| | |
| | |
| | |
| | function lib:addController(name, settings) |
| | local ctrl = { |
| | name = name, |
| | min = settings.min or 0, |
| | max = settings.max or 1, |
| | param = self, |
| | learn_next = false, |
| | } |
| | ctrl.range = ctrl.max - ctrl.min |
| | assert(ctrl.range ~= 0, 'min and max values should be different') |
| | self.controls[name] = ctrl |
| | return setmetatable(ctrl, CtrlMt) |
| | end |
| |
|
| | |
| |
|
| | |
| | function ProxyMt.__index(proxy, key) |
| | return proxy.__original[key] |
| | end |
| |
|
| | function ProxyMt.__newindex(proxy, key, value) |
| | proxy.__param:setValue(proxy, key, value) |
| | end |
| |
|
| | |
| |
|
| | CtrlMt.__index = CtrlMt |
| |
|
| | function CtrlMt:learn() |
| | self.learn_next = true |
| | self.param.learn_next = true |
| | end |
| |
|
| | function CtrlMt:__call(key, cvalue) |
| | local param = self.param |
| | if self.learn_next then |
| | param:mapParameter(self.name, key) |
| | self.learn_next = false |
| | end |
| |
|
| | local value = (cvalue - self.min) / self.range |
| | param:control(self, key, value) |
| | end |
| |
|
| | |
| | |
| | |
| | |
| |
|
| | |
| | function private:loadpath(filepath) |
| | if lub.exist(filepath) then |
| | local data = yaml.loadpath(filepath) |
| | self.presets = data.presets or {} |
| | self.mappings = data.mappings or {} |
| | self.rmappings = {} |
| | local rmappings = self.rmappings |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | for proxy_name, proxy_mappings in pairs(self.mappings) do |
| | for key, mappings in pairs(proxy_mappings) do |
| | for ctrl_name, ctrl_key in pairs(mappings) do |
| | |
| | lub.deepMerge(rmappings, ctrl_name, { |
| | [ctrl_key] = { |
| | proxy_name = proxy_name, |
| | key = key, |
| | } |
| | }) |
| | end |
| | end |
| | end |
| |
|
| | if data.preset then |
| | self:selectPreset(data.preset) |
| | end |
| | end |
| | end |
| |
|
| | function private:save() |
| | lub.writeall(self.filepath, self:dump()) |
| | end |
| |
|
| | function private:notifyControls(proxy_name, key, value, skip_control) |
| | local proxy_mappings = self.mappings[proxy_name] |
| | if proxy_mappings then |
| | local mappings = proxy_mappings[key] |
| | if mappings then |
| | local controls = self.controls |
| | for ctrl_name, ctrl_key in pairs(mappings) do |
| | local ctrl = controls[ctrl_name] |
| | if ctrl and ctrl ~= skip_control then |
| | local fun = ctrl.changed |
| | if fun then |
| | fun(ctrl, ctrl_key, ctrl.min + (value * ctrl.range)) |
| | end |
| | else |
| | |
| | end |
| | end |
| | end |
| | end |
| | end |
| |
|
| | return lib |
| |
|