annotate Libs/AceGUI-3.0/widgets/AceGUIWidget-Slider.lua @ 44:7f9a7d2000ea

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