Antkeeper  0.0.1
screen-transition.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
22 #include <functional>
23 
25 {
26  progress = std::make_shared<render::matvar_float>(1, 0.0f);
27 
28  // Setup material
29  material = std::make_shared<render::material>();
30  material->set_blend_mode(render::material_blend_mode::translucent);
31  material->set_variable("progress", progress);
32 
33  // Setup billboard
34  billboard.set_material(material);
35  billboard.set_layer_mask(0);
36 
37  // Add single channel to transition animation
38  channel = animation.add_channel(0);
39 
40  // Setup animation start callback to show transition billboard
41  // animation.set_start_callback
42  // (
43  // std::bind(&scene::object_base::set_active, &billboard, true)
44  // );
45 
46  // Setup animation end callback to hide transition billboard
47  // animation.set_end_callback
48  // (
49  // std::bind(&scene::object_base::set_active, &billboard, false)
50  // );
51 
52  // Setup animation frame callback to update transition progress material property
54  (
55  [this](int channel, float progress)
56  {
57  this->progress->set(progress);
58  }
59  );
60 
61  // Setup animation frame callback to update transition progress material property
63  (
64  [this](int channel, float progress)
65  {
66  this->progress->set(progress);
67  }
68  );
69 }
70 
72 {
73  //billboard.set_active(visible);
74 }
75 
76 void screen_transition::transition(float duration, bool reverse, ::animation<float>::interpolator_type interpolator, bool hide, const std::function<void()>& callback)
77 {
78  float initial_state = (reverse) ? 1.0f : 0.0f;
79  float final_state = (reverse) ? 0.0f : 1.0f;
80 
81  // Build transition animation
82  channel->remove_keyframes();
83  channel->insert_keyframe({0.0f, initial_state});
84  channel->insert_keyframe({duration, final_state});
85 
86  // Set transition animation interpolator
87  animation.set_interpolator(interpolator);
88 
89  this->callback = callback;
90  if (hide)
91  {
92  // Setup animation end callback to hide transition billboard
94  (
95  [this]()
96  {
97  this->billboard.set_layer_mask(0);
98  if (this->callback)
99  this->callback();
100  }
101  );
102  }
103  else
104  {
105  animation.set_end_callback(callback);
106  }
107 
108  // Update tweens
109  progress->set(initial_state);
110 
111  // Reset and play transition animation
112  animation.stop();
113  animation.play();
114  this->billboard.set_layer_mask(1);
115 }
void set_end_callback(std::function< void()> callback)
Sets the callback that's executed when a non-looped animation has finished.
Definition: animation.cpp:87
void play()
Plays the animation.
Definition: animation.cpp:54
void stop()
Stops the animation, rewinds it, and resets the loop count.
Definition: animation.cpp:69
Keyframe animation.
Definition: animation.hpp:149
void set_interpolator(interpolator_type interpolator)
Sets the frame interpolator function object.
Definition: animation.hpp:339
channel * add_channel(int id)
Adds a channel to the animation.
Definition: animation.hpp:317
void set_frame_callback(std::function< void(int, const T &)> callback)
Sets the callback that's executed on each frame of animation.
Definition: animation.hpp:345
void set_material(std::shared_ptr< render::material > material)
Sets the billboard material.
Definition: billboard.cpp:125
constexpr void set_layer_mask(std::uint32_t mask) noexcept
Sets the layer mask of the object.
Definition: object.hpp:64
void transition(float duration, bool reverse, animation< float >::interpolator_type interpolator, bool hide=true, const std::function< void()> &callback=nullptr)
void set_visible(bool visible)
@ translucent
Material is translucent.