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