From 0f362f80e30c5ba1e87bd86f1b17371c5f2fac54 Mon Sep 17 00:00:00 2001 From: BenDol Date: Sun, 13 Jul 2014 22:27:09 +1200 Subject: [PATCH] Added easy menu hooking, fixed terminal default size/pos & more: * Added autowalk style option to Minimap style. * Added onCreate for setting up variables before styling is applied etc. --- modules/client_terminal/terminal.lua | 4 ++-- modules/game_interface/gameinterface.lua | 25 +++++++++++++++++++++++- modules/gamelib/ui/uiminimap.lua | 15 +++++++++++++- src/framework/ui/uimanager.cpp | 2 ++ 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/modules/client_terminal/terminal.lua b/modules/client_terminal/terminal.lua index e0666911..861cbf35 100644 --- a/modules/client_terminal/terminal.lua +++ b/modules/client_terminal/terminal.lua @@ -202,9 +202,9 @@ function popWindow() else terminalWindow:breakAnchors() terminalWindow:setOn(true) - local size = oldSize or { width = g_window.getWidth()/2, height = g_window.getHeight()/2 } + local size = oldSize or { width = g_window.getWidth()/2.5, height = g_window.getHeight()/4 } terminalWindow:setSize(size) - local pos = oldPos or { x = (g_window.getWidth() - terminalWindow:getWidth())/2, y = (g_window.getHeight() - terminalWindow:getHeight())/2 } + local pos = oldPos or { x = 0, y = g_window.getHeight() } terminalWindow:setPosition(pos) terminalWindow:getChildById('bottomResizeBorder'):enable() terminalWindow:getChildById('rightResizeBorder'):enable() diff --git a/modules/game_interface/gameinterface.lua b/modules/game_interface/gameinterface.lua index ec5d8018..2ca87d3d 100644 --- a/modules/game_interface/gameinterface.lua +++ b/modules/game_interface/gameinterface.lua @@ -16,6 +16,7 @@ currentViewMode = 0 smartWalkDirs = {} smartWalkDir = nil walkFunction = nil +hookedMenuOptions = {} function init() g_ui.importStyle('styles/countwindow') @@ -104,6 +105,8 @@ function terminate() save() hide() + hookedMenuOptions = {} + stopSmartWalk() disconnect(g_game, { @@ -409,6 +412,17 @@ function startTradeWith(thing) g_mouse.pushCursor('target') end +function addMenuHook(category, name, callback, condition, shortcut) + if not hookedMenuOptions[category] then + hookedMenuOptions[category] = {} + end + hookedMenuOptions[category][name] = { + callback = callback, + condition = condition, + shortcut = shortcut + } +end + function createThingMenu(menuPosition, lookThing, useThing, creatureThing) if not g_game.isOnline() then return end local menu = g_ui.createWidget('PopupMenu') @@ -440,7 +454,6 @@ function createThingMenu(menuPosition, lookThing, useThing, creatureThing) if useThing:isRotateable() then menu:addOption(tr('Rotate'), function() g_game.rotate(useThing) end) end - end if lookThing and not lookThing:isCreature() and not lookThing:isNotMoveable() and lookThing:isPickupable() then @@ -550,6 +563,16 @@ function createThingMenu(menuPosition, lookThing, useThing, creatureThing) menu:addOption(tr('Copy Name'), function() g_window.setClipboardText(creatureThing:getName()) end) end + -- hooked menu options + for _,category in pairs(hookedMenuOptions) do + menu:addSeparator() + for name,opt in pairs(category) do + if opt.condition(menuPosition, lookThing, useThing, creatureThing) then + menu:addOption(name, opt.callback, opt.shortcut) + end + end + end + menu:display(menuPosition) end diff --git a/modules/gamelib/ui/uiminimap.lua b/modules/gamelib/ui/uiminimap.lua index 1a3a4f2b..db68270d 100644 --- a/modules/gamelib/ui/uiminimap.lua +++ b/modules/gamelib/ui/uiminimap.lua @@ -1,3 +1,7 @@ +function UIMinimap:onCreate() + self.autowalk = true +end + function UIMinimap:onSetup() self.flagWindow = nil self.floorUpWidget = self:getChildById('floorUp') @@ -6,7 +10,6 @@ function UIMinimap:onSetup() self.zoomOutWidget = self:getChildById('zoomOut') self.flags = {} self.alternatives = {} - self.autowalk = true self.onAddAutomapFlag = function(pos, icon, description) self:addFlag(pos, icon, description) end self.onRemoveAutomapFlag = function(pos, icon, description) self:removeFlag(pos, icon, description) end connect(g_game, { @@ -251,6 +254,16 @@ function UIMinimap:onDragLeave(widget, pos) return true end +function UIMinimap:onStyleApply(styleName, styleNode) + for name,value in pairs(styleNode) do + print(name) + if name == 'autowalk' then + print(value) + self.autowalk = value + end + end +end + function UIMinimap:createFlagWindow(pos) if self.flagWindow then return end if not pos then return end diff --git a/src/framework/ui/uimanager.cpp b/src/framework/ui/uimanager.cpp index 6b7761f8..c8e8a20a 100644 --- a/src/framework/ui/uimanager.cpp +++ b/src/framework/ui/uimanager.cpp @@ -447,6 +447,8 @@ UIWidgetPtr UIManager::createWidgetFromOTML(const OTMLNodePtr& widgetNode, const if(parent) parent->addChild(widget); + widget->callLuaField("onCreate"); + if(widget) { widget->setStyleFromNode(styleNode);