File: Script.c

package info (click to toggle)
openclonk 8.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 169,516 kB
  • sloc: cpp: 180,479; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 145; sh: 101; javascript: 34
file content (305 lines) | stat: -rw-r--r-- 7,358 bytes parent folder | download | duplicates (5)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/** 
	Tutorial Guide
	The tutorial guide can be clicked by the player, it supplies the player with information and hints.
	The following callbacks are made to the scenario script:
	 * OnGuideMessageShown(int plr, int index) when a message is shown
	 * OnGuideMessageRemoved(int plr, int index) when a message is removed, all events
	
	@author Maikel
*/


local messages; // A container to hold all messages.
local message_open; // Currently open message.
local close_on_last; // Close guide on after last message.

protected func Initialize()
{
	// Visibility
	this.Visibility = VIS_Owner;
	messages = [];
	message_open = nil;
	close_on_last = false;
	
	// Initialize menu properties.
	InitializeMenu();
	
	// create the main menu
	UpdateGuideMenu("");	
	return;
}

/* Adds a message to the guide. The internal index is set to this message meaning that this message will
* be shown if the player clicks the guide.
* @param msg Message that should be added to the message stack.
*/
public func AddGuideMessage(string msg)
{
	// Add message to list.
	messages[GetLength(messages)] = msg;
	// Update the menu, because of the next button.
	if (message_open != nil)
		ShowGuideMenu(message_open);
	return;
}

/* Shows a guide message to the player, also resets the internal index to that point.
*	@param show_index The message corresponding to this index will be shown, if nil the last message will be shown.
*/
public func ShowGuideMessage(int show_index)
{
	// If the index is not specified show last message, index must be between first and last message.
	if (show_index == nil)
		show_index = GetLength(messages) - 1;
	show_index = BoundBy(show_index, 0, GetLength(messages) - 1);
	// Show the guide message.
	ShowGuideMenu(show_index);
	return;
}

// Hides the guide and its menu to the player.
public func HideGuide()
{
	if (this.Visibility = VIS_Owner)
	{
		// Change visibility and do script callback.
		this.Visibility = VIS_None;
		GameCall("OnGuideMessageRemoved", GetOwner(), message_open);
	}
	return;
}

// Shows the guide and its menu to the player, if it was hidden before.
public func ShowGuide()
{
	if (this.Visibility = VIS_None)
	{
		// Change visibility and do script callback.
		this.Visibility = VIS_Owner;
		GameCall("OnGuideMessageShown", GetOwner(), message_open);
	}
	return;
}

// Returns whether the guide is currently hidden.
public func IsHidden()
{
	return this.Visibility == VIS_None;
}

public func EnableCloseOnLastMessage(bool disable)
{
	close_on_last = !disable;
	return;
}

public func GetMessageCount()
{
	return GetLength(messages);
}

protected func Destruction()
{
	CloseGuideMenu();
	return;
}


/*-- Menu implementation --*/

// Menu IDs.
local id_menu;

// Menu proplists.
local prop_menu;
local prop_next;
local prop_prev;

private func InitializeMenu()
{
	var menu_width = 25; // in percentage of whole screen
	var meny_offset = 4; // in em
	var menu_height = 6; // in em, should correspond to six lines of text
	var text_margin = 1; // margins in 1/10 em
	
	// Menu proplist.
	prop_menu =
	{
		Target = this,
		Style = GUI_Multiple,
		Decoration = GUI_MenuDeco,
		Left = Format("%d%%", 50 - menu_width),
		Right = Format("%d%%", 50 + menu_width),
		Top = Format("0%%+%dem", meny_offset),
		Bottom = Format("0%%+%dem", meny_offset + menu_height),
		BackgroundColor = {Std = 0},
	};

	// Submenu proplists.
	var prop_guide = 
	{
		Target = this,
		ID = 1,
		Right = Format("0%%+%dem", menu_height),
		Symbol = GetID(),
	};
	var prop_text = 
	{
		Left = Format("0%%%s", ToEmString(10 * menu_height + text_margin)),
		// 'Right' will be set on update
		Target = this,
		ID = 2,
		Text = nil,
	};	
	prop_next =
	{
		Target = this,
		ID = 3,
		Left = Format("100%%-%dem", menu_height / 2),
		Top = Format("100%%-%dem", menu_height / 2),
		Symbol = Icon_Arrow,
		GraphicsName = "Down",
		Text = "$MsgNext$",
		Style = GUI_TextHCenter | GUI_TextVCenter,
		BackgroundColor = {Std = 0, Hover = 0x50ffff00},
		OnMouseIn = GuiAction_SetTag("Hover"),
		OnMouseOut = GuiAction_SetTag("Std"),
		OnClick = GuiAction_Call(this, "ShowNextMessage"),
	};
	prop_prev =
	{
		Target = this,
		ID = 4,
		Left = Format("100%%-%dem", menu_height / 2),
		Bottom = Format("0%%+%dem", menu_height / 2),
		Symbol = Icon_Arrow,
		GraphicsName = "Up",
		Text = "$MsgPrevious$",
		Style = GUI_TextHCenter | GUI_TextVCenter,
		BackgroundColor = {Std = 0, Hover = 0x50ffff00},
		OnMouseIn = GuiAction_SetTag("Hover"),
		OnMouseOut = GuiAction_SetTag("Std"),
		OnClick = GuiAction_Call(this, "ShowPreviousMessage"),
	};
	
	// Add menu elements.
	prop_menu.guide = prop_guide;
	prop_menu.text = prop_text;
	prop_menu.next = prop_next;
	prop_menu.prev = prop_prev;
	
	// Menu ID.
	id_menu = GuiOpen(prop_menu);
	return;
}

