view SkeenMelee.lua @ 0:6e44a9e52393 v1.0.6

initial update
author Emil Madsen <skeen@cs.au.dk>
date Sun, 26 Jun 2011 17:20:50 +0200
parents
children f8b96a717bd1
line wrap: on
line source
--
-- SkeenMelee
-- 

-- Local table (used to store functions, ect)
local SkeenMelee = {}

SkeenMelee.eventHandler = CreateFrame("Frame")
SkeenMelee.eventHandler:SetScript("OnEvent", function(this, event, ...) SkeenMelee.events[event](...) end)
--Events to listen to
SkeenMelee.eventHandler:RegisterEvent("PLAYER_TARGET_CHANGED")
SkeenMelee.eventHandler:RegisterEvent("ADDON_LOADED")   
--EventHandler Functions
--by our eventHandler frame.
SkeenMelee.events = {}
--Player logged in
function SkeenMelee.events.ADDON_LOADED(addon)
    if (addon ~= "SkeenMelee") then
        return
    end
    
    SkeenMelee:PrepareDatabase()
    SkeenMelee:CreateGUI()
    SkeenMelee:UpdateDragability()
    SkeenMelee:CreateOptionFrame()
end

-- Prepare the Normal database (settings database)
-- This is the maintained database, and isn't wiped on each login, like the active one is
function SkeenMelee:PrepareDatabase()

    -- If there doesn't exist a database currently
    if not SkeenMeleeDatabase then
        SkeenMeleeDatabase = {} -- fresh start
        SkeenMeleeDatabase.update_interval = 0.1 -- Fill it with the default value of 0.1 seconds per interval
        SkeenMeleeDatabase.scale = 1 --100% size
        SkeenMeleeDatabase.locked = false --Not locked by default
        SkeenMeleeDatabase.x = 100 --TODO: Make some better values I guess?
        SkeenMeleeDatabase.y = 100 --TODO: Make some better values I guess?
    end
end

-- Functions Section
function SkeenMelee_OnUpdate(self, elapsed)
    self.TimeSinceLastUpdate = self.TimeSinceLastUpdate + elapsed; 	

    if (self.TimeSinceLastUpdate > SkeenMeleeDatabase.update_interval) then
        SkeenMelee:Update()
        self.TimeSinceLastUpdate = 0;
    end
end

function SkeenMelee.events.PLAYER_TARGET_CHANGED(...)
    SkeenMelee:Update()
end

function SkeenMelee:Update()
    local is_in_melee_range = nil
    local unit = "target"
    -- Test class and find a spell accordingly
    local _, class = UnitClass("player");
    local meleespell

    -- Choose a melee spell for each class (if any)
    if (class == "DEATH KNIGHT") then
        meleespell = "Blood Strike"
    elseif (class == "DRUID") then
        meleespell = "Claw"   
    elseif (class == "HUNTER") then
        meleespell = "Raptor Strike"
    elseif (class == "PALADIN") then
        meleespell = "Crusader Strike"
    elseif (class == "ROGUE") then
        meleespell = "Sinister Strike"
    elseif (class == "SHAMAN") then
        meleespell = "Primal Strike"
    elseif (class == "WARRIOR") then
        meleespell = "Strike"
    else --mage, priest, warlock
        SkeenMelee:HideFrame()
    end

    if UnitExists(unit) and UnitIsVisible(unit) then
        is_in_melee_range = IsSpellInRange(meleespell, unit)
    end
    if (is_in_melee_range==1) then --Is in melee range
        SkeenMelee.infoString:SetText("Melee")
        SkeenMelee:ShowFrame()
    elseif (is_in_melee_range==0) then --Is not in melee range
        SkeenMelee.infoString:SetText("|cffff0000NOT MELEE|r")
        SkeenMelee:ShowFrame()
    else --Not even a melee target 
        SkeenMelee:HideFrame()
    end
end

function SkeenMelee:CreateGUI()

    local displayFrame = CreateFrame("Frame", "SkeenMeleeDisplayFrame", UIParent)
    displayFrame:SetFrameStrata("BACKGROUND")
    displayFrame:SetWidth(100)
    displayFrame:SetHeight(30)
    displayFrame:SetBackdrop({bgFile = "Interface\\Tooltips\\UI-Tooltip-Background", tile = true, tileSize = 32,})
    displayFrame:SetBackdropColor(0, 0, 0, .4)
    displayFrame:EnableMouse(true)
    displayFrame:SetMovable(true)
    displayFrame:SetClampedToScreen(true)
    displayFrame:SetScript("OnMouseDown", function(self) self:StartMoving() end)
    displayFrame:SetScript("OnMouseUp", function(self) self:StopMovingOrSizing() end)
    displayFrame:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
    displayFrame:SetPoint("CENTER",-200,-200)

    local infoString = displayFrame:CreateFontString("SkeenMelee_infoString","OVERLAY","GameFontNormal")
    infoString:SetText("Unknown")
    infoString:SetPoint("CENTER", 0, 0)

    SkeenMelee.infoString = infoString
    SkeenMelee.displayFrame = displayFrame

    displayFrame:SetScale(SkeenMeleeDatabase.scale)
end

