// from cegui sample 'Minesweeper_timer.h' /*********************************************************************** filename: Minesweeper_Timer.h created: 08/08/2006 author: Olivier Delannoy purpose: Interface to timer window *************************************************************************/ /*************************************************************************** * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR * OTHER DEALINGS IN THE SOFTWARE. ***************************************************************************/ #ifndef _Cegui_Timer_h_#define _Cegui_Timer_h_ #include "CEGUIWindow.h" #include "CEGUIWindowFactory.h" #include "CEGUIProperty.h" namespace TimerProperties { /*! \brief Property to access the delay between two alarm \par Usage: - Name: Delay - Format: "[float]". \par Where: - [float] represents the current delay of the timer */ class Delay : public CEGUI::Property { public: Delay() : Property("Delay", "Property to get/set the current delay used by the timer. Value is a float.", "0.000000") {} CEGUI::String get(const CEGUI::PropertyReceiver* receiver) const; void set(CEGUI::PropertyReceiver* receiver, const CEGUI::String& value); }; } /*! \brief Window class intended to be used as a timer. An application can contains several timer to generate events each count milliseconds. The accuracy of the timer is not accurate enough to do any time counting and is not guaranty either. This class does no rendering and so appears totally transparent. This window defaults to position 0.0f, 0.0f with a size of 1.0f x 1.0f. */ class Timer : public CEGUI::Window { public: /************************************************************************* Constants *************************************************************************/ // type name for this widget static const CEGUI::String WidgetTypeName; //!< The unique typename of this widget static const CEGUI::String EventNamespace; //!< Store the event namespace related to the timer static const CEGUI::String EventTimerAlarm; //!< The name of the event generated by this widget /************************************************************************* Construction and Destruction *************************************************************************/ /*! \brief Constructor for Timer windows. */ Timer(const CEGUI::String& type, const CEGUI::String& name); /*! \brief Destructor for Timer windows. */ virtual ~Timer(void) {} /*! \brief start the timer in order to generate alarm event */ void start(); /*! \brief stop generating alarm event */ void stop(); /*! \brief Check wether the timer is started or not */ bool isStarted() const; /*! \brief Set the delay between to event generation in seconds */ void setDelay(float delay); float getDelay() const; protected: virtual void updateSelf(float elapsed); /*! \brief Return whether this window was inherited from the given class name at some point in the inheritance hierarchy. \param class_name The class name that is to be checked. \return true if this window was inherited from \a class_name. false if not. */ virtual bool testClassName_impl(const CEGUI::String& class_name) const { if (class_name=="Timer") return true; return CEGUI::Window::testClassName_impl(class_name); } private: static TimerProperties::Delay d_delayProperty; float d_delay; //!< Store the current delay between two alarm float d_currentValue; //!< Set the current value bool d_started; //!< Store wether the timer should produce event or not void addTimerProperties(void); }; class TimerFactory : public CEGUI::WindowFactory { public: TimerFactory() : CEGUI::WindowFactory(Timer::WidgetTypeName) {} CEGUI::Window* createWindow(const CEGUI::String& name) { return new Timer(d_type, name); } void destroyWindow(CEGUI::Window* window) { delete window;} }; TimerFactory& getTimerFactory();#endif // end of guard _CEGUIGUISheet_h_
//cpp
1:
2: /***********************************************************************
3: filename: CEGUITimer.cpp
4: created: 08/08/2006
5: author: Olivier Delannoy
6: *************************************************************************/
7: /***************************************************************************
8: * Copyright (C) 2004 - 2006 Paul D Turner & The CEGUI Development Team
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 BE LIABLE FOR ANY CLAIM, DAMAGES OR
25: * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
26: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27: * OTHER DEALINGS IN THE SOFTWARE.
28: ***************************************************************************/
29: #include "stdafx.h"
30: #include "Timer_CeguiImpl.h"
31:
32: #include "CEGUIPropertyHelper.h"
33:
34: // Start of CEGUI namespace section
35: namespace TimerProperties
36: {
37: CEGUI::String Delay::get(const CEGUI::PropertyReceiver* receiver) const
38: {
39: return CEGUI::PropertyHelper::floatToString(static_cast<const Timer*>(receiver)->getDelay());
40: }
41: void Delay::set(CEGUI::PropertyReceiver* receiver, const CEGUI::String& value)
42: {
43: static_cast<Timer*>(receiver)->setDelay(CEGUI::PropertyHelper::stringToFloat(value));
44: }
45:
46: }
47: /*************************************************************************
48: Constants
49: *************************************************************************/
50: // type name for this widget
51: const CEGUI::String Timer::WidgetTypeName("Timer");
52: const CEGUI::String Timer::EventNamespace("Timer");
53: const CEGUI::String Timer::EventTimerAlarm("EventTimerAlarm");
54: /*************************************************************************
55: * Property definition
56: *************************************************************************/
57: TimerProperties::Delay Timer::d_delayProperty;
58: /*************************************************************************
59: Constructor
60: *************************************************************************/
61: Timer::Timer(const CEGUI::String& type, const CEGUI::String& name) :
62: Window(type, name),
63: d_delay(0),
64: d_started(false),
65: d_currentValue(0)
66: {
67: CEGUI::UVector2 sz(CEGUI::UDim(1.0, 0.0), CEGUI::UDim(1.0, 0.0));
68: setMaxSize(sz);
69: setSize(sz);
70: addTimerProperties();
71: }
72:
73: void Timer::start()
74: {
75: d_started = true;
76: }
77: void Timer::stop()
78: {
79: d_started = false;
80: }
81: bool Timer::isStarted() const
82: {
83: return d_started;
84: }
85: void Timer::setDelay(float delay)
86: {
87: d_delay = delay;
88: }
89:
90: float Timer::getDelay() const
91: {
92: return d_delay;
93: }
94:
95: void Timer::updateSelf(float elapsed)
96: {
97: if (d_delay > 0 && d_started)
98: {
99: d_currentValue += elapsed;
100: if (d_currentValue >= d_delay)
101: {
102: d_currentValue -= d_delay;
103: CEGUI::WindowEventArgs args(this);
104: fireEvent(EventTimerAlarm, args, EventNamespace);
105: }
106: }
107: }
108:
109: void Timer::addTimerProperties(void)
110: {
111: addProperty(&d_delayProperty);
112: }
113:
114: TimerFactory& getTimerFactory()
115: {
116: static TimerFactory s_factory;
117: return s_factory;
118: }
// usage:
1: using namespace CEGUI;
2: // Register Timer Window
3: WindowFactoryManager::getSingleton().addFactory( &getTimerFactory() );
4:
5: mTimerPtr = (Timer*)winMgr.createWindow("Timer",buff);
6: mTimerPtr->setDelay(1.0);
7: winMgr.getWindow("root")->addChildWindow(mTimerPtr[i]);
8: mIntCount = 0;
9: mTimerPtr->subscribeEvent(Timer::EventTimerAlarm, Event::Subscriber(&SkrUI::handleUpdateTimer, this));
10: