Posted by
| Nick Gammon
Australia (22,990 posts) bio
Forum Administrator |
Message
| It's not that hard, I think you are confusing yourself.
You basically need two functions. One that the trigger calls, that needs to take the wildcards and work out the values for the bars.
It can also make the window and draw the background, etc.
The second function (DoGauge) draws the actual bars (so it is called three times). That draws the text of what the bar represents, followed by the box which is the bar itself. If you put this in your script file it should work:
-- where to start each gauge
GAUGE_LEFT = 55
-- height of gauge
GAUGE_HEIGHT = 15
-- width of health bar window
WINDOW_WIDTH = 200
-- height of it
WINDOW_HEIGHT = 65
-- how many ticks to draw
NUMBER_OF_TICKS = 5
-- assorted colours
BACKGROUND_COLOUR = ColourNameToRGB "rosybrown"
FONT_COLOUR = ColourNameToRGB "darkred"
BORDER_COLOUR = ColourNameToRGB "darkorchid"
FILL_COLOUR = ColourNameToRGB "lightgrey"
GAUGE_BORDER = ColourNameToRGB "dimgray"
function DoGauge (sPrompt, Percent, Colour)
local Fraction = tonumber (Percent)
if Fraction > 1 then Fraction = 1 end
if Fraction < 0 then Fraction = 0 end
local width = WindowTextWidth (win, font_id, sPrompt)
-- prompt on left (eg. "HP:")
WindowText (win, font_id, sPrompt,
GAUGE_LEFT - width, vertical, 0, 0, FONT_COLOUR)
-- fill entire box with "unused" colour
WindowRectOp (win, 2, GAUGE_LEFT, vertical, WINDOW_WIDTH - 5, vertical + GAUGE_HEIGHT,
FILL_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
check (WindowRectOp (win, 1, GAUGE_LEFT, vertical, WINDOW_WIDTH - 5, vertical + GAUGE_HEIGHT,
GAUGE_BORDER)) -- frame entire box
vertical = vertical + font_height + 3
end -- function DoGauge
function DoNextSimpleBar (name, line, wildcards, styles)
win = GetPluginID () .. "_health_bar"
font_id = "fn"
font_name = "Fixedsys" -- the actual font
-- make miniwindow so I can grab the font info
check (WindowCreate (win,
0, 0, WINDOW_WIDTH, WINDOW_HEIGHT,
miniwin.pos_top_left,
0,
BACKGROUND_COLOUR) )
-- add the font to the window
check (WindowFont (win, font_id, font_name, 9, false, false, false, false, 0, 0)) -- normal
font_height = WindowFontInfo (win, font_id, 1) -- height
-- copy hp, mana, movements points from 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))
-- calculate what percentage we are of each one
local hp_percent = hp / max_hp
local mana_percent = mana / max_mana
local move_percent = move / max_move
-- where to start the first gauge
vertical = 6
-- draw the gauges
DoGauge ("HP: ", hp_percent, ColourNameToRGB "darkgreen")
DoGauge ("Mana: ", mana_percent, ColourNameToRGB "mediumblue")
DoGauge ("Move: ", move_percent, ColourNameToRGB "gold")
WindowShow (win, true)
end -- DoNextSimpleBar
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | top |
|