///////////////////////////////////////////////////////////////////////////////
//  Server Browser                                                           //
///////////////////////////////////////////////////////////////////////////////

_sb_mm = 0
_sb_mode = 0
_sb_ping = 9999
_sb_players = -1
_sb_sort = 1
_sb_showwait = 1

// quickfilterlist ["NAME" [children] [action] ..] <COLUMNS> <X PADDING> <Y PADDING> <BUTTON WIDTH> <BUTTON HEIGHT>
quickfilterlist = [
   uigrid $arg2 $arg3 $arg4 [
      looplist3 n c a $arg1 [
         if (!=s $n "") [
            UIbutton "hold2" [
               c ; uitext $n 0.6
            ] $arg5 $arg6 $a -1
         ] [uifill $arg5 $arg6]
      ]
   ]
]

UImenu "sb_filters" [
   uigrid 2 0.01 0.02 [
      uivlist 0.01 [
         UItitle [uitext "Mastermode" 0.7] 0.28 0.03
         quickfilterlist [
            "Open"     [UIcheckbox [& $_sb_mm  1] 0.016] [_sb_mm = (^ $_sb_mm  1)]
            "Veto"     [UIcheckbox [& $_sb_mm  2] 0.016] [_sb_mm = (^ $_sb_mm  2)]
            "Locked"   [UIcheckbox [& $_sb_mm  4] 0.016] [_sb_mm = (^ $_sb_mm  4)]
            "Private"  [UIcheckbox [& $_sb_mm  8] 0.016] [_sb_mm = (^ $_sb_mm  8)]
            "Password" [UIcheckbox [& $_sb_mm 16] 0.016] [_sb_mm = (^ $_sb_mm 16)]
            "Disable"  [UIradio    [= $_sb_mm  0] 0.016] [_sb_mm = 0]
         ] 2 0 0 0.14 0.032
      ]
      uivlist 0.01 [
         UItitle [uitext "Ping" 0.7] 0.28 0.03
         quickfilterlist [
            "50+"     [UIradio [= $_sb_ping   55] 0.016] [_sb_ping =   55]
            "100+"    [UIradio [= $_sb_ping  110] 0.016] [_sb_ping =  110]
            "150+"    [UIradio [= $_sb_ping  165] 0.016] [_sb_ping =  165]
            "250+"    [UIradio [= $_sb_ping  270] 0.016] [_sb_ping =  270]
            "500+"    [UIradio [= $_sb_ping  525] 0.016] [_sb_ping =  525]
            "Disable" [UIradio [= $_sb_ping 9999] 0.016] [_sb_ping = 9999]
         ] 2 0 0 0.14 0.032
      ]
      uivlist 0.01 [
         UItitle [uitext "Gamemode" 0.7] 0.28 0.03
         quickfilterlist [
            "rDM"       [UIcheckbox [& $_sb_mode  2] 0.016] [_sb_mode = (^ $_sb_mode  2)]
            "pDM"       [UIcheckbox [& $_sb_mode  4] 0.016] [_sb_mode = (^ $_sb_mode  4)]
            "rTDM"      [UIcheckbox [& $_sb_mode  8] 0.016] [_sb_mode = (^ $_sb_mode  8)]
            "pTDM"      [UIcheckbox [& $_sb_mode 16] 0.016] [_sb_mode = (^ $_sb_mode 16)]
            "rCTF"      [UIcheckbox [& $_sb_mode 32] 0.016] [_sb_mode = (^ $_sb_mode 32)]
            "pCTF"      [UIcheckbox [& $_sb_mode 64] 0.016] [_sb_mode = (^ $_sb_mode 64)]
            "Edit"      [UIcheckbox [& $_sb_mode  1] 0.016] [_sb_mode = (^ $_sb_mode  1)]
            "Disable"   [UIradio    [= $_sb_mode  0] 0.016] [_sb_mode = 0]
         ] 2 0 0 0.14 0.032
      ]
      uivlist 0.01 [
         UItitle [uitext "Players" 0.7] 0.28 0.03
         quickfilterlist [
            "Empty"   [UIradio [= $_sb_players  0] 0.016] [_sb_players =  0]
            "Full"    [UIradio [= $_sb_players  1] 0.016] [_sb_players =  1]
            "" "" ""
            "Disable" [UIradio [= $_sb_players -1] 0.016] [_sb_players = -1]
         ] 2 0 0 0.14 0.032
      ]
   ]
] [] [] [] "Server Filters"


