annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Slider.lua @ 0:fc346da3afd9

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