CHDK Wiki
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
=Drawings - lua module=
+
=Drawings - Lua module=
  +
CHDK 1.2.0 and later <span style="font-size:13px;line-height:21px;"> </span><span style="font-size:13px;line-height:21px;">releases</span><span style="font-size:13px;line-height:21px;"> </span><span style="font-size:13px;line-height:21px;"> support Lua functions that draw simple shapes on your camera's LCD screen from a script.  See : </span>http://chdk.wikia.com/wiki/Lua/Lua_Reference#Drawings<span style="font-size:13px;line-height:21px;"> for a description of these functions.</span>
This page describes special commands that are availble since changeset #1479, and are defined in drawings.lua module (A/CHDK/LUALIB/DRAWINGS.LUA).
 
 
Changeset #1479 bring a few Lua functions that provide a way do draw simple shapes on LCD screen from script (http://chdk.wikia.com/wiki/Lua/Lua_Reference#Drawings). This module enhances drawing procedure to give you more control in adding and removing shapes.
 
   
  +
<span style="font-size:13px;line-height:21px;">There is also a Lua module that allows you to enhances these drawing functions, giving you more control in adding and removing shapes.  The primary advantage of using this module is that it allows you to easily and simply redraw (repaint) the shapes you have drawn  - something that proves necessary periodically as the Canon UI tends to overwrite things randomly.</span>
   
  +
<span style="font-size:13px;line-height:21px;">These functions are defined in drawings.lua module (A/CHDK/LUALIB/DRAWINGS.LUA).</span>
 
==Initialization==
 
==Initialization==
Prior to using the commands listed below, the drawings module has to be initialized with this imple command:
+
Prior to using the commands listed below, the drawings module has to be initialized with this simple command:
 
require "drawings"
 
require "drawings"
 
Note: Do not use .lua extension.
 
Note: Do not use .lua extension.
Line 14: Line 14:
 
