Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Mar 5, 2011, 8:39:21 PM (14 years ago)
Author:
landauf
Message:

added RealTimer, a subclass of Timer which is not affected by the game's time factor
added "delayreal" command which works like "delay" but uses RealTimer instead of Timer

in the graphics menu, the resolution confirmation popup now also closes itself after 10 seconds if it is used ingame while the game is paused

Location:
code/branches/usability/src/libraries/tools
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • code/branches/usability/src/libraries/tools/Timer.cc

    r8020 r8022  
    4343#include "core/command/CommandExecutor.h"
    4444#include "core/command/Functor.h"
     45#include "tools/interfaces/TimeFactorListener.h"
    4546
    4647namespace orxonox
    4748{
    4849    SetConsoleCommand("delay", &delay).argumentCompleter(1, autocompletion::command());
     50    SetConsoleCommand("delayreal", &delayreal).argumentCompleter(1, autocompletion::command());
    4951    SetConsoleCommand("killdelay", &killdelay);
    5052    SetConsoleCommand("killdelays", &killdelays);
     
    5456
    5557    /**
    56         @brief Console-command: Calls another console command after @a delay seconds.
     58        @brief Console-command: Calls another console command after @a delay seconds (game time).
    5759        @param delay The delay in seconds
    5860        @param command The console command
     
    6163    unsigned int delay(float delay, const std::string& command)
    6264    {
    63         Timer* delaytimer = new Timer();
    64         delaytimers.insert(boost::bimap<unsigned int, Timer*>::value_type(++delayHandleCounter, delaytimer));
     65        return addDelayedCommand(new Timer(), delay, command);
     66    }
     67
     68    /**
     69        @brief Console-command: Calls another console command after @a delay seconds (real time)
     70        @param delay The delay in seconds
     71        @param command The console command
     72        @return The handle of the delayed command, can be used as argument for killdelay()
     73    */
     74    unsigned int delayreal(float delay, const std::string& command)
     75    {
     76        return addDelayedCommand(new RealTimer(), delay, command);
     77    }
     78
     79    /**
     80        @brief Helper function, used by delay() and delayreal() to add a delayed command.
     81        @param timer The timer which will execute the command
     82        @param delay The delay in seconds
     83        @param command The console command
     84        @return The handle of the delayed command, can be used as argument for killdelay()
     85    */
     86    unsigned int addDelayedCommand(Timer* timer, float delay, const std::string& command)
     87    {
     88        delaytimers.insert(boost::bimap<unsigned int, Timer*>::value_type(++delayHandleCounter, timer));
    6589
    6690        const ExecutorStaticPtr& delayexecutor = createExecutor(createFunctor(&executeDelayedCommand));
    67         delayexecutor->setDefaultValues(delaytimer, command);
    68         delaytimer->setTimer(delay, false, delayexecutor);
     91        delayexecutor->setDefaultValues(timer, command);
     92        timer->setTimer(delay, false, delayexecutor);
    6993
    7094        return delayHandleCounter;
     
    113137    {
    114138        this->init();
    115         RegisterObject(Timer);
     139        RegisterRootObject(Timer);
    116140    }
    117141
     
    126150    {
    127151        this->init();
    128         RegisterObject(Timer);
     152        RegisterRootObject(Timer);
    129153
    130154        this->setTimer(interval, bLoop, executor, bKillAfterCall);
     
    143167
    144168        this->time_ = 0;
     169    }
     170
     171    /**
     172        @brief Returns the current time factor of the game.
     173    */
     174    float Timer::getTimeFactor()
     175    {
     176        return TimeFactorListener::getTimeFactor();
    145177    }
    146178
     
    188220        }
    189221    }
     222
     223    ///////////////
     224    // RealTimer //
     225    ///////////////
     226    /// @copydoc Timer::Timer
     227    RealTimer::RealTimer()
     228    {
     229        RegisterObject(RealTimer);
     230    }
     231
     232    /// @copydoc Timer::Timer(float, bool, const ExecutorPtr&, bool)
     233    RealTimer::RealTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall) : Timer(interval, bLoop, executor, bKillAfterCall)
     234    {
     235        RegisterObject(RealTimer);
     236    }
     237
     238    /// Returns always 1 because RealTimer doesn't depend on the game time.
     239    float RealTimer::getTimeFactor()
     240    {
     241        return 1;
     242    }
    190243}
  • code/branches/usability/src/libraries/tools/Timer.h

    r8020 r8022  
    8181#include "core/OrxonoxClass.h"
    8282#include "core/command/Executor.h"
    83 #include "tools/interfaces/TimeFactorListener.h"
    8483
    8584namespace orxonox
    8685{
    8786    unsigned int delay(float delay, const std::string& command);
     87    unsigned int delayreal(float delay, const std::string& command);
     88
     89    unsigned int addDelayedCommand(Timer* timer, float delay, const std::string& command);
     90    void executeDelayedCommand(Timer* timer, const std::string& command);
     91
    8892    void killdelay(unsigned int handle);
    8993    void killdelays();
    90     void executeDelayedCommand(Timer* timer, const std::string& command);
    9194
    9295    /**
    93         @brief Timer is a helper class that executes a function after a given amount of time.
     96        @brief Timer is a helper class that executes a function after a given amount of seconds in game-time.
    9497
    9598        @see See @ref TimerExample "Timer.h" for an example.
     99
     100        The time interval of Timer depends on the game time, hence it stops if the game is paused or runs
     101        slower/faster if the game-speed is modified. See RealTimer for a timer class which doesn't depend
     102        on the game time.
    96103    */
    97     class _ToolsExport Timer : public TimeFactorListener
     104    class _ToolsExport Timer : virtual public OrxonoxClass
    98105    {
    99106        public:
     
    124131            void run();
    125132
    126             /// Re-starts the Timer: The executor will be called after @a interval seconds.
     133            /// Re-starts the timer: The executor will be called after @a interval seconds.
    127134            inline void startTimer()
    128135                { this->bActive_ = true; this->time_ = this->interval_; }
    129             /// Stops the Timer.
     136            /// Stops the timer.
    130137            inline void stopTimer()
    131138                { this->bActive_ = false; this->time_ = this->interval_; }
    132             /// Pauses the Timer - it will continue with the actual state if you call unpauseTimer().
     139            /// Pauses the timer - it will continue with the actual state if you call unpauseTimer().
    133140            inline void pauseTimer()
    134141                { this->bActive_ = false; }
    135             /// Unpauses the Timer - continues with the given state.
     142            /// Unpauses the timer - continues with the given state.
    136143            inline void unpauseTimer()
    137144                { this->bActive_ = true; }
    138             /// Returns true if the Timer is active (neither stopped nor paused).
     145            /// Returns true if the timer is active (neither stopped nor paused).
    139146            inline bool isActive() const
    140147                { return this->bActive_; }
    141             /// Returns the remaining time until the Timer calls the executor.
     148            /// Returns the remaining time until the timer calls the executor.
    142149            inline float getRemainingTime() const
    143150                { return static_cast<float>(this->time_ / 1000000.0f); }
    144             /// Increases the remaining time of the Timer by the given amount of time (in seconds).
     151            /// Increases the remaining time of the timer by the given amount of time (in seconds).
    145152            inline void addTime(float time)
    146153                { if (time > 0.0f) this->time_ += static_cast<long long>(time * 1000000.0f); }
    147             /// Decreases the remaining time of the Timer by the given amount of time (in seconds)
     154            /// Decreases the remaining time of the timer by the given amount of time (in seconds)
    148155            inline void removeTime(float time)
    149156                { if (time > 0.0f) this->time_ -= static_cast<long long>(time * 1000000.0f); }
     
    157164            void tick(const Clock& time);
    158165
     166        protected:
     167            virtual float getTimeFactor();
     168
    159169        private:
    160170            void init();
     
    164174            long long interval_;    //!< The time-interval in micro seconds
    165175            bool bLoop_;            //!< If true, the executor gets called every @a interval seconds
    166             bool bActive_;          //!< If true, the Timer ticks and calls the executor if the time's up
     176            bool bActive_;          //!< If true, the timer ticks and calls the executor if the time's up
    167177            bool bKillAfterCall_;   //!< If true the timer gets deleted after it expired and called the executor
    168178
    169179            long long time_;        //!< Internal variable, counting the time untill the next executor-call
    170180    };
     181
     182    /**
     183        @brief RealTimer is a helper class that executes a function after a given amount of seconds in real-time.
     184
     185        The time interval of RealTimer doesn't depend on the game time, it will also call the function
     186        if the game is paused. See Timer for a timer class that depends on the game time.
     187    */
     188    class _ToolsExport RealTimer : public Timer
     189    {
     190        public:
     191            RealTimer();
     192            RealTimer(float interval, bool bLoop, const ExecutorPtr& executor, bool bKillAfterCall = false);
     193
     194        protected:
     195            virtual float getTimeFactor();
     196    };
    171197}
    172198
  • code/branches/usability/src/libraries/tools/ToolsPrereqs.h

    r7163 r8022  
    8585    class Mesh;
    8686    class ParticleInterface;
     87    class RealTimer;
    8788    class ResourceCollection;
    8889    class ResourceLocation;
Note: See TracChangeset for help on using the changeset viewer.