timer.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**************************************************************************/
  2. /* timer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "timer.h"
  31. void Timer::_notification(int p_what) {
  32. switch (p_what) {
  33. case NOTIFICATION_READY: {
  34. if (autostart) {
  35. #ifdef TOOLS_ENABLED
  36. if (is_part_of_edited_scene()) {
  37. break;
  38. }
  39. #endif
  40. start();
  41. autostart = false;
  42. }
  43. } break;
  44. case NOTIFICATION_INTERNAL_PROCESS: {
  45. if (!processing || timer_process_callback == TIMER_PROCESS_PHYSICS || !is_processing_internal()) {
  46. return;
  47. }
  48. time_left -= get_process_delta_time();
  49. if (time_left < 0) {
  50. if (!one_shot) {
  51. time_left += wait_time;
  52. } else {
  53. stop();
  54. }
  55. emit_signal(SNAME("timeout"));
  56. }
  57. } break;
  58. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  59. if (!processing || timer_process_callback == TIMER_PROCESS_IDLE || !is_physics_processing_internal()) {
  60. return;
  61. }
  62. time_left -= get_physics_process_delta_time();
  63. if (time_left < 0) {
  64. if (!one_shot) {
  65. time_left += wait_time;
  66. } else {
  67. stop();
  68. }
  69. emit_signal(SNAME("timeout"));
  70. }
  71. } break;
  72. }
  73. }
  74. void Timer::set_wait_time(double p_time) {
  75. ERR_FAIL_COND_MSG(p_time <= 0, "Time should be greater than zero.");
  76. wait_time = p_time;
  77. update_configuration_warnings();
  78. }
  79. double Timer::get_wait_time() const {
  80. return wait_time;
  81. }
  82. void Timer::set_one_shot(bool p_one_shot) {
  83. one_shot = p_one_shot;
  84. }
  85. bool Timer::is_one_shot() const {
  86. return one_shot;
  87. }
  88. void Timer::set_autostart(bool p_start) {
  89. autostart = p_start;
  90. }
  91. bool Timer::has_autostart() const {
  92. return autostart;
  93. }
  94. void Timer::start(double p_time) {
  95. ERR_FAIL_COND_MSG(!is_inside_tree(), "Timer was not added to the SceneTree. Either add it or set autostart to true.");
  96. if (p_time > 0) {
  97. set_wait_time(p_time);
  98. }
  99. time_left = wait_time;
  100. _set_process(true);
  101. }
  102. void Timer::stop() {
  103. time_left = -1;
  104. _set_process(false);
  105. autostart = false;
  106. }
  107. void Timer::set_paused(bool p_paused) {
  108. if (paused == p_paused) {
  109. return;
  110. }
  111. paused = p_paused;
  112. _set_process(processing);
  113. }
  114. bool Timer::is_paused() const {
  115. return paused;
  116. }
  117. bool Timer::is_stopped() const {
  118. return get_time_left() <= 0;
  119. }
  120. double Timer::get_time_left() const {
  121. return time_left > 0 ? time_left : 0;
  122. }
  123. void Timer::set_timer_process_callback(TimerProcessCallback p_callback) {
  124. if (timer_process_callback == p_callback) {
  125. return;
  126. }
  127. switch (timer_process_callback) {
  128. case TIMER_PROCESS_PHYSICS:
  129. if (is_physics_processing_internal()) {
  130. set_physics_process_internal(false);
  131. set_process_internal(true);
  132. }
  133. break;
  134. case TIMER_PROCESS_IDLE:
  135. if (is_processing_internal()) {
  136. set_process_internal(false);
  137. set_physics_process_internal(true);
  138. }
  139. break;
  140. }
  141. timer_process_callback = p_callback;
  142. }
  143. Timer::TimerProcessCallback Timer::get_timer_process_callback() const {
  144. return timer_process_callback;
  145. }
  146. void Timer::_set_process(bool p_process, bool p_force) {
  147. switch (timer_process_callback) {
  148. case TIMER_PROCESS_PHYSICS:
  149. set_physics_process_internal(p_process && !paused);
  150. break;
  151. case TIMER_PROCESS_IDLE:
  152. set_process_internal(p_process && !paused);
  153. break;
  154. }
  155. processing = p_process;
  156. }
  157. PackedStringArray Timer::get_configuration_warnings() const {
  158. PackedStringArray warnings = Node::get_configuration_warnings();
  159. if (wait_time < 0.05 - CMP_EPSILON) {
  160. warnings.push_back(RTR("Very low timer wait times (< 0.05 seconds) may behave in significantly different ways depending on the rendered or physics frame rate.\nConsider using a script's process loop instead of relying on a Timer for very low wait times."));
  161. }
  162. return warnings;
  163. }
  164. void Timer::_bind_methods() {
  165. ClassDB::bind_method(D_METHOD("set_wait_time", "time_sec"), &Timer::set_wait_time);
  166. ClassDB::bind_method(D_METHOD("get_wait_time"), &Timer::get_wait_time);
  167. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Timer::set_one_shot);
  168. ClassDB::bind_method(D_METHOD("is_one_shot"), &Timer::is_one_shot);
  169. ClassDB::bind_method(D_METHOD("set_autostart", "enable"), &Timer::set_autostart);
  170. ClassDB::bind_method(D_METHOD("has_autostart"), &Timer::has_autostart);
  171. ClassDB::bind_method(D_METHOD("start", "time_sec"), &Timer::start, DEFVAL(-1));
  172. ClassDB::bind_method(D_METHOD("stop"), &Timer::stop);
  173. ClassDB::bind_method(D_METHOD("set_paused", "paused"), &Timer::set_paused);
  174. ClassDB::bind_method(D_METHOD("is_paused"), &Timer::is_paused);
  175. ClassDB::bind_method(D_METHOD("is_stopped"), &Timer::is_stopped);
  176. ClassDB::bind_method(D_METHOD("get_time_left"), &Timer::get_time_left);
  177. ClassDB::bind_method(D_METHOD("set_timer_process_callback", "callback"), &Timer::set_timer_process_callback);
  178. ClassDB::bind_method(D_METHOD("get_timer_process_callback"), &Timer::get_timer_process_callback);
  179. ADD_SIGNAL(MethodInfo("timeout"));
  180. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_timer_process_callback", "get_timer_process_callback");
  181. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "wait_time", PROPERTY_HINT_RANGE, "0.001,4096,0.001,or_greater,exp,suffix:s"), "set_wait_time", "get_wait_time");
  182. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "is_one_shot");
  183. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "has_autostart");
  184. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_paused", "is_paused");
  185. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s", PROPERTY_USAGE_NONE), "", "get_time_left");
  186. BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);
  187. BIND_ENUM_CONSTANT(TIMER_PROCESS_IDLE);
  188. }
  189. Timer::Timer() {}