| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218 |
- /*
- * This source file is part of libRocket, the HTML/CSS Interface Middleware
- *
- * For the latest information, see http://www.librocket.com
- *
- * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
- *
- * 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 OR COPYRIGHT HOLDERS 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.
- *
- */
- #include "precompiled.h"
- #include "EventDispatcher.h"
- #include "../../Include/Rocket/Core/Element.h"
- #include "../../Include/Rocket/Core/Event.h"
- #include "../../Include/Rocket/Core/EventListener.h"
- #include "../../Include/Rocket/Core/Factory.h"
- #include "EventSpecification.h"
- namespace Rocket {
- namespace Core {
- EventDispatcher::EventDispatcher(Element* _element)
- {
- element = _element;
- }
- EventDispatcher::~EventDispatcher()
- {
- // Detach from all event dispatchers
- for (Events::iterator event_itr = events.begin(); event_itr != events.end(); ++event_itr)
- {
- for (Listeners::iterator listener_itr = (*event_itr).second.begin(); listener_itr != (*event_itr).second.end(); ++listener_itr)
- {
- (*listener_itr).listener->OnDetach(element);
- }
- }
- }
- void EventDispatcher::AttachEvent(EventId id, EventListener* listener, bool in_capture_phase)
- {
- // See if event type exists already
- Events::iterator event_itr = events.find(id);
- if (event_itr == events.end())
- {
- // No, add listener to new event type entry
- event_itr = events.emplace(id, Listeners{ Listener(listener, in_capture_phase) }).first;
- }
- else
- {
- // Yes, add listener to the existing list of events for the type
- (*event_itr).second.emplace_back(listener, in_capture_phase);
- }
- listener->OnAttach(element);
- }
- void EventDispatcher::DetachEvent(EventId id, EventListener* listener, bool in_capture_phase)
- {
- // Look up the event
- Events::iterator event_itr = events.find(id);
- // Bail if we can't find the event
- if (event_itr == events.end())
- {
- return;
- }
- // Find the relevant listener and erase it
- Listeners::iterator listener_itr = (*event_itr).second.begin();
- while (listener_itr != (*event_itr).second.end())
- {
- if ((*listener_itr).listener == listener && (*listener_itr).in_capture_phase == in_capture_phase)
- {
- listener_itr = (*event_itr).second.erase(listener_itr);
- listener->OnDetach(element);
- }
- else
- ++listener_itr;
- }
- }
- // Detaches all events from this dispatcher and all child dispatchers.
- void EventDispatcher::DetachAllEvents()
- {
- for (Events::iterator event_iterator = events.begin(); event_iterator != events.end(); ++event_iterator)
- {
- Listeners& listeners = event_iterator->second;
- for (size_t i = 0; i < listeners.size(); ++i)
- listeners[i].listener->OnDetach(element);
- }
- events.clear();
- for (int i = 0; i < element->GetNumChildren(true); ++i)
- element->GetChild(i)->GetEventDispatcher()->DetachAllEvents();
- }
- bool EventDispatcher::DispatchEvent(Element* target_element, EventId id, const Dictionary& parameters)
- {
- Event* event = Factory::InstanceEvent(target_element, id, parameters);
- if (!event)
- return false;
- const DefaultActionPhase default_action_phase = event->GetDefaultActionPhase();
- const bool bubbles = event->GetBubbles();
- // Build the element traversal from the tree
- typedef std::vector<Element*> Elements;
- Elements elements;
- Element* walk_element = target_element->GetParentNode();
- while (walk_element)
- {
- elements.push_back(walk_element);
- walk_element = walk_element->GetParentNode();
- }
- event->SetPhase(EventPhase::Capture);
- // Capture phase - root, to target (only events that have registered as capture events)
- // Note: We walk elements in REVERSE as they're placed in the list from the elements parent to the root
- for (int i = (int)elements.size() - 1; i >= 0 && event->IsPropagating(); i--)
- {
- EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
- event->SetCurrentElement(elements[i]);
- dispatcher->TriggerEvents(event, default_action_phase);
- }
- // Target phase - direct at the target
- if (event->IsPropagating())
- {
- event->SetPhase(EventPhase::Target);
- event->SetCurrentElement(target_element);
- TriggerEvents(event, default_action_phase);
- }
- if (bubbles && event->IsPropagating())
- {
- event->SetPhase(EventPhase::Bubble);
- // Bubble phase - target to root (normal event bindings)
- for (size_t i = 0; i < elements.size() && event->IsPropagating(); i++)
- {
- EventDispatcher* dispatcher = elements[i]->GetEventDispatcher();
- event->SetCurrentElement(elements[i]);
- dispatcher->TriggerEvents(event, default_action_phase);
- }
- }
- bool propagating = event->IsPropagating();
- event->RemoveReference();
- return propagating;
- }
- String EventDispatcher::ToString() const
- {
- String result;
- for (auto nvp : events)
- {
- const EventSpecification& specification = EventSpecificationInterface::Get(nvp.first);
- result += CreateString(specification.type.size() + 32, "%s (%d), ", specification.type.c_str(), static_cast<int>(nvp.second.size()));
- }
- if (result.size() > 2)
- {
- result.resize(result.size() - 2);
- }
- return result;
- }
- void EventDispatcher::TriggerEvents(Event* event, DefaultActionPhase default_action_phase)
- {
- const EventPhase phase = event->GetPhase();
- // Look up the event
- Events::iterator itr = events.find(event->GetId());
- if (itr != events.end())
- {
- // Dispatch all actions
- Listeners& listeners = (*itr).second;
- for (size_t i = 0; i < listeners.size() && event->IsPropagating(); i++)
- {
- if (phase == EventPhase::Target
- || (phase == EventPhase::Capture && listeners[i].in_capture_phase)
- || (phase == EventPhase::Bubble && !listeners[i].in_capture_phase))
- {
- listeners[i].listener->ProcessEvent(*event);
- }
- }
- }
- const bool do_default_action = ((int)phase & (int)default_action_phase);
- if (do_default_action)
- {
- element->ProcessDefaultAction(*event);
- }
- }
- }
- }
|