from PSPApp import *
import PSPUtils

def ScriptProperties():
    return {
        'Author': u'Corel Corporation',
        'Copyright': u'Copyright (c) 2002-2006 Corel Corporation. All rights reserved.',
        'Description': "Center the active layer on the canvas",
        'Host': u'Paint Shop Pro',
        'Host Version': u'8.00'
        }

def Do(Environment):
    # we need a document for this to work
    if PSPUtils.RequireADoc( Environment ) == App.Constants.Boolean.false:
        return

     # need the canvas size, the layer size and the layer position
    CanvasSize = App.TargetDocument.Size
    LayerInfo = App.Do( Environment, 'ReturnLayerProperties' )
    # LayerRect comes back as a tuple of the from ( (x,y), dx, dy )
    # we want to work with a size tuple and a position tuple, so rearrange it.
    if len(LayerInfo['LayerRect']) == 2:
        LayerSize = (LayerInfo[ 'LayerRect' ][1], LayerInfo[ 'LayerRect' ][1] )
    else:
        LayerSize = (LayerInfo[ 'LayerRect' ][1], LayerInfo[ 'LayerRect' ][2] )
    LayerPos =  LayerInfo[ 'LayerRect' ][0]
    
    # now compute how far to move the layer to get it centered
    DesiredPos = ( (CanvasSize[0] - LayerSize[0]) / 2, (CanvasSize[1] - LayerSize[1]) / 2 )
    Offset = ( DesiredPos[0] - LayerPos[0], DesiredPos[1] - LayerPos[1] )
    App.Do( Environment, 'Mover', {
            'Offset': Offset, 
            'Object': App.Constants.LayerOrSelection.Layer, 
            'SelectPoint': None,
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match
                }
            })

