dofile ( "Scripts/guielement.lua" )

--
-- Drag selectable area
--

DragSelectable = {}
DragSelectableMT = { __index = DragSelectable }

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

function DragSelectable:Create ( x, y, z, width, height )

	local drag_selectable = {}
	setmetatable ( drag_selectable, DragSelectableMT )

	drag_selectable.x = x
	drag_selectable.y = y
	drag_selectable.z = z
	drag_selectable.width = width
	drag_selectable.height = height

	drag_selectable.sprite = Sprite ( )
	drag_selectable.sprite.depth = z
	display:add_child ( drag_selectable.sprite )

	return drag_selectable

end

function DragSelectable:Destroy ( )

	display:remove_child ( self.sprite )

end

function DragSelectable:OnSelect ( x1, y1, x2, y2 )

	self:Paint ( )

end

function DragSelectable:Paint ( )

	self.sprite.canvas:clear ( )

	if self.x1 and self.y2 and self.x2 and self.y2 then

		self.sprite.canvas:clear ( )
		self.sprite.canvas:move_to ( self.x1, self.y1 )
		self.sprite.canvas:line_to ( self.x2, self.y1 )
		self.sprite.canvas:line_to ( self.x2, self.y2 )
		self.sprite.canvas:line_to ( self.x1, self.y2 )
		self.sprite.canvas:close_path ( )
		self.sprite.canvas:set_fill_style ( RED, 0.5 )
		self.sprite.canvas:fill_preserve ( )
		self.sprite.canvas:set_line_style ( 1, RED, 1 )
		self.sprite.canvas:stroke ( )
	end

end

function DragSelectable:MousePress ( button, x, y )

	self:Dragging ( button, x, y )

end

function DragSelectable:MouseRelease ( button, x, y )

end

function DragSelectable:Dragging ( button, x, y )

	if button == MBUTTON_LEFT then
		self.x2 = x
		self.y2 = y

		self:Paint ( )
	end

end

function DragSelectable:BeginDrag ( button, x, y )

	if button == MBUTTON_LEFT then
		self.x1 = x
		self.y1 = y
	end

end

function DragSelectable:EndDrag ( button, x, y )

	if button == MBUTTON_LEFT then

		local oldx = self.x1
		local oldy = self.y1

		self.x1 = nil
		self.y1 = nil
		self.x2 = nil
		self.y2 = nil

		self:OnSelect ( oldx, oldy, x, y )

	end

end

function DragSelectable:SingleClick ( button, x, y )

end

function DragSelectable:DoubleClick ( button, x, y )

end

function DragSelectable:WheelMove ( z )

end

function DragSelectable:KeyPress ( key )

end

function DragSelectable:KeyRelease ( key )

end
