A problem with queued events

classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

A problem with queued events

gunterhm
Hello!

This is my question:

I have a GUI with a button in my Plua 2 b4 program. When the button is
pressed, it shows some information on the screen, makes a sound for
about 200 milliseconds, and does an os.sleep(4). After all this, it
does ui.destroy() and builds a new GUI which is exactly like the
original one and includes the same button, I'll call this one GUI 2.

So I click the button and the os.sleep(4) begins, but if in this lapse
of time I press the button that is still showing on the screen, it
looks like this event is still generated and beeing stored. What
hapens here is this: GUI 1 is destroyed, GUI 2 is then built, it
enters an ui.event() instruction but immediatly GUI 2 catches and
processes the button event previously stored in the event queue.

In fewer words, what I'd like to happen is that any button event that
was generated and queued in this 4 second sleep time, before GUI 2 is
shown, be ignored so that when GUI 2 comes, it has to wait for a
completely new button press from the user, instead of processing an
old unwanted event.

I think there is no way to disable the button just before entering the
os.sleep(4) in order to avoid this situation. It would solve my
problem but I believe it is not possible. So if you have any
alternative I would appreciate it.

I hope I explained this well, sorry for my possible bad english, I'm
not a native english speaker.

Thanks in advance.

Regards!





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply | Threaded
Open this post in threaded view
|

RE: A problem with queued events

Emil Tchekov-3
Hi,

it is very good idea to post your code, so someone can
help you.


As a proposal:

Change the ID of the button in GUI2, so that even if
the event routine was called, nothing to be proceed
(and check not only for event type, but also for ID of
the cousing element)


Regards

Emil



       

       
               
___________________________________________________________
Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply | Threaded
Open this post in threaded view
|

Re: A problem with queued events

gunterhm
Thanks a lot for answering Emil.

I didn't post the code because I didn't want any of you to spend much
time reading it, although I admit that understanding the code might
have been easier than understanding my explanation :D However I am
glad that you got my point.

I'm not sure how can the ID of the button be changed. I am creating
the button every time I destroy the previous GUI, hoping that my
button would receive a different ID when created again, but it looks
like IDs are being recycled and the same ID is being assigned to my
button (1804) the first time it is created and the second time too.
Even if the program finishes and I execute it again, I see the same ID
number for this button. As you said, I am checking for the ID and not
only for the event but in this case the effect is the same. I took a
look at the Plua documentation but haven't found a function to change
the button ID, if you know how I would appreciate that you tell me.

I'll follow your suggestion and post some code, but I prefer to show
only the important parts if you don't mind, in order to keep it
simple. I'm also changing variable and function names cause in my
original source code they are in spanish. You may also notice that I'm
not a Plua expert so my coding technique could have been better.


mygui = {
  butOk = nil
}

function mygui.showgui()
  ui.destroy()
  ui.title("MyProg")
  ui.moveto(110,160)
  mygui.butOk = ui.button("OK");
  mygui.eventhandler()
end

function mygui.eventhandler()
  while 1 do
    e,id = ui.event()

    if e == ui.ctlSelect then
       if id == mygui.butOk then
         mygui.procOk()
         break;
       end
    end
  end
end

function mygui.procOk()
   ui.moveto(146.90)
   ui.label("You pressed OK")
   ui.sound(150,250,20)
   os.sleep(4)
end

-- Starting point (Two GUIs created, one after another)
mygui.showgui()
mygui.showgui()


Just to sumarize, the problem occurs when in os.sleep(4), the user
presses the button, that is still visible, and still generates an
event that the second GUI will receive and process, instead of
ignoring all the button events that happened when sleeping (which I
woud prefer).

Thank you!

Günter

--- In [hidden email], Emil Tchekov <etchekov@...> wrote:

>
> Hi,
>
> it is very good idea to post your code, so someone can
> help you.
>
>
> As a proposal:
>
> Change the ID of the button in GUI2, so that even if
> the event routine was called, nothing to be proceed
> (and check not only for event type, but also for ID of
> the cousing element)
>
>
> Regards
>
> Emil
>
>
>
>
>
>
>
> ___________________________________________________________
> Telefonate ohne weitere Kosten vom PC zum PC: http://messenger.yahoo.de
>






 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 



