From a45b364a80b1373ab8645319714afe8062230e59 Mon Sep 17 00:00:00 2001 From: bluesaxman Date: Thu, 10 Nov 2022 13:30:20 -0700 Subject: [PATCH] Added outOfBoundsBy method to bound, and modified modValue of boundValue, this change breaks previous useages of boundValue --- libs/bluemath.js | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/libs/bluemath.js b/libs/bluemath.js index 2530997..c1c12a2 100644 --- a/libs/bluemath.js +++ b/libs/bluemath.js @@ -191,6 +191,20 @@ class bound { if (valueArray.length != this.min.length) { return [...this.min.map((v,i) => Math.min(Math.max(0, v), this.max[i]))]; } return valueArray.map((value,index) => Math.min(Math.max(value,this.min[index]),this.max[index])); } + + outOfBoundsBy(valueArray=[...this.min.map(v => 0)]) { + if (this.inbounds(valueArray)) { + return [...this.min.map(e=>0)]; + } else { + return this.min.map((value,index)=>{ + if (undefined == valueArray[index]) { return NaN } + if (valueArray[index] < value) { return -(value - valueArray[index]) } + if (valueArray[index] > this.max[index]) { return valueArray[index] - this.max[index] } + return 0; + }); + } + } + } class boundValue { @@ -214,7 +228,8 @@ class boundValue { modValue(mod=0) { mod = !isNaN(mod) ? mod : 0; - return this.setValue(this.value+mod); + var outOfBoundsBy = this.bounds.outOfBoundsBy([this.value+mod]); + return {newValue:this.setValue(this.value+mod),remainder:outOfBoundsBy}; } }