Author Topic: Problem with on.loseFocus() function  (Read 6805 times)

0 Members and 1 Guest are viewing this topic.

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Problem with on.loseFocus() function
« on: September 30, 2012, 11:34:19 am »
Hello,

I am a newbie regarding TI-Nspire Lua programming (got my CX CAS this week, immediately updated to OS v3.2).

Question:

Within a page running my Lua script, I want to store all entered values to TI.Nspire variables, before switching to another page within the same problem.
I tried to do it by using on.loseFocus() function, expecting this function will be the last one called  before leaving the script page.
On my surprise, printing out the variable values at the other page showed the old variable values, indicating the variables have not been stored.
In order the correct values are printed out, I have to re-visit the script page (there I do not edit anything, as I see there the correct values I entered the first time)
and switch back to printing page: then I see the current values.

Putting a breakpoint within on.loseFocus() function revealed that navigating away from the script page did not trigger the function.
The function has been triggered for the first time on re-visiting the script page, after on.getFocus() function made its job.

On the other hand, on.getFocus() function does its job as expected: whenever I enter the script page, I get the initial setup I defined by this function.

Do I do/understand something wrong?

Another short question just crossed my mind:
Is on.construction() function triggered each time I enter a script page, or only once, at entering the script page for the first time after I load a document?
 
Thanks for your help,
Goran

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Problem with on.loseFocus() function
« Reply #1 on: September 30, 2012, 11:46:37 am »
Hi Goran,

This is indeed a strange issue, and it might very well be that the computer software has a bug.
Do you experience the same problem on your handheld too ?
I've tried this code on my handheld (CX CAS 3.2+), and it's working correctly:
Code: [Select]
local t = 0
function on.paint(gc)
    gc:drawString("Times that focus got lost: " .. t, 10, 10, "top")
end

function on.loseFocus()
    t=t+1
end


So, I assume that it will be a bug with the computer software. If that is the case, I will need to send a bug report to TI.
Thank you for creating a topic about this.

Also, on.construction is only called at script startup.

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Problem with on.loseFocus() function
« Reply #2 on: September 30, 2012, 11:52:14 am »
The on.construction() function is called once, namely when you open the document. For the other problem, if I understand correctly you'd want to do something like the following, right?
users switches to other page>store all entered values>reset values on the other page
The on.loseFocus() is called when user input focus is lost (I don't know when that is) May indeed be a bug like jim said, it works fine for me too. Personally if I'd wanted an event to fire when moving to a new page, I'd use on.deactivate() instead.

[edit] I also recommend having a look at the scripting api reference guide by TI http://bit.ly/P4dxnY
[edit2] Aww jim you beat me (why did I have to go fetch a drink while typing this comment :P)
« Last Edit: September 30, 2012, 12:03:37 pm by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Re: Problem with on.loseFocus() function
« Reply #3 on: September 30, 2012, 12:26:57 pm »
Hello guys,

thanks for your quick answers.

This is my first TI/Lua script and I am developing it using the software (Windows).
I'll now install it at handheld and tell you the outcome.

Best regards,
Goran

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Re: Problem with on.loseFocus() function
« Reply #4 on: September 30, 2012, 01:07:48 pm »
Hi,

the same happens on the handheld, too :(

Here is what I do:


InputField = class()   -- a class I use to enter input variables

function InputField:init(name,x,y,init_value)
    self.name = name
    self.x = x
    self.y = y
    self.editor = D2Editor.newRichText()
    self.editor:move(x+30,y)
    self.editor:resize(100,28)
    self.editor:setBorder(1)
    self.editor:setText(var.recall(name) or (init_value or 0))  -- initially store at construction time: init_value (if defined) or 0 (if not defined) or already stored variable value (if the page is re-entered)
    var.store(name,self.editor:getText())
end

...

function InputField:storeVariable()   -- called by clients like on.loseFocus() function 
    var.store(self.name,self.editor:getText())
end

...

function on.construction()  -- create "objects"
    ...   
    field = {}
   
    field[1] = InputField('er',2,4)
    field[2] = InputField('ev',2,34)   
    ...
end

function on.getFocus()
    field[1]:setFocus()
end

function on.loseFocus()
    for i = 1, #field do
        field:storeVariable()
    end
end


Is the order of entered code (I am a newbie in Lua generally) important, too (e.g. of class definitions, variable definitions, event handlers)?

Regards,
Goran

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Re: Problem with on.loseFocus() function
« Reply #5 on: September 30, 2012, 01:17:51 pm »
Note:

When copying/pasting the code, the i-th field table member in on loseFocus() function was interpreted as italics tag,
so in on.loseField() function it is originally written:

    field<bracket open> i <bracket close>  :storeVariable()

For sure must exist some better option to enter the code than directly copy/paste.


Regards,
Goran

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Problem with on.loseFocus() function
« Reply #6 on: September 30, 2012, 05:35:21 pm »
Ah, I see your problem. D2Editors are windows of there own, the script loses focus when you select the to enter data in them.

I need to go sleep now, so I'll post a solution to this tomorrow.
Good night :)

Offline ElementCoder

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 611
  • Rating: +42/-2
    • View Profile
Re: Problem with on.loseFocus() function
« Reply #7 on: October 01, 2012, 02:31:07 am »
Next time you'd want to post your code in code tags (see below). That makes it much easier to read :)
Is the order of entered code (I am a newbie in Lua generally) important, too (e.g. of class definitions, variable definitions, event handlers)?
Sometimes it is, sometimes it isn't. Variables for example need to be declared before you use them. For example, you cannot do x+1 when you haven't said x=<value> before. But functions sometimes don't necessarily need to be declared before they are called.
Spoiler For Code tags:
Code: [Select]
InputField = class()   -- a class I use to enter input variables

function InputField:init(name,x,y,init_value)
    self.name = name
    self.x = x
    self.y = y
    self.editor = D2Editor.newRichText()
    self.editor:move(x+30,y)
    self.editor:resize(100,28)
    self.editor:setBorder(1)
    self.editor:setText(var.recall(name) or (init_value or 0))  -- initially store at construction time: init_value (if defined) or 0 (if not defined) or already stored variable value (if the page is re-entered)
    var.store(name,self.editor:getText())
end

...

function InputField:storeVariable()   -- called by clients like on.loseFocus() function
    var.store(self.name,self.editor:getText())
end

...

function on.construction()  -- create "objects"
    ...   
    field = {}
   
    field[1] = InputField('er',2,4)
    field[2] = InputField('ev',2,34)   
    ...
end

function on.getFocus()
    field[1]:setFocus()
end

function on.loseFocus()
    for i = 1, #field do
        field:storeVariable()
    end
end
« Last Edit: October 01, 2012, 02:34:51 am by ElementCoder »

Some people need a high five in the face... with a chair.
~EC

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Problem with on.loseFocus() function
« Reply #8 on: October 01, 2012, 03:56:21 am »
Okay, here goes.

D2Editor's are special things, they have a window of there own. When selecting the D2editor, the script will already lose focus. Because of this, when you switch to another page it will not lose focus again.

Personally, I don't like to use D2editor, and have my own input objects. Anyway, let me show you how to solve this problem.
Add this line to your InputField class:
self.editor:registerFilter(editors)
Rename on.loseFocus to editors.loseFocus.
Add editors = {} before editors.loseFocus.

Try your code, it should do what you want :)

See D2Editor:registerFilter in the manual for some more info.

Edit, you could also add one line such as this to your InputField class:
Code: [Select]
self.editor:registerFilter{loseFocus=function () self:storeVariable() end}
I don't like to create functions during script runtime (it's slow), but since your inputs are created at script startup it shouldn't be a problem.
« Last Edit: October 01, 2012, 04:00:00 am by Jim Bauwens »

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Re: Problem with on.loseFocus() function
« Reply #9 on: October 01, 2012, 05:56:52 am »
@ Jim:

