Noch eine Sache zum Imagegadget: Es wäre auch schön, wenn man eine Option hätte, um beim Skalieren ein korrektes Seitenverhältnis einzuhalten.
Code: Alles auswählen
;- Top
; -----------------------------------------------------------------------------
; Name: Image Demo
; Description: Demonstration of correct aspect ration scaling
; Author: MAQ
; Date: 2024-03-23
; Version: 0.1
; PB-Version: 6.03
; OS: Windows
; Credit: Scaling Routine 2016 (c) Lunasole
; Forum:
; Created by: IceDesign
; -----------------------------------------------------------------------------
EnableExplicit
;- Enumerations
Enumeration Window
#Window_0
EndEnumeration
Enumeration Gadgets
#Gadget_0_Img_1
#Gadget_0_Check_Correct_Aspect_Ratio
EndEnumeration
Enumeration Image
#Imag_0_PureBasicLogo
EndEnumeration
Structure GadgetImages
Image.i
PressedImage.i
Width.i
Height.i
EndStructure
Global Aspect.b = #False
;- Load Images
CatchImage(#Imag_0_PureBasicLogo, ?Imag_0_PureBasicLogo)
;- Declare
Declare ScaleProportional (*res_w.Integer, *res_h.Integer, from_width, from_heigh, to_width = 0, to_heigh = 0)
Declare ResizeGadgetImage(Gadget, OriginalImage, OriginalPressedImage = #PB_Ignore)
Declare Resize_Window_0()
Declare Open_Window_0(X = 0, Y = 0, Width = 440, Height = 160)
Procedure ScaleProportional (*res_w.Integer, *res_h.Integer, from_width, from_heigh, to_width = 0, to_heigh = 0)
Protected.d hMod
If Not to_width : to_width = *res_w\i : EndIf : *res_w\i = to_width
If Not to_heigh : to_heigh = *res_h\i : EndIf : *res_h\i = to_heigh
If from_heigh > to_heigh Or from_width > to_width
hMod = from_width / to_width
If Not Round(from_heigh / hMod, #PB_Round_Nearest) > to_heigh And Not Round(from_width / hMod, #PB_Round_Nearest) > to_width
Else
hMod = from_heigh / to_heigh
EndIf
*res_w\i = from_width / hMod
*res_h\i = from_heigh / hMod
ElseIf from_heigh < to_heigh Or from_width < to_width
hMod = to_width / from_width
If Not Round(from_heigh * hMod, #PB_Round_Nearest) > to_heigh And Not Round(from_width * hMod, #PB_Round_Nearest) > to_width
Else
hMod = to_heigh / from_heigh
EndIf
*res_w\i = from_width * hMod
*res_h\i = from_heigh * hMod
EndIf
EndProcedure
Procedure ResizeGadgetImage(Gadget, OriginalImage, OriginalPressedImage = #PB_Ignore)
Protected Image, Width, Height
Static NewMap GadgetImage.GadgetImages()
If IsGadget(Gadget) And IsImage(OriginalImage)
Width = DesktopScaledX(GadgetWidth(Gadget)) : Height = DesktopScaledY(GadgetHeight(Gadget))
If Width > 0 And Height > 0
If GadgetImage(Str(Gadget))\Width <> Width Or GadgetImage(Str(Gadget))\Height <> Height
GadgetImage()\Width = Width : GadgetImage()\Height = Height
Image = CopyImage(OriginalImage, #PB_Any)
If Image
If Aspect = #True
ScaleProportional(@Width, @Height, ImageWidth(Image), ImageHeight(Image), Width, Height)
EndIf
ResizeImage(Image, Width, Height)
Select GadgetType(Gadget)
Case #PB_GadgetType_ButtonImage
SetGadgetAttribute(Gadget, #PB_Button_Image, ImageID(Image))
Case #PB_GadgetType_Image
SetGadgetState(Gadget, ImageID(Image))
EndSelect
If GadgetImage()\Image And IsImage(GadgetImage()\Image)
FreeImage(GadgetImage()\Image)
EndIf
GadgetImage()\Image = Image
EndIf
If GadgetType(Gadget) = #PB_GadgetType_ButtonImage And IsImage(OriginalPressedImage)
Image = CopyImage(OriginalPressedImage, #PB_Any)
If Image
ResizeImage(Image, Width, Height)
SetGadgetAttribute(Gadget, #PB_Button_PressedImage, ImageID(Image))
If GadgetImage()\PressedImage And IsImage(GadgetImage()\PressedImage)
FreeImage(GadgetImage()\PressedImage)
EndIf
GadgetImage()\PressedImage = Image
EndIf
EndIf
EndIf
EndIf
EndIf
EndProcedure
Procedure Resize_Window_0()
Protected Window_0_WidthIni = 440, Window_0_HeightIni = 160
Protected ScaleX.f, ScaleY.f
ScaleX = WindowWidth(#Window_0) / Window_0_WidthIni : ScaleY = WindowHeight(#Window_0) / Window_0_HeightIni
ResizeGadget(#Gadget_0_Img_1, WindowWidth(#Window_0)/2 - (ScaleX * 381)/2, ScaleY * 30, ScaleX * 381, ScaleY * 68)
ResizeGadgetImage(#Gadget_0_Img_1, #Imag_0_PureBasicLogo)
ResizeGadget(#Gadget_0_Check_Correct_Aspect_Ratio, WindowWidth(#Window_0)/2 - (ScaleX * 140)/2, ScaleY * 120, ScaleX * 140, ScaleY * 20)
EndProcedure
Procedure Open_Window_0(X = 0, Y = 0, Width = 440, Height = 160)
If OpenWindow(#Window_0, X, Y, Width, Height, "Image Demo", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_SizeGadget | #PB_Window_ScreenCentered)
ImageGadget(#Gadget_0_Img_1, 30, 30, 381, 68, ImageID(#Imag_0_PureBasicLogo))
CheckBoxGadget(#Gadget_0_Check_Correct_Aspect_Ratio, 150, 120, 140, 20, "Correct Aspect Ratio")
BindEvent(#PB_Event_SizeWindow, @Resize_Window_0(), #Window_0)
PostEvent(#PB_Event_SizeWindow, #Window_0, 0)
WindowBounds(#Window_0, 440, 160, #PB_Ignore, #PB_Ignore)
ProcedureReturn #True
EndIf
EndProcedure
;- Main Program
If Open_Window_0()
;- Event Loop
Repeat
Select WaitWindowEvent()
Case #PB_Event_CloseWindow
Break
;-> Event Gadget
Case #PB_Event_Gadget
Select EventGadget()
Case #Gadget_0_Check_Correct_Aspect_Ratio
If GetGadgetState(#Gadget_0_Check_Correct_Aspect_Ratio) = #True
Aspect = #True
Else
Aspect = #False
EndIf
EndSelect
EndSelect
ForEver
EndIf
;- DataSection
DataSection
Imag_0_PureBasicLogo: : IncludeBinary #PB_Compiler_Home + "\Examples\Sources\Data\PureBasicLogo.bmp"
EndDataSection