TickHook: Fix crash when TickHook isn't set.
[gemrb.git] / gemrb / GUIScripts / iwd / GUIMA.py
blob235200521081224a2ebd625d6e5024e7a4790672
1 # -*-python-*-
2 # GemRB - Infinity Engine Emulator
3 # Copyright (C) 2003-2004 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 # GUIMA.py - scripts to control map windows from the GUIMA and GUIWMAP winpacks
23 ###################################################
25 import GemRB
26 import GUICommon
27 import GUICommonWindows
28 from GUIDefines import *
30 MapWindow = None
31 WorldMapWindow = None
32 WorldMapControl = None
33 PortraitWindow = None
34 OptionsWindow = None
35 OldPortraitWindow = None
36 OldOptionsWindow = None
38 def RevealMap ():
39 global MapWindow
40 global OldPortraitWindow, OldOptionsWindow
42 if GUICommon.CloseOtherWindow (ShowMap):
43 if MapWindow:
44 MapWindow.Unload ()
45 if OptionsWindow:
46 OptionsWindow.Unload ()
47 if PortraitWindow:
48 PortraitWindow.Unload ()
50 MapWindow = None
51 #this window type should block the game
52 GemRB.SetVar ("OtherWindow", -1)
53 GUICommon.GameWindow.SetVisible(WINDOW_VISIBLE)
54 GemRB.UnhideGUI ()
55 GUICommonWindows.PortraitWindow = OldPortraitWindow
56 OldPortraitWindow = None
57 GUICommonWindows.OptionsWindow = OldOptionsWindow
58 OldOptionsWindow = None
60 PosX = GemRB.GetVar ("MapControlX")
61 PosY = GemRB.GetVar ("MapControlY")
63 GemRB.RevealArea (PosX, PosY, 30, 1)
64 GemRB.GamePause (0,0)
65 return
67 ###################################################
68 # for farsight effect
69 ###################################################
70 def ShowMap ():
71 global MapWindow, OptionsWindow, PortraitWindow
72 global OldPortraitWindow, OldOptionsWindow
74 if GUICommon.CloseOtherWindow (ShowMap):
75 if MapWindow:
76 MapWindow.Unload ()
77 if OptionsWindow:
78 OptionsWindow.Unload ()
79 if PortraitWindow:
80 PortraitWindow.Unload ()
82 MapWindow = None
83 #this window type should block the game
84 GemRB.SetVar ("OtherWindow", -1)
85 GUICommon.GameWindow.SetVisible(WINDOW_VISIBLE)
86 GemRB.UnhideGUI ()
87 GUICommonWindows.PortraitWindow = OldPortraitWindow
88 OldPortraitWindow = None
89 GUICommonWindows.OptionsWindow = OldOptionsWindow
90 OldOptionsWindow = None
91 return
93 GemRB.HideGUI ()
94 GUICommon.GameWindow.SetVisible(WINDOW_INVISIBLE)
96 GemRB.LoadWindowPack ("GUIMAP", 640, 480)
97 MapWindow = Window = GemRB.LoadWindow (2)
98 #this window type blocks the game normally, but map window doesn't
99 GemRB.SetVar ("OtherWindow", MapWindow.ID)
100 #saving the original portrait window
101 OldOptionsWindow = GUICommonWindows.OptionsWindow
102 OptionsWindow = GemRB.LoadWindow (0)
103 GUICommonWindows.SetupMenuWindowControls (OptionsWindow, 0, ShowMap)
104 OldPortraitWindow = GUICommonWindows.PortraitWindow
105 PortraitWindow = GUICommonWindows.OpenPortraitWindow (0)
106 OptionsWindow.SetFrame ()
108 # World Map
109 Button = Window.GetControl (1)
110 Button.SetState (IE_GUI_BUTTON_LOCKED)
112 # Hide or Show mapnotes
113 Button = Window.GetControl (3)
114 Button.SetState (IE_GUI_BUTTON_LOCKED)
116 Label = Window.GetControl (0x10000003)
117 Label.SetText ("")
119 # Map Control
120 Window.CreateMapControl (2, 0, 0, 0, 0)
121 Map = Window.GetControl (2)
122 GemRB.SetVar ("ShowMapNotes",IE_GUI_MAP_REVEAL_MAP)
123 Map.SetVarAssoc ("ShowMapNotes", IE_GUI_MAP_REVEAL_MAP)
124 Map.SetEvent (IE_GUI_MAP_ON_PRESS, RevealMap)
125 Window.SetVisible (WINDOW_VISIBLE)
126 OptionsWindow.SetVisible (WINDOW_GRAYED)
127 PortraitWindow.SetVisible (WINDOW_GRAYED)
128 OptionsWindow.SetVisible (WINDOW_FRONT)
129 PortraitWindow.SetVisible (WINDOW_FRONT)
130 Window.SetVisible (WINDOW_FRONT)
131 Map.SetStatus (IE_GUI_CONTROL_FOCUSED)
132 GemRB.GamePause (0,0)
133 return
135 ###################################################
136 def OpenMapWindow ():
137 global MapWindow, OptionsWindow, PortraitWindow
138 global OldPortraitWindow, OldOptionsWindow
140 if GUICommon.CloseOtherWindow (OpenMapWindow):
141 if MapWindow:
142 MapWindow.Unload ()
143 if OptionsWindow:
144 OptionsWindow.Unload ()
145 if PortraitWindow:
146 PortraitWindow.Unload ()
148 MapWindow = None
149 #this window type should block the game
150 GemRB.SetVar ("OtherWindow", -1)
151 GUICommon.GameWindow.SetVisible(WINDOW_VISIBLE)
152 GemRB.UnhideGUI ()
153 GUICommonWindows.PortraitWindow = OldPortraitWindow
154 OldPortraitWindow = None
155 GUICommonWindows.OptionsWindow = OldOptionsWindow
156 OldOptionsWindow = None
157 return
159 GemRB.HideGUI ()
160 GUICommon.GameWindow.SetVisible(WINDOW_INVISIBLE)
162 GemRB.LoadWindowPack ("GUIMAP", 640, 480)
163 MapWindow = Window = GemRB.LoadWindow (2)
164 #this window type blocks the game normally, but map window doesn't
165 GemRB.SetVar ("OtherWindow", MapWindow.ID)
166 #saving the original portrait window
167 OldOptionsWindow = GUICommonWindows.OptionsWindow
168 OptionsWindow = GemRB.LoadWindow (0)
169 GUICommonWindows.SetupMenuWindowControls (OptionsWindow, 0, OpenMapWindow)
170 OldPortraitWindow = GUICommonWindows.PortraitWindow
171 PortraitWindow = GUICommonWindows.OpenPortraitWindow (0)
172 OptionsWindow.SetFrame ()
174 # World Map
175 Button = Window.GetControl (1)
176 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, OpenWorldMapWindowInside)
178 # Map Control
179 Window.CreateMapControl (2, 0, 0, 0, 0)
180 Map = Window.GetControl (2)
181 Map.SetEvent (IE_GUI_MAP_ON_DOUBLE_PRESS, LeftDoublePressMap)
183 OptionsWindow.SetVisible (WINDOW_VISIBLE)
184 Window.SetVisible (WINDOW_VISIBLE)
185 PortraitWindow.SetVisible (WINDOW_VISIBLE)
186 Map.SetStatus (IE_GUI_CONTROL_FOCUSED)
188 def LeftDoublePressMap ():
189 OpenMapWindow()
190 return
192 def OpenWorldMapWindowInside ():
193 global MapWindow
195 OpenMapWindow () #closes mapwindow
196 MapWindow = -1
197 print "MapWindow=",MapWindow
198 WorldMapWindowCommon (-1)
199 return
201 def OpenWorldMapWindow ():
202 WorldMapWindowCommon (GemRB.GetVar ("Travel"))
203 return
205 def MoveToNewArea ():
206 global WorldMapWindow, WorldMapControl
208 tmp = WorldMapControl.GetDestinationArea (1)
209 CloseWorldMapWindow ()
210 GemRB.CreateMovement (tmp["Destination"], tmp["Entrance"], tmp["Direction"])
211 return
213 def ChangeTooltip ():
214 global WorldMapWindow, WorldMapControl
215 global str
217 tmp = WorldMapControl.GetDestinationArea ()
218 if (tmp):
219 str = "%s: %d"%(GemRB.GetString(23084),tmp["Distance"])
220 else:
221 str=""
223 WorldMapControl.SetTooltip (str)
224 return
226 def CloseWorldMapWindow ():
227 global WorldMapWindow, WorldMapControl
229 print "CloseWorldMapWindow found Mapwindow = ",MapWindow
230 if MapWindow:
231 # reopen map window
232 if WorldMapWindow:
233 WorldMapWindow.Unload ()
234 WorldMapWindow = None
235 WorldMapControl = None
236 OpenMapWindow ()
237 return
239 if WorldMapWindow:
240 WorldMapWindow.Unload ()
241 WorldMapWindow = None
242 WorldMapControl = None
243 GUICommon.GameWindow.SetVisible(WINDOW_VISIBLE)
244 GemRB.UnhideGUI ()
245 return
247 def WorldMapWindowCommon (Travel):
248 global WorldMapWindow, WorldMapControl
250 if WorldMapWindow:
251 CloseWorldMapWindow ()
252 return
254 GemRB.HideGUI ()
255 GUICommon.GameWindow.SetVisible(WINDOW_INVISIBLE)
257 GemRB.LoadWindowPack ("GUIWMAP", 640, 480)
258 WorldMapWindow = Window = GemRB.LoadWindow (0)
259 #saving the original portrait window
260 Window.SetFrame ()
262 Window.CreateWorldMapControl (4, 0, 62, 640, 418, Travel, "infofont")
263 WorldMapControl = Window.GetControl (4)
264 WorldMapControl.SetAnimation ("WMDAG")
265 WorldMapControl.SetEvent (IE_GUI_WORLDMAP_ON_PRESS, MoveToNewArea)
266 WorldMapControl.SetEvent (IE_GUI_MOUSE_ENTER_WORLDMAP, ChangeTooltip)
267 #center on current area
268 WorldMapControl.AdjustScrolling (0,0)
270 #north
271 Button = Window.GetControl (1)
272 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, MapN)
274 #south
275 Button = Window.GetControl (2)
276 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, MapS)
278 # Done
279 Button = Window.GetControl (0)
280 Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseWorldMapWindow)
281 Button.SetFlags (IE_GUI_BUTTON_CANCEL, OP_OR)
282 Window.SetVisible (WINDOW_VISIBLE)
283 return
285 def MapN():
286 WorldMapControl.AdjustScrolling (0, -10)
287 return
289 def MapS():
290 WorldMapControl.AdjustScrolling (0, 10)
291 return
294 ###################################################
295 # End of file GUIMA.py