From 1d9f4377b120221688e0f9ab06a8e70d6834fbd6 Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Tue, 8 Feb 2022 10:18:26 -0700 Subject: [PATCH] added boundValue to bluemath.js --- libs/bluemath.js | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/libs/bluemath.js b/libs/bluemath.js index f0c392e..4b0f0b9 100644 --- a/libs/bluemath.js +++ b/libs/bluemath.js @@ -125,3 +125,28 @@ class bound { return valueArray.map((value,index) => Math.min(Math.max(value,this.min[index]),this.max[index])); } } + +class boundValue { + constructor (bounds=new bound(),value=0) { + this.bounds = bounds instanceof bound ? bounds : new bound(); + this.value = !isNaN(value) ? value : 0; + } + + setValue(value=0) { + value = !isNaN(value) ? value : 0; + return this.value = this.bounds.clamp([value])[0]; + } + + getValue() { + return this.bounds.clamp([this.value])[0]; + } + + getValuePercent() { + return this.bounds.normalize([this.value])[0]; + } + + modValue(mod=0) { + mod = !isNaN(mod) ? mod : 0; + return this.setValue(this.value+mod); + } +}