context: #region Copyright & License Information
  /*
   * Copyright 2007-2014 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.Globalization;
  using System.IO;
  using System.Linq;
  using OpenRA.FileSystem;
  using OpenRA.Graphics;
  using OpenRA.Widgets;
  
  namespace OpenRA.Mods.Common.Widgets.Logic
  {
  	public class AssetBrowserLogic
  	{
  		Widget panel;
  
  		TextFieldWidget filenameInput;
  		SliderWidget frameSlider;
  		ScrollPanelWidget assetList;
  		ScrollItemWidget template;
  
  		IFolder assetSource = null;
  		List<string> availableShps = new List<string>();
  		bool animateFrames = false;
  
  		string currentPalette;
  		string currentFilename;
  		Sprite[] currentSprites;
  		VqaPlayerWidget player = null;
  		bool isVideoLoaded = false;
  		int currentFrame;
  
  		readonly World world;
  
  		static readonly string[] AllowedExtensions = { ".shp", ".r8", "tmp", ".tem", ".des", ".sno", ".int", ".jun", ".vqa" };
  
  		[ObjectCreator.UseCtor]
  		public AssetBrowserLogic(Widget widget, Action onExit, World world)
  		{
  			this.world = world;
  
  			panel = widget;
  			assetSource = GlobalFileSystem.MountedFolders.First();
  
  			var ticker = panel.GetOrNull<LogicTickerWidget>("ANIMATION_TICKER");
  			if (ticker != null)
  			{
  				ticker.OnTick = () =>
  				{
  					if (animateFrames)
  						SelectNextFrame();
  				};
  			}
  
  			var sourceDropdown = panel.GetOrNull<DropDownButtonWidget>("SOURCE_SELECTOR");
  			if (sourceDropdown != null)
  			{
  				sourceDropdown.OnMouseDown = _ => ShowSourceDropdown(sourceDropdown);
  				sourceDropdown.GetText = () =>
  				{
  					var name = assetSource != null ? Platform.UnresolvePath(assetSource.Name) : "All Packages";
  					if (name.Length > 15)
  						name = "..." + name.Substring(name.Length - 15);
  
  					return name;
  				};
  			}
  
  			var spriteWidget = panel.GetOrNull<SpriteWidget>("SPRITE");
  			if (spriteWidget != null)
  			{
  				spriteWidget.GetSprite = () => currentSprites != null ? currentSprites[currentFrame] : null;
  				currentPalette = spriteWidget.Palette;
  				spriteWidget.GetPalette = () => currentPalette;
  				spriteWidget.IsVisible = () => !isVideoLoaded;
  			}
  
  			var playerWidget = panel.GetOrNull<VqaPlayerWidget>("PLAYER");
  			if (playerWidget != null)			
  				playerWidget.IsVisible = () => isVideoLoaded;
  
  			var paletteDropDown = panel.GetOrNull<DropDownButtonWidget>("PALETTE_SELECTOR");
  			if (paletteDropDown != null)
  			{
  				paletteDropDown.OnMouseDown = _ => ShowPaletteDropdown(paletteDropDown, world);
  				paletteDropDown.GetText = () => currentPalette;
  			}
  
  			var colorPreview = panel.GetOrNull<ColorPreviewManagerWidget>("COLOR_MANAGER");
  			if (colorPreview != null)
  				colorPreview.Color = Game.Settings.Player.Color;
  
  			var colorDropdown = panel.GetOrNull<DropDownButtonWidget>("COLOR");
  			if (colorDropdown != null)
  			{
  				colorDropdown.IsDisabled = () => currentPalette != colorPreview.PaletteName;
  				colorDropdown.OnMouseDown = _ => ShowColorDropDown(colorDropdown, colorPreview, world);
  				panel.Get<ColorBlockWidget>("COLORBLOCK").GetColor = () => Game.Settings.Player.Color.RGB;
  			}
  
  			filenameInput = panel.Get<TextFieldWidget>("FILENAME_INPUT");
  			filenameInput.OnTextEdited = () => ApplyFilter(filenameInput.Text);
  
  			var frameContainer = panel.GetOrNull("FRAME_SELECTOR");
  			if (frameContainer != null)
  				frameContainer.IsVisible = () => (currentSprites != null && currentSprites.Length > 1) || (player != null && player.Video != null && player.Video.Frames > 1);
  
  			frameSlider = panel.Get<SliderWidget>("FRAME_SLIDER");
  			if (frameSlider != null)
  			{
  				frameSlider.OnChange += x => 
  				{
  					if (!isVideoLoaded)					
  						currentFrame = (int)Math.Round(x);					
  				};
  
  				frameSlider.GetValue = () => isVideoLoaded ? player.Video.CurrentFrame : currentFrame;
  				frameSlider.IsDisabled = () => isVideoLoaded;				
  			}
  
  			var frameText = panel.GetOrNull<LabelWidget>("FRAME_COUNT");
  			if (frameText != null)
  			{
  				frameText.GetText = () => 
  					isVideoLoaded ? 
  					"{0} / {1}".F(player.Video.CurrentFrame + 1, player.Video.Frames) : 
  					"{0} / {1}".F(currentFrame + 1, currentSprites.Length);
  			}
  
  			var playButton = panel.GetOrNull<ButtonWidget>("BUTTON_PLAY");
  			if (playButton != null)
  			{
  				playButton.OnClick = () =>
  				{
  					if (isVideoLoaded)
  						player.Play();
  					else
  						animateFrames = true;
  				};
  
  				playButton.IsVisible = () => isVideoLoaded ? player.Paused : !animateFrames;
  			}
  
  			var pauseButton = panel.GetOrNull<ButtonWidget>("BUTTON_PAUSE");
  			if (pauseButton != null)
  			{
  				pauseButton.OnClick = () =>
  				{
  					if (isVideoLoaded)
  						player.Pause();
  					else
  						animateFrames = false;
  				};
  
  				pauseButton.IsVisible = () => isVideoLoaded ? !player.Paused : animateFrames;
  			}
  
  			var stopButton = panel.GetOrNull<ButtonWidget>("BUTTON_STOP");
  			if (stopButton != null)
  			{
  				stopButton.OnClick = () =>
  				{
  					if (isVideoLoaded)
  						player.Stop();
  					else
  					{
  						frameSlider.Value = 0;
  						currentFrame = 0;
  						animateFrames = false;
  					}
  				};
  			}
  
  			var nextButton = panel.GetOrNull<ButtonWidget>("BUTTON_NEXT");
  			if (nextButton != null)
  			{
  				nextButton.OnClick = () =>
  				{
  					if (!isVideoLoaded)						
  						nextButton.OnClick = SelectNextFrame;
  				};
  
  				nextButton.IsVisible = () => !isVideoLoaded;
  			}
  
  			var prevButton = panel.GetOrNull<ButtonWidget>("BUTTON_PREV");
  			if (prevButton != null)
  			{
  				prevButton.OnClick = () =>
  				{
  					if (!isVideoLoaded)
  						SelectPreviousFrame();
  				};
  
  				prevButton.IsVisible = () => !isVideoLoaded;
  			}
  
  			assetList = panel.Get<ScrollPanelWidget>("ASSET_LIST");
  			template = panel.Get<ScrollItemWidget>("ASSET_TEMPLATE");
  			PopulateAssetList();
  
  			var closeButton = panel.GetOrNull<ButtonWidget>("CLOSE_BUTTON");
  			if (closeButton != null)
  				closeButton.OnClick = () => 
  				{
  					if (isVideoLoaded)
  						player.Stop();
  					Ui.CloseWindow();
  					onExit(); 
  				};
  		}
  
  		void SelectNextFrame()
  		{
  			currentFrame++;
  			if (currentFrame >= currentSprites.Length)
  				currentFrame = 0;
  		}
  
  		void SelectPreviousFrame()
  		{
  			currentFrame--;
  
gt: 			if (currentFrame < 0)