Difference between revisions of "Overview of the API"

From Inspired-Lua Wiki
Jump to navigation Jump to search
m
 
(20 intermediate revisions by 4 users not shown)
Line 1: Line 1:
Note: Neither the ''io'' nor ''os'' libraries are present for the TI-Nspire, which seems to be running a light version of Lua 5.1.4.  
+
Note: The Lua version that comes with OS 3.x is 5.1.4, without the ''io'' nor ''os'' libraries.
  
== Standard Library ==
+
Tutorials on Nspire Lua scripting in general are available here : http://inspired-lua.org/
  
( This has been taken from [http://www.wowwiki.com/Lua_functions a WoWWiki page] - See the full, official documentation [http://www.lua.org/manual/5.1/manual.html here] )  
+
== Standard Library  ==
 +
 
 +
( This has been taken from [http://www.wowwiki.com/Lua_functions a WoWWiki page] - See the full, official documentation [http://www.lua.org/manual/5.1/manual.html here] )
 +
 
 +
=== Common Lua Functions ===
  
 
*'''[[G]]''' - Global Variable - A global variable (not a function) that holds the global environment (that is, _G._G = _G). Lua itself does not use this variable; changing its value does not affect any environment, nor vice-versa. (Use setfenv to change environments.) - taken from Lua docs  
 
*'''[[G]]''' - Global Variable - A global variable (not a function) that holds the global environment (that is, _G._G = _G). Lua itself does not use this variable; changing its value does not affect any environment, nor vice-versa. (Use setfenv to change environments.) - taken from Lua docs  
 
*'''[[assert]]'''(value[, errormsg]) - asserts a value evaluates to true. If it is, returns value, otherwise causes a Lua error to be thrown.  
 
*'''[[assert]]'''(value[, errormsg]) - asserts a value evaluates to true. If it is, returns value, otherwise causes a Lua error to be thrown.  
 +
*'''[[collectgarbage]]''' - among other things, force free unused memory.
 +
*'''[[error]]'''(str) - generates an error with the str message.
 
*'''[[getfenv]]'''(function or integer) - Returns the table representing the stack frame of the given function or stack level.  
 
*'''[[getfenv]]'''(function or integer) - Returns the table representing the stack frame of the given function or stack level.  
 
*'''[[getmetatable]]'''(obj, mtable) - Returns the metatable of the given table or userdata object.  
 
*'''[[getmetatable]]'''(obj, mtable) - Returns the metatable of the given table or userdata object.  
 
*'''[[next]]'''(table, index) - Returns the next key, value pair of the table, allowing you to walk over the table.  
 
*'''[[next]]'''(table, index) - Returns the next key, value pair of the table, allowing you to walk over the table.  
 
*'''[[newproxy]]'''(boolean or proxy) - Creates a userdata with a sharable metatable.  
 
*'''[[newproxy]]'''(boolean or proxy) - Creates a userdata with a sharable metatable.  
*'''[[print]]''' - print (···) - Receives any number of arguments, and prints their values to stdout, using the tostring function to convert them to strings. print is not intended for formatted output, but only as a quick way to show a value, typically for debugging. For formatted output, use string.format. - taken from Lua Docs
+
*'''[[print]]'''(...) - Receives any number of arguments, and prints their values. Not intended for formatted output, but only as a quick way to show a value, typically for debugging. For formatted output, use string.format.
 
*'''[[select]]'''(index, list) - Returns the number of items in list or the value of the item in list at index.  
 
*'''[[select]]'''(index, list) - Returns the number of items in list or the value of the item in list at index.  
 
*'''[[setfenv]]'''(function or integer, table) - Sets the table representing the stack frame of the given function or stack level.  
 
*'''[[setfenv]]'''(function or integer, table) - Sets the table representing the stack frame of the given function or stack level.  
 
*'''[[setmetatable]]'''(obj, mtable) - Sets the metatable of the given table or userdata object.  
 
*'''[[setmetatable]]'''(obj, mtable) - Sets the metatable of the given table or userdata object.  
 
*'''[[tostring]]''' - tostring (e) - Receives an argument of any type and converts it to a string in a reasonable format. For complete control of how numbers are converted, use string.format. If the metatable of e has a "__tostring" field, then tostring calls the corresponding value with e as argument, and uses the result of the call as its result. - taken from Lua Docs.  
 
*'''[[tostring]]''' - tostring (e) - Receives an argument of any type and converts it to a string in a reasonable format. For complete control of how numbers are converted, use string.format. If the metatable of e has a "__tostring" field, then tostring calls the corresponding value with e as argument, and uses the result of the call as its result. - taken from Lua Docs.  
 +
*'''[[tonumber]]''' - tonumber (e) - Receives an argument of the string type and converts it to a number when possible. If the metatable of e has a "__tonumber" field, then tonumber calls the corresponding value with e as argument, and uses the result of the call as its result. - taken from Lua Docs.
 
*'''[[type]]'''(var) - Returns the type of variable as a string, "number", "string", "table", "function" or "userdata".  
 
*'''[[type]]'''(var) - Returns the type of variable as a string, "number", "string", "table", "function" or "userdata".  
 
*'''[[unpack]]'''(table[, start][, end]) - Returns the contents of its argument as separate values.  
 
*'''[[unpack]]'''(table[, start][, end]) - Returns the contents of its argument as separate values.  
 
*'''[[xpcall]]'''(func, err) - Returns a boolean indicating successful execution of func and calls err on failure, additionally returning func's or err's results.
 
*'''[[xpcall]]'''(func, err) - Returns a boolean indicating successful execution of func and calls err on failure, additionally returning func's or err's results.
  
== Concepts and Basics ==
+
=== Coroutines Library ===
 +
The coroutines are useful tools to control the running of differents parts of a code ("threads").
  
This part will explain you how the Lua actually works inside the OS and help you to figure out what you're doing when you write a script for the TI-Nspire. Before reading this, you have to know all about the Lua basics.
+
create, wrap, resume, yield, status, running
  
The Lua is an interpreted script language which means that it isn't as fast as ASM/C programs, but is still better than the TI-BASIC. One good thing is that this language is in a complete harmony with the OS with basic events and a powerfull graphic context. First, we have to understand how this works. Basicly the script is launched before the executive engine. This means that we can neither evaluate expressions nor use some of the standard API (like platform, var) until the engine is launched. Here is a call summary :
+
More information can be fouded in the [[coroutines]] page.
  
*Launch string library
+
=== String Library ===
*Launch math library
+
byte, char, dump, find, format, gmatch, gsub, len, lower, match, rep, reverse, sub, upper
*...
 
*Open and launch User's Lua script
 
*...
 
*Launch var API (store, recall, ...)
 
*Launch platform API (window, gc, ...)
 
*...
 
*Link events (pseudo code)
 
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> <span class="kw1">while</span><span class="br0">(</span>Exit<span class="br0">)</span> <span class="co1">------- Some OS routines here</span> &nbsp; <span class="co1">---- Begin of Event link</span> buffer:captureDataFromKeyPad<span class="br0">(</span><span class="br0">)</span> <span class="co1">--  (N) some underground routines to catch events</span> <span class="kw1">if</span> buffer.charInput ~<span class="sy0">=</span> <span class="st0">""</span> <span class="kw1">then</span> <span class="co1">--  (N)</span> <span class="kw3">on.charIn</span><span class="br0">(</span>buffer.charInput<span class="br0">)</span> buffer.charInput<span class="sy0">=</span> <span class="st0">""</span> <span class="co1">--  (N)</span> <span class="kw1">end</span> <span class="kw1">if</span> buffer.arrowKey ~<span class="sy0">=</span> <span class="st0">""</span> <span class="kw1">then</span> <span class="co1">--  (N)</span> <span class="kw3">on.arrowKey</span><span class="br0">(</span>buffer.arrowKey<span class="br0">)</span> buffer.arrowKey <span class="sy0">=</span> <span class="st0">""</span> <span class="co1">--  (N)</span> <span class="kw1">end</span> <span class="co1">----- etc ... </span> <span class="kw1">if</span> platform.window.isInvalidate <span class="kw1">then</span> platform.gc<span class="br0">(</span><span class="br0">)</span>:purge<span class="br0">(</span><span class="br0">)</span> <span class="co1">--  (N) Empty buffer before starting to draw</span> <span class="kw3">on.paint</span><span class="br0">(</span>platform.gc<span class="br0">(</span><span class="br0">)</span><span class="br0">)</span> <span class="co1">-- save all the things we have to draw</span> platform:paint<span class="br0">(</span><span class="br0">)</span> <span class="co1">--  (N) draw all those things</span> platform.window.isInvalidate <span class="sy0">=</span> <span class="kw1">false</span> <span class="co1">-- say that the window has been drawn</span> <span class="kw1">end</span> <span class="co1">----- End of Event link</span> <span class="kw1">end</span></pre></div></div>
 
''Note&nbsp;: the (N) commented line only indicates the meaning of the routine. Those functions don't really exist.''
 
  
Now we can understand how everything is linked, just by a main loop. This helps you to understand that you don't have to code a loop yourself, because the screen wouldn't be refreshed. This also helps to see when the screen gets refreshed. In other words, we cannot use niether '''gc''' nor '''platform.gc()''' (which are the same) in order to draw somthing on the screen if we are outside of '''on.paint()''' (except if your outside function is called within on.paint() ). This also means that we don't need to pass '''gc''' as parameter, because we can rather use '''platform.gc()''' for any functions called within on.paint(). This is exactly what [https://github.com/adriweb/BetterLuaAPI-for-TI-Nspire/blob/master/BetterLuaAPI.lua the BetterLuaAPI library for Nspire] does.
+
=== Math Library ===
 +
abs, acos, asin, atan, atan2, ceil, cos, cosh, deg, exp, floor, fmod, frexp, huge, ldexp, log, log10, max, min, modf, pi, pow, rad, random, randomseed, sin, sinh, sqrt, tan, tanh
 +
More information about the other libraries can be found on the [[Extended Standard Library]] Page
  
Here is an example of a simple Lua program that displays a message only when a key is pressed (and let the screen blank otherwise).  
+
== [[:Category:D2Editor|D2Editor]] ==
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> <span class="kw1">function</span> <span class="kw3">on.paint</span><span class="br0">(</span>gc<span class="br0">)</span> <span class="kw1">if</span> message <span class="kw1">then</span> <span class="kw2">gc:setFont</span><span class="br0">(</span><span class="st0">"sansserif"</span>, <span class="st0">"r"</span>, <span class="nu0">10</span><span class="br0">)</span> <span class="co1">-- initialize font drawing</span> <span class="kw2">gc:drawString</span><span class="br0">(</span>message, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="st0">"top"</span><span class="br0">)</span> <span class="co1">-- display the message at (0, 0) coordinates</span> message <span class="sy0">=</span> <span class="kw1">nil</span> <span class="co1">-- erase the message</span> <span class="kw2">timer.start</span><span class="br0">(</span><span class="nu0">1</span><span class="br0">)</span> <span class="co1">-- start a timer to exit on.paint() but keep in mind that we have to redraw the screen</span> <span class="kw1">end</span> <span class="kw1">end</span> &nbsp; <span class="kw1">function</span> <span class="kw3">on.timer</span><span class="br0">(</span><span class="br0">)</span> <span class="kw2">timer.stop</span><span class="br0">(</span><span class="br0">)</span> <span class="kw2">platform.window:invalidate<span class="br0">(</span><span class="br0">)</span></span> <span class="kw1">end</span> &nbsp; <span class="kw1">function</span> <span class="kw3">on.charIn</span><span class="br0">(</span>ch<span class="br0">)</span> message <span class="sy0">=</span> <span class="st0">"Hello World&nbsp;!"</span> <span class="co1">-- store a message</span> <span class="kw2">platform.window:invalidate<span class="br0">(</span><span class="br0">)</span></span> <span class="co1">-- force display</span> <span class="kw1">end</span></pre></div></div>
+
The Lua 2D editor bindings enable 2D rich text editors to be created and manipulated within scripts.
When you open the document, the script is read once. It initializes and overwrites all the functions and globals with the ones you defined. Thus, '''message''' is nil. Once the '''on.paint()''' event is called, '''message''' is nil, thus, nothing is done. When you press a key that calls '''on.charIn()''' (see below for more information), '''message''' is now "Hello World" and we tell the '''platform''' that the screen has to be refreshed. Then, '''on.paint()''' is called again, '''message''' is not nil then we display it, erase '''message''' and launch a timer. Why is that&nbsp;? Because if we call '''platform.window:invalidate()''' right there, we won't refresh the screen. Why again&nbsp;? Just look at the pseudo-code above. We set the window as drawn after each call of on.paint(). Thus a timer is necessary to manually recall '''on.paint()''' and exit the '''on.paint()''' function to draw the screen. When the timer is ended, '''on.timer()''' is called and we refresh the screen. The screen is then redrawn but there is nothing to draw because '''message''' is nil. Thus, the graphic context lets the screen blank.  
+
* [[D2Editor:createChemBox]]() : Inserts a Chem Box in the current cursor position of the editor.
 +
* [[D2Editor:createMathBox]]() : Inserts a Math Box (Expression Box) in the current cursor position of the editor.
 +
* [[D2Editor:getExpression]]() : Returns the contents of the text editor as a UTF-8 encoded string.
 +
* [[D2Editor:getExpressionSelection]]() : Returns three values: the contents of the text editor (UTF-8 encoded string), the cursor position, and the selection start.
 +
* [[D2Editor:getText]]() : same as ''getExpression'' : Returns the contents of the text editor as a UTF-8 encoded string.
 +
* [[D2Editor:hasFocus]]() : Returns true if the editor has focus; otherwise returns false.
 +
* [[D2Editor:isVisible]]() : Returns true if the editor is visible; otherwise returns false.
 +
* [[D2Editor:move]]() : Sets the parent-relative location of the upper left corner of the text editor. Both x and y must be between -32767 and 32767.
 +
* [[D2Editor:newRichText]]() : Creates and returns a new 2D rich text editor.
 +
* [[D2Editor:registerFilter]](handlerTable) : This routine registers a table of handler functions that can alter events before they are sent to the 2D editor widget, or unregisters if nil is passed.
 +
* [[D2Editor:resize]](w, h) : Changes the width and height of the text editor. Both width and height must be > 0 and < 32768.
 +
* [[D2Editor:setBorder]](thickness) : Sets the editor's border thickness. The thickness value must be between 0 and 10.
 +
* [[D2Editor:setBorderColor]](color) : Sets the editor's border color. The color value must be between 0 and 16777215 (0x000000 and 0xFFFFFF).
 +
* [[D2Editor:setColorable]](true or false) : Makes the expression colorable or uncolorable.
 +
* [[D2Editor:setDisable2DinRT]](true or false) : Turns on/off 2D layout of math input to the text box.
 +
* [[D2Editor:setExpression]](...) : Sets the text content of the text editor.
 +
* [[D2Editor:setFocus]](true or false) : Sets the user input focus on the editor if true (the default). This is usually called from the [[on.getFocus]] event handler.
 +
* [[D2Editor:setFontSize]](size) : Sets the text font size in the editor. Only 7, 9, 10, 11, 12, or 24 for the handheld, any on the desktop software.
 +
* [[D2Editor:setMainFont]](family, style) : Sets the main font family ("serif" or "sansserif") and style ("r", "b", "i", "bi").
 +
* [[D2Editor:setReadOnly]](true or false) : Makes the text editor content modifiable (false) or unmodifiable (true) by the user. If a Boolean value is not specified, defaults to true.
 +
* [[D2Editor:setSelectable]](true or false) : Makes the text editor content selectable (true) or unselectable (false) by the user. If a Boolean value is not specified, defaults to true.
 +
* [[D2Editor:setSizeChangeListener]](function(editor, w, h)) : Sets the callback function for when the editor contents exceed the current editor size, when the contents fit on fewer lines, or when the contents fit on a single line of smaller width.
 +
* [[D2Editor:setTextChangeListener]](function(editor)) : Sets the callback function for when the text expression changes, which will be to the editor object. This allows for processing text input as it occurs.
 +
* [[D2Editor:setText]](...) : Same as setExpression : Sets the text content of the text editor.
 +
* [[D2Editor:setTextColor]](color) : Sets the editor text color. The color value must be between 0 and 16777215 (0x000000 and 0xFFFFFF).
 +
* [[D2Editor:setVisible]](true or false) : Sets the visibility of the text editor.
 +
* [[D2Editor:setWordWrapWidth]](width) : Sets the rich text editor word-wrapping width in pixels. Ignored if the editor is in 2D mode. To indicate widget width, sets to 0. To disable wrapping, sets to < 0. The width must be -32767 to 32767.
  
== gc (as in Graphics Context) ==
+
== [[:Category:Events|Events]] ==
 +
Events are one of the most fundamental things to know about the Nspire Lua language.
 +
You can see how events are managed inside the Lua implementation [[Life Cycle|here]].
  
Note: You need to add “gc:” before each of these commands in order to use them. ex. '''gc:setAlpha(...)'''. You can also call gc like this&nbsp;: '''platform.gc():setAlpha(...)'''. Then you can use the gc library in a function where gc is not passed as an argument. but this function *has* to be called within '''on.paint(gc)'''  
+
*'''[[on.paint]]'''(gc) is called when the GUI is painted. [[:Category:gc|'gc' is the Graphics Context]]
 +
*'''[[on.resize]]''' is called when the window is rezised
 +
*'''[[on.timer]]''' is called at each timer ticks. See [[:Category:timer|timer]].<br>
 +
*'''[[on.arrowKey]]''' is called when an '''arrow key''' from the clickPad/TouchPad is pressed (right, left, up, down)
 +
*'''[[on.arrowLeft]]''' is called when the '''left''' arrow is pressed
 +
*'''[[on.arrowRight]]''' is called when the '''right''' arrow is pressed
 +
*'''[[on.arrowUp]]''' is called when the up '''arrow''' is pressed
 +
*'''[[on.arrowDown]]''' is called when the '''down''' arrow is pressed
 +
*'''[[on.enterKey]]''' is called when the '''enter''' key is pressed.
 +
*'''[[on.escapeKey]]''' is called when the '''escape''' key is pressed.
 +
*'''[[on.tabKey]]''' is called when the '''tab''' key is pressed.
 +
*'''[[on.deleteKey]]''' is called when the '''delete''' key is pressed.
 +
*'''[[on.backspaceKey]]''' is called when the '''clear''' key is pressed.
 +
*'''[[on.returnKey]]''' is called when the '''return''' key is pressed.
 +
*'''[[on.contextMenu]]''' is called when the combo-key '''Ctrl Menu''' is pressed.
 +
*'''[[on.backtabKey]]''' is called when the combo-key '''Maj Tab''' is pressed.
 +
*'''[[on.clearKey]]''' is called when the combo-key '''Ctrl Clear''' is pressed.
 +
*'''[[on.help]]''' is called when the combo-key '''Ctrl ?''' is pressed.
 +
*'''[[on.charIn]]''' is called when the Nspire detects a non arrow key being pressed.
 +
*'''[[on.createMathBox]]''' called when there is a MathBox created.
 +
*'''[[on.create]]''' called when the script gets created. '''Deprecated in 3.2'''. See ''on.contruction''.
 +
*'''[[on.construction]]''' called when the script gets created.  
 +
*'''[[on.deactivate]]''' called when the focus is lost on the page (like launching the document, changing page etc...)  
 +
*'''[[on.activate]]''' called when the focus is on the page (like launching the document, changing page etc...)
 +
*'''[[on.destroy]]''' is called when the document is about to get closed (or cut)
 +
*'''[[on.getFocus]]''' is called when the script receives user input focus. 
 +
*'''[[on.loseFocus]]''' is called when the script loses user input focus.
 +
*'''[[on.mouseDown]]''' is called when we press the left mouse button. X and Y are the pressed point coordinates.
 +
*'''[[on.mouseUp]]''' is called when we release the left mouse button.
 +
*'''[[on.mouseMove]]''' is called when the mouse moves
 +
*'''[[on.grabDown]]''' is called when the grabbing motion is detected
 +
*'''[[on.grabUp]]''' is called after the grabbing action.
 +
*'''[[on.rightMouseDown]]''' is called when the user pushes the "right click" button
 +
*'''[[on.rightMouseUp]]''' is called when the user releases the "right click" button
 +
*'''[[on.keyboardUp]]'''() which fires when the tablet's on-screen keyboard gets shown.
 +
*'''[[on.keyboardDown]]'''() which fires when the tablet's on-screen keyboard gets hidden.
 +
*'''[[on.varChange]]''' is called when a [[var.monitor|monitored variable]] gets changed.
 +
*'''[[on.getSymbolList]]''' is called when the script app symbol list is being serialized to the clipboard.
 +
*'''[[on.save]]''' is called when the script app is saved to the document or copied to the clipboard.
 +
*'''[[on.restore]]''' is called when the script application is restored from its saved state in a document or when the app is pasted into a document..
 +
*'''[[on.cut]]'''  is called when the user cuts something.
 +
*'''[[on.copy]]''' is called when the user copies something.
 +
*'''[[on.paste]]''' is called when the user pastes something.
  
The maximum screen resolution available to Lua programs is 318 by 212 pixels.  
+
== [[:Category:gc|Graphics Operations]]  ==
 +
'''gc''' is the Nspire's ''graphic context''. A graphics context is a module that has a handle to the script’s graphics output window and a library of graphics routines that are used to draw on the window. A graphics context is supplied to the script ”on.paint” event handler each time the window needs to be redrawn. The graphics context employs a pixel-based coordinate system with the origin in the upper left corner of the drawing window.
  
*'''[[gc:begin]]''' -
+
*'''[[gc:clipRect]]'''(op, x, y, width, height) - Sets the clipping rectangle for subsequent graphics operations.
*'''[[gc:clipRect]]''' -  
+
*'''[[gc:drawArc]]'''(x, y, width, height, start angle, finish angle).
*'''[[gc:drawArc]]'''(x, y, width, height, start angle, finish angle) Note, to draw a circle, use drawArc(x - diameter/2, y - diameter/2, diameter,diameter,0,360) where x and y are the coordinates of the middle.  
+
*'''[[gc:drawImage]]'''(image,x,y) First argument in format “[[TI.Image]]”, x and y the coords.
*'''[[gc:drawImage]]'''(image,x,y) First argument in format “[[TI.Image]]”, x and y the coords.  
+
*'''[[gc:drawLine]]'''(xstart, ystart, xend, yend) Draws a line starting at the point (xstart,ystart) and ending at the point (xend, yend)
*'''[[gc:drawLine]]'''(xstart, ystart, xend, yend) Draws a line starting at the point (xstart,ystart) and ending at the point (xend, yend)  
 
 
*'''[[gc:drawPolyLine]]'''(int list1 [,int list2, .., int listN]) Draws a shape from a list contaning successively the x and y coordinates of each point the line have to draw.
 
*'''[[gc:drawPolyLine]]'''(int list1 [,int list2, .., int listN]) Draws a shape from a list contaning successively the x and y coordinates of each point the line have to draw.
 +
*'''[[gc:drawRect]]'''(x, y, xwidth, yheight) Draws a rectangle at (x,y) with the “x” side being “xwidth” long and the “y” side being “yheight” long
 +
*'''[[gc:drawString]]'''(string, x, y, position) position is the string’s anchor point and can be "baseline", “bottom”, “middle”, or “top”.
 +
*'''[[gc:fillArc]]'''(x, y, width, height, start angle, finish angle) see [[gc:drawArc]]
 +
*'''[[gc:fillPolygon]]'''(int list1 [,int list2, .., int listN]) see [[gc:drawPolyLine]]
 +
*'''[[gc:fillRect]]'''(x, y, width, height) see [[gc:drawRect]]
 +
*'''[[gc:getStringHeight]]'''(string) - Returns the string's height.
 +
*'''[[gc:getStringWidth]]'''(string) - Returns the string's width
 +
*'''[[gc:setAlpha]]'''(alpha) - where alpha is an integer between 0 and 255. Sets the transparency. '''''Not available anymore in 3.2+'''''
 +
*'''[[gc:setColorRGB]]'''(red, green, blue) RGB values are integers, from 0 to 255.
 +
*'''[[gc:setFont]]'''(font, type, size), with font : {“sansserif”, "serif", ..}, type {“b”, “r”, “i”}, size(int)
 +
*'''[[gc:setPen]]'''(thickness, smoothness) : thickness {“thin”, “medium”, "thick"}, smoothness {“smooth”, "dotted", "dashed"}
  
For example
+
== [[:Category:image|Images]] ==
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> drawPolyLine<span class="br0">(</span><span class="br0">{</span><span class="nu0">0</span>,<span class="nu0">0</span>, <span class="nu0">0</span>,<span class="nu0">100</span>, <span class="nu0">100</span>,<span class="nu0">100</span>, <span class="nu0">100</span>,<span class="nu0">0</span>, <span class="nu0">0</span>,<span class="nu0">0</span><span class="br0">}</span><span class="br0">)</span></pre></div></div>
+
*'''[[image.copy]]'''(theImage, width, height) : Returns a copy of the input image scaled to fit the specified pixel width and height.  
and
+
*'''[[image.height]]'''(theImage) : Returns the height of the ''image''
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> drawRect<span class="br0">(</span><span class="nu0">0</span>,<span class="nu0">0</span>,<span class="nu0">100</span>,<span class="nu0">100</span><span class="br0">)</span></pre></div></div>
+
*'''[[image.new]]'''(theImage) : Allocates a new [[TI.Image]]
do the same. If there are multiple argument (which can be 4 elements list to represent lines), each list has to contain an even number of element.
+
*'''[[image.rotate]]'''(theImage, angle) : Returns a rotated copy of ''theImage'' by ''angle'' degrees
 
+
*'''[[image.width]]'''(theImage) : Returns the width of the ''image''
*'''[[gc:drawRect]]'''(x, y, xwidth, yheight) Draws a rectangle at (x,y) with the “x” side being “xwidth” long and the “y” side being “yheight” long
 
*'''[[gc:drawString]]'''(string, x, y, PositionString) PositionString is the string’s anchor point and can be “bottom”, “middle”, or “top”.
 
*'''[[gc:fillArc]]'''(x, y, width, height, start angle, finish angle) see drawArc
 
*'''[[gc:fillPolygon]]'''(int list1 [,int list2, .., int listN]) see drawPolyLine
 
*'''[[gc:fillRect]]'''(x, y, width, height) see drawRect
 
*'''[[gc:finish]]''' -
 
*'''[[gc:getStringHeight]]'''(string)
 
*'''[[gc:getStringWidth]]'''(string)
 
*'''[[gc:isColorDisplay]]''' Bool (Read-only) Returns 1 if color, 0 if not.
 
*'''[[gc:setAlpha]]'''(int) - where the argument is an int between 0 and 255. Sets the transparency.
 
*'''[[gc:setColorRGB]]'''(red, green, blue) RGB values are integers, from 0 to 255.
 
*'''[[gc:setFont]]'''(font, type, size), with font&nbsp;: {“sansserif”, "serif", ..}, type {“b”, “r”, “i”}, size(int)
 
*'''[[gc:setPen]]'''(size, smooth) size {“thin”, “medium”, "thick"}, smooth {“smooth”, "dotted", "dashed"}
 
 
 
== platform  ==
 
 
 
These are mainly read-only. These work by writing "'''platform.'''" in front of them. Example&nbsp;: "'''platform.window:invalidate()'''"
 
 
 
*'''[[platform.apilevel()]]'''&nbsp;: Returns the version of the API. Currently (OS 3.0.1) returns 1.0.0.
 
*'''[[platform.window]]'''
 
 
 
:*'''[[platform.window:width()]]''' - Returns the width of the window. Ex&nbsp;: ''platform.window:height()''
 
:*'''[[platform.window:height()]]''' - Returns the height of the window
 
:*'''[[platform.window:invalidate()]]''' - Repaints the window (it calls '''on.paint(gc)''')
 
 
 
*'''[[platform.isDeviceModeRendering()]]''' Returns true or false whether the unit is "rendering" or not
 
*'''[[platform.gc]]''' - Other way to call '''gc'''. Use it like that&nbsp;: '''platform.gc():setAlpha(...)''' for example. This is useful when you're in a function outside '''on.paint(gc)''' (but this function has to be called within [[on.paint]](gc).)
 
*'''[[platform.isColorDisplay()]]''' Returns ''true'' if the unit the code is being run on has a color display (-&gt; Nspire CX), and ''false'' otherwise.
 
 
 
== cursor ==
 
 
 
*'''[[cursor.hide]]'''() - hides the cursor (mouse pointer)
 
*'''[[cursor.set]]'''(string) - string can be one of those&nbsp;: (interrogation, crosshair, text, pointer, link select, diag resize, wait busy, hollow pointer, rotation, pencil, zoom box, arrow, zoom out, dotted arrow, clear, animate, excel plus, mod label, writing, unavailable, resize row, resize column, drag grab, hand open, hand close, hand pointer, zoom in, dilation, translation).
 
*'''[[cursor.show]]'''() - Shows the cursor on screen
 
 
 
== document ==
 
 
 
*'''[[document.markChanged]]'''() - Flag the document as "changed" so the user has to save it after using it.
 
 
 
== clipboard ==
 
 
 
*'''[[clipboard.addText]]'''()
 
*'''[[clipboard.getText]]'''()
 
 
 
== locale ==
 
 
 
*'''[[locale.name]]'''() - Returns the current language the calculator is set in, formatted as ISO-639 (i.e&nbsp;: "fr", "en", "it" ...).
 
 
 
== image ==
 
 
 
*'''[[image.copy]]'''(image, width, height) - returns a new Image which has a new scale. Width is the final width, Heigth, the final height of the image.
 
*'''[[image.height]]'''(image) - returns the height of the image given in parameter
 
*'''[[image.new]]'''(string) - Creates a new image from the string given in parameter (see TI.Image). Call it with <tt>img = image.new("...")</tt>
 
*'''[[image.width]]'''(image) - returns the width of the image given in parameter
 
 
 
== timer ==
 
 
 
*'''[[timer.getMilliSecCounter]]'''() - Returns the amount of milliseconds elapsed since last calculator reboot. (in TI's Computer software, returns an absolute negative time)
 
*'''[[timer.start]]'''(int) - Starts a timer with 1 second.
 
*'''[[timer.stop]]'''() - Stops a timer
 
 
 
== toolpalette ==
 
 
 
*'''[[toolpalette.enable]]'''(string)
 
*'''[[toolpalette.enableCopy]]'''()
 
*'''[[toolpalette.enableCut]]'''()
 
*'''[[toolpalette.enablePaste]]'''()
 
*'''[[toolpalette.register]]'''(table)
 
 
 
== var ==
 
 
 
*'''[[var.list]]'''() - returns a list of all the variables in the entire Activity
 
*'''[[var.monitor]]'''() -&nbsp;?
 
*'''[[var.recall]]'''(string) - returns the variable value which name is given in parameter
 
*'''[[var.recallstr]]'''(string) - returns the variable value converted to string which name is given in parameter
 
*'''[[var.store]]'''(string, value) - store in the variable, which name is given in parameter, the value
 
*'''[[var.unmonitor]]'''() -&nbsp;?
 
 
 
== Events ==
 
 
 
*'''[[on.paint]]'''(gc) is called when the GUI is painted. 'gc' is the Graphics Context (see above)
 
*'''[[on.resize]]'''() is called when the window is rezised
 
*'''[[on.timer]]'''() is called when the timer has been finished. Here an example of using timer to play an animation&nbsp;:
 
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> x <span class="sy0">=</span> <span class="nu0">1</span> animating <span class="sy0">=</span> <span class="kw1">false</span> <span class="kw1">function</span> <span class="kw3">on.paint</span><span class="br0">(</span>gc<span class="br0">)</span> <span class="kw2">gc:setFont</span><span class="br0">(</span><span class="st0">"sansserif"</span>, <span class="st0">"r"</span>, <span class="nu0">10</span><span class="br0">)</span> <span class="kw2">gc:drawString</span><span class="br0">(</span><span class="kw1">tostring</span><span class="br0">(</span>x<span class="br0">)</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="st0">"top"</span><span class="br0">)</span> <span class="kw1">if</span> animating <span class="kw1">then</span> x <span class="sy0">=</span> x + <span class="nu0">1</span> <span class="kw2">timer.start</span><span class="br0">(</span><span class="nu0">0.5</span><span class="br0">)</span> <span class="kw1">end</span> <span class="kw1">end</span> &nbsp; <span class="kw1">function</span> <span class="kw3">on.charIn</span><span class="br0">(</span>ch<span class="br0">)</span> animating <span class="sy0">=</span> <span class="kw1">not</span> animating <span class="co1">-- switch state</span> <span class="kw2">platform.window:invalidate<span class="br0">(</span><span class="br0">)</span></span> <span class="co1">-- recall graph engine</span> <span class="kw1">end</span> &nbsp; <span class="kw1">function</span> <span class="kw3">on.timer</span><span class="br0">(</span><span class="br0">)</span> <span class="kw2">timer.stop</span><span class="br0">(</span><span class="br0">)</span> <span class="kw2">platform.window:invalidate<span class="br0">(</span><span class="br0">)</span></span> <span class="co1">-- recall graph engine</span> <span class="kw1">end</span></pre></div></div>
 
----
 
 
 
*'''[[on.arrowKey]]'''(key) is called when an '''arrow key''' from the clickPad/TouchPad is pressed (right, left, up, down)
 
*'''[[on.arrowLeft]]'''() is called when the '''left''' arrow is pressed
 
*'''[[on.arrowRight]]'''() is called when the '''right''' arrow is pressed
 
*'''[[on.arrowUp]]'''() is called when the up '''arrow''' is pressed
 
*'''[[on.arrowDown]]'''() is called when the '''down''' arrow is pressed
 
 
 
----
 
 
 
*'''[[on.up]]'''() -- probably for the touchpad up motion
 
*'''[[on.down]]'''() -- probably for the touchpad down motion
 
 
 
----
 
 
 
*'''[[on.enterKey]]'''() is called when the '''enter''' key is pressed.
 
*'''[[on.escapeKey]]'''() is called when the '''escape''' key is pressed.
 
*'''[[on.tabKey]]'''() is called when the '''tab''' key is pressed.
 
*'''[[on.deleteKey]]'''() is called when the '''delete''' key is pressed.
 
*'''[[on.backspaceKey]]'''() is called when the '''clear''' key is pressed.
 
*'''[[on.returnKey]]'''() is called when the '''return''' key is pressed.
 
*'''[[on.contextMenu]]'''() is called when the combo-key '''Ctrl Menu''' is pressed.
 
*'''[[on.backtabKey]]'''() is called when the combo-key '''Maj Tab''' is pressed.
 
*'''[[on.clearKey]]'''() is called when the combo-key '''Ctrl Clear''' is pressed.
 
*'''[[on.help]]'''() is called when the combo-key '''Ctrl H''' is pressed.
 
 
 
*'''[[on.charIn]]'''(string) is called when the Nspire detects a non arrow key being pressed. ch is the character it detect. If you want to auto start an event in the file linked to key r(like a reset) put on.charIn(“r”) where you want. This Lua program lets you display the value of a valid non arrow key&nbsp;:
 
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> c <span class="sy0">=</span> <span class="st0">""</span> <span class="kw1">function</span> <span class="kw3">on.charIn</span><span class="br0">(</span>ch<span class="br0">)</span> c <span class="sy0">=</span> ch <span class="kw2">platform.window:invalidate<span class="br0">(</span><span class="br0">)</span></span> <span class="co1">-- we force the screen draw</span> <span class="kw1">end</span> <span class="kw1">function</span> <span class="kw3">on.paint</span><span class="br0">(</span>gc<span class="br0">)</span> <span class="kw2">gc:setFont</span><span class="br0">(</span>“sansserif”, <span class="st0">"r"</span>, <span class="nu0">10</span><span class="br0">)</span> <span class="kw2">gc:drawString</span><span class="br0">(</span>c, <span class="nu0">0</span>, <span class="nu0">0</span>, <span class="st0">"top"</span><span class="br0">)</span> <span class="kw1">end</span></pre></div></div>
 
----
 
 
 
*'''[[on.blink]]'''()&nbsp;? is called when the focus is lost on the page (like launching the document, changing page etc...)
 
 
 
----
 
 
 
*'''[[on.deactivate]]'''()&nbsp;? is called when the focus is lost on the page (like launching the document, changing page etc...)
 
*'''[[on.activate]]'''()&nbsp;? is called when the focus is on the page (like launching the document, changing page etc...)
 
 
 
----
 
 
 
*'''[[on.mouseDown]]'''(x, y) is called when we press the left mouse button. X and Y are the pressed point coordinates.
 
*'''[[on.mouseUp]]'''() is called when we release the left mouse button.
 
*'''[[on.mouseMove]]'''() is called when the mouse moves
 
*'''[[on.grabDown]]'''()&nbsp;? (software only&nbsp;?)
 
*'''[[on.grabMove]]'''()&nbsp;? (software only&nbsp;?)
 
*'''[[on.grabUp]]'''()&nbsp;? (software only&nbsp;?)
 
*'''[[on.rightMouseDown]]'''()&nbsp;? (software only&nbsp;?)
 
*'''[[on.rightMouseUp]]'''()&nbsp;? (software only&nbsp;?)
 
 
 
----
 
 
 
*'''[[on.cutEnabled]]'''()&nbsp;? (software only&nbsp;?)
 
*'''[[on.copyEnabled]]'''()&nbsp;? (software only&nbsp;?)
 
*'''[[on.pasteEnabled]]'''()&nbsp;? (software only&nbsp;?)
 
*'''[[on.cut]]'''()&nbsp;? (software only&nbsp;?)
 
*'''[[on.copy]]'''()&nbsp;? (software only&nbsp;?)
 
*'''[[on.paste]]'''()&nbsp;? (software only&nbsp;?)
 
 
 
----
 
  
== D2Editor ==
+
== [[:Category:Physics_Engine|Physics Engine]] ==
 +
A port of Chipmunk Physics 5.3 has been embedded as an optional module (you have to 'require' it at the beginning of your script, in order to use it)
 +
*Arbiters and Collision Pairs
 +
*Bodies
 +
*Bounding Boxes
 +
*Circle Shapes
 +
*Constraints
 +
*Misc
 +
*Polygon Shapes
 +
*Segment Shapes
 +
*SegmentQueryInfo
 +
*Shape Queries
 +
*Shapes
 +
*Space Queries
 +
*Spaces
 +
*Vectors
  
*'''[[D2Editor.newRichText]]'''() creates a new RichText object (default values&nbsp;: x, y, width, height = 0)  
+
== [[:Category:platform|Platform]] ==
*'''[[D2Editor.resize]]'''(width, height)
+
*[[platform.isColorDisplay]]() : Returns true if the display of the host platform is color-capable (Nspire CX and emulator). Returns false if the display is grayscale (Nspire non-CX).     
*'''[[D2Editor.move]]'''(x, y)  
+
*[[platform.isDeviceModeRendering]]() : Returns true if the script is running on the hand-held device or in the emulator (with the calculator view), and false if the script is running in the normal view of the emulator.
*'''[[D2Editor.setText]]'''(string)
+
*[[platform.isTabletModeRendering]]() : Returns true if the script is running on a tablet device (iPad app) and false otherwise.
*'''[[D2Editor.getText]]'''() returns the RichText value
+
*[[:Category:platform.window|platform.window]] : Returns the window object currently owned by the script application. The window object contains several other methods.
*'''[[D2Editor.setReadOnly]]'''(bool) bool&nbsp;: true/false&nbsp;: If true, the content becomes read-only, i.e. non-editable.  
+
*[[platform.withGC]](...) : executes a function which a passed dummy [[gc]].
*'''[[D2Editor.setFormattedText]]'''() -&nbsp;?
+
*[[platform.registerErrorHandler]](...) : A global error handler for the lua script.
 +
*[[platform.hw]]() : returns a number corresponding to the type of device the script is running on.
  
Example of a valid function using the D2Editor
+
== [[:Category:timer|Timer]] ==
<div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> <span class="kw1">function</span> createRichTextBox box <span class="sy0">=</span> <span class="kw2">D2Editor.newRichText</span><span class="br0">(</span><span class="br0">)</span> box:move<span class="br0">(</span><span class="nu0">50</span>, <span class="nu0">50</span><span class="br0">)</span> box:resize<span class="br0">(</span><span class="nu0">50</span>, <span class="nu0">50</span><span class="br0">)</span> box:setText<span class="br0">(</span><span class="st0">"Hello World&nbsp;!"</span><span class="br0">)</span> <span class="kw1">end</span></pre></div></div>
+
*'''[[timer.start]]'''(period) : Starts the timer with the given ''period'' (in seconds)<br />
== External Links ==
+
*'''[[timer.getMilliSecCounter]]'''() : Returns the value of the milliseconds elapsed since the calculator's last boot.<br />
 +
*'''[[timer.stop]]'''() : Stops the timer.
  
An interesting page about LUA code optimization, that can be really useful, especially for calculators&nbsp;: [http://trac.caspring.org/wiki/LuaPerformance Lua Performance]
+
== [[:Category:toolpalette|Toolpalette]] ==
 +
The tool palette provides a menu of commands, selectable by the user, that invoke functionality of the script app. The menu itself is accessible by pressing the "Menu" button.
 +
*'''[[toolpalette.register|register]]'''(menuStructure) :  Registers the ''menuStructure'' table.
 +
*'''[[toolpalette.enable|enable]]'''(toolname, itemname, state) : Enables or disables a menu item in the tool palette.   
 +
*'''[[toolpalette.enableCopy|enableCopy]]'''(state) : This routine enables / disables the Edit > Copy menu command.
 +
*'''[[toolpalette.enableCut|enableCut]]'''(state) : This routine enables / disables the Edit > Cut menu command.
 +
*'''[[toolpalette.enablePaste|enablePaste]]'''(state) : This routine enables / disables the Edit > Paste menu command.
  
== Online Demo ==
+
== [[:Category:var|var]]  ==
 +
'var' functions allows the script to interact with BASIC variables.
 +
*[[var.list]]() - Lists of names of variables currently used.
 +
*[[var.monitor]](name) - Turns on monitoring of the math variable with given ''name''.
 +
*[[var.unmonitor]](name) - Turns off monitoring of the math variable with given ''name''.
 +
*[[var.makeNumericList]](name)&nbsp;: Creates a list in the symbol table with the given ''name'', which is optimized to hold numeric values.
 +
*[[var.recall]](name)&nbsp;: Returns the value of a math variable with the given ''name''.
 +
*[[var.recallAt]](name, col [,row])&nbsp;: Recalls a value from a cell of a list or matrix in the symbol table.
 +
*[[var.recallstr]](name)&nbsp;: Returns the value of a math variable with the given name as a string.
 +
*[[var.store]](name, value)&nbsp;: Stores ''value'' as a math variable with the given ''name''.
 +
*[[var.storeAt]](name, numericValue, col [,row])&nbsp;: Stores a numeric value into an element of a math list or matrix with the given ''name''.
  
[[Document Player]]
+
== Misc. ==
 +
*'''[[locale.name]]'''() : Returns the name of the current locale as an ISO-639 code.
 +
*'''[[clipboard.addText]]'''(string) : Add 'string' to the clipboard
 +
*'''[[clipboard.getText]]'''() : Get the current content of the clipboard
 +
*'''[[cursor.hide]]'''() :  Hides the cursor on the screen
 +
*'''[[cursor.set]]'''(cursorname) : Sets the new shape of the cursor to ''cursorname'' (see full list in the specific article)
 +
*'''[[cursor.show]]'''() : Makes the cursor visible on the screen
 +
*'''[[document.markChanged]]'''()&nbsp;: Mark the current document as changed

Latest revision as of 21:28, 8 November 2014

Note: The Lua version that comes with OS 3.x is 5.1.4, without the io nor os libraries.

Tutorials on Nspire Lua scripting in general are available here : http://inspired-lua.org/

Standard Library

( This has been taken from a WoWWiki page - See the full, official documentation here )

Common Lua Functions

  • G - Global Variable - A global variable (not a function) that holds the global environment (that is, _G._G = _G). Lua itself does not use this variable; changing its value does not affect any environment, nor vice-versa. (Use setfenv to change environments.) - taken from Lua docs
  • assert(value[, errormsg]) - asserts a value evaluates to true. If it is, returns value, otherwise causes a Lua error to be thrown.
  • collectgarbage - among other things, force free unused memory.
  • error(str) - generates an error with the str message.
  • getfenv(function or integer) - Returns the table representing the stack frame of the given function or stack level.
  • getmetatable(obj, mtable) - Returns the metatable of the given table or userdata object.
  • next(table, index) - Returns the next key, value pair of the table, allowing you to walk over the table.
  • newproxy(boolean or proxy) - Creates a userdata with a sharable metatable.
  • print(...) - Receives any number of arguments, and prints their values. Not intended for formatted output, but only as a quick way to show a value, typically for debugging. For formatted output, use string.format.
  • select(index, list) - Returns the number of items in list or the value of the item in list at index.
  • setfenv(function or integer, table) - Sets the table representing the stack frame of the given function or stack level.
  • setmetatable(obj, mtable) - Sets the metatable of the given table or userdata object.
  • tostring - tostring (e) - Receives an argument of any type and converts it to a string in a reasonable format. For complete control of how numbers are converted, use string.format. If the metatable of e has a "__tostring" field, then tostring calls the corresponding value with e as argument, and uses the result of the call as its result. - taken from Lua Docs.
  • tonumber - tonumber (e) - Receives an argument of the string type and converts it to a number when possible. If the metatable of e has a "__tonumber" field, then tonumber calls the corresponding value with e as argument, and uses the result of the call as its result. - taken from Lua Docs.
  • type(var) - Returns the type of variable as a string, "number", "string", "table", "function" or "userdata".
  • unpack(table[, start][, end]) - Returns the contents of its argument as separate values.
  • xpcall(func, err) - Returns a boolean indicating successful execution of func and calls err on failure, additionally returning func's or err's results.

Coroutines Library

The coroutines are useful tools to control the running of differents parts of a code ("threads").

create, wrap, resume, yield, status, running

More information can be fouded in the coroutines page.

String Library

byte, char, dump, find, format, gmatch, gsub, len, lower, match, rep, reverse, sub, upper

Math Library

abs, acos, asin, atan, atan2, ceil, cos, cosh, deg, exp, floor, fmod, frexp, huge, ldexp, log, log10, max, min, modf, pi, pow, rad, random, randomseed, sin, sinh, sqrt, tan, tanh More information about the other libraries can be found on the Extended Standard Library Page

D2Editor

The Lua 2D editor bindings enable 2D rich text editors to be created and manipulated within scripts.

  • D2Editor:createChemBox() : Inserts a Chem Box in the current cursor position of the editor.
  • D2Editor:createMathBox() : Inserts a Math Box (Expression Box) in the current cursor position of the editor.
  • D2Editor:getExpression() : Returns the contents of the text editor as a UTF-8 encoded string.
  • D2Editor:getExpressionSelection() : Returns three values: the contents of the text editor (UTF-8 encoded string), the cursor position, and the selection start.
  • D2Editor:getText() : same as getExpression : Returns the contents of the text editor as a UTF-8 encoded string.
  • D2Editor:hasFocus() : Returns true if the editor has focus; otherwise returns false.
  • D2Editor:isVisible() : Returns true if the editor is visible; otherwise returns false.
  • D2Editor:move() : Sets the parent-relative location of the upper left corner of the text editor. Both x and y must be between -32767 and 32767.
  • D2Editor:newRichText() : Creates and returns a new 2D rich text editor.
  • D2Editor:registerFilter(handlerTable) : This routine registers a table of handler functions that can alter events before they are sent to the 2D editor widget, or unregisters if nil is passed.
  • D2Editor:resize(w, h) : Changes the width and height of the text editor. Both width and height must be > 0 and < 32768.
  • D2Editor:setBorder(thickness) : Sets the editor's border thickness. The thickness value must be between 0 and 10.
  • D2Editor:setBorderColor(color) : Sets the editor's border color. The color value must be between 0 and 16777215 (0x000000 and 0xFFFFFF).
  • D2Editor:setColorable(true or false) : Makes the expression colorable or uncolorable.
  • D2Editor:setDisable2DinRT(true or false) : Turns on/off 2D layout of math input to the text box.
  • D2Editor:setExpression(...) : Sets the text content of the text editor.
  • D2Editor:setFocus(true or false) : Sets the user input focus on the editor if true (the default). This is usually called from the on.getFocus event handler.
  • D2Editor:setFontSize(size) : Sets the text font size in the editor. Only 7, 9, 10, 11, 12, or 24 for the handheld, any on the desktop software.
  • D2Editor:setMainFont(family, style) : Sets the main font family ("serif" or "sansserif") and style ("r", "b", "i", "bi").
  • D2Editor:setReadOnly(true or false) : Makes the text editor content modifiable (false) or unmodifiable (true) by the user. If a Boolean value is not specified, defaults to true.
  • D2Editor:setSelectable(true or false) : Makes the text editor content selectable (true) or unselectable (false) by the user. If a Boolean value is not specified, defaults to true.
  • D2Editor:setSizeChangeListener(function(editor, w, h)) : Sets the callback function for when the editor contents exceed the current editor size, when the contents fit on fewer lines, or when the contents fit on a single line of smaller width.
  • D2Editor:setTextChangeListener(function(editor)) : Sets the callback function for when the text expression changes, which will be to the editor object. This allows for processing text input as it occurs.
  • D2Editor:setText(...) : Same as setExpression : Sets the text content of the text editor.
  • D2Editor:setTextColor(color) : Sets the editor text color. The color value must be between 0 and 16777215 (0x000000 and 0xFFFFFF).
  • D2Editor:setVisible(true or false) : Sets the visibility of the text editor.
  • D2Editor:setWordWrapWidth(width) : Sets the rich text editor word-wrapping width in pixels. Ignored if the editor is in 2D mode. To indicate widget width, sets to 0. To disable wrapping, sets to < 0. The width must be -32767 to 32767.

Events

Events are one of the most fundamental things to know about the Nspire Lua language. You can see how events are managed inside the Lua implementation here.

  • on.paint(gc) is called when the GUI is painted. 'gc' is the Graphics Context
  • on.resize is called when the window is rezised
  • on.timer is called at each timer ticks. See timer.
  • on.arrowKey is called when an arrow key from the clickPad/TouchPad is pressed (right, left, up, down)
  • on.arrowLeft is called when the left arrow is pressed
  • on.arrowRight is called when the right arrow is pressed
  • on.arrowUp is called when the up arrow is pressed
  • on.arrowDown is called when the down arrow is pressed
  • on.enterKey is called when the enter key is pressed.
  • on.escapeKey is called when the escape key is pressed.
  • on.tabKey is called when the tab key is pressed.
  • on.deleteKey is called when the delete key is pressed.
  • on.backspaceKey is called when the clear key is pressed.
  • on.returnKey is called when the return key is pressed.
  • on.contextMenu is called when the combo-key Ctrl Menu is pressed.
  • on.backtabKey is called when the combo-key Maj Tab is pressed.
  • on.clearKey is called when the combo-key Ctrl Clear is pressed.
  • on.help is called when the combo-key Ctrl ? is pressed.
  • on.charIn is called when the Nspire detects a non arrow key being pressed.
  • on.createMathBox called when there is a MathBox created.
  • on.create called when the script gets created. Deprecated in 3.2. See on.contruction.
  • on.construction called when the script gets created.
  • on.deactivate called when the focus is lost on the page (like launching the document, changing page etc...)
  • on.activate called when the focus is on the page (like launching the document, changing page etc...)
  • on.destroy is called when the document is about to get closed (or cut)
  • on.getFocus is called when the script receives user input focus.
  • on.loseFocus is called when the script loses user input focus.
  • on.mouseDown is called when we press the left mouse button. X and Y are the pressed point coordinates.
  • on.mouseUp is called when we release the left mouse button.
  • on.mouseMove is called when the mouse moves
  • on.grabDown is called when the grabbing motion is detected
  • on.grabUp is called after the grabbing action.
  • on.rightMouseDown is called when the user pushes the "right click" button
  • on.rightMouseUp is called when the user releases the "right click" button
  • on.keyboardUp() which fires when the tablet's on-screen keyboard gets shown.
  • on.keyboardDown() which fires when the tablet's on-screen keyboard gets hidden.
  • on.varChange is called when a monitored variable gets changed.
  • on.getSymbolList is called when the script app symbol list is being serialized to the clipboard.
  • on.save is called when the script app is saved to the document or copied to the clipboard.
  • on.restore is called when the script application is restored from its saved state in a document or when the app is pasted into a document..
  • on.cut is called when the user cuts something.
  • on.copy is called when the user copies something.
  • on.paste is called when the user pastes something.

Graphics Operations

gc is the Nspire's graphic context. A graphics context is a module that has a handle to the script’s graphics output window and a library of graphics routines that are used to draw on the window. A graphics context is supplied to the script ”on.paint” event handler each time the window needs to be redrawn. The graphics context employs a pixel-based coordinate system with the origin in the upper left corner of the drawing window.

  • gc:clipRect(op, x, y, width, height) - Sets the clipping rectangle for subsequent graphics operations.
  • gc:drawArc(x, y, width, height, start angle, finish angle).
  • gc:drawImage(image,x,y) First argument in format “TI.Image”, x and y the coords.
  • gc:drawLine(xstart, ystart, xend, yend) Draws a line starting at the point (xstart,ystart) and ending at the point (xend, yend)
  • gc:drawPolyLine(int list1 [,int list2, .., int listN]) Draws a shape from a list contaning successively the x and y coordinates of each point the line have to draw.
  • gc:drawRect(x, y, xwidth, yheight) Draws a rectangle at (x,y) with the “x” side being “xwidth” long and the “y” side being “yheight” long
  • gc:drawString(string, x, y, position) position is the string’s anchor point and can be "baseline", “bottom”, “middle”, or “top”.
  • gc:fillArc(x, y, width, height, start angle, finish angle) see gc:drawArc
  • gc:fillPolygon(int list1 [,int list2, .., int listN]) see gc:drawPolyLine
  • gc:fillRect(x, y, width, height) see gc:drawRect
  • gc:getStringHeight(string) - Returns the string's height.
  • gc:getStringWidth(string) - Returns the string's width
  • gc:setAlpha(alpha) - where alpha is an integer between 0 and 255. Sets the transparency. Not available anymore in 3.2+
  • gc:setColorRGB(red, green, blue) RGB values are integers, from 0 to 255.
  • gc:setFont(font, type, size), with font : {“sansserif”, "serif", ..}, type {“b”, “r”, “i”}, size(int)
  • gc:setPen(thickness, smoothness) : thickness {“thin”, “medium”, "thick"}, smoothness {“smooth”, "dotted", "dashed"}

Images

  • image.copy(theImage, width, height) : Returns a copy of the input image scaled to fit the specified pixel width and height.
  • image.height(theImage) : Returns the height of the image
  • image.new(theImage) : Allocates a new TI.Image
  • image.rotate(theImage, angle) : Returns a rotated copy of theImage by angle degrees
  • image.width(theImage) : Returns the width of the image

Physics Engine

A port of Chipmunk Physics 5.3 has been embedded as an optional module (you have to 'require' it at the beginning of your script, in order to use it)

  • Arbiters and Collision Pairs
  • Bodies
  • Bounding Boxes
  • Circle Shapes
  • Constraints
  • Misc
  • Polygon Shapes
  • Segment Shapes
  • SegmentQueryInfo
  • Shape Queries
  • Shapes
  • Space Queries
  • Spaces
  • Vectors

Platform

  • platform.isColorDisplay() : Returns true if the display of the host platform is color-capable (Nspire CX and emulator). Returns false if the display is grayscale (Nspire non-CX).
  • platform.isDeviceModeRendering() : Returns true if the script is running on the hand-held device or in the emulator (with the calculator view), and false if the script is running in the normal view of the emulator.
  • platform.isTabletModeRendering() : Returns true if the script is running on a tablet device (iPad app) and false otherwise.
  • platform.window : Returns the window object currently owned by the script application. The window object contains several other methods.
  • platform.withGC(...) : executes a function which a passed dummy gc.
  • platform.registerErrorHandler(...) : A global error handler for the lua script.
  • platform.hw() : returns a number corresponding to the type of device the script is running on.

Timer

Toolpalette

The tool palette provides a menu of commands, selectable by the user, that invoke functionality of the script app. The menu itself is accessible by pressing the "Menu" button.

  • register(menuStructure) : Registers the menuStructure table.
  • enable(toolname, itemname, state) : Enables or disables a menu item in the tool palette.
  • enableCopy(state) : This routine enables / disables the Edit > Copy menu command.
  • enableCut(state) : This routine enables / disables the Edit > Cut menu command.
  • enablePaste(state) : This routine enables / disables the Edit > Paste menu command.

var

'var' functions allows the script to interact with BASIC variables.

  • var.list() - Lists of names of variables currently used.
  • var.monitor(name) - Turns on monitoring of the math variable with given name.
  • var.unmonitor(name) - Turns off monitoring of the math variable with given name.
  • var.makeNumericList(name) : Creates a list in the symbol table with the given name, which is optimized to hold numeric values.
  • var.recall(name) : Returns the value of a math variable with the given name.
  • var.recallAt(name, col [,row]) : Recalls a value from a cell of a list or matrix in the symbol table.
  • var.recallstr(name) : Returns the value of a math variable with the given name as a string.
  • var.store(name, value) : Stores value as a math variable with the given name.
  • var.storeAt(name, numericValue, col [,row]) : Stores a numeric value into an element of a math list or matrix with the given name.

Misc.