Tweeny 4.1.0
A Tweening library for modern C++
Loading...
Searching...
No Matches
tweeny.h
1/*
2This file is part of the Tweeny library.
3
4Copyright (c) 2016-2026 Leonardo Guilherme Lucena de Freitas
5Copyright (c) 2016 Guilherme R. Costa
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software and associated documentation files (the "Software"), to deal in
9the Software without restriction, including without limitation the rights to
10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11the Software, and to permit persons to whom the Software is furnished to do so,
12subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*/
24
36
37#ifndef TWEENY_TWEENY_H
38#define TWEENY_TWEENY_H
39
40#include "tween.h"
41#include <tuple>
42#include <type_traits>
43
44#include "detail/tuple-utilities.h"
45#include "event.h"
46#include "easing.h"
47
56namespace tweeny {
68 template <bool WasToCalled, typename FirstValue, typename... RemainingValues>
70
71 template <typename FirstValue, typename... RemainingValues>
82 class tweeny_builder<false, FirstValue, RemainingValues...> {
83 typedef std::vector<detail::key_frame<FirstValue, RemainingValues...>> key_frames_t;
84 typedef tween<FirstValue, RemainingValues...> tween_t;
85 static size_t constexpr value_count = 1 + sizeof...(RemainingValues);
86
87 public:
88 explicit tweeny_builder(FirstValue firstComponent, RemainingValues... remainingComponents) {
89 key_frames.emplace_back(firstComponent, remainingComponents...);
90 }
91
92 explicit tweeny_builder(key_frames_t && frames) : key_frames(std::move(frames)) {}
93 explicit tweeny_builder(const key_frames_t & frames) : key_frames(frames) {}
94
109 tweeny_builder<true, FirstValue, RemainingValues...> to(const FirstValue & firstComponent, const RemainingValues &... remainingComponents) & {
110 key_frames.emplace_back(firstComponent, remainingComponents...);
111 return tweeny_builder<true, FirstValue, RemainingValues...>(key_frames);
112 }
113
115 tweeny_builder<true, FirstValue, RemainingValues...> to(const FirstValue & firstComponent, const RemainingValues &... remainingComponents) && {
116 key_frames.emplace_back(firstComponent, remainingComponents...);
117 return tweeny_builder<true, FirstValue, RemainingValues...>(std::move(key_frames));
118 }
119
120 private:
121 key_frames_t key_frames;
122 };
123
134 template <typename FirstValue, typename... RemainingValues>
135 class tweeny_builder<true, FirstValue, RemainingValues...> {
136 typedef std::vector<detail::key_frame<FirstValue, RemainingValues...>> key_frames_t;
137 typedef tween<FirstValue, RemainingValues...> tween_t;
138 static size_t constexpr value_count = 1 + sizeof...(RemainingValues);
139
140 public:
141 explicit tweeny_builder(FirstValue firstComponent, RemainingValues... remainingComponents) {
142 key_frames.emplace_back(firstComponent, remainingComponents...);
143 }
144
145 explicit tweeny_builder(key_frames_t && frames) : key_frames(std::move(frames)) {}
146 explicit tweeny_builder(const key_frames_t & frames) : key_frames(frames) {}
147
168 tweeny_builder to(const FirstValue & firstComponent, const RemainingValues &... remainingComponents) & {
169 key_frames.emplace_back(firstComponent, remainingComponents...);
170 return tweeny_builder(key_frames);
171 }
172
174 tweeny_builder to(const FirstValue & firstComponent, const RemainingValues &... remainingComponents) && {
175 key_frames.emplace_back(firstComponent, remainingComponents...);
176 return tweeny_builder(std::move(key_frames));
177 }
178
197 template<typename... EasingFunctionTypes>
198 tweeny_builder & via(EasingFunctionTypes... easing_functions) {
199 static_assert(sizeof...(EasingFunctionTypes) == value_count,
200 "via() must have one easing function per tween component");
201 auto & key_frame = key_frames.at(key_frames.size() - 2);
202 key_frame.easing_functions = std::make_tuple(easing_functions...);
203 return *this;
204 }
205
220 template<typename EasingFunctionType>
221 tweeny_builder & via(EasingFunctionType easing_function) {
222 auto & key_frame = key_frames.at(key_frames.size() - 2);
223 key_frame.easing_functions = detail::make_repeated_tuple<EasingFunctionType, value_count>(easing_function);
224 return *this;
225 }
226
244 template<typename... FrameCountsType>
245 tweeny_builder & during(FrameCountsType... frame_counts) {
246 static_assert(sizeof...(FrameCountsType) == value_count,
247 "during() must have one frame count per tween component");
248 static_assert((std::is_same_v<std::decay_t<FrameCountsType>, uint32_t> && ...),
249 "during() parameters must be of type uint32_t");
250 auto & key_frame = key_frames.at(key_frames.size() - 2);
251 std::array<uint32_t, sizeof...(FrameCountsType)> frame_counts_array = { frame_counts... };
252 std::copy(
253 std::begin(frame_counts_array),
254 std::end(frame_counts_array),
255 std::begin(key_frame.tween_frame_counts));
256 fix_frame_positions();
257 return *this;
258 }
259
274 tweeny_builder & during(uint32_t frame_count) {
275 auto & key_frame = key_frames.at(key_frames.size() - 2);
276 std::fill(
277 std::begin(key_frame.tween_frame_counts),
278 std::end(key_frame.tween_frame_counts),
279 frame_count
280 );
281 fix_frame_positions();
282 return *this;
283 }
284
309 tween_t build() const & { return tween(key_frames); }
310
312 tween_t build() && { return tween(std::move(key_frames)); }
313
314 private:
315 key_frames_t key_frames;
316 void fix_frame_positions() {
317 uint32_t key_frame_position = 0;
318 for (auto & key_frame : key_frames) {
319 key_frame.position = key_frame_position;
320 key_frame_position += key_frame.highest_frame_count();
321 }
322 }
323 };
324
346 template <typename FirstValue, typename... RemainingValues>
347 tweeny_builder<false, FirstValue, RemainingValues...> from(FirstValue firstComponent, RemainingValues... remainingComponents) {
348 return tweeny_builder<false, FirstValue, RemainingValues...>(firstComponent, remainingComponents...);
349 }
350}
351
352#endif //TWEENY_TWEENY_H
A tween represents an animation between keyframes.
Definition tween.h:68
tweeny_builder< true, FirstValue, RemainingValues... > to(const FirstValue &firstComponent, const RemainingValues &... remainingComponents) &
Adds a target keyframe to the tween.
Definition tweeny.h:109
tweeny_builder< true, FirstValue, RemainingValues... > to(const FirstValue &firstComponent, const RemainingValues &... remainingComponents) &&
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tweeny.h:115
tweeny_builder & during(FrameCountsType... frame_counts)
Specifies per-component frame durations for the last keyframe segment.
Definition tweeny.h:245
tweeny_builder & via(EasingFunctionType easing_function)
Specifies a single easing function for all components.
Definition tweeny.h:221
tweeny_builder to(const FirstValue &firstComponent, const RemainingValues &... remainingComponents) &
Adds another keyframe to create multipoint animations.
Definition tweeny.h:168
tweeny_builder & during(uint32_t frame_count)
Specifies a uniform frame duration for all components.
Definition tweeny.h:274
tween_t build() const &
Constructs a tween object from the configured keyframes.
Definition tweeny.h:309
tweeny_builder & via(EasingFunctionTypes... easing_functions)
Specifies per-component easing functions for the last keyframe segment.
Definition tweeny.h:198
tweeny_builder to(const FirstValue &firstComponent, const RemainingValues &... remainingComponents) &&
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tweeny.h:174
tween_t build() &&
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition tweeny.h:312
Primary template for the tween builder type.
Definition tweeny.h:69
tweeny_builder< false, FirstValue, RemainingValues... > from(FirstValue firstComponent, RemainingValues... remainingComponents)
Creates a new tween builder starting from the specified value(s).
Definition tweeny.h:347
Contains all public API types and functions for creating and managing tweens.
Definition manual.dox:1
Represents a key frame in a tween.
Definition key-frame.h:48