Mod Chooser move.

Moved code for mod chooser to mod.common.
This commit is contained in:
steelphase
2014-09-20 13:43:31 -04:00
parent b30fcc2110
commit 7199b6fd96
6 changed files with 6 additions and 7 deletions

View File

@@ -0,0 +1,47 @@
#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.Collections.Generic;
using System.Drawing;
using OpenRA.Graphics;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common
{
public class ModChooserLoadScreen : ILoadScreen
{
Sprite sprite;
Rectangle bounds;
public void Init(Manifest m, Dictionary<string, string> info)
{
var sheet = new Sheet(info["Image"]);
var res = Game.Renderer.Resolution;
bounds = new Rectangle(0, 0, res.Width, res.Height);
sprite = new Sprite(sheet, new Rectangle(0,0,1024,480), TextureChannel.Alpha);
}
public void Display()
{
var r = Game.Renderer;
if (r == null)
return;
r.BeginFrame(int2.Zero, 1f);
WidgetUtils.FillRectWithSprite(bounds, sprite);
r.EndFrame(new NullInputHandler());
}
public void StartGame()
{
Ui.LoadWidget("MODCHOOSER", Ui.Root, new WidgetArgs());
}
}
}

View File

@@ -45,6 +45,7 @@
<Compile Include="Graphics\BeamRenderable.cs" />
<Compile Include="Graphics\ContrailRenderable.cs" />
<Compile Include="Graphics\RangeCircleRenderable.cs" />
<Compile Include="ModChooserLoadScreen.cs" />
<Compile Include="ServerTraits\ColorValidator.cs" />
<Compile Include="ServerTraits\MasterServerPinger.cs" />
<Compile Include="ServerTraits\PlayerPinger.cs" />
@@ -68,6 +69,8 @@
<Compile Include="Graphics\TextRenderable.cs" />
<Compile Include="Graphics\VoxelActorPreview.cs" />
<Compile Include="Graphics\VoxelRenderable.cs" />
<Compile Include="Widgets\Logic\ButtonTooltipLogic.cs" />
<Compile Include="Widgets\Logic\ModBrowserLogic.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@@ -0,0 +1,41 @@
#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.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class ButtonTooltipLogic
{
[ObjectCreator.UseCtor]
public ButtonTooltipLogic(Widget widget, ButtonWidget button)
{
var label = widget.Get<LabelWidget>("LABEL");
var font = Game.Renderer.Fonts[label.Font];
var labelWidth = font.Measure(button.TooltipText).X;
label.GetText = () => button.TooltipText;
label.Bounds.Width = labelWidth;
widget.Bounds.Width = 2 * label.Bounds.X + labelWidth;
if (button.Key.IsValid())
{
var hotkey = widget.Get<LabelWidget>("HOTKEY");
hotkey.Visible = true;
var hotkeyLabel = "({0})".F(button.Key.DisplayString());
hotkey.GetText = () => hotkeyLabel;
hotkey.Bounds.X = labelWidth + 2 * label.Bounds.X;
widget.Bounds.Width = hotkey.Bounds.X + label.Bounds.X + font.Measure(hotkeyLabel).X;
}
}
}
}

View File

@@ -0,0 +1,168 @@
#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.Drawing;
using System.IO;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets.Logic
{
public class ModBrowserLogic
{
Widget modList;
ButtonWidget modTemplate;
ModMetadata[] allMods;
ModMetadata selectedMod;
string selectedAuthor;
string selectedDescription;
int modOffset = 0;
Dictionary<string, Sprite> previews;
Dictionary<string, Sprite> logos;
[ObjectCreator.UseCtor]
public ModBrowserLogic(Widget widget)
{
var panel = widget;
var loadButton = panel.Get<ButtonWidget>("LOAD_BUTTON");
loadButton.OnClick = () => LoadMod(selectedMod);
loadButton.IsDisabled = () => selectedMod.Id == Game.modData.Manifest.Mod.Id;
panel.Get<ButtonWidget>("QUIT_BUTTON").OnClick = Game.Exit;
modList = panel.Get("MOD_LIST");
modTemplate = modList.Get<ButtonWidget>("MOD_TEMPLATE");
panel.Get<LabelWidget>("MOD_DESC").GetText = () => selectedDescription;
panel.Get<LabelWidget>("MOD_TITLE").GetText = () => selectedMod.Title;
panel.Get<LabelWidget>("MOD_AUTHOR").GetText = () => selectedAuthor;
panel.Get<LabelWidget>("MOD_VERSION").GetText = () => selectedMod.Version;
var prevMod = panel.Get<ButtonWidget>("PREV_MOD");
prevMod.OnClick = () => { modOffset -= 1; RebuildModList(); };
prevMod.IsVisible = () => modOffset > 0;
var nextMod = panel.Get<ButtonWidget>("NEXT_MOD");
nextMod.OnClick = () => { modOffset += 1; RebuildModList(); };
nextMod.IsVisible = () => modOffset + 5 < allMods.Length;
panel.Get<RGBASpriteWidget>("MOD_PREVIEW").GetSprite = () =>
{
Sprite ret = null;
previews.TryGetValue(selectedMod.Id, out ret);
return ret;
};
var sheetBuilder = new SheetBuilder(SheetType.BGRA);
previews = new Dictionary<string, Sprite>();
logos = new Dictionary<string, Sprite>();
allMods = ModMetadata.AllMods.Values.Where(m => m.Id != "modchooser")
.OrderBy(m => m.Title)
.ToArray();
// Load preview images, and eat any errors
foreach (var mod in allMods)
{
try
{
var preview = new Bitmap(new[] { "mods", mod.Id, "preview.png" }.Aggregate(Path.Combine));
if (preview.Width != 296 || preview.Height != 196)
continue;
previews.Add(mod.Id, sheetBuilder.Add(preview));
}
catch (Exception) { }
try
{
var logo = new Bitmap(new[] { "mods", mod.Id, "logo.png" }.Aggregate(Path.Combine));
if (logo.Width != 96 || logo.Height != 96)
continue;
logos.Add(mod.Id, sheetBuilder.Add(logo));
}
catch (Exception) { }
}
ModMetadata initialMod = null;
ModMetadata.AllMods.TryGetValue(Game.Settings.Game.PreviousMod, out initialMod);
SelectMod(initialMod ?? ModMetadata.AllMods["ra"]);
RebuildModList();
}
void RebuildModList()
{
modList.RemoveChildren();
var width = modTemplate.Bounds.Width;
var height = modTemplate.Bounds.Height;
var innerMargin = modTemplate.Bounds.Left;
var outerMargin = (modList.Bounds.Width - Math.Min(5, allMods.Length) * width - 4 * innerMargin) / 2;
var stride = width + innerMargin;
for (var i = 0; i < 5; i++)
{
var j = i + modOffset;
if (j >= allMods.Length)
break;
var mod = allMods[j];
var item = modTemplate.Clone() as ButtonWidget;
item.Bounds = new Rectangle(outerMargin + i * stride, 0, width, height);
item.IsHighlighted = () => selectedMod == mod;
item.OnClick = () => SelectMod(mod);
item.OnDoubleClick = () => LoadMod(mod);
item.OnKeyPress = e =>
{
if (e.MultiTapCount == 2)
LoadMod(mod);
else
SelectMod(mod);
};
item.TooltipText = mod.Title;
if (j < 9)
item.Key = new Hotkey((Keycode)((int)Keycode.NUMBER_1 + j), Modifiers.None);
Sprite logo = null;
logos.TryGetValue(mod.Id, out logo);
item.Get<RGBASpriteWidget>("MOD_LOGO").GetSprite = () => logo;
item.Get("MOD_NO_LOGO").IsVisible = () => logo == null;
modList.AddChild(item);
}
}
void SelectMod(ModMetadata mod)
{
selectedMod = mod;
selectedAuthor = "By " + (mod.Author ?? "unknown author");
selectedDescription = (mod.Description ?? "").Replace("\\n", "\n");
var selectedIndex = Array.IndexOf(allMods, mod);
if (selectedIndex - modOffset > 4)
modOffset = selectedIndex - 4;
}
static void LoadMod(ModMetadata mod)
{
Game.RunAfterTick(() =>
{
Ui.CloseWindow();
Game.InitializeMod(mod.Id, null);
});
}
}
}