-------------------------------------------------------------------------------------
-- Компонент редактора отвечающий отображение свойств выделенного обьекта
-- AA 2013
-------------------------------------------------------------------------------------

class 'TransformWindow' (Node)

function TransformWindow:__init(game_scene)
	Node.__init(self, game_scene:getMainEntity(), 'TransformWindow', 1)
	self.game_scene = game_scene
	self.inactive_opacity = .7
	self.active_opacity = 1
	self.apear_time = .2

	if self.CreateLoaderTable then
		local table_resource = self:CreateLoaderTable()
		LoadScene(self.main_entity, table_resource)
	end

	self:init_text_fields()
	self:setup_dragging()
	self:setup_buttons()
end

-------------------------------------------------------------------------------------
function TransformWindow:init_text_fields()
	local function create_text_field(parent)
		local text_field = createTextFieldEntity()
		text_field:attachTo(parent)
		return text_field
	end

	self.main_window = self:getEntityNamed('transform_window')

	self.text_fields = 
	{
		name =
		{
			value = create_text_field(self:getEntityNamed('name')),
		},
		position =
		{
			x = create_text_field(self:getEntityNamed('position_x')),
			y = create_text_field(self:getEntityNamed('position_y')),
			z = create_text_field(self:getEntityNamed('position_z')),
		},
		rotation =
		{
			deg = create_text_field(self:getEntityNamed('rotation_deg')),
			rad = create_text_field(self:getEntityNamed('rotation_rad')),
		},
		scale = 
		{
			x = create_text_field(self:getEntityNamed('scale_x')),
			y = create_text_field(self:getEntityNamed('scale_y')),
		},
		size = 
		{
			x = create_text_field(self:getEntityNamed('size_x')),
			y = create_text_field(self:getEntityNamed('size_y')),
		},
		color = 
		{
			r = create_text_field(self:getEntityNamed('color_r')),
			g = create_text_field(self:getEntityNamed('color_g')),
			b = create_text_field(self:getEntityNamed('color_b')),
			a = create_text_field(self:getEntityNamed('color_a')),
		},
		opacity =
		{
			value = create_text_field(self:getEntityNamed('opacity'))
		}
	}

	for group, text_fields in pairs(self.text_fields) do
		for key, text_field in pairs(text_fields) do
			text_field:setScaleX(.6)
			text_field:setScaleY(.6)
		end 
	end

	self.text_fields.color.r:setScale(.55)
	self.text_fields.color.g:setScale(.55)
	self.text_fields.color.b:setScale(.55)
	self.text_fields.color.a:setScale(.55)
end

-------------------------------------------------------------------------------------
function TransformWindow:setup_buttons()
	local buttons =
	{
		position_x_up = 	function(object) object:setX(object:getX() + 1) end,
		position_x_down = 	function(object) object:setX(object:getX() - 1) end,
		position_y_up = 	function(object) object:setY(object:getY() + 1) end,
		position_y_down = 	function(object) object:setY(object:getY() - 1) end,
		position_z_up = 	function(object) object:setOrder(object:getOrder() + 1) end,
		position_z_down = 	function(object) object:setOrder(object:getOrder() - 1) end,

		rotation_deg_up = 	function(object) object:setRotationDeg(object:getRotationDeg() + 1) end,
		rotation_deg_down =	function(object) object:setRotationDeg(object:getRotationDeg() - 1) end,
		rotation_rad_up = 	function(object) object:setRotationRad(object:getRotationRad() + .01) end,
		rotation_rad_down = function(object) object:setRotationRad(object:getRotationRad() - .01) end,

		scale_x_up = 		function(object) object:setScaleX(object:getScale().x + .01) end,
		scale_x_down =	 	function(object) object:setScaleX(object:getScale().x - .01) end,
		scale_y_up = 		function(object) object:setScaleY(object:getScale().y + .01) end,
		scale_y_down =	 	function(object) object:setScaleY(object:getScale().y - .01) end,

		size_x_up = 		function(object) object:setSize(object:getWidth() + 1, object:getHeight()) end,
		size_x_down =	 	function(object) object:setSize(object:getWidth() - 1, object:getHeight()) end,
		size_y_up = 		function(object) object:setSize(object:getWidth(), object:getHeight() + 1) end,
		size_y_down =	 	function(object) object:setSize(object:getWidth(), object:getHeight() - 1) end,

		color_r_up =	 	function(object)
								local c = object:getColor()
								if c:getR() < 1 then object:setColor(c:getR() + .01, c:getG(), c:getB(), c:getA()) end
		 					end,
		color_r_down =	 	function(object)
								local c = object:getColor()
								if c:getR() > 0.01 then object:setColor(c:getR() - .01, c:getG(), c:getB(), c:getA()) end
		 					end,
		color_g_up =	 	function(object)
								local c = object:getColor()
								if c:getG() < 1 then object:setColor(c:getR(), c:getG() + .01, c:getB(), c:getA()) end
		 					end,
		color_g_down =	 	function(object)
								local c = object:getColor()
								if c:getG() > 0.01 then object:setColor(c:getR(), c:getG() - .01, c:getB(), c:getA()) end
		 					end,
		color_b_up =	 	function(object)
								local c = object:getColor()
								if c:getB() < 1 then object:setColor(c:getR(), c:getG(), c:getB() + .01, c:getA()) end
		 					end,
		color_b_down =	 	function(object)
								local c = object:getColor()
								if c:getB() > 0.01 then object:setColor(c:getR(), c:getG(), c:getB() - .01, c:getA()) end
		 					end,
		color_a_up =	 	function(object)
								local c = object:getColor()
								if c:getA() < 1 then object:setColor(c:getR(), c:getG(), c:getB(), c:getA() + .01) end
		 					end,
		color_a_down =	 	function(object)
								local c = object:getColor()
								if c:getA() > 0.01 then object:setColor(c:getR(), c:getG(), c:getB(), c:getA() - .01) end
		 					end,

		opacity_up =	 	function(object) 
								local o = object:getOpacity()
								if o < 1 then object:setOpacity(o + .01) end
							end,
		opacity_down =		function(object) 
								local o = object:getOpacity()
								if o > 0.01 then object:setOpacity(o - .01) end
							end,
	}

	local frames_delay = 60
	local num_skip_updates = 10
	local updates_counter = 0
	local skip_updates = 0

	for button_name, handler_func in pairs(buttons) do
		local current_selected_object = nil
		self:getEntityNamed(button_name).onClickBegin:removeAll()
		self:getEntityNamed(button_name).onClickBegin:add( function(target, screenFocus) 
			self.button_hold = true
			current_selected_object = self.game_scene:getIngameEditor():get_selection():get_current_selected_object()
			if current_selected_object then
				handler_func(current_selected_object)
				self.game_scene:getIngameEditor():on_sprite_transformed(current_selected_object)
				screenFocus:startDrag(self:getEntityNamed(button_name)) 
			end
		end)

		self:getEntityNamed(button_name).onDragFocusKeep:removeAll()
		self:getEntityNamed(button_name).onDragFocusKeep:add( function(target, screenFocus) 
			updates_counter = updates_counter + 1
			if updates_counter > frames_delay then
				skip_updates = skip_updates + 1
				if skip_updates == num_skip_updates then
					if current_selected_object then
						handler_func(current_selected_object)
						self.game_scene:getIngameEditor():on_sprite_transformed(current_selected_object)
					end
					skip_updates = 0
				end
			end
		end)

		self:getEntityNamed(button_name).onDragFinish:removeAll()
		self:getEntityNamed(button_name).onDragFinish:add( function(target, screenFocus) 
			updates_counter = 0
			skip_updates = 0
			self.button_hold = false
		end) 
	end
end

-------------------------------------------------------------------------------------
function TransformWindow:setup_dragging()
	self.main_window.onClickBegin:removeAll()
	self.main_window.onClickBegin:add( function(target, screenFocus) 
		offset = Vector2f(screenFocus:getScrX() - self.main_window:getScrX(),
							screenFocus:getScrY() - self.main_window:getScrY())
		screenFocus:startDrag(self.main_window) 
	end)

	self.main_window.onDragFocusKeep:removeAll()
	self.main_window.onDragFocusKeep:add( function(target, screenFocus) 
		self.main_window:setScrXY(screenFocus:getScrX() - offset.x, screenFocus:getScrY() - offset.y)
	end)

	self.main_window.onFocusIn:add(function()
		self.main_window:destroyEffectors()
		Helpers.ChangeOpacity(self.main_window, self.main_window:getOpacity(), self.active_opacity, self.apear_time)
	end)

	self.main_window.onFocusOut:add(function()
		if not self.button_hold then
			self.main_window:destroyEffectors()
			Helpers.ChangeOpacity(self.main_window, self.main_window:getOpacity(), self.inactive_opacity, self.apear_time)
		end
	end)

	self.main_window:setOpacity(self.inactive_opacity)
end

-------------------------------------------------------------------------------------
function TransformWindow:update_values(object)
	if object then
		self.text_fields.name.value:setText(object:getName())

		self.text_fields.position.x:setText(string.format('%d', object:getX()))
		self.text_fields.position.y:setText(string.format('%d', object:getY()))
		self.text_fields.position.z:setText(string.format('%d', object:getOrder()))

		self.text_fields.rotation.deg:setText(string.format('%d', object:getRotationDeg()))
		self.text_fields.rotation.rad:setText(string.format('%.2f', object:getRotationRad()))

		self.text_fields.scale.x:setText(string.format('%.2f', object:getScale().x))
		self.text_fields.scale.y:setText(string.format('%.2f', object:getScale().y))

		self.text_fields.size.x:setText(string.format('%d', object:getWidth()))
		self.text_fields.size.y:setText(string.format('%d', object:getHeight()))

		self.text_fields.color.r:setText(string.format('%.2f', object:getColor():getR()))
		self.text_fields.color.g:setText(string.format('%.2f', object:getColor():getG()))
		self.text_fields.color.b:setText(string.format('%.2f', object:getColor():getB()))
		self.text_fields.color.a:setText(string.format('%.2f', object:getColor():getA()))

		self.text_fields.opacity.value:setText(string.format('%.2f', object:getOpacity()))
	else
		for group, text_fields in pairs(self.text_fields) do
			for key, text_field in pairs(text_fields) do
				text_field:setText('')
				text_field:setText('')
			end 
		end
	end
end

-------------------------------------------------------------------------------------
function TransformWindow:enable()
	self.enabled = true
	self:getEntityNamed('transform_window'):show()
end

-------------------------------------------------------------------------------------
function TransformWindow:disable()
	self.enabled = false
	self:getEntityNamed('transform_window'):hide()
end

--------------------------------------------------------------------------------
--                      GENERATED CONTENT. DO NOT EDIT.                       --
--------------------------------------------------------------------------------

function TransformWindow:CreateLoaderTable()
	local objects =
	{
		{ name = 'transform_window',  entityType = 'SpriteEntity',  x = 1068,  y = 213,  order = 0,  editingLocked = false,  treeItemIsExpanded = true,  hotSpot = {x = 143, y = 170},  texturePath = 'Data/Textures/Screens/IngameEditor/transform_window.png',  width = 261,  height = 331,  },
		{ name = 'name',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -93,  y = -139,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'position_x',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -114,  y = -90,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'position_y',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -46,  y = -90,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'position_z',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 24,  y = -91,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'rotation_deg',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -101,  y = -39,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'rotation_rad',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -18,  y = -39,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'scale_x',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -114,  y = 11,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'scale_y',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -46,  y = 11,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'size_x',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -111,  y = 61,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'size_y',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -38,  y = 61,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'color_r',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -115,  y = 118,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'color_g',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -53,  y = 118,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'color_b',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 8,  y = 118,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'color_a',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 71,  y = 118,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'opacity',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -83,  y = 147,  order = 0,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 10,  height = 10,  },
		{ name = 'position_x_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -79,  y = -101,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'position_x_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -79,  y = -93,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'position_y_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -10,  y = -101,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'position_y_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -10,  y = -93,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'position_z_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 59,  y = -101,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'position_z_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 59.0001,  y = -93,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'rotation_deg_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -64,  y = -49,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'rotation_deg_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -64,  y = -41,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'rotation_rad_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 19,  y = -50,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'rotation_rad_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 19,  y = -41,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'scale_x_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -79,  y = 1,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'scale_x_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -79,  y = 9,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'scale_y_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -11,  y = 1,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'scale_y_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -11,  y = 9,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'size_x_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -74,  y = 51,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'size_x_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -74,  y = 59,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'size_y_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -1,  y = 51,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'size_y_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -1,  y = 59,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'color_r_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -86,  y = 108,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'color_r_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -86,  y = 116,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'color_g_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -24,  y = 108,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'color_g_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -24,  y = 116,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'color_b_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 38,  y = 108,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'color_b_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 38,  y = 116,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'color_a_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 99,  y = 108,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'color_a_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = 99,  y = 116,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'opacity_up',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -41,  y = 137,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
		{ name = 'opacity_down',  entityType = 'SpriteEntity',  parentName = 'transform_window',  x = -41,  y = 145,  order = 5,  color = {r = 1, g = 1, b = 1, a = 0.00392157},  editingLocked = false,  treeItemIsExpanded = false,  width = 14,  height = 8,  },
	}
	return objects
end
