Chili:Example 3: Difference between revisions
From Spring
Jump to navigationJump to search
Silentwings (talk | contribs) |
|||
| (5 intermediate revisions by one other user not shown) | |||
| Line 3: | Line 3: | ||
This is a very basic menu that displays a button for each command currently available. | This is a very basic menu that displays a button for each command currently available. | ||
< | <syntaxhighlight lang="lua"> | ||
function widget:GetInfo() | function widget:GetInfo() | ||
return { | return { | ||
| Line 18: | Line 18: | ||
local Chili, window, orderGrid, buildGrid | local Chili, window, orderGrid, buildGrid | ||
local updateRequired | |||
----------------------- | ----------------------- | ||
| Line 32: | Line 33: | ||
function ActionCommand(self, x, y, button, mods) | function ActionCommand(self, x, y, button, mods) | ||
local index = Spring.GetCmdDescIndex(self.cmdID) | local index = Spring.GetCmdDescIndex(self.cmdID) | ||
if | if index then | ||
local left, right = (button == 1), (button == 3) | local left, right = (button == 1), (button == 3) | ||
local alt, ctrl, meta, shift = mods.alt, mods.ctrl, mods.meta, mods.shift | local alt, ctrl, meta, shift = mods.alt, mods.ctrl, mods.meta, mods.shift | ||
| Line 43: | Line 44: | ||
name = "orderButton_" .. cmd.action, | name = "orderButton_" .. cmd.action, | ||
cmdID = cmd.id, | cmdID = cmd.id, | ||
caption = cmd. | caption = cmd.name, | ||
padding = {0,0,0,0}, | padding = {0,0,0,0}, | ||
margin = {0,0,0,0}, | margin = {0,0,0,0}, | ||
| Line 60: | Line 61: | ||
OnMouseUp = {ActionCommand}, | OnMouseUp = {ActionCommand}, | ||
} | } | ||
stateGrid:AddChild(button) | |||
end | end | ||
| Line 104: | Line 105: | ||
buildGrid:ClearChildren() | buildGrid:ClearChildren() | ||
orderGrid:ClearChildren() | orderGrid:ClearChildren() | ||
stateGrid:ClearChildren() | |||
local cmds = Spring.GetActiveCmdDescs() | local cmds = Spring.GetActiveCmdDescs() | ||
| Line 136: | Line 138: | ||
Spring.ForceLayoutUpdate() | Spring.ForceLayoutUpdate() | ||
if | if not WG.Chili then | ||
widgetHandler:RemoveWidget() | widgetHandler:RemoveWidget() | ||
return | return | ||
| Line 143: | Line 145: | ||
Chili = WG.Chili | Chili = WG.Chili | ||
stateGrid = Chili.Grid:New{ | |||
name = "stateGrid", | |||
x = '0%', | |||
y = '0%', | |||
width = '100%', | |||
height = '15%', | |||
rows = 3, | |||
columns = 3, | |||
padding = {0,0,0,0}, | |||
} | |||
orderGrid = Chili.Grid:New{ | orderGrid = Chili.Grid:New{ | ||
name = "orderGrid", | name = "orderGrid", | ||
x = '0%', | x = '0%', | ||
y = ' | y = '15%', | ||
width = '100%', | width = '100%', | ||
height = ' | height = '25%', | ||
rows = | rows = 5, | ||
columns = 3, | columns = 3, | ||
padding = {0,0,0,0}, | padding = {0,0,0,0}, | ||
| Line 172: | Line 185: | ||
height = '65%', | height = '65%', | ||
width = '30%', | width = '30%', | ||
children = {orderGrid, buildGrid}, | children = {stateGrid, orderGrid, buildGrid}, | ||
} | } | ||
| Line 179: | Line 192: | ||
function widget:CommandsChanged() | function widget:CommandsChanged() | ||
updateRequired = true -- the active cmd descs haven't changed yet; wait until the next widget:Update | |||
end | end | ||
function widget:Update() | function widget:Update() | ||
if | if updateRequired then | ||
ParseCmds() | ParseCmds() | ||
updateRequired = false | |||
end | end | ||
end | end | ||
| Line 193: | Line 206: | ||
Spring.ForceLayoutUpdate() | Spring.ForceLayoutUpdate() | ||
end | end | ||
</ | </syntaxhighlight> | ||
[[Category:Libraries]][[Category:Tutorial]] | [[Category:Libraries]][[Category:Tutorial]] | ||
Latest revision as of 01:20, 11 June 2016
This is a very basic menu that displays a button for each command currently available.
<syntaxhighlight lang="lua"> function widget:GetInfo()
return {
name = "Command Menu",
desc = "Shows a button for each order that can currently be given",
author = "Turnips",
date = "2016",
license = "GNU GPL v2 or later",
layer = math.pi,
enabled = true,
handler = true,
}
end
local Chili, window, orderGrid, buildGrid local updateRequired
-- buttons
local ignoreCMDs = {
timewait=true, deathwait=true, squadwait=true, gatherwait=true, loadonto=true,
}
function ActionCommand(self, x, y, button, mods)
local index = Spring.GetCmdDescIndex(self.cmdID)
if index then
local left, right = (button == 1), (button == 3)
local alt, ctrl, meta, shift = mods.alt, mods.ctrl, mods.meta, mods.shift
Spring.SetActiveCommand(index, button, left, right, alt, ctrl, meta, shift)
end
end
function OrderButton(cmd)
local button = Chili.Button:New{
name = "orderButton_" .. cmd.action,
cmdID = cmd.id,
caption = cmd.name,
padding = {0,0,0,0},
margin = {0,0,0,0},
OnMouseUp = {ActionCommand},
}
orderGrid:AddChild(button)
end
function StateButton(cmd)
local button = Chili.Button:New{
name = "stateButton_" .. cmd.action,
cmdID = cmd.id,
caption = cmd.params[cmd.params[1] + 2],
padding = {0,0,0,0},
margin = {0,0,0,0},
OnMouseUp = {ActionCommand},
}
stateGrid:AddChild(button)
end
function UnitButton(cmd)
local uDID = -cmd.id
local unitDef = UnitDefs[uDID]
local name = unitDef.name
local image = Chili.Image:New{
name = "unitImage_" .. name,
height = '100%',
width = '100%',
file = '#'..unitDef.id,
-- flip = false,
}
local button = Chili.Button:New{
name = "unitButton_" .. name,
cmdID = cmd.id,
caption = "",
children = {image},
padding = {0,0,0,0},
margin = {0,0,0,0},
OnMouseUp = {ActionCommand},
}
buildGrid:AddChild(button)
end
function ParseCmd(cmd)
if UnitDefNames[cmd.name] then
-- unit
UnitButton(cmd)
elseif #cmd.params > 1 then
-- state
StateButton(cmd)
else
-- order
OrderButton(cmd)
end
end
function ParseCmds()
-- clean out the grids & repopulate
buildGrid:ClearChildren()
orderGrid:ClearChildren()
stateGrid:ClearChildren()
local cmds = Spring.GetActiveCmdDescs()
local haveCmd = false
for _,cmd in ipairs(cmds) do
if cmd.name ~= and not (ignoreCMDs[cmd.name] or ignoreCMDs[cmd.action]) then
haveCmd = true
ParseCmd(cmd)
end
end
if haveCmd and window.hidden then window:Show() elseif not haveCmd and window.visible then window:Hide() end
end
-- callins
function LayoutHandler(xIcons, yIcons, cmdCount, commands)
-- its really better not to think about this bit
-- you may need to change widgetHandler to plain handler, dependent on your games lua internals
widgetHandler.commands = commands
widgetHandler.commands.n = cmdCount
widgetHandler:CommandsChanged()
local reParamsCmds = {}
local customCmds = {}
return "", xIcons, yIcons, {}, customCmds, {}, {}, {}, {}, reParamsCmds, {[1337]=9001}
end
function widget:Initialize()
widgetHandler:ConfigLayoutHandler(LayoutHandler)
Spring.ForceLayoutUpdate()
if not WG.Chili then
widgetHandler:RemoveWidget()
return
end
Chili = WG.Chili
stateGrid = Chili.Grid:New{
name = "stateGrid",
x = '0%',
y = '0%',
width = '100%',
height = '15%',
rows = 3,
columns = 3,
padding = {0,0,0,0},
}
orderGrid = Chili.Grid:New{
name = "orderGrid",
x = '0%',
y = '15%',
width = '100%',
height = '25%',
rows = 5,
columns = 3,
padding = {0,0,0,0},
}
buildGrid = Chili.Grid:New{
name = "buildGrid",
x = '0%',
y = '40%',
width = '100%',
height = '60%',
rows = 6,
columns = 5,
padding = {0,0,0,0},
}
window = Chili.Window:New{
name = "window",
parent = Chili.Screen0,
x = '0%',
y = '25%',
height = '65%',
width = '30%',
children = {stateGrid, orderGrid, buildGrid},
}
window:Hide()
end
function widget:CommandsChanged()
updateRequired = true -- the active cmd descs haven't changed yet; wait until the next widget:Update
end
function widget:Update()
if updateRequired then
ParseCmds()
updateRequired = false
end
end
function widget:Shutdown()
widgetHandler:ConfigLayoutHandler(nil) Spring.ForceLayoutUpdate()
end </syntaxhighlight>