dofile ( "Scripts/guielement.lua" )

--
-- Slider component
--

Slider = {}
SliderMT = { __index = Slider }

-- inherit GUIElement
setmetatable ( Slider, { __index = GUIElement } )

function Slider:Create ( x, y, z, width, height, value )

	local progress_bar = {}
	setmetatable ( progress_bar, SliderMT )

	progress_bar.x = x
	progress_bar.y = y
	progress_bar.z = z
	progress_bar.width = width
	progress_bar.height = height

	progress_bar.sprite = Sprite ( x, y )
	progress_bar.sprite.depth = z
	display:add_child ( progress_bar.sprite )

	progress_bar:SetValue ( value )

	return progress_bar

end

function Slider:Destroy ( )

	display:remove_child ( self.sprite )

end

function Slider:SetPosition ( x, y )
	self.sprite:set_position ( x, y )
	self.x = x
	self.y = y
end

function Slider:Paint ( )

	self.sprite.canvas:clear ( )

	local v = ( self.width - 8 ) * self.value

	self.sprite.canvas:move_to ( 0, 0 )
	self.sprite.canvas:line_to ( self.width, 0 )
	self.sprite.canvas:line_to ( self.width, self.height )
	self.sprite.canvas:line_to ( 0, self.height )
	self.sprite.canvas:close_path ( )
	self.sprite.canvas:set_line_style ( 1, WHITE, 1 )
	self.sprite.canvas:stroke ( )


	local x = v + 4
	self.sprite.canvas:move_to ( x - 4, 0 )
	self.sprite.canvas:line_to ( x + 4, 0 )
	self.sprite.canvas:line_to ( x + 4, self.height )
	self.sprite.canvas:set_line_style ( 1, DARKRED, 1 )
	self.sprite.canvas:stroke ( )

	self.sprite.canvas:move_to ( x - 4, 0 )
	self.sprite.canvas:line_to ( x - 4, self.height )
	self.sprite.canvas:line_to ( x + 4, self.height )
	self.sprite.canvas:set_line_style ( 1, RED, 1 )
	self.sprite.canvas:stroke ( )

end

function Slider:SetValue ( value )

	self.value = value

	if self.value < 0 then
		self.value = 0
	elseif self.value > 1 then
		self.value = 1
	end
	
	self:OnChange ( self.value )

end

function Slider:OnChange ( value )

	self:Paint ( )

end

function Slider:MousePress ( button, x, y )

	self:Dragging ( button, x, y )

end

function Slider:MouseRelease ( button, x, y )

end

function Slider:Dragging ( button, x, y )

	x = x - 4
	if button == MBUTTON_LEFT then
		self:SetValue ( ( x - self.x + 4 ) / ( self.width - 4 ) )
	end

end

function Slider:BeginDrag ( button, x, y )

end

function Slider:EndDrag ( button, x, y )

end

function Slider:SingleClick ( button, x, y )

end

function Slider:DoubleClick ( button, x, y )

end

function Slider:WheelMove ( z )

end

function Slider:KeyPress ( key )

end

function Slider:KeyRelease ( key )

end