Tweeny 4.1.0
A Tweening library for modern C++
Loading...
Searching...
No Matches
tween.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
40
41#ifndef TWEENY_TWEEN_H
42#define TWEENY_TWEEN_H
43
44#include <vector>
45#include <cstdint>
46#include <cstddef>
47#include <functional>
48
49#include "detail/key-frame.h"
50#include "detail/tween-value.h"
51#include "detail/event.h"
52
53namespace tweeny {
67 template<typename FirstValueType, typename... RemainingValueTypes>
68 class tween {
69 typedef detail::key_frame<FirstValueType, RemainingValueTypes...> key_frame_t;
70 typedef std::vector<key_frame_t> key_frames_t;
71
72 public:
79 using tween_value_t = detail::tween_value_t<FirstValueType, RemainingValueTypes...>;
80
85 explicit tween(const key_frames_t & key_frames_input);
86
88 explicit tween(key_frames_t && key_frames_input);
89
105 auto seek(uint32_t target_frame) -> tween_value_t;
106
123 auto jump(std::size_t target_key_frame) -> tween_value_t;
124
141 auto step(int32_t frames) -> tween_value_t;
142
157 [[nodiscard]] auto peek() const -> tween_value_t;
158
176 [[nodiscard]] auto peek(uint32_t target_frame) const -> tween_value_t;
177
197 [[nodiscard]] auto progress() const -> float;
198
217 template <typename Callback> auto on(detail::event::step_t, Callback&& cb) -> void ;
218
233 template <typename Callback> auto on(detail::event::seek_t, Callback&& cb) -> void ;
234
249 template <typename Callback> auto on(detail::event::jump_t, Callback&& cb) -> void ;
250
268 template <typename Callback> auto on(detail::event::complete_t, Callback&& cb) -> void ;
269
287 template <typename Callback> auto on(detail::event::keyframeEnter_t, Callback&& cb) -> void ;
288
306 template <typename Callback> auto on(detail::event::keyframeLeave_t, Callback&& cb) -> void ;
307
326 template <typename Callback> auto on(detail::event::update_t, Callback&& cb) -> void ;
327
328 private:
329 using callback_t = std::function<event::response(tween&)>;
330 using keyframe_enter_callback_t = std::function<event::response(tween&, struct event::keyframeEnter)>;
331 using keyframe_leave_callback_t = std::function<event::response(tween&, struct event::keyframeLeave)>;
332
333 key_frames_t key_frames;
334 uint32_t current_frame = 0;
335 tween_value_t current_value;
336 std::size_t current_keyframe_index = 0;
337 std::vector<callback_t> step_listeners;
338 std::vector<callback_t> seek_listeners;
339 std::vector<callback_t> jump_listeners;
340 std::vector<callback_t> complete_listeners;
341 std::vector<callback_t> update_listeners;
342 std::vector<keyframe_enter_callback_t> keyframe_enter_listeners;
343 std::vector<keyframe_leave_callback_t> keyframe_leave_listeners;
344
345 auto invoke_listeners(std::vector<callback_t>& listeners) -> void;
346 auto invoke_keyframe_listeners(std::size_t old_keyframe_index, std::size_t new_keyframe_index) -> void;
347 auto render(uint32_t target_frame) const -> tween_value_t;
348 [[nodiscard]] auto find_key_frame_index(uint32_t frame) const -> std::size_t;
349 };
350}
351
352#include "tween.tcc"
353
354#endif //TWEENY_TWEEN_H
auto step(int32_t frames) -> tween_value_t
Advances or rewinds the animation by a frame delta.
auto seek(uint32_t target_frame) -> tween_value_t
Seeks to a specific frame in the animation.
auto jump(std::size_t target_key_frame) -> tween_value_t
Jumps to a specific keyframe index.
detail::tween_value_t< FirstValueType, RemainingValueTypes... > tween_value_t
The type returned by navigation and query methods.
Definition tween.h:79
auto on(detail::event::step_t, Callback &&cb) -> void
auto peek() const -> tween_value_t
Returns the current interpolated value without changing state.
tween(const key_frames_t &key_frames_input)
Constructs a tween from keyframes (use builder API instead).
tween(key_frames_t &&key_frames_input)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Event types and response codes for tween callbacks.
Definition detail/event.h:74
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