SetPlayerData(GetThisPlayer(), "RaceName", "terran")

-- Global useful objects for menus  ----------
dark = Color(38, 38, 78)
clear = Color(200, 200, 120)
black = Color(0, 0, 0)

bckground = CGraphic:New("ui/Menu background without title.png")
bckground:Load()
bckground:Resize(Video.Width, Video.Height)
backgroundWidget = ImageWidget(bckground)

-- button large
g_tbln = CGraphic:New("ui/terran/agg/button_large.png")
g_tbln:Load()
g_tblp = CGraphic:New("ui/terran/agg/button_large_pressed.png")
g_tblp:Load()
g_tblg = CGraphic:New("ui/terran/agg/button_large_disabled.png")
g_tblg:Load()

g_zbln = CGraphic:New("ui/zerg/agg/button_large.png")
g_zbln:Load()
g_zblp = CGraphic:New("ui/zerg/agg/button_large_pressed.png")
g_zblp:Load()
g_zblg = CGraphic:New("ui/zerg/agg/button_large_disabled.png")
g_zblg:Load()

g_pbln = CGraphic:New("ui/protoss/agg/button_large.png")
g_pbln:Load()
g_pblp = CGraphic:New("ui/protoss/agg/button_large_pressed.png")
g_pblp:Load()
g_pblg = CGraphic:New("ui/protoss/agg/button_large_disabled.png")
g_pblg:Load()

-- button left large
g_tblln = CGraphic:New("ui/terran/agg/button_left_large.png")
g_tblln:Load()
g_tbllp = CGraphic:New("ui/terran/agg/button_left_large_pressed.png")
g_tbllp:Load()
g_tbllg = CGraphic:New("ui/terran/agg/button_left_large_disabled.png")
g_tbllg:Load()

g_zblln = CGraphic:New("ui/zerg/agg/button_left_large.png")
g_zblln:Load()
g_zbllp = CGraphic:New("ui/zerg/agg/button_left_large_pressed.png")
g_zbllp:Load()
g_zbllg = CGraphic:New("ui/zerg/agg/button_left_large_disabled.png")
g_zbllg:Load()

g_pblln = CGraphic:New("ui/protoss/agg/button_left_large.png")
g_pblln:Load()
g_pbllp = CGraphic:New("ui/protoss/agg/button_left_large_pressed.png")
g_pbllp:Load()
g_pbllg = CGraphic:New("ui/protoss/agg/button_left_large_disabled.png")
g_pbllg:Load()

-- button small
g_tbhn = CGraphic:New("ui/terran/agg/button_small.png")
g_tbhn:Load()
g_tbhp = CGraphic:New("ui/terran/agg/button_small_pressed.png")
g_tbhp:Load()
g_tbhg = CGraphic:New("ui/terran/agg/button_small_disabled.png")
g_tbhg:Load()

g_zbhn = CGraphic:New("ui/zerg/agg/button_small.png")
g_zbhn:Load()
g_zbhp = CGraphic:New("ui/zerg/agg/button_small_pressed.png")
g_zbhp:Load()
g_zbhg = CGraphic:New("ui/zerg/agg/button_small_disabled.png")
g_zbhg:Load()

g_pbhn = CGraphic:New("ui/protoss/agg/button_small.png")
g_pbhn:Load()
g_pbhp = CGraphic:New("ui/protoss/agg/button_small_pressed.png")
g_pbhp:Load()
g_pbhg = CGraphic:New("ui/protoss/agg/button_small_disabled.png")
g_pbhg:Load()

-- button left small
g_tbhln = CGraphic:New("ui/terran/agg/button_left_small.png")
g_tbhln:Load()
g_tbhlp = CGraphic:New("ui/terran/agg/button_left_small_pressed.png")
g_tbhlp:Load()
g_tbhlg = CGraphic:New("ui/terran/agg/button_left_small_disabled.png")
g_tbhlg:Load()

g_zbhln = CGraphic:New("ui/zerg/agg/button_left_small.png")
g_zbhln:Load()
g_zbhlp = CGraphic:New("ui/zerg/agg/button_left_small_pressed.png")
g_zbhlp:Load()
g_zbhlg = CGraphic:New("ui/zerg/agg/button_left_small_disabled.png")
g_zbhlg:Load()

