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

111 lines
2.2 KiB

  1. /*
  2. * Copyright (C) 2021 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 "control.hpp"
  20. control::control():
  21. deadzone(0.0f),
  22. current_value(0.0f),
  23. previous_value(0.0f),
  24. reset(false),
  25. activated_callback(nullptr),
  26. deactivated_callback(nullptr),
  27. value_changed_callback(nullptr),
  28. callbacks_enabled(true)
  29. {}
  30. control::~control()
  31. {}
  32. void control::update()
  33. {
  34. // Perform callbacks, if enabled
  35. if (callbacks_enabled)
  36. {
  37. if (activated_callback)
  38. {
  39. if (is_active() && !was_active())
  40. {
  41. activated_callback();
  42. }
  43. }
  44. if (deactivated_callback)
  45. {
  46. if (!is_active() && was_active())
  47. {
  48. deactivated_callback();
  49. }
  50. }
  51. if (value_changed_callback)
  52. {
  53. if (current_value != previous_value)
  54. {
  55. if (is_active() || was_active())
  56. {
  57. value_changed_callback(current_value);
  58. }
  59. }
  60. }
  61. }
  62. // Update previous value
  63. previous_value = current_value;
  64. // Reset temporary values
  65. if (reset)
  66. {
  67. current_value = 0.0f;
  68. reset = false;
  69. }
  70. }
  71. void control::set_current_value(float value)
  72. {
  73. current_value = value;
  74. reset = false;
  75. }
  76. void control::set_temporary_value(float value)
  77. {
  78. current_value = value;
  79. reset = true;
  80. }
  81. void control::set_deadzone(float value)
  82. {
  83. deadzone = value;
  84. }
  85. void control::set_activated_callback(std::function<void()> callback)
  86. {
  87. this->activated_callback = callback;
  88. }
  89. void control::set_deactivated_callback(std::function<void()> callback)
  90. {
  91. this->deactivated_callback = callback;
  92. }
  93. void control::set_value_changed_callback(std::function<void(float)> callback)
  94. {
  95. this->value_changed_callback = callback;
  96. }