Wikia

CHDK Wiki

Lua/Drawings

Talk0
565pages on
this wiki

< Lua

Revision as of 19:16, June 22, 2013 by SkepticaLee (Talk | contribs)

Drawings - lua module

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.


Initialization

Prior to using the commands listed below, the drawings module has to be initialized with this imple 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 tape 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 remoced 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 was said that when you create object it is stored in memory. In fact all these objects are stored in two-dimentional array named SCREEN_DRAWINGS. You can access this array directly, but using of drawings commands is prefered. If you use drawings module you can not use SCREEN_DRAWINGS name for any other pourposes, due to conflict. The other restricted word is draw. You should not use variables such as draw, draw[] and other derivatives.

Advertisement | Your ad here

Around Wikia's network

Random Wiki