Random gadget layout generator

Share your advanced PureBasic knowledge/code with the community.
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Random gadget layout generator

Post by firace »

Just a quick experiment. Each run will produce a different layout.

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 
User avatar
minimy
Enthusiast
Enthusiast
Posts: 551
Joined: Mon Jul 08, 2013 8:43 pm
Location: off world

Re: Random gadget layout generator

Post by minimy »

It's a good solution so that they can never learn how to use your app. :lol:
Don't give it a star rating! :mrgreen:

Seriously, as an experiment it's very interesting. Even to apply it in demo software as a nuisance to force to buy.
Thanks for share
If translation=Error: reply="Sorry, Im Spanish": Endif
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Random gadget layout generator

Post by firace »

Thanks for the feedback, glad you like it :)
firace
Addict
Addict
Posts: 946
Joined: Wed Nov 09, 2011 8:58 am

Re: Random gadget layout generator

Post by firace »

dark mode version (still working on it):

Image
Post Reply