timer.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*************************************************************************/
  2. /* timer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. #endif
  40. start();
  41. autostart = false;
  42. }
  43. } break;
  44. case NOTIFICATION_INTERNAL_PROCESS: {
  45. if (timer_process_mode == TIMER_PROCESS_PHYSICS || !is_processing_internal())
  46. return;
  47. time_left -= get_process_delta_time();
  48. if (time_left < 0) {
  49. if (!one_shot)
  50. time_left += wait_time;
  51. else
  52. stop();
  53. emit_signal("timeout");
  54. }
  55. } break;
  56. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  57. if (timer_process_mode == TIMER_PROCESS_IDLE || !is_physics_processing_internal())
  58. return;
  59. time_left -= get_physics_process_delta_time();
  60. if (time_left < 0) {
  61. if (!one_shot)
  62. time_left += wait_time;
  63. else
  64. stop();
  65. emit_signal("timeout");
  66. }
  67. } break;
  68. }
  69. }
  70. void Timer::set_wait_time(float p_time) {
  71. ERR_EXPLAIN("time should be greater than zero.");
  72. ERR_FAIL_COND(p_time <= 0);
  73. wait_time = p_time;
  74. }
  75. float Timer::get_wait_time() const {
  76. return wait_time;
  77. }
  78. void Timer::set_one_shot(bool p_one_shot) {
  79. one_shot = p_one_shot;
  80. }
  81. bool Timer::is_one_shot() const {
  82. return one_shot;
  83. }
  84. void Timer::set_autostart(bool p_start) {
  85. autostart = p_start;
  86. }
  87. bool Timer::has_autostart() const {
  88. return autostart;
  89. }
  90. void Timer::start(float p_time) {
  91. if (p_time > 0) {
  92. set_wait_time(p_time);
  93. }
  94. time_left = wait_time;
  95. _set_process(true);
  96. }
  97. void Timer::stop() {
  98. time_left = -1;
  99. _set_process(false);
  100. autostart = false;
  101. }
  102. void Timer::set_paused(bool p_paused) {
  103. if (paused == p_paused)
  104. return;
  105. paused = p_paused;
  106. _set_process(processing);
  107. }
  108. bool Timer::is_paused() const {
  109. return paused;
  110. }
  111. bool Timer::is_stopped() const {
  112. return get_time_left() <= 0;
  113. }
  114. float Timer::get_time_left() const {
  115. return time_left > 0 ? time_left : 0;
  116. }
  117. void Timer::set_timer_process_mode(TimerProcessMode p_mode) {
  118. if (timer_process_mode == p_mode)
  119. return;
  120. switch (timer_process_mode) {
  121. case TIMER_PROCESS_PHYSICS:
  122. if (is_physics_processing_internal()) {
  123. set_physics_process_internal(false);
  124. set_process_internal(true);
  125. }
  126. break;
  127. case TIMER_PROCESS_IDLE:
  128. if (is_processing_internal()) {
  129. set_process_internal(false);
  130. set_physics_process_internal(true);
  131. }
  132. break;
  133. }
  134. timer_process_mode = p_mode;
  135. }
  136. Timer::TimerProcessMode Timer::get_timer_process_mode() const {
  137. return timer_process_mode;
  138. }
  139. void Timer::_set_process(bool p_process, bool p_force) {
  140. switch (timer_process_mode) {
  141. case TIMER_PROCESS_PHYSICS: set_physics_process_internal(p_process && !paused); break;
  142. case TIMER_PROCESS_IDLE: set_process_internal(p_process && !paused); break;
  143. }
  144. processing = p_process;
  145. }
  146. void Timer::_bind_methods() {
  147. ClassDB::bind_method(D_METHOD("set_wait_time", "time_sec"), &Timer::set_wait_time);
  148. ClassDB::bind_method(D_METHOD("get_wait_time"), &Timer::get_wait_time);
  149. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Timer::set_one_shot);
  150. ClassDB::bind_method(D_METHOD("is_one_shot"), &Timer::is_one_shot);
  151. ClassDB::bind_method(D_METHOD("set_autostart", "enable"), &Timer::set_autostart);
  152. ClassDB::bind_method(D_METHOD("has_autostart"), &Timer::has_autostart);
  153. ClassDB::bind_method(D_METHOD("start", "time_sec"), &Timer::start, DEFVAL(-1));
  154. ClassDB::bind_method(D_METHOD("stop"), &Timer::stop);
  155. ClassDB::bind_method(D_METHOD("set_paused", "paused"), &Timer::set_paused);
  156. ClassDB::bind_method(D_METHOD("is_paused"), &Timer::is_paused);
  157. ClassDB::bind_method(D_METHOD("is_stopped"), &Timer::is_stopped);
  158. ClassDB::bind_method(D_METHOD("get_time_left"), &Timer::get_time_left);
  159. ClassDB::bind_method(D_METHOD("set_timer_process_mode", "mode"), &Timer::set_timer_process_mode);
  160. ClassDB::bind_method(D_METHOD("get_timer_process_mode"), &Timer::get_timer_process_mode);
  161. ADD_SIGNAL(MethodInfo("timeout"));
  162. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_timer_process_mode", "get_timer_process_mode");
  163. ADD_PROPERTY(PropertyInfo(Variant::REAL, "wait_time", PROPERTY_HINT_EXP_RANGE, "0.01,4096,0.01"), "set_wait_time", "get_wait_time");
  164. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "is_one_shot");
  165. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "has_autostart");
  166. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "paused", PROPERTY_HINT_NONE, "", 0), "set_paused", "is_paused");
  167. ADD_PROPERTY(PropertyInfo(Variant::REAL, "time_left", PROPERTY_HINT_NONE, "", 0), "", "get_time_left");
  168. BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);
  169. BIND_ENUM_CONSTANT(TIMER_PROCESS_IDLE);
  170. }
  171. Timer::Timer() {
  172. timer_process_mode = TIMER_PROCESS_IDLE;
  173. autostart = false;
  174. wait_time = 1;
  175. one_shot = false;
  176. time_left = -1;
  177. processing = false;
  178. paused = false;
  179. }