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

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