diff options
author | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-09-22 11:35:56 +0300 |
---|---|---|
committer | Jaakko Keränen <jaakko.keranen@iki.fi> | 2020-09-22 11:35:56 +0300 |
commit | f55e327bdcdefc1c130febcc8f3a03062fba782e (patch) | |
tree | 463f164e3db16e0bd804c7d04abed106d33e7877 /src/ui | |
parent | bfe86dc8121e208ef8e656b2501a7f9a5de42236 (diff) |
Added an animation utility
Diffstat (limited to 'src/ui')
-rw-r--r-- | src/ui/util.c | 33 | ||||
-rw-r--r-- | src/ui/util.h | 14 |
2 files changed, 47 insertions, 0 deletions
diff --git a/src/ui/util.c b/src/ui/util.c index a6329180..5d283742 100644 --- a/src/ui/util.c +++ b/src/ui/util.c | |||
@@ -127,6 +127,39 @@ iRangei union_Rangei(iRangei a, iRangei b) { | |||
127 | return (iRangei){ iMin(a.start, b.start), iMax(a.end, b.end) }; | 127 | return (iRangei){ iMin(a.start, b.start), iMax(a.end, b.end) }; |
128 | } | 128 | } |
129 | 129 | ||
130 | /*----------------------------------------------------------------------------------------------*/ | ||
131 | |||
132 | iBool isFinished_Anim(const iAnim *d) { | ||
133 | return frameTime_Window(get_Window()) >= d->due; | ||
134 | } | ||
135 | |||
136 | void init_Anim(iAnim *d, float value) { | ||
137 | d->due = d->when = frameTime_Window(get_Window()); | ||
138 | d->from = d->to = value; | ||
139 | } | ||
140 | |||
141 | void setValue_Anim(iAnim *d, float to, uint32_t span) { | ||
142 | if (fabsf(to - d->to) > 0.00001f) { | ||
143 | const uint32_t now = frameTime_Window(get_Window()); | ||
144 | d->from = value_Anim(d); | ||
145 | d->to = to; | ||
146 | d->when = now; | ||
147 | d->due = now + span; | ||
148 | } | ||
149 | } | ||
150 | |||
151 | float value_Anim(const iAnim *d) { | ||
152 | const uint32_t now = frameTime_Window(get_Window()); | ||
153 | if (now >= d->due) { | ||
154 | return d->to; | ||
155 | } | ||
156 | if (now <= d->when) { | ||
157 | return d->from; | ||
158 | } | ||
159 | const float pos = (float) (now - d->when) / (float) (d->due - d->when); | ||
160 | return d->from * (1.0f - pos) + d->to * pos; | ||
161 | } | ||
162 | |||
130 | /*-----------------------------------------------------------------------------------------------*/ | 163 | /*-----------------------------------------------------------------------------------------------*/ |
131 | 164 | ||
132 | void init_Click(iClick *d, iAnyObject *widget, int button) { | 165 | void init_Click(iClick *d, iAnyObject *widget, int button) { |
diff --git a/src/ui/util.h b/src/ui/util.h index 754310ad..a33bf713 100644 --- a/src/ui/util.h +++ b/src/ui/util.h | |||
@@ -66,6 +66,20 @@ iLocalDef iBool isOverlapping_Rangei(iRangei a, iRangei b) { | |||
66 | 66 | ||
67 | /*-----------------------------------------------------------------------------------------------*/ | 67 | /*-----------------------------------------------------------------------------------------------*/ |
68 | 68 | ||
69 | iDeclareType(Anim) | ||
70 | |||
71 | struct Impl_Anim { | ||
72 | float from, to; | ||
73 | uint32_t when, due; | ||
74 | }; | ||
75 | |||
76 | void init_Anim (iAnim *, float value); | ||
77 | void setValue_Anim (iAnim *, float to, uint32_t span); | ||
78 | float value_Anim (const iAnim *); | ||
79 | iBool isFinished_Anim (const iAnim *); | ||
80 | |||
81 | /*-----------------------------------------------------------------------------------------------*/ | ||
82 | |||
69 | enum iClickResult { | 83 | enum iClickResult { |
70 | none_ClickResult, | 84 | none_ClickResult, |
71 | started_ClickResult, | 85 | started_ClickResult, |