| 42 | |
---|
| 43 | Now 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 | |
---|
| 52 | class DragListener : public Rocket::Core::EventListener |
---|
| 53 | { |
---|
| 54 | public: |
---|
| 55 | /// Registers an elemenet as being a container of draggable elements. |
---|
| 56 | static void RegisterDraggableContainer(Rocket::Core::Element* element); |
---|
| 57 | |
---|
| 58 | protected: |
---|
| 59 | virtual void ProcessEvent(Rocket::Core::Event& event); |
---|
| 60 | }; |
---|
| 61 | |
---|
| 62 | #endif |
---|
| 63 | }}} |
---|
| 64 | |
---|
| 65 | The 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 | |
---|
| 71 | static DragListener drag_listener; |
---|
| 72 | |
---|
| 73 | // Registers an element as being a container of draggable elements. |
---|
| 74 | void DragListener::RegisterDraggableContainer(Rocket::Core::Element* element) |
---|
| 75 | { |
---|
| 76 | element->AddEventListener("dragdrop", &drag_listener); |
---|
| 77 | } |
---|
| 78 | }}} |
---|
| 79 | |
---|
| 80 | The 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 | |
---|
| 84 | The 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 | |
---|
| 86 | We can now write a simple handler that will move dragged elements between the two containers: |
---|
| 87 | |
---|
| 88 | {{{ |
---|
| 89 | void 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 | |
---|
| 102 | The 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 | |
---|
| 104 | The dragged element is simply removed from its old parent and attached to the container it was dropped onto. |
---|
| 105 | |
---|
| 106 | === Registering the containers === |
---|
| 107 | |
---|
| 108 | All 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 | |
---|
| 114 | Fire it up, start dragging and see what happens. |