-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTwoClickHandler.asc
290 lines (260 loc) · 6.54 KB
/
TwoClickHandler.asc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// label to use for text actions
Label* action;
void set_ActionLabel(this TwoClickHandler*, Label* label)
{
action = label;
action.Text = "";
}
Label* get_ActionLabel(this TwoClickHandler*)
{
return action;
}
// GUI to use as inventory
GUI* interface_inv;
void set_InventoryGUI(this TwoClickHandler*, GUI* invGui)
{
interface_inv = invGui;
}
GUI* get_InventoryGUI(this TwoClickHandler*)
{
return interface_inv;
}
// reversed control mode (left click to look)
bool classic = false;
void set_ReversedClicks(this TwoClickHandler*, bool reversed)
{
classic = reversed;
}
bool get_ReversedClicks(this TwoClickHandler*)
{
return classic;
}
static function TwoClickHandler::Close()
{
if (interface_inv != null)
{
interface_inv.Visible = false;
}
}
// popup distance from screen edge
int popup_distance = 0;
void set_PopupDistance(this TwoClickHandler*, int distance)
{
popup_distance = distance;
}
int get_PopupDistance(this TwoClickHandler*)
{
return popup_distance;
}
// popup distance proportional to GUI height
float popup_proportional = 0.75;
void set_PopupProportional(this TwoClickHandler*, float height)
{
popup_proportional = height;
}
float get_PopupProportional(this TwoClickHandler*)
{
return popup_proportional;
}
bool check_show_distance(int y)
{
if (y < popup_distance)
{
return true;
}
if (interface_inv != null &&
y < FloatToInt(IntToFloat(interface_inv.Height) * popup_proportional, eRoundNearest))
{
return true;
}
return false;
}
bool check_hide_distance(int y)
{
if (interface_inv == null)
{
return y > popup_distance;
}
return y > popup_distance &&
y > interface_inv.Height &&
y > FloatToInt(IntToFloat(interface_inv.Height) * popup_proportional, eRoundNearest);
}
MouseButton check_reversed(MouseButton button)
{
if (classic)
{
if (button == eMouseLeft) return eMouseRight;
if (button == eMouseRight) return eMouseLeft;
if (button == eMouseLeftInv) return eMouseRightInv;
if (button == eMouseRightInv) return eMouseLeftInv;
}
return button;
}
function do_room_action(MouseButton button)
{
if (GetLocationType(mouse.x, mouse.y) != eLocationNothing)
{
// clicked on something
if (player.ActiveInventory == null)
{
if (button == check_reversed(eMouseLeft))
{
// left click to interact with target
Room.ProcessClick(mouse.x, mouse.y, eModeInteract);
}
else
{
// right click to look at target
Room.ProcessClick(mouse.x, mouse.y, eModeLookat);
}
}
else
{
if (button == check_reversed(eMouseLeft))
{
// left click to use inventory on target
Room.ProcessClick(mouse.x, mouse.y, eModeUseinv);
}
else
{
// right click to deselect inventory item
player.ActiveInventory = null;
}
}
}
else
{
// click on nothing
if (player.ActiveInventory == null)
{
// left click to walk
Room.ProcessClick(mouse.x, mouse.y, eModeWalkto);
}
else
{
// right click to deselect inventory item
player.ActiveInventory = null;
}
}
}
function do_inventory_action(MouseButton button, InventoryItem* item)
{
if (button == check_reversed(eMouseLeftInv))
{
if (player.ActiveInventory == null)
{
if (item.GetProperty("InstantUse") == true)
{
// left click to instant use (if property is set)
item.RunInteraction(eModeInteract);
}
else
{
// left click to set active inventory item
player.ActiveInventory = item;
}
}
else if (item.ID != player.ActiveInventory.ID)
{
// left click to use active inventory on another item
item.RunInteraction(eModeUseinv);
}
else
{
// left click item on itself to deselect it
player.ActiveInventory = null;
}
}
else
{
// right click to look at inventory item
item.RunInteraction(eModeLookat);
}
}
//----------------------------------------------------------------------------------------------------
// on_mouse_click()
//----------------------------------------------------------------------------------------------------
function on_mouse_click(MouseButton button)
{
// when mouse is clicked, text label is cleared
if (action != null)
{
action.Text = "";
}
if (!IsGamePaused() && (button == eMouseLeft || button == eMouseRight))
{
do_room_action(button);
}
else if (button == eMouseLeftInv || button == eMouseRightInv)
{
// InventoryItem.GetAtScreenXY could return null here
// so using game.inv_activated instead is a safer option
do_inventory_action(button, inventory[game.inv_activated]);
}
}
//----------------------------------------------------------------------------------------------------
// repeatedly_execute()
//----------------------------------------------------------------------------------------------------
function repeatedly_execute()
{
// Inventory GUI:
if (interface_inv == null)
{
// pass
}
else if (interface_inv.Visible && check_hide_distance(mouse.y))
{
interface_inv.Visible = false;
}
else if (!IsGamePaused() && !interface_inv.Visible && check_show_distance(mouse.y))
{
// make visible when the game is not paused and the cursor is within the popup position
interface_inv.Visible = true;
}
// Action Text
// We always display the name of what is under the mouse, with one exception:
// IF the player has an inventory item selected and hovers over the same inventory item,
// we display nothing to indicate that an item can not be used on itself
if (player.ActiveInventory == null)
{
if (action != null && !IsGamePaused())
{
action.Text = Game.GetLocationName(mouse.x, mouse.y);
}
}
else
{
InventoryItem *item = InventoryItem.GetAtScreenXY(mouse.x, mouse.y);
if (action == null)
{
// pass
}
else if (item != null && item.ID == player.ActiveInventory.ID)
{
action.Text = "";
}
else
{
action.Text = Game.GetLocationName(mouse.x, mouse.y);
}
}
}
// handle clicks in the inventory area that are not on an inventory item
function on_event(EventType event, int data)
{
if (event == eEventGUIMouseDown &&
interface_inv != null &&
data == interface_inv.ID &&
InventoryItem.GetAtScreenXY(mouse.x, mouse.y) == null)
{
GUIControl* control = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
if (control == null || control.AsInvWindow == null)
{
// pass
}
else if (player.ActiveInventory != null)
{
player.ActiveInventory = null;
}
}
}