Scrolling on a canvas

BlitzMax Forums/BlitzMax GUI Programming/Scrolling on a canvas

First you could may split the mouse movement and the scrolling.
The mouse move event set the scroll direction and the timer updates the canvas.

try this quick hack:
SuperStrict

Global background:TImage = LoadImage("background.jpg") 
Global mapXcenter:Int = ImageWidth(background)/2
Global mapYcenter:Int = ImageHeight(background)/2
Global characterX:Int = 400
Global characterY:Int = 300

Global Xoffset:Int,Yoffset:Int
Global ScrollBoarder:Int = 100
Global Scroll:Int


Global	Canvas1:TGadget

Local UpdateCanvas:TTimer = CreateTimer:TTimer( 120 )

Local Window1:TGadget = CreateWindow:TGadget("Window1",75,62,436,272,Null,WINDOW_TITLEBAR|WINDOW_RESIZABLE |WINDOW_STATUS |WINDOW_CLIENTCOORDS )
	Canvas1:TGadget = CreateCanvas:TGadget(0,0,436,272,Window1:TGadget,Null)
		ActivateGadget( Canvas1:TGadget )
		SetGadgetLayout( Canvas1:TGadget,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED,EDGE_ALIGNED )

Repeat
	WaitEvent()
	Select EventID()
		Case EVENT_WINDOWCLOSE
			Select EventSource()
				Case Window1	Window1_WC( Window1:TGadget )
			End Select

		Case EVENT_MOUSEMOVE
			Select EventSource()
				Case Canvas1	Canvas1_MM( Canvas1:TGadget , EventX() , EventY() )
			End Select

		Case EVENT_GADGETPAINT
			Select EventSource()
				Case Canvas1	Canvas1_GP( Canvas1:TGadget )
			End Select

		Case EVENT_TIMERTICK
			Select EventSource()
				Case UpdateCanvas	UpdateCanvas_Timer( UpdateCanvas:TTimer )
			End Select

	End Select
Forever

Function Window1_WC( Window:TGadget )
	DebugLog "Window Window1 wants to be closed"
'	HideGadget( Window:TGadget )

	End
End Function

Function Canvas1_MM( Canvas:TGadget , x:Int , y:Int )
	Scroll=0
	If x<ScrollBoarder Then Scroll:|1
	If x>(ClientWidth(Canvas)-ScrollBoarder) Then Scroll:|2
	If y<ScrollBoarder Then Scroll:|4
	If y>(ClientHeight(Canvas)-ScrollBoarder) Then Scroll:|8
End Function

Function Canvas1_GP( Canvas:TGadget )
	DebugLog "Canvas Canvas1 needs to be redrawn"
	SetGraphics CanvasGraphics ( Canvas )
	SetViewport 0,0,GadgetWidth( Canvas ),GadgetHeight( Canvas )
	Redraw(Canvas)
End Function

Function UpdateCanvas_Timer( Timer:TTimer )
	'DebugLog "Timer UpdateCanvas ticked"
	Redraw(Canvas1)
End Function

Function Redraw(Canvas:TGadget)
	SetGraphics CanvasGraphics ( Canvas )
	SetViewport 0,0,GadgetWidth( Canvas ),GadgetHeight( Canvas )
	Cls
	If Scroll&1 Then Xoffset:-2
	If Scroll&2 Then Xoffset:+2
	If Scroll&4 Then Yoffset:-2
	If Scroll&8 Then Yoffset:+2
 	DrawImage background,(400-mapXcenter)+Xoffset,(300-mapYcenter)+Yoffset

	Flip
End Function