1 |
/* |
/* |
2 |
$Id$ |
$Id$ |
3 |
|
|
4 |
Copyright (C) 2002/2003/2004 Kai Sterker <kaisterker@linuxgames.com> |
Copyright (C) 2002/2003/2004/2005 Kai Sterker <kaisterker@linuxgames.com> |
5 |
Part of the Adonthell Project http://adonthell.linuxgames.com |
Part of the Adonthell Project http://adonthell.linuxgames.com |
6 |
|
|
7 |
Adonthell is free software; you can redistribute it and/or modify |
Adonthell is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
(at your option) any later version. |
11 |
|
|
12 |
Adonthell is distributed in the hope that it will be useful, |
Adonthell is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
GNU General Public License for more details. |
16 |
|
|
17 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License |
18 |
along with Adonthell; if not, write to the Free Software |
along with Adonthell; if not, write to the Free Software |
19 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
20 |
*/ |
*/ |
21 |
|
|
22 |
/** |
/** |
23 |
* @file event/time_event_manager.cc |
* @file event/time_event_manager.cc |
24 |
* |
* |
25 |
* @author Kai Sterker |
* @author Kai Sterker |
26 |
* @brief Implements the time_event_manager class. |
* @brief Implements the time_event_manager class. |
27 |
*/ |
*/ |
28 |
|
|
29 |
#include <algorithm> |
#include <algorithm> |
30 |
#include "event/date.h" |
#include "event/date.h" |
31 |
#include "event/time_event.h" |
#include "event/types.h" |
32 |
#include "event/time_event_manager.h" |
#include "event/time_event.h" |
33 |
|
#include "event/time_event_manager.h" |
34 |
using std::vector; |
|
35 |
using events::time_event_manager; |
using std::vector; |
36 |
|
using events::time_event_manager; |
37 |
// See whether a matching event is registered and execute the |
using events::event_type; |
38 |
// according script(s) |
|
39 |
void time_event_manager::raise_event (const event * e) |
// function returning a new time event |
40 |
{ |
NEW_EVENT (time_event) |
41 |
s_int32 repeat; |
|
42 |
listener *li; |
// register time events with event subsystem |
43 |
|
void time_event_manager::init () |
44 |
// As long as matching events are in the list |
{ |
45 |
while (!Listeners.empty () && (li = Listeners.back ())->equals (e)) |
event_type::register_type ("time_event", new time_event_manager (), &new_time_event); |
46 |
{ |
} |
47 |
// no matter whether the listener will be destroyed or not, |
|
48 |
// it needs to be reregistered, so remove it in any case |
// remove time events from event subsystem |
49 |
Listeners.pop_back (); |
void time_event_manager::cleanup () |
50 |
|
{ |
51 |
// execute event callback |
event_type::remove_type ("time_event"); |
52 |
repeat = li->raise_event (e); |
} |
53 |
|
|
54 |
// only re-register listener if time event is repeating |
// See whether a matching event is registered and execute the |
55 |
if (repeat) add (li); |
// according script(s) |
56 |
else delete li; |
void time_event_manager::raise_event (const event * e) |
57 |
} |
{ |
58 |
|
s_int32 repeat; |
59 |
return; |
listener *li; |
60 |
} |
|
61 |
|
// As long as matching events are in the list |
62 |
// Unregister a listener |
while (!Listeners.empty () && (li = Listeners.back ())->equals (e)) |
63 |
void time_event_manager::remove (listener *li) |
{ |
64 |
{ |
// no matter whether the listener will be destroyed or not, |
65 |
vector<listener*>::iterator i; |
// it needs to be reregistered, so remove it in any case |
66 |
|
Listeners.pop_back (); |
67 |
// Search for the event we want to remove |
|
68 |
i = find (Listeners.begin (), Listeners.end (), li); |
// execute event callback |
69 |
|
repeat = li->raise_event (e); |
70 |
// found? -> get rid of it :) |
|
71 |
if (i != Listeners.end ()) Listeners.erase (i); |
// only re-register listener if time event is repeating |
72 |
} |
if (repeat) add (li); |
73 |
|
else delete li; |
74 |
// register a listener with the manager |
} |
75 |
void time_event_manager::add (listener *li) |
|
76 |
{ |
return; |
77 |
vector<listener*>::iterator i = Listeners.begin (); |
} |
78 |
|
|
79 |
// search for the proper place to insert new listener |
// Unregister a listener |
80 |
while (i != Listeners.end ()) |
void time_event_manager::remove (listener *li) |
81 |
{ |
{ |
82 |
// skip events that are raised earlier than e |
vector<listener*>::iterator i; |
83 |
if (((time_event *) li->get_event ())->time () > ((time_event *) (*i)->get_event ())->time ()) break; |
|
84 |
i++; |
// Search for the event we want to remove |
85 |
} |
i = find (Listeners.begin (), Listeners.end (), li); |
86 |
|
|
87 |
Listeners.insert (i, li); |
// found? -> get rid of it :) |
88 |
} |
if (i != Listeners.end ()) Listeners.erase (i); |
89 |
|
} |
90 |
|
|
91 |
|
// register a listener with the manager |
92 |
|
void time_event_manager::add (listener *li) |
93 |
|
{ |
94 |
|
vector<listener*>::iterator i = Listeners.begin (); |
95 |
|
|
96 |
|
// search for the proper place to insert new listener |
97 |
|
while (i != Listeners.end ()) |
98 |
|
{ |
99 |
|
// skip events that are raised earlier than e |
100 |
|
if (((time_event *) li->get_event ())->time () > ((time_event *) (*i)->get_event ())->time ()) break; |
101 |
|
i++; |
102 |
|
} |
103 |
|
|
104 |
|
Listeners.insert (i, li); |
105 |
|
} |