g_pbhln = CGraphic:New("ui/protoss/agg/button_left_small.png")
g_pbhln:Load()
g_pbhlp = CGraphic:New("ui/protoss/agg/button_left_small_pressed.png")
g_pbhlp:Load()
g_pbhlg = CGraphic:New("ui/protoss/agg/button_left_small_disabled.png")
g_pbhlg:Load()

-- button right small
g_tbhrn = CGraphic:New("ui/terran/agg/button_right_small.png")
g_tbhrn:Load()
g_tbhrp = CGraphic:New("ui/terran/agg/button_right_small_pressed.png")
g_tbhrp:Load()
g_tbhrg = CGraphic:New("ui/terran/agg/button_right_small_disabled.png")
g_tbhrg:Load()

g_zbhrn = CGraphic:New("ui/zerg/agg/button_right_small.png")
g_zbhrn:Load()
g_zbhrp = CGraphic:New("ui/zerg/agg/button_right_small_pressed.png")
g_zbhrp:Load()
g_zbhrg = CGraphic:New("ui/zerg/agg/button_right_small_disabled.png")
g_zbhrg:Load()

g_pbhrn = CGraphic:New("ui/protoss/agg/button_right_small.png")
g_pbhrn:Load()
g_pbhrp = CGraphic:New("ui/protoss/agg/button_right_small_pressed.png")
g_pbhrp:Load()
g_pbhrg = CGraphic:New("ui/protoss/agg/button_right_small_disabled.png")
g_pbhrg:Load()

function panel(n)
  return "ui/panels/" .. n .. ".png"
end



