5#include <fifechan/dragdrop.hpp>
12#include "fifechan/platform.hpp"
15#include <fifechan/events/dragevent.hpp>
16#include <fifechan/graphics.hpp>
17#include <fifechan/listeners/droptargetlistener.hpp>
18#include <fifechan/widget.hpp>
39 return m_hoveredWidget;
44 return m_activeTarget;
49 return m_payload.
get();
57 m_sourceWidget = source;
58 m_payload = std::move(payload);
60 m_lastMousePos =
DragPoint(mouseX, mouseY);
75 m_lastMousePos =
DragPoint(mouseX, mouseY);
77 if (m_gui ==
nullptr) {
83 if (candidate != m_hoveredWidget) {
85 m_hoveredWidget = candidate;
89 if (m_activeTarget !=
nullptr) {
92 m_activeTarget->getAbsolutePosition(absX, absY);
93 int const localX = mouseX - absX;
94 int const localY = mouseY - absY;
96 m_activeTarget, m_payload.get(), DragEvent::Type::Hover, localX, localY, mouseX, mouseY);
97 m_activeTarget->distributeDragHover(hoverEvt);
107 if (m_activeTarget !=
nullptr) {
108 int const mouseX = m_lastMousePos.x;
109 int const mouseY = m_lastMousePos.y;
112 m_activeTarget->getAbsolutePosition(absX, absY);
113 int const localX = mouseX - absX;
114 int const localY = mouseY - absY;
115 DragEvent dropEvt(m_activeTarget, m_payload.get(), DragEvent::Type::Drop, localX, localY, mouseX, mouseY);
116 m_activeTarget->distributeDragDrop(dropEvt);
119 m_activeTarget =
nullptr;
134 if (m_activeTarget !=
nullptr) {
135 distributeDragLeave();
136 m_activeTarget =
nullptr;
149 m_renderConfig = std::move(config);
154 return m_renderConfig;
159 m_modalWidget = modal;
163 return m_modalWidget;
169 if (root ==
nullptr) {
175 void DragHandler::distributeDragLeave()
177 if (m_activeTarget ==
nullptr || m_payload ==
nullptr) {
181 int const mouseX = m_lastMousePos.
x;
182 int const mouseY = m_lastMousePos.
y;
186 int const localX = mouseX - absX;
187 int const localY = mouseY - absY;
188 DragEvent leaveEvt(m_activeTarget, m_payload.get(), DragEvent::Type::Leave, localX, localY, mouseX, mouseY);
192 void DragHandler::distributeDragEnter(
Widget* candidate)
194 if (candidate ==
nullptr || m_payload ==
nullptr) {
198 int const mouseX = m_lastMousePos.x;
199 int const mouseY = m_lastMousePos.y;
202 candidate->getAbsolutePosition(absX, absY);
203 int const localX = mouseX - absX;
204 int const localY = mouseY - absY;
205 DragEvent enterEvt(candidate, m_payload.get(), DragEvent::Type::Enter, localX, localY, mouseX, mouseY);
206 bool const accepted = candidate->distributeDragEnter(enterEvt);
208 m_activeTarget = candidate;
212 void DragHandler::updateActiveTarget()
215 if (m_activeTarget !=
nullptr && m_hoveredWidget != m_activeTarget) {
216 distributeDragLeave();
217 m_activeTarget =
nullptr;
221 if (m_hoveredWidget !=
nullptr && m_activeTarget ==
nullptr) {
222 distributeDragEnter(m_hoveredWidget);
Drag and drop event type container.
DragPayload const * getPayload() const
Get the active payload.
DropResult drop(int mouseX, int mouseY)
Drop at the given position.
void render(Graphics &graphics)
Render drag related visuals.
Widget * getHoveredWidget() const
Get the widget currently hovered by the drag.
Widget * getModalWidget() const
Get the modal widget if any.
bool beginDrag(Widget *source, std::unique_ptr< DragPayload > payload, int mouseX, int mouseY)
Start a drag with the given payload.
Widget * getActiveDropTarget() const
Get the active drop target widget.
void update(int mouseX, int mouseY)
Update drag position.
DragRenderConfig const & getRenderConfig() const
Get the current render config.
void setModalWidget(Widget *modal)
Set a modal widget to ignore hits.
DragHandler(Gui *gui)
Create a drag handler for a GUI.
static Widget * findWidgetAt(Widget *root, int x, int y, bool mustBeVisible=true, bool mustBeEnabled=true)
Find a widget at the given coordinates.
void setRenderConfig(DragRenderConfig config)
Set how drag visuals are rendered.
DragState getState() const
Get the current drag state.
void cancel()
Cancel the current drag.
bool beginDragFromWidget(Widget *source, int mouseX, int mouseY)
Start a drag from a widget without explicit payload.
Container for arbitrary data carried during a drag operation.
DragPayload(std::shared_ptr< void > data=nullptr)
Construct a payload.
T * get() const
Get raw pointer to stored data.
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Used replacement tokens by configure_file():
DropResult
Result returned after attempting to drop payload onto a target.
@ Accepted
Target accepted the drop.
@ Cancelled
Drop was cancelled before delivery.
DragState
State of the drag controller.
@ Dragging
A drag operation is in progress.
Simple 2D integer point used for drag offsets and positions.
Configuration for how drag visuals are rendered.