Reply | Threaded
Open this post in threaded view
|

Re: A problem with queued events

Emil Tchekov-3
Hi again,

my proposal contains only small changes:


mygui = {
   butOk = nil
}

function mygui.showgui()
-- here
   ui.destroy()
   ui.clear()

   ui.title("MyProg")
   ui.moveto(110,160)
   mygui.butOk = ui.button("OK");
   mygui.eventhandler()

end

function mygui.eventhandler()
   while 1 do
     e,id = ui.event()

     if e == ui.ctlSelect then
        if id == mygui.butOk then
          mygui.procOk()
          break;
        end
     end
   end
end

function mygui.procOk()
-- and here
        ui.destroy()
        ui.clear()
  ui.title("MyProg")

        ui.moveto(146,90)
        ui.label("You pressed OK")
        ui.sound(150,250,20)
        os.sleep(4)
end


As you can see after btnOK was pressed the Gui is destroyed (allways
use ui.destroy with ui.clear!), the message is shown (no events can be
proceeded since butOk is not there ;-)

Create next gui etc.

I hope this helps you.


Very best regards

Emil Tchekov


P.S. Yor code looks very good for someone who means to be "not so
fluent in plua"





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply | Threaded
Open this post in threaded view
|

Re: A problem with queued events

gunterhm
Hi Emil,
Thanks for taking the time for answering and for your advices.
I'm afraid I don't have very good news, actually I've got an
interesting but unexpected result when I added the lines that you
suggested. It was a little disapointing since I also thought that
would be the solution.

