Get started with gui to draw...

  • 14 Replies
  • 115 Views
*

d.k

  • Apprentice
  • **
  • 32
Get started with gui to draw...
« on: April 09, 2017, 15:44:09 »
Hi!

I try to get started with the gui features.  But my app gets killed after using g:moveto.
What's wrong with it?

Code: [Select]
true app:isgui !

\ Canvas
{
"kind" : "win" ,
"wide" : 400 ,
"high" : 400 ,
"init" : ( 10 10 g:xy ) ,
"children" :
  [
  {
    "kind" : "image" ,
    "name" : "canvas" ,
    "bounds" : "0,0,parent.width,parent.height" ,
    "bg" : "Black" ,
    "click" : "clicked"
  }
  ]
} var, guiobj
var gui

: clicked \ g x y --
  .s \ debug info
  g:moveto ;

: app:main
  guiobj @ g:new g:show dup gui !
  ;

Best regards,
d.k

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: Get started with gui to draw...
« Reply #1 on: April 09, 2017, 15:51:18 »
The problem here is that you are drawing in a click callback.  What you want to do is to do the drawing in a "draw" callback, not a "click" callback.

*

d.k

  • Apprentice
  • **
  • 32
Re: Get started with gui to draw...
« Reply #2 on: April 09, 2017, 16:38:08 »
Thank you, it doesn't get killed anymore, but it's not drawing...
I looked for documentation in the manual, the samples, the apps, etc. but at the moment it's trial-and-error.

Code: [Select]
true app:isgui !

\ Canvas
{
"kind" : "win" ,
"wide" : 400 ,
"high" : 400 ,
"init" : ( 10 10 g:xy ) ,
"children" :
  [
  {
    "kind" : "image" ,
    "name" : "canvas" ,
    "bounds" : "0,0,parent.width,parent.height" ,
    "bg" : "Black" ,
    "click" : "clicked",
    "draw" : "do-draw"
  }
  ]
} var, guiobj
var gui

with: g
: clicked \ g x y --
  .s \ debug info
  2drop invalidate ;
 
: do-draw \ g --
  mousepos? pix!
  ;

: app:main
  guiobj @ new show dup gui !
  "white" fcolor
  .s
  ;
« Last Edit: April 09, 2017, 17:02:54 by d.k »

*

d.k

  • Apprentice
  • **
  • 32
Re: Get started with gui to draw...
« Reply #3 on: April 09, 2017, 16:48:34 »
Another question:  Do I need an image to draw something?

*

d.k

  • Apprentice
  • **
  • 32
Re: Get started with gui to draw...
« Reply #4 on: April 09, 2017, 17:02:23 »
Looking at gui/3min.8th - I don't, right?

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: Get started with gui to draw...
« Reply #5 on: April 09, 2017, 17:03:20 »
No, you can draw lines etc. without having an image.  But you need to draw in a 'draw' callback.  See the different gui samples for examples.

In your example, you're not giving a color to "pix!" so nothing happens.

*

d.k

  • Apprentice
  • **
  • 32
Re: Get started with gui to draw...
« Reply #6 on: April 09, 2017, 17:13:31 »
Is the color not set by
Code: [Select]
"white" fcolor used in
Code: [Select]
app:main?

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: Get started with gui to draw...
« Reply #7 on: April 09, 2017, 17:16:35 »
not for pix!....  see the help

*

d.k

  • Apprentice
  • **
  • 32
Re: Get started with gui to draw...
« Reply #8 on: April 09, 2017, 17:27:02 »
The manual v17.03 for g:pix!: "Set the pixel at position ( x , y ) in the gui g to the current g:fcolor"

*

d.k

  • Apprentice
  • **
  • 32
Re: Get started with gui to draw...
« Reply #9 on: April 09, 2017, 17:29:39 »
It's drawing!  :)

I moved
Code: [Select]
"white" fcolor into
Code: [Select]
do-draw.

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: Get started with gui to draw...
« Reply #10 on: April 09, 2017, 17:32:16 »
Ah, yes; you are right.  I was looking at img:pix! not g:pix!   :-[

Yes, setting the color inside the draw callback is what you need to do, since a new 'drawing context' is created for each draw callback, and the state is not preserved.

*

d.k

  • Apprentice
  • **
  • 32
Re: Get started with gui to draw...
« Reply #11 on: April 09, 2017, 17:41:12 »
Is it therefore, that you use g:save to save the context?

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: Get started with gui to draw...
« Reply #12 on: April 09, 2017, 17:45:36 »
Yes, but it saves it only during your draw callback.  So you use it to "save the current state" while you do something (e.g. rotate 34 degrees) and then "restore" will "pop the current state" to the previously saved one.

*

d.k

  • Apprentice
  • **
  • 32
Re: Get started with gui to draw...
« Reply #13 on: April 09, 2017, 17:48:49 »
Ok, thank you! :)

*

ron

  • Administrator
  • Guru
  • *****
  • 3,243
Re: Get started with gui to draw...
« Reply #14 on: April 09, 2017, 17:57:27 »
No problem, have fun!