forked from squeek502/Minimap-HUD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodmain.lua
137 lines (112 loc) · 3.8 KB
/
modmain.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
GLOBAL.setmetatable(env, {__index=GLOBAL})
local mapscale = GetModConfigData("Minimap Size")
local position_str = GetModConfigData("Position")
local margin_size_x = GetModConfigData("Horizontal Margin")
local margin_size_y = GetModConfigData("Vertical Margin")
local dir_vert = 0
local dir_horiz = 0
local anchor_vert = 0
local anchor_horiz = 0
local margin_dir_vert = 0
local margin_dir_horiz = 0
local y_align, x_align = position_str:match("(%a+)_(%a+)")
if x_align == "left" then
dir_horiz = -1
anchor_horiz = 1
margin_dir_horiz = 1
elseif x_align == "center" then
dir_horiz = 0
anchor_horiz = 0
margin_dir_horiz = 0
elseif x_align == "right" then
dir_horiz = 1
anchor_horiz = -1
margin_dir_horiz = -1
end
if y_align == "top" then
dir_vert = 0
anchor_vert = -1
margin_dir_vert = -1
elseif y_align == "middle" then
dir_vert = -1
anchor_vert = 0
margin_dir_vert = 0
elseif y_align == "bottom" then
dir_vert = -2
anchor_vert = 1
margin_dir_vert = 1
end
----------------------------------------
-- Do the stuff
----------------------------------------
local function PositionMiniMap(controls, screensize)
local hudscale = controls.top_root:GetScale()
local screenw_full, screenh_full = unpack(screensize)
local screenw = screenw_full/hudscale.x
local screenh = screenh_full/hudscale.y
controls.minimap_small:SetPosition(
(anchor_horiz*controls.minimap_small.mapsize.w/2)+(dir_horiz*screenw/2)+(margin_dir_horiz*margin_size_x),
(anchor_vert*controls.minimap_small.mapsize.h/2)+(dir_vert*screenh/2)+(margin_dir_vert*margin_size_y),
0
)
end
local function AddMiniMap( inst )
-- for some reason, without this the game would crash without an error when calling controls.topright_root:AddChild
-- too lazy to track down the cause, so just using this workaround
inst:DoTaskInTime( 0, function()
-- add the minimap widget and set its position
local MiniMapWidget = require "widgets/minimapwidget"
local controls = inst.HUD.controls
controls.minimap_small = controls.top_root:AddChild( MiniMapWidget( mapscale ) )
local screensize = {TheSim:GetScreenSize()}
PositionMiniMap(controls, screensize)
local OnUpdate_base = controls.OnUpdate
controls.OnUpdate = function(self, dt)
OnUpdate_base(self, dt)
local curscreensize = {TheSim:GetScreenSize()}
if curscreensize[1] ~= screensize[1] or curscreensize[2] ~= screensize[2] then
PositionMiniMap(controls, curscreensize)
screensize = curscreensize
end
end
-- show and hide the minimap whenever the map gets toggled
local ToggleMap_base = controls.ToggleMap
controls.ToggleMap = function( self )
local wasvisible = controls.minimap_small:IsVisible()
if wasvisible then
controls.minimap_small:Hide()
end
ToggleMap_base( self )
if not wasvisible then
controls.minimap_small:Show()
end
end
-- special case: ToggleMap gets bypassed when the map gets hidden while on the map screen
local MapScreen = require "screens/mapscreen"
MapScreen_OnControl_base = MapScreen.OnControl
MapScreen.OnControl = function( self, control, down )
local ret = MapScreen_OnControl_base(self, control, down)
if ret and control == CONTROL_MAP then
controls.minimap_small:Show()
end
return ret
end
-- keep track of zooming while on the map screen
local MapWidget = require "widgets/mapwidget"
MapWidget_OnZoomIn_base = MapWidget.OnZoomIn
MapWidget.OnZoomIn = function(self)
MapWidget_OnZoomIn_base( self )
if self.shown then
controls.minimap_small.mapscreenzoom = math.max(0,controls.minimap_small.mapscreenzoom-1)
end
end
MapWidget_OnZoomOut_base = MapWidget.OnZoomOut
MapWidget.OnZoomOut = function(self)
MapWidget_OnZoomOut_base( self )
if self.shown then
controls.minimap_small.mapscreenzoom = controls.minimap_small.mapscreenzoom+1
end
end
end)
end
AddSimPostInit( AddMiniMap )