timer.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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/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 (Engine::get_singleton()->is_editor_hint() && get_tree()->get_edited_scene_root() && (get_tree()->get_edited_scene_root() == this || get_tree()->get_edited_scene_root()->is_a_parent_of(this))) {
  38. break;
  39. }
  40. #endif
  41. start();
  42. autostart = false;
  43. }
  44. } break;
  45. case NOTIFICATION_INTERNAL_PROCESS: {
  46. if (!processing || timer_process_mode == TIMER_PROCESS_PHYSICS || !is_processing_internal()) {
  47. return;
  48. }
  49. time_left -= get_process_delta_time();
  50. if (time_left < 0) {
  51. if (!one_shot) {
  52. time_left += wait_time;
  53. } else {
  54. stop();
  55. }
  56. emit_signal("timeout");
  57. }
  58. } break;
  59. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  60. if (!processing || timer_process_mode == TIMER_PROCESS_IDLE || !is_physics_processing_internal()) {
  61. return;
  62. }
  63. time_left -= get_physics_process_delta_time();
  64. if (time_left < 0) {
  65. if (!one_shot) {
  66. time_left += wait_time;
  67. } else {
  68. stop();
  69. }
  70. emit_signal("timeout");
  71. }
  72. } break;
  73. }
  74. }
  75. void Timer::set_wait_time(float p_time) {
  76. ERR_FAIL_COND_MSG(p_time <= 0, "Time should be greater than zero.");
  77. wait_time = p_time;
  78. update_configuration_warning();
  79. }
  80. float Timer::get_wait_time() const {
  81. return wait_time;
  82. }
  83. void Timer::set_one_shot(bool p_one_shot) {
  84. one_shot = p_one_shot;
  85. }
  86. bool Timer::is_one_shot() const {
  87. return one_shot;
  88. }
  89. void Timer::set_autostart(bool p_start) {
  90. autostart = p_start;
  91. }
  92. bool Timer::has_autostart() const {
  93. return autostart;
  94. }
  95. void Timer::start(float p_time) {
  96. ERR_FAIL_COND_MSG(!is_inside_tree(), "Timer was not added to the SceneTree. Either add it or set autostart to true.");
  97. if (p_time > 0) {
  98. set_wait_time(p_time);
  99. }
  100. time_left = wait_time;
  101. _set_process(true);
  102. }
  103. void Timer::stop() {
  104. time_left = -1;
  105. _set_process(false);
  106. autostart = false;
  107. }
  108. void Timer::set_paused(bool p_paused) {
  109. if (paused == p_paused) {
  110. return;
  111. }
  112. paused = p_paused;
  113. _set_process(processing);
  114. }
  115. bool Timer::is_paused() const {
  116. return paused;
  117. }
  118. bool Timer::is_stopped() const {
  119. return get_time_left() <= 0;
  120. }
  121. float Timer::get_time_left() const {
  122. return time_left > 0 ? time_left : 0;
  123. }
  124. void Timer::set_timer_process_mode(TimerProcessMode p_mode) {
  125. if (timer_process_mode == p_mode) {
  126. return;
  127. }
  128. switch (timer_process_mode) {
  129. case TIMER_PROCESS_PHYSICS:
  130. if (is_physics_processing_internal()) {
  131. set_physics_process_internal(false);
  132. set_process_internal(true);
  133. }
  134. break;
  135. case TIMER_PROCESS_IDLE:
  136. if (is_processing_internal()) {
  137. set_process_internal(false);
  138. set_physics_process_internal(true);
  139. }
  140. break;
  141. }
  142. timer_process_mode = p_mode;
  143. }
  144. Timer::TimerProcessMode Timer::get_timer_process_mode() const {
  145. return timer_process_mode;
  146. }
  147. void Timer::_set_process(bool p_process, bool p_force) {
  148. switch (timer_process_mode) {
  149. case TIMER_PROCESS_PHYSICS:
  150. set_physics_process_internal(p_process && !paused);
  151. break;
  152. case TIMER_PROCESS_IDLE:
  153. set_process_internal(p_process && !paused);
  154. break;
  155. }
  156. processing = p_process;
  157. }
  158. String Timer::get_configuration_warning() const {
  159. String warning = Node::get_configuration_warning();
  160. if (wait_time < 0.05 - CMP_EPSILON) {
  161. if (warning != String()) {
  162. warning += "\n\n";
  163. }
  164. warning += TTR("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.");
  165. }
  166. return warning;
  167. }
  168. void Timer::_bind_methods() {
  169. ClassDB::bind_method(D_METHOD("set_wait_time", "time_sec"), &Timer::set_wait_time);
  170. ClassDB::bind_method(D_METHOD("get_wait_time"), &Timer::get_wait_time);
  171. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Timer::set_one_shot);
  172. ClassDB::bind_method(D_METHOD("is_one_shot"), &Timer::is_one_shot);
  173. ClassDB::bind_method(D_METHOD("set_autostart", "enable"), &Timer::set_autostart);
  174. ClassDB::bind_method(D_METHOD("has_autostart"), &Timer::has_autostart);
  175. ClassDB::bind_method(D_METHOD("start", "time_sec"), &Timer::start, DEFVAL(-1));
  176. ClassDB::bind_method(D_METHOD("stop"), &Timer::stop);
  177. ClassDB::bind_method(D_METHOD("set_paused", "paused"), &Timer::set_paused);
  178. ClassDB::bind_method(D_METHOD("is_paused"), &Timer::is_paused);
  179. ClassDB::bind_method(D_METHOD("is_stopped"), &Timer::is_stopped);
  180. ClassDB::bind_method(D_METHOD("get_time_left"), &Timer::get_time_left);
  181. ClassDB::bind_method(D_METHOD("set_timer_process_mode", "mode"), &Timer::set_timer_process_mode);
  182. ClassDB::bind_method(D_METHOD("get_timer_process_mode"), &Timer::get_timer_process_mode);
  183. ADD_SIGNAL(MethodInfo("timeout"));
  184. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_timer_process_mode", "get_timer_process_mode");
  185. ADD_PROPERTY(PropertyInfo(Variant::REAL, "wait_time", PROPERTY_HINT_EXP_RANGE, "0.001,4096,0.001,or_greater"), "set_wait_time", "get_wait_time");
  186. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "is_one_shot");
  187. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "has_autostart");
  188. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused", PROPERTY_HINT_NONE, "", 0), "set_paused", "is_paused");
  189. ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_left", PROPERTY_HINT_NONE, "", 0), "", "get_time_left");
  190. BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);
  191. BIND_ENUM_CONSTANT(TIMER_PROCESS_IDLE);
  192. }
  193. Timer::Timer() {
  194. timer_process_mode = TIMER_PROCESS_IDLE;
  195. autostart = false;
  196. wait_time = 1;
  197. one_shot = false;
  198. time_left = -1;
  199. processing = false;
  200. paused = false;
  201. }