1# -*-python-*-
2# GemRB - Infinity Engine Emulator
3# Copyright (C) 2020 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# GUIMA.py - scripts to control map windows from the GUIMA and GUIWMAP winpacks
21
22###################################################
23
24import GemRB
25import GUICommonWindows
26import GUIMACommon
27from GUIDefines import *
28
29WorldMapControl = None
30AreaMapControl = None
31
32def InitMapWindow (Window, WorldMap = False):
33	global WorldMapControl, AreaMapControl
34
35	Label = Window.GetControl (0)
36	if WorldMap:
37		Label.SetText ("World map")
38	else:
39		Label.SetText ("Area map")
40
41	# World Map
42	Button = Window.GetControl (1)
43	if WorldMap:
44		Button.SetText ("MAP")
45	else:
46		Button.SetText ("WMAP")
47	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, lambda: InitMapWindow (Window, not WorldMap))
48
49	# Map or World Map control
50	if WorldMap:
51		if AreaMapControl:
52			AreaMapControl.SetVisible (False)
53			AreaMapControl.SetDisabled (True)
54
55		if WorldMapControl:
56			WorldMapControl.SetVisible (True)
57			WorldMapControl.SetDisabled (False)
58		else:
59			WorldMapControl = Window.ReplaceSubview (2, IE_GUI_WORLDMAP, "floattxt")
60			WorldMapControl.SetAnimation ("WMDAG")
61			WorldMapControl.SetEvent (IE_GUI_WORLDMAP_ON_PRESS, GUIMACommon.MoveToNewArea)
62
63		WorldMapControl.SetVarAssoc("Travel", GemRB.GetVar("Travel"))
64		# center on current area
65		WorldMapControl.Scroll (0, 0, False)
66		WorldMapControl.Focus ()
67	else:
68		if WorldMapControl:
69			WorldMapControl.SetVisible (False)
70			WorldMapControl.SetDisabled (True)
71
72		if AreaMapControl:
73			AreaMapControl.SetVisible (True)
74			AreaMapControl.SetDisabled (False)
75		else:
76			AreaMapControl = Window.ReplaceSubview (4, IE_GUI_MAP)
77			AreaMapControl.SetAction (CloseMapWindow, IE_ACT_MOUSE_PRESS, GEM_MB_ACTION, 0, 2)
78		AreaMapControl.Focus ()
79
80	Button = Window.GetControl (3)
81	Button.SetText ("Close")
82	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseMapWindow)
83	Button.SetHotKey ('m')
84
85	# workaround for proper closure with ESC
86	Button = Window.GetControl (99)
87	if not Button:
88		Button = Window.CreateButton (99, 0, 0, 0, 0)
89	Button.SetEvent (IE_GUI_BUTTON_ON_PRESS, CloseMapWindow)
90	Button.MakeEscape ()
91
92	return
93
94def CloseMapWindow ():
95	global WorldMapControl, AreaMapControl
96
97	WorldMapControl = None
98	AreaMapControl = None
99	GUICommonWindows.CloseTopWindow ()
100
101ToggleMapWindow = GUICommonWindows.CreateTopWinLoader (0, "GUIMAP", GUICommonWindows.ToggleWindow, InitMapWindow)
102OpenMapWindow = GUICommonWindows.CreateTopWinLoader (0, "GUIMAP", GUICommonWindows.OpenWindowOnce, InitMapWindow)
103
104def OpenTravelWindow ():
105	Window = OpenMapWindow ()
106	InitMapWindow (Window, True)
107	return
108