├── .gitattributes ├── .vscode └── settings.json ├── ImTween.h ├── LICENSE └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "files.associations": { 3 | "algorithm": "cpp", 4 | "array": "cpp", 5 | "atomic": "cpp", 6 | "bit": "cpp", 7 | "cctype": "cpp", 8 | "charconv": "cpp", 9 | "chrono": "cpp", 10 | "clocale": "cpp", 11 | "cmath": "cpp", 12 | "compare": "cpp", 13 | "concepts": "cpp", 14 | "cstddef": "cpp", 15 | "cstdint": "cpp", 16 | "cstdio": "cpp", 17 | "cstdlib": "cpp", 18 | "cstring": "cpp", 19 | "ctime": "cpp", 20 | "cwchar": "cpp", 21 | "exception": "cpp", 22 | "format": "cpp", 23 | "forward_list": "cpp", 24 | "fstream": "cpp", 25 | "functional": "cpp", 26 | "hash_map": "cpp", 27 | "hash_set": "cpp", 28 | "initializer_list": "cpp", 29 | "iomanip": "cpp", 30 | "ios": "cpp", 31 | "iosfwd": "cpp", 32 | "iostream": "cpp", 33 | "istream": "cpp", 34 | "iterator": "cpp", 35 | "limits": "cpp", 36 | "list": "cpp", 37 | "locale": "cpp", 38 | "map": "cpp", 39 | "memory": "cpp", 40 | "new": "cpp", 41 | "optional": "cpp", 42 | "ostream": "cpp", 43 | "ratio": "cpp", 44 | "sstream": "cpp", 45 | "stdexcept": "cpp", 46 | "stop_token": "cpp", 47 | "streambuf": "cpp", 48 | "string": "cpp", 49 | "system_error": "cpp", 50 | "thread": "cpp", 51 | "tuple": "cpp", 52 | "type_traits": "cpp", 53 | "typeinfo": "cpp", 54 | "unordered_map": "cpp", 55 | "unordered_set": "cpp", 56 | "utility": "cpp", 57 | "vector": "cpp", 58 | "xfacet": "cpp", 59 | "xhash": "cpp", 60 | "xiosbase": "cpp", 61 | "xlocale": "cpp", 62 | "xlocbuf": "cpp", 63 | "xlocinfo": "cpp", 64 | "xlocmes": "cpp", 65 | "xlocmon": "cpp", 66 | "xlocnum": "cpp", 67 | "xloctime": "cpp", 68 | "xmemory": "cpp", 69 | "xstddef": "cpp", 70 | "xstring": "cpp", 71 | "xtr1common": "cpp", 72 | "xtree": "cpp", 73 | "xutility": "cpp" 74 | } 75 | } -------------------------------------------------------------------------------- /ImTween.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | class ImTween 10 | { 11 | public: 12 | enum TweenType 13 | { 14 | PingPong 15 | }; 16 | 17 | private: 18 | TweenType type; 19 | float speed = 0.5f; 20 | std::optional> onComplete; 21 | std::function condition; 22 | 23 | std::vector>&& tweens = {}; 24 | 25 | ImTween(std::vector>&& tweens) 26 | { 27 | this->tweens = std::move(tweens); 28 | } 29 | 30 | public: 31 | /* 32 | * accepts unlimited number of values in form of std::tuple { min, max, value } 33 | */ 34 | template 35 | constexpr static auto Tween(Values... values) -> ImTween 36 | { 37 | static_assert(sizeof...(values) > 0, "No values to animate"); 38 | static_assert((std::is_same_v, Values> && ...), "Values must be of type std::tuple"); 39 | 40 | std::vector> tweens; 41 | 42 | (tweens.push_back(values), ...); 43 | 44 | return ImTween(std::move(tweens)); 45 | } 46 | 47 | auto OfType(TweenType&& type) -> ImTween& 48 | { 49 | this->type = type; 50 | 51 | return *this; 52 | } 53 | 54 | auto Speed(float&& speed) -> ImTween& 55 | { 56 | this->speed = speed; 57 | 58 | return *this; 59 | } 60 | 61 | auto When(std::function&& condition) -> ImTween& 62 | { 63 | this->condition = condition; 64 | 65 | return *this; 66 | } 67 | 68 | auto OnComplete(std::function&& onComplete) -> ImTween& 69 | { 70 | this->onComplete = onComplete; 71 | 72 | return *this; 73 | } 74 | 75 | auto Tick() 76 | { 77 | for (auto&& tween : tweens) 78 | { 79 | auto&& min = std::get<0>(tween); 80 | auto&& max = std::get<1>(tween); 81 | auto&& value = *std::get<2>(tween); 82 | 83 | if (this->condition() && value < max) 84 | { 85 | value += speed; 86 | 87 | if (value > max) 88 | value = max; 89 | } 90 | else if ((this->type == PingPong) && !this->condition() && value > min) 91 | { 92 | value -= speed; 93 | 94 | if (value < min) 95 | value = min; 96 | } 97 | 98 | if (this->onComplete.has_value() && value == max) 99 | this->onComplete.value()(); 100 | } 101 | } 102 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Kareem Olim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

ImTween

2 | 3 |

A simple, small, dependency-free single header-file tweening\animation extension for dear imgui.

4 | 5 |

6 | 7 |

8 | 9 | ImTween aims to implement [Tweening](https://en.wikipedia.org/wiki/Inbetweening) concept in under a simple and easy to use template class, for [dear imgui](https://github.com/ocornut/imgui). 10 | 11 | If you are not familiar with [Tweening](https://en.wikipedia.org/wiki/Inbetweening), it is a concept of interpolating between two values over time. It is used in many places but the most common use case is animation. 12 | 13 | ## Usage: 14 | 15 | Download the [ImTween.h](ImTween.h) file and include it in your project. 16 | 17 | - `ImTween::Tween`: returns a tween object that can be used to animate an undefined number of `std::tuple` in the form of `std::tuple { min, max, &value }`, supports all types that have `+=` and `-=` operators defined. 18 | 19 | - **Note**: T of the tween object is the T of the tuple so for example if your tween is of type `float` then your values must be of type `std::tuple)`. 20 | 21 | - `OfType`: sets the type of the tween object, currently there's only one possible type that is `PingPong` which returns the value to it's `min` state when the condition is no longer met. 22 | 23 | - `Speed`: sets the speed of how much the value is increased on each tick. 24 | 25 | - `When`: sets the condition of the tween object. 26 | 27 | - `OnComplete`: sets the callback function that is called when the `value` is equal to `max`. 28 | 29 | - `Tick`: ticks the tween object, has to be called every frame. 30 | 31 | ### Example: 32 | 33 | ```cpp 34 | ImTween::Tween( 35 | std::tuple { 50.0f, 100.0f, &settingsButtonWidth }, 36 | std::tuple { 50.0f, 100.0f, &closeButtonWidth }, 37 | std::tuple { 65.f, 115.f, &rectwidth }) 38 | .OfType(ImTween<>::TweenType::PingPong) 39 | .Speed(8.0f) 40 | .When( 41 | [&]() 42 | { 43 | return settingsButtonIsHovered || closeButtonIsHovered; 44 | }) 45 | .OnComplete( //Optional 46 | [&]() 47 | { 48 | // Do something when tween completes 49 | }) 50 | .Tick(); 51 | ``` 52 | 53 | ## License: 54 | ImTween is licensed under the MIT License, see [LICENSE](LICENSE) for more information. 55 | 56 | Credits are appreciated but not required. --------------------------------------------------------------------------------