Zoom and pan.

BlitzMax Forums/BlitzMax Programming/Zoom and pan.

Sanctus(Posted 2009) [#1]
Hey guys.
Sorry to post again but I have a intresting problem and I just can't figure it out.
I'm trying to make a little map editor (2D) and thus I'd like to use pan and zoom.
So I have these variables to hold my zoom and pan
Global Offx:Float = 0
Global Offy:Float = 0
Global Zoom:Float = 1

In my Draw I use this thing
SetScale(TApp.Zoom, TApp.Zoom)
DrawImage (TImage(map.tileset.TileList.ValueAtIndex(grid[I, J].img)), (sx - TApp.Offx) * TApp.Zoom, (sy - TApp.Offy) * TApp.Zoom)


sx and sy are the basic tile positions (It's isometric and I have to calculate it)

Works fine when I pan, but when I zoom in and out I'd like it to zoom around the place where my mouse is so I'm guessing that has to modify my offset but I can't figure out how to do that.
Could anyone be so nice to help me?


matibee(Posted 2009) [#2]
Like you say, your zoom value affects your offsets too. Something like..

SetScale(TApp.Zoom, TApp.Zoom)
DrawImage (TImage(map.tileset.TileList.ValueAtIndex(grid[I, J].img)), sx - (TApp.Offx * TApp.Zoom), sy - (TApp.Offy * TApp.Zoom) )



Sanctus(Posted 2009) [#3]
I tried this the first time and the result is even worse...

Look at this. The distance between the tiles stays the same now.
sx and sy must really be multiplied with the zoom.
There is something with the offx and offy that's not right.


Midimaster(Posted 2009) [#4]
test this:

write before "DrawImage.....

Print "before: " + sx - TApp.Offx
Print "after: " + (sx - TApp.Offx) * TApp.Zoom


.. to make sure, that the values are as expected


matibee(Posted 2009) [#5]
Try adjusting the sx & sy according to the zoom value too...

SetScale(TApp.Zoom, TApp.Zoom)
DrawImage (TImage(map.tileset.TileList.ValueAtIndex(grid[I, J].img)), (sx * TApp.Zoom) - (TApp.Offx * TApp.Zoom), (sy * TApp.Zoom) - (TApp.Offy * TApp.Zoom) )


If possible post more code so we can test what were offering :)


_JIM(Posted 2009) [#6]
Well, I gave him a very handy solution but I'll post it here if anyone needs it:



In order to use it, you have to basically call 3 methods:

AttachTo(gadget) - use it when you want the control to handle a specific gadget (used on canvases I presume :) )
SetUp() - just after you attached it for the first time. This resets the parameters
Update() - call per frame to have the zoom update

When you draw your images, all you have to do is:

DrawImage img, imgx * ZPC.ScaleFactor + ZPC.OX, imgy * ZPC.ScaleFactor + ZPC.OY


As always, code is free to use in any way. Credits are welcome :)