/*
Copyright (C) 2014-2015 Runtime Revolution Ltd.

This file is part of LiveCode.

LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.

LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with LiveCode.  If not see <http://www.gnu.org/licenses/>.  */

-- Library modules add public handlers to the bottom of the message path.
--
/* 
This simple library gives access to the LiveCode Builder canvas syntax from LiveCode Script by wrapping in a few simple handlers.

	Example usage:

	  canvasCreate 100, 100
	  canvasSetColor 1, 0, 0, 1
	  canvasFillCircle 50, 50, 25
	  canvasApplyToImage "image 1"
      canvasDestroy
  
   This creates a canvas of size 100x100, fills a circle of radius 25 at
   the centre, then copies the contents of the canvas to 'image 1' (which
   must already exist).
   
Tags: Canvas Library
*/
library com.livecode.library.canvas

metadata version is "1.0.0"
metadata author is "LiveCode"
metadata title is "Canvas Library"

-- We need syntax from both canvas and engine built-in modules.
use com.livecode.canvas
use com.livecode.engine

-- We store the current canvas in a module-scope variable.
variable sCanvas as optional Canvas

/*
Summary: Creates a canvas of the given size for the other handlers to use.
pWidth:  The width of the created canvas.
pHeight:  The height of the created canvas.

Description:
Creates a canvas of the given size for the other handlers to use. 
*/

public handler canvasCreate(in pWidth as Integer, in pHeight as Integer) returns nothing
  -- Create a new canvas, this will release any previous canvas.
  put a new canvas with size [ pWidth, pHeight ] into sCanvas
  -- Set the paint to opaque black and fill the whole area.
  set the paint of sCanvas to solid paint with color [0, 0, 0, 1]
  fill rectangle path of rectangle [ 0, 0, pWidth, pHeight ] on sCanvas
end handler

/*
Summary: Destroys the canvas 

Description:
Destroys the canvas by simply assigning nothing to the canvas variable.
*/

public handler canvasDestroy() returns nothing
  put nothing into sCanvas
end handler

/*
Summary: Sets the current color of the canvas to the given RGBA value.
pRed: The red component of the color to set.
pGreen: The green component of the color to set.
pBlue: The blue component of the color to set.
pAlpha: The alpha value of the color to set.

Description:
Sets the current color of the canvas to the given RGBA value.
*/
public handler canvasSetColor(in pRed as Real, in pGreen as Real, in pBlue as Real, in pAlpha as Real) returns nothing
  set the paint of sCanvas to solid paint with color [pRed, pGreen, pBlue, pRed]
end handler

/*
Summary: Draws a filled circle.
pX: The x-coordinate of the centre of the circle.
pY: The y-coordinate of the centre of the circle.
pRadius: The radius of the circle.
Description:
Fills a circle of the given radius at the given position in the canvas.
*/
public handler canvasFillCircle(in pX as Real, in pY as Real, in pRadius as Real) returns nothing
  fill circle path centered at point [pX, pY] with radius pRadius on sCanvas
end handler

/*
Summary: Copies the current contents of the canvas to the specified image object.
pObjectId: A string which is an object chunk referring to an image.

Description:
This handler copies the current contents of the canvas to the specified image object.
*/
public handler canvasApplyToImage(in pObjectId as String) returns nothing
  variable tObject as ScriptObject

  -- Resolving the script object gets a weak reference to the specified
  -- object and places it in 'the result'.
  resolve script object pObjectId
  put the result into tObject

  -- Now set the width and height of the image object to that of our canvas
  set property "width" of tObject to the pixel width of sCanvas
  set property "height" of tObject to the pixel height of sCanvas

  -- Finally set the image data of the image object.
  set property "imageData" of tObject to the pixel data of sCanvas
end handler

end library
