AceGUIWidget-Slider.lua 7.29 KB
Newer Older
1 2
local AceGUI = LibStub("AceGUI-3.0")

3 4 5 6 7
-- Lua APIs
local min, max, floor = math.min, math.max, math.floor
local tonumber = tonumber

-- WoW APIs
8
local PlaySound = PlaySound
9 10 11 12 13 14
local CreateFrame, UIParent = CreateFrame, UIParent

-- Global vars/functions that we don't upvalue since they might get hooked, or upgraded
-- List them here for Mikk's FindGlobals script
-- GLOBALS: GameFontHighlightSmall

15 16 17 18 19
--------------------------
-- Slider  	            --
--------------------------
do
	local Type = "Slider"
20
	local Version = 11
21 22
	
	local function OnAcquire(self)
nevcairiel's avatar
nevcairiel committed
23 24
		self:SetWidth(200)
		self:SetHeight(44)
25 26
		self:SetDisabled(false)
		self:SetIsPercent(nil)
27
		self:SetSliderValues(0,100,1)
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
		self:SetValue(0)
	end
	
	local function OnRelease(self)
		self.frame:ClearAllPoints()
		self.frame:Hide()
		self.slider:EnableMouseWheel(false)
		self:SetDisabled(false)
	end

	local function Control_OnEnter(this)
		this.obj:Fire("OnEnter")
	end
	
	local function Control_OnLeave(this)
		this.obj:Fire("OnLeave")
	end
	
	local function UpdateText(self)
47 48
		local value = self.value or 0
		if self.ispercent then
49
			self.editbox:SetText(("%s%%"):format(floor(value*1000+0.5)/10))
50
		else
51
			self.editbox:SetText(floor(value*100+0.5)/100)
52 53 54 55 56
		end
	end
	
	local function UpdateLabels(self)
		local min, max = (self.min or 0), (self.max or 100)
57
		if self.ispercent then
58 59
			self.lowtext:SetFormattedText("%s%%",(min * 100))
			self.hightext:SetFormattedText("%s%%",(max * 100))
60
		else
61 62
			self.lowtext:SetText(min)
			self.hightext:SetText(max)
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
		end
	end
	
	local function Slider_OnValueChanged(this)
		local self = this.obj
		if not this.setup then
			local newvalue
			newvalue = this:GetValue()
			if newvalue ~= self.value and not self.disabled then
				self.value = newvalue
				self:Fire("OnValueChanged", newvalue)
			end
			if self.value then
				local value = self.value
				UpdateText(self)
			end
		end
	end
	
	local function Slider_OnMouseUp(this)
		local self = this.obj
		self:Fire("OnMouseUp",this:GetValue())
	end
	
	local function Slider_OnMouseWheel(this, v)
		local self = this.obj
		if not self.disabled then
			local value = self.value
			if v > 0 then
92
				value = min(value + (self.step or 1),self.max)
93
			else
94
				value = max(value - (self.step or 1), self.min)
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
			end
			self.slider:SetValue(value)
		end
	end
	
	local function SetDisabled(self, disabled)
		self.disabled = disabled
		if disabled then
			self.slider:EnableMouse(false)
			self.label:SetTextColor(.5,.5,.5)
			self.hightext:SetTextColor(.5,.5,.5)
			self.lowtext:SetTextColor(.5,.5,.5)
			--self.valuetext:SetTextColor(.5,.5,.5)
			self.editbox:SetTextColor(.5,.5,.5)
			self.editbox:EnableMouse(false)
			self.editbox:ClearFocus()
		else
			self.slider:EnableMouse(true)
			self.label:SetTextColor(1,.82,0)
			self.hightext:SetTextColor(1,1,1)
			self.lowtext:SetTextColor(1,1,1)
			--self.valuetext:SetTextColor(1,1,1)
			self.editbox:SetTextColor(1,1,1)
			self.editbox:EnableMouse(true)
		end
	end
	
	local function SetValue(self, value)
		self.slider.setup = true
		self.slider:SetValue(value)
		self.value = value
		UpdateText(self)
		self.slider.setup = nil
	end
	
	local function SetLabel(self, text)
		self.label:SetText(text)
	end
	
134
	local function SetSliderValues(self, min, max, step)
135 136 137 138 139 140
		local frame = self.slider
		frame.setup = true
		self.min = min
		self.max = max
		self.step = step
		frame:SetMinMaxValues(min or 0,max or 100)
141
		UpdateLabels(self)
142
		frame:SetValueStep(step or 1)
143 144 145
		if self.value then
			frame:SetValue(self.value)
		end
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
		frame.setup = nil
	end
	
	local function EditBox_OnEscapePressed(this)
		this:ClearFocus()
	end
	
	local function EditBox_OnEnterPressed(this)
		local self = this.obj
		local value = this:GetText()
		if self.ispercent then
			value = value:gsub('%%','')
			value = tonumber(value) / 100
		else
			value = tonumber(value)
		end
		
		if value then
164
			PlaySound("igMainMenuOptionCheckBoxOn")
165
			self:Fire("OnValueChanged", value)
