timer.cpp 8.3 KB

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