timer.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*************************************************************************/
  2. /* timer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "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() {
  91. time_left = wait_time;
  92. _set_process(true);
  93. }
  94. void Timer::stop() {
  95. time_left = -1;
  96. _set_process(false);
  97. autostart = false;
  98. }
  99. void Timer::set_paused(bool p_paused) {
  100. if (paused == p_paused)
  101. return;
  102. paused = p_paused;
  103. _set_process(processing);
  104. }
  105. bool Timer::is_paused() const {
  106. return paused;
  107. }
  108. bool Timer::is_stopped() const {
  109. return get_time_left() <= 0;
  110. }
  111. float Timer::get_time_left() const {
  112. return time_left > 0 ? time_left : 0;
  113. }
  114. void Timer::set_timer_process_mode(TimerProcessMode p_mode) {
  115. if (timer_process_mode == p_mode)
  116. return;
  117. switch (timer_process_mode) {
  118. case TIMER_PROCESS_PHYSICS:
  119. if (is_physics_processing_internal()) {
  120. set_physics_process_internal(false);
  121. set_process_internal(true);
  122. }
  123. break;
  124. case TIMER_PROCESS_IDLE:
  125. if (is_processing_internal()) {
  126. set_process_internal(false);
  127. set_physics_process_internal(true);
  128. }
  129. break;
  130. }
  131. timer_process_mode = p_mode;
  132. }
  133. Timer::TimerProcessMode Timer::get_timer_process_mode() const {
  134. return timer_process_mode;
  135. }
  136. void Timer::_set_process(bool p_process, bool p_force) {
  137. switch (timer_process_mode) {
  138. case TIMER_PROCESS_PHYSICS: set_physics_process_internal(p_process && !paused); break;
  139. case TIMER_PROCESS_IDLE: set_process_internal(p_process && !paused); break;
  140. }
  141. processing = p_process;
  142. }
  143. void Timer::_bind_methods() {
  144. ClassDB::bind_method(D_METHOD("set_wait_time", "time_sec"), &Timer::set_wait_time);
  145. ClassDB::bind_method(D_METHOD("get_wait_time"), &Timer::get_wait_time);
  146. ClassDB::bind_method(D_METHOD("set_one_shot", "enable"), &Timer::set_one_shot);
  147. ClassDB::bind_method(D_METHOD("is_one_shot"), &Timer::is_one_shot);
  148. ClassDB::bind_method(D_METHOD("set_autostart", "enable"), &Timer::set_autostart);
  149. ClassDB::bind_method(D_METHOD("has_autostart"), &Timer::has_autostart);
  150. ClassDB::bind_method(D_METHOD("start"), &Timer::start);
  151. ClassDB::bind_method(D_METHOD("stop"), &Timer::stop);
  152. ClassDB::bind_method(D_METHOD("set_paused", "paused"), &Timer::set_paused);
  153. ClassDB::bind_method(D_METHOD("is_paused"), &Timer::is_paused);
  154. ClassDB::bind_method(D_METHOD("is_stopped"), &Timer::is_stopped);
  155. ClassDB::bind_method(D_METHOD("get_time_left"), &Timer::get_time_left);
  156. ClassDB::bind_method(D_METHOD("set_timer_process_mode", "mode"), &Timer::set_timer_process_mode);
  157. ClassDB::bind_method(D_METHOD("get_timer_process_mode"), &Timer::get_timer_process_mode);
  158. ADD_SIGNAL(MethodInfo("timeout"));
  159. ADD_PROPERTY(PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Physics,Idle"), "set_timer_process_mode", "get_timer_process_mode");
  160. ADD_PROPERTY(PropertyInfo(Variant::REAL, "wait_time", PROPERTY_HINT_EXP_RANGE, "0.01,4096,0.01"), "set_wait_time", "get_wait_time");
  161. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "one_shot"), "set_one_shot", "is_one_shot");
  162. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "autostart"), "set_autostart", "has_autostart");
  163. BIND_ENUM_CONSTANT(TIMER_PROCESS_PHYSICS);
  164. BIND_ENUM_CONSTANT(TIMER_PROCESS_IDLE);
  165. }
  166. Timer::Timer() {
  167. timer_process_mode = TIMER_PROCESS_IDLE;
  168. autostart = false;
  169. wait_time = 1;
  170. one_shot = false;
  171. time_left = -1;
  172. processing = false;
  173. paused = false;
  174. }