/* **************************************************************************** This source file is a plugIT for OpenSpace3D software For the latest info about Openspace3d, see http://www.openspace3d.com For the support for this plugIT, see http://www.scolring.org (section : Openspace3d) Copyright (c) 2013 Stephane Bisaro aka iri 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 **************************************************************************** */ /* Feature : This plugIT is simple. It allows to start a timer when you want. When the timer is read, an event gives the elapsed time (by a single string or a number of milliseconds). */ /* Structure of the plugin */ struct ChronoPlugin = [ CHR_inst : PInstance, /* PlugIT instance */ CHR_bState : I, /* pause status */ CHR_lastTick : I, /* initial time */ CHR_curresult : I, /* current raw result in millisecond */ CHR_curresultSz : S, /* current raw result by a human readable string */ CHR_curresultF : S, /* current raw result by a formatted string (dd_hh:mm:ss:ms) */ CHR_curresultS : S, /* current raw result by a simplified string (dd hh mm ss ms) */ CHR_day : S, /* translation from the language user */ CHR_hour : S, /* translation from the language user */ CHR_minute : S, /* translation from the language user */ CHR_second : S, /* translation from the language user */ CHR_millisecond : S, /* translation from the language user */ CHR_plural : S /* translation from the language user */ ] mkChronoPlugin;; var CHR_SZ_HUMAN = 0;; var CHR_SZ_FORMATTED = 1;; var CHR_SZ_SIMPLIFIED = 2;; fun getFormatedNumber(i)= if (i < 10) then strcat "0" (itoa i) else itoa i;; fun cbChronoPreRender(inst, sessionstr, etime, chrinst)= if ((chrinst.CHR_lastTick == nil) || chrinst.CHR_bState) then ( SendPluginEvent chrinst.CHR_inst "Current" itoa chrinst.CHR_curresult nil; ) else ( set chrinst.CHR_curresult = chrinst.CHR_curresult + (_tickcount - chrinst.CHR_lastTick); SendPluginEvent chrinst.CHR_inst "Current" itoa chrinst.CHR_curresult nil; ); set chrinst.CHR_lastTick = _tickcount; 0;; fun initChronoPlugin (inst)= let _getress "DefaultLanguage" -> lang in if !strcmpi lang "french" then mkChronoPlugin [inst 0 nil (-1) "nothing" "nothing" "" " jour" " heure" " minute" " seconde" " ms" "s "] else mkChronoPlugin [inst 0 nil (-1) "nothing" "nothing" "" " day" " hour" " minute" " second" " ms" "s "];; fun cbStartChrono (inst, from, action, param, rep, chrinst)= set chrinst.CHR_bState = 0; set chrinst.CHR_lastTick = _tickcount; set chrinst.CHR_curresult = 0; set chrinst.CHR_curresultSz = "nothing"; set chrinst.CHR_curresultS = ""; setPluginInstanceCbScenePreRender inst mkfun4 @cbChronoPreRender chrinst; SendPluginEvent chrinst.CHR_inst "Started" nil nil; 0;; fun cbStopChrono (inst, from, action, param, rep, chrinst)= set chrinst.CHR_lastTick = nil; setPluginInstanceCbScenePreRender inst nil; SendPluginEvent chrinst.CHR_inst "Stopped" nil nil; 0;; fun cbPauseChrono (inst, from, action, param, rep, chrinst)= set chrinst.CHR_bState = 1; set chrinst.CHR_lastTick = nil; setPluginInstanceCbScenePreRender inst nil; SendPluginEvent chrinst.CHR_inst "Paused" nil nil; 0;; fun cbResumeChrono (inst, from, action, param, rep, chrinst)= set chrinst.CHR_bState = 0; set chrinst.CHR_lastTick = _tickcount; setPluginInstanceCbScenePreRender inst mkfun4 @cbChronoPreRender chrinst; SendPluginEvent chrinst.CHR_inst "Resumed" nil nil; 0;; fun cbReadChrono (inst, from, action, param, rep, chrinst)= if ((chrinst.CHR_lastTick == nil) || chrinst.CHR_bState) then // not started yet SendPluginEvent chrinst.CHR_inst "Number" itoa chrinst.CHR_curresult nil else SendPluginEvent chrinst.CHR_inst "Number" itoa (chrinst.CHR_curresult + (_tickcount - chrinst.CHR_lastTick)) nil; 0;; fun cbReadStringChrono (inst, from, action, param, rep, u)= let u -> [chrinst flag] in if chrinst.CHR_lastTick == nil then // not started yet SendPluginEvent chrinst.CHR_inst "String" chrinst.CHR_curresultSz nil else let -1 -> data in ( set chrinst.CHR_curresultSz = ""; set chrinst.CHR_curresultF = ""; set chrinst.CHR_curresultS = ""; set data = if ((chrinst.CHR_lastTick == nil) || chrinst.CHR_bState) then chrinst.CHR_curresult else chrinst.CHR_curresult + (_tickcount - chrinst.CHR_lastTick); let data / 8400000 -> res in // ms in a day if 1 < res then ( if flag == CHR_SZ_HUMAN then set chrinst.CHR_curresultSz = strcatn chrinst.CHR_curresultSz :: (getFormatedNumber res) :: chrinst.CHR_day :: chrinst.CHR_plural :: nil else if flag == CHR_SZ_FORMATTED then set chrinst.CHR_curresultF = strcat chrinst.CHR_curresultF getFormatedNumber res else if flag == CHR_SZ_SIMPLIFIED then set chrinst.CHR_curresultS = strcat chrinst.CHR_curresultS getFormatedNumber res else nil; set data = mod data 8400000; ) else if res == 1 then ( if flag == CHR_SZ_HUMAN then set chrinst.CHR_curresultSz = strcatn chrinst.CHR_curresultSz :: (getFormatedNumber res) :: chrinst.CHR_day :: " " :: nil else if flag == CHR_SZ_FORMATTED then set chrinst.CHR_curresultF = strcat chrinst.CHR_curresultF getFormatedNumber res else if flag == CHR_SZ_SIMPLIFIED then set chrinst.CHR_curresultS = strcat chrinst.CHR_curresultS getFormatedNumber res else nil; set data = mod data 8400000; ) else ( set chrinst.CHR_curresultF = strcat chrinst.CHR_curresultF "0"; 0; ); let data / 3600000 -> res in // ms in a hour if 1 < res then ( if flag == CHR_SZ_HUMAN then set chrinst.CHR_curresultSz = strcatn chrinst.CHR_curresultSz :: (getFormatedNumber res) :: chrinst.CHR_hour :: chrinst.CHR_plural :: nil else if flag == CHR_SZ_FORMATTED then set chrinst.CHR_curresultF = strcatn chrinst.CHR_curresultF :: "_" :: (getFormatedNumber res) :: nil else if flag == CHR_SZ_SIMPLIFIED then set chrinst.CHR_curresultS = strcatn chrinst.CHR_curresultS :: " " :: (getFormatedNumber res) :: nil else nil; set data = mod data 3600000; ) else if res == 1 then ( if flag == CHR_SZ_HUMAN then set chrinst.CHR_curresultSz = strcatn chrinst.CHR_curresultSz :: (getFormatedNumber res) :: chrinst.CHR_hour :: " " :: nil else if flag == CHR_SZ_FORMATTED then set chrinst.CHR_curresultF = strcatn chrinst.CHR_curresultF :: "_" :: (getFormatedNumber res) :: nil else if flag == CHR_SZ_SIMPLIFIED then set chrinst.CHR_curresultS = strcatn chrinst.CHR_curresultS :: " " :: (getFormatedNumber res) :: nil else nil; set data = mod data 3600000; ) else ( set chrinst.CHR_curresultF = strcat chrinst.CHR_curresultF "_00"; 0; ); let data / 60000 -> res in // ms in a minute if 1 < res then ( if flag == CHR_SZ_HUMAN then set chrinst.CHR_curresultSz = strcatn chrinst.CHR_curresultSz :: (getFormatedNumber res) :: chrinst.CHR_minute :: chrinst.CHR_plural :: nil else if flag == CHR_SZ_FORMATTED then set chrinst.CHR_curresultF = strcatn chrinst.CHR_curresultF :: ":" :: (getFormatedNumber res) :: nil else if flag == CHR_SZ_SIMPLIFIED then set chrinst.CHR_curresultS = strcatn chrinst.CHR_curresultS :: " " :: (getFormatedNumber res) :: nil else nil; set data = mod data 60000; ) else if res == 1 then ( if flag == CHR_SZ_HUMAN then set chrinst.CHR_curresultSz = strcatn chrinst.CHR_curresultSz :: (getFormatedNumber res) :: chrinst.CHR_minute :: " " :: nil else if flag == CHR_SZ_FORMATTED then set chrinst.CHR_curresultF = strcatn chrinst.CHR_curresultF :: ":" :: (getFormatedNumber res) :: nil else if flag == CHR_SZ_SIMPLIFIED then set chrinst.CHR_curresultS = strcatn chrinst.CHR_curresultS :: " " :: (getFormatedNumber res) :: nil else nil; set data = mod data 60000; ) else ( set chrinst.CHR_curresultF = strcat chrinst.CHR_curresultF ":00"; set chrinst.CHR_curresultS = strcat chrinst.CHR_curresultS " 00"; 0; ); let data / 1000 -> res in // ms in a second if 1 < res then ( if flag == CHR_SZ_HUMAN then set chrinst.CHR_curresultSz = strcatn chrinst.CHR_curresultSz :: (getFormatedNumber res) :: chrinst.CHR_second :: chrinst.CHR_plural :: nil else if flag == CHR_SZ_FORMATTED then set chrinst.CHR_curresultF = strcatn chrinst.CHR_curresultF :: ":" :: (getFormatedNumber res) :: nil else if flag == CHR_SZ_SIMPLIFIED then set chrinst.CHR_curresultS = strcatn chrinst.CHR_curresultS :: " " :: (getFormatedNumber res) :: nil else nil; set data = mod data 1000; ) else if res == 1 then ( if flag == CHR_SZ_HUMAN then set chrinst.CHR_curresultSz = strcatn chrinst.CHR_curresultSz :: (getFormatedNumber res) :: chrinst.CHR_second :: " " :: nil else if flag == CHR_SZ_FORMATTED then set chrinst.CHR_curresultF = strcatn chrinst.CHR_curresultF :: ":" :: (getFormatedNumber res) :: nil else if flag == CHR_SZ_SIMPLIFIED then set chrinst.CHR_curresultS = strcatn chrinst.CHR_curresultS :: " " :: (getFormatedNumber res) :: nil else nil; set data = mod data 1000; ) else ( set chrinst.CHR_curresultF = strcat chrinst.CHR_curresultF ":00"; set chrinst.CHR_curresultS = strcat chrinst.CHR_curresultS " 00"; 0; ); if data != 0 then if flag == CHR_SZ_HUMAN then set chrinst.CHR_curresultSz = strcatn chrinst.CHR_curresultSz :: (getFormatedNumber data) :: chrinst.CHR_millisecond :: nil else if flag == CHR_SZ_FORMATTED then set chrinst.CHR_curresultF = strcatn chrinst.CHR_curresultF :: ":" :: (getFormatedNumber data) :: nil else if flag == CHR_SZ_SIMPLIFIED then set chrinst.CHR_curresultS = strcatn chrinst.CHR_curresultS :: " " :: (getFormatedNumber data) :: nil else nil else ( set chrinst.CHR_curresultF = strcat chrinst.CHR_curresultF ":00"; set chrinst.CHR_curresultS = strcat chrinst.CHR_curresultS " 00"; ); if flag == CHR_SZ_HUMAN then SendPluginEvent chrinst.CHR_inst "String" chrinst.CHR_curresultSz nil else if flag == CHR_SZ_FORMATTED then SendPluginEvent chrinst.CHR_inst "Formatted" chrinst.CHR_curresultF nil else if flag == CHR_SZ_SIMPLIFIED then SendPluginEvent chrinst.CHR_inst "Simplified" chrinst.CHR_curresultS nil else nil; ); 0;; /* When the object is deleted, ther is nothing to do */ fun deleteOb (inst, chrinst)= setPluginInstanceCbScenePreRender inst nil; 0;; /* Create a new plugIt object */ fun newOb (inst)= let atoi (getPluginInstanceParam inst "oninit") -> oninit in // let mkChronoPlugin [inst nil -1 "nothing"] -> chrinst in let initChronoPlugin inst -> chrinst in ( PluginRegisterAction inst "Start" mkfun6 @cbStartChrono chrinst; PluginRegisterAction inst "Stop" mkfun6 @cbStopChrono chrinst; PluginRegisterAction inst "Pause" mkfun6 @cbPauseChrono chrinst; PluginRegisterAction inst "Resume" mkfun6 @cbResumeChrono chrinst; PluginRegisterAction inst "ReadNumber" mkfun6 @cbReadChrono chrinst; PluginRegisterAction inst "ReadString" mkfun6 @cbReadStringChrono [chrinst CHR_SZ_HUMAN]; PluginRegisterAction inst "ReadFormatted" mkfun6 @cbReadStringChrono [chrinst CHR_SZ_FORMATTED]; PluginRegisterAction inst "ReadSimplified" mkfun6 @cbReadStringChrono [chrinst CHR_SZ_SIMPLIFIED]; setPluginInstanceCbDel inst mkfun2 @deleteOb chrinst; if oninit then cbStartChrono inst nil nil nil nil chrinst else nil; 0 );; /* PlugIT initialization when os3d loads */ fun IniPlug (file)= PlugRegister @newOb nil; setPluginEditor @dynamicedit; 0;;