The way Plua works internally is unknown to me, however from what I am
seeing, what happens is the following:
1) The first GUI is destroyed
2) the os.sleep(4) begins (there are no buttons on the screen)
3) I click on the part of the screen where the button once was
4) The os.sleep(4) ends and the second GUI is shown
5) ui.event() catches the button event even though there was no button
at the time I clicked there.
6) The second GUI disappears almost instantly because the unwanted
event is processed. This means that the problem is still there :(

There are two things here. Firstable, the ID assigned to the button is
still being recycled, I mean, the ID of the button when in GUI 1 is
the same as the ID of it when in GUI 2, despite the ui.destroy()
ui.clear() calls.

The second thing is, I believe that the button event is not being
queued, the queued thing is the event of the stylus making contact on
that specific position of the screen that will later be translated
into a button event, when os.sleep(4) ends and the button appears
again in that position. Hard to believe but true.

Maybe I shoud give up and try a different approach. What I have in
mind now is to remove the os.sleep(4) and add another button (or maybe
the same button) that the user must press when he is ready to continue
to the next GUI. I think it would be ok for the user.

However, if you or anyone has a new idea regarding the original
problem, it will be welcome and I can try it.

Thanks again for helping me.

Regards!

--- In [hidden email], "etchekov" <etchekov@...> wrote:

>
> Hi again,
>
> my proposal contains only small changes:
>
>
> mygui = {
>    butOk = nil
> }
>
> function mygui.showgui()
> -- here
>    ui.destroy()
>    ui.clear()
>
>    ui.title("MyProg")
>    ui.moveto(110,160)
>    mygui.butOk = ui.button("OK");
>    mygui.eventhandler()
>
> end
>
> function mygui.eventhandler()
>    while 1 do
>      e,id = ui.event()
>
>      if e == ui.ctlSelect then
>         if id == mygui.butOk then
>           mygui.procOk()
>           break;
>         end
>      end
>    end
> end
>
> function mygui.procOk()
> -- and here
> ui.destroy()
> ui.clear()
>   ui.title("MyProg")
>
> ui.moveto(146,90)
> ui.label("You pressed OK")
> ui.sound(150,250,20)
> os.sleep(4)
> end
>
>
> As you can see after btnOK was pressed the Gui is destroyed (allways
> use ui.destroy with ui.clear!), the message is shown (no events can be
> proceeded since butOk is not there ;-)
>
> Create next gui etc.
>
> I hope this helps you.
>
>
> Very best regards
>
> Emil Tchekov
>
>
> P.S. Yor code looks very good for someone who means to be "not so
> fluent in plua"
>






 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply | Threaded
Open this post in threaded view
|

Re: A problem with queued events

Emil Tchekov-3
Hi again,

I was afraid that you can get problem like this...

You have very good programming style (code reuse) and I think in this
particular case this is one of the cause of your problems.

Yes. May be you will need a new approach (workaround) on this problem.

First Idea: Define two (or as more as need) separate variables for
storing Gui element IDs. Write separate functions for creating the
GUIs (do not reuse the same). Write separate  functions for event
handling on each gui screen.(see group message #2159)


Additional/Alternative idea: instead of os.sleep write your own wait
function that also consumes (checks for) events until running, this
way you will may be able to empty the "event queue".



Regards


Emil Tchekov





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply | Threaded
Open this post in threaded view
|

Re: A problem with queued events

gunterhm
Hi Emil,

Thanks for your comments about my programming style :) Actually I have
a good reason to do it that way, the number of GUIs that I want to
show is variable.

Let me tell you a little what my program is about. This is an
application that I'm writing to help me with my study of the german
language, specifically with the german vocabulary, I am mexican so it
asks a word in spanish and the user must answer with the word in german.

Now, each one of the GUIs, GUI1 and GUI2 are the same because the
basic layout of a test question is basically the same. But more than
that, the number of questions are really more than two, it's variable
and difficult to predict because it depends on the last date each word
 was asked and it's score. So it will be impossible to write a
function or separate code for each question of the test. As I said
earlier, I reduced the program's code just to show the problem I was
having but it is not exactly the same. My real program doesn't do a
mygui.showgui() twice, it actually is inside a "while" loop and
reading data from a database.

The good news is that your second alternative worked great. I used a
small variation of it. At first I was skeptical because I didn't know
that ui.event() could act in an asynchronous way, then I realized that
when you pass a number as parameter, it does. All I had to do is go
and read the docummentation again and there it was. I am leaving the
ui.sleep(4), but I do a queue cleaning process after it, when a new
GUI is shown.

So I created a very small function:

function mygui.consumeevents()
 repeat
   e = ui.event(10)
 until e == 0
end

I call it just after ui.destroy() ui.clear(). At this time, the
ui.sleep(4) has ended so we can start removing all the garbage from
the queue.

The only issue was this: The documentation says that nilEvent (which
is supposed to be a constant) is returned when there was no event. But
when I wrote "until e == nilEvent" and tested, it hanged. I only tried
it once, but then I found out that ui.event(10) was actually returning
0 when there is no event.

The result... It behaves the way I want and I'm a happy person.

Thanks a lot for all your valious help Emil.
Best regards!

Gunter Roberto Hagedorn


--- In [hidden email], "etchekov" <etchekov@...> wrote:

>
> Hi again,
>
> I was afraid that you can get problem like this...
>
> You have very good programming style (code reuse) and I think in this
> particular case this is one of the cause of your problems.
>
> Yes. May be you will need a new approach (workaround) on this problem.
>
> First Idea: Define two (or as more as need) separate variables for
> storing Gui element IDs. Write separate functions for creating the
> GUIs (do not reuse the same). Write separate  functions for event
> handling on each gui screen.(see group message #2159)
>
>
> Additional/Alternative idea: instead of os.sleep write your own wait
> function that also consumes (checks for) events until running, this
> way you will may be able to empty the "event queue".
>
>
>
> Regards
>
>
> Emil Tchekov
>







 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


Reply | Threaded
Open this post in threaded view
|

AW: Re: A problem with queued events

Emil Tchekov-3
HI,

You are welcome!


Just few things about me:

I am Bulgarian, but live since round about 9 years in Germany. I will not tell that my German skills are like native speaker, but very close. What I am trying to tell is - if you need assistance and mean I can help you in your study, feel free to contact me (also directly, not over the plua group)...

...under:

[hidden email]

or

[hidden email]



Ich wünsche dir viel Erfolg in dein Studium und verbleibe

Mit freundlichen Grüßen

Emil Tchekov



P.S. For not German speaker: those above are only greetings.





 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/plua/

<*> To unsubscribe from this group, send an email to:
    [hidden email]

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/