1 /**
2  * @file
3  * @brief The <code>zone</code> node allow to create an hidden active node.
4  * Currently we only use it to support repeat mouse actions without merging
5  * the code which managing this feature.
6  */
7 
8 /*
9 Copyright (C) 2002-2013 UFO: Alien Invasion.
10 
11 This program is free software; you can redistribute it and/or
12 modify it under the terms of the GNU General Public License
13 as published by the Free Software Foundation; either version 2
14 of the License, or (at your option) any later version.
15 
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 
20 See the GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
25 
26 */
27 
28 #include "../ui_nodes.h"
29 #include "../ui_parse.h"
30 #include "../ui_behaviour.h"
31 #include "../ui_input.h"
32 #include "../ui_timer.h"
33 #include "../ui_actions.h"
34 #include "ui_node_zone.h"
35 #include "ui_node_window.h"
36 
37 #include "../../input/cl_keys.h"
38 
39 #define EXTRADATA_TYPE zoneExtraData_t
40 #define EXTRADATA(node) UI_EXTRADATA(node, EXTRADATA_TYPE)
41 
42 static uiTimer_t* capturedTimer;
43 
UI_ZoneNodeRepeat(uiNode_t * node,uiTimer_t * timer)44 static void UI_ZoneNodeRepeat (uiNode_t* node, uiTimer_t* timer)
45 {
46 	if (node->onClick) {
47 		UI_ExecuteEventActions(node, node->onClick);
48 	}
49 }
50 
onMouseDown(uiNode_t * node,int x,int y,int button)51 void uiZoneNode::onMouseDown (uiNode_t* node, int x, int y, int button)
52 {
53 	if (!EXTRADATA(node).repeat)
54 		return;
55 	if (button == K_MOUSE1) {
56 		UI_SetMouseCapture(node);
57 		capturedTimer = UI_AllocTimer(node, EXTRADATA(node).clickDelay, UI_ZoneNodeRepeat);
58 		UI_TimerStart(capturedTimer);
59 	}
60 }
61 
onMouseUp(uiNode_t * node,int x,int y,int button)62 void uiZoneNode::onMouseUp (uiNode_t* node, int x, int y, int button)
63 {
64 	if (!EXTRADATA(node).repeat)
65 		return;
66 	if (button == K_MOUSE1) {
67 		UI_MouseRelease();
68 	}
69 }
70 
71 /**
72  * @brief Called when the node have lost the captured node
73  * We clean cached data
74  */
onCapturedMouseLost(uiNode_t * node)75 void uiZoneNode::onCapturedMouseLost (uiNode_t* node)
76 {
77 	if (capturedTimer) {
78 		UI_TimerRelease(capturedTimer);
79 		capturedTimer = nullptr;
80 	}
81 }
82 
83 /**
84  * @brief Call before the script initialized the node
85  */
onLoading(uiNode_t * node)86 void uiZoneNode::onLoading (uiNode_t* node)
87 {
88 	EXTRADATA(node).clickDelay = 1000;
89 }
90 
UI_RegisterZoneNode(uiBehaviour_t * behaviour)91 void UI_RegisterZoneNode (uiBehaviour_t* behaviour)
92 {
93 	behaviour->name = "zone";
94 	behaviour->manager = UINodePtr(new uiZoneNode());
95 	behaviour->extraDataSize = sizeof(EXTRADATA_TYPE);
96 
97 	/* If true, the <code>onclick</code> call back is called more than one time if the user does not release the button. */
98 	UI_RegisterExtradataNodeProperty(behaviour, "repeat", V_BOOL, zoneExtraData_t, repeat);
99 	/* Delay is used between 2 calls of <code>onclick</code>. */
100 	UI_RegisterExtradataNodeProperty(behaviour, "clickdelay", V_INT, zoneExtraData_t, clickDelay);
101 }
102