1 /* 2 Copyright (c) 2013 Andrey Penechko 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license the "Software" to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 module anchovy.gui.timer; 30 31 import core.time; 32 import std.math: isNaN, trunc; 33 34 import anchovy.gui; 35 36 /// Handler must number > 0 if custom repeat needed. 37 /// Timer will be added to queue again with nextTime set to number. 38 /// If number = 0 then nextTime will be set to Timer.delay. 39 /// If number < 0 or is nan, timer will be removed from queue. 40 alias double delegate(double dt) TimerHandler; 41 42 enum TimerTickType 43 { 44 PROCESS_ALL_UNORDERED, 45 PROCESS_ALL_ORDERED, 46 PROCESS_LAST, 47 } 48 49 /// Provides basic timer functionality. 50 /// Designed to be used with TimerManager. 51 class Timer 52 { 53 /// Used for initializing timer. 54 /// Params: 55 /// delay specifies the delay after which handler will be called. 56 /// handler specifies handler to be called when delay exceeds. 57 58 /// tickType sets processing method to be used with this timer. 59 void init(double _firstTime, double _currentTime, double _delay, TimerHandler _handler, TimerTickType _tickType = TimerTickType.init) 60 in 61 { 62 assert(_handler); 63 assert(_delay > 0); 64 assert(_firstTime > 0); 65 assert(_currentTime > 0); 66 } 67 body 68 { 69 delay = _delay; 70 lastUpdateTime = _currentTime; 71 handler = _handler; 72 tickType = _tickType; 73 nextUpdateTime = _firstTime; 74 } 75 76 /// The whole delta time will be provided if currentTime > nextTime even when dt % thisDelay > 1. 77 /// Timer will automaticaly decide to process only one period, or all. It can also process only the last update if needed. 78 /// This method will be called only when currentTime > nextTime. 79 void tick(double currentTime) 80 { 81 void updateNextTime(double newNextTime) 82 { 83 lastUpdateTime = nextUpdateTime; 84 85 if (newNextTime == 0) 86 nextUpdateTime += delay; 87 else if (newNextTime > 0) 88 nextUpdateTime += newNextTime; 89 else 90 nextUpdateTime = double.nan; 91 } 92 93 double newNextTime; 94 95 with(TimerTickType) 96 final switch(tickType) 97 { 98 case PROCESS_ALL_ORDERED: 99 { 100 newNextTime = handler(nextUpdateTime - lastUpdateTime); 101 updateNextTime(newNextTime); 102 break; 103 } 104 case PROCESS_ALL_UNORDERED: 105 { 106 while(currentTime > nextUpdateTime) 107 { 108 newNextTime = handler(nextUpdateTime - lastUpdateTime); 109 updateNextTime(newNextTime); 110 } 111 break; 112 } 113 case PROCESS_LAST: 114 { 115 uint timesUpdated = cast(uint)trunc((currentTime - nextUpdateTime) / delay) + 1; 116 nextUpdateTime += timesUpdated * delay; 117 118 newNextTime = handler(timesUpdated); 119 updateNextTime(newNextTime); 120 break; 121 } 122 } 123 } 124 125 TimerHandler handler; 126 127 TimerTickType tickType; 128 129 double lastUpdateTime; 130 double nextUpdateTime; 131 double delay; 132 }