Code: Select all
; Random Gadget Layout Generator (Grid-Aligned)
#WindowWidth = 880
#WindowHeight = 600
#MaxGadgets = 40
#MinSize = 120
#MaxSize = 450
#MaxTries = 300
#GridSize = 120
Structure GadgetRect
x.i
y.i
w.i
h.i
EndStructure
Global NewList Gadgets.GadgetRect()
Procedure.i SnapToGrid(value.i)
ProcedureReturn (value / #GridSize) * #GridSize
EndProcedure
Procedure.i RectsOverlap(*a.GadgetRect, *b.GadgetRect)
If *a\x + *a\w <= *b\x Or *b\x + *b\w <= *a\x
ProcedureReturn #False
EndIf
If *a\y + *a\h <= *b\y Or *b\y + *b\h <= *a\y
ProcedureReturn #False
EndIf
ProcedureReturn #True
EndProcedure
Procedure.i IsNonOverlapping(*newRect.GadgetRect)
Protected *existing.GadgetRect
ForEach Gadgets()
*existing = @Gadgets()
If RectsOverlap(*newRect, *existing)
ProcedureReturn #False
EndIf
Next
ProcedureReturn #True
EndProcedure
Procedure GenerateGadgets()
Protected i, tries, rect.GadgetRect
RandomSeed(Date())
; For a = 1 To 6
For i = 0 To #MaxGadgets
tries = 0
Repeat
rect\w = SnapToGrid(Random(#MaxSize - #MinSize) + #MinSize) - 10
rect\h = SnapToGrid(Random(#MaxSize - #MinSize) + #MinSize) - 10
rect\x = SnapToGrid(Random(#WindowWidth - rect\w))
rect\y = SnapToGrid(Random(#WindowHeight - rect\h))
tries + 1
If tries > #MaxTries
Debug "brk"
Break
EndIf
Until IsNonOverlapping(@rect)
If tries <= #MaxTries
AddElement(Gadgets())
gg + 1
Gadgets() = rect
EndIf
Next
; Next
Debug gg
EndProcedure
OpenWindow(0, 50, 50, #WindowWidth, #WindowHeight, "Random Gadget Layout (Grid-aligned)", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget)
GenerateGadgets()
id = 0
ForEach Gadgets()
If Random(1) And Gadgets()\h < #GridSize And Gadgets()\w < #GridSize
ButtonGadget(id, Gadgets()\x + 20, Gadgets()\y, 90, 30, ".")
SetGadgetText(id, "Button " + Str(id))
Else
EditorGadget(id, Gadgets()\x + 20, Gadgets()\y, Gadgets()\w, Gadgets()\h)
SetGadgetColor(id, #PB_Gadget_BackColor, $F1C2C2) ;; Random($F1C2C2)
SetGadgetText(id, "Editor " + Str(id))
EndIf
id + 1
Next
Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow