It is currently Tue Jul 23, 2019 1:05 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 13 posts ] 
Author Message
 Post subject: Updating canvas while dragging the scrollbars
PostPosted: Mon Jan 29, 2018 12:08 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 5:38 am
Posts: 250
Location: Australia
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Mon Jan 29, 2018 3:42 pm 
Online
Enthusiast
Enthusiast
User avatar

Joined: Wed Sep 22, 2010 1:17 pm
Posts: 288
Location: United Kingdom
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:
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...


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Mon Jan 29, 2018 10:05 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 5:38 am
Posts: 250
Location: Australia
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?


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Mon Jan 29, 2018 11:23 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Apr 12, 2009 6:27 am
Posts: 3355
Use CanvasGadget() inside ScrollAreaGadget()

_________________
Egypt my love


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Tue Jan 30, 2018 12:59 am 
Offline
Enthusiast
Enthusiast

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


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Tue Jan 30, 2018 1:22 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 5:38 am
Posts: 250
Location: Australia
I have put all my application variables into a global structure, and it works. I really wish there was another solution


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Tue Jan 30, 2018 9:21 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 13595
Location: France
You can use SetGadgetData() to bind a structure to a gadget


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Sun Nov 18, 2018 5:45 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 5:38 am
Posts: 250
Location: Australia
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:


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Sun Nov 18, 2018 12:07 pm 
Offline
Addict
Addict

Joined: Sun Sep 07, 2008 12:45 pm
Posts: 4256
Location: Germany
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Sun Nov 18, 2018 12:39 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10520
Location: Beyond the pale...
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Mon Nov 19, 2018 9:01 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 5:38 am
Posts: 250
Location: Australia
I'm not quite sure how to do that. Can you complete this code?

Code:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Mon Nov 19, 2018 9:29 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Wed Oct 29, 2003 4:35 pm
Posts: 10520
Location: Beyond the pale...
All kinds of ways. As someone stated above, you can use SetGadgetData()/GetGadgetData().

Code:
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: Updating canvas while dragging the scrollbars
PostPosted: Mon Nov 19, 2018 9:48 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 5:38 am
Posts: 250
Location: Australia
Thank you srod that works well.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 13 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye