Changes between Version 8 and Version 9 of documentation/tutorials/Dragging

Show
Ignore:
Author:
peterc (IP: 203.96.63.172)
Timestamp:
03/12/08 18:13:24 (9 years ago)
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • documentation/tutorials/Dragging

    v8 v9  
    3333 * ''drag'': If the left mouse button is pressed while over the element and dragged, the element will trigger a 'dragstart' event. Every subsequent time the mouse is moved, the element will trigger a 'drag' event. When the button is released, the element will trigger a 'dragend' event. 
    3434 * ''dragdrop'': As ''drag'', but as the mouse moves over other elements 'dragover' and 'dragout' events will be triggered (similarly to the 'mouseover' and 'mouseout' events). When the button is released, the element the mouse is hovering over will trigger the 'dragdrop' message. 
    35  * ''clone'': As ''dragdrop'', but a clone of the element is attached to the mouse cursor during dragging. The clone has the pseudo-class 'drag' set on it to allow it to be differentiated
     35 * ''clone'': As ''dragdrop'', but a clone of the element is attached to the mouse cursor during dragging. The clone has the pseudo-class 'drag' set on it to allow it to be differentiated from the original element
    3636 
    3737So both ''drag'' and ''dragdrop'' only send messages; they don't actually drag any elements anywhere automatically. Very useful for complicated dragging operations or dragging multiple elements. 
    4040 
    4141== Step 3: Listening to the events == 
     42 
     43Now that the items can be visibly dragged around, we need to actually change their parenting when they're dropped. Create a class which inherits from Rocket::Core::EventListener and give it a static method for registering the containers. Override the ProcessEvent() function as well so we can process the 'dragdrop' event. 
     44 
     45{{{ 
     46#ifndef DRAGLISTENER_H 
     47#define DRAGLISTENER_H 
     48 
     49#include <Rocket/Core/EventListener.h> 
     50#include <Rocket/Core/Types.h> 
     51 
     52class DragListener : public Rocket::Core::EventListener 
     53{ 
     54public: 
     55        /// Registers an elemenet as being a container of draggable elements. 
     56        static void RegisterDraggableContainer(Rocket::Core::Element* element); 
     57 
     58protected: 
     59        virtual void ProcessEvent(Rocket::Core::Event& event); 
     60}; 
     61 
     62#endif 
     63}}} 
     64 
     65The RegisterDraggableContainer() function simply needs to attach the listener object to the 'dragdrop' event: 
     66 
     67{{{ 
     68#include "DragListener.h" 
     69#include <Rocket/Core/Element.h> 
     70 
     71static DragListener drag_listener; 
     72 
     73// Registers an element as being a container of draggable elements. 
     74void DragListener::RegisterDraggableContainer(Rocket::Core::Element* element) 
     75{ 
     76        element->AddEventListener("dragdrop", &drag_listener); 
     77} 
     78}}} 
     79 
     80The DragListener object will now receive a call to ProcessEvent() whenever an item is dropped on the registered elements or any of their children. 
     81 
     82=== The 'dragdrop' event === 
     83 
     84The event we'll be processing is the 'dragdrop' event. This event is sent to the element that the dragged element was dropped onto. The dragged element itself can be queried from the event as the parameter 'drag_element'. 
     85 
     86We can now write a simple handler that will move dragged elements between the two containers: 
     87 
     88{{{ 
     89void DragListener::ProcessEvent(Rocket::Core::Event& event) 
     90{ 
     91        if (event == "dragdrop") 
     92        { 
     93                Rocket::Core::Element* container = event.GetCurrentElement(); 
     94                Rocket::Core::Element* drag_element = static_cast< Rocket::Core::Element* >(event.GetParameter< void* >("drag_element", NULL)); 
     95 
     96                drag_element->GetParentNode()->RemoveChild(drag_element); 
     97                container->AppendChild(drag_element); 
     98        } 
     99} 
     100}}} 
     101 
     102The container that received the drop is determined by ''event.GetCurrentElement()''. Events have both a target and a current element. The ''target element'' is the element the event was actually targetted at. The ''current element'' is the element the processing listener is observing; in our case, the container. 
     103 
     104The dragged element is simply removed from its old parent and attached to the container it was dropped onto. 
     105 
     106=== Registering the containers === 
     107 
     108All that's left to do before we can try out the dragging is to register the containers. In the constructor of the Inventory object (top of Inventory.cpp), we need to register the inventory window as a draggable container; add the following line at the end of the constructor: 
     109 
     110{{{ 
     111        DragListener::RegisterDraggableContainer(document->GetElementById("content")); 
     112}}} 
     113 
     114Fire it up, start dragging and see what happens.