166 167 168 169
			self:Fire("OnMouseUp",value)
		end
	end
	
170 171 172 173 174 175 176 177
	local function EditBox_OnEnter(this)
		this:SetBackdropBorderColor(0.5,0.5,0.5,1)
	end
	
	local function EditBox_OnLeave(this)
		this:SetBackdropBorderColor(0.3,0.3,0.3,0.8)
	end
	
178 179
	local function SetIsPercent(self, value)
		self.ispercent = value
180 181
		UpdateLabels(self)
		UpdateText(self)
182 183 184 185 186 187 188 189 190 191 192 193 194 195
	end
	
	local function FrameOnMouseDown(this)
		this.obj.slider:EnableMouseWheel(true)
		AceGUI:ClearFocus()
	end
	
	local SliderBackdrop  = {
		bgFile = "Interface\\Buttons\\UI-SliderBar-Background",
		edgeFile = "Interface\\Buttons\\UI-SliderBar-Border",
		tile = true, tileSize = 8, edgeSize = 8,
		insets = { left = 3, right = 3, top = 6, bottom = 6 }
	}
	
196 197 198 199 200 201
	local ManualBackdrop = {
		bgFile = "Interface\\ChatFrame\\ChatFrameBackground",
		edgeFile = "Interface\\ChatFrame\\ChatFrameBackground",
		tile = true, edgeSize = 1, tileSize = 5,
	}
	
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
	local function Constructor()
		local frame = CreateFrame("Frame",nil,UIParent)
		local self = {}
		self.type = Type

		self.OnRelease = OnRelease
		self.OnAcquire = OnAcquire
		
		self.frame = frame
		frame.obj = self
		
		self.SetDisabled = SetDisabled
		self.SetValue = SetValue
		self.SetSliderValues = SetSliderValues
		self.SetLabel = SetLabel
		self.SetIsPercent = SetIsPercent
		
		self.alignoffset = 25
		
		frame:EnableMouse(true)
		frame:SetScript("OnMouseDown",FrameOnMouseDown)
		self.slider = CreateFrame("Slider",nil,frame)
		local slider = self.slider
		slider:SetScript("OnEnter",Control_OnEnter)
		slider:SetScript("OnLeave",Control_OnLeave)
		slider:SetScript("OnMouseUp", Slider_OnMouseUp)
		slider.obj = self
		slider:SetOrientation("HORIZONTAL")
		slider:SetHeight(15)
		slider:SetHitRectInsets(0,0,-10,0)
		slider:SetBackdrop(SliderBackdrop)
		--slider:EnableMouseWheel(true)
		slider:SetScript("OnMouseWheel", Slider_OnMouseWheel)
			
		local label = frame:CreateFontString(nil,"OVERLAY","GameFontNormal")
		label:SetPoint("TOPLEFT",frame,"TOPLEFT",0,0)
		label:SetPoint("TOPRIGHT",frame,"TOPRIGHT",0,0)
		label:SetJustifyH("CENTER")
		label:SetHeight(15)
		self.label = label
	
		self.lowtext = slider:CreateFontString(nil,"ARTWORK","GameFontHighlightSmall")
		self.lowtext:SetPoint("TOPLEFT",slider,"BOTTOMLEFT",2,3)
	
		self.hightext = slider:CreateFontString(nil,"ARTWORK","GameFontHighlightSmall")
		self.hightext:SetPoint("TOPRIGHT",slider,"BOTTOMRIGHT",-2,3)
	
	
		local editbox = CreateFrame("EditBox",nil,frame)
		editbox:SetAutoFocus(false)
		editbox:SetFontObject(GameFontHighlightSmall)
		editbox:SetPoint("TOP",slider,"BOTTOM",0,0)
		editbox:SetHeight(14)
		editbox:SetWidth(70)
		editbox:SetJustifyH("CENTER")
		editbox:EnableMouse(true)
		editbox:SetScript("OnEscapePressed",EditBox_OnEscapePressed)
		editbox:SetScript("OnEnterPressed",EditBox_OnEnterPressed)
260 261 262 263 264
		editbox:SetScript("OnEnter",EditBox_OnEnter)
		editbox:SetScript("OnLeave",EditBox_OnLeave)
		editbox:SetBackdrop(ManualBackdrop)
		editbox:SetBackdropColor(0,0,0,0.5)
		editbox:SetBackdropBorderColor(0.3,0.3,0.30,0.80)
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
		self.editbox = editbox
		editbox.obj = self
		
		slider:SetThumbTexture("Interface\\Buttons\\UI-SliderBar-Button-Horizontal")
	
		frame:SetWidth(200)
		frame:SetHeight(44)
		slider:SetPoint("TOP",label,"BOTTOM",0,0)
		slider:SetPoint("LEFT",frame,"LEFT",3,0)
		slider:SetPoint("RIGHT",frame,"RIGHT",-3,0)
	

		slider:SetValue(self.value or 0)
		slider:SetScript("OnValueChanged",Slider_OnValueChanged)
	
		AceGUI:RegisterAsWidget(self)
		return self
	end
	
	AceGUI:RegisterWidgetType(Type,Constructor,Version)
end