Updating canvas while dragging the scrollbars

Just starting out? Need help? Post your questions and find answers here.
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Updating canvas while dragging the scrollbars

Post by coco2 »

I have added scrollbars to the canvas but when I tried to bind an event to it it wouldn't allow me to pass a parameter. How do you code a canvas that moves while you move the scrollbar? If you can't pass parameters then what's the point of the bound event?
User avatar
spikey
Enthusiast
Enthusiast
Posts: 581
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Updating canvas while dragging the scrollbars

Post by spikey »

It depends exactly what you are trying to achieve.

If you just want the canvas to be slightly larger than the available screen area then it might be easier to put the canvas inside a scroll area gadget.

Code: Select all

Procedure BindScrollDatas()
  SetWindowTitle(0, "ScrollAreaGadget " +
                    "(" +
                    GetGadgetAttribute(0, #PB_ScrollArea_X) +
                    "," +                      
                    GetGadgetAttribute(0, #PB_ScrollArea_Y) +
                    ")" )
EndProcedure

If OpenWindow(0, 0, 0, 405, 240, "ScrollAreaGadget", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ScrollAreaGadget(0, 10, 10, 390,220, 1500, 1500, 30)
  CanvasGadget(1, 2, 2, 1500, 1500)
  CloseGadgetList()
  
  StartDrawing(CanvasOutput(1))
  DrawingMode(#PB_2DDrawing_Gradient)      
  BackColor($00FFFF)
  FrontColor($FF0000)
  
  BoxedGradient(0,0, 1500, 1500)
  Box(0,0,1998,1998)
  BindGadgetEvent(0, @ BindScrollDatas())
  StopDrawing()
  
  Repeat
    Select WaitWindowEvent()
      Case  #PB_Event_CloseWindow
        End
      Case  #PB_Event_Gadget
        Select EventGadget()
          Case 0
            MessageRequester("Info","A Scroll has been used ! (" +
                                    GetGadgetAttribute(0, #PB_ScrollArea_X) +
                                    "," +                      
                                    GetGadgetAttribute(0, #PB_ScrollArea_Y) +
                                    ")" ,#PB_MessageRequester_Ok)
        EndSelect
    EndSelect
  ForEver
EndIf
On the other hand if you want the canvas to represent a drawing area significantly larger than the screen size, say to represent an A0 size CAD drawing or an infinite drawing space then you would need to treat the canvas as a viewport to a virtual surface. The Event(), EventType(), EventGadget(), GetGadgetAttribute() functions are available inside the bound procedures to determine the current status. You'd need to scale these settings and the gadget/window sizes to determine the relationship between the screen co-ordinates and the virtual co-ordinates and draw the appropriate section of the virtual surface in the viewport canvas...
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Updating canvas while dragging the scrollbars

Post by coco2 »

I'm making an image editor so I want to have scrollbars that can pan an image within the canvas. It takes a huge amount of variables to display the image in the canvas, do I have to make everything global?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: Updating canvas while dragging the scrollbars

Post by RASHAD »

Use CanvasGadget() inside ScrollAreaGadget()
Egypt my love
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Updating canvas while dragging the scrollbars

Post by coco2 »

It would slow down too much when zooming. I've optimised the canvas to show just the zoomed portion of the image.
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Updating canvas while dragging the scrollbars

Post by coco2 »

I have put all my application variables into a global structure, and it works. I really wish there was another solution
Fred
Administrator
Administrator
Posts: 16617
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Updating canvas while dragging the scrollbars

Post by Fred »

You can use SetGadgetData() to bind a structure to a gadget
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Updating canvas while dragging the scrollbars

Post by coco2 »

I can store a value using that method (is that what you mean?), but my main loop doesn't seem to be running while dragging a scroll bar so I can't update the contents in real time.

Anyone know how to run code while dragging a scroll bar or does it lock up your program? :lol:
infratec
Always Here
Always Here
Posts: 6817
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Updating canvas while dragging the scrollbars

Post by infratec »

coco2 wrote:I can store a value using that method (is that what you mean?)
This value can be the address of a structure.
So you don't need to make the structure global.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Updating canvas while dragging the scrollbars

Post by srod »

coco2 wrote:Anyone know how to run code while dragging a scroll bar or does it lock up your program? :lol:
Use BindEventGadget() on the scrollbars. You can get live scrolling events this way - albeit in a 'blanket' fashion.
I may look like a mule, but I'm not a complete ass.
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Updating canvas while dragging the scrollbars

Post by coco2 »

I'm not quite sure how to do that. Can you complete this code?

Code: Select all

Structure TestStructure
  A.i
  B.i
  C.i
EndStructure

Procedure BindScrollBar()
  SetWindowTitle(0, "Scrollbar: " + GetGadgetState(0))
  ; how do you get access to variable A here?
EndProcedure

Define Quit.i
Define Test.TestStructure

Quit = 0
Test\A = 70
Test\B = 80
Test\C = 90

OpenWindow(0, 0, 0, 400, 300, "Scrollbar Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ScrollBarGadget(0, 20, 260, 360, 20, 0, 255, 64)
BindGadgetEvent(0, @BindScrollbar())
SetGadgetData(0, Test)
Repeat
  Select WaitWindowEvent()
    Case  #PB_Event_CloseWindow
      Quit = 1
  EndSelect
Until Quit
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Updating canvas while dragging the scrollbars

Post by srod »

All kinds of ways. As someone stated above, you can use SetGadgetData()/GetGadgetData().

Code: Select all

Structure TestStructure
  A.i
  B.i
  C.i
EndStructure

Procedure BindScrollBar()
  Protected *ptrTest.TestStructure
  *ptrTest = GetGadgetData(0)
  SetWindowTitle(0, "Scrollbar: " + GetGadgetState(0) + ", Test\A = " + Str(*ptrTest\A))
EndProcedure

Define Quit.i
Define Test.TestStructure

Quit = 0
Test\A = 70
Test\B = 80
Test\C = 90

OpenWindow(0, 0, 0, 400, 300, "Scrollbar Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

ScrollBarGadget(0, 20, 260, 360, 20, 0, 255, 64)
SetGadgetData(0, @Test)
BindGadgetEvent(0, @BindScrollbar())
Repeat
  Select WaitWindowEvent()
    Case  #PB_Event_CloseWindow
      Quit = 1
  EndSelect
Until Quit
I may look like a mule, but I'm not a complete ass.
coco2
Enthusiast
Enthusiast
Posts: 368
Joined: Mon Nov 25, 2013 5:38 am
Location: Australia

Re: Updating canvas while dragging the scrollbars

Post by coco2 »

Thank you srod that works well.
Post Reply