Message
| Below is an improved version of the original one on page 1.
Improvements are:
- Neatened up the code, to make it easier to modify in future (eg. the width of the word "Mana" and "Move" are now calculated, to get the offset of the bar from the edge).
- Shows mouse-over information (like the example above)
- Incorporates the movewindow module for dragging rather than having code inline, which makes it all smaller.
- Hides the window if you disable the plugin, and shows it again if you enable it.
|
To save and install the Health_Bar_Miniwindow plugin do this:
- Copy between the lines below (to the Clipboard)
- Open a text editor (such as Notepad) and paste the plugin into it
- Save to disk on your PC, preferably in your plugins directory, as Health_Bar_Miniwindow.xml
- Go to the MUSHclient File menu -> Plugins
- Click "Add"
- Choose the file Health_Bar_Miniwindow.xml (which you just saved in step 3) as a plugin
- Click "Close"
|
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE muclient>
<muclient>
<plugin
name="Health_Bar_Miniwindow"
author="Nick Gammon"
id="48062dcd6b968c590df50f32"
language="Lua"
purpose="Shows stats in a mini window"
date_written="2010-02-14 09:00"
requires="4.40"
version="2.0"
save_state="y"
>
<description trim="y">
<![CDATA[
Install this plugin to show an info bar with HP, Mana,
and Movement points shown as a bar.
The window can be dragged to a new location with the mouse.
For Smaug your prompt needs to be set like this:
prompt <%h/%H hp %m/%M m %v/%V mv %x/%X xp>
fprompt <%h/%H hp %m/%M m %v/%V mv %x/%X xp>
]]>
</description>
</plugin>
<!-- Triggers -->
<triggers>
<trigger
enabled="y"
match="^\<(\d+)\s*\/(\d+)\s*hp (\d+)\s*\/(\d+)\s*m (\d+)\s*\/(\d+)\s*mv "
regexp="y"
script="do_prompt"
sequence="100"
>
</trigger>
</triggers>
<!-- Script -->
<script>
<![CDATA[
GAUGE_HEIGHT = 15
WINDOW_WIDTH = 300
WINDOW_HEIGHT = 65
NUMBER_OF_TICKS = 5
BACKGROUND_COLOUR = ColourNameToRGB "rosybrown"
FONT_COLOUR = ColourNameToRGB "darkred"
BORDER_COLOUR = ColourNameToRGB "#553333"
function DoGauge (sPrompt, current, max, Colour)
if max <= 0 then
return
end -- no divide by zero
-- fraction in range 0 to 1
local Fraction = math.min (math.max (current / max, 0), 1)
local width = WindowTextWidth (win, font_id, sPrompt)
WindowText (win, font_id, sPrompt, gauge_left - width, vertical, 0, 0, FONT_COLOUR)
WindowRectOp (win, 2, gauge_left, vertical, WINDOW_WIDTH - 5, vertical + GAUGE_HEIGHT,
BACKGROUND_COLOUR) -- fill entire box
local gauge_width = (WINDOW_WIDTH - gauge_left - 5) * Fraction
-- box size must be > 0 or WindowGradient fills the whole thing
if math.floor (gauge_width) > 0 then
-- top half
WindowGradient (win, gauge_left, vertical, gauge_left + gauge_width, vertical + GAUGE_HEIGHT / 2,
0x000000,
Colour, 2)
-- bottom half
WindowGradient (win, gauge_left, vertical + GAUGE_HEIGHT / 2,
gauge_left + gauge_width, vertical + GAUGE_HEIGHT,
Colour,
0x000000,
2)
end -- non-zero
-- show ticks
local ticks_at = (WINDOW_WIDTH - gauge_left - 5) / (NUMBER_OF_TICKS + 1)
-- ticks
for i = 1, NUMBER_OF_TICKS do
WindowLine (win, gauge_left + (i * ticks_at), vertical,
gauge_left + (i * ticks_at), vertical + GAUGE_HEIGHT, ColourNameToRGB ("silver"), 0, 1)
end -- for
-- draw a box around it
WindowRectOp (win, 1, gauge_left, vertical, WINDOW_WIDTH - 5, vertical + GAUGE_HEIGHT,
ColourNameToRGB ("lightgrey")) -- frame entire box
-- mouse-over information: add hotspot if not there
if not WindowHotspotInfo(win, sPrompt, 1) then
WindowAddHotspot (win, sPrompt, gauge_left, vertical, WINDOW_WIDTH - 5, vertical + font_height,
"", "", "", "", "", "", 0, 0)
end -- if
-- store numeric values in case they mouse over it
WindowHotspotTooltip(win, sPrompt, string.format ("%s\t%i / %i (%i%%)",
sPrompt, current, max, Fraction * 100) )
vertical = vertical + font_height + 3
end -- function DoGauge
function do_prompt (name, line, wildcards)
hp, max_hp = tonumber (wildcards [1]), tonumber (wildcards [2])
mana, max_mana = tonumber (wildcards [3]), tonumber (wildcards [4])
move, max_move = tonumber (wildcards [5]), tonumber (wildcards [6])
-- fill entire box to clear it
check (WindowRectOp (win, 2, 0, 0, 0, 0, BACKGROUND_COLOUR)) -- fill entire box
-- Edge around box rectangle
check (WindowCircleOp (win, 3, 0, 0, 0, 0, BORDER_COLOUR, 0, 2, 0, 1))
vertical = 6 -- pixel to start at
DoGauge ("HP: ", hp , max_hp, ColourNameToRGB "darkgreen")
DoGauge ("Mana: ", mana, max_mana, ColourNameToRGB "mediumblue")
DoGauge ("Move: ", move, max_move, ColourNameToRGB "gold")
WindowShow (win, true)
end -- function do_prompt
function OnPluginInstall ()
win = GetPluginID ()
font_id = "fn"
require "movewindow" -- load the movewindow.lua module
-- install the window movement handler, get back the window position
windowinfo = movewindow.install (win, 7) -- default to 7 (on right, center top/bottom)
font_name = "Fixedsys" -- the font
-- make miniwindow so I can grab the font info
WindowCreate (win,
windowinfo.window_left,
windowinfo.window_top,
WINDOW_WIDTH,
WINDOW_HEIGHT,
windowinfo.window_mode,
windowinfo.window_flags,
BACKGROUND_COLOUR)
-- add the drag handler so they can move the window around
movewindow.add_drag_handler (win, 0, 0, 0, 0)
WindowFont (win, font_id, font_name, 9)
font_height = WindowFontInfo (win, font_id, 1) -- height
-- work out how far in to start the gauge
gauge_left = WindowTextWidth (win, font_id, "HP: ")
gauge_left = math.max (gauge_left, WindowTextWidth (win, font_id, "Mana: "))
gauge_left = math.max (gauge_left, WindowTextWidth (win, font_id, "Move: "))
gauge_left = gauge_left + 5 -- allow gap from edge
if GetVariable ("enabled") == "false" then
ColourNote ("yellow", "", "Warning: Plugin " .. GetPluginName ().. " is currently disabled.")
check (EnablePlugin(GetPluginID (), false))
end -- they didn't enable us last time
end -- OnPluginInstall
function OnPluginDisable ()
WindowShow (win, false)
end -- OnPluginDisable
function OnPluginEnable ()
WindowShow (win, true)
-- draw gauge again if possible
if hp and max_hp and mana and max_mana and move and max_move then
do_prompt ("", "", { hp, max_hp, mana, max_mana, move, max_move } )
end -- if know hp, endurance and guile
end -- OnPluginEnable
function OnPluginSaveState ()
-- save window current location for next time
movewindow.save_state (win)
SetVariable ("enabled", tostring (GetPluginInfo (GetPluginID (), 17)))
end -- OnPluginSaveState
]]>
</script>
</muclient>
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|