diff options
Diffstat (limited to 'src/ui/util.c')
-rw-r--r-- | src/ui/util.c | 90 |
1 files changed, 48 insertions, 42 deletions
diff --git a/src/ui/util.c b/src/ui/util.c index 38124b22..27950c5e 100644 --- a/src/ui/util.c +++ b/src/ui/util.c | |||
@@ -135,54 +135,15 @@ iBool isFinished_Anim(const iAnim *d) { | |||
135 | } | 135 | } |
136 | 136 | ||
137 | void init_Anim(iAnim *d, float value) { | 137 | void init_Anim(iAnim *d, float value) { |
138 | d->due = d->when = SDL_GetTicks(); // frameTime_Window(get_Window()); | 138 | d->due = d->when = SDL_GetTicks(); |
139 | d->from = d->to = value; | 139 | d->from = d->to = value; |
140 | d->flags = 0; | 140 | d->flags = 0; |
141 | } | 141 | } |
142 | 142 | ||
143 | void setValue_Anim(iAnim *d, float to, uint32_t span) { | ||
144 | if (fabsf(to - d->to) > 0.00001f) { | ||
145 | const uint32_t now = SDL_GetTicks(); | ||
146 | d->from = value_Anim(d); | ||
147 | d->to = to; | ||
148 | d->when = now; | ||
149 | d->due = now + span; | ||
150 | } | ||
151 | } | ||
152 | |||
153 | iLocalDef float pos_Anim_(const iAnim *d, uint32_t now) { | 143 | iLocalDef float pos_Anim_(const iAnim *d, uint32_t now) { |
154 | return (float) (now - d->when) / (float) (d->due - d->when); | 144 | return (float) (now - d->when) / (float) (d->due - d->when); |
155 | } | 145 | } |
156 | 146 | ||
157 | void setValueEased_Anim(iAnim *d, float to, uint32_t span) { | ||
158 | if (fabsf(to - d->to) <= 0.00001f) { | ||
159 | d->to = to; /* Pretty much unchanged. */ | ||
160 | return; | ||
161 | } | ||
162 | const uint32_t now = SDL_GetTicks(); | ||
163 | if (isFinished_Anim(d)) { | ||
164 | d->from = d->to; | ||
165 | d->when = now; | ||
166 | d->flags = easeBoth_AnimFlag; | ||
167 | } | ||
168 | else { | ||
169 | d->from = value_Anim(d); | ||
170 | d->when = frameTime_Window(get_Window()); /* to match the timing of value_Anim */ | ||
171 | d->flags = easeOut_AnimFlag; | ||
172 | } | ||
173 | d->to = to; | ||
174 | d->due = now + span; | ||
175 | } | ||
176 | |||
177 | void setFlags_Anim(iAnim *d, int flags, iBool set) { | ||
178 | iChangeFlags(d->flags, flags, set); | ||
179 | } | ||
180 | |||
181 | void stop_Anim(iAnim *d) { | ||
182 | d->from = d->to = value_Anim(d); | ||
183 | d->when = d->due = SDL_GetTicks(); | ||
184 | } | ||
185 | |||
186 | iLocalDef float easeIn_(float t) { | 147 | iLocalDef float easeIn_(float t) { |
187 | return t * t; | 148 | return t * t; |
188 | } | 149 | } |
@@ -198,8 +159,7 @@ iLocalDef float easeBoth_(float t) { | |||
198 | return 0.5f + easeOut_((t - 0.5f) * 2.0f) * 0.5f; | 159 | return 0.5f + easeOut_((t - 0.5f) * 2.0f) * 0.5f; |
199 | } | 160 | } |
200 | 161 | ||
201 | float value_Anim(const iAnim *d) { | 162 | static float valueAt_Anim_(const iAnim *d, const uint32_t now) { |
202 | const uint32_t now = frameTime_Window(get_Window()); | ||
203 | if (now >= d->due) { | 163 | if (now >= d->due) { |
204 | return d->to; | 164 | return d->to; |
205 | } | 165 | } |
@@ -219,6 +179,52 @@ float value_Anim(const iAnim *d) { | |||
219 | return d->from * (1.0f - t) + d->to * t; | 179 | return d->from * (1.0f - t) + d->to * t; |
220 | } | 180 | } |
221 | 181 | ||
182 | void setValue_Anim(iAnim *d, float to, uint32_t span) { | ||
183 | if (span == 0) { | ||
184 | d->from = d->to = to; | ||
185 | d->when = d->due = SDL_GetTicks(); | ||
186 | } | ||
187 | else if (fabsf(to - d->to) > 0.00001f) { | ||
188 | const uint32_t now = SDL_GetTicks(); | ||
189 | d->from = valueAt_Anim_(d, now); | ||
190 | d->to = to; | ||
191 | d->when = now; | ||
192 | d->due = now + span; | ||
193 | } | ||
194 | } | ||
195 | |||
196 | void setValueEased_Anim(iAnim *d, float to, uint32_t span) { | ||
197 | if (fabsf(to - d->to) <= 0.00001f) { | ||
198 | d->to = to; /* Pretty much unchanged. */ | ||
199 | return; | ||
200 | } | ||
201 | const uint32_t now = SDL_GetTicks(); | ||
202 | if (isFinished_Anim(d)) { | ||
203 | d->from = d->to; | ||
204 | d->flags = easeBoth_AnimFlag; | ||
205 | } | ||
206 | else { | ||
207 | d->from = valueAt_Anim_(d, now); | ||
208 | d->flags = easeOut_AnimFlag; | ||
209 | } | ||
210 | d->to = to; | ||
211 | d->when = now; | ||
212 | d->due = now + span; | ||
213 | } | ||
214 | |||
215 | void setFlags_Anim(iAnim *d, int flags, iBool set) { | ||
216 | iChangeFlags(d->flags, flags, set); | ||
217 | } | ||
218 | |||
219 | void stop_Anim(iAnim *d) { | ||
220 | d->from = d->to = value_Anim(d); | ||
221 | d->when = d->due = SDL_GetTicks(); | ||
222 | } | ||
223 | |||
224 | float value_Anim(const iAnim *d) { | ||
225 | return valueAt_Anim_(d, frameTime_Window(get_Window())); | ||
226 | } | ||
227 | |||
222 | /*-----------------------------------------------------------------------------------------------*/ | 228 | /*-----------------------------------------------------------------------------------------------*/ |
223 | 229 | ||
224 | void init_Click(iClick *d, iAnyObject *widget, int button) { | 230 | void init_Click(iClick *d, iAnyObject *widget, int button) { |