Page 3 of 3

Re: Keep a window at a certain aspect ratio

Posted: Thu Apr 06, 2023 1:12 am
by jacdelad
idle wrote: Thu Apr 06, 2023 12:44 am
jacdelad wrote: Thu Apr 06, 2023 12:02 am Thanks and yeah, it's great. I just need to add some code to make it work with WindowBounds(). This doesn't work yet.
I tried that too and wasn't sure what was going on with the windowbounds function but you can do it in the callback adding the min and max parameters to your structure and then do the fiddling in the cases if result > min or result > max
Yeah, that's what I'm basically trying, but I can't get it to work, somehow...I give up for today and do some other stuff.
Edit: Lol, the solution is actually very easy. Will post it when I'm done.

Re: Keep a window at a certain aspect ratio

Posted: Thu Apr 06, 2023 1:37 am
by jacdelad
Now working with WindowBounds:

Code: Select all

Structure _AR
  Window.i
  Ratio.f
  dx.l
  dy.l
EndStructure
Global NewMap AspectRatioMap._AR()
Procedure AspectRatioCallBack(hwnd,msg,wparam,lparam);
  Protected *rc.rect,w,h  
  Select msg 
    Case #WM_SIZING  
      If FindMapElement(AspectRatioMap(),Str(hwnd))
        *rc = lparam 
        w = *rc\right - *rc\left - AspectRatioMap(Str(hwnd))\dx
        h = *rc\bottom - *rc\top - AspectRatioMap(Str(hwnd))\dy
        Select wparam 
          Case 1;Left
            *rc\bottom = *rc\top + w/AspectRatioMap(Str(hwnd))\Ratio + AspectRatioMap(Str(hwnd))\dy
          Case 2;Right
            *rc\bottom = *rc\top + w/AspectRatioMap(Str(hwnd))\Ratio + AspectRatioMap(Str(hwnd))\dy
          Case 3;Top
            *rc\right = *rc\left + h*AspectRatioMap(Str(hwnd))\Ratio + AspectRatioMap(Str(hwnd))\dx
          Case 6;Bottom
            *rc\right = *rc\left + h*AspectRatioMap(Str(hwnd))\Ratio + AspectRatioMap(Str(hwnd))\dx
          Case 4;Top Left
            If w/AspectRatioMap(Str(hwnd))\Ratio<h
              *rc\top = *rc\bottom - w/AspectRatioMap(Str(hwnd))\Ratio - AspectRatioMap(Str(hwnd))\dy
            Else
              *rc\left = *rc\right - h*AspectRatioMap(Str(hwnd))\Ratio - AspectRatioMap(Str(hwnd))\dx
            EndIf
          Case 7;Bottom Left 
            If w/AspectRatioMap(Str(hwnd))\Ratio<h
              *rc\bottom = *rc\top + w/AspectRatioMap(Str(hwnd))\Ratio + AspectRatioMap(Str(hwnd))\dy
            Else
              *rc\left = *rc\right - h*AspectRatioMap(Str(hwnd))\Ratio - AspectRatioMap(Str(hwnd))\dx
            EndIf
          Case 5;Top Right
            If w/AspectRatioMap(Str(hwnd))\Ratio<h
              *rc\top = *rc\bottom - w/AspectRatioMap(Str(hwnd))\Ratio - AspectRatioMap(Str(hwnd))\dy
            Else
              *rc\right = *rc\left + h*AspectRatioMap(Str(hwnd))\Ratio + AspectRatioMap(Str(hwnd))\dx
            EndIf
          Case 8;Bottom Right  
            If w/AspectRatioMap(Str(hwnd))\Ratio<h
              *rc\bottom = *rc\top + w/AspectRatioMap(Str(hwnd))\Ratio + AspectRatioMap(Str(hwnd))\dy
            Else
              *rc\right = *rc\left + h*AspectRatioMap(Str(hwnd))\Ratio + AspectRatioMap(Str(hwnd))\dx
            EndIf
        EndSelect
      EndIf
  EndSelect 
  ProcedureReturn #PB_ProcessPureBasicEvents
EndProcedure
Procedure SetWindowAspectRatio(Window,Enable=#True)
  Protected rect.rect,WindowID=WindowID(Window)
  If Enable
    GetWindowRect_(WindowID(Window),rect)
    AspectRatioMap(Str(WindowID))\Window=Window
    AspectRatioMap(Str(WindowID))\Ratio=WindowWidth(Window,#PB_Window_InnerCoordinate)/WindowHeight(Window,#PB_Window_InnerCoordinate)
    AspectRatioMap(Str(WindowID))\dy=rect\bottom-rect\top-WindowHeight(Window,#PB_Window_InnerCoordinate)
    AspectRatioMap(Str(WindowID))\dx=rect\right-rect\left-WindowWidth(Window,#PB_Window_InnerCoordinate)
    SetWindowCallback(@AspectRatioCallBack(),Window)
  Else
    If FindMapElement(AspectRatioMap(),Str(WindowID))
      SetWindowCallback(0,AspectRatioMap(Str(WindowID))\Window)
      DeleteMapElement(AspectRatioMap(),Str(WindowID))
    EndIf
  EndIf
EndProcedure
Procedure WindowBoundsEx(Window,minx,miny,maxx,maxy)
  Protected WindowID=WindowID(Window)
  If FindMapElement(AspectRatioMap(),Str(WindowID))
    If minx<>#PB_Ignore Or miny<>#PB_Ignore
      If minx<>#PB_Ignore And miny<>#PB_Ignore
        If miny*AspectRatioMap(Str(WindowID))\Ratio>minx:minx=miny*AspectRatioMap(Str(WindowID))\Ratio:EndIf
        If minx/AspectRatioMap(Str(WindowID))\Ratio>miny:miny=minx/AspectRatioMap(Str(WindowID))\Ratio:EndIf
      ElseIf minx=#PB_Ignore
        minx=miny/AspectRatioMap(Str(WindowID))\Ratio
      ElseIf miny=#PB_Ignore
        miny=minx*AspectRatioMap(Str(WindowID))\Ratio
      EndIf
    EndIf
    If maxx<>#PB_Ignore Or maxy<>#PB_Ignore
      If maxx<>#PB_Ignore And maxy<>#PB_Ignore
        If maxy*AspectRatioMap(Str(WindowID))\Ratio<maxx:maxx=maxy*AspectRatioMap(Str(WindowID))\Ratio:EndIf
        If maxx/AspectRatioMap(Str(WindowID))\Ratio<maxy:maxy=maxx/AspectRatioMap(Str(WindowID))\Ratio:EndIf
      ElseIf maxx=#PB_Ignore
        maxx=maxy/AspectRatioMap(Str(WindowID))\Ratio
      ElseIf maxy=#PB_Ignore
        maxy=maxx*AspectRatioMap(Str(WindowID))\Ratio
      EndIf
    EndIf
  EndIf
  WindowBounds(Window,minx,miny,maxx,maxy)
EndProcedure
Macro WindowBounds(window,minx,miny,maxx,maxy)
  WindowBoundsEx(window,minx,miny,maxx,maxy)
EndMacro

CompilerIf #PB_Compiler_IsMainFile
  EnableExplicit 
  OpenWindow(0,0,0,250,250,"Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu|#PB_Window_SizeGadget)
  SetWindowAspectRatio(0,1)
  WindowBounds(0,#PB_Ignore,200,1200,900)
  Repeat
  Until WaitWindowEvent()=#PB_Event_CloseWindow
CompilerEndIf

Re: Keep a window at a certain aspect ratio

Posted: Thu Apr 06, 2023 1:58 am
by idle
that works really well :D