FifeGUI 0.2.0
A C++ GUI library designed for games.
dragdrop.hpp
1// SPDX-License-Identifier: LGPL-2.1-or-later OR BSD-3-Clause
2// SPDX-FileCopyrightText: 2013 - 2026 Fifengine contributors
3
4#ifndef INCLUDE_FIFECHAN_DRAGDROP_HPP
5#define INCLUDE_FIFECHAN_DRAGDROP_HPP
6
7// Standard library includes
8#include <cstdint>
9#include <functional>
10#include <memory>
11#include <string>
12#include <utility>
13
14// Platform config include
15#include "fifechan/platform.hpp"
16
17// Project headers (subdirs before local)
18#include <fifechan/gui.hpp>
19#include <fifechan/widget.hpp>
20
21namespace fcn
22{
23
24 class Graphics;
25
29 struct DragPoint
30 {
34 int x;
35
39 int y;
40
44 explicit DragPoint(int x_ = 0, int y_ = 0) : x(x_), y(y_)
45 {
46 }
47 };
48
52 enum class DragState : uint8_t
53 {
56
59 };
60
64 enum class DropResult : uint8_t
65 {
68
71
74 };
75
82 class FIFEGUI_API DragPayload
83 {
84 public:
88 using RenderCallback = std::function<void(Graphics&, DragPoint const & screenPos)>;
89
93 explicit DragPayload(std::shared_ptr<void> data = nullptr);
94
98 template <typename T>
99 std::shared_ptr<T> getShared() const
100 {
101 return std::static_pointer_cast<T>(m_data);
102 }
103
107 template <typename T>
108 T* get() const
109 {
110 return static_cast<T*>(m_data.get());
111 }
112
116 void setData(std::shared_ptr<void> data)
117 {
118 m_data = std::move(data);
119 }
120
123 bool hasData() const
124 {
125 return m_data != nullptr;
126 }
127
132 {
133 m_ghostRenderer = std::move(cb);
134 }
135
139 {
140 return m_ghostRenderer;
141 }
142
146 void setTooltip(std::string tip)
147 {
148 m_tooltip = std::move(tip);
149 }
150
153 std::string const & getTooltip() const
154 {
155 return m_tooltip;
156 }
157
158 private:
162 std::shared_ptr<void> m_data;
163
167 RenderCallback m_ghostRenderer;
168
172 std::string m_tooltip;
173 };
174
183 {
187 using GhostRenderer = std::function<void(Graphics&, DragPayload const &, DragPoint const &)>;
188
192 using HighlightRenderer = std::function<void(Graphics&, Widget* target, bool isValid)>;
193
198
203
208 };
209
216 class FIFEGUI_API DragHandler
217 {
218 public:
222 explicit DragHandler(Gui* gui);
223
227 DragState getState() const;
228
232 Widget* getHoveredWidget() const;
233
238
242 DragPayload const * getPayload() const;
243
247 bool beginDrag(Widget* source, std::unique_ptr<DragPayload> payload, int mouseX, int mouseY);
248
252 bool beginDragFromWidget(Widget* source, int mouseX, int mouseY);
253
257 void update(int mouseX, int mouseY);
258
262 DropResult drop(int mouseX, int mouseY);
263
267 void cancel();
268
272 void render(Graphics& graphics);
273
278
282 DragRenderConfig const & getRenderConfig() const;
283
287 void setModalWidget(Widget* modal);
288
292 Widget* getModalWidget() const;
293
297 static Widget* findWidgetAt(
298 Widget* root, int x, int y, bool mustBeVisible = true, bool mustBeEnabled = true);
299
300 private:
304 void distributeDragLeave();
305
309 void distributeDragEnter(Widget* candidate);
310
314 void updateActiveTarget();
315
319 Gui* m_gui{nullptr};
320
324 DragState m_state{DragState::Idle};
325
329 std::unique_ptr<DragPayload> m_payload;
330
334 Widget* m_sourceWidget{nullptr};
335
339 Widget* m_hoveredWidget{nullptr};
340
344 Widget* m_activeTarget{nullptr};
345
349 Widget* m_modalWidget{nullptr};
350
354 DragPoint m_dragOffset;
355
359 DragRenderConfig m_renderConfig{};
360
364 DragPoint m_lastMousePos;
365 };
366
367} // namespace fcn
368
369#endif // INCLUDE_FIFECHAN_DRAGDROP_HPP
DragPayload const * getPayload() const
Get the active payload.
Definition dragdrop.cpp:47
DropResult drop(int mouseX, int mouseY)
Drop at the given position.
Definition dragdrop.cpp:102
void render(Graphics &graphics)
Render drag related visuals.
Definition dragdrop.cpp:142
Widget * getHoveredWidget() const
Get the widget currently hovered by the drag.
Definition dragdrop.cpp:37
Widget * getModalWidget() const
Get the modal widget if any.
Definition dragdrop.cpp:161
bool beginDrag(Widget *source, std::unique_ptr< DragPayload > payload, int mouseX, int mouseY)
Start a drag with the given payload.
Definition dragdrop.cpp:52
Widget * getActiveDropTarget() const
Get the active drop target widget.
Definition dragdrop.cpp:42
void update(int mouseX, int mouseY)
Update drag position.
Definition dragdrop.cpp:70
DragRenderConfig const & getRenderConfig() const
Get the current render config.
Definition dragdrop.cpp:152
void setModalWidget(Widget *modal)
Set a modal widget to ignore hits.
Definition dragdrop.cpp:157
DragHandler(Gui *gui)
Create a drag handler for a GUI.
Definition dragdrop.cpp:28
static Widget * findWidgetAt(Widget *root, int x, int y, bool mustBeVisible=true, bool mustBeEnabled=true)
Find a widget at the given coordinates.
Definition dragdrop.cpp:166
void setRenderConfig(DragRenderConfig config)
Set how drag visuals are rendered.
Definition dragdrop.cpp:147
DragState getState() const
Get the current drag state.
Definition dragdrop.cpp:32
void cancel()
Cancel the current drag.
Definition dragdrop.cpp:128
bool beginDragFromWidget(Widget *source, int mouseX, int mouseY)
Start a drag from a widget without explicit payload.
Definition dragdrop.cpp:64
Container for arbitrary data carried during a drag operation.
Definition dragdrop.hpp:83
DragPayload(std::shared_ptr< void > data=nullptr)
Construct a payload.
Definition dragdrop.cpp:24
T * get() const
Get raw pointer to stored data.
Definition dragdrop.hpp:108
bool hasData() const
True if payload stores data.
Definition dragdrop.hpp:123
void setData(std::shared_ptr< void > data)
Set the stored data.
Definition dragdrop.hpp:116
void setTooltip(std::string tip)
Set tooltip text for the payload.
Definition dragdrop.hpp:146
std::shared_ptr< T > getShared() const
Get shared pointer to stored data.
Definition dragdrop.hpp:99
std::string const & getTooltip() const
Get the tooltip text.
Definition dragdrop.hpp:153
void setGhostRenderer(RenderCallback cb)
Set the ghost renderer callback.
Definition dragdrop.hpp:131
RenderCallback getGhostRenderer() const
Get the ghost renderer callback.
Definition dragdrop.hpp:138
std::function< void(Graphics &, DragPoint const &screenPos)> RenderCallback
Callback used to render a ghost image at a screen position.
Definition dragdrop.hpp:88
Abstract interface providing primitive drawing functions (lines, rectangles, etc.).
Definition graphics.hpp:58
The central GUI manager.
Definition gui.hpp:108
Abstract base class defining the common behavior, properties, and lifecycle of all GUI elements.
Definition widget.hpp:55
Used replacement tokens by configure_file():
DropResult
Result returned after attempting to drop payload onto a target.
Definition dragdrop.hpp:65
@ Accepted
Target accepted the drop.
Definition dragdrop.hpp:67
@ Cancelled
Drop was cancelled before delivery.
Definition dragdrop.hpp:73
@ Rejected
Target rejected the drop.
Definition dragdrop.hpp:70
DragState
State of the drag controller.
Definition dragdrop.hpp:53
@ Dragging
A drag operation is in progress.
Definition dragdrop.hpp:58
@ Idle
No drag is active.
Definition dragdrop.hpp:55
Simple 2D integer point used for drag offsets and positions.
Definition dragdrop.hpp:30
int y
Y coordinate.
Definition dragdrop.hpp:39
int x
X coordinate.
Definition dragdrop.hpp:34
DragPoint(int x_=0, int y_=0)
Construct a point with optional coordinates.
Definition dragdrop.hpp:44
Configuration for how drag visuals are rendered.
Definition dragdrop.hpp:183
HighlightRenderer highlight
Renderer used to draw a highlight on targets.
Definition dragdrop.hpp:202
static HighlightRenderer defaultHighlight()
Default highlight renderer.
GhostRenderer ghost
Renderer used to draw the ghost.
Definition dragdrop.hpp:197
std::function< void(Graphics &, DragPayload const &, DragPoint const &)> GhostRenderer
Renderer used to draw the ghost image.
Definition dragdrop.hpp:187
std::function< void(Graphics &, Widget *target, bool isValid)> HighlightRenderer
Renderer used to draw a highlight on candidate targets.
Definition dragdrop.hpp:192