GUIScript: Use None instead if "", for null event handler.
[gemrb.git] / gemrb / GUIScripts / pst / GUISAVE.py
blob759b12d99101de7f42241d350dbf38af5c371827
1 # -*-python-*-
2 # GemRB - Infinity Engine Emulator
3 # Copyright (C) 2003 The GemRB Project
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.
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.
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.
21 # GUISAVE.py - Save game screen from GUISAVE winpack
23 ###################################################
25 import GemRB
26 from GUIDefines import *
27 from LoadScreen import *
28 from GUIClasses import GWindow
30 SaveWindow = None
31 SaveDetailWindow = None
32 OptionsWindow = None
33 Games = ()
34 ScrollBar = 0
37 def OpenSaveWindow ():
38 global SaveWindow, OptionsWindow, Games, ScrollBar
40 if SaveWindow:
41 GemRB.HideGUI ()
43 if SaveDetailWindow: OpenSaveDetailWindow ()
45 if SaveWindow:
46 SaveWindow.Unload ()
47 SaveWindow = None
48 # FIXME: LOAD GUIOPT?
49 GemRB.SetVar ("OtherWindow", OptionsWindow.ID)
51 GemRB.UnhideGUI ()
52 return
54 GemRB.HideGUI ()
55 GemRB.LoadWindowPack ("GUISAVE", 640, 480)
56 SaveWindow = Window = GemRB.LoadWindow (0)
57 OptionsWindow = GWindow( GemRB.GetVar ("OtherWindow") )
58 GemRB.SetVar ("OtherWindow", SaveWindow.ID)
60 # Cancel button
61 CancelButton = Window.GetControl (46)
62 CancelButton.SetText (4196)
63 CancelButton.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "CancelPress")
64 CancelButton.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
65 GemRB.SetVar ("SaveIdx", 0)
67 for i in range (4):
68 Button = Window.GetControl (14 + i)
69 Button.SetText (28645)
70 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "SaveGamePress")
71 Button.SetState (IE_GUI_BUTTON_DISABLED)
72 Button.SetVarAssoc ("SaveIdx", i)
74 Button = Window.GetControl (18 + i)
75 Button.SetText (28640)
76 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "DeleteGamePress")
77 Button.SetState (IE_GUI_BUTTON_DISABLED)
78 Button.SetVarAssoc ("SaveIdx", i)
80 # area previews
81 Button = Window.GetControl (1 + i)
82 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE|IE_GUI_BUTTON_PICTURE, OP_SET)
84 # PC portraits
85 for j in range (6):
86 Button = Window.GetControl (22 + i*6 + j)
87 Button.SetFlags (IE_GUI_BUTTON_NO_IMAGE|IE_GUI_BUTTON_PICTURE, OP_SET)
89 ScrollBar = Window.GetControl(13)
90 ScrollBar.SetEventByName(IE_GUI_SCROLLBAR_ON_CHANGE, "ScrollBarPress")
91 Games = GemRB.GetSaveGames()
92 TopIndex = max (0, len(Games) - 4 + 1) #one more for the 'new game'
94 GemRB.SetVar ("TopIndex",TopIndex)
95 ScrollBar.SetVarAssoc ("TopIndex", len(Games))
96 ScrollBarPress ()
98 GemRB.UnhideGUI ()
101 def ScrollBarPress():
102 Window = SaveWindow
103 # draw load game portraits
104 Pos = GemRB.GetVar ("TopIndex")
105 for i in range (4):
106 ActPos = Pos + i
108 Button1 = Window.GetControl (14 + i)
109 Button2 = Window.GetControl (18 + i)
110 if ActPos <= len(Games):
111 Button1.SetState(IE_GUI_BUTTON_ENABLED)
112 else:
113 Button1.SetState(IE_GUI_BUTTON_DISABLED)
115 if ActPos < len(Games):
116 Slotname = Games[ActPos].GetName()
117 Slottime = Games[ActPos].GetDate()
118 Button2.SetState(IE_GUI_BUTTON_ENABLED)
119 elif ActPos == len(Games):
120 Slotname = 28647 # "Empty"
121 Slottime = ""
122 Button2.SetState(IE_GUI_BUTTON_DISABLED)
123 else:
124 Slotname = ""
125 Slottime = ""
126 Button2.SetState(IE_GUI_BUTTON_DISABLED)
128 Label = Window.GetControl (0x10000004+i)
129 Label.SetText (Slotname)
131 Label = Window.GetControl (0x10000008 + i)
132 Label.SetText (Slottime)
134 Button = Window.GetControl (1 + i)
135 if ActPos < len(Games):
136 Button.SetSprite2D(Games[ActPos].GetPreview())
137 else:
138 Button.SetPicture ("")
140 for j in range (6):
141 Button = Window.GetControl (22 + i*6 + j)
142 if ActPos < len(Games):
143 Button.SetSprite2D(Games[ActPos].GetPortrait(j))
144 else:
145 Button.SetPicture ("")
148 def SaveGamePress ():
149 OpenSaveDetailWindow ()
150 return
152 def DeleteGameConfirm():
153 global Games
155 TopIndex = GemRB.GetVar("TopIndex")
156 Pos = TopIndex +GemRB.GetVar("SaveIdx")
157 GemRB.DeleteSaveGame(Games[Pos])
158 if TopIndex>0:
159 GemRB.SetVar("TopIndex",TopIndex-1)
160 del Games[pos]
161 ScrollBar.SetVarAssoc("TopIndex", len(Games))
162 ScrollBarPress()
163 if ConfirmWindow:
164 ConfirmWindow.Unload()
165 SaveWindow.SetVisible(WINDOW_VISIBLE)
166 return
168 def DeleteGameCancel():
169 if ConfirmWindow:
170 ConfirmWindow.Unload()
171 SaveWindow.SetVisible(WINDOW_VISIBLE)
172 return
174 def DeleteGamePress():
175 global ConfirmWindow
177 SaveWindow.SetVisible(WINDOW_INVISIBLE)
178 ConfirmWindow=GemRB.LoadWindow(1)
180 Text=ConfirmWindow.GetControl(0)
181 Text.SetText(15305)
183 DeleteButton=ConfirmWindow.GetControl(1)
184 DeleteButton.SetText(13957)
185 DeleteButton.SetEventByName(IE_GUI_BUTTON_ON_PRESS, "DeleteGameConfirm")
186 DeleteButton.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
188 CancelButton=ConfirmWindow.GetControl(2)
189 CancelButton.SetText(4196)
190 CancelButton.SetEventByName(IE_GUI_BUTTON_ON_PRESS, "DeleteGameCancel")
191 CancelButton.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
193 ConfirmWindow.SetVisible(WINDOW_VISIBLE)
194 return
196 def CancelPress():
197 OpenSaveWindow ()
200 def OpenSaveDetailWindow ():
201 global SaveDetailWindow
203 GemRB.HideGUI ()
205 if SaveDetailWindow != None:
206 if SaveDetailWindow:
207 SaveDetailWindow.Unload ()
208 SaveDetailWindow = None
209 GemRB.SetVar ("FloatWindow", -1)
211 GemRB.UnhideGUI ()
212 return
214 SaveDetailWindow = Window = GemRB.LoadWindow (1)
215 GemRB.SetVar ("FloatWindow", SaveDetailWindow.ID)
218 Pos = GemRB.GetVar ("TopIndex") + GemRB.GetVar ("SaveIdx")
221 # Save/Overwrite
222 Button = Window.GetControl (4)
223 if Pos < len(Games):
224 Button.SetText (28644) # Overwrite
225 else:
226 Button.SetText (28645) # Save
227 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "ConfirmedSaveGame")
228 Button.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
230 # Cancel
231 Button = Window.GetControl (5)
232 Button.SetText (4196)
233 Button.SetEventByName (IE_GUI_BUTTON_ON_PRESS, "OpenSaveDetailWindow")
234 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
236 # Slot name and time
237 if Pos < len(Games):
238 Slotname = Games[Pos].GetName()
239 Slottime = Games[Pos].GetGameDate()
240 else:
241 Slotname = ""
242 Slottime = ""
244 Edit = Window.GetControl (1)
245 Edit.SetText (Slotname)
246 Edit.SetEventByName (IE_GUI_EDIT_ON_CHANGE, "CheckSaveName")
247 Edit.SetEventByName (IE_GUI_EDIT_ON_DONE, "ConfirmedSaveGame")
249 Label = Window.GetControl (0x10000002)
250 Label.SetText (Slottime)
253 # Areapreview
254 Button = Window.GetControl (0)
255 Button.SetSprite2D(GemRB.GetGamePreview())
257 # PC portraits
258 for j in range (PARTY_SIZE):
259 Button = Window.GetControl (6 + j)
260 Button.SetSprite2D(GemRB.GetGamePortraitPreview(j))
263 CheckSaveName ()
264 GemRB.UnhideGUI ()
265 Window.ShowModal (MODAL_SHADOW_GRAY)
266 Edit.SetStatus (IE_GUI_CONTROL_FOCUSED) # ShowModal will happily reset this..
269 # Disable Save/Overwrite button if the save slotname is empty,
270 # else enable it
271 def CheckSaveName ():
272 Window = SaveDetailWindow
273 Button = Window.GetControl (4)
274 Edit = Window.GetControl (1)
275 Name = Edit.QueryText ()
277 if Name == "":
278 Button.SetState (IE_GUI_BUTTON_DISABLED)
279 else:
280 Button.SetState (IE_GUI_BUTTON_ENABLED)
284 # User entered save name and pressed save/overwrite.
285 # Display progress bar screen and save the game, close the save windows
286 def ConfirmedSaveGame ():
287 Window = SaveDetailWindow
289 Pos = GemRB.GetVar ("TopIndex") + GemRB.GetVar ("SaveIdx")
290 Label = Window.GetControl (1)
291 Slotname = Label.QueryText ()
293 # Empty save name. We can get here if user presses Enter key
294 if Slotname == "":
295 return
297 # We have to close floating window first
298 OpenSaveDetailWindow ()
299 StartLoadScreen (LS_TYPE_SAVING)
300 if Pos < len(Games):
301 GemRB.SaveGame(Games[Pos], Slotname)
302 else:
303 GemRB.SaveGame(None, Slotname)
304 CloseSaveWindow ()
307 # Exit either back to game or to the Start window
308 def CloseSaveWindow ():
309 OpenSaveWindow ()
310 if GemRB.GetVar ("QuitAfterSave"):
311 GemRB.QuitGame ()
312 GemRB.SetNextScript ("Start")
313 return
315 GemRB.RunEventHandler ("OpenOptionsWindow")