💿🐜 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.

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