Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Slider Widget Zerotorescue@0: Graphical Slider, like, for Range values. Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local Type, Version = "Slider", 20 Zerotorescue@0: local AceGUI = LibStub and LibStub("AceGUI-3.0", true) Zerotorescue@0: if not AceGUI or (AceGUI:GetWidgetVersion(Type) or 0) >= Version then return end Zerotorescue@0: Zerotorescue@0: -- Lua APIs Zerotorescue@0: local min, max, floor = math.min, math.max, math.floor Zerotorescue@0: local tonumber, pairs = tonumber, pairs Zerotorescue@0: Zerotorescue@0: -- WoW APIs Zerotorescue@0: local PlaySound = PlaySound Zerotorescue@0: local CreateFrame, UIParent = CreateFrame, UIParent Zerotorescue@0: Zerotorescue@0: -- Global vars/functions that we don't upvalue since they might get hooked, or upgraded Zerotorescue@0: -- List them here for Mikk's FindGlobals script Zerotorescue@0: -- GLOBALS: GameFontHighlightSmall Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Support functions Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local function UpdateText(self) Zerotorescue@0: local value = self.value or 0 Zerotorescue@0: if self.ispercent then Zerotorescue@0: self.editbox:SetText(("%s%%"):format(floor(value * 1000 + 0.5) / 10)) Zerotorescue@0: else Zerotorescue@0: self.editbox:SetText(floor(value * 100 + 0.5) / 100) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function UpdateLabels(self) Zerotorescue@0: local min, max = (self.min or 0), (self.max or 100) Zerotorescue@0: if self.ispercent then Zerotorescue@0: self.lowtext:SetFormattedText("%s%%", (min * 100)) Zerotorescue@0: self.hightext:SetFormattedText("%s%%", (max * 100)) Zerotorescue@0: else Zerotorescue@0: self.lowtext:SetText(min) Zerotorescue@0: self.hightext:SetText(max) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Scripts Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local function Control_OnEnter(frame) Zerotorescue@0: frame.obj:Fire("OnEnter") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Control_OnLeave(frame) Zerotorescue@0: frame.obj:Fire("OnLeave") Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Frame_OnMouseDown(frame) Zerotorescue@0: frame.obj.slider:EnableMouseWheel(true) Zerotorescue@0: AceGUI:ClearFocus() Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Slider_OnValueChanged(frame) Zerotorescue@0: local self = frame.obj Zerotorescue@0: if not frame.setup then Zerotorescue@0: local newvalue = frame:GetValue() Zerotorescue@0: if newvalue ~= self.value and not self.disabled then Zerotorescue@0: self.value = newvalue Zerotorescue@0: self:Fire("OnValueChanged", newvalue) Zerotorescue@0: end Zerotorescue@0: if self.value then Zerotorescue@0: UpdateText(self) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Slider_OnMouseUp(frame) Zerotorescue@0: local self = frame.obj Zerotorescue@0: self:Fire("OnMouseUp", self.value) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function Slider_OnMouseWheel(frame, v) Zerotorescue@0: local self = frame.obj Zerotorescue@0: if not self.disabled then Zerotorescue@0: local value = self.value Zerotorescue@0: if v > 0 then Zerotorescue@0: value = min(value + (self.step or 1), self.max) Zerotorescue@0: else Zerotorescue@0: value = max(value - (self.step or 1), self.min) Zerotorescue@0: end Zerotorescue@0: self.slider:SetValue(value) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function EditBox_OnEscapePressed(frame) Zerotorescue@0: frame:ClearFocus() Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function EditBox_OnEnterPressed(frame) Zerotorescue@0: local self = frame.obj Zerotorescue@0: local value = frame:GetText() Zerotorescue@0: if self.ispercent then Zerotorescue@0: value = value:gsub('%%', '') Zerotorescue@0: value = tonumber(value) / 100 Zerotorescue@0: else Zerotorescue@0: value = tonumber(value) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: if value then Zerotorescue@0: PlaySound("igMainMenuOptionCheckBoxOn") Zerotorescue@0: self.slider:SetValue(value) Zerotorescue@0: self:Fire("OnMouseUp", value) Zerotorescue@0: end Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function EditBox_OnEnter(frame) Zerotorescue@0: frame:SetBackdropBorderColor(0.5, 0.5, 0.5, 1) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: local function EditBox_OnLeave(frame) Zerotorescue@0: frame:SetBackdropBorderColor(0.3, 0.3, 0.3, 0.8) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Methods Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local methods = { Zerotorescue@0: ["OnAcquire"] = function(self) Zerotorescue@0: self:SetWidth(200) Zerotorescue@0: self:SetHeight(44) Zerotorescue@0: self:SetDisabled(false) Zerotorescue@0: self:SetIsPercent(nil) Zerotorescue@0: self:SetSliderValues(0,100,1) Zerotorescue@0: self:SetValue(0) Zerotorescue@0: self.slider:EnableMouseWheel(false) Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: -- ["OnRelease"] = nil, Zerotorescue@0: Zerotorescue@0: ["SetDisabled"] = function(self, disabled) Zerotorescue@0: self.disabled = disabled Zerotorescue@0: if disabled then Zerotorescue@0: self.slider:EnableMouse(false) Zerotorescue@0: self.label:SetTextColor(.5, .5, .5) Zerotorescue@0: self.hightext:SetTextColor(.5, .5, .5) Zerotorescue@0: self.lowtext:SetTextColor(.5, .5, .5) Zerotorescue@0: --self.valuetext:SetTextColor(.5, .5, .5) Zerotorescue@0: self.editbox:SetTextColor(.5, .5, .5) Zerotorescue@0: self.editbox:EnableMouse(false) Zerotorescue@0: self.editbox:ClearFocus() Zerotorescue@0: else Zerotorescue@0: self.slider:EnableMouse(true) Zerotorescue@0: self.label:SetTextColor(1, .82, 0) Zerotorescue@0: self.hightext:SetTextColor(1, 1, 1) Zerotorescue@0: self.lowtext:SetTextColor(1, 1, 1) Zerotorescue@0: --self.valuetext:SetTextColor(1, 1, 1) Zerotorescue@0: self.editbox:SetTextColor(1, 1, 1) Zerotorescue@0: self.editbox:EnableMouse(true) Zerotorescue@0: end Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetValue"] = function(self, value) Zerotorescue@0: self.slider.setup = true Zerotorescue@0: self.slider:SetValue(value) Zerotorescue@0: self.value = value Zerotorescue@0: UpdateText(self) Zerotorescue@0: self.slider.setup = nil Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["GetValue"] = function(self) Zerotorescue@0: return self.value Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetLabel"] = function(self, text) Zerotorescue@0: self.label:SetText(text) Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetSliderValues"] = function(self, min, max, step) Zerotorescue@0: local frame = self.slider Zerotorescue@0: frame.setup = true Zerotorescue@0: self.min = min Zerotorescue@0: self.max = max Zerotorescue@0: self.step = step Zerotorescue@0: frame:SetMinMaxValues(min or 0,max or 100) Zerotorescue@0: UpdateLabels(self) Zerotorescue@0: frame:SetValueStep(step or 1) Zerotorescue@0: if self.value then Zerotorescue@0: frame:SetValue(self.value) Zerotorescue@0: end Zerotorescue@0: frame.setup = nil Zerotorescue@0: end, Zerotorescue@0: Zerotorescue@0: ["SetIsPercent"] = function(self, value) Zerotorescue@0: self.ispercent = value Zerotorescue@0: UpdateLabels(self) Zerotorescue@0: UpdateText(self) Zerotorescue@0: end Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: --[[----------------------------------------------------------------------------- Zerotorescue@0: Constructor Zerotorescue@0: -------------------------------------------------------------------------------]] Zerotorescue@0: local SliderBackdrop = { Zerotorescue@0: bgFile = "Interface\\Buttons\\UI-SliderBar-Background", Zerotorescue@0: edgeFile = "Interface\\Buttons\\UI-SliderBar-Border", Zerotorescue@0: tile = true, tileSize = 8, edgeSize = 8, Zerotorescue@0: insets = { left = 3, right = 3, top = 6, bottom = 6 } Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: local ManualBackdrop = { Zerotorescue@0: bgFile = "Interface\\ChatFrame\\ChatFrameBackground", Zerotorescue@0: edgeFile = "Interface\\ChatFrame\\ChatFrameBackground", Zerotorescue@0: tile = true, edgeSize = 1, tileSize = 5, Zerotorescue@0: } Zerotorescue@0: Zerotorescue@0: local function Constructor() Zerotorescue@0: local frame = CreateFrame("Frame", nil, UIParent) Zerotorescue@0: Zerotorescue@0: frame:EnableMouse(true) Zerotorescue@0: frame:SetScript("OnMouseDown", Frame_OnMouseDown) Zerotorescue@0: Zerotorescue@0: local label = frame:CreateFontString(nil, "OVERLAY", "GameFontNormal") Zerotorescue@0: label:SetPoint("TOPLEFT") Zerotorescue@0: label:SetPoint("TOPRIGHT") Zerotorescue@0: label:SetJustifyH("CENTER") Zerotorescue@0: label:SetHeight(15) Zerotorescue@0: Zerotorescue@0: local slider = CreateFrame("Slider", nil, frame) Zerotorescue@0: slider:SetOrientation("HORIZONTAL") Zerotorescue@0: slider:SetHeight(15) Zerotorescue@0: slider:SetHitRectInsets(0, 0, -10, 0) Zerotorescue@0: slider:SetBackdrop(SliderBackdrop) Zerotorescue@0: slider:SetThumbTexture("Interface\\Buttons\\UI-SliderBar-Button-Horizontal") Zerotorescue@0: slider:SetPoint("TOP", label, "BOTTOM") Zerotorescue@0: slider:SetPoint("LEFT", 3, 0) Zerotorescue@0: slider:SetPoint("RIGHT", -3, 0) Zerotorescue@0: slider:SetValue(0) Zerotorescue@0: slider:SetScript("OnValueChanged",Slider_OnValueChanged) Zerotorescue@0: slider:SetScript("OnEnter", Control_OnEnter) Zerotorescue@0: slider:SetScript("OnLeave", Control_OnLeave) Zerotorescue@0: slider:SetScript("OnMouseUp", Slider_OnMouseUp) Zerotorescue@0: slider:SetScript("OnMouseWheel", Slider_OnMouseWheel) Zerotorescue@0: Zerotorescue@0: local lowtext = slider:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall") Zerotorescue@0: lowtext:SetPoint("TOPLEFT", slider, "BOTTOMLEFT", 2, 3) Zerotorescue@0: Zerotorescue@0: local hightext = slider:CreateFontString(nil, "ARTWORK", "GameFontHighlightSmall") Zerotorescue@0: hightext:SetPoint("TOPRIGHT", slider, "BOTTOMRIGHT", -2, 3) Zerotorescue@0: Zerotorescue@0: local editbox = CreateFrame("EditBox", nil, frame) Zerotorescue@0: editbox:SetAutoFocus(false) Zerotorescue@0: editbox:SetFontObject(GameFontHighlightSmall) Zerotorescue@0: editbox:SetPoint("TOP", slider, "BOTTOM") Zerotorescue@0: editbox:SetHeight(14) Zerotorescue@0: editbox:SetWidth(70) Zerotorescue@0: editbox:SetJustifyH("CENTER") Zerotorescue@0: editbox:EnableMouse(true) Zerotorescue@0: editbox:SetBackdrop(ManualBackdrop) Zerotorescue@0: editbox:SetBackdropColor(0, 0, 0, 0.5) Zerotorescue@0: editbox:SetBackdropBorderColor(0.3, 0.3, 0.30, 0.80) Zerotorescue@0: editbox:SetScript("OnEnter", EditBox_OnEnter) Zerotorescue@0: editbox:SetScript("OnLeave", EditBox_OnLeave) Zerotorescue@0: editbox:SetScript("OnEnterPressed", EditBox_OnEnterPressed) Zerotorescue@0: editbox:SetScript("OnEscapePressed", EditBox_OnEscapePressed) Zerotorescue@0: Zerotorescue@0: local widget = { Zerotorescue@0: label = label, Zerotorescue@0: slider = slider, Zerotorescue@0: lowtext = lowtext, Zerotorescue@0: hightext = hightext, Zerotorescue@0: editbox = editbox, Zerotorescue@0: alignoffset = 25, Zerotorescue@0: frame = frame, Zerotorescue@0: type = Type Zerotorescue@0: } Zerotorescue@0: for method, func in pairs(methods) do Zerotorescue@0: widget[method] = func Zerotorescue@0: end Zerotorescue@0: slider.obj, editbox.obj = widget, widget Zerotorescue@0: Zerotorescue@0: return AceGUI:RegisterAsWidget(widget) Zerotorescue@0: end Zerotorescue@0: Zerotorescue@0: AceGUI:RegisterWidgetType(Type,Constructor,Version)