File: Script.c

package info (click to toggle)
openclonk 8.1-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 169,500 kB
  • sloc: cpp: 180,478; ansic: 108,988; xml: 31,371; python: 1,223; php: 767; makefile: 139; sh: 101; javascript: 34
file content (301 lines) | stat: -rw-r--r-- 7,005 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
/**
	ControllerGoal

	Shows the scenario goal in the top right corner, next to the wealth icon.
	Goal icon is clickable. On click displays a detailed goal description.

	@authors Maikel, Clonkonaut
*/

// HUD margin and size in tenths of em.
static const GUI_Controller_Goal_IconSize = 25;
static const GUI_Controller_Goal_IconMargin = 5;

// Does also use constants defined by ControllerWealth to ensure distance between both icons.

local goal_gui_menu;
local goal_gui_id;

local goal_info_id;
local goals;

/*-- Wealth Showing / Hiding --*/

public func ShiftGoal()
{
	if (goal_gui_id == nil) return;

	var offset = GUI_Controller_Wealth_IconSize + GUI_Controller_Wealth_IconMargin;
	var x_end = GUI_Controller_Goal_IconMargin + offset;
	var x_begin = GUI_Controller_Goal_IconMargin + GUI_Controller_Goal_IconSize + offset;

	var update = {
		Left = Format("100%%%s", ToEmString(-x_begin)),
		Right = Format("100%%%s", ToEmString(-x_end)),
	};

	GuiUpdate(update, goal_gui_id);
}

public func UnshiftGoal()
{
	if (goal_gui_id == nil) return;

	var x_end = GUI_Controller_Goal_IconMargin;
	var x_begin = GUI_Controller_Goal_IconMargin + GUI_Controller_Goal_IconSize;

	var update = {
		Left = Format("100%%%s", ToEmString(-x_begin)),
		Right = Format("100%%%s", ToEmString(-x_end)),
	};

	GuiUpdate(update, goal_gui_id);
}

/*-- Creation --*/

public func Construction()
{
	var y_begin = GUI_Controller_Goal_IconMargin;
	var y_end = y_begin + GUI_Controller_Goal_IconSize;
	// Also take into account the margin and size of the wealth HUD if shown
	var x_begin = y_end;
	var x_end = y_begin;
	if (this->~IsShowingWealth())
	{
		x_begin += GUI_Controller_Wealth_IconSize + GUI_Controller_Wealth_IconMargin;
		x_end += GUI_Controller_Wealth_IconSize + GUI_Controller_Wealth_IconMargin;
	}
	// See also ShiftGoal() / UnshiftGoal()

	goal_gui_menu = 
	{
		Target = this,
		Player = NO_OWNER, // The goal icon will become visible if OnGoalUpdate is called
		Style = GUI_Multiple | GUI_IgnoreMouse,
		Left = Format("100%%%s", ToEmString(-x_begin)),
		Right = Format("100%%%s", ToEmString(-x_end)),
		Top = ToEmString(y_begin),
		Bottom = ToEmString(y_end),
		Priority = 1,
		text = 
		{
			Style = GUI_TextHCenter | GUI_TextBottom,
			Text = nil,
			Priority = 3,
		},
	};
	goal_gui_id = GuiOpen(goal_gui_menu);

	return _inherited(...);
}

private func Destruction()
{
	GuiClose(goal_gui_id);
	if (goal_info_id) GuiClose(goal_info_id);

	_inherited(...);
}

/*-- Callbacks --*/

// Callback from the goal library: display this goal.
public func OnGoalUpdate(object goal)
{
	// Also notify the open info menu.
	OnGoalWindowUpdate(goal);
	// If there is no goal hide the menu
	if (!goal)
	{
		GuiUpdate({ Player = NO_OWNER }, goal_gui_id);

		return _inherited(goal, ...);
	}

	// Get current goal display settings
	var symbol = GetGoalSymbol(goal);
	var graphics = GetGoalGraphicsName(goal);
	var text = goal->~GetShortDescription(GetOwner());

	// Determine changes
	var update_symbol = goal_gui_menu.Symbol != symbol;
	var update_graphics = goal_gui_menu.GraphicsName != graphics;
	var update_text = goal_gui_menu.text.Text != text;

	// Only update if something has changed.
	if (update_symbol || update_graphics || update_text)
	{
		goal_gui_menu.text.Text = text;
		goal_gui_menu.Symbol = symbol;
		goal_gui_menu.GraphicsName = graphics;

		goal_gui_menu.Player = GetOwner();
		goal_gui_menu.Style = GUI_Multiple;
		// Also add an hover and mouse click element.
		goal_gui_menu.hover = 
		{
			Symbol = { Std = nil, OnHover = GUI_Controller_Goal},
			OnClick = GuiAction_Call(this, "OnGoalClick"),
			OnMouseIn = GuiAction_SetTag("OnHover"),
			OnMouseOut = GuiAction_SetTag("Std"),
			Priority = 2,
		};
		GuiUpdate(goal_gui_menu, goal_gui_id);
	}

	return _inherited(goal, ...);
}

