00001 #include "gui_eventmanager.hpp"
00002 #include "gui_event.hpp"
00003 #include "gui_eventreceiver.hpp"
00004 #include "gui_out.hpp"
00005
00006 #include <utils_string.hpp>
00007
00008
00009 #define DEBUG_LOG(msg)
00010
00011 namespace gui
00012 {
00013 typedef std::multimap<std::string, event_receiver*>::iterator iterator;
00014
00015 void event_manager::register_event(event_receiver* pReceiver, const std::string& sEventName)
00016 {
00017 if (lReceiverList_.find(sEventName) != lReceiverList_.end())
00018 {
00019
00020
00021 auto mRange = lReceiverList_.equal_range(sEventName);
00022 for (iterator iterReceiver = mRange.first; iterReceiver != mRange.second; ++iterReceiver)
00023 {
00024 if (iterReceiver->second == pReceiver)
00025 {
00026 gui::out << gui::warning << "event_manager : "
00027 << "Event \"" << sEventName << "\" is already registered to this event_receiver "
00028 << "(event_receiver : " << pReceiver << ")." << std::endl;
00029 return;
00030 }
00031 }
00032 }
00033
00034 lReceiverList_.insert(std::make_pair(sEventName, pReceiver));
00035 }
00036
00037 void event_manager::unregister_event(event_receiver* pReceiver, const std::string& sEventName)
00038 {
00039
00040
00041 auto mRange = lReceiverList_.equal_range(sEventName);
00042 for (iterator iterReceiver = mRange.first; iterReceiver != mRange.second; ++iterReceiver)
00043 {
00044 if (iterReceiver->second == pReceiver)
00045 {
00046 lReceiverList_.erase(iterReceiver);
00047 return;
00048 }
00049 }
00050
00051 gui::out << gui::warning << "event_manager : "
00052 << "Event \"" << sEventName << "\" is not registered to this event_receiver "
00053 << "(event_receiver : " << pReceiver << ")." << std::endl;
00054 }
00055
00056 void event_manager::unregister_receiver(event_receiver* pReceiver)
00057 {
00058 iterator iterReceiver = lReceiverList_.begin();
00059 while (iterReceiver != lReceiverList_.end())
00060 {
00061 if (iterReceiver->second == pReceiver)
00062 iterReceiver = lReceiverList_.erase(iterReceiver);
00063 else
00064 ++iterReceiver;
00065 }
00066 }
00067
00068 void event_manager::fire_event(const event& mEvent)
00069 {
00070 DEBUG_LOG(mEvent.get_name());
00071 if (lReceiverList_.find(mEvent.get_name()) != lReceiverList_.end())
00072 {
00073 DEBUG_LOG(mEvent.get_name()+"!");
00074
00075
00076 if (utils::find(lFiredEventList_, mEvent.get_name()) == lFiredEventList_.end())
00077 {
00078 std::pair<iterator, iterator> mRange = lReceiverList_.equal_range(mEvent.get_name());
00079
00080
00081 for (iterator iterReceiver = mRange.first; iterReceiver != mRange.second; ++iterReceiver)
00082 {
00083 DEBUG_LOG(std::string(" ") + utils::to_string(iterReceiver->second));
00084 iterReceiver->second->on_event(mEvent);
00085 }
00086
00087
00088 if (mEvent.is_once_per_frame())
00089 lFiredEventList_.push_back(mEvent.get_name());
00090 }
00091 }
00092 }
00093
00094 void event_manager::frame_ended()
00095 {
00096 lFiredEventList_.clear();
00097 }
00098 }