timer.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. if (ignore_time_scale) {
  49. time_left -= Engine::get_singleton()->get_process_step();
  50. } else {
  51. time_left -= get_process_delta_time();
  52. }
  53. if (time_left < 0) {
  54. if (!one_shot) {
  55. time_left += wait_time;
  56. } else {
  57. stop();
  58. }
  59. emit_signal(SNAME("timeout"));
  60. }
  61. } break;
  62. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  63. if (!processing || timer_process_callback == TIMER_PROCESS_IDLE || !is_physics_processing_internal()) {
  64. return;
  65. }
  66. if (ignore_time_scale) {
  67. time_left -= Engine::get_singleton()->get_process_step();
  68. } else {
  69. time_left -= get_physics_process_delta_time();
  70. }
  71. if (time_left < 0) {
  72. if (!one_shot) {
  73. time_left += wait_time;
  74. } else {
  75. stop();
  76. }
  77. emit_signal(SNAME("timeout"));
  78. }
  79. } break;
  80. }
  81. }
  82. void Timer::set_wait_time(double p_time) {
  83. ERR_FAIL_COND_MSG(p_time <= 0, "Time should be greater than zero.");
  84. wait_time = p_time;
  85. update_configuration_warnings();
  86. }
  87. double Timer::get_wait_time() const {
  88. return wait_time;
  89. }
  90. void Timer::set_one_shot(bool p_one_shot) {
  91. one_shot = p_one_shot;
  92. }
  93. bool Timer::is_one_shot() const {
  94. return one_shot;
  95. }
  96. void Timer::set_autostart(bool p_start) {
  97. autostart = p_start;
  98. }
  99. bool Timer::has_autostart() const {
  100. return autostart;
  101. }
  102. void Timer::start(double p_time) {
  103. ERR_FAIL_COND_MSG(!is_inside_tree(), "Timer was not added to the SceneTree. Either add it or set autostart to true.");
  104. if (p_time > 0) {
  105. set_wait_time(p_time);
  106. }
  107. time_left = wait_time;
  108. _set_process(true);
  109. }
  110. void Timer::stop() {
  111. time_left = -1;
  112. _set_process(false);
  113. autostart = false;
  114. }
  115. void Timer::set_paused(bool p_paused) {
  116. if (paused == p_paused) {
  117. return;
  118. }
  119. paused = p_paused;
  120. _set_process(processing);
  121. }
  122. bool Timer::is_paused() const {
  123. return paused;
  124. }
  125. void Timer::set_ignore_time_scale(bool p_ignore) {
  126. ignore_time_scale = p_ignore;
  127. }
  128. bool Timer::is_ignoring_time_scale() {
  129. return ignore_time_scale;
  130. }
  131. bool Timer::is_stopped() const {
  132. return get_time_left() <= 0;
  133. }
  134. double Timer::get_time_left() const {
  135. return time_left > 0 ? time_left : 0;
  136. }
  137. void Timer::set_timer_process_callback(TimerProcessCallback p_callback) {
  138. if (timer_process_callback == p_callback) {
  139. return;
  140. }
  141. switch (timer_process_callback) {
  142. case TIMER_PROCESS_PHYSICS:
  143. if (is_physics_processing_internal()) {
  144. set_physics_process_internal(false);
  145. set_process_internal(true);
  146. }
  147. break;
  148. case TIMER_PROCESS_IDLE:
  149. if (is_processing_internal()) {
  150. set_process_internal(false);
  151. set_physics_process_internal(true);
  152. }
  153. break;
  154. }
  155. timer_process_callback = p_callback;
  156. }
  157. Timer::TimerProcessCallback Timer::get_timer_process_callback() const {
  158. return timer_process_callback;
  159. }
  160. void Timer::_set_process(bool p_process, bool p_force) {
  161. switch (timer_process_callback) {
  162. case TIMER_PROCESS_PHYSICS:
  163. set_physics_process_internal(p_process && !paused);
  164. break;
  165. case TIMER_PROCESS_IDLE:
  166. set_process_internal(p_process && !paused);
  167. break;
  168. }
  169. processing = p_process;
  170. }
  171. PackedStringArray Timer::get_configuration_warnings() const {
  172. PackedStringArray warnings = Node::get_configuration_warnings();
  173. if (wait_time < 0.05 - CMP_EPSILON) {
  174. 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."));
  175. }
  176. return warnings;
  177. }
  178. void Timer::_bind_methods() {
  179. ClassDB::bind_method(D_METHOD("set_wait_time", "time_sec"), &Timer::set_wait_time);
  180. ClassDB::bind_method(D_METHOD("get_wait_time"), &Timer::get_wait_time);
  181. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Timer::set_one_shot);
  182. ClassDB::bind_method(D_METHOD("is_one_shot"), &Timer::is_one_shot);
  183. ClassDB::bind_method(D_METHOD("set_autostart", "enable"), &Timer::set_autostart);
  184. ClassDB::bind_method(D_METHOD("has_autostart"), &Timer::has_autostart);
  185. ClassDB::bind_method(D_METHOD("start", "time_sec"), &Timer::start, DEFVAL(-1));
  186. ClassDB::bind_method(D_METHOD("stop"), &Timer::stop);
  187. ClassDB::bind_method(D_METHOD("set_paused", "paused"), &Timer::set_paused);
  188. ClassDB::bind_method(D_METHOD("is_paused"), &Timer::is_paused);
  189. ClassDB::bind_method(D_METHOD("set_ignore_time_scale", "ignore"), &Timer::set_ignore_time_scale);
  190. ClassDB::bind_method(D_METHOD("is_ignoring_time_scale"), &Timer::is_ignoring_time_scale);
  191. ClassDB::bind_method(D_METHOD("is_stopped"), &Timer::is_stopped);
  192. ClassDB::bind_method(D_METHOD("get_time_left"), &Timer::get_time_left);
  193. ClassDB::bind_method(D_METHOD("set_timer_process_callback", "callback"), &Timer::set_timer_process_callback);
  194. ClassDB::bind_method(D_METHOD("get_timer_process_callback"), &Timer::get_timer_process_callback);
  195. ADD_SIGNAL(MethodInfo("timeout"));
  196. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_callback", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_timer_process_callback", "get_timer_process_callback");
  197. 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");
  198. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "is_one_shot");
  199. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "has_autostart");
  200. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NONE), "set_paused", "is_paused");
  201. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "ignore_time_scale"), "set_ignore_time_scale", "is_ignoring_time_scale");
  202. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_left", PROPERTY_HINT_NONE, "suffix:s", PROPERTY_USAGE_NONE), "", "get_time_left");
  203. BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);
  204. BIND_ENUM_CONSTANT(TIMER_PROCESS_IDLE);
  205. }
  206. Timer::Timer() {}