UFO: Alien Invasion
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ui_dragndrop.cpp
Go to the documentation of this file.
1 
5 /*
6 Copyright (C) 2002-2020 UFO: Alien Invasion.
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 
17 See the GNU General Public License for more details.
18 
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 
23 */
24 
25 #include "ui_internal.h"
26 #include "ui_dragndrop.h"
27 #include "ui_input.h"
28 #include "ui_node.h"
29 #include "ui_sound.h"
30 
32 #include "node/ui_node_container.h"
33 
34 #include "../input/cl_input.h"
35 
36 static int oldMousePosX = -1;
37 static int oldMousePosY = -1;
39 static bool nodeAcceptDND = false;
40 static bool positionAcceptDND = false;
52 bool UI_DNDIsDragging (void)
53 {
54  return objectType != DND_NOTHING;
55 }
56 
61 {
62  if (!UI_DNDIsDragging())
63  return false;
64  return targetNode == node;
65 }
66 
71 {
72  if (!UI_DNDIsDragging())
73  return false;
74  return sourceNode == node;
75 }
76 
80 int UI_DNDGetType (void)
81 {
82  return objectType;
83 }
84 
89 {
90  assert(UI_DNDIsDragging());
91  return targetNode;
92 }
93 
98 {
99  assert(UI_DNDIsDragging());
100  return sourceNode;
101 }
102 
109 static void UI_DNDDrag (uiNode_t* node)
110 {
111  assert(!UI_DNDIsDragging());
113  sourceNode = node;
114 
115  UI_PlaySound("item-drag");
116 }
117 
124 void UI_DNDDragItem (uiNode_t* node, const Item* item)
125 {
126  UI_DNDDrag(node);
127  assert(UI_DNDIsDragging());
129  draggingItem = *item;
130 }
131 
135 static inline void UI_DNDCleanup (void)
136 {
138  targetNode = nullptr;
139  sourceNode = nullptr;
140 }
141 
147 void UI_DNDAbort (void)
148 {
149  assert(UI_DNDIsDragging());
150  assert(objectType != DND_NOTHING);
151  assert(sourceNode != nullptr);
152 
153  if (nodeAcceptDND && targetNode) {
154  UI_Node_DndLeave(targetNode);
155  }
156  UI_Node_DndFinished(sourceNode, false);
157 
158  UI_DNDCleanup();
160 }
161 
167 void UI_DNDDrop (void)
168 {
169  bool result = false;
170  assert(UI_DNDIsDragging());
171  assert(objectType != DND_NOTHING);
172  assert(sourceNode != nullptr);
173 
174  if (!positionAcceptDND) {
175  UI_DNDAbort();
176  return;
177  }
178 
179  if (targetNode) {
180  result = UI_Node_DndDrop(targetNode, mousePosX, mousePosY);
181  }
182  UI_Node_DndFinished(sourceNode, result);
183 
184  UI_PlaySound("item-drop");
185 
186  UI_DNDCleanup();
188 }
189 
191 {
192  assert(objectType == DND_ITEM);
193  return &draggingItem;
194 }
195 
199 static void UI_DNDMouseMove (int mousePosX, int mousePosY)
200 {
201  uiNode_t* node = UI_GetNodeAtPosition(mousePosX, mousePosY);
202 
203  if (node != targetNode) {
204  if (nodeAcceptDND && targetNode) {
205  UI_Node_DndLeave(targetNode);
206  }
207  targetNode = node;
208  if (targetNode) {
209  nodeAcceptDND = UI_Node_DndEnter(targetNode);
210  }
211  }
212 
213  if (targetNode == nullptr) {
214  nodeAcceptDND = false;
215  positionAcceptDND = false;
216  return;
217  }
218 
219  if (!nodeAcceptDND) {
220  positionAcceptDND = false;
221  return;
222  }
223 
224  positionAcceptDND = UI_Node_DndMove(targetNode, mousePosX, mousePosY);
225 }
226 
231 {
232  const vec3_t scale = { 3.5, 3.5, 3.5 };
233  vec3_t orgine;
234  vec4_t color = { 1, 1, 1, 1 };
235 
236  /* check mouse move */
237  if (mousePosX != oldMousePosX || mousePosY != oldMousePosY) {
240  UI_DNDMouseMove(mousePosX, mousePosY);
241  }
242 
243  /* draw the dragging item */
244 
245  VectorSet(orgine, mousePosX, mousePosY, -50);
246 
247  /* Tune down the opacity of the cursor-item if the preview item is drawn. */
248  if (positionAcceptDND)
249  color[3] = 0.2;
250 
251  switch (objectType) {
252  case DND_ITEM:
253  UI_DrawItem(nullptr, orgine, &draggingItem, -1, -1, scale, color);
254  break;
255 
256  default:
257  assert(false);
258  }
259 }
static void UI_DNDDrag(uiNode_t *node)
Private function to initialize a the start of a DND.
int UI_DNDGetType(void)
Return the current type of the dragging object, else DND_NOTHING.
uiNode_t * UI_DNDGetTargetNode(void)
Return target of the DND.
#define VectorSet(v, x, y, z)
Definition: vector.h:59
static int oldMousePosX
Item * UI_DNDGetItem(void)
static uiNode_t * sourceNode
uiDNDType_t
Definition: ui_dragndrop.h:32
static const vec3_t scale
void UI_DNDDragItem(uiNode_t *node, const Item *item)
Start to drag an item.
uiNode_t * UI_GetNodeAtPosition(int x, int y)
Return the first visible node at a position.
Definition: ui_nodes.cpp:527
void UI_DNDAbort(void)
Drop the object at the current position.
void UI_PlaySound(const char *soundFile)
Plays a ui sound.
Definition: ui_sound.cpp:35
static uiNode_t * targetNode
static Item draggingItem
void UI_InvalidateMouse(void)
Force to invalidate the mouse position and then to update hover nodes...
Definition: ui_input.cpp:560
static bool positionAcceptDND
uiNode_t * UI_DNDGetSourceNode(void)
Return source of the DND.
static uiDNDType_t objectType
bool UI_Node_DndFinished(uiNode_t *node, bool isDroped)
Definition: ui_node.cpp:327
Internal data use by the UI package.
C interface to allow to access to cpp node code.
bool UI_Node_DndEnter(uiNode_t *node)
Definition: ui_node.cpp:303
item instance data, with linked list capability
Definition: inv_shared.h:402
int mousePosY
Definition: cl_input.cpp:80
static bool nodeAcceptDND
bool UI_Node_DndMove(uiNode_t *node, int x, int y)
Definition: ui_node.cpp:309
bool UI_DNDIsTargetNode(uiNode_t *node)
Return true if the requested node is the current target of the DND.
Atomic structure used to define most of the UI.
Definition: ui_nodes.h:80
bool UI_Node_DndDrop(uiNode_t *node, int x, int y)
Definition: ui_node.cpp:321
void UI_DrawDragAndDrop(int mousePosX, int mousePosY)
Draw to dragging object and catch mouse move event.
static void UI_DNDCleanup(void)
Cleanup data about DND.
bool UI_DNDIsSourceNode(uiNode_t *node)
Return true if the requested node is the source of the DND.
void UI_Node_DndLeave(uiNode_t *node)
Definition: ui_node.cpp:315
vec_t vec3_t[3]
Definition: ufotypes.h:39
static int oldMousePosY
static void UI_DNDMouseMove(int mousePosX, int mousePosY)
Manage the DND when we move the mouse.
bool UI_DNDIsDragging(void)
Return true if we are dragging something.
int mousePosX
Definition: cl_input.cpp:80
void UI_DrawItem(uiNode_t *node, const vec3_t org, const Item *item, int x, int y, const vec3_t scale, const vec4_t color)
Draws an item to the screen.
void UI_DNDDrop(void)
Drop the object at the current position.
vec_t vec4_t[4]
Definition: ufotypes.h:40