💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.1 KiB

  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. #include <engine/animation/screen-transition.hpp>
  20. #include <engine/render/material-flags.hpp>
  21. #include <functional>
  22. screen_transition::screen_transition()
  23. {
  24. progress = std::make_shared<render::matvar_float>(1, 0.0f);
  25. // Setup material
  26. material = std::make_shared<render::material>();
  27. material->set_blend_mode(render::material_blend_mode::translucent);
  28. material->set_variable("progress", progress);
  29. // Setup billboard
  30. billboard.set_material(material);
  31. //billboard.set_active(false);
  32. // Add single channel to transition animation
  33. channel = animation.add_channel(0);
  34. // Setup animation start callback to show transition billboard
  35. // animation.set_start_callback
  36. // (
  37. // std::bind(&scene::object_base::set_active, &billboard, true)
  38. // );
  39. // Setup animation end callback to hide transition billboard
  40. // animation.set_end_callback
  41. // (
  42. // std::bind(&scene::object_base::set_active, &billboard, false)
  43. // );
  44. // Setup animation frame callback to update transition progress material property
  45. animation.set_frame_callback
  46. (
  47. [this](int channel, float progress)
  48. {
  49. this->progress->set(progress);
  50. }
  51. );
  52. // Setup animation frame callback to update transition progress material property
  53. animation.set_frame_callback
  54. (
  55. [this](int channel, float progress)
  56. {
  57. this->progress->set(progress);
  58. }
  59. );
  60. }
  61. void screen_transition::set_visible(bool visible)
  62. {
  63. //billboard.set_active(visible);
  64. }
  65. void screen_transition::transition(float duration, bool reverse, ::animation<float>::interpolator_type interpolator, bool hide, const std::function<void()>& callback)
  66. {
  67. float initial_state = (reverse) ? 1.0f : 0.0f;
  68. float final_state = (reverse) ? 0.0f : 1.0f;
  69. // Build transition animation
  70. channel->remove_keyframes();
  71. channel->insert_keyframe({0.0f, initial_state});
  72. channel->insert_keyframe({duration, final_state});
  73. // Set transition animation interpolator
  74. animation.set_interpolator(interpolator);
  75. this->callback = callback;
  76. if (hide)
  77. {
  78. // Setup animation end callback to hide transition billboard
  79. animation.set_end_callback
  80. (
  81. [this]()
  82. {
  83. //this->billboard.set_active(false);
  84. if (this->callback)
  85. this->callback();
  86. }
  87. );
  88. }
  89. else
  90. {
  91. animation.set_end_callback(callback);
  92. }
  93. // Update tweens
  94. progress->set(initial_state);
  95. // Reset and play transition animation
  96. animation.stop();
  97. animation.play();
  98. }