[Tired Brain]-CanvasGadget in a called procedure

Just starting out? Need help? Post your questions and find answers here.
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

[Tired Brain]-CanvasGadget in a called procedure

Post by flaith »

Hi all,

I'm struggling with a simple code, I've just created a procedure calling a canvas gadget
I call it 2 times but I only see the result of the first call :shock:

Here is the simple code:

Code: Select all

Procedure.i CanvasTextGadget(id.i, x.i, y.i, width.i, height.i, text.s = "")
    CanvasGadget(id, x, y, width, height, #PB_Canvas_Keyboard)
    
    If StartDrawing(CanvasOutput(id))
        DrawingMode(#PB_2DDrawing_Default)
        Box(x, y, width, height, $202020)
        
        DrawText(x, y, text, $A0A0A0, $202020)
        
        StopDrawing()
    EndIf
EndProcedure

If OpenWindow(0, 400, 200, 600, 400, "Canvas Text Gadget", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
    CanvasTextGadget(0,  0,  0, 200, 20, "Font Loaded")
    CanvasTextGadget(1, 10, 40, 150, 40, "Another text")
    
    Repeat
        If WaitWindowEvent() = #PB_Event_CloseWindow
           End
        EndIf
    ForEver
EndIf
and the result:
Image

I really don't get it, what's wrong with the code?
Thanks for your help :)
“Fear is a reaction. Courage is a decision.” - WC
User avatar
Keya
Addict
Addict
Posts: 1890
Joined: Thu Jun 04, 2015 7:10 am

Re: CanvasGadget in a called procedure

Post by Keya »

lol this had me stumped for a good five minutes, but I've got homemade iced coffee so I knew I could do it, reducing your code to minimal as possible the error revealed itself:
when you're StartDrawing() the x,y coordinates used by Box() etc is relative to the control not the form, ie 0,0 .... even though youve placed the gadget at for example, 40, 100 ... so your Box()+DrawText() are trying to draw at 40,100 which isnt even within the region of the gadget :) it would be pretty sweet if the compiler could detect that
Good thing software doesn't drive cars huh? .... :D
Last edited by Keya on Wed Jan 11, 2017 12:41 am, edited 6 times in total.
User avatar
Demivec
Addict
Addict
Posts: 4257
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CanvasGadget in a called procedure

Post by Demivec »

Code: Select all

Procedure.i CanvasTextGadget(id.i, x.i, y.i, width.i, height.i, text.s = "")
    CanvasGadget(id, x, y, width, height, #PB_Canvas_Keyboard)
    
    If StartDrawing(CanvasOutput(id))
        DrawingMode(#PB_2DDrawing_Default)
        Box(0, 0, width, height, $202020) ;each canvas starts with its own reference coordinates
        
        DrawText(0, 0, text, $A0A0A0, $202020)
        
        StopDrawing()
    EndIf
EndProcedure

If OpenWindow(0, 400, 200, 600, 400, "Canvas Text Gadget", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget)
    CanvasTextGadget(0,  0,  0, 200, 20, "Font Loaded")
    CanvasTextGadget(1, 10, 40, 150, 40, "Another text")
    
    Repeat
        If WaitWindowEvent() = #PB_Event_CloseWindow
           End
        EndIf
    ForEver
EndI
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Re: CanvasGadget in a called procedure

Post by flaith »

:oops: :o :lol:
Oops, something's wrong with my brain, it's really time to sleep :mrgreen:
Thanks a lot guys :D
“Fear is a reaction. Courage is a decision.” - WC
Post Reply