AceGUIWidget-Slider.lua 6.49 KB
Newer Older
1 2 3 4 5 6 7
local AceGUI = LibStub("AceGUI-3.0")

--------------------------
-- Slider  	            --
--------------------------
do
	local Type = "Slider"
8
	local Version = 8
9 10
	
	local function OnAcquire(self)
nevcairiel's avatar
nevcairiel committed
11 12
		self:SetWidth(200)
		self:SetHeight(44)
13 14
		self:SetDisabled(false)
		self:SetIsPercent(nil)
15
		self:SetSliderValues(0,100,1)
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
		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)
35 36 37 38 39 40 41 42 43 44
		local value = self.value or 0
		if self.ispercent then
			self.editbox:SetText(("%s%%"):format(math.floor(value*1000+0.5)/10))
		else
			self.editbox:SetText(math.floor(value*100+0.5)/100)
		end
	end
	
	local function UpdateLabels(self)
		local min, max = (self.min or 0), (self.max or 100)
45
		if self.ispercent then
46 47
			self.lowtext:SetFormattedText("%s%%",(min * 100))
			self.hightext:SetFormattedText("%s%%",(max * 100))
48
		else
49 50
			self.lowtext:SetText(min)
			self.hightext:SetText(max)
51 52 53 54 55 56 57 58 59 60 61 62 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 92 93 94 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
		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
				value = math.min(value + (self.step or 1),self.max)
			else
				value = math.max(value - (self.step or 1), self.min)
			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
	
122
	local function SetSliderValues(self, min, max, step)
123 124 125 126 127 128
		local frame = self.slider
		frame.setup = true
		self.min = min
		self.max = max
		self.step = step
		frame:SetMinMaxValues(min or 0,max or 100)
129
		UpdateLabels(self)
130
		frame:SetValueStep(step or 1)
131 132 133
		if self.value then
			frame:SetValue(self.value)
		end
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
		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
			self:Fire("OnMouseUp",value)
		end
	end
	
	local function SetIsPercent(self, value)
		self.ispercent = value
158 159
		UpdateLabels(self)
		UpdateText(self)
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 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
	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 }
	}
	
	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)
		self.editbox = editbox
		editbox.obj = self
		
		local bg = editbox:CreateTexture(nil,"BACKGROUND")
		editbox.bg = bg
		bg:SetTexture("Interface\\ChatFrame\\ChatFrameBackground")
		bg:SetVertexColor(0,0,0,0.25)
		bg:SetAllPoints(editbox)
		
		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