-- The optionsframe in interface
function SkeenMelee:CreateOptionFrame()

    local panel = CreateFrame("FRAME", "SkeenMeleeOptions", UIParent);
    panel.name = "SkeenMelee";

    local fstring1 = panel:CreateFontString("SkeenMeleeOptions_string1","OVERLAY","GameFontNormal")
    fstring1:SetText("Lock")
    fstring1:SetPoint("TOPLEFT", 10, -10)

    local checkbox1 = CreateFrame("CheckButton", "$parent_cb1", panel, "OptionsCheckButtonTemplate")
    checkbox1:SetWidth(18)
    checkbox1:SetHeight(18)
    checkbox1:SetScript("OnClick", function() SkeenMelee:ToggleLocked() end)
    checkbox1:SetPoint("TOPRIGHT", -10, -10)
    checkbox1:SetChecked(SkeenMelee:GetLocked())

    local fstring2 = panel:CreateFontString("SkeenMeleeOptions_string2","OVERLAY","GameFontNormal")
    fstring2:SetText("GUI Scale")
    fstring2:SetPoint("TOPLEFT", 10, -40)

    local slider2 = CreateFrame("Slider", "$parent_sl2", panel, "OptionsSliderTemplate")
    slider2:SetMinMaxValues(.5, 1.5)
    slider2:SetValue(SkeenMelee:GetScale())
    slider2:SetValueStep(.05)
    slider2:SetScript("OnValueChanged", function(self) SkeenMelee:SetScale(self:GetValue()); getglobal(self:GetName() .. "Text"):SetText(string.format("%.0f%%", self:GetValue()*100))  end)
    getglobal(slider2:GetName() .. "Low"):SetText("50%")
    getglobal(slider2:GetName() .. "High"):SetText("150%")
    getglobal(slider2:GetName() .. "Text"):SetText(string.format("%.0f%%", SkeenMelee:GetScale()*100))
    slider2:SetPoint("TOPRIGHT", -10, -40)

    local fstring3 = panel:CreateFontString("SkeenMeleeOptions_string3","OVERLAY","GameFontNormal")
    fstring3:SetText("Update Interval (sec)")
    fstring3:SetPoint("TOPLEFT", 10, -70)

    local slider3 = CreateFrame("Slider", "$parent_sl3", panel, "OptionsSliderTemplate")
    slider3:SetMinMaxValues(0, 1)
    slider3:SetValue(SkeenMelee:GetUpdateInterval())
    slider3:SetValueStep(.1)
    slider3:SetScript("OnValueChanged", function(self) SkeenMelee:SetUpdateInterval(self:GetValue()); getglobal(self:GetName() .. "Text"):SetText(string.format("%.1f", self:GetValue()))  end)
    getglobal(slider3:GetName() .. "Low"):SetText("0")
    getglobal(slider3:GetName() .. "High"):SetText("1")
    getglobal(slider3:GetName() .. "Text"):SetText(string.format("%.1f", SkeenMelee:GetUpdateInterval()))
    slider3:SetPoint("TOPRIGHT", -10, -70)

    SkeenMelee.options = panel
    panel.parent = "Skeen"

    InterfaceOptions_AddCategory(panel);
end

function SkeenMelee:GetLocked()
    return SkeenMeleeDatabase.locked
end

function SkeenMelee:ToggleLocked()
    SkeenMeleeDatabase.locked = not SkeenMeleeDatabase.locked
    SkeenMelee:UpdateDragability()
end

function SkeenMelee:GetScale()
    return SkeenMeleeDatabase.scale
end

function SkeenMelee:SetScale(num)
    SkeenMeleeDatabase.scale = num
    SkeenMelee.displayFrame:SetScale(SkeenMeleeDatabase.scale)
end

function SkeenMelee:GetUpdateInterval()
    return SkeenMeleeDatabase.update_interval
end

function SkeenMelee:SetUpdateInterval(num)
    SkeenMeleeDatabase.update_interval = num
end

function SkeenMelee:UpdateDragability()
    if SkeenMeleeDatabase.locked then
        SkeenMelee.displayFrame:SetScript("OnMouseDown", nil)
        SkeenMelee.displayFrame:SetScript("OnMouseUp", nil)
        SkeenMelee.displayFrame:SetScript("OnDragStop", nil)
        SkeenMelee.displayFrame:SetBackdropColor(0, 0, 0, 0)
        SkeenMelee.displayFrame:EnableMouse(false)
    else
        SkeenMelee.displayFrame:SetScript("OnMouseDown", function(self) self:StartMoving() end)
        SkeenMelee.displayFrame:SetScript("OnMouseUp", function(self) self:StopMovingOrSizing() end)
        SkeenMelee.displayFrame:SetScript("OnDragStop", function(self) self:StopMovingOrSizing() end)
        SkeenMelee.displayFrame:SetBackdropColor(0, 0, 0, .4)
        SkeenMelee.displayFrame:EnableMouse(true)
    end
end

function SkeenMelee:HideFrame()
    SkeenMelee.displayFrame:Hide()
    SkeenMelee.infoString:Hide()
end

function SkeenMelee:ShowFrame()
    SkeenMelee.displayFrame:Show()
    SkeenMelee.infoString:Show()
end