private func OnGoalClick()
{
	if (goal_info_id)
		CloseGoalWindow();
	else
		OpenGoalWindow();
}

/*-- Goal Info Menu --*/

private func OpenGoalWindow()
{
	goals = FindObjects(Find_Category(C4D_Goal));
	var nr_goals = GetLength(goals);
	var menu_width = BoundBy(nr_goals * 2, 10, 20); // in em

	// Safety: there has to be at least one goal.
	if (nr_goals <= 0)
		return;

	// Main menu
	var goal_info_menu =
	{
		Target = this,
		Player = GetOwner(),
		Decoration = GUI_MenuDeco,
		Left = Format("50%%-%dem", menu_width),
		Right = Format("50%%+%dem", menu_width),
		Top = "50%-4em",
		Bottom = "50%+8em",
		OnClose = GuiAction_Call(this, "OnGoalWindowClosed"),
	};

	// Close button
	GuiAddCloseButton(goal_info_menu, this, "OnCloseButtonClick");

	// Text submenu
	goal_info_menu.text =
	{
		Target = this,
		ID = 1,
		Left = "0%",
		Right = "100%",
		Top = "0%+4em",
		Bottom = "100%",
	};

	// Goal icons: maximum number of 10 goals for now
	for (var i = 0; i < Min(10, nr_goals); i++)
	{
		var menu = GoalSubMenu(goals[i], i);
		GuiAddSubwindow(menu, goal_info_menu);
	}
	
	goal_info_id = GuiOpen(goal_info_menu);
	
	// Select first goal and show its description.
	OnGoalGUIHover(goals[0]);
}

private func CloseGoalWindow()
{
	GuiClose(goal_info_id);
}

private func OnGoalWindowClosed()
{
	goal_info_id = nil;
}

private func GoalSubMenu(object goal, int nr, int size)
{
	if (size == nil)
		size = 4;

	var symbol = GetGoalSymbol(goal);
	var graphics = GetGoalGraphicsName(goal);
	// Create the goal submenu with id counting upwards from 2.
	var prop_goal = 
	{
		Target = this,
		ID = nr + 2,
		Left = Format("0%%+%dem", nr * size),
		Right = Format("0%%+%dem", (nr + 1) * size),
		Top = "0%",
		Bottom = Format("0%%+%dem", size),
		Symbol = symbol,
		GraphicsName = graphics,
		BackgroundColor = {Std = 0, Hover = 0x50ffffff},
		OnMouseIn = [GuiAction_SetTag("Hover"), GuiAction_Call(this, "OnGoalGUIHover", goal)],
		OnMouseOut = GuiAction_SetTag("Std"),
		fulfilled = nil
	};
	// Indicate whether the goal is already fulfilled
	if (goal->~IsFulfilled())
	{
		prop_goal.fulfilled = 
		{
			Target = this,
			Left = "100%-1em",
			Right = "100%",
			Top = "0%",
			Bottom = "0%+1em",
			Symbol = Icon_Ok,
		};
	}

	return prop_goal;
}

public func OnCloseButtonClick()
{
	CloseGoalWindow();
}

public func OnGoalGUIHover(object goal)
{
	if (!goal)
		return;
	// Change text to the current goal.
	var text = Format("<c ff0000>%s:</c> %s", goal->GetName(), goal->~GetDescription(GetOwner()));
	GuiUpdateText(text, goal_info_id, 1, this);
}

private func OnGoalWindowUpdate(object goal)
{
	if (!goal || !goal_info_id)
		return;

	var index = GetIndexOf(goals, goal);
	if (index == -1) return;

	var menu = GoalSubMenu(goal, index);
	// Update only very selectively. (To e.g. not reset the background/tag)
	var update = 
	{
		Symbol = menu.Symbol,
		GraphicsName = menu.GraphicsName,
		fulfilled = menu.fulfilled,
	};
	GuiUpdate(update, goal_info_id, menu.ID, menu.Target);
}

private func GetGoalSymbol(object goal)
{
	return goal->~GetPictureDefinition(GetOwner()) ?? goal->GetID();
}

private func GetGoalGraphicsName(object goal)
{
	return goal->~GetPictureName(GetOwner()) ?? goal->GetGraphics(GetOwner());
}