yes, that helped!

Actually, the utmost important thing here is to fully understand the bits and bolts of TI/Lua specific event mechanism,
(what/when/why triggers), which gives you then the real power in designing apps.

Where can one find an additional/complete documentation on that subject?

@ ElementCoder:

Also thanks for the great/prompt support!

Best regards,
Goran

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Problem with on.loseFocus() function
« Reply #10 on: October 01, 2012, 06:15:37 am »
Actually, the utmost important thing here is to fully understand the bits and bolts of TI/Lua specific event mechanism,
(what/when/why triggers), which gives you then the real power in designing apps.

Where can one find an additional/complete documentation on that subject?

I guess you can see this article here : http://www.inspired-lua.org/2011/05/concepts-and-basics/ (basics) and http://www.inspired-lua.org/2011/05/linking-events/ (a bit more about events)
and the next ones.

More practice-oriented tutorials can be found here, then : http://compasstech.com.au/TNS_Authoring/Scripting/
« Last Edit: October 01, 2012, 06:16:41 am by adriweb »
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation

Offline Hayleia

  • Programming Absol
  • Coder Of Tomorrow
  • LV12 Extreme Poster (Next: 5000)
  • ************
  • Posts: 3367
  • Rating: +393/-7
    • View Profile
Re: Problem with on.loseFocus() function
« Reply #11 on: October 01, 2012, 07:04:56 am »
I am a newbie regarding TI-Nspire Lua programming (got my CX CAS this week, immediately updated to OS v3.2).
Then you might not know that the OS 3.2 blocks Ndless, so you can't run any native program anymore (like the GBA emulator, Doom, nTNOC (to get more free space), mViewer (to see images on your calculator), etc).
I am not telling you to downgrade in anyway, if you don't care about Ndless, that is your choice, but I still wanted to warn you in case you didn't know about that choice ;)

Also, you triple posted here O.O
http://ourl.ca/17114/318258
http://ourl.ca/17114/318274
http://ourl.ca/17114/318276
I own: 83+ ; 84+SE ; 76.fr ; CX CAS ; Prizm ; 84+CSE
Sorry if I answer with something that seems unrelated, English is not my primary language and I might not have understood well. Sorry if I make English mistakes too.

click here to know where you got your last +1s

Offline Jim Bauwens

  • Lua! Nspire! Linux!
  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1881
  • Rating: +206/-7
  • Linux!
    • View Profile
    • nothing...
Re: Problem with on.loseFocus() function
« Reply #12 on: October 01, 2012, 07:10:44 am »
Also, I can see that you have already quite some experience with programming.
Take a look at https://github.com/adriweb/EEPro-for-Nspire/ , an application I made with Adriweb. It makes use of a big toolkit I made, maybe you can find some interesting stuff in it :)

Offline Goran

  • LV2 Member (Next: 40)
  • **
  • Posts: 32
  • Rating: +0/-0
    • View Profile
Re: Problem with on.loseFocus() function
« Reply #13 on: October 01, 2012, 08:05:10 am »
Hello guys,

you gave me a nice "homework" to do now :)

I'll enjoy study it (which will take some time).

Thanks and best regards,
Goran

Offline Adriweb

  • Editor
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1708
  • Rating: +229/-17
    • View Profile
    • TI-Planet.org
Re: Problem with on.loseFocus() function
« Reply #14 on: October 01, 2012, 08:15:46 am »
Hello guys,

you gave me a nice "homework" to do now :)

I'll enjoy study it (which will take some time).

Thanks and best regards,
Goran

Don't hesitate to ask questions, as it's quite complex ;-)
My calculator programs
TI-Planet.org co-admin.
TI-Nspire Lua programming : Tutorials  |  API Documentation