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 00030 // Start of CEGUI namespace section 00031 namespace CEGUI 00032 { 00033 /************************************************************************* 00034 Constructor 00035 *************************************************************************/ 00036 EventSet::EventSet() : 00037 d_muted(false) 00038 { 00039 } 00040 00041 00042 /************************************************************************* 00043 Destructor 00044 *************************************************************************/ 00045 EventSet::~EventSet(void) 00046 { 00047 removeAllEvents(); 00048 } 00049 00050 00051 /************************************************************************* 00052 Add a new event to the EventSet 00053 *************************************************************************/ 00054 void EventSet::addEvent(const String& name) 00055 { 00056 if (isEventPresent(name)) 00057 { 00058 throw AlreadyExistsException("An event named '" + name + "' already exists in the EventSet."); 00059 } 00060 00061 d_events[name] = new Event(name); 00062 } 00063 00064 00065 /************************************************************************* 00066 Remove an event from the EventSet 00067 *************************************************************************/ 00068 void EventSet::removeEvent(const String& name) 00069 { 00070 EventMap::iterator pos = d_events.find(name); 00071 00072 if (pos != d_events.end()) 00073 { 00074 delete pos->second; 00075 d_events.erase(pos); 00076 } 00077 00078 } 00079 00080 00081 /************************************************************************* 00082 Remove all events from the EventSet 00083 *************************************************************************/ 00084 void EventSet::removeAllEvents(void) 00085 { 00086 EventMap::iterator pos = d_events.begin(); 00087 EventMap::iterator end = d_events.end() ; 00088 00089 for (; pos != end; ++pos) 00090 { 00091 delete pos->second; 00092 } 00093 00094 d_events.clear(); 00095 } 00096 00097 00098 /************************************************************************* 00099 Check to see if an event is available 00100 *************************************************************************/ 00101 bool EventSet::isEventPresent(const String& name) 00102 { 00103 return (d_events.find(name) != d_events.end()); 00104 } 00105 00106 00107 /************************************************************************* 00108 Subscribe to an event (no group) 00109 *************************************************************************/ 00110 Event::Connection EventSet::subscribeEvent(const String& name, Event::Subscriber subscriber) 00111 { 00112 EventMap::iterator pos = d_events.find(name); 00113 00114 if (pos == d_events.end()) 00115 { 00116 throw UnknownObjectException("No event named '" + name + "' is defined for this EventSet"); 00117 } 00118 00119 return pos->second->subscribe(subscriber); 00120 } 00121 00122 00123 /************************************************************************* 00124 Subscribe to an event group 00125 *************************************************************************/ 00126 Event::Connection EventSet::subscribeEvent(const String& name, Event::Group group, Event::Subscriber subscriber) 00127 { 00128 EventMap::iterator pos = d_events.find(name); 00129 00130 if (pos == d_events.end()) 00131 { 00132 throw UnknownObjectException("No event named '" + name + "' is defined for this EventSet"); 00133 } 00134 00135 return pos->second->subscribe(group, subscriber); 00136 } 00137 00138 /************************************************************************* 00139 Fire / Trigger an event 00140 *************************************************************************/ 00141 void EventSet::fireEvent(const String& name, EventArgs& args, const String& eventNamespace) 00142 { 00143 EventMap::iterator pos = d_events.find(name); 00144 00145 if (pos == d_events.end()) 00146 { 00147 throw UnknownObjectException("No event named '" + name + "' is defined for this EventSet"); 00148 } 00149 00150 // fire the event 00151 if (!d_muted) 00152 { 00153 (*pos->second)(args); 00154 } 00155 00156 // handle global events 00157 GlobalEventSet::getSingleton().fireEvent(name, args, eventNamespace); 00158 } 00159 00160 00161 /************************************************************************* 00162 Return whether the EventSet is muted or not. 00163 *************************************************************************/ 00164 bool EventSet::isMuted(void) const 00165 { 00166 return d_muted; 00167 } 00168 00169 00170 /************************************************************************* 00171 Set the mute state for this EventSet. 00172 *************************************************************************/ 00173 void EventSet::setMutedState(bool setting) 00174 { 00175 d_muted = setting; 00176 } 00177 00178 00179 /************************************************************************* 00180 Return a EventSet::EventIterator object to iterate over the available 00181 events. 00182 *************************************************************************/ 00183 EventSet::EventIterator EventSet::getIterator(void) const 00184 { 00185 return EventIterator(d_events.begin(), d_events.end()); 00186 } 00187 00188 00189 } // End of CEGUI namespace section