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