vitalsbox.lua

--- Gauges that sit at the bottom and do gauge things.
--@classmod VitalsBox

VitalsBox = {}





--------------------------------------------------------------------------------
--- Initialisation.
-- For creating and Initialising VitalsBox object.
-- @section initialisation
--------------------------------------------------------------------------------

----------------------------------------
--- Creates a new instance of the VitalsBox class.
-- @tparam string _name Instance name, should be identical to variable instance is assigned to.
-- @tparam number _elementX X coordinate of VitalsBox, in screen coordinates.
-- @tparam number _elementY Y coordinate of VitalsBox, in screen coordinates.
-- @treturn table A new instance of the VitalsBox class.
-- @usage vitals = VitalsBox:new("vitals",89,916)
function VitalsBox:new(_name, _elementX, _elementY)

    cloudDir = cloudDir or [[D:\Achaea\]]
    self.__index = VitalsBox
    local self = setmetatable({}, VitalsBox)

    --===============================
    -- BASIC ATTRIBUTES

    self._instanceName = _name
    self._lowHPflashing = false

    if self._updateHandler then killAnonymousEventHandler(self._updateHandler) self._updateHandler = false end
    self._updateHandler = self._updateHandler or registerAnonymousEventHandler("svo done with prompt", self._instanceName..":update")

    --===============================
    -- MAIN CONTAINER

    self._container = Geyser.Label:new({ name = self._instanceName.."._container", x = _elementX, y = _elementY, width = 942, height = 86 })

    --===============================
    -- LABELS

    self._labelBackground        = Geyser.Label:new({ name = self._instanceName.."._labelBackground",        x = 0,   y = 0,  width  = 942, height = 86 }, self._container )
    self._labelBalance           = Geyser.Label:new({ name = self._instanceName.."._labelBalance",           x = 445, y = 6,  width  = 52,  height = 34, color = "#007700" }, self._container )
    self._labelBezel             = Geyser.Label:new({ name = self._instanceName.."._labelBezel",             x = 0,   y = 0,  width  = 942, height = 86 }, self._container )
    self._labelEquilibrium       = Geyser.Label:new({ name = self._instanceName.."._labelEquilibrium",       x = 445, y = 46, width  = 52,  height = 34, color = "#007700" }, self._container )
    self._labelLowHPWarningLeft  = Geyser.Label:new({ name = self._instanceName.."._labelLowHPWarningLeft",  x = 0,   y = 6,  width  = 27,  height = 74 }, self._container )
    self._labelLowHPWarningRight = Geyser.Label:new({ name = self._instanceName.."._labelLowHPWarningRight", x = 915, y = 5,  width  = 27,  height = 74 }, self._container )
    self._labelRage              = Geyser.Label:new({ name = self._instanceName.."._labelRage",              x = 342, y = 6,  width  = 97,  height = 34 }, self._container )
    self._labelTargetName        = Geyser.Label:new({ name = self._instanceName.."._labelTargetName",        x = 709, y = 6,  width  = 200, height = 34 }, self._container )
    self._labelTicker            = Geyser.Label:new({ name = self._instanceName.."._labelTicker",            x = 342, y = 46, width  = 97,  height = 34 }, self._container )

    --===============================
    -- LABEL STYLESHEETS AND IMAGES

    self._labelBackground:setStyleSheet(        [[background-color: #0000FF;]]       )
    self._labelBalance:setStyleSheet(           gui.css.balanceGreen:getCSS()        )
    self._labelBezel:setStyleSheet(             [[background-color: #00000000;]]     )
    self._labelEquilibrium:setStyleSheet(       gui.css.balanceGreen:getCSS()        )
    self._labelLowHPWarningLeft:setStyleSheet(  [[background-color: #FF0000;]]       )
    self._labelLowHPWarningRight:setStyleSheet( [[background-color: #FF0000;]]       )
    self._labelRage:setStyleSheet(              [[background-color: #00000000;]]     )
    self._labelTargetName:setStyleSheet(        [[background-color: #000000;]]       )
    self._labelTicker:setStyleSheet(            [[background-color: #000000;]]       )

    self._labelBezel:setBackgroundImage( cloudDir..[[GUI\images\vitalsBoxBezel.png]] )

    --===============================
    -- LABEL ECHOES

    self._labelBalance:echo(       "B","white","c20"                )
    self._labelEquilibrium:echo(   "E","white","c20"                )
    --self.lowHPWarning:echo(      "FLEE","yellow","b16"            )
    --self._lowHPWarningLeft:echo( "FLEE","yellow","b16"            )
    self._labelRage:echo(          " Rage: --","white","b10"   )
    self._labelTargetName:echo(    " tarName","white","b12"    )
    self._labelTicker:echo(        " Gold: ----","white","b10" )

    --===============================
    -- GAUGES

    self._gaugeEndurance         = Geyser.Gauge:new({ name = self._instanceName.."._gaugeEndurance",         x = 239, y = 6,  height = 34,  width = 97   }, self._container )
    self._gaugeHealth            = Geyser.Gauge:new({ name = self._instanceName.."._gaugeHealth",            x = 33,  y = 6,  height = 34,  width = 200  }, self._container )
    self._gaugeMana              = Geyser.Gauge:new({ name = self._instanceName.."._gaugeMana",              x = 33,  y = 46, height = 34,  width = 200  }, self._container )
    self._gaugeRage              = Geyser.Gauge:new({ name = self._instanceName.."._gaugeRage",              x = 342, y = 6,  height = 34,  width = 97   }, self._container )
    self._gaugeTargetHP          = Geyser.Gauge:new({ name = self._instanceName.."._targetHP",               x = 709, y = 46, height = 34,  width = 200  }, self._container )
    self._gaugeWillpower         = Geyser.Gauge:new({ name = self._instanceName.."._gaugeWillpower",         x = 239, y = 46, height = 34,  width = 97   }, self._container )

    --===============================
    -- GAUGE STYLESHEETS

    self._gaugeEndurance.back:setStyleSheet(   gui.css.defaultGaugeBack:getCSS()    )
    self._gaugeEndurance.front:setStyleSheet(  gui.css.defaultGaugeGreen:getCSS()   )
    self._gaugeHealth.back:setStyleSheet(      gui.css.defaultGaugeBack:getCSS()    )
    self._gaugeHealth.front:setStyleSheet(     gui.css.defaultGaugeGreen:getCSS()   )
    self._gaugeMana.back:setStyleSheet(        gui.css.defaultGaugeBack:getCSS()    )
    self._gaugeMana.front:setStyleSheet(       gui.css.defaultGaugeGreen:getCSS()   )
    self._gaugeRage.back:setStyleSheet(        gui.css.defaultGaugeBack:getCSS()    )
    self._gaugeRage.front:setStyleSheet(       gui.css.defaultGaugeOrange:getCSS()  )
    self._gaugeTargetHP.back:setStyleSheet(    gui.css.defaultGaugeBack:getCSS()    )
    self._gaugeTargetHP.front:setStyleSheet(   gui.css.defaultGaugeOrange:getCSS()  )
    self._gaugeWillpower.back:setStyleSheet(   gui.css.defaultGaugeBack:getCSS()    )
    self._gaugeWillpower.front:setStyleSheet(  gui.css.defaultGaugeGreen:getCSS()   )

    --===============================
    -- GAUGE ECHOES AND SETVALUES

    self._gaugeEndurance:setValue(50,100)
    self._gaugeHealth:setValue(50,100)
    self._gaugeMana:setValue(50,100)
    self._gaugeRage:setValue(50,100)
    self._gaugeTargetHP:setValue(50,100)
    self._gaugeWillpower:setValue(50,100)

    --self._gaugeRage.back:echo(     " Rage: 0",       "white", "b10" )
    --self._gaugeRage.front:echo(    " Rage: 0",       "white", "b10" )
    self._gaugeEndurance.front:echo( " EN: ---%",      "white", "b10" )
    self._gaugeEndurance.back:echo(  " EN: ---%",      "white", "b10" )
    self._gaugeHealth.front:echo(    " HP: ----/----", "white", "b10" )
    self._gaugeHealth.back:echo(     " HP: ----/----", "white", "b10" )
    self._gaugeMana.front:echo(      " MP: ----/----", "white", "b10" )
    self._gaugeMana.back:echo(       " MP: ----/----", "white", "b10" )
    self._gaugeTargetHP.front:echo(  " ---%",          "white", "b12" )
    self._gaugeTargetHP.back:echo(   " ---%",          "white", "b12" )
    self._gaugeWillpower.front:echo( " WP: ---%",      "white", "b10" )
    self._gaugeWillpower.back:echo(  " WP: ---%",      "white", "b10" )

    self._container:raiseAll()

    return self

end





--------------------------------------------------------------------------------
--- Gauge methods.
-- @section Gauge
--------------------------------------------------------------------------------

----------------------------------------
--- Sets endurance gauge and text.
-- @tparam int _currentEnd Current endurance
-- @tparam int _maxEnd Maximum endurance
function VitalsBox:setEndurance(_currentEnd, _maxEnd)
    self._gaugeEndurance:setValue(_currentEnd, _maxEnd)
    self._gaugeEndurance.front:echo(" EN: " .. round((_currentEnd/_maxEnd)*100).."%", "white", "b10" )
    self._gaugeEndurance.back:echo(" EN: " .. round((_currentEnd/_maxEnd)*100).."%", "white", "b10" )
end

----------------------------------------
--- Sets HP gauge and text.
-- @tparam string _currentHP Current HP
-- @tparam string _maxHP Maximum HP
-- @usage vitals:setHealth(3723, 5200)
function VitalsBox:setHealth(_currentHP, _maxHP)
    self._gaugeHealth:setValue(_currentHP, _maxHP)
    self._gaugeHealth.front:echo(" HP: " .. _currentHP .. " / " .. _maxHP, "white", "b10" )
    self._gaugeHealth.back:echo(" HP: " .. _currentHP .. " / " .. _maxHP, "white", "b10" )
end

----------------------------------------
--- Sets MP gauge and text.
-- @tparam string _currentMP Current MP
-- @tparam string _maxMP Maximum MP
-- @usage vitals:setMana(4158, 4700)
function VitalsBox:setMana(_currentMP, _maxMP)
    self._gaugeMana:setValue(_currentMP, _maxMP)
    self._gaugeMana.front:echo(" MP: " .. _currentMP .. " / " .. _maxMP, "white", "b10" )
    self._gaugeMana.back:echo(" MP: " .. _currentMP .. " / " .. _maxMP, "white", "b10" )
end

----------------------------------------
--- Sets target HP gauge and readout.
-- @tparam int _value Target HP (percent)
-- @usage vitals:setTargetHealth(57)
function VitalsBox:setTargetHealth(_value)
    self._gaugeTargetHP:setValue(_value,100)
    self._gaugeTargetHP.front:echo(" " .. _value .. "%","white","b12")
    self._gaugeTargetHP.back:echo(" " .. _value .. "%","white","b12")
end

----------------------------------------
--- Sets willpower gauge and text.
-- @tparam int _currentWP Current willpower
-- @tparam int _maxWP Maximum willpower
function VitalsBox:setWillpower(_currentWP, _maxWP)
    self._gaugeWillpower:setValue(_currentWP, _maxWP)
    self._gaugeWillpower.front:echo(" WP: " .. round((_currentWP/_maxWP)*100).."%", "white", "b10" )
    self._gaugeWillpower.back:echo(" WP: " .. round((_currentWP/_maxWP)*100).."%", "white", "b10" )
end





--------------------------------------------------------------------------------
--- Label methods
-- @section labels
--------------------------------------------------------------------------------

----------------------------------------
--- Sets balance indicator.
-- @tparam Boolean _labelBalance True if on balance.
-- @usage vitals:setBalance(true)
function VitalsBox:setBalance(_labelBalance)
    if inShipGUI then
        if _labelBalance then
            self._labelBalance:setStyleSheet(gui.css.ship.balanceGreen)
        else
            self._labelBalance:setStyleSheet(gui.css.ship.balanceRed)
        end
    else
        if _labelBalance then
            self._labelBalance:setStyleSheet(gui.css.balanceGreen:getCSS())
        else
            self._labelBalance:setStyleSheet(gui.css.balanceRed:getCSS())
        end
    end
end

----------------------------------------
--- Sets equilibrium indicator.
-- @tparam boolean _labelBalance True if on balance.
-- @usage vitals:setEquilibrium(true)
function VitalsBox:setEquilibrium(_labelBalance)
    if inShipGUI then
        if _labelBalance then
            self._labelEquilibrium:setStyleSheet(gui.css.ship.balanceGreen)
        else
            self._labelEquilibrium:setStyleSheet(gui.css.ship.balanceRed)
        end
    else
        if _labelBalance then
            self._labelEquilibrium:setStyleSheet(gui.css.balanceGreen:getCSS())
        else
            self._labelEquilibrium:setStyleSheet(gui.css.balanceRed:getCSS())
        end
    end
end

----------------------------------------
--- Sets low HP warning indicator.
-- @tparam boolean _value True, to enable low HP warnings.
-- @usage vitals:setLowHP(true)
function VitalsBox:setLowHP(_value)

    if inShipGUI then return end
    if _value and svo.stats.hp >= 0 then

        if _value and not soundCooldown then
            playSoundFile(cloudDir..[[Sounds\warning.wav]])
            soundCooldown = true
            tempTimer(1.0,[[soundCooldown = false]])
        end

        if not self._lowHPflashing then
            self._lowHPflashing = true
            self._labelLowHPWarningLeft:show()
            self._labelLowHPWarningRight:show()
            if exists("Temporary HP flasher "..self._instanceName, "timer") < 1 then
                permTimer("Temporary HP flasher "..self._instanceName, "", 0.25, [[
                    if  ]]..self_.instanceName..[[._labelLowHPWarningLeft.hidden then
                        ]]..self_.instanceName..[[._labelLowHPWarningLeft:show()
                        ]]..self_.instanceName..[[._labelLowHPWarningRight:show()
                    else
                        ]]..self_.instanceName..[[._labelLowHPWarningLeft:hide()
                        ]]..self_.instanceName..[[._labelLowHPWarningRight:hide()
                    end
                ]])
            end
            enableTimer("Temporary HP flasher")
        end

    elseif _value == false then

        self._lowHPflashing = false
        self._labelLowHPWarningLeft:hide()
        self._labelLowHPWarningRight:hide()
        disableTimer("Temporary HP flasher"..self._instanceName)

    end
end

----------------------------------------
--- Sets target name to be displayed.
-- @tparam string _name Set target name
-- @usage vitals:setTargetName("orc48192")
function VitalsBox:setTargetName(_name)
    self._labelTargetName:echo("&nbsp;" .. _name)
end




--------------------------------------------------------------------------------
--- Battlerage
-- @section battlerage
--------------------------------------------------------------------------------

----------------------------------------
--- Sets rage text.
-- @tparam int _currentRage Current rage
-- ! And it doesn't work
function VitalsBox:setRage(_currentRage) -- Set rage display
    --self._labelRage:echo("&nbsp;Rage: " .. _currentRage,"white","b10")
end





--------------------------------------------------------------------------------
--- Misc methods
-- @section misc
--------------------------------------------------------------------------------

----------------------------------------
--- Shows or hides target information.
-- @tparam bool _show True to show self._targetHP, false to hide.
-- @usage vitals:showTarget(true)
function VitalsBox:showTargetHP(_show)
    if _show then
        self._gaugeTargetHP:show()
    else
        self._gaugeTargetHP:hide()
    end
end

----------------------------------------
--- Shows or hides the whole object on the screen.
-- Just a shortcut for things like getting into my ship UI.
-- @tparam bool _visible True to show object, false to hide.
-- @usage vitals:showTarget(true)
function VitalsBox:setVisible(_visible)
    if _visible then
        self._container:show()
    else
        self._container:hide()
    end
end

----------------------------------------
--- It updates things.
function VitalsBox:update()

    if not svo.stats then return end
    if not loginComplete then return end

    self:setHealth(   svo.stats.currenthealth, svo.stats.maxhealth        )
    self:setMana(     svo.stats.currentmana, svo.stats.maxmana            )
    self:setEndurance( svo.stats.currentendurance, svo.stats.maxendurance )
    self:setWillpower( svo.stats.currentwillpower, svo.stats.maxwillpower )

    self:setRage(svo.stats.battlerage)
    self._labelTicker:echo("&nbsp;GP: "..gmcp.Char.Status.gold,"white","b10")

    self:setBalance(svo.bals.balance)
    self:setEquilibrium(svo.bals.equilibrium)

    local _isLowHP = not (svo.stats.hp >= 40) or (svo.stats.hp == 0)
    self:setLowHP(_isLowHP)

    self:showTargetHP(svo.me.gametargethp)
    self:setTargetName(svo.me.gametarget or "")
    self:setTargetHealth(svo.me.gametargethp or 0 )

    -- DON'T LEAVE THIS HERE -- Wait why though?
    if jcs.target.autoAttack then
        self._labelTargetName:setStyleSheet([[background-color: #777700;]])
    elseif jcs.autoStrike then
        self._labelTargetName:setStyleSheet([[background-color: #770000;]])
    else
        self._labelTargetName:setStyleSheet([[background-color: #000000;]])
    end
    -- DON'T LEAVE THIS HERE

end
generated by LDoc 1.4.3 Last updated 2021-01-24 20:08:44