From 3a2ed39b5145963bb8b72b55e6b5d3d3f30e9775 Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Mon, 27 Jan 2014 19:23:35 +0000 Subject: [PATCH] Add new Range Node --- nodes/core/logic/16-range.html | 78 +++++++++++++++++++++++++++++++++ nodes/core/logic/16-range.js | 47 ++++++++++++++++++++ public/icons/range.png | Bin 0 -> 500 bytes 3 files changed, 125 insertions(+) create mode 100644 nodes/core/logic/16-range.html create mode 100644 nodes/core/logic/16-range.js create mode 100644 public/icons/range.png diff --git a/nodes/core/logic/16-range.html b/nodes/core/logic/16-range.html new file mode 100644 index 000000000..79e94f688 --- /dev/null +++ b/nodes/core/logic/16-range.html @@ -0,0 +1,78 @@ + + + + + + + diff --git a/nodes/core/logic/16-range.js b/nodes/core/logic/16-range.js new file mode 100644 index 000000000..6c68fccfb --- /dev/null +++ b/nodes/core/logic/16-range.js @@ -0,0 +1,47 @@ +/** + * Copyright 2013 IBM Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + **/ + +var RED = require(process.env.NODE_RED_HOME + "/red/red"); + +function RangeNode(n) { + RED.nodes.createNode(this, n); + this.action = n.action; + this.round = n.round || false; + this.minin = Number(n.minin); + this.maxin = Number(n.maxin); + this.minout = Number(n.minout); + this.maxout = Number(n.maxout); + var node = this; + + this.on('input', function (msg) { + var n = Number(msg.payload); + if (!isNaN(n)) { + if (node.action == "clamp") { + if (n < node.minin) { n = node.minin; } + if (n > node.maxin) { n = node.maxin; } + } + if (node.action == "roll") { + if (n >= node.maxin) { n = (n - node.minin) % (node.maxin - node.minin) + node.minin; } + if (n < node.minin) { n = (n - node.minin) % (node.maxin - node.minin) + node.maxin; } + } + msg.payload = ((n - node.minin) / (node.maxin - node.minin) * (node.maxout - node.minout)) + node.minout; + if (node.round) { msg.payload = Math.round(msg.payload); } + node.send(msg); + } + else { node.log("Not a number: "+msg.payload); } + }); +} +RED.nodes.registerType("range", RangeNode); diff --git a/public/icons/range.png b/public/icons/range.png new file mode 100644 index 0000000000000000000000000000000000000000..f4a71485d6255461b4fa8c3f77927241437dbd15 GIT binary patch literal 500 zcmVP001Zm1^@s63(rw&00001b5ch_0Itp) z=>Px#24YJ`L;wH)0002_L%V+f000SaNLh0L01FcU01FcV0GgZ_00007bV*G`2i*)0 z4Fed@egMD#00DGKL_t(Y$IX||PQx$^#(!z2v2GXsNfQVpBslRFy#GURKmsOln^=LR z4HtLHx^+pLWSXVOY8A`={Mm`4j4`B^j3dZVoQwkP4WLAKi3&k-CwiPJUAMqnYEIro zzW{%(&1ygiKLD?dy~RybPCi86`x*>EVp=GoA<2a7My_HvV-C!PY_qY`dMjFMJpvP; zXv*7_t{j+31^tSX8L;vQej@fu7m`ch+j0P~s;Ljz%z;`0S^A{w2dFD5tiE{8cFl~n zK3m`3)wQXpdVgeI)nyK>MxHc!&=JufIT#6GzCc}DKPzE1N}mWD>KwgOc72f)KpA+i zIFZam-^l6|NBIupM5EQnx