timer.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*************************************************************************/
  2. /* timer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2016 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "timer.h"
  30. void Timer::_notification(int p_what) {
  31. switch(p_what) {
  32. case NOTIFICATION_READY: {
  33. if (autostart) {
  34. #ifdef TOOLS_ENABLED
  35. if (get_tree()->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)))
  36. break;
  37. #endif
  38. start();
  39. autostart=false;
  40. }
  41. } break;
  42. case NOTIFICATION_PROCESS: {
  43. if (timer_process_mode == TIMER_PROCESS_FIXED || !is_processing())
  44. return;
  45. time_left -= get_process_delta_time();
  46. if (time_left<0) {
  47. if (!one_shot)
  48. //time_left=wait_time+time_left;
  49. time_left = wait_time;
  50. else
  51. stop();
  52. emit_signal("timeout");
  53. }
  54. } break;
  55. case NOTIFICATION_FIXED_PROCESS: {
  56. if (timer_process_mode == TIMER_PROCESS_IDLE || !is_fixed_processing())
  57. return;
  58. time_left -= get_fixed_process_delta_time();
  59. if (time_left<0) {
  60. if (!one_shot)
  61. //time_left = wait_time + time_left;
  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. float Timer::get_time_left() const {
  100. return time_left >0 ? time_left : 0;
  101. }
  102. void Timer::set_timer_process_mode(TimerProcessMode p_mode) {
  103. if (timer_process_mode == p_mode)
  104. return;
  105. switch (timer_process_mode) {
  106. case TIMER_PROCESS_FIXED:
  107. if (is_fixed_processing()) {
  108. set_fixed_process(false);
  109. set_process(true);
  110. }
  111. break;
  112. case TIMER_PROCESS_IDLE:
  113. if (is_processing()) {
  114. set_process(false);
  115. set_fixed_process(true);
  116. }
  117. break;
  118. }
  119. timer_process_mode = p_mode;
  120. }
  121. Timer::TimerProcessMode Timer::get_timer_process_mode() const{
  122. return timer_process_mode;
  123. }
  124. void Timer::_set_process(bool p_process, bool p_force)
  125. {
  126. switch (timer_process_mode) {
  127. case TIMER_PROCESS_FIXED: set_fixed_process(p_process); break;
  128. case TIMER_PROCESS_IDLE: set_process(p_process); break;
  129. }
  130. }
  131. void Timer::_bind_methods() {
  132. ObjectTypeDB::bind_method(_MD("set_wait_time","time_sec"),&Timer::set_wait_time);
  133. ObjectTypeDB::bind_method(_MD("get_wait_time"),&Timer::get_wait_time);
  134. ObjectTypeDB::bind_method(_MD("set_one_shot","enable"),&Timer::set_one_shot);
  135. ObjectTypeDB::bind_method(_MD("is_one_shot"),&Timer::is_one_shot);
  136. ObjectTypeDB::bind_method(_MD("set_autostart","enable"),&Timer::set_autostart);
  137. ObjectTypeDB::bind_method(_MD("has_autostart"),&Timer::has_autostart);
  138. ObjectTypeDB::bind_method(_MD("start"),&Timer::start);
  139. ObjectTypeDB::bind_method(_MD("stop"),&Timer::stop);
  140. ObjectTypeDB::bind_method(_MD("get_time_left"),&Timer::get_time_left);
  141. ObjectTypeDB::bind_method(_MD("set_timer_process_mode", "mode"), &Timer::set_timer_process_mode);
  142. ObjectTypeDB::bind_method(_MD("get_timer_process_mode"), &Timer::get_timer_process_mode);
  143. ADD_SIGNAL( MethodInfo("timeout") );
  144. ADD_PROPERTY( PropertyInfo(Variant::INT, "process_mode", PROPERTY_HINT_ENUM, "Fixed,Idle"), _SCS("set_timer_process_mode"), _SCS("get_timer_process_mode") );
  145. ADD_PROPERTY( PropertyInfo(Variant::REAL, "wait_time", PROPERTY_HINT_EXP_RANGE, "0.01,4096,0.01" ), _SCS("set_wait_time"), _SCS("get_wait_time") );
  146. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "one_shot" ), _SCS("set_one_shot"), _SCS("is_one_shot") );
  147. ADD_PROPERTY( PropertyInfo(Variant::BOOL, "autostart" ), _SCS("set_autostart"), _SCS("has_autostart") );
  148. BIND_CONSTANT( TIMER_PROCESS_FIXED );
  149. BIND_CONSTANT( TIMER_PROCESS_IDLE );
  150. }
  151. Timer::Timer() {
  152. timer_process_mode = TIMER_PROCESS_IDLE;
  153. autostart=false;
  154. wait_time=1;
  155. one_shot=false;
  156. time_left=-1;
  157. }