Page 1 of 2

Is PureBasic good for interactive 2D graphics applications?

Posted: Tue Oct 14, 2014 3:48 pm
by ifmihai
First of all, hi,
I'm new here

Before the actual question,
What I like about PB is: multi-platform and basic language

Now, what I want
is to create mini apps for myself mostly, research purposes, which can maybe grow into bigger applications.

but I need the possibility to draw interactive objects (circles, rectangles, lines, etc)
that can be receive events (that can be clicked, dragged around, etc)

The toolkit which seems closest to my needs, is Qt framework (QGraphicsScene, QGraphicsView, QGraphicsItem),
but it's c++, and... ughhh :P
It has a port in python (my language of choice), but python is slow at these things (in my perception at least)

I'm aware of q7basic, basic for Qt, which could be great, but,
from what I read here, it's not so good (compile times, updates, support, etc)

Back to PB,
I browsed the docs, I did a little browsing in this forum,
but it's not so clear if interactivity is provided

It seems to me drawing in PB is like in delphi's panel, painting only, which is too primitive for me
But I'm not sure if I'm right, I didn't go too deep

I wanted to ask here because it gets too much time now to find the answer by myself,
and I suppose this is a trivial question for average PB users
hence the question :)

Thanks

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Tue Oct 14, 2014 7:26 pm
by blueznl
Drawn objects are, by their nature, not interactive. They aren't objects as such, but they are parts of a bitmap that is being drawn upon.

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Tue Oct 14, 2014 8:15 pm
by Little John
Hi ifmihai,

welcome on board! :-)

I think you should have a close look at PB's CanvasGadget.
Reference Manual wrote:Create a canvas gadget in the current GadgetList. This gadget provides a drawing surface and events for mouse and keyboard interaction to easily create custom views.

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Tue Oct 14, 2014 11:12 pm
by IdeasVacuum
Here is a good example of what can be done with the Canvas Gadget:
MindMap

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Wed Oct 15, 2014 10:16 am
by ifmihai
thank you for pointing out canvasgadget, Little John
and thank you IdeasVacuum for MindMap link