function AddMenuHelpers(menu)
  function menu:addCentered(widget, x, y)
    self:add(widget, x - widget:getWidth() / 2, y)
  end

  function menu:addLabel(text, x, y, font, center)
    local label = Label(text)
    if (font == nil) then font = Fonts["large"] end
    label:setFont(font)
    label:adjustSize()
    if (center == nil or center == true) then -- center text by default
      x = x - label:getWidth() / 2
    end
    self:add(label, x, y)

    return label
  end

  function menu:writeText(text, x, y)
    return self:addLabel(text, x, y, Fonts["game"], false)
  end

  function menu:writeLargeText(text, x, y)
    return self:addLabel(text, x, y, Fonts["large"], false)
  end

  function menu:addButton(caption, hotkey, x, y, callback, size)
    local b = ButtonWidget(caption)
    b:setHotKey(hotkey)
    b:setActionCallback(callback)
    if (size == nil) then size = {200, 24} end
    b:setSize(size[1], size[2])
    b:setBackgroundColor(dark)
    b:setBaseColor(dark)
    self:add(b, x, y)
    return b
  end
  
  function menu:addTextButton(caption, hotkey, x, y, callback, size)
    local b = ButtonWidget(caption)
    b:setFont(Fonts["font16x"])
    b:setHotKey(hotkey)
    b:setActionCallback(callback)
    if size then
      b:setSize(size[1], size[2])
    else
      b:adjustSize()
    end
    b:setBackgroundColor(Color(0, 0, 0, 0))
    b:setBaseColor(Color(0, 0, 0, 0))
    self:add(b, x, y)
    return b
  end

  function menu:addImageButton(caption, hotkey, x, y, callback)
    local b = ImageButton(caption)
    b:setBorderSize(0)
    b:setHotKey(hotkey)
    b:setActionCallback(callback)
    self:add(b, x, y)
    return b
  end

  function menu:addFullLeftButton(caption, hotkey, x, y, callback)
    local b = self:addImageButton(caption, hotkey, x, y, callback)
    if (GetPlayerData(GetThisPlayer(), "RaceName") == "terran") then
      b:setNormalImage(g_tblln)
      b:setPressedImage(g_tbllp)
      b:setDisabledImage(g_tbllg)
    elseif (GetPlayerData(GetThisPlayer(), "RaceName") == "zerg") then
      b:setNormalImage(g_zblln)
      b:setPressedImage(g_zbllp)
      b:setDisabledImage(g_zbllg)
    else
      b:setNormalImage(g_pblln)
      b:setPressedImage(g_pbllp)
      b:setDisabledImage(g_pbllg)
    end
    b:setSize(224, 28)
    return b
  end

  function menu:addFullButton(caption, hotkey, x, y, callback)
    local b = self:addImageButton(caption, hotkey, x, y, callback)
    if (GetPlayerData(GetThisPlayer(), "RaceName") == "terran") then
      b:setNormalImage(g_tbln)
      b:setPressedImage(g_tblp)
      b:setDisabledImage(g_tblg)
    elseif (GetPlayerData(GetThisPlayer(), "RaceName") == "zerg") then
      b:setNormalImage(g_zbln)
      b:setPressedImage(g_zblp)
      b:setDisabledImage(g_zblg)
    else
      b:setNormalImage(g_pbln)
      b:setPressedImage(g_pblp)
      b:setDisabledImage(g_pblg)
    end
    b:setSize(224, 28)
    return b
  end

  function menu:addHalfLeftButton(caption, hotkey, x, y, callback)
    local b = self:addImageButton(caption, hotkey, x, y, callback)
    if (GetPlayerData(GetThisPlayer(), "RaceName") == "terran") then
      b:setNormalImage(g_tbhln)
      b:setPressedImage(g_tbhlp)
      b:setDisabledImage(g_tbhlg)
    elseif (GetPlayerData(GetThisPlayer(), "RaceName") == "zerg") then
      b:setNormalImage(g_zbhln)
      b:setPressedImage(g_zbhlp)
      b:setDisabledImage(g_zbhlg)
    else
      b:setNormalImage(g_pbhln)
      b:setPressedImage(g_pbhlp)
      b:setDisabledImage(g_pbhlg)
    end
    b:setSize(104, 28)
    return b
  end

  function menu:addHalfButton(caption, hotkey, x, y, callback)
    local b = self:addImageButton(caption, hotkey, x, y, callback)
    if (GetPlayerData(GetThisPlayer(), "RaceName") == "terran") then
      b:setNormalImage(g_tbhn)
      b:setPressedImage(g_tbhp)
      b:setDisabledImage(g_tbhg)
    elseif (GetPlayerData(GetThisPlayer(), "RaceName") == "zerg") then
      b:setNormalImage(g_zbhn)
      b:setPressedImage(g_zbhp)
      b:setDisabledImage(g_zbhg)
    else
      b:setNormalImage(g_pbhn)
      b:setPressedImage(g_pbhp)
      b:setDisabledImage(g_pbhg)
    end
    b:setSize(104, 28)
    return b
  end

  function menu:addHalfRightButton(caption, hotkey, x, y, callback)
    local b = self:addImageButton(caption, hotkey, x, y, callback)
    if (GetPlayerData(GetThisPlayer(), "RaceName") == "terran") then
      b:setNormalImage(g_tbhrn)
      b:setPressedImage(g_tbhrp)
      b:setDisabledImage(g_tbhrg)
    elseif (GetPlayerData(GetThisPlayer(), "RaceName") == "zerg") then
      b:setNormalImage(g_zbhrn)
      b:setPressedImage(g_zbhrp)
      b:setDisabledImage(g_zbhrg)
    else
      b:setNormalImage(g_pbhrn)
      b:setPressedImage(g_pbhrp)
      b:setDisabledImage(g_pbhrg)
    end
    b:setSize(104, 28)
    return b
  end

  function menu:addSlider(min, max, w, h, x, y, callback)
    local b = Slider(min, max)
    b:setBaseColor(dark)
    b:setForegroundColor(clear)
    b:setBackgroundColor(clear)
    b:setSize(w, h)
    b:setActionCallback(function(s) callback(b, s) end)
    self:add(b, x, y)
    return b
  end

  function menu:addListBox(x, y, w, h, list)
    local bq = ListBoxWidget(w, h)
    bq:setList(list)
    bq:setBaseColor(black)
    bq:setForegroundColor(clear)
    bq:setBackgroundColor(dark)
    bq:setFont(Fonts["game"])
    self:add(bq, x, y)   
    bq.itemslist = list
    return bq
  end

  function menu:addBrowser(path, filter, x, y, w, h, default)
    -- Create a list of all dirs and files in a directory
    local function listfiles(path)
      local dirlist = {}
      local i
      local f
      local u = 1

      local dirs = ListDirsInDirectory(path)
      for i,f in ipairs(dirs) do
        dirlist[u] = f .. "/"
        u = u + 1
      end

      local fileslist = ListFilesInDirectory(path)
      for i,f in ipairs(fileslist) do
        if (string.find(f, filter)) then
          dirlist[u] = f
          u = u + 1
        end
      end

      return dirlist
    end

    local bq = self:addListBox(x, y, w, h, {})
    bq.origpath = path
    bq.actioncb = nil

    -- The directory changed, update the list
    function bq:updateList()
      self.itemslist = listfiles(self.path)
      if (self.path ~= self.origpath) then
        table.insert(self.itemslist, 1, "../")
      end
      self:setList(self.itemslist)
    end

    -- Change to the default directory and select the default file
    if (default == nil) then
      bq.path = path
      bq:updateList()
    else
      local i
      for i=string.len(default)-1,1,-1 do
        if (string.sub(default, i, i) == "/") then
          bq.path = string.sub(default, 1, i)
          bq:updateList()

          local f = string.sub(default, i + 1)
          for i=1,table.getn(bq.itemslist) do
            if (bq.itemslist[i] == f) then
              bq:setSelected(i - 1)
            end
          end
          break
        end
      end
    end

    function bq:getSelectedItem()
      if (self:getSelected() < 0) then
        return self.itemslist[1]
      end
      return self.itemslist[self:getSelected() + 1]
    end

    -- If a directory was clicked change dirs
    -- Otherwise call the user's callback
    local function cb(s)
      local f = bq:getSelectedItem()
      if (f == "../") then
        local i
        for i=string.len(bq.path)-1,1,-1 do
          if (string.sub(bq.path, i, i) == "/") then
            bq.path = string.sub(bq.path, 1, i)
            bq:updateList()
            break
          end
        end
      elseif (string.sub(f, string.len(f)) == '/') then
        bq.path = bq.path .. f
        bq:updateList()
      else
        if (bq.actioncb ~= nil) then
          bq:actioncb(s)
        end
      end
    end
    bq:setActionCallback(cb)

    bq.oldSetActionCallback = bq.setActionCallback
    function bq:setActionCallback(cb)
      bq.actioncb = cb
    end

    return bq
  end

  function menu:addCheckBox(caption, x, y, callback)
    local b = CheckBox(caption)
    b:setBaseColor(clear)
    b:setForegroundColor(clear)
    b:setBackgroundColor(dark)
    b:setActionCallback(function(s) callback(b, s) end)
    b:setFont(Fonts["game"])
    self:add(b, x, y)
    return b
  end

  function menu:addRadioButton(caption, group, x, y, callback)
    local b = RadioButton(caption, group)
    b:setBaseColor(dark)
    b:setForegroundColor(clear)
    b:setBackgroundColor(dark)
    b:setActionCallback(callback)
    self:add(b, x, y)
    return b
  end

  function menu:addDropDown(list, x, y, callback)
    local dd = DropDownWidget()
    dd:setFont(Fonts["game"])
    dd:setList(list)
    dd:setActionCallback(function(s) callback(dd, s) end)
    dd:setBaseColor(dark)
    dd:setForegroundColor(clear)
    dd:setBackgroundColor(dark)
    self:add(dd, x, y)
    return dd
  end

  function menu:addTextInputField(text, x, y, w)
    local b = TextField(text)
    b:setActionCallback(function() end) --FIXME: remove this?
    b:setFont(Fonts["game"])
    b:setBaseColor(clear)
    b:setForegroundColor(clear)
    b:setBackgroundColor(dark)
    if (w == nil) then w = 100 end
    b:setSize(w, 18)
    self:add(b, x, y)
    return b
  end
  
  function menu:addAnimation(filename, x, y, center)
    local mng1 = Mng:New(filename)
    if mng1 then
      mng1:Load()
      mng1:Reset()
      local im1 = ImageWidget(mng1)
      
      if center then
        x = x - (im1:getWidth() / 2)
      end
      menu:add(im1, x, y)
      
      return im1
    end
  end

  function menu:addAnimatedButton(filename, filenameOn, x, y, xOn, yOn, caption, hotkey, callback, animationIsBehind, center)
    local mng1 = Mng:New(filename)
    local im1
    local mngOn
    local imOn
    if mng1 then
      mng1:Load()
      mng1:Reset()
      im1 = ImageWidget(mng1)

      if center then
        x = x - (im1:getWidth() / 2)
      end

      if not animationIsBehind then
        self:add(im1, x, y)
      end

      mngOn = Mng:New(filenameOn)
      mngOn:Load()
      imOn = ImageWidget(mngOn)
      self:add(imOn, xOn, yOn)
      imOn:setVisible(false)

      if animationIsBehind then
        self:add(im1, x, y)
      end
    end

    local fnt = Fonts["font16x"]
    local label_max_width = fnt:Width(caption)
    local label_max_height = fnt:Height()
    local max_width = label_max_width
    local max_height = label_max_height
    if im1 then
      max_width = math.max(im1:getWidth(), label_max_width)
      max_height = math.max(im1:getHeight(), label_max_height) 
    end

    local label = MultiLineLabel(caption)
    label:setSize(max_width, max_height)
    label:setAlignment(MultiLineLabel.RIGHT)
    label:setVerticalAlignment(MultiLineLabel.BOTTOM)
    label:setFont(fnt)
    self:add(label, x, y)
    
    local button = self:addTextButton("", hotkey, x, y, callback, {max_width, max_height})

    if mngOn then
      button:setMouseCallback(function(evt, btn, cnt)
        if evt == "mouseIn" then
          mngOn:Reset()
          imOn:setVisible(true)
        elseif evt == "mouseOut" then
          imOn:setVisible(false)
        end
      end)
    end
  end

  function menu:addBottomButton(caption, hotkey, x, row, callback)
    local bckground = CGraphic:New("ui/readyt/butterr.png")
    bckground:Load()
    local backgroundWidget = ImageWidget(bckground)
    local h = backgroundWidget:getHeight()
    local x = x - (backgroundWidget:getWidth() / 2)
    local y = Video.Height - (h * 0.5 * row) - h
    menu:add(backgroundWidget, x, y)
    local btn = menu:addTextButton(caption, hotkey, x, y + 16, callback)
    btn:setSize(backgroundWidget:getWidth(), btn:getHeight())
    return btn
  end
end

function WarMenu(title, background, resize)
  local menu
  local exitButton
  local bg
  local bgg

  menu = MenuScreen()

  if background == nil then
    menu:add(backgroundWidget, 0, 0)
  elseif type(background) == "string" then
    bgg = CGraphic:New(background)
    bgg:Load()
    if (resize == nil or resize == true) then
      bgg:Resize(Video.Width, Video.Height)
    end
    menu:add(ImageWidget(bgg), 0, 0)
  else
    menu:setOpaque(true)
    menu:setBaseColor(Color(0, 0, 0, 255))
  end

  AddMenuHelpers(menu)

  if title then
    menu:addLabel(title, Video.Width / 2, Video.Height / 20, Fonts["large"])
  end

  return menu
end

-- Default configurations -------
Widget:setGlobalFont(Fonts["large"])


DefaultObjectives = {"-Destroy the enemy"}
Objectives = DefaultObjectives


-- Define the different menus ----------

function InitGameSettings()
  GameSettings.NetGameType = 1
  for i=0,PlayerMax-1 do
    GameSettings.Presets[i].Race = -1
    GameSettings.Presets[i].Team = -1
    GameSettings.Presets[i].Type = -1
  end
  GameSettings.Resources = -1
  GameSettings.NumUnits = -1
  GameSettings.Opponents = -1
  GameSettings.Terrain = -1
  GameSettings.GameType = -1
  GameSettings.NoFogOfWar = false
  GameSettings.RevealMap = 0
