ui_dragndrop.c

Go to the documentation of this file.
00001 
00005 /*
00006 Copyright (C) 2002-2010 UFO: Alien Invasion.
00007 
00008 This program is free software; you can redistribute it and/or
00009 modify it under the terms of the GNU General Public License
00010 as published by the Free Software Foundation; either version 2
00011 of the License, or (at your option) any later version.
00012 
00013 This program is distributed in the hope that it will be useful,
00014 but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00016 
00017 See the GNU General Public License for more details.
00018 
00019 You should have received a copy of the GNU General Public License
00020 along with this program; if not, write to the Free Software
00021 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00022 
00023 */
00024 
00025 #include "ui_dragndrop.h"
00026 #include "ui_input.h"
00027 #include "ui_sound.h"
00028 
00029 #include "node/ui_node_abstractnode.h"
00030 #include "node/ui_node_container.h"
00031 
00032 #include "../input/cl_input.h"
00033 
00034 static int oldMousePosX = -1;               
00035 static int oldMousePosY = -1;               
00037 static qboolean nodeAcceptDND = qfalse;     
00038 static qboolean positionAcceptDND = qfalse; 
00040 static uiDNDType_t objectType;              
00041 static item_t draggingItem;                 
00043 static uiNode_t *sourceNode;                
00044 static uiNode_t *targetNode;                
00050 qboolean UI_DNDIsDragging (void)
00051 {
00052     return objectType != DND_NOTHING;
00053 }
00054 
00058 qboolean UI_DNDIsTargetNode (struct uiNode_s *node)
00059 {
00060     if (!UI_DNDIsDragging())
00061         return qfalse;
00062     return targetNode == node;
00063 }
00064 
00068 qboolean UI_DNDIsSourceNode (struct uiNode_s *node)
00069 {
00070     if (!UI_DNDIsDragging())
00071         return qfalse;
00072     return sourceNode == node;
00073 }
00074 
00078 int UI_DNDGetType (void)
00079 {
00080     return objectType;
00081 }
00082 
00086 uiNode_t *UI_DNDGetTargetNode (void)
00087 {
00088     assert(UI_DNDIsDragging());
00089     return targetNode;
00090 }
00091 
00095 uiNode_t *UI_DNDGetSourceNode (void)
00096 {
00097     assert(UI_DNDIsDragging());
00098     return sourceNode;
00099 }
00100 
00107 static void UI_DNDDrag (uiNode_t *node)
00108 {
00109     assert(!UI_DNDIsDragging());
00110     objectType = DND_SOMETHING;
00111     sourceNode = node;
00112 
00113     UI_PlaySound("item-drag");
00114 }
00115 
00122 void UI_DNDDragItem (uiNode_t *node, const item_t *item)
00123 {
00124     UI_DNDDrag(node);
00125     assert(UI_DNDIsDragging());
00126     objectType = DND_ITEM;
00127     draggingItem = *item;
00128 }
00129 
00133 static inline void UI_DNDCleanup (void)
00134 {
00135     objectType = DND_NOTHING;
00136     targetNode = NULL;
00137     sourceNode = NULL;
00138 }
00139 
00145 void UI_DNDAbort (void)
00146 {
00147     assert(UI_DNDIsDragging());
00148     assert(objectType != DND_NOTHING);
00149     assert(sourceNode != NULL);
00150 
00151     if (nodeAcceptDND && targetNode) {
00152         targetNode->behaviour->dndLeave(targetNode);
00153     }
00154     sourceNode->behaviour->dndFinished(sourceNode, qfalse);
00155 
00156     UI_DNDCleanup();
00157     UI_InvalidateMouse();
00158 }
00159 
00165 void UI_DNDDrop (void)
00166 {
00167     qboolean result = qfalse;
00168     assert(UI_DNDIsDragging());
00169     assert(objectType != DND_NOTHING);
00170     assert(sourceNode != NULL);
00171 
00172     if (!positionAcceptDND) {
00173         UI_DNDAbort();
00174         return;
00175     }
00176 
00177     if (targetNode) {
00178         result = targetNode->behaviour->dndDrop(targetNode, mousePosX, mousePosY);
00179     }
00180     sourceNode->behaviour->dndFinished(sourceNode, result);
00181 
00182     UI_PlaySound("item-drop");
00183 
00184     UI_DNDCleanup();
00185     UI_InvalidateMouse();
00186 }
00187 
00188 item_t *UI_DNDGetItem (void)
00189 {
00190     assert(objectType == DND_ITEM);
00191     return &draggingItem;
00192 }
00193 
00197 static void UI_DNDMouseMove (int mousePosX, int mousePosY)
00198 {
00199     uiNode_t *node = UI_GetNodeAtPosition(mousePosX, mousePosY);
00200 
00201     if (node != targetNode) {
00202         if (nodeAcceptDND && targetNode) {
00203             targetNode->behaviour->dndLeave(targetNode);
00204         }
00205         targetNode = node;
00206         if (targetNode) {
00207             nodeAcceptDND = targetNode->behaviour->dndEnter(targetNode);
00208         }
00209     }
00210 
00211     if (targetNode == NULL) {
00212         nodeAcceptDND = qfalse;
00213         positionAcceptDND = qfalse;
00214         return;
00215     }
00216 
00217     if (!nodeAcceptDND) {
00218         positionAcceptDND = qfalse;
00219         return;
00220     }
00221 
00222     positionAcceptDND = node->behaviour->dndMove(targetNode, mousePosX, mousePosY);
00223 }
00224 
00225 
00229 void UI_DrawDragAndDrop (int mousePosX, int mousePosY)
00230 {
00231     const vec3_t scale = { 3.5, 3.5, 3.5 };
00232     vec3_t orgine;
00233     vec4_t color = { 1, 1, 1, 1 };
00234 
00235     /* check mouse move */
00236     if (mousePosX != oldMousePosX || mousePosY != oldMousePosY) {
00237         oldMousePosX = mousePosX;
00238         oldMousePosY = mousePosY;
00239         UI_DNDMouseMove(mousePosX, mousePosY);
00240     }
00241 
00242     /* draw the dragging item */
00243 
00244     VectorSet(orgine, mousePosX, mousePosY, -50);
00245 
00246     /* Tune down the opacity of the cursor-item if the preview item is drawn. */
00247     if (positionAcceptDND)
00248         color[3] = 0.2;
00249 
00250     switch (objectType) {
00251     case DND_ITEM:
00252         UI_DrawItem(NULL, orgine, &draggingItem, -1, -1, scale, color);
00253         break;
00254 
00255     default:
00256         assert(qfalse);
00257     }
00258 }

Generated by  doxygen 1.6.2