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

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