UImenu "server_browser" [
   refreshservers
   uivlist 0.01 [
      uihlist 0 [
         uiclamp 1 1
         UIbutton "" [uitext "Update List" 0.6] 0.2 0.032 [updatefrommaster] 0 [uialign -1 0]
         uihlist 0 [
            UIbutton "" [
               UIcheckbox $searchlan 0.016
               uitext "Search LAN" 0.6
            ] 0.18 0.032 [searchlan (! $searchlan)] -1
            UIbutton "" [
               UIcheckbox $autoupdateservers 0.016
               uitext "Auto-refresh" 0.6
            ] 0.18 0.032 [autoupdateservers (! $autoupdateservers)] -1
            UIbutton "" [
               UIcheckbox $autosortservers 0.016
               uitext "Auto-sort" 0.6
            ] 0.18 0.032 [
               _sb_sort = (! $_sb_sort)
               autosortservers $_sb_sort
            ] -1
         ]
         UIbutton "" [
            UItriangle 0xCCCCCC 0.01 0.01 270
            uitext "Filters" 0.6
         ] 0.2 0.032 [showui "sb_filters"] 0 [uialign 1 0]
      ]
      uihlist 0.01 [
         uialign 1 0
         uiscroll 1.1 0.65 [
            uitarget 1.07 0.65 [
               if $_sb_sort [autosortservers (! (uihover?))]
               uivlist 0.0035 [
                  uialign 0 -1
                  uiclamp 1 1
                  loop i $numservers [
                     if (servinfovalid $i) [
                        if (&& [
                           <= (servinfoping $i) $_sb_ping
                        ] [
                           ! (& (<< 1 (servinfomastermode $i)) $_sb_mm)
                        ] [
                           ! (& (<< 1 (servinfomode $i)) $_sb_mode)
                        ] [
                           != (servinfonumplayers $i) (* $_sb_players (servinfomaxplayers $i))
                        ]) [
                           _servers_map = (servinfomap $i)
                           UIbuttonarea "" 0 0.065 [
                              uirelease [connectservinfo $i]
                              uispace 0.01 0 [
                                 uiclamp 1 1
                                 uihlist 0.01 [
                                    uialign -1
                                    uifill 0.09 0 [
                                       uivlist 0.002 [
                                          uihlist 0.006 [
                                             uitext (servinfonumplayers $i) 0.8
                                             uitext "/" 0.6
                                             uitext (servinfomaxplayers $i) 0.6
                                          ]
                                          uihlist 0.006 [
                                             uitext (servinfoping $i) 0.6
                                             uitext "ms" 0.55
                                          ]
                                       ]
                                    ]
                                    uiimage "media/interface/cube.png" 0.06 0.06 [
                                       uiimage (concatword "media/map/" $_servers_map ".png") 0.06 0.06
                                    ]
                                    uifill 0.68 0 [
                                       uiclip 0.68 0 [
                                          uialign -1
                                          uivlist 0.002 [
                                             uispace 0.01 0 [uitext (servinfodesc $i) 0.65]
                                             uialign- -1
                                             uihlist 0.015 [
                                                uialign -1
                                                uifill 0.012
                                                uihlist 0.005 [
                                                   uitext "^fs[" 0.6
                                                   uitext (servinfomastermodename $i) 0.6
                                                   uitext "^fS]" 0.6
                                                ]
                                                UItriangle 0x404040 0.01 0.01 270
                                                uitext (servinfomodename $i) 0.6
                                                if (!=s $_servers_map "") [
                                                   UItriangle 0x404040 0.01 0.01 270
                                                   uitext $_servers_map 0.6
                                                ]
                                                if (m_timed (servinfomode $i)) [
                                                   UItriangle 0x404040 0.01 0.01 270
                                                   uitext (concatword "^f8" (servinfotime $i)) 0.6
                                                ]
                                             ]
                                          ]
                                       ]
                                       UIbar 0 1 ; uialign- 1
                                    ]
                                 ]
                                 uihlist 0.005 [
                                    uialign 1
                                    uivlist 0.004 [
                                       uicolortext (servinfoname $i) 0x808080 0.55
                                       uicolortext (servinfoport $i) 0x808080 0.55
                                       uialign* 1
                                    ]
                                    if (&& [=s $connectedip (servinfoname $i)] [= $connectedport (servinfoport $i)]) [
                                       UItriangle 0xCCCCCC 0.04 0.015 90
                                    ] [uifill 0.015 0]
                                 ]
                              ]
                           ]
                        ]
                     ] [
                        if $_sb_showwait [
                           // could offer customised messages one day
                           UIbutton "" [uitext (servinfodesc $i) 0.6] 0 0.065
                        ]
                     ]
                  ]
                  uiclamp* 1 1
               ]
            ]
         ]
         UIvscroll 0.02 0.65 1.3
      ]
   ]
] [initservers] [] [] "Server Browser"
