Tweeny 4.2.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
220 [[nodiscard]] auto keyframe() const -> std::size_t;
221
240 template <typename Callback> auto on(detail::event::step_t, Callback&& cb) -> void ;
241
256 template <typename Callback> auto on(detail::event::seek_t, Callback&& cb) -> void ;
257
272 template <typename Callback> auto on(detail::event::jump_t, Callback&& cb) -> void ;
273
291 template <typename Callback> auto on(detail::event::complete_t, Callback&& cb) -> void ;
292
310 template <typename Callback> auto on(detail::event::keyframeEnter_t, Callback&& cb) -> void ;
311
329 template <typename Callback> auto on(detail::event::keyframeLeave_t, Callback&& cb) -> void ;
330
349 template <typename Callback> auto on(detail::event::update_t, Callback&& cb) -> void ;
350
351 private:
352 using callback_t = std::function<event::response(tween&)>;
353 using keyframe_enter_callback_t = std::function<event::response(tween&, struct event::keyframeEnter)>;
354 using keyframe_leave_callback_t = std::function<event::response(tween&, struct event::keyframeLeave)>;
355
356 key_frames_t key_frames;
357 uint32_t current_frame = 0;
358 tween_value_t current_value;
359 std::size_t current_keyframe_index = 0;
360 std::vector<callback_t> step_listeners;
361 std::vector<callback_t> seek_listeners;
362 std::vector<callback_t> jump_listeners;
363 std::vector<callback_t> complete_listeners;
364 std::vector<callback_t> update_listeners;
365 std::vector<keyframe_enter_callback_t> keyframe_enter_listeners;
366 std::vector<keyframe_leave_callback_t> keyframe_leave_listeners;
367
368 auto invoke_listeners(std::vector<callback_t>& listeners) -> void;
369 auto invoke_keyframe_listeners(std::size_t old_keyframe_index, std::size_t new_keyframe_index) -> void;
370 auto render(uint32_t target_frame) const -> tween_value_t;
371 [[nodiscard]] auto find_key_frame_index(uint32_t frame) const -> std::size_t;
372 };
373}
374
375#include "tween.tcc"
376
377#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