TickHook: Fix crash when TickHook isn't set.
[gemrb.git] / gemrb / GUIScripts / iwd / GUISAVE.py
blobe6a2091b72c6999de7614b4fcb25cdc2f10572ce
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 window
23 ###################################################
25 import GemRB
26 import GUICommon
27 import LoadScreen
28 from GUIDefines import *
30 SaveWindow = 0
31 ConfirmWindow = 0
32 NameField = 0
33 SaveButton = 0
34 TextAreaControl = 0
35 Games = ()
36 ScrollBar = 0
38 def OpenSaveWindow ():
39 global SaveWindow, TextAreaControl, Games, ScrollBar
41 if GUICommon.CloseOtherWindow (OpenSaveWindow):
42 CloseSaveWindow ()
43 return
45 GemRB.HideGUI ()
46 GUICommon.GameWindow.SetVisible(WINDOW_INVISIBLE)
48 GemRB.LoadWindowPack ("GUISAVE", 640, 480)
49 Window = SaveWindow = GemRB.LoadWindow (0)
50 Window.SetFrame ()
51 CancelButton=Window.GetControl (34)
52 CancelButton.SetText (13727)
53 CancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, OpenSaveWindow)
54 CancelButton.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
55 GemRB.SetVar ("LoadIdx",0)
57 for i in range(4):
58 Button = Window.GetControl (26+i)
59 Button.SetText (15588)
60 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, SavePress)
61 Button.SetState (IE_GUI_BUTTON_DISABLED)
62 Button.SetVarAssoc ("LoadIdx",i)
64 Button = Window.GetControl (30+i)
65 Button.SetText (13957)
66 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, DeleteGamePress)
67 Button.SetState (IE_GUI_BUTTON_DISABLED)
68 Button.SetVarAssoc ("LoadIdx",i)
70 #area previews
71 Button = Window.GetControl (1+i)
72 Button.SetState (IE_GUI_BUTTON_LOCKED)
73 Button.SetFlags(IE_GUI_BUTTON_NO_IMAGE|IE_GUI_BUTTON_PICTURE,OP_SET)
75 #PC portraits
76 for j in range(PARTY_SIZE):
77 Button = Window.GetControl (40+i*PARTY_SIZE+j)
78 Button.SetState (IE_GUI_BUTTON_LOCKED)
79 Button.SetFlags(IE_GUI_BUTTON_NO_IMAGE|IE_GUI_BUTTON_PICTURE,OP_SET)
81 ScrollBar=Window.GetControl (25)
82 ScrollBar.SetEvent (IE_GUI_SCROLLBAR_ON_CHANGE, ScrollBarPress)
83 Games=GemRB.GetSaveGames ()
84 TopIndex = max (0, len(Games) - 4 + 1) #one more for the 'new game'
85 GemRB.SetVar ("TopIndex",TopIndex)
86 ScrollBar.SetVarAssoc ("TopIndex", TopIndex+1)
87 ScrollBar.SetDefaultScrollBar ()
88 ScrollBarPress ()
89 Window.SetVisible (WINDOW_VISIBLE)
90 return
92 def ScrollBarPress():
93 Window = SaveWindow
95 #draw load game portraits
96 Pos = GemRB.GetVar ("TopIndex")
97 for i in range(4):
98 ActPos = Pos + i
100 Button1 = Window.GetControl (26+i)
101 Button2 = Window.GetControl (30+i)
102 if ActPos<=len(Games):
103 Button1.SetState (IE_GUI_BUTTON_ENABLED)
104 else:
105 Button1.SetState (IE_GUI_BUTTON_DISABLED)
107 if ActPos<len(Games):
108 Slotname = Games[ActPos].GetName()
109 Button2.SetState (IE_GUI_BUTTON_ENABLED)
110 elif ActPos == len(Games):
111 Slotname = 15304
112 Button2.SetState (IE_GUI_BUTTON_DISABLED)
113 else:
114 Slotname = ""
115 Button2.SetState (IE_GUI_BUTTON_DISABLED)
117 Label = Window.GetControl (0x10000008+i)
118 Label.SetText (Slotname)
120 if ActPos<len(Games):
121 Slotname = Games[ActPos].GetGameDate()
122 else:
123 Slotname = ""
124 Label = Window.GetControl (0x10000010+i)
125 Label.SetText (Slotname)
127 Button=Window.GetControl (1+i)
128 if ActPos<len(Games):
129 Button.SetSprite2D(Games[ActPos].GetPreview())
130 else:
131 Button.SetPicture("")
132 for j in range(PARTY_SIZE):
133 Button=Window.GetControl (40+i*PARTY_SIZE+j)
134 if ActPos<len(Games):
135 Button.SetSprite2D(Games[ActPos].GetPortrait(j))
136 else:
137 Button.SetPicture("")
138 return
140 def AbortedSaveGame():
141 if ConfirmWindow:
142 ConfirmWindow.Unload ()
143 SaveWindow.SetVisible (WINDOW_VISIBLE)
144 return
146 def ConfirmedSaveGame():
147 global ConfirmWindow
149 Pos = GemRB.GetVar ("TopIndex")+GemRB.GetVar ("LoadIdx")
150 Label = ConfirmWindow.GetControl (3)
151 Slotname = Label.QueryText ()
152 LoadScreen.StartLoadScreen()
153 if Pos < len(Games):
154 GemRB.SaveGame(Games[Pos], Slotname)
155 else:
156 GemRB.SaveGame(None, Slotname)
157 if ConfirmWindow:
158 ConfirmWindow.Unload ()
159 SaveWindow.SetVisible (WINDOW_VISIBLE)
160 return
162 def SavePress():
163 global ConfirmWindow, NameField, SaveButton
165 Pos = GemRB.GetVar ("TopIndex")+GemRB.GetVar ("LoadIdx")
166 ConfirmWindow = GemRB.LoadWindow (1)
168 #slot name
169 if Pos<len(Games):
170 Slotname = Games[Pos].GetName()
171 save_strref = 15306
172 else:
173 Slotname = ""
174 save_strref = 15588
175 NameField = ConfirmWindow.GetControl (3)
176 NameField.SetText (Slotname)
177 NameField.SetEvent (IE_GUI_EDIT_ON_CHANGE, EditChange)
179 #game hours (should be generated from game)
180 if Pos<len(Games):
181 Slotname = Games[Pos].GetGameDate()
182 else:
183 Slotname = ""
184 Label = ConfirmWindow.GetControl (0x10000004)
185 Label.SetText (Slotname)
187 #areapreview
188 Button=ConfirmWindow.GetControl (0)
189 if Pos<len(Games):
190 Button.SetSprite2D(Games[Pos].GetPreview())
191 else:
192 Button.SetPicture("")
194 #portraits
195 for j in range(PARTY_SIZE):
196 Button=ConfirmWindow.GetControl (40+j)
197 if Pos<len(Games):
198 Button.SetSprite2D(Games[Pos].GetPortrait(j))
199 else:
200 Button.SetPicture("")
202 #save
203 SaveButton=ConfirmWindow.GetControl (7)
204 SaveButton.SetText (save_strref)
205 SaveButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, ConfirmedSaveGame)
206 SaveButton.SetFlags (IE_GUI_BUTTON_DEFAULT, OP_OR)
207 #SaveButton.SetState (IE_GUI_BUTTON_DISABLED)
208 if Slotname == "":
209 SaveButton.SetState (IE_GUI_BUTTON_DISABLED)
211 #cancel
212 CancelButton=ConfirmWindow.GetControl (8)
213 CancelButton.SetText (13727)
214 CancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, AbortedSaveGame)
215 CancelButton.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
217 ConfirmWindow.SetVisible (WINDOW_VISIBLE)
218 NameField.SetStatus (IE_GUI_CONTROL_FOCUSED)
219 return
221 def EditChange():
222 Name = NameField.QueryText ()
223 if len(Name)==0:
224 SaveButton.SetState (IE_GUI_BUTTON_DISABLED)
225 else:
226 SaveButton.SetState (IE_GUI_BUTTON_ENABLED)
227 return
229 def DeleteGameConfirm():
230 global Games
232 TopIndex = GemRB.GetVar ("TopIndex")
233 Pos = TopIndex +GemRB.GetVar ("LoadIdx")
234 GemRB.DeleteSaveGame(Games[Pos])
235 if TopIndex>0:
236 GemRB.SetVar ("TopIndex",TopIndex-1)
237 del Games[pos]
238 ScrollBar.SetVarAssoc ("TopIndex", len(Games))
239 ScrollBarPress()
240 if ConfirmWindow:
241 ConfirmWindow.Unload ()
242 SaveWindow.SetVisible (WINDOW_VISIBLE)
243 return
245 def DeleteGameCancel():
246 if ConfirmWindow:
247 ConfirmWindow.Unload ()
248 SaveWindow.SetVisible (WINDOW_VISIBLE)
249 return
251 def DeleteGamePress():
252 global ConfirmWindow
254 SaveWindow.SetVisible (WINDOW_INVISIBLE)
255 ConfirmWindow=GemRB.LoadWindow (2)
256 Text=ConfirmWindow.GetControl (0)
257 Text.SetText (15305)
258 DeleteButton=ConfirmWindow.GetControl (1)
259 DeleteButton.SetText (13957)
260 DeleteButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, DeleteGameConfirm)
261 CancelButton=ConfirmWindow.GetControl (2)
262 CancelButton.SetText (13727)
263 CancelButton.SetEvent (IE_GUI_BUTTON_ON_PRESS, DeleteGameCancel)
264 CancelButton.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
266 ConfirmWindow.SetVisible (WINDOW_VISIBLE)
267 return
269 def CloseSaveWindow ():
270 if SaveWindow:
271 SaveWindow.Unload ()
272 if GemRB.GetVar ("QuitAfterSave"):
273 GemRB.QuitGame ()
274 GemRB.SetNextScript ("Start")
275 return
277 GUICommon.GameWindow.SetVisible(WINDOW_VISIBLE) #enabling the game control screen
278 GemRB.UnhideGUI () #enabling the other windows
279 return