I am programming a small memory game containing 54 cards (9x6 boxes) and a status bar on a desktop. I want to allow resizing of the windows, but the aspect ratio should be kept.
I tried to calculate the height of the window, if the width has been changed and vice versa, but this does not work. So I have added a flag "sensible" and it got a little bit better, but the window size is still very unstable and even worse, the window does not get the right size (the red status box is seen within the cards).
I also wanted to have something like a snapping function, so that the width could only be changed by 9 and the height by 6 pixels (because of the 9 x 6 cards), but failed completely.
Should I give up and add some buttons for increasing/decreasing the window size?
Code: Select all
Procedure Init()
Global WinID
Global WinX,WinY
Global MaxX=GetSystemMetrics_(#SM_CXFULLSCREEN)
Global MaxY=GetSystemMetrics_(#SM_CYFULLSCREEN)
Global CardSize,CardTotal
Global sensible=#True
#Space=10
#Status=50
#MinX=640
#MinY=480
WinX=640
WinY=480
Enumeration
#Win
#Desk
EndEnumeration
WinID=OpenWindow(#Win,0,0,WinX,WinY,"Memory",#PB_Window_SystemMenu|#PB_Window_SizeGadget|#PB_Window_ScreenCentered)
WindowBounds(#Win,#MinX,#MinY,#MinX<<1,#MinY<<1)
CanvasGadget(#Desk,0,0,WinX,WinY)
EndProcedure
Procedure Draw()
Protected i,j,n,x,y
CardSize=(WinX-(#Space*10))/9
CardTotal=CardSize+#Space
StartDrawing(CanvasOutput(#Desk))
Box(0,0,WinX,WinY,#Yellow)
n=0
y=#Space
For j=0 To 5
x=#Space
For i=0 To 8
n+1
Box(x,y,CardSize,CardSize,#Black)
x+CardTotal
Next i
y+CardTotal
Next j
Box(#Space,WinY-50,WinX-#Space<<1,40,#Red)
StopDrawing()
EndProcedure
Procedure NewGame()
Draw()
EndProcedure
Procedure Main()
Init()
NewGame()
Repeat
Select WaitWindowEvent()
Case #PB_Event_SizeWindow
If sensible
x=WindowWidth(0)
y=WindowHeight(0)
If x<>WinX
If x<#MinX
x=#MinX
ElseIf x>MaxX
x=MaxX
EndIf
WinX=x
y=(x-10*#Space)/9
y=y*6+7*#Space+#Status
If y<>WinY
WinY=y
sensible=#False
ResizeWindow(#Win,#PB_Ignore,#PB_Ignore,#PB_Ignore,WinY)
EndIf
ElseIf y<>WinY
If y<#MinY
y=#MinY
ElseIf y>MaxY
y=MaxY
EndIf
WinY=y
x=(y-7*#Space-#Status)/6
x=x*9+10*#Space
If x<>WinX
WinX=x
sensible=#False
ResizeWindow(#Win,#PB_Ignore,#PB_Ignore,WinX,#PB_Ignore)
EndIf
EndIf
If sensible=#False
ResizeGadget(#Desk,#PB_Ignore,#PB_Ignore,WinX,WinY)
Draw()
EndIf
Else
sensible=#True
EndIf
Case #WM_CHAR
quit=#True
EndSelect
Until quit
EndProcedure
Main()