00001 /************************************************************************ 00002 filename: CEGUIEventSet.cpp 00003 created: 21/2/2004 00004 author: Paul D Turner 00005 00006 purpose: Implements the EventSet class 00007 *************************************************************************/ 00008 /************************************************************************* 00009 Crazy Eddie's GUI System (http://www.cegui.org.uk) 00010 Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk) 00011 00012 This library is free software; you can redistribute it and/or 00013 modify it under the terms of the GNU Lesser General Public 00014 License as published by the Free Software Foundation; either 00015 version 2.1 of the License, or (at your option) any later version. 00016 00017 This library is distributed in the hope that it will be useful, 00018 but WITHOUT ANY WARRANTY; without even the implied warranty of 00019 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00020 Lesser General Public License for more details. 00021 00022 You should have received a copy of the GNU Lesser General Public 00023 License along with this library; if not, write to the Free Software 00024 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00025 *************************************************************************/ 00026 #include "CEGUIEventSet.h" 00027 #include "CEGUIExceptions.h" 00028 #include "CEGUIGlobalEventSet.h" 00029 #include "CEGUIScriptModule.h" 00030 00031 // Start of CEGUI namespace section 00032 namespace CEGUI 00033 { 00034 /************************************************************************* 00035 Constructor 00036 *************************************************************************/ 00037 EventSet::EventSet() : 00038 d_muted(false) 00039 { 00040 } 00041 00042 00043 /************************************************************************* 00044 Destructor 00045 *************************************************************************/ 00046 EventSet::~EventSet(void) 00047 { 00048 removeAllEvents(); 00049 } 00050 00051 00052 /************************************************************************* 00053 Add a new event to the EventSet 00054 *************************************************************************/ 00055 void EventSet::addEvent(const String& name) 00056 { 00057 if (isEventPresent(name)) 00058 { 00059 throw AlreadyExistsException("An event named '" + name + "' already exists in the EventSet."); 00060 } 00061 00062 d_events[name] = new Event(name); 00063 } 00064 00065 00066 /************************************************************************* 00067 Remove an event from the EventSet 00068 *************************************************************************/ 00069 void EventSet::removeEvent(const String& name) 00070 { 00071 EventMap::iterator pos = d_events.find(name); 00072 00073 if (pos != d_events.end()) 00074 { 00075 delete pos->second; 00076 d_events.erase(pos); 00077 } 00078 00079 } 00080 00081 00082 /************************************************************************* 00083 Remove all events from the EventSet 00084 *************************************************************************/ 00085 void EventSet::removeAllEvents(void) 00086 { 00087 EventMap::iterator pos = d_events.begin(); 00088 EventMap::iterator end = d_events.end() ; 00089 00090 for (; pos != end; ++pos) 00091 { 00092 delete pos->second; 00093 } 00094 00095 d_events.clear(); 00096 } 00097 00098 00099 /************************************************************************* 00100 Check to see if an event is available 00101 *************************************************************************/ 00102 bool EventSet::isEventPresent(const String& name) 00103 { 00104 return (d_events.find(name) != d_events.end()); 00105 } 00106 00107 00108 /************************************************************************* 00109 Subscribe to a scripted event (no group) 00110 *************************************************************************/ 00111 Event::Connection EventSet::subscribeScriptedEvent(const String& name, const String& subscriber_name) 00112 { 00113 return subscribeEvent(name, Event::Subscriber(ScriptFunctor(subscriber_name))); 00114 } 00115 00116 00117 /************************************************************************* 00118 Subscribe to a scripted event 00119 *************************************************************************/ 00120 Event::Connection EventSet::subscribeScriptedEvent(const String& name, Event::Group group, const String& subscriber_name) 00121 { 00122 return subscribeEvent(name, group, Event::Subscriber(ScriptFunctor(subscriber_name))); 00123 } 00124 00125 00126 /************************************************************************* 00127 Subscribe to an event (no group) 00128 *************************************************************************/ 00129 Event::Connection EventSet::subscribeEvent(const String& name, Event::Subscriber subscriber) 00130 { 00131 EventMap::iterator pos = d_events.find(name); 00132 00133 if (pos == d_events.end()) 00134 { 00135 throw UnknownObjectException("No event named '" + name + "' is defined for this EventSet"); 00136 } 00137 00138 return pos->second->subscribe(subscriber); 00139 } 00140 00141 00142 /************************************************************************* 00143 Subscribe to an event group 00144 *************************************************************************/ 00145 Event::Connection EventSet::subscribeEvent(const String& name, Event::Group group, Event::Subscriber subscriber) 00146 { 00147 EventMap::iterator pos = d_events.find(name); 00148 00149 if (pos == d_events.end()) 00150 { 00151 throw UnknownObjectException("No event named '" + name + "' is defined for this EventSet"); 00152 } 00153 00154 return pos->second->subscribe(group, subscriber); 00155 } 00156 00157 /************************************************************************* 00158 Fire / Trigger an event 00159 *************************************************************************/ 00160 void EventSet::fireEvent(const String& name, EventArgs& args, const String& eventNamespace) 00161 { 00162 // handle global events 00163 GlobalEventSet::getSingleton().fireEvent(name, args, eventNamespace); 00164 00165 EventMap::iterator pos = d_events.find(name); 00166 00167 if (pos == d_events.end()) 00168 { 00169 throw UnknownObjectException("No event named '" + name + "' is defined for this EventSet"); 00170 } 00171 00172 // fire the event 00173 if (!d_muted) 00174 { 00175 (*pos->second)(args); 00176 } 00177 00178 } 00179 00180 00181 /************************************************************************* 00182 Return whether the EventSet is muted or not. 00183 *************************************************************************/ 00184 bool EventSet::isMuted(void) const 00185 { 00186 return d_muted; 00187 } 00188 00189 00190 /************************************************************************* 00191 Set the mute state for this EventSet. 00192 *************************************************************************/ 00193 void EventSet::setMutedState(bool setting) 00194 { 00195 d_muted = setting; 00196 } 00197 00198 00199 /************************************************************************* 00200 Return a EventSet::EventIterator object to iterate over the available 00201 events. 00202 *************************************************************************/ 00203 EventSet::EventIterator EventSet::getIterator(void) const 00204 { 00205 return EventIterator(d_events.begin(), d_events.end()); 00206 } 00207 00208 00209 } // End of CEGUI namespace section