end
InitGameSettings()

function RunMap(map, objective, fow, revealmap)
  SetMusic(GetPlayerData(GetThisPlayer(), "RaceName"))
  if objective == nil then
    Objectives = DefaultObjectives
  else
    Objectives = objective
  end
  loop = true
  while (loop) do
    InitGameVariables()
    if fow ~= nil then
      SetFogOfWar(fow)
    end
    if revealmap == true then
       RevealMap()
    end
    
    if (preferences.RapidStratagusIDE == true) then
  Load("RapidStratagusIDE/RSI_Functions.lua")
  --RSI_MapConfiguration()
  --CreateUnit("unit-terran-command-center", 0, {0, 0})
end

    
    StartMap(map)
    if GameResult ~= GameRestart then
      loop = false
    end
  end
  RunResultsMenu(s)

  InitGameSettings()
  SetPlayerData(GetThisPlayer(), "RaceName", "terran")
  SetMusic("menu")
end

mapname = "maps/\(2\)Space Madness.smp"
local mapinfo = {
  playertypes = {nil, nil, nil, nil, nil, nil, nil, nil},
  description = "",
  nplayers = 1,
  w = 32,
  h = 32,
  id = 0
}

function GetMapInfo(mapname)
  local OldDefinePlayerTypes = DefinePlayerTypes
  local OldPresentMap = PresentMap

  function DefinePlayerTypes(p1, p2, p3, p4, p5, p6, p7, p8)
    mapinfo.playertypes[1] = p1
    mapinfo.playertypes[2] = p2
    mapinfo.playertypes[3] = p3
    mapinfo.playertypes[4] = p4
    mapinfo.playertypes[5] = p5
    mapinfo.playertypes[6] = p6
    mapinfo.playertypes[7] = p7
    mapinfo.playertypes[8] = p8

    mapinfo.nplayers = 0
    for i=0,8 do
      local t = mapinfo.playertypes[i]
      if (t == "person" or t == "computer") then
        mapinfo.nplayers = mapinfo.nplayers + 1
      end
    end
  end

  function PresentMap(description, nplayers, w, h, id)
    mapinfo.description = description
    -- nplayers includes rescue-passive and rescue-active
    -- calculate the real nplayers in DefinePlayerTypes
    --mapinfo.nplayers = nplayers
    mapinfo.w = w
    mapinfo.h = h
    mapinfo.id = id
  end

  Load(mapname)

  DefinePlayerTypes = OldDefinePlayerTypes
  PresentMap = OldPresentMap