I browsed both superficially (i've given more attention to canvas widget)

It's still not clear to me if events are passed down to shapes in the canvas

in the following scenario:

1. add canvas widget
2. add a circle into it
3. mouse click on canvas widget

is this event transmitted to the circle drawn?
can circle shape (or object) receive events?

Or circles and other basic shapes are drawn through GDI, painted only on the canvas?

Another way to ask is:
Do I have to implement something to know if user clicked on the circle?
Or is this functionality already into canvas widget ?

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Wed Oct 15, 2014 8:47 pm
by idle
no you would have to use hit tests for the various shapes or regions on mouse clicks
here's a very crude example, right click to create a circle, left click to move

Code: Select all

EnableExplicit 

Structure sCircle
  r.i
EndStructure 

Structure sBox 
  w.i
  h.i
EndStructure   

Structure objects 
  type.i 
  x.i
  y.i
  color.i
  StructureUnion 
    scircle.scircle
    sbox.sbox 
  EndStructureUnion 
EndStructure 

#circle =1
#box =2 

Global NewList objects.objects()  
Global *Obj.objects 
Global Event 

Procedure ReDraw()
  If StartDrawing(CanvasOutput(0))
    Box(0,0,GadgetWidth(0),GadgetHeight(0),RGB(255,255,255)) 
    ForEach objects() 
      Select objects()\type 
        Case #circle 
          Circle(objects()\x,objects()\y,objects()\scircle\r,objects()\color)
        Case #box 
          Box(objects()\x,objects()\y,objects()\sbox\w,objects()\sbox\h,objects()\color)
      EndSelect 
    Next 
    StopDrawing()
  EndIf   
EndProcedure  

Procedure HitTest() 
  Protected x,y,dx.f,dy.f,d.f
  Protected *tmp = LastElement(objects()) 
  
  x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
  y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
  
  ForEach objects() 
    Select objects()\type 
      Case #circle 
        dx.f = x - objects()\x   
        dy.f = y - objects()\y  
        d.f = Sqr(dx*dx+dy*dy) 
        If d < objects()\scircle\r 
          SwapElements(objects(),@objects(),*tmp)
          *obj = @objects() 
          Break 
        EndIf 
      Case #box 
        If (x > objects()\x And x < objects()\x+objects()\sbox\w And y > objects()\y And y < objects()\y + objects()\sbox\h) 
          SwapElements(objects(),@objects(),*tmp)
          *obj = @objects() 
          Break 
        EndIf 
    EndSelect 
  Next 
EndProcedure  

Procedure MouseMove()
  Protected x,y 
  If *obj 
    x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
    y = GetGadgetAttribute(0, #PB_Canvas_MouseY) 
    Select *obj\type 
      Case #circle   
        *obj\x = x 
        *obj\y = y 
      Case #box 
        *obj\x = x - (*obj\sbox\w * 0.5)
        *obj\y = y - (*obj\sbox\h * 0.5) 
    EndSelect   
    ReDraw() 
  EndIf   
EndProcedure   

Procedure MouseUP()
  *obj = 0 
EndProcedure

Procedure MouseRightClick()
  Protected type,x,y 
  If StartDrawing(CanvasOutput(0))
    x = GetGadgetAttribute(0, #PB_Canvas_MouseX)
    y = GetGadgetAttribute(0, #PB_Canvas_MouseY)
    AddElement(objects())
    type = Random(2,1)
    Select type 
      Case #circle    
        objects()\type = #circle 
        objects()\x = x 
        objects()\y = y 
        objects()\scircle\r = 10 
        objects()\color = RGB(Random(255), Random(255), Random(255))
        Circle(x, y, 10, objects()\color)
      Case #box 
        objects()\type = #box 
        objects()\x = x 
        objects()\y = y 
        objects()\sbox\w = 20
        objects()\sbox\h = 20 
        objects()\color = RGB(Random(255), Random(255), Random(255))
        Box(x,y,20,20,objects()\color)
    EndSelect    
    StopDrawing()
  EndIf
EndProcedure   

If OpenWindow(0, 0, 0, 220, 220, "CanvasGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  CanvasGadget(0, 10, 10, 200, 200)
  BindGadgetEvent(0,@HitTest(),#PB_EventType_LeftButtonDown)
  BindGadgetEvent(0,@MouseMove(),#PB_EventType_MouseMove)
  BindGadgetEvent(0,@MouseUP(),#PB_EventType_LeftButtonUp)
  BindGadgetEvent(0,@MouseRightClick(),#PB_EventType_RightClick)
  Repeat
    Event = WaitWindowEvent()
  Until Event = #PB_Event_CloseWindow
EndIf

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Thu Oct 16, 2014 1:21 am
by IdeasVacuum
You could also try using canvas gadgets that represent your graphical items - for example a circle can be drawn on a transparent canvas and that canvas can be user-resized. This is somewhat similar to how a Form Designer works.
Code Example: http://www.purebasic.fr/english/viewtop ... 52&start=0

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Thu Oct 16, 2014 3:31 am
by electrochrisso
@idle: Might be crude, but a nice example. 8)

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Thu Oct 16, 2014 9:07 am
by ifmihai
Now it's clear.
Thank you very much for the example and clarification.

Then i will pass PB, as i consider these as basic requirements.

Im not into reinventing the wheels,

But why isnt there a more advanced canvas?
Maybe it's because gtk?

I read somewhere here in the forum,
When reading about PB vs q7basic, and some user(s) suggested replacing Gtk with qt
Providing qt capabilities in a basic language would be perfect.

Thank you again.

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Thu Oct 16, 2014 11:47 am
by IdeasVacuum
Providing qt capabilities in a basic language would be perfect.
....it might be if you can afford to pay 3195 Euros plus an annual renewal fee of 985 Euros :mrgreen:
However, I don't think QT actually delivers what you want. Their 'dynamic objects' is reference to adding gadgets/widgets/controls at runtime, something that PB is very good at.

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Thu Oct 16, 2014 2:12 pm
by Tenaja
ifmihai wrote:Now it's clear.
Thank you very much for the example and clarification.

Then i will pass PB, as i consider these as basic requirements.

Im not into reinventing the wheels,
I am not aware of any compiler that does exactly what you have described. Idle has provided a great PB example, but if there is something similar with a lot less code, I'd love to see it. If you find one capable, please come back here and share it with us, as I have a similar project in mind.

Thanks.

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Thu Oct 16, 2014 2:15 pm
by heartbone
ifmihai wrote:Is PureBasic good for interactive 2D graphics applications?
Yes, it is excellent for creating them.
First of all, hi,
I'm new here
Hi fellow creator.
Before the actual question,
Then what was the title about??? :?:
What I like about PB is: multi-platform and basic language

Now, what I want
is to create mini apps for myself mostly, research purposes, which can maybe grow into bigger applications.
Nice idea.
but I need the possibility to draw interactive objects (circles, rectangles, lines, etc)
that can be receive events (that can be clicked, dragged around, etc)
That possibility certainly exists in PureBasic.
The toolkit which seems closest to my needs, is Qt framework (QGraphicsScene, QGraphicsView, QGraphicsItem),
but it's c++, and... ughhh :P
I commiserate with you there. UGGGH.
It has a port in python (my language of choice), but python is slow at these things (in my perception at least)

I'm aware of q7basic, basic for Qt, which could be great, but,
from what I read here, it's not so good (compile times, updates, support, etc)
I like Python as well, but the high level constructs means loss of fine control.
Back to PB,
I browsed the docs, I did a little browsing in this forum,
but it's not so clear if interactivity is provided
Interactivity with program created objects is provided the programmer.
It seems to me drawing in PB is like in delphi's panel, painting only, which is too primitive for me
But I'm not sure if I'm right, I didn't go too deep

I wanted to ask here because it gets too much time now to find the answer by myself,
and I suppose this is a trivial question for average PB users
hence the question :)

Thanks
PureBasic is a programming language application.
It seems clear to me that what you are asking for is an application that instantiates an environment for user manipulatible objects.
That also seems awfully close to the description of a generic 2D game creator.
You will have better results in finding what you seek in a search looking for a 2D game creator.

Not trying to be too picky with you, but:
1) There really are no average PB users in this forum's membership.
2) You never got around to asking a question in your post. :)
ifmihai wrote:Now it's clear.
Thank you very much for the example and clarification.

Then i will pass PB, as i consider these as basic requirements.

Im not into reinventing the wheels,
Seems to me to be more like reinventing the transmission and power steering.

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Thu Oct 16, 2014 3:15 pm
by ifmihai
@tenaja
By compiler, you mean basic compiler?
Im not aware of any, searching though.

Its quite trivial to do in python through default tkinter canvas, and through pyqt, qt port to python.
But, i dont want python for this.

Another way is javascript way, there are quite a few options there. But. Its hellavascript :P with all its hellish friends, html and css :D


@heartbone
Thank you for the idea with 2d game creator. Seems like a good idea.
Sorry about ‘average’. I dont know if anyone felt hurt by it.
I wanted to say: almost all programmers here could know about drawing capabilities of PB. (English is not my main language)

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Thu Oct 16, 2014 10:33 pm
by idle
ifmihai wrote:Now it's clear.
Thank you very much for the example and clarification.

Then i will pass PB, as i consider these as basic requirements.

Im not into reinventing the wheels,

But why isnt there a more advanced canvas?
Maybe it's because gtk?

I read somewhere here in the forum,
When reading about PB vs q7basic, and some user(s) suggested replacing Gtk with qt
Providing qt capabilities in a basic language would be perfect.

Thank you again.
What you're essentially asking for is a scene graph with drawing primitives. something like the clutter project.
A scene graph is fairly trivial to set up in PB to provide the functionality you want, though you may need to add missing
2D primitives like arc and splines primitives, though there are numerous implementations of those on the forum
so again it'd be fairly easy to add them.
It's not really a lot of work to get the basic functionality you asked for but perhaps not what you'd want to be doing
being new to the language.

So perhaps if there's an interest from a few more people, I'll write an extensible oo runtime scene graph framework
which users can contribute to, with the aim to support rendering targets for images, canvas, sprites, screens and later opengl gadget
which will provide 2D primitives, Zorder, events, actions, callbacks, runtime, cache, garbage collection.

Re: Is PureBasic good for interactive 2D graphics applicatio

Posted: Fri Oct 17, 2014 5:48 am
by heartbone
OT:
ifmihai wrote:@heartbone
Thank you for the idea with 2d game creator. Seems like a good idea.
Sorry about ‘average’. I dont know if anyone felt hurt by it.
I wanted to say: almost all programmers here could know about drawing capabilities of PB. (English is not my main language)
ifmihai, I am surprised that English is not your native language because your post was so well written.
Now I feel like a jerk about giving you a hard time about your post.
Although I knew the idea that you were expressing from your original posting, the ambiguity of English allowed me to intentionally misinterpret your post to nitpick.
I apologize for doing that.

end OT

Good luck on finding a solution ifmihai.
Since you have a forum account, after you've made your final decision I hope that you will post your finding.