private func ShowGuideMenu(int index)
{
	// There will always be the removal of the previous message.
	if (message_open != nil && !IsHidden())
		GameCall("OnGuideMessageRemoved", GetOwner(), message_open);

	// Show the new message.
	var message = messages[index];
	if (!message)
		return;
	var has_next = index < GetLength(messages) - 1;
	var has_prev = index > 0;
	var has_close = close_on_last && !has_next;
	UpdateGuideMenu(message, has_next, has_prev, has_close);
	message_open = index;

	// Notify the scenario script.
	if (message_open != nil && !IsHidden())	
		GameCall("OnGuideMessageShown", GetOwner(), message_open);
	return;
}

private func UpdateGuideMenu(string guide_message, bool has_next, bool has_prev, bool has_close)
{
	// Update the text message entry.
	prop_menu.text.Text = guide_message;

	// Don't usually leave a margin for the text - just when actually showing buttons.
	var is_showing_buttons = has_next || has_close || has_prev;
	
	var text_right_side = "100%";
	if (is_showing_buttons)
	{
		text_right_side = Format("100%-2.9em");
	}
	GuiUpdate({Right = text_right_side, Text = guide_message}, id_menu, prop_menu.text.ID, this);
	
	// Update the next/close button.
	if (has_next || has_close)
	{
		prop_menu.next = prop_next;
		if (has_next)
		{
			prop_menu.next.Symbol = Icon_Arrow;
			prop_menu.next.GraphicsName = "Down";
			prop_menu.next.Text = "$MsgNext$";
		}
		else if (has_close)
		{
			prop_menu.next.Symbol = Icon_Cancel;
			prop_menu.next.Text = "$MsgClose$";
		}
		GuiUpdate(prop_menu, id_menu, prop_menu.ID, this);
	}
	else if (prop_menu.next != nil)
	{
		GuiClose(id_menu, prop_menu.next.ID, this);
		prop_menu.next = nil;
	}
		
	// Update the previous button.	
	if (has_prev)
	{
		prop_menu.prev = prop_prev;
		GuiUpdate(prop_menu, id_menu, prop_menu.ID, this);
	}
	else if (prop_menu.prev != nil)
	{
		GuiClose(id_menu, prop_menu.prev.ID, this);
		prop_menu.prev = nil;
	}
	return;
}

private func CloseGuideMenu()
{
	// Gamecall on closing of the open message.
	if (message_open != nil)
		GameCall("OnGuideMessageRemoved", GetOwner(), message_open);
	message_open = nil;
	GuiClose(id_menu, nil, this);
	return;
}

// Menu callback: the player has clicked on next message.
private func ShowNextMessage()
{
	if (message_open >= GetLength(messages) - 1)
	{
		if (close_on_last)
			CloseGuideMenu();
		return;
	}
	ShowGuideMenu(message_open + 1);
	return;
}

// Menu callback: the player has clicked on previous message.
private func ShowPreviousMessage()
{
	if (message_open == 0)
		return;
	ShowGuideMenu(message_open - 1);
	return;
}


/*-- Properties --*/

local Name = "$Name$";