Skip to content

Commit

Permalink
Add "Hold Delay" feature to Animation panel
Browse files Browse the repository at this point in the history
  • Loading branch information
grillo-delmal committed May 22, 2024
1 parent c37af99 commit d7eef9c
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 3 deletions.
140 changes: 137 additions & 3 deletions source/session/animation/package.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
module session.animation;

public import session.tracking;
import inochi2d;
import inochi2d.core.animation;
import inochi2d.core.animation.player;
import fghj;
Expand Down Expand Up @@ -95,6 +96,114 @@ private:
false);
}

bool playHoldCheck(float src){
return defaultThresholds ?
(src >= 1) :
((playThresholdDir & ThresholdDir.Up) && (src >= playThresholdValue) ?
true :
((playThresholdDir & ThresholdDir.Down) && (src <= playThresholdValue)) ?
true :
false);
}

bool stopHoldCheck(float src){
return defaultThresholds ?
(src <= 0) :
((stopThresholdDir & ThresholdDir.Up) && (src >= stopThresholdValue) ?
true :
((stopThresholdDir & ThresholdDir.Down) && (src <= stopThresholdValue)) ?
true :
false);
}

bool fullStopHoldCheck(float src){
return defaultThresholds ?
(src <= -1) :
((fullStopThresholdDir & ThresholdDir.Up) && (src >= fullStopThresholdValue) ?
true :
((fullStopThresholdDir & ThresholdDir.Down) && (src <= fullStopThresholdValue)) ?
true :
false);
}

bool playHoldTest(float src) {
return playHoldCheck(src) && playThresholdHoldTime >= playThresholdHoldDelay;
}

bool stopHoldTest(float src) {
return stopHoldCheck(src) && stopThresholdHoldTime >= stopThresholdHoldDelay;
}

bool fullStopHoldTest(float src) {
return fullStopHoldCheck(src) && fullStopThresholdHoldTime >= fullStopThresholdHoldDelay;
}

void updateHold(float src){
if(useHoldDelay && playThresholdHoldDelay > 0) {
if(playThresholdHoldTime == 0)
{
if(playTest(src)) {
playThresholdHoldTime += cast(int)(deltaTime() * 1000);
}
else {
playThresholdHoldTime = 0;
}
}
else
{
if(playHoldCheck(src)) {
playThresholdHoldTime += cast(int)(deltaTime() * 1000);
}
else {
playThresholdHoldTime = 0;
}
}
}

if(useHoldDelay && stopThresholdHoldDelay > 0) {
if(stopThresholdHoldTime == 0)
{
if(stopTest(src)) {
stopThresholdHoldTime += cast(int)(deltaTime() * 1000);
}
else {
stopThresholdHoldTime = 0;
}
}
else
{
if(stopHoldCheck(src)) {
stopThresholdHoldTime += cast(int)(deltaTime() * 1000);
}
else {
stopThresholdHoldTime = 0;
}
}
}

if(useHoldDelay && fullStopThresholdHoldDelay > 0) {
if(fullStopThresholdHoldTime == 0)
{
if(fullStopTest(src)) {
fullStopThresholdHoldTime += cast(int)(deltaTime() * 1000);
}
else {
fullStopThresholdHoldTime = 0;
}
}
else
{
if(fullStopHoldCheck(src)) {
fullStopThresholdHoldTime += cast(int)(deltaTime() * 1000);
}
else {
fullStopThresholdHoldTime = 0;
}

}
}
}

bool playEventTest(TriggerEvent event){
return cast(bool) (event & playEvent);
}
Expand All @@ -119,6 +228,7 @@ public:
SourceType sourceType;

bool defaultThresholds = true;
bool useHoldDelay = false;

float playThresholdValue = 1;
float stopThresholdValue = 0;
Expand All @@ -128,6 +238,14 @@ public:
ThresholdDir stopThresholdDir = ThresholdDir.Down;
ThresholdDir fullStopThresholdDir = ThresholdDir.Down;

int playThresholdHoldDelay = 0;
int stopThresholdHoldDelay = 0;
int fullStopThresholdHoldDelay = 0;

int playThresholdHoldTime = 0;
int stopThresholdHoldTime = 0;
int fullStopThresholdHoldTime = 0;

// EventBidning
BitFlags!TriggerEvent playEvent = TriggerEvent.None;
BitFlags!TriggerEvent stopEvent = TriggerEvent.None;
Expand Down Expand Up @@ -164,6 +282,8 @@ public:

serializer.putKey("defaultThresholds");
serializer.putValue(defaultThresholds);
serializer.putKey("useHoldDelay");
serializer.putValue(useHoldDelay);

serializer.putKey("playThresholdValue");
serializer.putValue(playThresholdValue);
Expand All @@ -172,6 +292,13 @@ public:
serializer.putKey("fullStopThresholdValue");
serializer.putValue(fullStopThresholdValue);

serializer.putKey("playThresholdHoldDelay");
serializer.putValue(playThresholdHoldDelay);
serializer.putKey("stopThresholdHoldDelay");
serializer.putValue(stopThresholdHoldDelay);
serializer.putKey("fullStopThresholdHoldDelay");
serializer.putValue(fullStopThresholdHoldDelay);

serializer.putKey("playThresholdDir");
serializer.serializeValue(playThresholdDir);
serializer.putKey("stopThresholdDir");
Expand Down Expand Up @@ -204,11 +331,16 @@ public:
data["sourceType"].deserializeValue(sourceType);

data["defaultThresholds"].deserializeValue(defaultThresholds);
if (!data["useHoldDelay"].isEmpty) data["useHoldDelay"].deserializeValue(useHoldDelay);

