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# GUILOAD.py - Load window
22
23###################################################
24
25import GemRB
26import LoadScreen
27import GameCheck
28import GUICommon
29
30from GameCheck import MAX_PARTY_SIZE
31
32LoadWindow = 0
33TextAreaControl = 0
34Games = ()
35ScrollBar = 0
36
37def OnLoad ():
38	global LoadWindow, TextAreaControl, Games, ScrollBar
39
40	if GameCheck.IsIWD1():
41		GemRB.SetVar ("PlayMode", 0) # old code, not sure if needed
42
43	GUICommon.SetSaveDir ()
44	LoadWindow = GemRB.LoadWindow (0, "GUILOAD")
45
46	CancelButton=LoadWindow.GetControl (34)
47	CancelButton.SetText (13727)
48	CancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, lambda: LoadWindow.Close())
49	CancelButton.MakeEscape()
50
51	GemRB.SetVar ("LoadIdx",0)
52	for i in range (4):
53		Button = LoadWindow.GetControl (26+i)
54		Button.SetText (15590)
55		Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, LoadGamePress)
56		Button.SetState (IE_GUI_BUTTON_DISABLED)
57		Button.SetVarAssoc ("LoadIdx",i)
58
59		Button = LoadWindow.GetControl (30+i)
60		Button.SetText (13957)
61		Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, DeleteGamePress)
62		Button.SetState (IE_GUI_BUTTON_DISABLED)
63		Button.SetVarAssoc ("LoadIdx",i)
64
65		#area previews
66		Button = LoadWindow.GetControl (1+i)
67		Button.SetState (IE_GUI_BUTTON_LOCKED)
68		Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE|IE_GUI_BUTTON_PICTURE,OP_SET)
69
70		#PC portraits
71		for j in range (min(6, MAX_PARTY_SIZE)):
72			Button = LoadWindow.GetControl (40 + i*min(6, MAX_PARTY_SIZE) + j)
73			Button.SetState (IE_GUI_BUTTON_LOCKED)
74			Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE|IE_GUI_BUTTON_PICTURE,OP_SET)
75
76	ScrollBar=LoadWindow.GetControl (25)
77	ScrollBar.SetEvent (IE_GUI_SCROLLBAR_ON_CHANGE, ScrollBarUpdated)
78	Games=GemRB.GetSaveGames ()
79	TopIndex = max (1, len(Games) - 4)
80	ScrollBar.SetVarAssoc ("TopIndex", TopIndex, 0, TopIndex)
81	LoadWindow.SetEventProxy(ScrollBar)
82	LoadWindow.Focus()
83	return
84
85def ScrollBarUpdated (sb, Pos):
86	#draw load game portraits
87	for i in range (4):
88		ActPos = Pos + i
89
90		Button1 = LoadWindow.GetControl (26+i)
91		Button2 = LoadWindow.GetControl (30+i)
92		PreviewButton = LoadWindow.GetControl (1+i)
93		SlotName = ""
94		SlotDate = ""
95		if ActPos < len(Games):
96			Button1.SetState (IE_GUI_BUTTON_ENABLED)
97			Button2.SetState (IE_GUI_BUTTON_ENABLED)
98			SlotName = Games[ActPos].GetName ()
99			SlotDate = Games[ActPos].GetGameDate ()
100			PreviewButton.SetSprite2D (Games[ActPos].GetPreview())
101		else:
102			Button1.SetState (IE_GUI_BUTTON_DISABLED)
103			Button2.SetState (IE_GUI_BUTTON_DISABLED)
104			PreviewButton.SetPicture (None)
105
106		Label = LoadWindow.GetControl (0x10000008+i)
107		Label.SetText (SlotName)
108
109		Label = LoadWindow.GetControl (0x10000010+i)
110		Label.SetText (SlotDate)
111
112		for j in range (min(6, MAX_PARTY_SIZE)):
113			Button = LoadWindow.GetControl (40 + i*min(6, MAX_PARTY_SIZE) + j)
114			if ActPos<len(Games):
115				Button.SetSprite2D(Games[ActPos].GetPortrait(j))
116			else:
117				Button.SetPicture (None)
118	return
119
120def OpenLoadMsgWindow ():
121	from GUIOPTControls import STR_OPT_CANCEL
122	LoadMsgWindow = Window = GemRB.LoadWindow (4, "GUIOPT")
123	Window.SetFlags (WF_BORDERLESS, OP_OR)
124
125	def AbandonGame():
126		GemRB.QuitGame()
127		GemRB.SetTimer(LoadGamePress, 0, 0)
128
129	# Load
130	Button = Window.GetControl (0)
131	Button.SetText (15590)
132	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbandonGame)
133	Button.MakeDefault()
134
135	# Cancel
136	Button = Window.GetControl (1)
137	Button.SetText (STR_OPT_CANCEL)
138	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, lambda: LoadMsgWindow.Close())
139	Button.MakeEscape()
140
141	# Loading a game will destroy ...
142	Text = Window.GetControl (3)
143	Text.SetText (19531)
144
145	Window.ShowModal (MODAL_SHADOW_GRAY)
146	return
147
148def LoadGamePress ():
149	if GemRB.GetView("GC"): # FIXME: is this the best way to know if we are ingame?
150		OpenLoadMsgWindow()
151		return
152
153	Pos = GemRB.GetVar ("TopIndex")+GemRB.GetVar ("LoadIdx")
154	LoadScreen.StartLoadScreen()
155	#loads savegame
156	GemRB.LoadGame (Games[Pos])
157
158	#enters game
159	if GameCheck.IsIWD1():
160		GemRB.SetNextScript ("PartyFormation")
161	else:
162		# it will close windows, including the loadscreen
163		GemRB.EnterGame ()
164	return
165
166def GetQuickLoadSlot():
167	global Games
168
169	Games=GemRB.GetSaveGames()
170	QuickLoadSlot = None
171	for Game in Games:
172		Slotname = Game.GetSaveID()
173		# quick save is 1
174		if Slotname == 1:
175			QuickLoadSlot = Game
176			break
177	return QuickLoadSlot
178
179def QuickLoadPressed():
180	QuickLoadSlot = GetQuickLoadSlot()
181	if QuickLoadSlot!=None:
182		LoadScreen.StartLoadScreen()
183		GemRB.LoadGame(QuickLoadSlot)
184		GemRB.EnterGame ()
185	return
186
187def DeleteGameConfirm():
188	global Games
189
190	TopIndex = GemRB.GetVar ("TopIndex")
191	Pos = TopIndex +GemRB.GetVar ("LoadIdx")
192	GemRB.DeleteSaveGame(Games[Pos])
193
194	del Games[Pos]
195	if TopIndex > 0:
196		ScrollBar.SetVarAssoc ("TopIndex", TopIndex, 0, len(Games) - 4)
197	else:
198		ScrollBarUpdated(ScrollBar, TopIndex)
199
200	if ConfirmWindow:
201		ConfirmWindow.Unload ()
202	LoadWindow.Focus()
203	return
204
205def DeleteGameCancel ():
206	if ConfirmWindow:
207		ConfirmWindow.Unload ()
208	LoadWindow.Focus()
209	return
210
211def DeleteGamePress ():
212	global ConfirmWindow
213
214	ConfirmWindow=GemRB.LoadWindow (1)
215	ConfirmWindow.SetFlags (WF_ALPHA_CHANNEL, OP_OR)
216	Text=ConfirmWindow.GetControl (0)
217	Text.SetText (15305)
218	DeleteButton=ConfirmWindow.GetControl (1)
219	DeleteButton.SetText (13957)
220	DeleteButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, DeleteGameConfirm)
221	CancelButton=ConfirmWindow.GetControl (2)
222	CancelButton.SetText (13727)
223	CancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, DeleteGameCancel)
224	CancelButton.MakeEscape()
225
226	ConfirmWindow.ShowModal(MODAL_SHADOW_GRAY)
227	return
228