From 3e9a56e3e8898e41101e4eac2bf65f81f4dac4c3 Mon Sep 17 00:00:00 2001 From: jean-emmanuel Date: Fri, 26 Jan 2018 11:05:39 +0100 Subject: [PATCH] osc: add support for osc typetag in widget's precision property (issue #137) --- CHANGELOG.md | 1 + app/main/osc.js | 8 ++++++-- resources/docs/widgets/widgets.md | 8 +++++--- src/browser/js/app/managers/widgets.js | 4 ++-- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e345d47f..5b0acb2e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ - switch: added `showValues` to display both values and labels in the buttons; array values can now be selected via osc - rgb: **removed** `touchAddress` option - labels/texts: multiple white spaces are no longer merged + - widgets' `precision` option allows setting a specific data type for numbers (sliders can send doubles !) - misc - event system rewritten - js build size reduced diff --git a/app/main/osc.js b/app/main/osc.js index 6c6fbed9..74f98b6e 100644 --- a/app/main/osc.js +++ b/app/main/osc.js @@ -38,11 +38,15 @@ var parseType = function(type){ } -var parseArg = function(arg,precision){ +var parseArg = function(arg, precision){ if (arg==null) return null switch (typeof arg) { case 'number': - return {type: precision == 0 ? 'i' : 'f', value: parseFloat(arg.toFixed(precision))} + var typeTagMatch = typeof precision == 'string' ? precision.match(/[0-9]+([a-zA-Z]{1})/) : null, + typeTag = typeTagMatch === null ? precision == 0 ? 'i' : 'f' : typeTagMatch[1], + precisionValue = parseFloat(precision) || 0 + + return {type: typeTag, value: parseFloat(arg.toFixed(precisionValue))} case 'boolean': return {type: arg ? 'T' : 'F', value: arg} case 'object': diff --git a/resources/docs/widgets/widgets.md b/resources/docs/widgets/widgets.md index 8ae3fb59..75432489 100644 --- a/resources/docs/widgets/widgets.md +++ b/resources/docs/widgets/widgets.md @@ -66,9 +66,11 @@ All widgets share a set of generic properties described below. ### `precision` -- type: `integer` +- type: `integer|string` - default: `2` -- usage: the `precision` property defines the number of decimals to display and to send. Set to `0` to send `integers` only. +- usage: the `precision` property defines the number of decimals to display and to send. + - set to `0` to send `integers` only. + - a specific data type can be specified by appending a valid osc type tag to the precision value, for example : `3d` will make the widget send double precision numbers rounded to three decimals ### `target` - type: `array` @@ -100,7 +102,7 @@ All widgets share a set of generic properties described below. - default: `[]` - usage: - each element of the `array` defines a constant value that will be prepended to the osc message - - values can be defined as objects if the type needs to be specified (ie different from the default implied by the `precision`) + - values can be defined as objects if the osc data type needs to be specified (ie different from the default implied by the `precision` option) - example: ```js preArgs: [ diff --git a/src/browser/js/app/managers/widgets.js b/src/browser/js/app/managers/widgets.js index b214c654..8cf073d3 100644 --- a/src/browser/js/app/managers/widgets.js +++ b/src/browser/js/app/managers/widgets.js @@ -24,7 +24,7 @@ var WidgetManager = class WidgetManager extends EventEmitter { ipc.send('addWidget', { hash:hash, data:{ - precision: this.widgets[hash].precision, + precision: this.widgets[hash].getProp('precision'), preArgs: this.widgets[hash].getProp('preArgs'), split: this.widgets[hash].split, target: this.widgets[hash].getProp('target'), @@ -107,7 +107,7 @@ var WidgetManager = class WidgetManager extends EventEmitter { ipc.send('addWidget', { hash:hash, data:{ - precision: widget.precision, + precision: widget.getProp('precision'), preArgs: widget.getProp('preArgs'), split: widget.split, target: widget.getProp('target'),