Cannot use gfx inside onupdate method
Closed this issue · 4 comments
Hi, I need to update some graphics on my Script and I'm trying to use onupdate
callback to make those updates
Unfortunately I cannot use the following code, the rtk UI is not rendered then:
window.onupdate = function ()
gfx.line(0,0, 100,100)
end
The line is drawn ok but rtk UI is not
Using ondraw
callback works fine but I need onupdate
because I need every frame to fetch mouseX and mouseY.
Is there some other place on the loop I can use gfx
? I tried defering and using callsoon
and other attempts with no success
Many thanks
I am now able to go around this problem by calling window.queue_draw()
inside onupdate
, using gfx during ondraw works fine
This isn't really a bug as such. It's working by design inasmuch as the ondraw handler is only called when the widget (window in this case) has been drawn, which is only needed when something changes. So it's intentionally not called on every defer cycle of the script for efficiency.
And when you draw manually from onupdate() without telling rtk it needs to draw (via queue_draw()) then you'll clobber the window's contents. I'll update the documentation for onupdate() to explicitly say this.
I am now able to go around this problem by calling
window.queue_draw()
insideonupdate
, using gfx during ondraw works fine
This was my first thought as well reading your original post. But depending on what you're trying to do, a custom widget may make more sense.
For example, if you just want one part of the UI to update when the mouse moves within the script window, and you want to do custom drawing in that particular area, then a custom widget that implements the _handle_event() method will be able to queue_draw() when a particular event necessitates it, and then implement _draw() for custom drawing. These methods are part of the subclass API. (This post includes a simple example of a custom widget called SimpleSlider. It subclasses rtk.Spacer (which I regret not having called rtk.Canvas and will be aliasing it at some point in the future) instead of rtk.Widget directly for slightly improved ergonomics, but the basic idea is the same.)
Custom widgets are nicer in the sense that you can insert them into containers and viewports and they obey all the usual behaviors you expect.
In contrast, if you want to implement some sort of global overlay that's drawing at a fixed point on the window regardless of what else is going on in the window, and in particular you want to be able to respond to mouse movements outside the script's window, then the approach you found is the cleanest way. I'd just suggest that if you only want to force the redraw when the mouse cursor moves that you track the last and current mouse position and only queue_draw() if it changes, which will reduce the background overhead of the script.
Think I got it, many thanks, can close this.
Cheers @tiagolr . Feel free to open another issue if you have any other questions.