Advertisement
Guest User

gameinferface.lua

a guest
Jul 27th, 2021
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 34.83 KB | None | 0 0
  1. WALK_STEPS_RETRY = 10
  2.  
  3. gameRootPanel = nil
  4. gameMapPanel = nil
  5. gameRightPanel = nil
  6. gameLeftPanel = nil
  7. gameSelectedPanel = nil
  8. panelsList = {}
  9. panelsRadioGroup = nil
  10. gameBottomPanel = nil
  11. showTopMenuButton = nil
  12. logoutButton = nil
  13. mouseGrabberWidget = nil
  14. countWindow = nil
  15. logoutWindow = nil
  16. exitWindow = nil
  17. bottomSplitter = nil
  18. limitedZoom = false
  19. currentViewMode = 0
  20. smartWalkDirs = {}
  21. smartWalkDir = nil
  22. firstStep = false
  23. hookedMenuOptions = {}
  24. lastDirTime = g_clock.millis()
  25.  
  26. function init()
  27.   g_ui.importStyle('styles/countwindow')
  28.  
  29.   connect(g_game, {
  30.     onGameStart = onGameStart,
  31.     onGameEnd = onGameEnd,
  32.     onLoginAdvice = onLoginAdvice,
  33.   }, true)
  34.  
  35.   -- Call load AFTER game window has been created and
  36.   -- resized to a stable state, otherwise the saved
  37.   -- settings can get overridden by false onGeometryChange
  38.   -- events
  39.   connect(g_app, {
  40.     onRun = load,
  41.     onExit = save
  42.   })
  43.  
  44.   gameRootPanel = g_ui.displayUI('gameinterface')
  45.   gameRootPanel:hide()
  46.   gameRootPanel:lower()
  47.   gameRootPanel.onGeometryChange = updateStretchShrink
  48.   gameRootPanel.onFocusChange = stopSmartWalk
  49.  
  50.   mouseGrabberWidget = gameRootPanel:getChildById('mouseGrabber')
  51.   mouseGrabberWidget.onMouseRelease = onMouseGrabberRelease
  52.  
  53.   bottomSplitter = gameRootPanel:getChildById('bottomSplitter')
  54.   gameMapPanel = gameRootPanel:getChildById('gameMapPanel')
  55.   gameRightPanel = gameRootPanel:getChildById('gameRightPanel')
  56.   gameLeftPanel = gameRootPanel:getChildById('gameLeftPanel')
  57.   gameBottomPanel = gameRootPanel:getChildById('gameBottomPanel')
  58.   connect(gameLeftPanel, { onVisibilityChange = onLeftPanelVisibilityChange })
  59.  
  60.   logoutButton = modules.client_topmenu.addLeftButton('logoutButton', tr('Exit'),
  61.     '/images/topbuttons/logout', tryLogout, true)
  62.  
  63.   showTopMenuButton = gameMapPanel:getChildById('showTopMenuButton')
  64.   showTopMenuButton.onClick = function() modules.client_topmenu.toggle() end
  65.  
  66.   setupViewMode(2)
  67.  
  68.   bindKeys()
  69.  
  70.   if g_game.isOnline() then
  71.     show()
  72.   end
  73. end
  74.  
  75. function bindKeys()
  76.   gameRootPanel:setAutoRepeatDelay(200)
  77.  
  78.   bindWalkKey('Up', North)
  79.   bindWalkKey('Right', East)
  80.   bindWalkKey('Down', South)
  81.   bindWalkKey('Left', West)
  82.   bindWalkKey('Numpad8', North)
  83.   bindWalkKey('Numpad9', NorthEast)
  84.   bindWalkKey('Numpad6', East)
  85.   bindWalkKey('Numpad3', SouthEast)
  86.   bindWalkKey('Numpad2', South)
  87.   bindWalkKey('Numpad1', SouthWest)
  88.   bindWalkKey('Numpad4', West)
  89.   bindWalkKey('Numpad7', NorthWest)
  90.  
  91.   bindTurnKey('Ctrl+Up', North)
  92.   bindTurnKey('Ctrl+Right', East)
  93.   bindTurnKey('Ctrl+Down', South)
  94.   bindTurnKey('Ctrl+Left', West)
  95.   bindTurnKey('Ctrl+Numpad8', North)
  96.   bindTurnKey('Ctrl+Numpad6', East)
  97.   bindTurnKey('Ctrl+Numpad2', South)
  98.   bindTurnKey('Ctrl+Numpad4', West)
  99.  
  100.   g_keyboard.bindKeyPress('Escape', function() g_game.cancelAttackAndFollow() end, gameRootPanel)
  101.   g_keyboard.bindKeyDown('Ctrl+Q', function() tryLogout(false) end, gameRootPanel)
  102.   g_keyboard.bindKeyDown('Ctrl+L', function() tryLogout(false) end, gameRootPanel)
  103.   g_keyboard.bindKeyDown('Ctrl+W', function() g_map.cleanTexts() modules.game_textmessage.clearMessages() end, gameRootPanel)
  104. end
  105.  
  106. function bindWalkKey(key, dir)
  107.     g_keyboard.bindKeyDown(key, function() onWalkKeyDown(dir)
  108.     end, gameRootPanel, true)
  109.     g_keyboard.bindKeyUp(key, function() changeWalkDir(dir, true)
  110.     end, gameRootPanel, true)
  111.     g_keyboard.bindKeyPress(key, function() smartWalk(dir) end, gameRootPanel)
  112. end
  113.  
  114. function unbindWalkKey(key)
  115.     g_keyboard.unbindKeyDown(key, gameRootPanel)
  116.     g_keyboard.unbindKeyUp(key, gameRootPanel)
  117.     g_keyboard.unbindKeyPress(key, gameRootPanel)
  118. end
  119.  
  120. function bindTurnKey(key, dir)
  121.     local function callback(widget, code, repeatTicks)
  122.         if g_clock.millis() - lastDirTime >=
  123.             modules.client_options.getOption('turnDelay') then
  124.             g_game.turn(dir)
  125.             changeWalkDir(dir)
  126.  
  127.             lastDirTime = g_clock.millis()
  128.         end
  129.     end
  130.  
  131.     g_keyboard.bindKeyPress(key, callback, gameRootPanel)
  132. end
  133.  
  134. function unbindTurnKey(key)
  135.   g_keyboard.unbindKeyPress(key, gameRootPanel)
  136. end
  137.  
  138. function terminate()
  139.   hide()
  140.  
  141.   hookedMenuOptions = {}
  142.  
  143.   stopSmartWalk()
  144.  
  145.   disconnect(g_game, {
  146.     onGameStart = onGameStart,
  147.     onGameEnd = onGameEnd,
  148.     onLoginAdvice = onLoginAdvice
  149.   })
  150.  
  151.   disconnect(gameLeftPanel, { onVisibilityChange = onLeftPanelVisibilityChange })
  152.  
  153.     for k,v in pairs(panelsList) do
  154.         disconnect(v.checkbox, { onCheckChange = onSelectPanel })
  155.     end
  156.  
  157.   logoutButton:destroy()
  158.   gameRootPanel:destroy()
  159. end
  160.  
  161. function onGameStart()
  162.   show()
  163.  
  164.   -- open tibia has delay in auto walking
  165.   if not g_game.isOfficialTibia() then
  166.     g_game.enableFeature(GameForceFirstAutoWalkStep)
  167.   else
  168.     g_game.disableFeature(GameForceFirstAutoWalkStep)
  169.   end
  170. end
  171.  
  172. function onGameEnd()
  173.   setupViewMode(2)
  174.   hide()
  175. end
  176.  
  177. function show()
  178.   connect(g_app, { onClose = tryExit })
  179.   modules.client_background.hide()
  180.   gameRootPanel:show()
  181.   gameRootPanel:focus()
  182.   gameMapPanel:followCreature(g_game.getLocalPlayer())
  183.   setupViewMode(2)
  184.   updateStretchShrink()
  185.   logoutButton:setTooltip(tr('Logout'))
  186.  
  187.   addEvent(function()
  188.     if not limitedZoom or g_game.isGM() then
  189.       gameMapPanel:setMaxZoomOut(513)
  190.       gameMapPanel:setLimitVisibleRange(false)
  191.     else
  192.       gameMapPanel:setMaxZoomOut(11)
  193.       gameMapPanel:setLimitVisibleRange(true)
  194.     end
  195.   end)
  196. end
  197.  
  198. function hide()
  199.   disconnect(g_app, { onClose = tryExit })
  200.   logoutButton:setTooltip(tr('Exit'))
  201.  
  202.   if logoutWindow then
  203.     logoutWindow:destroy()
  204.     logoutWindow = nil
  205.   end
  206.   if exitWindow then
  207.     exitWindow:destroy()
  208.     exitWindow = nil
  209.   end
  210.   if countWindow then
  211.     countWindow:destroy()
  212.     countWindow = nil
  213.   end
  214.   gameRootPanel:hide()
  215.   modules.client_background.show()
  216. end
  217.  
  218. function save()
  219.   local settings = {}
  220.   settings.splitterMarginBottom = bottomSplitter:getMarginBottom()
  221.   g_settings.setNode('game_interface', settings)
  222. end
  223.  
  224. function load()
  225.   local settings = g_settings.getNode('game_interface')
  226.   if settings then
  227.     if settings.splitterMarginBottom then
  228.       bottomSplitter:setMarginBottom(settings.splitterMarginBottom)
  229.     end
  230.   end
  231. end
  232.  
  233. function onLoginAdvice(message)
  234.   displayInfoBox(tr("For Your Information"), message)
  235. end
  236.  
  237. function forceExit()
  238.   g_game.cancelLogin()
  239.   scheduleEvent(exit, 10)
  240.   return true
  241. end
  242.  
  243. function tryExit()
  244.   if exitWindow then
  245.     return true
  246.   end
  247.  
  248.   local exitFunc = function() g_game.safeLogout() forceExit() end
  249.   local logoutFunc = function() g_game.safeLogout() exitWindow:destroy() exitWindow = nil end
  250.   local cancelFunc = function() exitWindow:destroy() exitWindow = nil end
  251.  
  252.   exitWindow = displayGeneralBox(tr('Exit'), tr("If you shut down the program, your character might stay in the game.\nClick on 'Logout' to ensure that you character leaves the game properly.\nClick on 'Exit' if you want to exit the program without logging out your character."),
  253.   { { text=tr('Force Exit'), callback=exitFunc },
  254.     { text=tr('Logout'), callback=logoutFunc },
  255.     { text=tr('Cancel'), callback=cancelFunc },
  256.     anchor=AnchorHorizontalCenter }, logoutFunc, cancelFunc)
  257.  
  258.   return true
  259. end
  260.  
  261. function tryLogout(prompt)
  262.   if type(prompt) ~= "boolean" then
  263.     prompt = true
  264.   end
  265.   if not g_game.isOnline() then
  266.     exit()
  267.     return
  268.   end
  269.  
  270.   if logoutWindow then
  271.     return
  272.   end
  273.  
  274.   local msg, yesCallback
  275.   if not g_game.isConnectionOk() then
  276.     msg = 'Your connection is failing, if you logout now your character will be still online, do you want to force logout?'
  277.  
  278.     yesCallback = function()
  279.       g_game.forceLogout()
  280.       if logoutWindow then
  281.         logoutWindow:destroy()
  282.         logoutWindow=nil
  283.       end
  284.     end
  285.   else
  286.     msg = 'Are you sure you want to logout?'
  287.  
  288.     yesCallback = function()
  289.       g_game.safeLogout()
  290.       if logoutWindow then
  291.         logoutWindow:destroy()
  292.         logoutWindow=nil
  293.       end
  294.     end
  295.   end
  296.  
  297.   local noCallback = function()
  298.     logoutWindow:destroy()
  299.     logoutWindow=nil
  300.   end
  301.  
  302.   if prompt then
  303.         logoutWindow = displayGeneralBox(tr('Logout'), tr(msg), {
  304.             {text = tr('Yes'), callback = yesCallback},
  305.             {text = tr('No'), callback = noCallback},
  306.             anchor = AnchorHorizontalCenter
  307.         }, yesCallback, noCallback)
  308.     else
  309.         yesCallback()
  310.     end
  311. end
  312.  
  313. function stopSmartWalk()
  314.   smartWalkDirs = {}
  315.   smartWalkDir = nil
  316. end
  317.  
  318. function onWalkKeyDown(dir)
  319.     firstStep = true
  320.     changeWalkDir(dir)
  321. end
  322.  
  323. function changeWalkDir(dir, pop)
  324.     while table.removevalue(smartWalkDirs, dir) do end
  325.     if pop then
  326.         if #smartWalkDirs == 0 then
  327.             stopSmartWalk()
  328.             return
  329.         end
  330.     else
  331.         table.insert(smartWalkDirs, 1, dir)
  332.     end
  333.  
  334.     smartWalkDir = smartWalkDirs[1]
  335.     if modules.client_options.getOption('smartWalk') and #smartWalkDirs > 1 then
  336.         for _, d in pairs(smartWalkDirs) do
  337.             if (smartWalkDir == North and d == West) or
  338.                 (smartWalkDir == West and d == North) then
  339.                 smartWalkDir = NorthWest
  340.                 break
  341.             elseif (smartWalkDir == North and d == East) or
  342.                 (smartWalkDir == East and d == North) then
  343.                 smartWalkDir = NorthEast
  344.                 break
  345.             elseif (smartWalkDir == South and d == West) or
  346.                 (smartWalkDir == West and d == South) then
  347.                 smartWalkDir = SouthWest
  348.                 break
  349.             elseif (smartWalkDir == South and d == East) or
  350.                 (smartWalkDir == East and d == South) then
  351.                 smartWalkDir = SouthEast
  352.                 break
  353.             end
  354.         end
  355.     end
  356. end
  357.  
  358. function smartWalk(dir)
  359.     if g_keyboard.getModifiers() ~= KeyboardNoModifier then return false end
  360.  
  361.     local dire = smartWalkDir or dir
  362.     g_game.walk(dire, firstStep)
  363.     firstStep = false
  364.     return true
  365. end
  366.  
  367. function updateStretchShrink()
  368.   if modules.client_options.getOption('dontStretchShrink') and not alternativeView then
  369.     gameMapPanel:setVisibleDimension({ width = 15, height = 11 })
  370.  
  371.     -- Set gameMapPanel size to height = 11 * 32 + 2
  372.     bottomSplitter:setMarginBottom(bottomSplitter:getMarginBottom() + (gameMapPanel:getHeight() - 32 * 11) - 10)
  373.   end
  374. end
  375.  
  376. function onMouseGrabberRelease(self, mousePosition, mouseButton)
  377.   if selectedThing == nil then return false end
  378.   if mouseButton == MouseLeftButton then
  379.     local clickedWidget = gameRootPanel:recursiveGetChildByPos(mousePosition, false)
  380.     if clickedWidget then
  381.       if selectedType == 'use' then
  382.         onUseWith(clickedWidget, mousePosition)
  383.       elseif selectedType == 'trade' then
  384.         onTradeWith(clickedWidget, mousePosition)
  385.       end
  386.     end
  387.   end
  388.  
  389.   selectedThing = nil
  390.   g_mouse.popCursor('target')
  391.   self:ungrabMouse()
  392.   return true
  393. end
  394.  
  395. function onUseWith(clickedWidget, mousePosition)
  396.   if clickedWidget:getClassName() == 'UIGameMap' then
  397.     local tile = clickedWidget:getTile(mousePosition)
  398.     if tile then
  399.       if selectedThing:isFluidContainer() or selectedThing:isMultiUse() then
  400.         g_game.useWith(selectedThing, tile:getTopMultiUseThing())
  401.       else
  402.         g_game.useWith(selectedThing, tile:getTopUseThing())
  403.       end
  404.     end
  405.   elseif clickedWidget:getClassName() == 'UIItem' and not clickedWidget:isVirtual() then
  406.     g_game.useWith(selectedThing, clickedWidget:getItem())
  407.   elseif clickedWidget:getClassName() == 'UICreatureButton' then
  408.     local creature = clickedWidget:getCreature()
  409.     if creature then
  410.       g_game.useWith(selectedThing, creature)
  411.     end
  412.   end
  413. end
  414.  
  415. function onTradeWith(clickedWidget, mousePosition)
  416.   if clickedWidget:getClassName() == 'UIGameMap' then
  417.     local tile = clickedWidget:getTile(mousePosition)
  418.     if tile then
  419.       g_game.requestTrade(selectedThing, tile:getTopCreature())
  420.     end
  421.   elseif clickedWidget:getClassName() == 'UICreatureButton' then
  422.     local creature = clickedWidget:getCreature()
  423.     if creature then
  424.       g_game.requestTrade(selectedThing, creature)
  425.     end
  426.   end
  427. end
  428.  
  429. function startUseWith(thing)
  430.   if not thing then return end
  431.   if g_ui.isMouseGrabbed() then
  432.     if selectedThing then
  433.       selectedThing = thing
  434.       selectedType = 'use'
  435.     end
  436.     return
  437.   end
  438.   selectedType = 'use'
  439.   selectedThing = thing
  440.   mouseGrabberWidget:grabMouse()
  441.   g_mouse.pushCursor('target')
  442. end
  443.  
  444. function startTradeWith(thing)
  445.   if not thing then return end
  446.   if g_ui.isMouseGrabbed() then
  447.     if selectedThing then
  448.       selectedThing = thing
  449.       selectedType = 'trade'
  450.     end
  451.     return
  452.   end
  453.   selectedType = 'trade'
  454.   selectedThing = thing
  455.   mouseGrabberWidget:grabMouse()
  456.   g_mouse.pushCursor('target')
  457. end
  458.  
  459. function isMenuHookCategoryEmpty(category)
  460.   if category then
  461.     for _,opt in pairs(category) do
  462.       if opt then return false end
  463.     end
  464.   end
  465.   return true
  466. end
  467.  
  468. function addMenuHook(category, name, callback, condition, shortcut)
  469.   if not hookedMenuOptions[category] then
  470.     hookedMenuOptions[category] = {}
  471.   end
  472.   hookedMenuOptions[category][name] = {
  473.     callback = callback,
  474.     condition = condition,
  475.     shortcut = shortcut
  476.   }
  477. end
  478.  
  479. function removeMenuHook(category, name)
  480.   if not name then
  481.     hookedMenuOptions[category] = {}
  482.   else
  483.     hookedMenuOptions[category][name] = nil
  484.   end
  485. end
  486.  
  487. function createThingMenu(menuPosition, lookThing, useThing, creatureThing)
  488.     if not g_game.isOnline() then return end
  489.  
  490.     local menu = g_ui.createWidget('PopupMenu')
  491.     menu:setGameMenu(true)
  492.  
  493.     local classic = modules.client_options.getOption('classicControl')
  494.     local shortcut = nil
  495.  
  496.     if not classic then
  497.         shortcut = '(Shift)'
  498.     else
  499.         shortcut = nil
  500.     end
  501.     if lookThing then
  502.         menu:addOption(tr('Look'), function() g_game.look(lookThing) end,
  503.                        shortcut)
  504.     end
  505.  
  506.     if not classic then
  507.         shortcut = '(Ctrl)'
  508.     else
  509.         shortcut = nil
  510.     end
  511.     if useThing then
  512.         if useThing:isContainer() then
  513.             if useThing:getParentContainer() then
  514.                 menu:addOption(tr('Open'), function()
  515.                     g_game.open(useThing, useThing:getParentContainer())
  516.                 end, shortcut)
  517.                 menu:addOption(tr('Open in new window'),
  518.                                function() g_game.open(useThing) end)
  519.             else
  520.                 menu:addOption(tr('Open'), function()
  521.                     g_game.open(useThing)
  522.                 end, shortcut)
  523.             end
  524.         else
  525.             if useThing:isMultiUse() then
  526.                 menu:addOption(tr('Use with ...'),
  527.                                function() startUseWith(useThing) end, shortcut)
  528.             else
  529.                 menu:addOption(tr('Use'), function()
  530.                     g_game.use(useThing)
  531.                 end, shortcut)
  532.             end
  533.         end
  534.  
  535.         if useThing:isRotateable() then
  536.             menu:addOption(tr('Rotate'), function()
  537.                 g_game.rotate(useThing)
  538.             end)
  539.         end
  540.  
  541.         if g_game.getFeature(GameBrowseField) and useThing:getPosition().x ~=
  542.             0xffff then
  543.             menu:addOption(tr('Browse Field'), function()
  544.                 g_game.browseField(useThing:getPosition())
  545.             end)
  546.         end
  547.     end
  548.  
  549.     if lookThing and not lookThing:isCreature() and
  550.         not lookThing:isNotMoveable() and lookThing:isPickupable() then
  551.         menu:addSeparator()
  552.         menu:addOption(tr('Trade with ...'),
  553.                        function() startTradeWith(lookThing) end)
  554.     end
  555.  
  556.     if lookThing then
  557.         local parentContainer = lookThing:getParentContainer()
  558.         if parentContainer and parentContainer:hasParent() then
  559.             menu:addOption(tr('Move up'), function()
  560.                 g_game.moveToParentContainer(lookThing, lookThing:getCount())
  561.             end)
  562.         end
  563.     end
  564.  
  565.     if creatureThing then
  566.         local localPlayer = g_game.getLocalPlayer()
  567.         menu:addSeparator()
  568.  
  569.         if creatureThing:isLocalPlayer() then
  570.             menu:addOption(tr('Set Outfit'),
  571.                            function() g_game.requestOutfit() end)
  572.  
  573.             if g_game.getFeature(GamePlayerMounts) then
  574.                 if not localPlayer:isMounted() then
  575.                     menu:addOption(tr('Mount'),
  576.                                    function()
  577.                         localPlayer:mount()
  578.                     end)
  579.                 else
  580.                     menu:addOption(tr('Dismount'),
  581.                                    function()
  582.                         localPlayer:dismount()
  583.                     end)
  584.                 end
  585.             end
  586.  
  587.             if creatureThing:isPartyMember() then
  588.                 if creatureThing:isPartyLeader() then
  589.                     if creatureThing:isPartySharedExperienceActive() then
  590.                         menu:addOption(tr('Disable Shared Experience'),
  591.                                        function()
  592.                             g_game.partyShareExperience(false)
  593.                         end)
  594.                     else
  595.                         menu:addOption(tr('Enable Shared Experience'),
  596.                                        function()
  597.                             g_game.partyShareExperience(true)
  598.                         end)
  599.                     end
  600.                 end
  601.                 menu:addOption(tr('Leave Party'),
  602.                                function() g_game.partyLeave() end)
  603.             end
  604.  
  605.         else
  606.             local localPosition = localPlayer:getPosition()
  607.             if not classic then
  608.                 shortcut = '(Alt)'
  609.             else
  610.                 shortcut = nil
  611.             end
  612.             if creatureThing:getPosition().z == localPosition.z then
  613.                 if g_game.getAttackingCreature() ~= creatureThing then
  614.                     menu:addOption(tr('Attack'),
  615.                                    function()
  616.                         g_game.attack(creatureThing)
  617.                     end, shortcut)
  618.                 else
  619.                     menu:addOption(tr('Stop Attack'),
  620.                                    function()
  621.                         g_game.cancelAttack()
  622.                     end, shortcut)
  623.                 end
  624.  
  625.                 if g_game.getFollowingCreature() ~= creatureThing then
  626.                     menu:addOption(tr('Follow'),
  627.                                    function()
  628.                         g_game.follow(creatureThing)
  629.                     end)
  630.                 else
  631.                     menu:addOption(tr('Stop Follow'),
  632.                                    function()
  633.                         g_game.cancelFollow()
  634.                     end)
  635.                 end
  636.             end
  637.  
  638.             if creatureThing:isPlayer() then
  639.                 menu:addSeparator()
  640.                 local creatureName = creatureThing:getName()
  641.                 menu:addOption(tr('Message to %s', creatureName), function()
  642.                     g_game.openPrivateChannel(creatureName)
  643.                 end)
  644.                 if modules.game_console.getOwnPrivateTab() then
  645.                     menu:addOption(tr('Invite to private chat'), function()
  646.                         g_game.inviteToOwnChannel(creatureName)
  647.                     end)
  648.                     menu:addOption(tr('Exclude from private chat'), function()
  649.                         g_game.excludeFromOwnChannel(creatureName)
  650.                     end) -- [TODO] must be removed after message's popup labels been implemented
  651.                 end
  652.                 if not localPlayer:hasVip(creatureName) then
  653.                     menu:addOption(tr('Add to VIP list'),
  654.                                    function()
  655.                         g_game.addVip(creatureName)
  656.                     end)
  657.                 end
  658.  
  659.                 if modules.game_console.isIgnored(creatureName) then
  660.                     menu:addOption(tr('Unignore') .. ' ' .. creatureName,
  661.                                    function()
  662.                         modules.game_console.removeIgnoredPlayer(creatureName)
  663.                     end)
  664.                 else
  665.                     menu:addOption(tr('Ignore') .. ' ' .. creatureName,
  666.                                    function()
  667.                         modules.game_console.addIgnoredPlayer(creatureName)
  668.                     end)
  669.                 end
  670.  
  671.                 local localPlayerShield = localPlayer:getShield()
  672.                 local creatureShield = creatureThing:getShield()
  673.  
  674.                 if localPlayerShield == ShieldNone or localPlayerShield ==
  675.                     ShieldWhiteBlue then
  676.                     if creatureShield == ShieldWhiteYellow then
  677.                         menu:addOption(tr('Join %s\'s Party',
  678.                                           creatureThing:getName()), function()
  679.                             g_game.partyJoin(creatureThing:getId())
  680.                         end)
  681.                     else
  682.                         menu:addOption(tr('Invite to Party'), function()
  683.                             g_game.partyInvite(creatureThing:getId())
  684.                         end)
  685.                     end
  686.                 elseif localPlayerShield == ShieldWhiteYellow then
  687.                     if creatureShield == ShieldWhiteBlue then
  688.                         menu:addOption(tr('Revoke %s\'s Invitation',
  689.                                           creatureThing:getName()), function()
  690.                             g_game.partyRevokeInvitation(creatureThing:getId())
  691.                         end)
  692.                     end
  693.                 elseif localPlayerShield == ShieldYellow or localPlayerShield ==
  694.                     ShieldYellowSharedExp or localPlayerShield ==
  695.                     ShieldYellowNoSharedExpBlink or localPlayerShield ==
  696.                     ShieldYellowNoSharedExp then
  697.                     if creatureShield == ShieldWhiteBlue then
  698.                         menu:addOption(tr('Revoke %s\'s Invitation',
  699.                                           creatureThing:getName()), function()
  700.                             g_game.partyRevokeInvitation(creatureThing:getId())
  701.                         end)
  702.                     elseif creatureShield == ShieldBlue or creatureShield ==
  703.                         ShieldBlueSharedExp or creatureShield ==
  704.                         ShieldBlueNoSharedExpBlink or creatureShield ==
  705.                         ShieldBlueNoSharedExp then
  706.                         menu:addOption(tr('Pass Leadership to %s',
  707.                                           creatureThing:getName()), function()
  708.                             g_game.partyPassLeadership(creatureThing:getId())
  709.                         end)
  710.                     else
  711.                         menu:addOption(tr('Invite to Party'), function()
  712.                             g_game.partyInvite(creatureThing:getId())
  713.                         end)
  714.                     end
  715.                 end
  716.             end
  717.         end
  718.  
  719.         if modules.game_ruleviolation.hasWindowAccess() and
  720.             creatureThing:isPlayer() then
  721.             menu:addSeparator()
  722.             menu:addOption(tr('Rule Violation'), function()
  723.                 modules.game_ruleviolation.show(creatureThing:getName())
  724.             end)
  725.         end
  726.  
  727.         menu:addSeparator()
  728.         menu:addOption(tr('Copy Name'), function()
  729.             g_window.setClipboardText(creatureThing:getName())
  730.         end)
  731.     end
  732.  
  733.     -- hooked menu options
  734.     for _, category in pairs(hookedMenuOptions) do
  735.         if not isMenuHookCategoryEmpty(category) then
  736.             menu:addSeparator()
  737.             for name, opt in pairs(category) do
  738.                 if opt and
  739.                     opt.condition(menuPosition, lookThing, useThing,
  740.                                   creatureThing) then
  741.                     menu:addOption(name, function()
  742.                         opt.callback(menuPosition, lookThing, useThing,
  743.                                      creatureThing)
  744.                     end, opt.shortcut)
  745.                 end
  746.             end
  747.         end
  748.     end
  749.  
  750.     menu:display(menuPosition)
  751. end
  752.  
  753. function processMouseAction(menuPosition, mouseButton, autoWalkPos, lookThing,
  754.                             useThing, creatureThing, attackCreature)
  755.     local keyboardModifiers = g_keyboard.getModifiers()
  756.  
  757.     if not modules.client_options.getOption('classicControl') then
  758.         if keyboardModifiers == KeyboardNoModifier and mouseButton ==
  759.             MouseRightButton then
  760.             createThingMenu(menuPosition, lookThing, useThing, creatureThing)
  761.             return true
  762.         elseif lookThing and keyboardModifiers == KeyboardShiftModifier and
  763.             (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  764.             g_game.look(lookThing)
  765.             return true
  766.         elseif useThing and keyboardModifiers == KeyboardCtrlModifier and
  767.             (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  768.             if useThing:isContainer() then
  769.                 if useThing:getParentContainer() then
  770.                     g_game.open(useThing, useThing:getParentContainer())
  771.                 else
  772.                     g_game.open(useThing)
  773.                 end
  774.                 return true
  775.             elseif useThing:isMultiUse() then
  776.                 startUseWith(useThing)
  777.                 return true
  778.             else
  779.                 g_game.use(useThing)
  780.                 return true
  781.             end
  782.             return true
  783.         elseif useThing and useThing:isContainer() and keyboardModifiers == KeyboardCtrlShiftModifier and (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  784.             g_game.open(useThing)
  785.             return true
  786.         elseif attackCreature and g_keyboard.isAltPressed() and
  787.             (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  788.             g_game.attack(attackCreature)
  789.             return true
  790.         elseif creatureThing and creatureThing:getPosition().z == autoWalkPos.z and
  791.             g_keyboard.isAltPressed() and
  792.             (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  793.             g_game.attack(creatureThing)
  794.             return true
  795.         end
  796.  
  797.         -- classic control
  798.     else
  799.         if useThing and keyboardModifiers == KeyboardNoModifier and mouseButton ==
  800.             MouseRightButton and not g_mouse.isPressed(MouseLeftButton) then
  801.             local player = g_game.getLocalPlayer()
  802.             if attackCreature and attackCreature ~= player then
  803.                 g_game.attack(attackCreature)
  804.                 return true
  805.             elseif creatureThing and creatureThing ~= player and
  806.                 creatureThing:getPosition().z == autoWalkPos.z then
  807.                 g_game.attack(creatureThing)
  808.                 return true
  809.             elseif useThing:isContainer() then
  810.                 if useThing:getParentContainer() then
  811.                     g_game.open(useThing, useThing:getParentContainer())
  812.                     return true
  813.                 else
  814.                     g_game.open(useThing)
  815.                     return true
  816.                 end
  817.             elseif useThing:isMultiUse() then
  818.                 startUseWith(useThing)
  819.                 return true
  820.             else
  821.                 g_game.use(useThing)
  822.                 return true
  823.             end
  824.             return true
  825.         elseif useThing and useThing:isContainer() and keyboardModifiers == KeyboardCtrlShiftModifier and (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  826.             g_game.open(useThing)
  827.             return true
  828.         elseif lookThing and keyboardModifiers == KeyboardShiftModifier and
  829.             (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  830.             g_game.look(lookThing)
  831.             return true
  832.         elseif lookThing and
  833.             ((g_mouse.isPressed(MouseLeftButton) and mouseButton ==
  834.                 MouseRightButton) or
  835.                 (g_mouse.isPressed(MouseRightButton) and mouseButton ==
  836.                     MouseLeftButton)) then
  837.             g_game.look(lookThing)
  838.             return true
  839.         elseif useThing and keyboardModifiers == KeyboardCtrlModifier and
  840.             (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  841.             createThingMenu(menuPosition, lookThing, useThing, creatureThing)
  842.             return true
  843.         elseif attackCreature and g_keyboard.isAltPressed() and
  844.             (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  845.             g_game.attack(attackCreature)
  846.             return true
  847.         elseif creatureThing and creatureThing:getPosition().z == autoWalkPos.z and
  848.             g_keyboard.isAltPressed() and
  849.             (mouseButton == MouseLeftButton or mouseButton == MouseRightButton) then
  850.             g_game.attack(creatureThing)
  851.             return true
  852.         end
  853.     end
  854.  
  855.     local player = g_game.getLocalPlayer()
  856.     player:stopAutoWalk()
  857.  
  858.     if autoWalkPos and keyboardModifiers == KeyboardNoModifier and mouseButton ==
  859.         MouseLeftButton then
  860.         player:autoWalk(autoWalkPos)
  861.         return true
  862.     end
  863.  
  864.     return false
  865. end
  866.  
  867. function moveStackableItem(item, toPos)
  868.     if countWindow then return end
  869.     if g_keyboard.isShiftPressed() then
  870.         g_game.move(item, toPos, 1)
  871.         return
  872.     elseif g_keyboard.isCtrlPressed() ~= modules.client_options.getOption('moveStack') then
  873.         g_game.move(item, toPos, item:getCount())
  874.         return
  875.     end
  876.     local count = item:getCount()
  877.  
  878.     countWindow = g_ui.createWidget('CountWindow', rootWidget)
  879.     local itembox = countWindow:getChildById('item')
  880.     local scrollbar = countWindow:getChildById('countScrollBar')
  881.     itembox:setItemId(item:getId())
  882.     itembox:setItemCount(count)
  883.     scrollbar:setMaximum(count)
  884.     scrollbar:setMinimum(1)
  885.     scrollbar:setValue(count)
  886.  
  887.     local spinbox = countWindow:getChildById('spinBox')
  888.     spinbox:setMaximum(count)
  889.     spinbox:setMinimum(0)
  890.     spinbox:setValue(0)
  891.     spinbox:hideButtons()
  892.     spinbox:focus()
  893.     spinbox.firstEdit = true
  894.  
  895.     local spinBoxValueChange = function(self, value)
  896.         spinbox.firstEdit = false
  897.         scrollbar:setValue(value)
  898.     end
  899.     spinbox.onValueChange = spinBoxValueChange
  900.  
  901.     local check = function()
  902.         if spinbox.firstEdit then
  903.             spinbox:setValue(spinbox:getMaximum())
  904.             spinbox.firstEdit = false
  905.         end
  906.     end
  907.     g_keyboard.bindKeyPress("Up", function()
  908.         check()
  909.         spinbox:up()
  910.     end, spinbox)
  911.     g_keyboard.bindKeyPress("Down", function()
  912.         check()
  913.         spinbox:down()
  914.     end, spinbox)
  915.     g_keyboard.bindKeyPress("Right", function()
  916.         check()
  917.         spinbox:up()
  918.     end, spinbox)
  919.     g_keyboard.bindKeyPress("Left", function()
  920.         check()
  921.         spinbox:down()
  922.     end, spinbox)
  923.     g_keyboard.bindKeyPress("PageUp", function()
  924.         check()
  925.         spinbox:setValue(spinbox:getValue() + 10)
  926.     end, spinbox)
  927.     g_keyboard.bindKeyPress("PageDown", function()
  928.         check()
  929.         spinbox:setValue(spinbox:getValue() - 10)
  930.     end, spinbox)
  931.  
  932.     scrollbar.onValueChange = function(self, value)
  933.         itembox:setItemCount(value)
  934.         spinbox.onValueChange = nil
  935.         spinbox:setValue(value)
  936.         spinbox.onValueChange = spinBoxValueChange
  937.     end
  938.  
  939.     local okButton = countWindow:getChildById('buttonOk')
  940.     local moveFunc = function()
  941.         g_game.move(item, toPos, itembox:getItemCount())
  942.         okButton:getParent():destroy()
  943.         countWindow = nil
  944.         modules.game_hotkeys.enableHotkeys(true)
  945.     end
  946.     local cancelButton = countWindow:getChildById('buttonCancel')
  947.     local cancelFunc = function()
  948.         cancelButton:getParent():destroy()
  949.         countWindow = nil
  950.         modules.game_hotkeys.enableHotkeys(true)
  951.     end
  952.  
  953.     countWindow.onEnter = moveFunc
  954.     countWindow.onEscape = cancelFunc
  955.  
  956.     okButton.onClick = moveFunc
  957.     cancelButton.onClick = cancelFunc
  958.  
  959.     modules.game_hotkeys.enableHotkeys(false)
  960. end
  961.  
  962. function getRootPanel() return gameRootPanel end
  963.  
  964. function getMapPanel() return gameMapPanel end
  965.  
  966. function getRightPanel() return gameRightPanel end
  967.  
  968. function getLeftPanel() return gameLeftPanel end
  969.  
  970. function getRightExtraPanel() return gameRightExtraPanel end
  971.  
  972. function getSelectedPanel() return gameSelectedPanel end
  973.  
  974. function getBottomPanel() return gameBottomPanel end
  975.  
  976. function getShowTopMenuButton() return showTopMenuButton end
  977.  
  978. function onLeftPanelVisibilityChange(leftPanel, visible)
  979.   if not visible and g_game.isOnline() then
  980.     local children = leftPanel:getChildren()
  981.     for i=1,#children do
  982.       children[i]:setParent(gameRightPanel)
  983.     end
  984.   end
  985. end
  986.  
  987. function nextViewMode()
  988.   setupViewMode((currentViewMode + 1) % 3)
  989. end
  990.  
  991. function setupViewMode(mode)
  992.   if mode == currentViewMode then return end
  993.  
  994.   if currentViewMode == 2 then
  995.     gameMapPanel:addAnchor(AnchorLeft, 'gameLeftPanel', AnchorRight)
  996.     gameMapPanel:addAnchor(AnchorRight, 'gameRightPanel', AnchorLeft)
  997.     gameMapPanel:addAnchor(AnchorBottom, 'gameBottomPanel', AnchorTop)
  998.     gameRootPanel:addAnchor(AnchorTop, 'topMenu', AnchorBottom)
  999.     gameLeftPanel:setOn(modules.client_options.getOption('showLeftPanel'))
  1000.     gameLeftPanel:setImageColor('white')
  1001.     gameRightPanel:setImageColor('white')
  1002.     gameLeftPanel:setMarginTop(4)
  1003.     gameRightPanel:setMarginTop(4)
  1004.     gameBottomPanel:setImageColor('white')
  1005.     modules.client_topmenu.getTopMenu():setImageColor('white')
  1006.     g_game.changeMapAwareRange(18, 14)
  1007.   end
  1008.  
  1009.   if mode == 0 then
  1010.     gameMapPanel:setKeepAspectRatio(true)
  1011.     gameMapPanel:setLimitVisibleRange(false)
  1012.     gameMapPanel:setZoom(12)
  1013.     gameMapPanel:setVisibleDimension({ width = 15, height = 11 })
  1014.   elseif mode == 1 then
  1015.     gameMapPanel:setKeepAspectRatio(false)
  1016.     gameMapPanel:setLimitVisibleRange(true)
  1017.     gameMapPanel:setZoom(12)
  1018.     gameMapPanel:setVisibleDimension({ width = 15, height = 11 })
  1019.   elseif mode == 2 then
  1020.     local limit = limitedZoom and not g_game.isGM()
  1021.     gameMapPanel:setLimitVisibleRange(limit)
  1022.     gameMapPanel:setZoom(12)
  1023.     gameMapPanel:setVisibleDimension({ width = 23, height = 11 })
  1024.     gameMapPanel:fill('parent')
  1025.     gameRootPanel:fill('parent')
  1026.     gameLeftPanel:setImageColor('alpha')
  1027.     gameRightPanel:setImageColor('alpha')
  1028.     gameLeftPanel:setMarginTop(modules.client_topmenu.getTopMenu()
  1029.       :getHeight() - gameLeftPanel:getPaddingTop())
  1030.     gameRightPanel:setMarginTop(modules.client_topmenu.getTopMenu()
  1031.       :getHeight() - gameRightPanel:getPaddingTop())
  1032.     gameLeftPanel:setOn(true)
  1033.     gameLeftPanel:setVisible(true)
  1034.     gameRightPanel:setOn(true)
  1035.     gameMapPanel:setOn(true)
  1036.     gameBottomPanel:setImageColor('#ffffff88')
  1037.     modules.client_topmenu.getTopMenu():setImageColor('#ffffff66')
  1038.     if not limit then
  1039.       g_game.changeMapAwareRange(24, 20)
  1040.     end
  1041.   end
  1042.  
  1043.   currentViewMode = mode
  1044. end
  1045.  
  1046. function limitZoom()
  1047.   limitedZoom = true
  1048. end
  1049.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement