Mods.Common Widgets

Moved over Widgets that don't require a bunch of things to move.
This commit is contained in:
steelphase
2014-09-26 21:34:14 -04:00
parent c57407de4a
commit 2f87a62ee8
47 changed files with 91 additions and 80 deletions

View File

@@ -1,320 +0,0 @@
#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.RA.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;
int currentFrame;
readonly World world;
static readonly string[] AllowedExtensions = { ".shp", ".r8", "tmp", ".tem", ".des", ".sno", ".int", ".jun" };
[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 ? assetSource.Name.Replace(Platform.SupportDir, "^") : "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;
}
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;
frameSlider = panel.Get<SliderWidget>("FRAME_SLIDER");
frameSlider.OnChange += x => { currentFrame = (int)Math.Round(x); };
frameSlider.GetValue = () => currentFrame;
var frameText = panel.GetOrNull<LabelWidget>("FRAME_COUNT");
if (frameText != null)
frameText.GetText = () => "{0} / {1}".F(currentFrame + 1, currentSprites.Length);
var playButton = panel.GetOrNull<ButtonWidget>("BUTTON_PLAY");
if (playButton != null)
{
playButton.OnClick = () => animateFrames = true;
playButton.IsVisible = () => !animateFrames;
}
var pauseButton = panel.GetOrNull<ButtonWidget>("BUTTON_PAUSE");
if (pauseButton != null)
{
pauseButton.OnClick = () => animateFrames = false;
pauseButton.IsVisible = () => animateFrames;
}
var stopButton = panel.GetOrNull<ButtonWidget>("BUTTON_STOP");
if (stopButton != null)
{
stopButton.OnClick = () =>
{
frameSlider.Value = 0;
currentFrame = 0;
animateFrames = false;
};
}
var nextButton = panel.GetOrNull<ButtonWidget>("BUTTON_NEXT");
if (nextButton != null)
nextButton.OnClick = SelectNextFrame;
var prevButton = panel.GetOrNull<ButtonWidget>("BUTTON_PREV");
if (prevButton != null)
prevButton.OnClick = SelectPreviousFrame;
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 = () => { Ui.CloseWindow(); onExit(); };
}
void SelectNextFrame()
{
currentFrame++;
if (currentFrame >= currentSprites.Length)
currentFrame = 0;
}
void SelectPreviousFrame()
{
currentFrame--;
if (currentFrame < 0)
currentFrame = currentSprites.Length - 1;
}
Dictionary<string, bool> assetVisByName = new Dictionary<string, bool>();
bool FilterAsset(string filename)
{
var filter = filenameInput.Text;
if (string.IsNullOrWhiteSpace(filter))
return true;
if (filename.IndexOf(filter, StringComparison.OrdinalIgnoreCase) >= 0)
return true;
return false;
}
void ApplyFilter(string filename)
{
assetVisByName.Clear();
assetList.Layout.AdjustChildren();
assetList.ScrollToTop();
// Select the first visible
var firstVisible = assetVisByName.FirstOrDefault(kvp => kvp.Value);
if (firstVisible.Key != null)
LoadAsset(firstVisible.Key);
}
void AddAsset(ScrollPanelWidget list, string filepath, ScrollItemWidget template)
{
var filename = Path.GetFileName(filepath);
var item = ScrollItemWidget.Setup(template,
() => currentFilename == filename,
() => { LoadAsset(filename); });
item.Get<LabelWidget>("TITLE").GetText = () => filepath;
item.IsVisible = () =>
{
bool visible;
if (assetVisByName.TryGetValue(filepath, out visible))
return visible;
visible = FilterAsset(filepath);
assetVisByName.Add(filepath, visible);
return visible;
};
list.AddChild(item);
}
bool LoadAsset(string filename)
{
if (string.IsNullOrEmpty(filename))
return false;
if (!GlobalFileSystem.Exists(filename))
return false;
currentFilename = filename;
currentSprites = world.Map.SequenceProvider.SpriteLoader.LoadAllSprites(filename);
currentFrame = 0;
frameSlider.MaximumValue = (float)currentSprites.Length - 1;
frameSlider.Ticks = currentSprites.Length;
return true;
}
bool ShowSourceDropdown(DropDownButtonWidget dropdown)
{
Func<IFolder, ScrollItemWidget, ScrollItemWidget> setupItem = (source, itemTemplate) =>
{
var item = ScrollItemWidget.Setup(itemTemplate,
() => assetSource == source,
() => { assetSource = source; PopulateAssetList(); });
item.Get<LabelWidget>("LABEL").GetText = () => source != null ? source.Name.Replace(Platform.SupportDir, "^") : "All Packages";
return item;
};
// TODO: Re-enable "All Packages" once list generation is done in a background thread
// var sources = new[] { (IFolder)null }.Concat(GlobalFileSystem.MountedFolders);
var sources = GlobalFileSystem.MountedFolders;
dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 280, sources, setupItem);
return true;
}
void PopulateAssetList()
{
assetList.RemoveChildren();
availableShps.Clear();
// TODO: This is too slow to run in the main thread
// var files = AssetSource != null ? AssetSource.AllFileNames() :
// GlobalFileSystem.MountedFolders.SelectMany(f => f.AllFileNames());
if (assetSource == null)
return;
var files = assetSource.AllFileNames().OrderBy(s => s);
foreach (var file in files)
{
if (AllowedExtensions.Any(ext => file.EndsWith(ext, true, CultureInfo.InvariantCulture)))
{
AddAsset(assetList, file, template);
availableShps.Add(file);
}
}
}
bool ShowPaletteDropdown(DropDownButtonWidget dropdown, World world)
{
Func<PaletteFromFile, ScrollItemWidget, ScrollItemWidget> setupItem = (palette, itemTemplate) =>
{
var name = palette.Name;
var item = ScrollItemWidget.Setup(itemTemplate,
() => currentPalette == name,
() => currentPalette = name);
item.Get<LabelWidget>("LABEL").GetText = () => name;
return item;
};
var palettes = world.WorldActor.TraitsImplementing<PaletteFromFile>();
dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 280, palettes, setupItem);
return true;
}
static void ShowColorDropDown(DropDownButtonWidget color, ColorPreviewManagerWidget preview, World world)
{
Action onExit = () =>
{
Game.Settings.Player.Color = preview.Color;
Game.Settings.Save();
};
color.RemovePanel();
Action<HSLColor> onChange = c => preview.Color = c;
var colorChooser = Game.LoadWidget(world, "COLOR_CHOOSER", null, new WidgetArgs()
{
{ "onChange", onChange },
{ "initialColor", Game.Settings.Player.Color }
});
color.AttachPanel(colorChooser, onExit);
}
}
}

View File

@@ -13,6 +13,7 @@ using System.Linq;
using OpenRA.Mods.RA.Widgets;
using OpenRA.Network;
using OpenRA.Widgets;
using OpenRA.Mods.Common.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{

View File

@@ -1,57 +0,0 @@
#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 OpenRA.Graphics;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class ColorPickerLogic
{
[ObjectCreator.UseCtor]
public ColorPickerLogic(Widget widget, HSLColor initialColor, Action<HSLColor> onChange, WorldRenderer worldRenderer)
{
var ticker = widget.GetOrNull<LogicTickerWidget>("ANIMATE_PREVIEW");
if (ticker != null)
{
var preview = widget.Get<SpriteSequenceWidget>("PREVIEW");
var anim = preview.GetAnimation();
anim.PlayRepeating(anim.CurrentSequence.Name);
ticker.OnTick = () => anim.Tick();
}
var hueSlider = widget.Get<SliderWidget>("HUE");
var mixer = widget.Get<ColorMixerWidget>("MIXER");
var randomButton = widget.GetOrNull<ButtonWidget>("RANDOM_BUTTON");
hueSlider.OnChange += _ => mixer.Set(hueSlider.Value);
mixer.OnChange += () => onChange(mixer.Color);
if (randomButton != null)
randomButton.OnClick = () =>
{
// Avoid colors with low sat or lum
var hue = (byte)Game.CosmeticRandom.Next(255);
var sat = (byte)Game.CosmeticRandom.Next(70, 255);
var lum = (byte)Game.CosmeticRandom.Next(70, 255);
mixer.Set(new HSLColor(hue, sat, lum));
hueSlider.Value = hue / 255f;
};
// Set the initial state
mixer.Set(initialColor);
hueSlider.Value = initialColor.H / 255f;
onChange(mixer.Color);
}
}
}

View File

@@ -9,6 +9,7 @@
#endregion
using System;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Network;
using OpenRA.Widgets;

View File

@@ -9,6 +9,7 @@
#endregion
using OpenRA.Graphics;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic

View File

@@ -1,41 +0,0 @@
#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 OpenRA.Network;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class DisconnectWatcherLogic
{
[ObjectCreator.UseCtor]
public DisconnectWatcherLogic(Widget widget, OrderManager orderManager)
{
var disconnected = false;
widget.Get<LogicTickerWidget>("DISCONNECT_WATCHER").OnTick = () =>
{
if (!disconnected && orderManager.Connection.ConnectionState == ConnectionState.NotConnected)
{
Game.RunAfterTick(() =>
{
Ui.OpenWindow("CONNECTIONFAILED_PANEL", new WidgetArgs()
{
{ "orderManager", orderManager },
{ "onAbort", null },
{ "onRetry", null }
});
});
disconnected = true;
}
};
}
}
}

View File

@@ -8,6 +8,7 @@
*/
#endregion
using OpenRA.Mods.Common.Widgets;
using OpenRA.Traits;
using OpenRA.Widgets;

View File

@@ -9,6 +9,7 @@
#endregion
using System.Drawing;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Mods.RA.Power;
using OpenRA.Widgets;

View File

@@ -8,9 +8,10 @@
*/
#endregion
using System.Drawing;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Mods.RA.Power;
using OpenRA.Widgets;
using System.Drawing;
namespace OpenRA.Mods.RA.Widgets.Logic
{

View File

@@ -1,49 +0,0 @@
#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.Drawing;
using System.Linq;
using OpenRA.Mods.RA.Widgets;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class IngameRadarDisplayLogic
{
[ObjectCreator.UseCtor]
public IngameRadarDisplayLogic(Widget widget, World world)
{
var radarEnabled = false;
var cachedRadarEnabled = false;
var blockColor = Color.Transparent;
var radar = widget.Get<RadarWidget>("RADAR_MINIMAP");
radar.IsEnabled = () => radarEnabled;
var ticker = widget.Get<LogicTickerWidget>("RADAR_TICKER");
ticker.OnTick = () =>
{
radarEnabled = world.ActorsWithTrait<ProvidesRadar>()
.Any(a => a.Actor.Owner == world.LocalPlayer && a.Trait.IsActive);
if (radarEnabled != cachedRadarEnabled)
Sound.PlayNotification(world.Map.Rules, null, "Sounds", radarEnabled ? "RadarUp" : "RadarDown", null);
cachedRadarEnabled = radarEnabled;
};
var block = widget.GetOrNull<ColorBlockWidget>("RADAR_FADETOBLACK");
if (block != null)
{
radar.Animating = x => blockColor = Color.FromArgb((int)(255 * x), Color.Black);
block.IsVisible = () => blockColor.A != 0;
block.GetColor = () => blockColor;
}
}
}
}

View File

@@ -9,6 +9,7 @@
#endregion
using System.Drawing;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Traits;
using OpenRA.Widgets;

View File

@@ -1,54 +0,0 @@
#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 OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{
public class LoadIngamePlayerOrObserverUILogic
{
[ObjectCreator.UseCtor]
public LoadIngamePlayerOrObserverUILogic(Widget widget, World world)
{
var ingameRoot = widget.Get("INGAME_ROOT");
var playerRoot = ingameRoot.Get("PLAYER_ROOT");
if (world.LocalPlayer == null)
Game.LoadWidget(world, "OBSERVER_WIDGETS", playerRoot, new WidgetArgs());
else
{
var playerWidgets = Game.LoadWidget(world, "PLAYER_WIDGETS", playerRoot, new WidgetArgs());
var sidebarTicker = playerWidgets.Get<LogicTickerWidget>("SIDEBAR_TICKER");
sidebarTicker.OnTick = () =>
{
// Switch to observer mode after win/loss
if (world.ObserveAfterWinOrLose && world.LocalPlayer.WinState != WinState.Undefined)
Game.RunAfterTick(() =>
{
playerRoot.RemoveChildren();
Game.LoadWidget(world, "OBSERVER_WIDGETS", playerRoot, new WidgetArgs());
});
};
}
Game.LoadWidget(world, "CHAT_PANEL", ingameRoot, new WidgetArgs());
Action ShowLeaveMapWidget = () =>
{
ingameRoot.RemoveChildren();
Game.LoadWidget(world, "LEAVE_MAP_WIDGET", Ui.Root, new WidgetArgs());
};
world.GameOver += ShowLeaveMapWidget;
}
}
}

View File

@@ -9,11 +9,8 @@
#endregion
using System;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.RA;
using OpenRA.Mods.RA.Widgets;
using OpenRA.Traits;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic

View File

@@ -14,6 +14,7 @@ using System.Drawing;
using System.Linq;
using System.Threading;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Network;
using OpenRA.Traits;
using OpenRA.Widgets;

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Network;
using OpenRA.Primitives;
using OpenRA.Traits;

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Network;
using OpenRA.Widgets;

View File

@@ -13,6 +13,7 @@ using System.Linq;
using OpenRA.Mods.RA.Orders;
using OpenRA.Widgets;
using OpenRA.Traits;
using OpenRA.Mods.Common.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic
{

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Primitives;
using OpenRA.Widgets;

View File

@@ -13,6 +13,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic