Message
| The plugin below uses miniwindows to implement "action icons" (see screenshot in the next post).
[EDIT] For a newer version that also shows cooldown times, see http://www.gammon.com.au/forum/?id=9359
What this lets you do is have a configurable (by editing the plugin) bar of icons, which can be dragged around to anywhere in your output window.
Each button has an image on it which is loaded from a BMP or PNG file. If you click on a button, then the associated action is performed. You can do either or both of:
- Execute a command (this is sent to the command processor in MUSHclient). The default is, that the command would be sent to the MUD, unless it matches an alias. Variables in the form @name are expanded, so you can do things like: kick @target
- Execute a script function. This lets you do fancier things like doing something if your health is low, and something else if it is high.
The icon size is configurable (see ICON_SIZE below). I chose 32 x 32 as that gives a reasonable icon. The image files you specify are shrunk or expanded to that 32 x 32 box, so the best results would occur if your images are the same size as ICON_SIZE.
The bar automatically resizes to fit as many buttons as you specify.
You configure the plugin by editing (and adding to) the buttons table below (the part in bold). Hopefully it is reasonably self-explanatory. The icon files default to being in the same directory as the MUSHclient executable.
If you want to change that, change the line below:
if WindowLoadImage (win, n, GetInfo (66) .. v.icon) ~= error_code.eOK then
to something that suits you. For example, make it just:
if WindowLoadImage (win, n, v.icon) ~= error_code.eOK then
If you do that, then you have to specify the full pathname for each image file.
Or, you could put the icons in a sub-directory, like this:
if WindowLoadImage (win, n, GetInfo (66) .. "icons\\" .. v.icon) ~= error_code.eOK then
You drag the button bar around by mousing down in the gaps between the icons (or at the edge of the window) in such a way that you aren't clicking on an icon itself. Then the window can be moved around.
Below is the plugin.
Save between the lines as Icon_Bar.xml and use File menu -> Plugins to load that file as a plugin.
[EDIT] Newer version released below (further down this thread) on 29th March 2009. See. http://www.gammon.com.au/forum/?id=9280&reply=8#reply8
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>
<muclient>
<plugin
name="Icon_Bar"
author="Nick Gammon"
id="ede0920fc1173d5a03140f0e"
language="Lua"
purpose="Shows a bar of buttons you can click on to do things"
date_written="2009-02-26 09:00"
requires="4.40"
save_state="y"
version="1.0"
>
<description trim="y">
<![CDATA[
Install this plugin to show an button bar.
Click on an icon to execute a script or send a command to the MUD.
]]>
</description>
</plugin>
<!-- Script -->
<script>
<![CDATA[
-- table of buttons
--[[
Each button can have up to four entries:
icon - filename of the image to draw
tooltip - what to show if you hover the mouse over the button
send - what to send to the MUD
script - a script function to call
--]]
buttons = {
-- button 1
{
icon = "HealIcon.png",
tooltip = "Heal self",
send = "cast 'lesser heal' self",
},
-- button 2
{
icon = "SwordIcon.png",
tooltip = "Attack",
send = "kill @target",
},
-- button 3
{
icon = "SparkIcon2.png",
tooltip = "Cast Fireball",
send = "cast fireball",
script = function ()
ColourNote ('white', 'green', 'script test')
end,
},
--> add more buttons here
} -- end of buttons table
-- configuration
ICON_SIZE = 32
BACKGROUND_COLOUR = ColourNameToRGB "bisque"
BOX_COLOUR = ColourNameToRGB "royalblue"
BUTTON_EDGE = ColourNameToRGB "silver"
MOUSE_DOWN_COLOUR = ColourNameToRGB "darkorange"
FONT_NAME = "Fixedsys"
FONT_SIZE = 9
-- where to put the window
WINDOW_POSITION = 6 -- top right
OFFSET = 6 -- gap inside box
EDGE_WIDTH = 2 -- size of border stroke
--[[
Useful positions:
4 = top left
5 = center left-right at top
6 = top right
7 = on right, center top-bottom
8 = on right, at bottom
9 = center left-right at bottom
--]]
function mousedown (flags, hotspot_id)
if hotspot_id == "box" then
-- find where mouse is so we can adjust window relative to mouse
startx, starty = WindowInfo (win, 14), WindowInfo (win, 15)
-- find where window is in case we drag it offscreen
origx, origy = WindowInfo (win, 10), WindowInfo (win, 11)
return
end -- if the box, not a button
-- convert id to a number, for table lookup
local n = tonumber (hotspot_id)
-- draw the button border in another colour for visual feedback
WindowRectOp (win, 1,
frames [n].x1, frames [n].y1, frames [n].x2, frames [n].y2,
MOUSE_DOWN_COLOUR)
Redraw ()
end -- mousedown
function cancelmousedown (flags, hotspot_id)
local n = tonumber (hotspot_id)
-- draw the button border in original colour for visual feedback
WindowRectOp (win, 1,
frames [n].x1, frames [n].y1, frames [n].x2, frames [n].y2,
BUTTON_EDGE)
Redraw ()
end -- cancelmousedown
function mouseup (flags, hotspot_id)
-- fix border colour
cancelmousedown (flags, hotspot_id)
local n = tonumber (hotspot_id)
local button = buttons [n]
-- send to world if something specified
if type (button.send) == "string" and
button.send ~= "" then
local errors = {} -- no errors yet
-- function to do the replacements for string.gsub
local function replace_variables (s)
s = string.sub (s, 2) -- remove the @
replacement = GetPluginVariable ("", s) -- look up variable in global variables
if not replacement then -- not there, add to errors list
table.insert (errors, s)
return
end -- not there
return replacement -- return variable
end -- replace_variables
-- replace all variables starting with @
local command = string.gsub (button.send, "@%a[%w_]*", replace_variables)
-- report any errors
if #errors > 0 then
for k, v in ipairs (errors) do
ColourNote ("white", "red", "Variable '" .. v .. "' does not exist")
end -- for
return
end -- error in replacing
Execute (command)
end -- if
-- execute script if wanted
if type (button.script) == "function" then
button.script (n)
end -- if
end -- mouseup
function dragmove(flags, hotspot_id)
-- find where it is now
local posx, posy = WindowInfo (win, 17),
WindowInfo (win, 18)
-- move the window to the new location
WindowPosition(win, posx - startx, posy - starty, 0, 2);
-- change the mouse cursor shape appropriately
if posx < 0 or posx > GetInfo (281) or
posy < 0 or posy > GetInfo (280) then
check (SetCursor ( 11)) -- X cursor
else
check (SetCursor ( 1)) -- hand cursor
end -- if
end -- dragmove
function dragrelease(flags, hotspot_id)
local newx, newy = WindowInfo (win, 17), WindowInfo (win, 18)
-- don't let them drag it out of view
if newx < 0 or newx > GetInfo (281) or
newy < 0 or newy > GetInfo (280) then
-- put it back
WindowPosition(win, origx, origy, 0, 2);
end -- if out of bounds
end -- dragrelease
frames = {}
function OnPluginInstall ()
local x, y, mode, flags =
tonumber (GetVariable ("windowx")) or 0,
tonumber (GetVariable ("windowy")) or 0,
tonumber (GetVariable ("windowmode")) or WINDOW_POSITION, -- top right
tonumber (GetVariable ("windowflags")) or 0
win = GetPluginID () -- get a unique name
-- make the miniwindow
WindowCreate (win,
x, y, -- left, top (auto-positions)
(#buttons * (ICON_SIZE + OFFSET)) + OFFSET, -- width
ICON_SIZE + (OFFSET * 2), -- height
mode, -- position mode
flags, -- flags
BACKGROUND_COLOUR)
-- draw the buttons
for n, v in ipairs (buttons) do
if v.icon then
if WindowLoadImage (win, n, GetInfo (66) .. v.icon) ~= error_code.eOK then
ColourNote ("white", "red", "Could not load image '" .. GetInfo (66) .. v.icon .. "'")
end -- if
end -- if icon specified
-- where to draw the icon
local x1, y1 = (n - 1) * (ICON_SIZE + OFFSET) + OFFSET, OFFSET
local x2, y2 = n * (ICON_SIZE + OFFSET) + OFFSET, ICON_SIZE + OFFSET
-- draw the image
WindowDrawImage(win, n,
x1, y1, -- left, top
x2 - OFFSET, y2, -- right, bottom
2) -- mode - stretch or shrink
-- remember where to draw the frame, for mouse clicks
frames [n] = {
x1 = x1 - 1,
y1 = y1 - 1,
x2 = x2 - OFFSET + 1,
y2 = y2 + 1
}
-- draw the button border
WindowRectOp (win, 1,
frames [n].x1, frames [n].y1, frames [n].x2, frames [n].y2,
BUTTON_EDGE)
-- make a hotspot we can click on
WindowAddHotspot(win, n,
x1 - 1, y1 - 1, x2 - OFFSET + 1, y2 + 1, -- rectangle
"", -- mouseover
"", -- cancelmouseover
"mousedown",
"cancelmousedown",
"mouseup",
v.tooltip, -- tooltip text
1, 0) -- hand cursor
end -- for each button
-- draw the border of the whole box
WindowCircleOp (win, 2, 0, 0, 0, 0, BOX_COLOUR, 6, EDGE_WIDTH, 0x000000, 1)
-- make a hotspot
WindowAddHotspot(win, "box",
0, 0, 0, 0, -- whole window
"", -- MouseOver
"", -- CancelMouseOver
"mousedown",
"", -- CancelMouseDown
"", -- MouseUp
"Drag to move", -- tooltip text
1, 0) -- hand cursor
WindowDragHandler(win, "box", "dragmove", "dragrelease", 0)
if GetVariable ("enabled") == "false" then
ColourNote ("yellow", "", "Warning: Plugin " .. GetPluginName ().. " is currently disabled.")
check (EnablePlugin(GetPluginID (), false))
return
end -- they didn't enable us last time
-- ensure window visible
WindowShow (win, true)
end -- OnPluginInstall
-- hide window on removal
function OnPluginClose ()
WindowShow (win, false) -- hide it
end -- OnPluginClose
-- show window on enable
function OnPluginEnable ()
WindowShow (win, true) -- show it
end -- OnPluginEnable
-- hide window on disable
function OnPluginDisable ()
WindowShow (win, false) -- hide it
end -- OnPluginDisable
function OnPluginSaveState ()
SetVariable ("enabled", tostring (GetPluginInfo (GetPluginID (), 17)))
SetVariable ("windowx", tostring (WindowInfo (win, 10)))
SetVariable ("windowy", tostring (WindowInfo (win, 11)))
SetVariable ("windowmode", tostring (WindowInfo (win, 7)))
SetVariable ("windowflags", tostring (WindowInfo (win, 8)))
end -- OnPluginSaveState
]]>
</script>
</muclient>
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|