/* ----------------------------------------------------------------------------- This source file is part of OpenSpace3D For the latest info, see http://www.openspace3d.com Copyright (c) 2012 I-maginer This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ----------------------------------------------------------------------------- */ struct CountDownPlugin=[ CDP_inst : PInstance, CDP_iTotalTime : I, CDP_iCurrentTime : I, CDP_iInitial : I, CDP_bSendCurSec : I, CDP_bSendCurMs : I, CDP_bState : I ] mkCountDownPlugin;; fun deleteOb(inst, obstr)= setPluginInstanceCbScenePreRender inst nil; 0;; fun getFormatedNumber(i)= if (i < 10) then strcat "0" (itoa i) else itoa i;; fun cbCountdownPreRender(inst, sessionstr, etime, obstr)= let _tickcount -> curtime in let curtime - obstr.CDP_iInitial -> ntime in let (obstr.CDP_iCurrentTime - ntime) -> rtime in let mod rtime 1000 -> rest in let rtime / 1000 -> secs in let mod obstr.CDP_iCurrentTime 1000 -> restc in let obstr.CDP_iCurrentTime / 1000 -> csecs in if ((csecs + (if (restc > 1) then 1 else 0)) == (secs + (if (rest > 1) then 1 else 0)) && obstr.CDP_bState != 1) then nil else ( if (rtime <= 0) then ( setPluginInstanceCbScenePreRender inst nil; if (!obstr.CDP_bSendCurMs) then nil else SendPluginEvent inst "Current ms" "0" nil; if (!obstr.CDP_bSendCurSec) then nil else SendPluginEvent inst "Current seconds" "0" nil; SendPluginEvent inst "Current" "00 00 00" nil; SendPluginEvent inst "Ended" nil nil; set obstr.CDP_bState = 0; ) else ( let rtime / 1000 -> secs in let secs / 3600 -> hours in let ((secs + (if rest then 1 else 0)) - (hours * 3600)) / 60 -> minutes in let secs - (hours * 3600) - (minutes * 60) + (if rest then 1 else 0) -> seconds in ( if (!obstr.CDP_bSendCurMs) then nil else SendPluginEvent inst "Current ms" (itoa rtime) nil; if (!obstr.CDP_bSendCurSec) then nil else let (rtime / 1000) + (if (rest > 1) then 1 else 0) -> fullsecs in SendPluginEvent inst "Current seconds" (itoa fullsecs) nil; SendPluginEvent inst "Current" (strcatnSep (getFormatedNumber hours)::(getFormatedNumber minutes)::(getFormatedNumber seconds)::nil " ") nil; set obstr.CDP_bState = 3; ); ); set obstr.CDP_iCurrentTime = rtime; set obstr.CDP_iInitial = curtime; ); 0;; fun cbStartCountdown(inst, from, action, param, rep, obstr)= if (obstr.CDP_bState == 1 || obstr.CDP_bState == 3) then nil else ( //addLogMessage itoa obstr.CDP_bState; if (obstr.CDP_bState == 2) then nil else set obstr.CDP_iCurrentTime = obstr.CDP_iTotalTime; set obstr.CDP_bState = 1; set obstr.CDP_iInitial = _tickcount; setPluginInstanceCbScenePreRender inst mkfun4 @cbCountdownPreRender obstr; SendPluginEvent inst "Started" nil nil; ); 0;; fun cbStopCountdown(inst, from, action, param, rep, obstr)= if (obstr.CDP_bState == 0) then nil else ( setPluginInstanceCbScenePreRender inst nil; set obstr.CDP_bState = 0; SendPluginEvent inst "Stopped" nil nil; ); 0;; fun cbPauseCountdown(inst, from, action, param, rep, obstr)= if ((obstr.CDP_bState == 0) || (obstr.CDP_bState == 2)) then nil else ( setPluginInstanceCbScenePreRender inst nil; set obstr.CDP_bState = 2; SendPluginEvent inst "Paused" nil nil; ); 0;; fun cbGetTimeLeft(inst, from, action, param, rep, obstr)= let obstr.CDP_iCurrentTime / 1000 -> secs in let mod obstr.CDP_iCurrentTime 1000 -> rest in let secs / 3600 -> hours in let (secs - (hours * 3600)) / 60 -> minutes in let secs - (hours * 3600) - (minutes * 60) + (if rest then 1 else 0) -> seconds in SendPluginEvent inst "Time left" (strcatnSep (getFormatedNumber hours)::(getFormatedNumber minutes)::(getFormatedNumber seconds)::nil " ") nil; 0;; fun cbGetElapsed(inst, from, action, param, rep, obstr)= let (obstr.CDP_iTotalTime - obstr.CDP_iCurrentTime) / 1000 -> secs in let secs / 3600 -> hours in let (secs - (hours * 3600)) / 60 -> minutes in let secs - (hours * 3600) - (minutes * 60) -> seconds in SendPluginEvent inst "Time elapsed" (strcatnSep (getFormatedNumber hours)::(getFormatedNumber minutes)::(getFormatedNumber seconds)::nil " ") nil; 0;; fun cbAddTime(inst, from, action, param, rep, obstr)= if ((param == nil) || ((atoi param) == nil)) then nil else let (atoi param) * 1000 -> secs in if ((obstr.CDP_iCurrentTime + secs) > obstr.CDP_iTotalTime) then set obstr.CDP_iCurrentTime = obstr.CDP_iTotalTime else if ((obstr.CDP_iCurrentTime + secs) < 0) then set obstr.CDP_iCurrentTime = 0 else set obstr.CDP_iCurrentTime = obstr.CDP_iCurrentTime + secs; 0;; fun cbSetTotalTime(inst, from, action, param, rep, obstr)= if ((param == nil) || ((atoi param) == nil)) then nil else ( let (atoi param) * 1000 -> secs in set obstr.CDP_iTotalTime = secs; if ((obstr.CDP_iCurrentTime) < obstr.CDP_iTotalTime) then nil else set obstr.CDP_iCurrentTime = obstr.CDP_iTotalTime; ); 0;; fun cbLoaded(inst, obstr)= cbStartCountdown inst nil nil nil nil obstr; 0;; fun newOb(inst)= let atoi (getPluginInstanceParam inst "hours") -> hours in let atoi (getPluginInstanceParam inst "minutes") -> minutes in let atoi (getPluginInstanceParam inst "seconds") -> seconds in let atoi (getPluginInstanceParam inst "autostart") -> autostart in let ((hours * 3600) + (minutes * 60) + seconds) * 1000 -> totalms in let (IsInEditor inst) || IsEventLinked inst "Current seconds" -> iscurrentsec in let (IsInEditor inst) || IsEventLinked inst "Current ms" -> iscurrentms in let mkCountDownPlugin [inst totalms 0 0 iscurrentsec iscurrentms 0] -> obstr in ( PluginRegisterAction inst "Start" mkfun6 @cbStartCountdown obstr; PluginRegisterAction inst "Stop" mkfun6 @cbStopCountdown obstr; PluginRegisterAction inst "Pause" mkfun6 @cbPauseCountdown obstr; PluginRegisterAction inst "Get time left" mkfun6 @cbGetTimeLeft obstr; PluginRegisterAction inst "Get elapsed" mkfun6 @cbGetElapsed obstr; PluginRegisterAction inst "Set total time" mkfun6 @cbSetTotalTime obstr; PluginRegisterAction inst "Add time" mkfun6 @cbAddTime obstr; if !autostart then nil else ( if inst.INST_groupstr.GRP_project.PRJ_bPluginsLoaded then ( cbStartCountdown inst nil nil nil nil obstr; ) else ( setPluginInstanceCbAllPluginsLoaded inst mkfun2 @cbLoaded obstr; 0; ); ); setPluginInstanceCbDel inst mkfun2 @deleteOb obstr; ); 0;; fun IniPlug(file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;