Starsector API
Loading...
Searching...
No Matches
FaderUtil.java
Go to the documentation of this file.
1package com.fs.starfarer.api.util;
2
3
4public class FaderUtil {
5
6 public static enum State {IN, OUT, IDLE};
7
8 private float currBrightness;
9 private float durationIn, durationOut;
10 private State state;
11
12 private boolean bounceDown = false;
13 private boolean bounceUp = false;
14
15 public FaderUtil(float currBrightness, float duration) {
16 this.currBrightness = currBrightness;
17 this.durationIn = duration;
18 this.durationOut = duration;
19 state = State.IDLE;
20 }
21
22 public FaderUtil(float currBrightness, float durationIn, float durationOut) {
23 this.currBrightness = currBrightness;
24 this.durationIn = durationIn;
25 this.durationOut = durationOut;
26 state = State.IDLE;
27 }
28
29 public FaderUtil(float currBrightness, float durationIn, float durationOut, boolean bounceUp, boolean bounceDown) {
30 this(currBrightness, durationIn, durationOut);
31 setBounce(bounceUp, bounceDown);
32 }
33
34 public State getState() {
35 return state;
36 }
37
38 public void setState(State state) {
39 this.state = state;
40 }
41
42 public void setBounceDown(boolean bounceDown) {
43 this.bounceDown = bounceDown;
44 }
45
46 public void setBounceUp(boolean bounceUp) {
47 this.bounceUp = bounceUp;
48 }
49
50 public void setBounce(boolean up, boolean down) {
51 this.bounceUp = up;
52 this.bounceDown = down;
53 }
54
55 public void forceIn() {
56 currBrightness = 1;
57 if (bounceDown) state = State.OUT;
58 else state = State.IDLE;
59 }
60
61 public void forceOut() {
62 currBrightness = 0;
63 if (bounceUp) state = State.IN;
64 else state = State.IDLE;
65 }
66
67 public void fadeIn() {
68 if (durationIn <= 0) {
69 forceIn();
70 } else {
71 state = State.IN;
72 }
73 }
74
75 public void fadeOut() {
76 if (durationOut <= 0) {
77 forceOut();
78 } else {
79 state = State.OUT;
80 }
81 }
82
83 public boolean isFadedOut() {
84 return getBrightness() == 0 && (isIdle() || isFadingOut());
85 }
86
87 public boolean isFadedIn() {
88 return getBrightness() == 1 && isIdle();
89 }
90
91 public boolean isFadingOut() {
92 return state == State.OUT;
93 }
94
95 public boolean isFadingIn() {
96 return state == State.IN;
97 }
98
99 public boolean isIdle() {
100 return state == State.IDLE;
101 }
102
103 public void advance(float amount) {
104 if (state == State.IDLE) return;
105
106 if (state == State.IN) {
107 if (currBrightness == 1) {
108 if (bounceDown) state = State.OUT;
109 else state = State.IDLE;
110 return;
111 }
112 float delta = amount / durationIn;
113 currBrightness += delta;
114 if (currBrightness > 1) {
115 currBrightness = 1;
116 }
117 } else if (state == State.OUT) {
118 if (currBrightness == 0) {
119 if (bounceUp) state = State.IN;
120 else state = State.IDLE;
121 return;
122 }
123 float delta = amount / durationOut;
124 currBrightness -= delta;
125 if (currBrightness < 0) {
126 currBrightness = 0;
127 }
128 }
129 }
130
131 public void setDurationIn(float durationIn) {
132 this.durationIn = durationIn;
133 }
134
135 public void setDurationOut(float durationOut) {
136 this.durationOut = durationOut;
137 }
138
139 public FaderUtil setDuration(float in, float out) {
140 this.durationIn = in;
141 this.durationOut = out;
142 return this;
143 }
144
145 public float getBrightness() {
146 return currBrightness;
147 }
148
149 public float getDurationIn() {
150 return durationIn;
151 }
152
153 public float getDurationOut() {
154 return durationOut;
155 }
156
157 public void setBrightness(float brightness) {
158 this.currBrightness = brightness;
159 }
160
161 @Override
162 public String toString() {
163 return String.format("%s, curr: %f, in: %f, out: %f, up: %b, down: %b", state.name(),
164 currBrightness, durationIn, durationOut,
165 bounceUp, bounceDown);
166 }
167
168 public boolean isBounceDown() {
169 return bounceDown;
170 }
171
172 public boolean isBounceUp() {
173 return bounceUp;
174 }
175
176
177
178}
FaderUtil(float currBrightness, float durationIn, float durationOut)
void setBounceUp(boolean bounceUp)
void setBounceDown(boolean bounceDown)
FaderUtil setDuration(float in, float out)
FaderUtil(float currBrightness, float durationIn, float durationOut, boolean bounceUp, boolean bounceDown)
FaderUtil(float currBrightness, float duration)
void setBrightness(float brightness)
void setDurationOut(float durationOut)
void setDurationIn(float durationIn)
void setBounce(boolean up, boolean down)