annotate ui/AmrUiButton.lua @ 57:01b63b8ed811 v21

total rewrite to version 21
author yellowfive
date Fri, 05 Jun 2015 11:05:15 -0700
parents
children 0515882856f1
rev   line source
yellowfive@57 1 --[[-----------------------------------------------------------------------------
yellowfive@57 2 Button Widget
yellowfive@57 3 Based on the AceGUI button, but with a custom look for AMR.
yellowfive@57 4 -------------------------------------------------------------------------------]]
yellowfive@57 5 local Type, Version = "AmrUiButton", 1
yellowfive@57 6 local AceGUI = LibStub and LibStub("AceGUI-3.0", true)
yellowfive@57 7 if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end
yellowfive@57 8
yellowfive@57 9 local Amr = LibStub("AceAddon-3.0"):GetAddon("AskMrRobot")
yellowfive@57 10
yellowfive@57 11 -- Lua APIs
yellowfive@57 12 local pairs = pairs
yellowfive@57 13
yellowfive@57 14 -- WoW APIs
yellowfive@57 15 local _G = _G
yellowfive@57 16 local PlaySound, CreateFrame, UIParent = PlaySound, CreateFrame, UIParent
yellowfive@57 17
yellowfive@57 18
yellowfive@57 19 --[[-----------------------------------------------------------------------------
yellowfive@57 20 Scripts
yellowfive@57 21 -------------------------------------------------------------------------------]]
yellowfive@57 22 local function buttonOnClick(frame, ...)
yellowfive@57 23 AceGUI:ClearFocus()
yellowfive@57 24 PlaySound("igMainMenuOption")
yellowfive@57 25 frame.obj:Fire("OnClick", ...)
yellowfive@57 26 end
yellowfive@57 27
yellowfive@57 28 --[[-----------------------------------------------------------------------------
yellowfive@57 29 Methods
yellowfive@57 30 -------------------------------------------------------------------------------]]
yellowfive@57 31 local methods = {
yellowfive@57 32 ["OnAcquire"] = function(self)
yellowfive@57 33 -- restore default values
yellowfive@57 34 self:SetHeight(24)
yellowfive@57 35 self:SetWidth(200)
yellowfive@57 36 self:SetDisabled(false)
yellowfive@57 37 self:SetText()
yellowfive@57 38 self:SetBackgroundColor({R = 0, G = 0, B = 0})
yellowfive@57 39 self:SetFont(Amr.CreateFont("Regular", 16, Amr.Colors.White))
yellowfive@57 40 self.frame:ClearAllPoints()
yellowfive@57 41 end,
yellowfive@57 42
yellowfive@57 43 ["SetText"] = function(self, text)
yellowfive@57 44 self.frame:SetText(text)
yellowfive@57 45 end,
yellowfive@57 46
yellowfive@57 47 ["SetFont"] = function(self, font)
yellowfive@57 48 self.frame:SetNormalFontObject(font)
yellowfive@57 49 end,
yellowfive@57 50
yellowfive@57 51 -- color is an object with R, G, B
yellowfive@57 52 ["SetBackgroundColor"] = function(self, color)
yellowfive@57 53 self.texNormal:SetTexture(color.R, color.G, color.B, 1)
yellowfive@57 54 self.texPush:SetTexture(color.R, color.G, color.B, 1)
yellowfive@57 55 end,
yellowfive@57 56
yellowfive@57 57 ["SetDisabled"] = function(self, disabled)
yellowfive@57 58 self.disabled = disabled
yellowfive@57 59 if disabled then
yellowfive@57 60 self.frame:Disable()
yellowfive@57 61 self.frame:SetAlpha(0.2)
yellowfive@57 62 else
yellowfive@57 63 self.frame:Enable()
yellowfive@57 64 self.frame:SetAlpha(1)
yellowfive@57 65 end
yellowfive@57 66 end,
yellowfive@57 67
yellowfive@57 68 ["SetVisible"] = function(self, visible)
yellowfive@57 69 if visible then
yellowfive@57 70 self.frame:Show()
yellowfive@57 71 else
yellowfive@57 72 self.frame:Hide()
yellowfive@57 73 end
yellowfive@57 74 end
yellowfive@57 75 }
yellowfive@57 76
yellowfive@57 77 --[[-----------------------------------------------------------------------------
yellowfive@57 78 Constructor
yellowfive@57 79 -------------------------------------------------------------------------------]]
yellowfive@57 80 local function Constructor()
yellowfive@57 81 local name = "AmrUiButton" .. AceGUI:GetNextWidgetNum(Type)
yellowfive@57 82 local frame = CreateFrame("Button", name, UIParent)
yellowfive@57 83 frame:Hide()
yellowfive@57 84
yellowfive@57 85 frame:EnableMouse(true)
yellowfive@57 86 frame:SetScript("OnClick", buttonOnClick)
yellowfive@57 87
yellowfive@57 88 Amr.DropShadow(frame)
yellowfive@57 89
yellowfive@57 90 -- normal bg color, can be set with SetBackgroundColor
yellowfive@57 91 local texNormal = frame:CreateTexture(nil, "BORDER")
yellowfive@57 92 texNormal:SetAllPoints(true)
yellowfive@57 93 frame:SetNormalTexture(texNormal)
yellowfive@57 94
yellowfive@57 95 -- gives a 1px down+right animation on press
yellowfive@57 96 local texPush = frame:CreateTexture(nil, "BORDER")
yellowfive@57 97 texPush:SetPoint("TOPLEFT", frame, "TOPLEFT", 1, -1)
yellowfive@57 98 texPush:SetPoint("BOTTOMRIGHT", frame, "BOTTOMRIGHT", 1, -1)
yellowfive@57 99 frame:SetPushedTexture(texPush)
yellowfive@57 100
yellowfive@57 101 -- not perfect, but more or less achieves the effect of lightening the bg color slightly on hover
yellowfive@57 102 local texHigh = frame:CreateTexture(nil, "BORDER")
yellowfive@57 103 texHigh:SetTexture(1, 1, 1, 0.1)
yellowfive@57 104 texHigh:SetAllPoints(true)
yellowfive@57 105 frame:SetHighlightTexture(texHigh)
yellowfive@57 106
yellowfive@57 107 local widget = {
yellowfive@57 108 texNormal = texNormal,
yellowfive@57 109 texPush = texPush,
yellowfive@57 110 frame = frame,
yellowfive@57 111 type = Type
yellowfive@57 112 }
yellowfive@57 113 for method, func in pairs(methods) do
yellowfive@57 114 widget[method] = func
yellowfive@57 115 end
yellowfive@57 116
yellowfive@57 117 return AceGUI:RegisterAsWidget(widget)
yellowfive@57 118 end
yellowfive@57 119
yellowfive@57 120 AceGUI:RegisterWidgetType(Type, Constructor, Version)