annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Slider.lua @ 57:01b63b8ed811 v21

total rewrite to version 21
author yellowfive
date Fri, 05 Jun 2015 11:05:15 -0700
parents
children e31b02b24488
rev   line source
yellowfive@57 1 --[[-----------------------------------------------------------------------------
yellowfive@57 2 Slider Widget
yellowfive@57 3 Graphical Slider, like, for Range values.
yellowfive@57 4 -------------------------------------------------------------------------------]]
yellowfive@57 5 local Type, Version = "Slider", 21
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 -- Lua APIs
yellowfive@57 10 local min, max, floor = math.min, math.max, math.floor
yellowfive@57 11 local tonumber, pairs = tonumber, pairs
yellowfive@57 12
yellowfive@57 13 -- WoW APIs
yellowfive@57 14 local PlaySound = PlaySound
yellowfive@57 15 local CreateFrame, UIParent = CreateFrame, UIParent
yellowfive@57 16
yellowfive@57 17 -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
yellowfive@57 18 -- List them here for Mikk's FindGlobals script
yellowfive@57 19 -- GLOBALS: GameFontHighlightSmall
yellowfive@57 20
yellowfive@57 21 --[[-----------------------------------------------------------------------------
yellowfive@57 22 Support functions
yellowfive@57 23 -------------------------------------------------------------------------------]]
yellowfive@57 24 local function UpdateText(self)
yellowfive@57 25 local value = self.value or 0
yellowfive@57 26 if self.ispercent then
yellowfive@57 27 self.editbox:SetText(("%s%%"):format(floor(value * 1000 + 0.5) / 10))
yellowfive@57 28 else
yellowfive@57 29 self.editbox:SetText(floor(value * 100 + 0.5) / 100)
yellowfive@57 30 end
yellowfive@57 31 end
yellowfive@57 32
yellowfive@57 33 local function UpdateLabels(self)
yellowfive@57 34 local min, max = (self.min or 0), (self.max or 100)
yellowfive@57 35 if self.ispercent then
yellowfive@57 36 self.lowtext:SetFormattedText("%s%%", (min * 100))
yellowfive@57 37 self.hightext:SetFormattedText("%s%%", (max * 100))
yellowfive@57 38 else
yellowfive@57 39 self.lowtext:SetText(min)
yellowfive@57 40 self.hightext:SetText(max)
yellowfive@57 41 end
yellowfive@57 42 end
yellowfive@57 43
yellowfive@57 44 --[[-----------------------------------------------------------------------------
yellowfive@57 45 Scripts
yellowfive@57 46 -------------------------------------------------------------------------------]]
yellowfive@57 47 local function Control_OnEnter(frame)
yellowfive@57 48 frame.obj:Fire("OnEnter")
yellowfive@57 49 end
yellowfive@57 50
yellowfive@57 51 local function Control_OnLeave(frame)
yellowfive@57 52 frame.obj:Fire("OnLeave")
yellowfive@57 53 end
yellowfive@57 54
yellowfive@57 55 local function Frame_OnMouseDown(frame)
yellowfive@57 56 frame.obj.slider:EnableMouseWheel(true)
yellowfive@57 57 AceGUI:ClearFocus()
yellowfive@57 58 end
yellowfive@57 59
yellowfive@57 60 local function Slider_OnValueChanged(frame)
yellowfive@57 61 local self = frame.obj
yellowfive@57 62 if not frame.setup then
yellowfive@57 63 local newvalue = frame:GetValue()
yellowfive@57 64 if self.step and self.step > 0 then
yellowfive@57 65 local min_value = self.min or 0
yellowfive@57 66 newvalue = floor((newvalue - min_value) / self.step + 0.5) * self.step + min_value
yellowfive@57 67 end
yellowfive@57 68 if newvalue ~= self.value and not self.disabled then
yellowfive@57 69 self.value = newvalue
yellowfive@57 70 self:Fire("OnValueChanged", newvalue)
yellowfive@57 71 end
yellowfive@57 72 if self.value then
yellowfive@57 73 UpdateText(self)
yellowfive@57 74 end
yellowfive@57 75 end
yellowfive@57 76 end
yellowfive@57 77
yellowfive@57 78 local function Slider_OnMouseUp(frame)
yellowfive@57 79 local self = frame.obj
yellowfive@57 80 self:Fire("OnMouseUp", self.value)
yellowfive@57 81 end
yellowfive@57 82
yellowfive@57 83 local function Slider_OnMouseWheel(frame, v)
yellowfive@57 84 local self = frame.obj
yellowfive@57 85 if not self.disabled then
yellowfive@57 86 local value = self.value
yellowfive@57 87 if v > 0 then
yellowfive@57 88 value = min(value + (self.step or 1), self.max)
yellowfive@57 89 else
yellowfive@57 90 value = max(value - (self.step or 1), self.min)
yellowfive@57 91 end
yellowfive@57 92 self.slider:SetValue(value)
yellowfive@57 93 end
yellowfive@57 94 end
yellowfive@57 95
yellowfive@57 96 local function EditBox_OnEscapePressed(frame)
yellowfive@57 97 frame:ClearFocus()
yellowfive@57 98 end
yellowfive@57 99
yellowfive@57 100 local function EditBox_OnEnterPressed(frame)
yellowfive@57 101 local self = frame.obj
yellowfive@57 102 local value = frame:GetText()
yellowfive@57 103 if self.ispercent then
yellowfive@57 104 value = value:gsub('%%', '')
yellowfive@57 105 value = tonumber(value) / 100
yellowfive@57 106 else
yellowfive@57 107 value = tonumber(value)
yellowfive@57 108 end
yellowfive@57 109
yellowfive@57 110 if value then
yellowfive@57 111 PlaySound("igMainMenuOptionCheckBoxOn")
yellowfive@57 112 self.slider:SetValue(value)
yellowfive@57 113 self:Fire("OnMouseUp", value)
yellowfive@57 114 end
yellowfive@57 115 end
yellowfive@57 116
yellowfive@57 117 local function EditBox_OnEnter(frame)
yellowfive@57 118 frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1)
yellowfive@57 119 end
yellowfive@57 120
yellowfive@57 121 local function EditBox_OnLeave(frame)
yellowfive@57 122 frame:SetBackdropBorderColor(0.3, 0.3, 0.3, 0.8)
yellowfive@57 123 end
yellowfive@57 124
yellowfive@57 125 --[[-----------------------------------------------------------------------------
yellowfive@57 126 Methods
yellowfive@57 127 -------------------------------------------------------------------------------]]
yellowfive@57 128 local methods = {
yellowfive@57 129 ["OnAcquire"] = function(self)
yellowfive@57 130 self:SetWidth(200)
yellowfive@57 131 self:SetHeight(44)
yellowfive@57 132 self:SetDisabled(false)
yellowfive@57 133 self:SetIsPercent(nil)
yellowfive@57 134 self:SetSliderValues(0,100,1)
yellowfive@57 135 self:SetValue(0)
yellowfive@57 136 self.slider:EnableMouseWheel(false)
yellowfive@57 137 end,
yellowfive@57 138
yellowfive@57 139 -- ["OnRelease"] = nil,
yellowfive@57 140
yellowfive@57 141 ["SetDisabled"] = function(self, disabled)
yellowfive@57 142 self.disabled = disabled
yellowfive@57 143 if disabled then
yellowfive@57 144 self.slider:EnableMouse(false)
yellowfive@57 145 self.label:SetTextColor(.5, .5, .5)
yellowfive@57 146 self.hightext:SetTextColor(.5, .5, .5)
yellowfive@57 147 self.lowtext:SetTextColor(.5, .5, .5)
yellowfive@57 148 --self.valuetext:SetTextColor(.5, .5, .5)
yellowfive@57 149 self.editbox:SetTextColor(.5, .5, .5)
yellowfive@57 150 self.editbox:EnableMouse(false)
yellowfive@57 151 self.editbox:ClearFocus()
yellowfive@57 152 else
yellowfive@57 153 self.slider:EnableMouse(true)
yellowfive@57 154 self.label:SetTextColor(1, .82, 0)
yellowfive@57 155 self.hightext:SetTextColor(1, 1, 1)
yellowfive@57 156 self.lowtext:SetTextColor(1, 1, 1)
yellowfive@57 157 --self.valuetext:SetTextColor(1, 1, 1)
yellowfive@57 158 self.editbox:SetTextColor(1, 1, 1)
yellowfive@57 159 self.editbox:EnableMouse(true)
yellowfive@57 160 end
yellowfive@57 161 end,
yellowfive@57 162
yellowfive@57 163 ["SetValue"] = function(self, value)
yellowfive@57 164 self.slider.setup = true
yellowfive@57 165 self.slider:SetValue(value)
yellowfive@57 166 self.value = value
yellowfive@57 167 UpdateText(self)
yellowfive@57 168 self.slider.setup = nil
yellowfive@57 169 end,
yellowfive@57 170
yellowfive@57 171 ["GetValue"] = function(self)
yellowfive@57 172 return self.value
yellowfive@57 173 end,
yellowfive@57 174
yellowfive@57 175 ["SetLabel"] = function(self, text)
yellowfive@57 176 self.label:SetText(text)
yellowfive@57 177 end,
yellowfive@57 178
yellowfive@57 179 ["SetSliderValues"] = function(self, min, max, step)
yellowfive@57 180 local frame = self.slider
yellowfive@57 181 frame.setup = true
yellowfive@57 182 self.min = min
yellowfive@57 183 self.max = max
yellowfive@57 184 self.step = step
yellowfive@57 185 frame:SetMinMaxValues(min or 0,max or 100)
yellowfive@57 186 UpdateLabels(self)
yellowfive@57 187 frame:SetValueStep(step or 1)
yellowfive@57 188 if self.value then
yellowfive@57 189 frame:SetValue(self.value)
yellowfive@57 190 end
yellowfive@57 191 frame.setup = nil
yellowfive@57 192 end,
yellowfive@57 193
yellowfive@57 194 ["SetIsPercent"] = function(self, value)
yellowfive@57 195 self.ispercent = value
yellowfive@57 196 UpdateLabels(self)
yellowfive@57 197 UpdateText(self)
yellowfive@57 198 end
yellowfive@57 199 }
yellowfive@57 200
yellowfive@57 201 --[[-----------------------------------------------------------------------------
yellowfive@57 202 Constructor
yellowfive@57 203 -------------------------------------------------------------------------------]]
yellowfive@57 204 local SliderBackdrop = {
yellowfive@57 205 bgFile = "Interface\\Buttons\\UI-SliderBar-Background",
yellowfive@57 206 edgeFile = "Interface\\Buttons\\UI-SliderBar-Border",
yellowfive@57 207 tile = true, tileSize = 8, edgeSize = 8,
yellowfive@57 208 insets = { left = 3, right = 3, top = 6, bottom = 6 }
yellowfive@57 209 }
yellowfive@57 210
yellowfive@57 211 local ManualBackdrop = {
yellowfive@57 212 bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
yellowfive@57 213 edgeFile = "Interface\\ChatFrame\\ChatFrameBackground",
yellowfive@57 214 tile = true, edgeSize = 1, tileSize = 5,
yellowfive@57 215 }
yellowfive@57 216
yellowfive@57 217 local function Constructor()
yellowfive@57 218 local frame = CreateFrame("Frame", nil, UIParent)
yellowfive@57 219
yellowfive@57 220 frame:EnableMouse(true)
yellowfive@57 221 frame:SetScript("OnMouseDown", Frame_OnMouseDown)
yellowfive@57 222
yellowfive@57 223 local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal")
yellowfive@57 224 label:SetPoint("TOPLEFT")
yellowfive@57 225 label:SetPoint("TOPRIGHT")
yellowfive@57 226 label:SetJustifyH("CENTER")
yellowfive@57 227 label:SetHeight(15)
yellowfive@57 228
yellowfive@57 229 local slider = CreateFrame("Slider", nil, frame)
yellowfive@57 230 slider:SetOrientation("HORIZONTAL")
yellowfive@57 231 slider:SetHeight(15)
yellowfive@57 232 slider:SetHitRectInsets(0, 0, -10, 0)
yellowfive@57 233 slider:SetBackdrop(SliderBackdrop)
yellowfive@57 234 slider:SetThumbTexture("Interface\\Buttons\\UI-SliderBar-Button-Horizontal")
yellowfive@57 235 slider:SetPoint("TOP", label, "BOTTOM")
yellowfive@57 236 slider:SetPoint("LEFT", 3, 0)
yellowfive@57 237 slider:SetPoint("RIGHT", -3, 0)
yellowfive@57 238 slider:SetValue(0)
yellowfive@57 239 slider:SetScript("OnValueChanged",Slider_OnValueChanged)
yellowfive@57 240 slider:SetScript("OnEnter", Control_OnEnter)
yellowfive@57 241 slider:SetScript("OnLeave", Control_OnLeave)
yellowfive@57 242 slider:SetScript("OnMouseUp", Slider_OnMouseUp)
yellowfive@57 243 slider:SetScript("OnMouseWheel", Slider_OnMouseWheel)
yellowfive@57 244
yellowfive@57 245 local lowtext = slider:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
yellowfive@57 246 lowtext:SetPoint("TOPLEFT", slider, "BOTTOMLEFT", 2, 3)
yellowfive@57 247
yellowfive@57 248 local hightext = slider:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall")
yellowfive@57 249 hightext:SetPoint("TOPRIGHT", slider, "BOTTOMRIGHT", -2, 3)
yellowfive@57 250
yellowfive@57 251 local editbox = CreateFrame("EditBox", nil, frame)
yellowfive@57 252 editbox:SetAutoFocus(false)
yellowfive@57 253 editbox:SetFontObject(GameFontHighlightSmall)
yellowfive@57 254 editbox:SetPoint("TOP", slider, "BOTTOM")
yellowfive@57 255 editbox:SetHeight(14)
yellowfive@57 256 editbox:SetWidth(70)
yellowfive@57 257 editbox:SetJustifyH("CENTER")
yellowfive@57 258 editbox:EnableMouse(true)
yellowfive@57 259 editbox:SetBackdrop(ManualBackdrop)
yellowfive@57 260 editbox:SetBackdropColor(0, 0, 0, 0.5)
yellowfive@57 261 editbox:SetBackdropBorderColor(0.3, 0.3, 0.30, 0.80)
yellowfive@57 262 editbox:SetScript("OnEnter", EditBox_OnEnter)
yellowfive@57 263 editbox:SetScript("OnLeave", EditBox_OnLeave)
yellowfive@57 264 editbox:SetScript("OnEnterPressed", EditBox_OnEnterPressed)
yellowfive@57 265 editbox:SetScript("OnEscapePressed", EditBox_OnEscapePressed)
yellowfive@57 266
yellowfive@57 267 local widget = {
yellowfive@57 268 label = label,
yellowfive@57 269 slider = slider,
yellowfive@57 270 lowtext = lowtext,
yellowfive@57 271 hightext = hightext,
yellowfive@57 272 editbox = editbox,
yellowfive@57 273 alignoffset = 25,
yellowfive@57 274 frame = frame,
yellowfive@57 275 type = Type
yellowfive@57 276 }
yellowfive@57 277 for method, func in pairs(methods) do
yellowfive@57 278 widget[method] = func
yellowfive@57 279 end
yellowfive@57 280 slider.obj, editbox.obj = widget, widget
yellowfive@57 281
yellowfive@57 282 return AceGUI:RegisterAsWidget(widget)
yellowfive@57 283 end
yellowfive@57 284
yellowfive@57 285 AceGUI:RegisterWidgetType(Type,Constructor,Version)