Line 35: |
Line 35: |
| *... | | *... |
| *Link events (pseudo code) | | *Link events (pseudo code) |
− | <div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"><span class="Apple-style-span" style="font-family: sans-serif; line-height: 19px; white-space: normal; font-size: 13px; ">''while(Exit) | + | <source lang="lua">while(Exit) |
| ------- Some OS routines here | | ------- Some OS routines here |
| | | |
Line 57: |
Line 57: |
| ----- End of Event link | | ----- End of Event link |
| end'' | | end'' |
− | ''Note : the (N) commented line only indicates the meaning of the routine. Those functions don't really exist.
| + | Note : the (N) commented line only indicates the meaning of the routine. Those functions don't really exist.</source> |
− | ''
| + | |
− | </span></pre></div></div> | |
| 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. | | 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. |
| | | |
| 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). | | 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). |
− | <div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> function on.paint(gc) | + | <source lang="lua"> function on.paint(gc) |
| if message then | | if message then |
| gc:setFont("sansserif", "r", 10) -- initialize font drawing | | gc:setFont("sansserif", "r", 10) -- initialize font drawing |
Line 80: |
Line 79: |
| message = "Hello World !" -- store a message | | message = "Hello World !" -- store a message |
| platform.window:invalidate() -- force display | | platform.window:invalidate() -- force display |
− | end</pre></div></div> | + | end</source> |
| 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 ? Because if we call '''platform.window:invalidate()''' right there, we won't refresh the screen. Why again ? 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. | | 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 ? Because if we call '''platform.window:invalidate()''' right there, we won't refresh the screen. Why again ? 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. |
| | | |
Line 179: |
Line 178: |
| *'''[[on.resize]]'''() is called when the window is rezised | | *'''[[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 : | | *'''[[on.timer]]'''() is called when the timer has been finished. Here an example of using timer to play an animation : |
− | <div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> x = 1 | + | <source lang="1ua"> x = 1 |
| animating = false | | animating = false |
| function on.paint(gc) | | function on.paint(gc) |
Line 199: |
Line 198: |
| platform.window:invalidate() -- recall graph engine | | platform.window:invalidate() -- recall graph engine |
| end | | end |
− | </pre></div></div> | + | </source> |
| ---- | | ---- |
| | | |
Line 268: |
Line 267: |
| | | |
| Example of a valid function using the D2Editor | | Example of a valid function using the D2Editor |
− | <div dir="ltr" class="mw-geshi" style="text-align: left;"><div class="lua source-lua"><pre class="de1"> function createRichTextBox | + | <source lang="1ua"> function createRichTextBox |
| box = D2Editor.newRichText() | | box = D2Editor.newRichText() |
| box:move(50, 50) | | box:move(50, 50) |
| box:resize(50, 50) | | box:resize(50, 50) |
| box:setText("Hello World !") | | box:setText("Hello World !") |
− | end</pre></div></div> | + | end</source> |
| == External Links == | | == External Links == |
| | | |