Dynamic Image Scaling
Blitz3D Forums/Blitz3D Programming/Dynamic Image Scaling
| ||
Hey all, I'm trying to create a little app which allows you to position and scale images. Eventually it will be able to save & crop them as well, but I'm stuck on something I was hoping would be simple!!! I can't seem to get the images to scale proportionally at all. Right now I've set up handles on the corners and when you click and drag the corner it works fine to stretch and squash the image, simply using the distance from the opposite corner to the mouse cursor, but I need it to scale proportionally. Any ideas? I've tried a bunch of different math, but nothing has worked like i was hoping it would. The program is built using nSprite for sprites, which allows for pixel perfect precision in 3d. I'll post it if anyone wants to try it. You'll have to supply your own "Test.jpg" for it to run, and you'll have to have nsprite in your decls & as an include.... Include "nsprite.bb" Graphics3D 640,480,0,2 Global cam = CreateCamera() PositionEntity cam,0,10,-40 l=CreateLight() Type img Field image Field width# Field height# Field x Field y Field proportion Field maskedimage Field maskwidth Field maskheight Field maskx Field masky Field scalefactor# Field scalex# Field scaley# Field initwidth Field initheight Field currentwidth Field currentheight End Type TFormFilter 1 Global selectedhandle Global editmode$ = "move" Global xdist Global ydist Global nudgex Global nudgey Global scaleing Global snap = 1 Global showoutlines = 1 Global rightanchor Global bottomanchor Global leftanchor Global topanchor nS_Initialize() i.img = New img i\image = nS_LoadImage("test.jpg") i\width = nS_ImageWidth(i\image) i\height = nS_ImageHeight(i\image) i\initwidth = i\width i\initheight = i\height i\proportion = i\width/i\height i\maskwidth = 100 i\maskheight = 100 i\maskx = 200 i\masky = 200 i\scalefactor = Float(i\maskwidth)/Float(i\width) nS_ResizeImage i\image,Int(i\width*i\scalefactor),Int(i\height*i\scalefactor) ;i\maskedimage = CreateImage(i\maskwidth,i\maskheight) selectedhandle = Handle(i) i\width = nS_ImageWidth(i\image) i\height = nS_ImageHeight(i\image) While Not KeyHit(1) If MouseHit(1) For i.img = Each img If MouseX() > i\x-4 And MouseX() < i\x+i\width+4 If MouseY() > i\y-4 And MouseY() < i\y+i\height+4 selectedhandle = Handle(i) xdist = MouseX() - i\x ydist = MouseY() - i\y If MouseX() > i\x-4 And MouseX() < i\x+4 And MouseY() >i\y-4 And MouseY() < i\y+4 scaleing = True scalecorner = 1 rightanchor = i\x+i\width bottomanchor = i\y+i\height i\currentwidth = i\width i\currentheight = i\height Else scaleing = False EndIf Else selectedhandle = 0 EndIf Else selectedhandle = 0 EndIf Next EndIf If MouseDown(1) If scaleing=False moveimg() Else scaleimg(1) moveimg() EndIf EndIf nudgeimg() snapimg() For i.img = Each img i\scalex = i\width/270 i\scaley = i\height/325 nS_DrawImage(i\image,i\x,i\y) nS_ResizeImage(i\image,i\scalex,i\scaley,True,False) Next UpdateWorld RenderWorld ;drawoutlines() drawhandles() Flip Wend End Function moveimg() For i.img = Each img If Handle(i) = selectedhandle i\x = MouseX()-xdist i\y = MouseY()-ydist EndIf Next End Function Function nudgeimg() nudgex = 0 nudgey = 0 If KeyDown(203) nudgex = -1 EndIf If KeyDown(205) nudgex = 1 EndIf If KeyDown(208) nudgey = 1 EndIf If KeyDown(200) nudgey = -1 EndIf If KeyDown(42) Or KeyDown(54) nudgex = nudgex*6 nudgey = nudgey*6 EndIf For i.img = Each img If Handle(i) = selectedhandle i\x = i\x+nudgex i\y = i\y+nudgey EndIf Next End Function Function snapimg() For i.img = Each img If Handle(i) = selectedhandle If snap = 1 If Abs(i\x-i\maskx) <=5 i\x = i\maskx EndIf If Abs(i\y-i\masky) <=5 i\y = i\masky EndIf If Abs((i\x+i\width)-(i\maskx+i\maskwidth)) <=5 i\x = (i\maskx+i\maskwidth)-i\width EndIf If Abs((i\y+i\height)-(i\masky+i\maskheight)) <=5 i\y = (i\masky+i\maskheight)-i\height EndIf EndIf EndIf Next End Function Function scaleimg(corner) For i.img = Each img If Handle(i) = selectedhandle Select corner Case 1 Color 255,255,0 Rect i\x-4,i\y-4,8,8 i\width = rightanchor-MouseX() i\height = bottomanchor-MouseY() Case 2 Case 3 Case 4 End Select EndIf Next End Function Function drawoutlines() For i.img = Each img If Handle(i) = selectedhandle Color 255,0,0 Rect i\x,i\y,i\width,i\height,0 Color 255,255,0 Rect i\maskx,i\masky,i\maskwidth,i\maskheight,0 EndIf Next End Function Function drawhandles() For i.img = Each img If Handle(i) = selectedhandle Color 0,255,0 Rect i\x-4,i\y-4,8,8 Rect i\x-4,i\y+i\height-4,8,8 Rect i\x+i\width-4,i\y-4,8,8 Rect i\x+i\width-4,i\y+i\height-4,8,8 EndIf Next End Function |