dofile ('Data/Scripts/InGameEditor/EntityInfoScreen.lua')   

-------------------------------------------------------------------------------
class 'EditorScreen' (Node)
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
local ENTITY_COLOR_DELTA    = 0.6
local COMMON_TECTURES       = 'Data/Textures/Common/'
local TEXTURE_CORNER        = 'cornerChange.jpg'
local PREFIX_TEXTURE        = 'areaChange_'
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:__init(playfield)   
    Node.__init(self, playfield:getMainEntity(), 'EditorScreen',  1000)  
    self.playfield      = playfield  
    self.entityInfo     = Helpers.CallFunctionByName('EntityInfoScreen', self)

    self:init()    
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:setEventHandlers()
    self.main_entity.onDestroy:add( bind(self.destroyAll, self) )

    for _, entity in pairs(self.objects) do
    	local obj = self.playfield:getEntityNamed(entity.name)
    	entity.focusIn 	= obj.onFocusIn:add( bind(self.entityFocusIn, self) )
    	entity.focusOut = obj.onFocusOut:add( bind(self.entityFocusOut, self) )
    end
end  
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:destroyAll()
    for _, entity in pairs(self.objects) do
        entity.focusIn:remove()
        entity.focusOut:remove()
    end

    for _, ent in pairs(self.objects_list) do
        if self:hasEntityNamed(ent.obj_c) then
            self:getEntityNamed(ent.obj_c):destroy()
        end
    end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:entityFocusIn(entity)
	self.entityInfo:getEntityNamed('entity_name'):setText(entity:getName())
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:entityFocusOut(entity)	
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:init()
    self.objects_list   = {}
    self.prevMousePos   = {}
    self.startOrder     = 1000

    self:loadData()
    self:createChangeZones()
    self:setEventHandlers()
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:loadData()
   self.objects = self.playfield:CreateLoaderTable()   
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:createChangeZones()
    local function create_corner(sx, sy, rect, prefix)
        local width = 16
        local height = 16

        local corner = CreateSpriteEntity(COMMON_TECTURES .. TEXTURE_CORNER)
        corner:attachTo         ( rect )
        corner:setName          ( rect:getName() .. prefix )   
        corner:setAnimationSize ( width, height)        
        corner:setHotSpot       ( width/2, height/2 )
        corner:setPosition      ( Vector2f(sx * rect:getWidth()/2, sy * rect:getHeight()/2) )
        corner:setOrder         ( 10)
        corner:hide()

        -- corner.onClickBegin:add( bind(self.onCornerClick, self) )
        -- corner.onClickEnd:add( bind(self.onCornerUnclick, self) )  
        -- corner.onUpdate:add( bind(self.updateMoveCursor, self) )
    end

    for i, obj in pairs(self.objects) do
        local scaleX     = obj.scaleX and obj.scaleX  or 1
        local scaleY     = obj.scaleY and obj.scaleY  or 1

        local width     = obj.width and obj.width * scaleX  or 1366
        local height    = obj.height and obj.height * scaleY or 768
        local entity    = self.playfield:getEntityNamed(obj.name) 

        local rect              = CreateSpriteEntity('')
        rect:attachTo           ( self.main_entity )
        rect:setName            ( PREFIX_TEXTURE .. obj.name )  
        rect:setAnimationSize   ( width, height)        
        rect:setHotSpot         ( width/2, height/2 )
        rect:setColor           ( Colorf(0, 0, 0, 0.01) )     
        rect:setOrder           ( self.startOrder  + obj.order)
        rect:setPosition        ( Vector2f(obj.x, obj.y) )
        

        table.insert(self.objects_list, {obj_c = rect:getName(), obj = obj.name, color = self.playfield:getEntityNamed(obj.name):getColor()})

        rect.onRightClickBegin:add( bind(self.onRightClick, self) )
        rect.onRightClickEnd:add( bind(self.onRightUnclick, self) )

        rect.onClickBegin:add( bind(self.setMoveScreen, self, true) )
        rect.onClickEnd:add( bind(self.setMoveScreen, self, false) )

        rect.onUpdate:add( bind(self.updateMoveCursor, self) )

        create_corner( 1, -1, rect, 'tr')
        create_corner(-1,  1, rect, 'bl')
        create_corner( 1,  1, rect, 'br')
        create_corner(-1, -1, rect, 'tl')
    end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:showCorners(show, obj_name, postfix)
    local obj = self.playfield:getEntityNamed(PREFIX_TEXTURE .. obj_name .. postfix) 
    
    if show then
        obj:show()
    else
        obj:hide()
    end 
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:hideAllCorners()
    for _, item in pairs(self.objects_list) do
        local obj = item.obj

        self:showCorners(false, obj, 'tr')
        self:showCorners(false, obj, 'bl')
        self:showCorners(false, obj, 'br')
        self:showCorners(false, obj, 'tl')
    end
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:onRightClick( object ) 
    local obj_name = string.sub(object:getName(), 12)
    local obj = self.playfield:getEntityNamed(obj_name)  
     
    if self.current_obj_change then 
        self.current_obj_change:setColor(0, 0, 0, 0.01)
    end 
    object:setColor(0, 0, 0, 0.45)

    self:hideAllCorners()

    self:showCorners(true, obj_name, 'tr')
    self:showCorners(true, obj_name, 'bl')
    self:showCorners(true, obj_name, 'br')
    self:showCorners(true, obj_name, 'tl')

    self.current_obj = obj
    self.current_obj_change = object

    self.prev_mouse_pos = getHandledCursor():getPosition()
    self.rightCliked = true
    self.prevMousePos = {}
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:setMoveScreen(move)
    self.isScreenMoved = move
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:onRightUnclick( object )
    self.rightCliked = false    
end
-------------------------------------------------------------------------------

-------------------------------------------------------------------------------
function EditorScreen:updateMoveCursor( dt )
    if self.isScreenMoved then
        if self.current_obj then
            local obj = self.current_obj
            local mousePos = getHandledCursor():getPosition()

            local x, y = obj:getX(), obj:getY()
            local dx, dy = 0, 0

            if self.prevMousePos.x then
                dx, dy = mousePos.x - self.prevMousePos.x, mousePos.y - self.prevMousePos.y
            end

            self.current_obj_change:setXY(x + dx, y + dy)
            obj:setXY(x + dx, y + dy)        

            self.prevMousePos = mousePos
        end
    else
        self.prevMousePos = {}
    end
end
-------------------------------------------------------------------------------

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

function EditorScreen:CreateLoaderTable()
    local objects =
    {
    }
    return objects
end