Skip to content

Commit

Permalink
improve: module controller
Browse files Browse the repository at this point in the history
  • Loading branch information
mehah authored May 13, 2023
1 parent 840102c commit 0b28516
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 201 deletions.
12 changes: 5 additions & 7 deletions modules/game_attachedeffects/attachedeffects.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
controller = Controller:new()

-- uncomment this line to apply an effect on the local player, just for testing purposes.
--[[controller:onGameStart(function()
--[[function controller:onGameStart()
g_game.getLocalPlayer():attachEffect(g_attachedEffects.getById(1))
g_game.getLocalPlayer():attachEffect(g_attachedEffects.getById(2))
g_game.getLocalPlayer():attachEffect(g_attachedEffects.getById(3))
end)
end
controller:onGameEnd(function()
function controller:onGameEnd()
g_game.getLocalPlayer():clearAttachedEffects()
end)
]]

controller.onTerminate = function()
end]]
function controller:onTerminate()
g_attachedEffects.clear()
end

Expand Down
68 changes: 32 additions & 36 deletions modules/game_minimap/minimap.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local otmm = true
local oldPos = nil
local minimapButton = nil

local function updateCameraPosition()
local player = g_game.getLocalPlayer()
Expand All @@ -12,7 +13,7 @@ local function updateCameraPosition()
return
end

local minimapWidget = controller.widgets.minimapWidget
local minimapWidget = controller.ui.contentsPanel.minimap
if minimapWidget:isDragging() then
return
end
Expand All @@ -25,34 +26,31 @@ local function updateCameraPosition()
end

local function toggle()
local minimapWindow = controller.widgets.minimapWindow

if controller.widgets.minimapButton:isOn() then
minimapWindow:close()
if minimapButton:isOn() then
controller.ui:close()
else
minimapWindow:open()
controller.ui:open()
end
end

local function toggleFullMap()
local minimapWindow = controller.widgets.minimapWindow
local minimapWidget = controller.widgets.minimapWidget
local minimapWidget = controller.ui.contentsPanel.minimap
local zoom;

if minimapWidget.fullMapView then
minimapWidget:setParent(minimapWindow:getChildById('contentsPanel'))
minimapWidget:setParent(controller.ui.contentsPanel)
minimapWidget:fill('parent')
minimapWindow:show(true)
controller.ui:show(true)
zoom = minimapWidget.zoomMinimap
else
minimapWindow:hide(true)
controller.ui:hide(true)
minimapWidget:setParent(modules.game_interface.getRootPanel())
minimapWidget:fill('parent')
zoom = minimapWidget.zoomFullmap
end

minimapWidget.fullMapView = not minimapWidget.fullMapView
minimapWidget:setAlternativeWidgetsVisible(fullmapView)
-- minimapWidget:setAlternativeWidgetsVisible(fullmapView)

local pos = oldPos or minimapWidget:getCameraPosition()
oldPos = minimapWidget:getCameraPosition()
Expand All @@ -65,18 +63,15 @@ local localPlayerEvent = EventController:new(LocalPlayer, {
})

controller = Controller:new()
controller:setUI('minimap')
controller:attachExternalEvent(localPlayerEvent)

function controller:onInit()
local minimapButton = modules.client_topmenu.addRightGameToggleButton('minimapButton', tr('Minimap') .. ' (Ctrl+M)',
'/images/topbuttons/minimap', toggle)
minimapButton = modules.client_topmenu.addRightGameToggleButton('minimapButton', tr('Minimap') .. ' (Ctrl+M)',
'/images/topbuttons/minimap', toggle)
minimapButton:setOn(true)

local minimapWindow = g_ui.loadUI('minimap')

minimapWindow:setContentMinimumHeight(80)

local minimapWidget = minimapWindow:recursiveGetChildById('minimap')
local minimapWidget = self.ui.contentsPanel.minimap

local gameRootPanel = modules.game_interface.getRootPanel()
self:bindKeyPress('Alt+Left', function()
Expand All @@ -95,15 +90,13 @@ function controller:onInit()
self:bindKeyDown('Ctrl+M', toggle)
self:bindKeyDown('Ctrl+Shift+M', toggleFullMap)

self:registerWidget('minimapButton', minimapButton)
self:registerWidget('minimapWindow', minimapWindow)
self:registerWidget('minimapWidget', minimapWidget)

minimapWindow:setup()
self.ui:setVisible(false)
self.ui:setContentMinimumHeight(80)
self.ui:setup()
end

controller:onGameStart(function()
controller.widgets.minimapWindow:setupOnStart() -- load character window configuration
function controller:onGameStart()
self.ui:setupOnStart() -- load character window configuration

-- Load Map
g_minimap.clean()
Expand All @@ -123,12 +116,11 @@ controller:onGameStart(function()
loadFnc(minimapFile)
end

local minimapWidget = controller.widgets.minimapWidget
minimapWidget:load()
end)
self.ui.contentsPanel.minimap:load()
end

controller:onGameEnd(function()
controller.widgets.minimapWindow:setParent(nil, true)
function controller:onGameEnd()
self.ui:setParent(nil, true)

-- Save Map
if otmm then
Expand All @@ -137,17 +129,21 @@ controller:onGameEnd(function()
g_map.saveOtcm('/minimap_' .. g_game.getClientVersion() .. '.otcm')
end

local minimapWidget = controller.widgets.minimapWidget
minimapWidget:save()
end)
self.ui.contentsPanel.minimap:save()
end

function controller:onTerminate()
minimapButton:destroy()
minimapButton = nil
end

function onMiniWindowOpen()
controller.widgets.minimapButton:setOn(true)
minimapButton:setOn(true)
localPlayerEvent:connect()
localPlayerEvent:execute('onPositionChange')
end

function onMiniWindowClose()
controller.widgets.minimapButton:setOn(false)
minimapButton:setOn(false)
localPlayerEvent:disconnect()
end
Loading

0 comments on commit 0b28516

Please sign in to comment.