Page 1 of 1

[Tired Brain]-CanvasGadget in a called procedure

Posted: Wed Jan 11, 2017 12:06 am
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 :)

Re: CanvasGadget in a called procedure

Posted: Wed Jan 11, 2017 12:35 am
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

Re: CanvasGadget in a called procedure

Posted: Wed Jan 11, 2017 12:38 am
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

Re: CanvasGadget in a called procedure

Posted: Wed Jan 11, 2017 12:43 am
by flaith
:oops: :o :lol:
Oops, something's wrong with my brain, it's really time to sleep :mrgreen:
Thanks a lot guys :D