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