{| border="1" cellpadding="1" cellspacing="1" style="width: 500px; "
 
{| border="1" cellpadding="1" cellspacing="1" style="width: 500px; "
 
! scope="col"|Element
 
! scope="col"|Element
! scope="col"|Element tape
+
! scope="col"|Element type
 
! scope="col"|Parameters
 
! scope="col"|Parameters
 
|-
 
|-
Line 77: Line 77:
 
A specified object can be removed using:
 
A specified object can be removed using:
 
draw.remove(identifier)
 
draw.remove(identifier)
All objects can be remoced by using:
+
All objects can be removed by using:
 
draw.clear()
 
draw.clear()
 
Note that this command removes objects from the screen as well as from memory. After this command none of these objects will be accessible.
 
Note that this command removes objects from the screen as well as from memory. After this command none of these objects will be accessible.
Line 135: Line 135:
   
 
==Memory - SCREEN_DRAWINGS==
 
==Memory - SCREEN_DRAWINGS==
It has been stated that when an object is created. it is stored in memory. In fact, all these objects are stored in a two-dimentional array named SCREEN_DRAWINGS. This array can be accessed directly, but use of drawing commands is prefered. When using the drawing module the name SCREEN_DRAWINGS cannot be used for any other purposes, due to conflict. The other reserved word is ''draw''. Variable names such as ''draw'', ''draw[]'' and other derivatives should not be used
+
It has been stated that when an object is created. it is stored in memory. In fact, all these objects are stored in a two-dimentional array named SCREEN_DRAWINGS. This array can be accessed directly, but use of drawing commands is prefered.
  +
  +
'''NOTE''' : When using the drawing module the name '''SCREEN_DRAWINGS''' cannot be used for any other purposes, due to conflict. The other reserved word is ''draw''. Variable names such as '''''draw''''', '''''draw'''[]'' and other derivatives should not be used
 
[[Category:Lua ]]
 
[[Category:Lua ]]
 
[[Category:Scripting]]
 
[[Category:Scripting]]

Latest revision as of 17:37, 11 October 2014

Drawings - Lua module

CHDK 1.2.0 and later  releases  support Lua functions that draw simple shapes on your camera's LCD screen from a script.  See : http://chdk.wikia.com/wiki/Lua/Lua_Reference#Drawings for a description of these functions.

There is also a Lua module that allows you to enhances these drawing functions, giving you more control in adding and removing shapes.  The primary advantage of using this module is that it allows you to easily and simply redraw (repaint) the shapes you have drawn  - something that proves necessary periodically as the Canon UI tends to overwrite things randomly.

These functions are defined in drawings.lua module (A/CHDK/LUALIB/DRAWINGS.LUA).

Initialization

Prior to using the commands listed below, the drawings module has to be initialized with this simple command:

require "drawings"

Note: Do not use .lua extension.

Objects

In this manual object is understood as an individual shape with its own parameters, which can be added, removed, moved or changed. Objects that are available through this module are as follows:

Element Element type Parameters
Pixel pixel x1, x2, color
Line line x1, y1, x2, y2, color
Rectangle rect x1, y1, x2, y2, color [, thickness]
Filled Rectangle rectf x1, y1, x2, y2, color1, color2 [, thickness]
Ellipse elps x, y, a, b, color
Filled ellipse elpsf x, y, a, b, color1, color2
Text string string x, y, string, text_color, backgr_color

Parameters in [] are not necessary.

Creating objects

Before objects can be drawn or seen, they must be created. To create an object use following syntax:

draw.add("element_type", parameter1, parameter2....)

For example, to create an empty rectangle whose top-left corner has the coordinates 10, 50, the bottom-right corner is at 220,200, the color is blue and line thickness is 3 use this command:

draw.add("rect", 10, 50, 220, 200, "blue", 3)

Every time the draw.add() function is called it returns a number which identifies the newly created object. This value can be further used to change various parameters of the object. To obtain this value, use a variable, e.g. my_line_id:

my_line_id=draw.add("line", 45, 30, 100, 101, "red")

Now my_line_id stores the identifier.

Drawing objects

Once objects have been created,they are stored in memory, but they are not visible on the screen. To draw them use either of the following commands:

draw.​redraw()

or

draw.overdraw()

Both commands draw all objects on the screen. The difference is that draw.redraw() first clears the screen, so only the new state visible, whereas draw.overdraw() draws new state but does not remove previous drawings. If an object is moved and then draw.overdraw() is called this object will appear in both places.

Changing objects

Any previously created objects can be changed with reference to their identifiers. Use this command:

draw.replace(id, "new_element_type", new_param1, new_param2...)

For example, assume a filled rectangle has been created:

my_rect_id=draw.add("rectf", 55, 104, 65, 114, "green", "yellow", 2)

and you would like to move it 30 px to the right, using this coommand:

draw.replace(my_rect_id, "rectf", 85, 104, 95, 114, "green", "yellow", 2)

Note that with draw.replace() every parameter of the object cam be changed. For example the small rectangle from above can be changed into a large circle:

draw.replace(my_rect_id, "elps", 90, 110, 100, 100, "white")

Note also that all these changes will not be visible until draw.redraw() or draw.overdraw() are called.

Removing objects

A specified object can be removed using:

draw.remove(identifier)

All objects can be removed by using:

draw.clear()

Note that this command removes objects from the screen as well as from memory. After this command none of these objects will be accessible.

Also note that simple Lua command draw_clear() is used to clear screen and is not the same as draw.clear() from this module!

Retrieving parameters

If the identifier of an object is known then all its parameters can be retrieved as an array. Syntax:

params=draw.get_params(identifier)

For example:

params=draw.get_params(my_rect_id)

Variable params will now be an array with the following content:

{ "elps"​, 90, 110, 100, 100, "white" }

Colors

In the examples above, colors were used with explicit names. However, colors can be specifiedin two ways:

  • When using the Canon firmware palette as numbers in the range 0-255. These numbers are not portable and the same number migh correspond to other color in an other camera (or even other mode). Also, the same color will have different numerical values for different cameras. Use Canon firmware numbers only to specify tones for a specific camera.
  • When using the CHDK script palette. Colors in this palette that are used by drawings module are exactly the same as described in basic Lua drawing commands, except numeric values are not used but names as strings. These names are as follows:

Basic colors:

trans

black

white

red

green

blue

Extended colors:

red_dark

red_light

green_dark

green_light

blue_dark

blue_light

grey

grey_dark

grey_light

yellow

yellow_dark

yellow_light

Basic colors should be available on most or all CHDK-supported cameras, since they are used by CHDK GUI and histogram. Extended colors are available only for cameras that use colored icons. If extended colors are needed in a script, cameras with no color icon support will display the most similar color (e.g. green for green_light, white for yellow). Currently blue_light and blue_dark are only placeholders and they are not available in any camera.

Memory - SCREEN_DRAWINGS

It has been stated that when an object is created. it is stored in memory. In fact, all these objects are stored in a two-dimentional array named SCREEN_DRAWINGS. This array can be accessed directly, but use of drawing commands is prefered.

NOTE : When using the drawing module the name SCREEN_DRAWINGS cannot be used for any other purposes, due to conflict. The other reserved word is draw. Variable names such as draw, draw[] and other derivatives should not be used