data["playThresholdValue"].deserializeValue(playThresholdValue);
data["stopThresholdValue"].deserializeValue(stopThresholdValue);
data["fullStopThresholdValue"].deserializeValue(fullStopThresholdValue);

if (!data["playThresholdHoldDelay"].isEmpty) data["playThresholdHoldDelay"].deserializeValue(playThresholdHoldDelay);
if (!data["stopThresholdHoldDelay"].isEmpty) data["stopThresholdHoldDelay"].deserializeValue(stopThresholdHoldDelay);
if (!data["fullStopThresholdHoldDelay"].isEmpty) data["fullStopThresholdHoldDelay"].deserializeValue(fullStopThresholdHoldDelay);

data["playThresholdDir"].deserializeValue(playThresholdDir);
data["stopThresholdDir"].deserializeValue(stopThresholdDir);
data["fullStopThresholdDir"].deserializeValue(fullStopThresholdDir);
Expand Down Expand Up @@ -350,14 +482,16 @@ public:
break;
}

updateHold(src);

// Check if need to trigger change
if (!anim.playing || anim.paused) {
// Test for play
if(playTest(src)) anim.play(loop);
if(!useHoldDelay || playThresholdHoldDelay == 0 ? playTest(src) : playHoldTest(src)) anim.play(loop);
} else {
// Test for Stop
if(fullStopTest(src)) anim.stop(true);
else if(stopTest(src)) anim.stop(false);
if(!useHoldDelay || fullStopThresholdHoldDelay == 0 ? fullStopTest(src) : fullStopHoldTest(src)) anim.stop(true);
else if(!useHoldDelay || stopThresholdHoldDelay == 0 ? stopTest(src) : stopHoldTest(src)) anim.stop(false);
}

//Set latest inVal
Expand Down
47 changes: 47 additions & 0 deletions source/session/panels/animations.d
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ private:

void trackingOptions(AnimationControl ac){
float default_val;
int default_hold_val = 0;
bool hasTrackingSrc = ac.sourceName.length > 0;
uiImIndent();

Expand Down Expand Up @@ -161,6 +162,10 @@ private:
uiImProgress(ac.inValToBindingValue(), vec2(-float.min_normal, 0), "");
uiImDummy(vec2(0, 8));
uiImCheckbox(__("Default Thresholds"), ac.defaultThresholds);
uiImSameLine(0, 0);
uiImDummy(vec2(10, 5));
uiImSameLine(0, 0);
uiImCheckbox(__("Use Hold Delay"), ac.useHoldDelay);

if(ac.defaultThresholds){
igBeginDisabled();
Expand Down Expand Up @@ -195,6 +200,20 @@ private:
if(ac.playThresholdDir < ThresholdDir.Both) ac.playThresholdDir += 1;
else ac.playThresholdDir = ThresholdDir.None;
}
if(ac.useHoldDelay){
if(ac.playThresholdDir == ThresholdDir.None || ac.playThresholdDir == ThresholdDir.Both){
igBeginDisabled();
}
uiImLabel(_("Hold Delay (ms):"));
uiImPush(0);
uiImDrag(
ac.defaultThresholds || ac.playThresholdDir == ThresholdDir.None || ac.playThresholdDir == ThresholdDir.Both ?
default_hold_val : ac.playThresholdHoldDelay, 0, int.max);
uiImPop();
if(ac.playThresholdDir == ThresholdDir.None || ac.playThresholdDir == ThresholdDir.Both){
igEndDisabled();
}
}

uiImUnindent();
uiImPop();
Expand Down Expand Up @@ -229,6 +248,20 @@ private:
if(ac.stopThresholdDir < ThresholdDir.Both) ac.stopThresholdDir += 1;
else ac.stopThresholdDir = ThresholdDir.None;
}
if(ac.useHoldDelay){
if(ac.stopThresholdDir == ThresholdDir.None || ac.stopThresholdDir == ThresholdDir.Both){
igBeginDisabled();
}
uiImLabel(_("Hold Delay (ms):"));
uiImPush(0);
uiImDrag(
ac.defaultThresholds || ac.stopThresholdDir == ThresholdDir.None || ac.stopThresholdDir == ThresholdDir.Both ?
default_hold_val : ac.stopThresholdHoldDelay, 0, int.max);
uiImPop();
if(ac.stopThresholdDir == ThresholdDir.None || ac.stopThresholdDir == ThresholdDir.Both){
igEndDisabled();
}
}
uiImUnindent();
uiImPop();

Expand Down Expand Up @@ -262,6 +295,20 @@ private:
if(ac.fullStopThresholdDir < ThresholdDir.Both) ac.fullStopThresholdDir += 1;
else ac.fullStopThresholdDir = ThresholdDir.None;
}
if(ac.useHoldDelay){
if(ac.fullStopThresholdDir == ThresholdDir.None || ac.fullStopThresholdDir == ThresholdDir.Both){
igBeginDisabled();
}
uiImLabel(_("Hold Delay (ms):"));
uiImPush(0);
uiImDrag(
ac.defaultThresholds || ac.fullStopThresholdDir == ThresholdDir.None || ac.fullStopThresholdDir == ThresholdDir.Both ?
default_hold_val : ac.fullStopThresholdHoldDelay, 0, int.max);
uiImPop();
if(ac.fullStopThresholdDir == ThresholdDir.None || ac.fullStopThresholdDir == ThresholdDir.Both){
igEndDisabled();
}
}
uiImUnindent();
uiImPop();
if(ac.defaultThresholds){
Expand Down

0 comments on commit d7eef9c

Please sign in to comment.