context: #region Copyright & License Information
  /*
   * Copyright 2007-2013 The OpenRA Developers (see AUTHORS)
   * This file is part of OpenRA, which is free software. It is made
   * available to you under the terms of the GNU General Public License
   * as published by the Free Software Foundation. For more information,
   * see COPYING.
   */
  #endregion
  
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using OpenRA.FileFormats.Graphics;
  using OpenRA.GameRules;
  using OpenRA.Widgets;
  
  namespace OpenRA.Mods.RA.Widgets.Logic
  {
  	public class SettingsMenuLogic
  	{
  		Widget bg;
  
  		public SettingsMenuLogic()
  		{
  			bg = Ui.Root.Get<BackgroundWidget>("SETTINGS_MENU");
  			var tabs = bg.Get<ContainerWidget>("TAB_CONTAINER");
  
  			//Tabs
  			tabs.Get<ButtonWidget>("GENERAL").OnClick = () => FlipToTab("GENERAL_PANE");
  			tabs.Get<ButtonWidget>("AUDIO").OnClick = () => FlipToTab("AUDIO_PANE");
  			tabs.Get<ButtonWidget>("DISPLAY").OnClick = () => FlipToTab("DISPLAY_PANE");
  			tabs.Get<ButtonWidget>("KEYS").OnClick = () => FlipToTab("KEYS_PANE");
  			tabs.Get<ButtonWidget>("DEBUG").OnClick = () => FlipToTab("DEBUG_PANE");
  			FlipToTab("GENERAL_PANE");
  
  			//General
  			var general = bg.Get("GENERAL_PANE");
  
  			var name = general.Get<TextFieldWidget>("NAME");
  			name.Text = Game.Settings.Player.Name;
  			name.OnLoseFocus = () =>
  			{
  				name.Text = name.Text.Trim();
  
  				if (name.Text.Length == 0)
  					name.Text = Game.Settings.Player.Name;
  				else
  					Game.Settings.Player.Name = name.Text;
  			};
  			name.OnEnterKey = () => { name.LoseFocus(); return true; };
  
  			var edgescrollCheckbox = general.Get<CheckboxWidget>("EDGE_SCROLL");
  			edgescrollCheckbox.IsChecked = () => Game.Settings.Game.ViewportEdgeScroll;
  			edgescrollCheckbox.OnClick = () => Game.Settings.Game.ViewportEdgeScroll ^= true;
  
  			var edgeScrollSlider = general.Get<SliderWidget>("EDGE_SCROLL_AMOUNT");
  			edgeScrollSlider.Value = Game.Settings.Game.ViewportEdgeScrollStep;
  			edgeScrollSlider.OnChange += x => Game.Settings.Game.ViewportEdgeScrollStep = x;
  
  			var inversescroll = general.Get<CheckboxWidget>("INVERSE_SCROLL");
  			inversescroll.IsChecked = () => Game.Settings.Game.MouseScroll == MouseScrollType.Inverted;
  			inversescroll.OnClick = () => Game.Settings.Game.MouseScroll = (Game.Settings.Game.MouseScroll == MouseScrollType.Inverted) ? MouseScrollType.Standard : MouseScrollType.Inverted;
  
  			var teamchatCheckbox = general.Get<CheckboxWidget>("TEAMCHAT_TOGGLE");
  			teamchatCheckbox.IsChecked = () => Game.Settings.Game.TeamChatToggle;
  			teamchatCheckbox.OnClick = () => Game.Settings.Game.TeamChatToggle ^= true;
  
  			var showShellmapCheckbox = general.Get<CheckboxWidget>("SHOW_SHELLMAP");
  			showShellmapCheckbox.IsChecked = () => Game.Settings.Game.ShowShellmap;
  			showShellmapCheckbox.OnClick = () => Game.Settings.Game.ShowShellmap ^= true;
  
  			var useClassicMouseStyleCheckbox = general.Get<CheckboxWidget>("USE_CLASSIC_MOUSE_STYLE_CHECKBOX");
  			useClassicMouseStyleCheckbox.IsChecked = () => Game.Settings.Game.UseClassicMouseStyle;
  			useClassicMouseStyleCheckbox.OnClick = () => Game.Settings.Game.UseClassicMouseStyle ^= true;
  
  			// Audio
  			var audio = bg.Get("AUDIO_PANE");
  			var soundSettings = Game.Settings.Sound;
  
  			var soundslider = audio.Get<SliderWidget>("SOUND_VOLUME");
  			soundslider.OnChange += x => Sound.SoundVolume = x;
  			soundslider.Value = Sound.SoundVolume;
  
  			var musicslider = audio.Get<SliderWidget>("MUSIC_VOLUME");
  			musicslider.OnChange += x => Sound.MusicVolume = x;
  			musicslider.Value = Sound.MusicVolume;
  
  			var videoslider = audio.Get<SliderWidget>("VIDEO_VOLUME");
  			videoslider.OnChange += x => Sound.VideoVolume = x;
  			videoslider.Value = Sound.VideoVolume;
  
  			var cashticksdropdown = audio.Get<DropDownButtonWidget>("CASH_TICK_TYPE");
  			cashticksdropdown.OnMouseDown = _ => ShowSoundTickDropdown(cashticksdropdown, soundSettings);
  			cashticksdropdown.GetText = () => soundSettings.SoundCashTickType == SoundCashTicks.Extreme ?
  				"Extreme" : soundSettings.SoundCashTickType == SoundCashTicks.Normal ? "Normal" : "Disabled";
  
  			var mapMusicCheckbox = audio.Get<CheckboxWidget>("MAP_MUSIC_CHECKBOX");
  			mapMusicCheckbox.IsChecked = () => Game.Settings.Sound.MapMusic;
  			mapMusicCheckbox.OnClick = () => Game.Settings.Sound.MapMusic ^= true;
  
  			var soundEngineDropdown = audio.Get<DropDownButtonWidget>("SOUND_ENGINE");
  			soundEngineDropdown.OnMouseDown = _ => ShowSoundEngineDropdown(soundEngineDropdown, soundSettings);
  			soundEngineDropdown.GetText = () => soundSettings.Engine == "AL" ?
  				"OpenAL" : soundSettings.Engine == "Null" ? "None" : "OpenAL";
  
  			
  			// Display
  			var display = bg.Get("DISPLAY_PANE");
  			var gs = Game.Settings.Graphics;
  
  			var GraphicsRendererDropdown = display.Get<DropDownButtonWidget>("GRAPHICS_RENDERER");
  			GraphicsRendererDropdown.OnMouseDown = _ => ShowRendererDropdown(GraphicsRendererDropdown, gs);
  			GraphicsRendererDropdown.GetText = () => gs.Renderer == "Gl" ?
  				"OpenGL" : gs.Renderer == "Cg" ? "Cg Toolkit" : "OpenGL";
  
  			var windowModeDropdown = display.Get<DropDownButtonWidget>("MODE_DROPDOWN");
  			windowModeDropdown.OnMouseDown = _ => ShowWindowModeDropdown(windowModeDropdown, gs);
  			windowModeDropdown.GetText = () => gs.Mode == WindowMode.Windowed ?
  				"Windowed" : gs.Mode == WindowMode.Fullscreen ? "Fullscreen" : "Pseudo-Fullscreen";
  
  			display.Get("WINDOW_RESOLUTION").IsVisible = () => gs.Mode == WindowMode.Windowed;
  			var windowWidth = display.Get<TextFieldWidget>("WINDOW_WIDTH");
  			windowWidth.Text = gs.WindowedSize.X.ToString();
  
  			var windowHeight = display.Get<TextFieldWidget>("WINDOW_HEIGHT");
  			windowHeight.Text = gs.WindowedSize.Y.ToString();
  
  			var pixelDoubleCheckbox = display.Get<CheckboxWidget>("PIXELDOUBLE_CHECKBOX");
  			pixelDoubleCheckbox.IsChecked = () => gs.PixelDouble;
  			pixelDoubleCheckbox.OnClick = () =>
  			{
  				gs.PixelDouble ^= true;
  				Game.viewport.Zoom = gs.PixelDouble ? 2 : 1;
  			};
  
  			var capFrameRateCheckbox = display.Get<CheckboxWidget>("CAPFRAMERATE_CHECKBOX");
  			capFrameRateCheckbox.IsChecked = () => gs.CapFramerate;
  			capFrameRateCheckbox.OnClick = () => gs.CapFramerate ^= true;
  
  			var maxFrameRate = display.Get<TextFieldWidget>("MAX_FRAMERATE");
  			maxFrameRate.Text = gs.MaxFramerate.ToString();
  
  			// Keys
  			var keys = bg.Get("KEYS_PANE");
  			var keyConfig = Game.Settings.Keys;
  
  			var specialHotkeyList = keys.Get<ScrollPanelWidget>("SPECIALHOTKEY_LIST");
  			var specialHotkeyTemplate = specialHotkeyList.Get<ScrollItemWidget>("SPECIALHOTKEY_TEMPLATE");
  
  			var pauseKey = ScrollItemWidget.Setup(specialHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(pauseKey, "Pause the game:", () => keyConfig.PauseKey, k => keyConfig.PauseKey = k);
  			specialHotkeyList.AddChild(pauseKey);
  
  			var viewportToBase = ScrollItemWidget.Setup(specialHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(viewportToBase, "Move Viewport to Base:", () => keyConfig.CycleBaseKey, k => keyConfig.CycleBaseKey = k);
  			specialHotkeyList.AddChild(viewportToBase);
  
  			var lastEventKey = ScrollItemWidget.Setup(specialHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(lastEventKey, "Move Viewport to Last Event:", () => keyConfig.ToLastEventKey, k => keyConfig.ToLastEventKey = k);
  			specialHotkeyList.AddChild(lastEventKey);
  
  			var viewportToSelectionKey = ScrollItemWidget.Setup(specialHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(viewportToSelectionKey, "Move Viewport to Selection:", () => keyConfig.ToSelectionKey, k => keyConfig.ToSelectionKey = k);
  			specialHotkeyList.AddChild(viewportToSelectionKey);
  			
  			var sellKey = ScrollItemWidget.Setup(specialHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(sellKey, "Switch to Sell-Cursor:", () => keyConfig.SellKey, k => keyConfig.SellKey = k);
  			specialHotkeyList.AddChild(sellKey);
  
  			var powerDownKey = ScrollItemWidget.Setup(specialHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(powerDownKey, "Switch to Power-Down-Cursor:", () => keyConfig.PowerDownKey, k => keyConfig.PowerDownKey = k);
  			specialHotkeyList.AddChild(powerDownKey);
  
  			var repairKey = ScrollItemWidget.Setup(specialHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(repairKey, "Switch to Repair-Cursor:", () => keyConfig.RepairKey, k => keyConfig.RepairKey = k);
  			specialHotkeyList.AddChild(repairKey);
  
  			var unitCommandHotkeyList = keys.Get<ScrollPanelWidget>("UNITCOMMANDHOTKEY_LIST");
  			var unitCommandHotkeyTemplate = unitCommandHotkeyList.Get<ScrollItemWidget>("UNITCOMMANDHOTKEY_TEMPLATE");
  
  			var attackKey = ScrollItemWidget.Setup(unitCommandHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(attackKey, "Attack Move:", () => keyConfig.AttackMoveKey, k => keyConfig.AttackMoveKey = k);
  			unitCommandHotkeyList.AddChild(attackKey);
  
  			var stopKey = ScrollItemWidget.Setup(unitCommandHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(stopKey, "Stop:", () => keyConfig.StopKey, k => keyConfig.StopKey = k);
  			unitCommandHotkeyList.AddChild(stopKey);
  
  			var scatterKey = ScrollItemWidget.Setup(unitCommandHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(scatterKey, "Scatter:", () => keyConfig.ScatterKey, k => keyConfig.ScatterKey = k);
  			unitCommandHotkeyList.AddChild(scatterKey);
  
  			var stanceCycleKey = ScrollItemWidget.Setup(unitCommandHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(stanceCycleKey, "Cycle Stance:", () => keyConfig.StanceCycleKey, k => keyConfig.StanceCycleKey = k);
  			unitCommandHotkeyList.AddChild(stanceCycleKey);
  
  			var deployKey = ScrollItemWidget.Setup(unitCommandHotkeyTemplate, () => false, () => {});
  			SetupKeyBinding(deployKey, "Deploy:", () => keyConfig.DeployKey, k => keyConfig.DeployKey = k);
  			unitCommandHotkeyList.AddChild(deployKey);
  
  			// Debug
  			var debug = bg.Get("DEBUG_PANE");
  
  			var perfgraphCheckbox = debug.Get<CheckboxWidget>("PERFGRAPH_CHECKBOX");
  			perfgraphCheckbox.IsChecked = () => Game.Settings.Debug.PerfGraph;
  			perfgraphCheckbox.OnClick = () => Game.Settings.Debug.PerfGraph ^= true;
  
  			var perftextCheckbox = debug.Get<CheckboxWidget>("PERFTEXT_CHECKBOX");
  
gt: 			perftextCheckbox.IsChecked = () => Game.Settings.Debug.PerfText;