end

function RunSelectScenarioMenu()
  local menu = WarMenu(nil, panel("384x256"), false)
  menu:setSize(384, 256)
  menu:setPosition((Video.Width - 384) / 2, (Video.Height - 256) / 2)

  menu:addLabel("Select scenario", 384/2, 8)

  local browser = menu:addBrowser("maps/", "^.*%.smp%.?g?z?$",
    32, 44, 320, 160, mapname)

  menu:addFullLeftButton("~!OK", "o", 20, 216,
    function()
      if (browser:getSelected() < 0) then
        return
      end

      mapname = browser.path .. browser:getSelectedItem()
      menu:stop()
    end)
  menu:addHalfRightButton("~!Cancel", "c", 260, 216,
    function() menu:stop() end)

  menu:run()
end

function RunInfoMenu()
  local menu = WarMenu()
  local offx = (Video.Width - 640) / 2
  local offy = (Video.Height - 480) / 2
  
  menu:addLabel("~<Game Information~>", offx + 640/2 + 12, offy + 192)
  
  menu:addFullButton("~!Back", "b", offx + 640 - 224 - 16, offy + 360 + 36*2, function() menu:stop() end)
  
  menu:addLabel(stargus.Name .. " V" .. stargus.Version .. "  " .. stargus.Homepage, offx + 320, offy + 226 + 18*0) 
  menu:addLabel("Stratagus V" .. GetStratagusVersion() .. "  " .. GetStratagusHomepage(), offx + 320, offy + 226 + 18*1)
  menu:addLabel(Copyright, offx + 320, offy + 290 + 18*4)
  
  menu:run()
end

function RunSinglePlayerGameMenu()
  local menu = WarMenu()
  local offx = (Video.Width - 640) / 2
  local offy = (Video.Height - 480) / 2
  local d
  local race
  local resources
  local opponents
  local numunits
  local gametype
  local mapl
  local descriptionl

  menu:addLabel("Scenario:", offx + 16, offy + 360, Fonts["game"], false)
  mapl = menu:addLabel(string.sub(mapname, 6), offx + 16, offy + 360 + 24, Fonts["game"], false)
  descriptionl = menu:addLabel("descriptionl", offx + 16 + 70, offy + 360, Fonts["game"], false)

  menu:addLabel("~<Single Player Game Setup~>", offx + 640/2 + 12, offy + 192)
  menu:addFullButton("S~!elect Scenario", "e", offx + 640 - 224 - 16, offy + 360 + 36*0,
    function()
      local oldmapname = mapname
      RunSelectScenarioMenu()
      if (mapname ~= oldmapname) then
        GetMapInfo(mapname)
        MapChanged()
      end
    end)
  menu:addFullButton("~!Start Game", "s", offx + 640 - 224 - 16, offy + 360 + 36*1,
    function()
      GameSettings.Presets[0].Race = race:getSelected()
      GameSettings.Resources = resources:getSelected()
      GameSettings.Opponents = opponents:getSelected()
      GameSettings.NumUnits = numunits:getSelected()
      GameSettings.GameType = gametype:getSelected() - 1
      RunMap(mapname)
      menu:stop()
    end)
  menu:addFullButton("~!Cancel Game", "c", offx + 640 - 224 - 16, offy + 360 + 36*2, function() menu:stop() end)

  menu:addLabel("~<Your Race:~>", offx + 40, offy + (10 + 240) - 20, Fonts["game"], false)
  race = menu:addDropDown({"Map Default", "Terran", "Zerg", "Protoss"}, offx + 40, offy + 10 + 240,
    function(dd) end)
  race:setSize(152, 20)

  menu:addLabel("~<Resources:~>", offx + 220, offy + (10 + 240) - 20, Fonts["game"], false)
  resources = menu:addDropDown({"Map Default", "Low", "Medium", "High"}, offx + 220, offy + 10 + 240,
    function(dd) end)
  resources:setSize(152, 20)

  menu:addLabel("~<Units:~>", offx + 640 - 224 - 16, offy + (10 + 240) - 20, Fonts["game"], false)
  numunits = menu:addDropDown({"Map Default", "One Peasant Only"}, offx + 640 - 224 - 16, offy + 10 + 240,
    function(dd) end)
  numunits:setSize(190, 20)

  local opponents_list = {"Map Default", "1 Opponent", "2 Opponents",
    "3 Opponents", "4 Opponents", "5 Opponents", "6 Opponents", "7 Opponents"}

  menu:addLabel("~<Opponents:~>", offx + 40, offy + (10 + 300) - 20, Fonts["game"], false)
  opponents = menu:addDropDown(opponents_list, offx + 40, offy + 10 + 300,
    function(dd) end)
  opponents:setSize(152, 20)

  menu:addLabel("~<Game Type:~>", offx + 220, offy + (10 + 300) - 20, Fonts["game"], false)
  gametype = menu:addDropDown({"Use map settings", "Melee", "Free for all", "Top vs bottom", "Left vs right", "Man vs Machine"}, offx + 220, offy + 10 + 300,
    function(dd) end)
  gametype:setSize(152, 20)

  function MapChanged()
    mapl:setCaption(string.sub(mapname, 6))
    mapl:adjustSize()

    descriptionl:setCaption(mapinfo.description ..
      " (" .. mapinfo.w .. " x " .. mapinfo.h .. ")")
    descriptionl:adjustSize()
 
    local o = {}
    for i=1,mapinfo.nplayers do
      table.insert(o, opponents_list[i])
    end
    opponents:setList(o)
  end

  GetMapInfo(mapname)
  MapChanged()

  menu:run()
end



function BuildProgramStartMenu()
  
  local menu = WarMenu()
  local offx = (Video.Width - 640) / 2
  local offy = (Video.Height - 480) / 2

  menu:addLabel("~white~" .. stargus.Name .. " V" .. stargus.Version, Video.Width - 60, Video.Height - 18*1) 
  
  menu:addAnimatedButton(
    "videos/mainmenu/single.mng",
    "videos/mainmenu/singleon.mng",
    100, 50,
    143, 115,
    "~light-green~S~!ingle Player",
    "s",
    function() RunCampaignGameMenu(); menu:stop(1) end,
    true
  )
  
  menu:addAnimatedButton(
    "videos/mainmenu/multi.mng",
    "videos/mainmenu/multion.mng",
    80, 250,
    99, 261,
    "~light-green~M~!ultiplayer",
    "m",
    function() RunMultiPlayerGameMenu(); menu:stop(1) end
  )
  
  menu:addAnimatedButton(
    "videos/mainmenu/exit.mng",
    "videos/mainmenu/exiton.mng",
    Video.Width - 300, Video.Height - 200,
    Video.Width - 285, Video.Height - 200,
    "~light-green~E~!xit Program",
    "x",
    function() menu:stop() end
  )

  --menu:addFullButton("~!Single Player Game", "s", offx + 208, offy + 104 + 36*0,
    --function() RunSinglePlayerGameMenu(); menu:stop(1) end)

  --menu:addFullButton("~!Multi Player Game", "m", offx + 208, offy + 104 + 36*1,
    --function() RunMultiPlayerGameMenu(); menu:stop(1) end)

  --menu:addFullButton("~!Campaign Game", "c", offx + 208, offy + 104 + 36*2,
    --function() RunCampaignGameMenu(); menu:stop(1) end)

  --menu:addFullButton("~!Load Game", "l", offx + 208, offy + 104 + 36*3,
    --function() RunLoadGameMenu(); menu:stop(1) end)
    
  --menu:addFullButton("~!Replay Game", "r", offx + 208, offy + 104 + 36*4,
    --function() RunReplayGameMenu(); menu:stop(1) end)
    
  --menu:addFullButton("~!Options", "o", offx + 208, offy + 104 + 36*5,
  --  function() RunOptionsMenu(); menu:stop(1) end)
  
    --menu:addFullButton("E~!xit Program", "x", offx + 208, offy + 104 + 36*8,
    --function() menu:stop() end)
  
  -- menu:addTextButton("~light-green~C~white~ampaign Game", "c", 100, Video.Height-120, function() RunCampaignGameMenu(); menu:stop(1) end)
  
  -- menu:addTextButton("~light-green~L~white~oad Game", "l", 100, Video.Height-100, function() RunReplayGameMenu(); menu:stop(1) end)
    
  -- menu:addTextButton("~light-green~R~white~eplay Game", "r", 100, Video.Height-80, function() RunReplayGameMenu(); menu:stop(1) end)
    
  -- TODO: merge video options and all needed widgets from wargus
  --menu:addTextButton("~light-green~V~white~ideo Options", "o", 100, Video.Height-80, function() BuildVideoOptionsMenu(); menu:stop(1) end)
  
  menu:addTextButton("~light-green~O~white~ptions", "o", 100, Video.Height-60, function() RunOptionsMenu(); menu:stop(1) end)
  
  menu:addTextButton("~white~Show ~light-green~I~white~nfo", "i", 100, Video.Height-40, function() RunInfoMenu(); menu:stop(1) end)
  
  menu:addTextButton("~white~Show Cre~light-green~d~white~its", "d", 100, Video.Height-20, RunShowCreditsMenu)

  return menu:run()
end



LoadGameFile = nil

function RunProgramStartMenu()
  local continue = 1

  while continue == 1 do
    if (LoadGameFile ~= nil) then
      LoadGame(LoadGameFile)
    else
      continue = BuildProgramStartMenu(menu)
    end
  end
end


Load("scripts/menus/campaign.lua")
Load("scripts/menus/load.lua")
Load("scripts/menus/save.lua")
Load("scripts/menus/replay.lua")
Load("scripts/menus/options.lua")
Load("scripts/menus/credits.lua")
Load("scripts/menus/game.lua")
Load("scripts/menus/help.lua")
Load("scripts/menus/objectives.lua")
Load("scripts/menus/endscenario.lua")
Load("scripts/menus/diplomacy.lua")
Load("scripts/menus/results.lua")
Load("scripts/menus/network.lua")

if SetShader then
  SetShader("none")
end

if (preferences.RapidStratagusIDE == true) then
  Load("RapidStratagusIDE/RSI_Functions.lua")
  RSI_MapStarter()
end
  
RunProgramStartMenu()

