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); + } +}