#!/usr/bin/wish
#
# MAMEX - MAME front End v.1
# 
# Chris Sharp <sharpc@hursley.ibm.com>
# 
# 5/9/97


wm title	. "M.A.M.E. selector"
wm iconname	. "M.A.M.E. selector"

set mamelist [split [ exec -keepnewline xmame -listfull |tail +1 ] "\n" ]
set sound 0
set FM 0
set scale 0

listbox .gameslist -relief raised -borderwidth 2 -yscrollcommand ".scroll set"

scrollbar .scroll -command ".gameslist yview"

bind .gameslist <Double-1> "runMame" 

checkbutton .sound -text sound -variable sound -anchor w 
checkbutton .fm -text FM -variable FM -anchor w 
checkbutton .trakball -text TrakBall -variable TB -anchor w 
checkbutton .joystick -text Joystick -variable JS -anchor w 

scale .factor -label scale -from 1 -to 3 -length 2c -orient horizontal -command "setScale"

button .play -text "Play" -command "runMame"
button .exit -text "Quit" -command exit

foreach i $mamelist {
  if {$i != "" } {
    set titlelist [split $i { \"} ]
    set index [string trim [lindex $titlelist 0]]
    if {$index != "NAMES:"} {
      set title [string trim [join [lrange $titlelist 1 end]]]
      set allGames($title) $index
      .gameslist insert end $title 
    }
  }
}


pack .gameslist -side left -fill both -expand 1
pack .scroll -side left -fill y
pack .sound .fm .trakball .joystick -side top -anchor w
pack .factor -side top -anchor w
pack .play .exit -side left -fill x 


proc runMame {} {
  global sound FM scale allGames TB JS
  set game "" 
  set game $allGames([selection get])
  set options ""
  if {$sound} { set options " -sound " }
  if {$FM} { set options "$options -fm " }
  if {$TB} { set options "$options -trak " }
  if {$JS} { set options "$options -joy " }
  if {$scale} { set options "$options -scale $scale" }

  eval exec xmame $game $options &
}

proc setScale value {
  global scale

  set scale [ .factor get ]
}


