--------------------------------------------------------------------------
-- Небольшой встроенный редактор.
-- Горячие клавиши:
-- 		Включить\выключить: F1\F3
-- 		Двигать: w
-- 		Вращать: e
-- 		Масштабировать: q
--		Менять текстурные размеры: r
--		Сохранить: ctrl+s
-- AA 2013
--------------------------------------------------------------------------

dofile('Data/Scripts/Common/IngameEditor/Transform.lua')
dofile('Data/Scripts/Common/IngameEditor/SpriteManagement.lua')
dofile('Data/Scripts/Common/IngameEditor/Selection.lua')
dofile('Data/Scripts/Common/IngameEditor/TransformWindow.lua')

class 'IngameEditor'

function IngameEditor:__init(game_scene)
	self.game_scene = game_scene
	self.enabled = false
	self.components = 
	{ 
		selection = Selection(game_scene),
		transform = Transform(game_scene), 
		sprite_management = SpriteManagement(game_scene),
		transform_window = TransformWindow(game_scene),
	}

	self:set_hotkeys()
end

--------------------------------------------------------------------------
function IngameEditor:set_hotkeys()
	self.game_scene:getWindow():getHotkeySignal(Hotkey_Ctrl(ERSK_S) ):add(function() 
		if self.enabled then
			self:save() 
		end
	end)
end

--------------------------------------------------------------------------
function IngameEditor:enable()
	self.enabled = true
	for _, component in pairs(self.components) do
		component:enable()
	end
	print('ingame editor enabled')
end

--------------------------------------------------------------------------
function IngameEditor:disable()
	self.enabled = false
	for _, component in pairs(self.components) do
		component:disable()
	end
	print('ingame editor disabled')
end

--------------------------------------------------------------------------
function IngameEditor:destroy()
end

--------------------------------------------------------------------------
function IngameEditor:is_enabled()
	return self.enabled
end

--------------------------------------------------------------------------
function IngameEditor:on_scene_start()
	for _, component in pairs(self.components) do
		if component.on_scene_start then
			component:on_scene_start()
		end
	end
end

--------------------------------------------------------------------------
function IngameEditor:on_scene_destroy()
	for _, component in pairs(self.components) do
		if component.on_scene_destroy then
			component:on_scene_destroy()
		end
	end
end

--------------------------------------------------------------------------
function IngameEditor:on_object_selected(object)
	for _, component in pairs(self.components) do
		if component.on_object_selected then
			component:on_object_selected(object)
		end
	end
	self.components.transform_window:update_values(object)
end

--------------------------------------------------------------------------
function IngameEditor:on_selection_removed()
	for _, component in pairs(self.components) do
		if component.on_selection_removed then
			component:on_selection_removed()
		end
	end
	self.components.transform_window:update_values()
end

--------------------------------------------------------------------------
function IngameEditor:save()
	for _, component in pairs(self.components) do
		if component.save then
			component:save()
		end
	end
	self:show_save_indicator()
end

--------------------------------------------------------------------------
function IngameEditor:on_sprite_created(object)
	self.components.selection:add_click_handler(object)
	self.components.transform_window:update_values(object)
end

--------------------------------------------------------------------------
function IngameEditor:on_sprite_removed()
	self.game_scene:getIngameEditor():get_selection():remove_selection()
end

--------------------------------------------------------------------------
function IngameEditor:on_sprite_transformed(object)
	self.components.selection:resize_click_handler(object)
	self.components.transform_window:update_values(object)
end

--------------------------------------------------------------------------
function IngameEditor:show_save_indicator()
	local indicator = CreateSpriteEntity('')
	indicator:setName('ingame_editor_save_indicator')
	indicator:setSize(WSCR_W, WSCR_H)
	indicator:setPosition(Vector2f(WORK_W / 2, WORK_H / 2))
	indicator:setColor(1, 1, 1, .5)
	indicator:attachTo(self.game_scene:getPlayField():getMainEntity())

	local eff = CreateAnimationEffector()
	eff:setKey(_TRACK_O, 0, 0)
	eff:setKey(_TRACK_O, .1, .9)
	eff:setKey(_TRACK_O, .2, 0)
	eff:attachTo(indicator)
	eff:play()

	self.game_scene:getPlayField():getTimer():add(function()
		indicator:destroy()
	end, .4)
end

--------------------------------------------------------------------------
function IngameEditor:get_selection()
	return self.components.selection
end