1# -*-python-*-
2# GemRB - Infinity Engine Emulator
3# Copyright (C) 2003 The GemRB Project
4#
5# This program is free software; you can redistribute it and/or
6# modify it under the terms of the GNU General Public License
7# as published by the Free Software Foundation; either version 2
8# of the License, or (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program; if not, write to the Free Software
17# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18#
19
20
21# GUIW.py - scripts to control some windows from the GUIWORLD winpack
22# except of Actions, Portrait, Options and Dialog windows
23#################################################################
24
25import GemRB
26import GameCheck
27import GUICommon
28import GUICommonWindows
29from GameCheck import MAX_PARTY_SIZE
30from GUIDefines import *
31from ie_stats import *
32import CommonWindow
33
34FRAME_PC_SELECTED = 0
35FRAME_PC_TARGET   = 1
36
37ContinueWindow = None
38ReformPartyWindow = None
39
40removable_pcs = []
41
42def OpenDialogButton(id):
43	window = GemRB.LoadWindow (id, GUICommon.GetWindowPack())
44	window.SetFlags (IE_GUI_VIEW_IGNORE_EVENTS, OP_OR)
45
46	MsgWin = GemRB.GetView("MSGWIN")
47
48	frame = MsgWin.GetFrame()
49	offset = 0
50	if GameCheck.IsGemRBDemo ():
51		offset = window.GetFrame()['h']
52	window.SetPos(frame['x'], frame['y'] + frame['h'] - offset)
53
54	# Dialog button shouldnt be in front
55	win = GemRB.GetView("OPTWIN")
56	if win:
57		win.Focus()
58	win = GemRB.GetView("PORTWIN")
59	if win:
60		win.Focus()
61
62	return window
63
64def DialogStarted ():
65	global ContinueWindow
66
67	# try to force-close anything which is open
68	GUICommonWindows.CloseTopWindow()
69	CommonWindow.CloseContainerWindow()
70
71	# opening control size to maximum, enabling dialog window
72	CommonWindow.SetGameGUIHidden(False)
73	GemRB.GameSetScreenFlags(GS_DIALOG, OP_OR)
74
75	# disable the 1-6 hotkeys, so they'll work for choosing answers
76	if GemRB.GetView ("PORTWIN"):
77		GUICommonWindows.UpdatePortraitWindow ()
78
79	ContinueWindow = OpenDialogButton(9)
80
81def DialogEnded ():
82	global ContinueWindow
83
84	GUICommonWindows.UpdateActionsWindow()
85
86	ContinueWindow.Close ()
87	ContinueWindow = None
88
89def CloseContinueWindow ():
90	# don't close the actual window now to avoid flickering: we might still want it open
91	GemRB.SetVar ("DialogChoose", GemRB.GetVar ("DialogOption"))
92
93def NextDialogState ():
94	if not ContinueWindow:
95		return
96
97	ContinueWindow.GetControl(0).SetVisible(False)
98	ContinueWindow.GetControl(0).SetDisabled(True)
99
100def OpenEndMessageWindow ():
101	Button = ContinueWindow.GetControl (0)
102	Button.SetVisible(True)
103	Button.SetDisabled(False)
104	EndDLGStrref = 9371
105	if GameCheck.IsGemRBDemo ():
106		EndDLGStrref = 67
107	Button.SetText (EndDLGStrref)
108	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseContinueWindow)
109	Button.SetFlags (IE_GUI_BUTTON_NO_TOOLTIP, OP_OR)
110	Button.MakeDefault(True)
111
112def OpenContinueMessageWindow ():
113	#continue
114	Button = ContinueWindow.GetControl (0)
115	Button.SetVisible(True)
116	Button.SetDisabled(False)
117	ContinueStrref = 9372
118	if GameCheck.IsGemRBDemo ():
119		ContinueStrref = 66
120	Button.SetText (ContinueStrref)
121	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseContinueWindow)
122	Button.SetFlags (IE_GUI_BUTTON_NO_TOOLTIP, OP_OR)
123	Button.MakeDefault(True)
124
125def UpdateReformWindow ():
126	Window = ReformPartyWindow
127
128	select = GemRB.GetVar ("Selected")
129
130	need_to_drop = GemRB.GetPartySize ()-MAX_PARTY_SIZE
131	if need_to_drop<0:
132		need_to_drop = 0
133
134	#excess player number
135	Label = Window.GetControl (0x1000000f)
136	Label.SetText (str(need_to_drop) )
137
138	#done
139	Button = Window.GetControl (8)
140	if need_to_drop:
141		Button.SetState (IE_GUI_BUTTON_DISABLED)
142	else:
143		Button.SetState (IE_GUI_BUTTON_ENABLED)
144
145	#remove
146	Button = Window.GetControl (15)
147	if select:
148		Button.SetState (IE_GUI_BUTTON_ENABLED)
149	else:
150		Button.SetState (IE_GUI_BUTTON_DISABLED)
151
152	PortraitButtons = GUICommonWindows.GetPortraitButtonPairs (Window, 1, "horizontal")
153	for i in PortraitButtons:
154		Button = PortraitButtons[i]
155		if i+1 not in removable_pcs:
156			Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
157			Button.SetState (IE_GUI_BUTTON_LOCKED)
158
159	for i in removable_pcs:
160		index = removable_pcs.index(i)
161		if index not in PortraitButtons:
162			continue # for saved games with higher party count than the current setup supports
163		Button = PortraitButtons[index]
164		Button.EnableBorder (FRAME_PC_SELECTED, select == i)
165		Portrait = GemRB.GetPlayerPortrait (i, 1)
166		pic = Portrait["Sprite"]
167		if not pic and not Portrait["ResRef"]:
168			Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE, OP_SET)
169			Button.SetState (IE_GUI_BUTTON_LOCKED)
170			continue
171		Button.SetState (IE_GUI_BUTTON_ENABLED)
172		Button.SetFlags (IE_GUI_BUTTON_PICTURE|IE_GUI_BUTTON_ALIGN_BOTTOM|IE_GUI_BUTTON_ALIGN_LEFT, OP_SET)
173		Button.SetPicture (pic, "NOPORTSM")
174	GUICommonWindows.UpdatePortraitWindow ()
175	return
176
177def RemovePlayer ():
178	global ReformPartyWindow
179
180	hideflag = CommonWindow.IsGameGUIHidden()
181
182	if ReformPartyWindow:
183		ReformPartyWindow.Unload ()
184	wid = 25
185	if GameCheck.IsHOW ():
186		wid = 0 # at least in guiw08, this is the correct window
187	ReformPartyWindow = Window = GemRB.LoadWindow (wid, GUICommon.GetWindowPack(), WINDOW_BOTTOM)
188
189	#are you sure
190	Label = Window.GetControl (0x0fffffff)
191	Label.SetText (17518)
192
193	#confirm
194	Button = Window.GetControl (1)
195	Button.SetText (17507)
196	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, RemovePlayerConfirm)
197	Button.MakeDefault()
198
199	#cancel
200	Button = Window.GetControl (2)
201	Button.SetText (13727)
202	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, RemovePlayerCancel)
203	Button.MakeEscape()
204
205	CommonWindow.SetGameGUIHidden(hideflag)
206	Window.ShowModal (MODAL_SHADOW_GRAY)
207	return
208
209def RemovePlayerConfirm ():
210	slot = GemRB.GetVar ("Selected")
211	if GameCheck.IsBG2():
212		GemRB.LeaveParty (slot, 2)
213	elif GameCheck.IsBG1():
214		GemRB.LeaveParty (slot, 1)
215	else:
216		GemRB.LeaveParty (slot)
217	OpenReformPartyWindow ()
218	return
219
220def RemovePlayerCancel ():
221	#Once for getting rid of the confirmation window
222	OpenReformPartyWindow ()
223	#and once for reopening the reform party window
224	OpenReformPartyWindow ()
225	return
226
227def OpenReformPartyWindow ():
228	global ReformPartyWindow
229	global removable_pcs
230
231	GemRB.SetVar ("Selected", 0)
232	hideflag = CommonWindow.IsGameGUIHidden()
233
234	if ReformPartyWindow:
235		ReformPartyWindow.Unload ()
236		ReformPartyWindow = None
237
238		CommonWindow.SetGameGUIHidden(hideflag)
239		#re-enabling party size control
240		GemRB.GameSetPartySize (MAX_PARTY_SIZE)
241		GUICommonWindows.UpdatePortraitWindow()
242		return
243
244	ReformPartyWindow = Window = GemRB.LoadWindow (24, GUICommon.GetWindowPack(), WINDOW_HCENTER|WINDOW_BOTTOM)
245
246	# skip exportable party members (usually only the protagonist)
247	removable_pcs = []
248	for i in range (1, GemRB.GetPartySize()+1):
249		if not GemRB.GetPlayerStat (i, IE_MC_FLAGS)&MC_EXPORTABLE:
250			removable_pcs.append(i)
251
252	#PC portraits
253	PortraitButtons = GUICommonWindows.GetPortraitButtonPairs (Window, 1, "horizontal")
254	for j in PortraitButtons:
255		Button = PortraitButtons[j]
256		Button.SetState (IE_GUI_BUTTON_LOCKED)
257		Button.SetFlags (IE_GUI_BUTTON_RADIOBUTTON|IE_GUI_BUTTON_NO_IMAGE|IE_GUI_BUTTON_PICTURE,OP_SET)
258		color = {'r' : 0, 'g' : 255, 'b' : 0, 'a' : 255}
259		Button.SetBorder (FRAME_PC_SELECTED, color, 0, 0, Button.GetInsetFrame(1,1,2,2))
260		if j < len(removable_pcs):
261			Button.SetVarAssoc ("Selected", removable_pcs[j])
262		Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, UpdateReformWindow)
263
264	# Remove
265	Button = Window.GetControl (15)
266	Button.SetText (17507)
267	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, RemovePlayer)
268
269	# Done
270	Button = Window.GetControl (8)
271	Button.SetText (11973)
272	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OpenReformPartyWindow)
273
274	# if nobody can be removed, just close the window
275	if not removable_pcs:
276		OpenReformPartyWindow ()
277		CommonWindow.SetGameGUIHidden(hideflag)
278		return
279
280	UpdateReformWindow ()
281	CommonWindow.SetGameGUIHidden(hideflag)
282
283	Window.ShowModal (MODAL_SHADOW_GRAY)
284	return
285
286def DeathWindow ():
287	# break out of any cutscenes, so we get mouse input back
288	# can also happen in "cutscene lite" mode, eg. with wk spirit warrior dream #715
289	GemRB.EndCutSceneMode ()
290	GemRB.ExecuteString ("SetCursorState(0)")
291	if GameCheck.IsIWD1():
292		#no death movie, but music is changed
293		GemRB.LoadMusicPL ("Theme.mus",1)
294	CommonWindow.SetGameGUIHidden(True)
295	GemRB.SetTimedEvent (DeathWindowEnd, 10)
296	return
297
298def DeathWindowEnd ():
299	#playing death movie before continuing
300	if not GameCheck.IsIWD1():
301		GemRB.PlayMovie ("deathand",1)
302	GemRB.GamePause (1,3)
303
304	Window = GemRB.LoadWindow (17, GUICommon.GetWindowPack(), WINDOW_BOTTOM|WINDOW_HCENTER)
305
306	#reason for death
307	Label = Window.GetControl (0x0fffffff)
308	Label.SetText (16498)
309
310	#load
311	Button = Window.GetControl (1)
312	Button.SetText (15590)
313	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, LoadPress)
314
315	#quit
316	Button = Window.GetControl (2)
317	Button.SetText (15417)
318	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, QuitPress)
319
320	Window.ShowModal (MODAL_SHADOW_GRAY)
321	return
322
323def QuitPress():
324	GemRB.QuitGame ()
325	GemRB.SetNextScript ("Start")
326	return
327
328def LoadPress():
329	GemRB.QuitGame ()
330	GemRB.SetNextScript ("GUILOAD")
331	return
332
333