added a palette chooser and colorpicker dropdown to the browser

This commit is contained in:
Matthias Mailänder
2013-11-17 20:20:45 +01:00
parent d9ac907315
commit e981275cb1
4 changed files with 127 additions and 39 deletions

View File

@@ -44,5 +44,15 @@ namespace OpenRA.Mods.RA
if (info.Tileset == null || info.Tileset.ToLowerInvariant() == world.Map.Tileset.ToLowerInvariant()) if (info.Tileset == null || info.Tileset.ToLowerInvariant() == world.Map.Tileset.ToLowerInvariant())
wr.AddPalette(info.Name, new Palette(FileSystem.Open(info.Filename), info.ShadowIndex), info.AllowModifiers); wr.AddPalette(info.Name, new Palette(FileSystem.Open(info.Filename), info.ShadowIndex), info.AllowModifiers);
} }
public string Filename
{
get { return info.Filename; }
}
public string Name
{
get { return info.Name; }
}
} }
} }

View File

@@ -14,6 +14,7 @@ using System.Globalization;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.RA.Widgets.Logic namespace OpenRA.Mods.RA.Widgets.Logic
@@ -22,7 +23,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
{ {
Widget panel; Widget panel;
ShpImageWidget spriteImage; ShpImageWidget spriteWidget;
TextFieldWidget filenameInput; TextFieldWidget filenameInput;
SliderWidget frameSlider; SliderWidget frameSlider;
ButtonWidget playButton, pauseButton; ButtonWidget playButton, pauseButton;
@@ -32,6 +33,8 @@ namespace OpenRA.Mods.RA.Widgets.Logic
IFolder assetSource = null; IFolder assetSource = null;
List<string> availableShps = new List<string>(); List<string> availableShps = new List<string>();
PaletteFromFile currentPalette;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public AssetBrowserLogic(Widget widget, Action onExit, World world) public AssetBrowserLogic(Widget widget, Action onExit, World world)
{ {
@@ -51,46 +54,60 @@ namespace OpenRA.Mods.RA.Widgets.Logic
assetSource = FileSystem.MountedFolders.First(); assetSource = FileSystem.MountedFolders.First();
spriteImage = panel.Get<ShpImageWidget>("SPRITE"); spriteWidget = panel.Get<ShpImageWidget>("SPRITE");
currentPalette = world.WorldActor.TraitsImplementing<PaletteFromFile>().First(p => p.Name == spriteWidget.Palette);
var paletteDropDown = panel.Get<DropDownButtonWidget>("PALETTE_SELECTOR");
paletteDropDown.OnMouseDown = _ => ShowPaletteDropdown(paletteDropDown, world);
var colorPreview = panel.Get<ColorPreviewManagerWidget>("COLOR_MANAGER");
colorPreview.Color = Game.Settings.Player.Color;
var color = panel.Get<DropDownButtonWidget>("COLOR");
color.IsDisabled = () => currentPalette.Name != colorPreview.Palette;
color.OnMouseDown = _ => ShowColorDropDown(color, colorPreview, world);
var block = panel.Get<ColorBlockWidget>("COLORBLOCK");
block.GetColor = () => Game.Settings.Player.Color.RGB;
filenameInput = panel.Get<TextFieldWidget>("FILENAME_INPUT"); filenameInput = panel.Get<TextFieldWidget>("FILENAME_INPUT");
filenameInput.OnEnterKey = () => LoadAsset(filenameInput.Text); filenameInput.OnEnterKey = () => LoadAsset(filenameInput.Text);
frameSlider = panel.Get<SliderWidget>("FRAME_SLIDER"); frameSlider = panel.Get<SliderWidget>("FRAME_SLIDER");
frameSlider.MaximumValue = (float)spriteImage.FrameCount; frameSlider.MaximumValue = (float)spriteWidget.FrameCount;
frameSlider.Ticks = spriteImage.FrameCount + 1; frameSlider.Ticks = spriteWidget.FrameCount + 1;
frameSlider.IsVisible = () => spriteImage.FrameCount > 0; frameSlider.IsVisible = () => spriteWidget.FrameCount > 0;
frameSlider.OnChange += x => { spriteImage.Frame = (int)Math.Round(x); }; frameSlider.OnChange += x => { spriteWidget.Frame = (int)Math.Round(x); };
frameSlider.GetValue = () => spriteImage.Frame; frameSlider.GetValue = () => spriteWidget.Frame;
panel.Get<LabelWidget>("FRAME_COUNT").GetText = () => "{0}/{1}".F(spriteImage.Frame, spriteImage.FrameCount); panel.Get<LabelWidget>("FRAME_COUNT").GetText = () => "{0}/{1}".F(spriteWidget.Frame, spriteWidget.FrameCount);
playButton = panel.Get<ButtonWidget>("BUTTON_PLAY"); playButton = panel.Get<ButtonWidget>("BUTTON_PLAY");
playButton.OnClick = () => playButton.OnClick = () =>
{ {
spriteImage.LoopAnimation = true; spriteWidget.LoopAnimation = true;
playButton.Visible = false; playButton.Visible = false;
pauseButton.Visible = true; pauseButton.Visible = true;
}; };
pauseButton = panel.Get<ButtonWidget>("BUTTON_PAUSE"); pauseButton = panel.Get<ButtonWidget>("BUTTON_PAUSE");
pauseButton.OnClick = () => pauseButton.OnClick = () =>
{ {
spriteImage.LoopAnimation = false; spriteWidget.LoopAnimation = false;
playButton.Visible = true; playButton.Visible = true;
pauseButton.Visible = false; pauseButton.Visible = false;
}; };
panel.Get<ButtonWidget>("BUTTON_STOP").OnClick = () => panel.Get<ButtonWidget>("BUTTON_STOP").OnClick = () =>
{ {
spriteImage.LoopAnimation = false; spriteWidget.LoopAnimation = false;
frameSlider.Value = 0; frameSlider.Value = 0;
spriteImage.Frame = 0; spriteWidget.Frame = 0;
playButton.Visible = true; playButton.Visible = true;
pauseButton.Visible = false; pauseButton.Visible = false;
}; };
panel.Get<ButtonWidget>("BUTTON_NEXT").OnClick = () => { spriteImage.RenderNextFrame(); }; panel.Get<ButtonWidget>("BUTTON_NEXT").OnClick = () => { spriteWidget.RenderNextFrame(); };
panel.Get<ButtonWidget>("BUTTON_PREV").OnClick = () => { spriteImage.RenderPreviousFrame(); }; panel.Get<ButtonWidget>("BUTTON_PREV").OnClick = () => { spriteWidget.RenderPreviousFrame(); };
panel.Get<ButtonWidget>("LOAD_BUTTON").OnClick = () => panel.Get<ButtonWidget>("LOAD_BUTTON").OnClick = () =>
{ {
@@ -101,21 +118,23 @@ namespace OpenRA.Mods.RA.Widgets.Logic
template = panel.Get<ScrollItemWidget>("ASSET_TEMPLATE"); template = panel.Get<ScrollItemWidget>("ASSET_TEMPLATE");
PopulateAssetList(); PopulateAssetList();
// TODO: Horrible hack
var modID = Game.modData.Manifest.Mod.Id; var modID = Game.modData.Manifest.Mod.Id;
var palette = (modID == "d2k") ? "d2k.pal" : "egopal.pal";
/* TODO:
* This should not invoke the OpenRA.Utility.exe, but use it's functions directly.
* Does not work with SHP(TS) yet?!
*/
panel.Get<ButtonWidget>("EXPORT_BUTTON").OnClick = () => panel.Get<ButtonWidget>("EXPORT_BUTTON").OnClick = () =>
{ {
var ExtractGameFiles = new string[][] var ExtractGameFiles = new string[][]
{ {
new string[] { "--extract", modID, palette, "--userdir" }, new string[] { "--extract", modID, currentPalette.Filename, "--userdir" },
new string[] { "--extract", modID, "{0}.shp".F(spriteImage.Image), "--userdir" }, new string[] { "--extract", modID, "{0}.shp".F(spriteWidget.Image), "--userdir" },
}; };
var ExportToPng = new string[][] var ExportToPng = new string[][]
{ {
new string[] { "--png", Platform.SupportDir + "{0}.shp".F(spriteImage.Image), Platform.SupportDir + palette }, new string[] { "--png", Platform.SupportDir + "{0}.shp".F(spriteWidget.Image), Platform.SupportDir + currentPalette.Filename },
}; };
var ImportFromPng = new string[][] { }; var ImportFromPng = new string[][] { };
@@ -135,12 +154,12 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var ExtractGameFilesList = new List<string[]>(); var ExtractGameFilesList = new List<string[]>();
var ExportToPngList = new List<string[]>(); var ExportToPngList = new List<string[]>();
ExtractGameFilesList.Add(new string[] { "--extract", modID, palette, "--userdir" }); ExtractGameFilesList.Add(new string[] { "--extract", modID, currentPalette.Filename, "--userdir" });
foreach (var shp in availableShps) foreach (var shp in availableShps)
{ {
ExtractGameFilesList.Add(new string[] { "--extract", modID, shp, "--userdir" }); ExtractGameFilesList.Add(new string[] { "--extract", modID, shp, "--userdir" });
ExportToPngList.Add(new string[] { "--png", Platform.SupportDir + shp, Platform.SupportDir + palette }); ExportToPngList.Add(new string[] { "--png", Platform.SupportDir + shp, Platform.SupportDir + currentPalette.Filename });
Console.WriteLine(Platform.SupportDir + shp); Console.WriteLine(Platform.SupportDir + shp);
} }
@@ -189,7 +208,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var filename = Path.GetFileName(filepath); var filename = Path.GetFileName(filepath);
var sprite = r8 ? filename : Path.GetFileNameWithoutExtension(filepath); var sprite = r8 ? filename : Path.GetFileNameWithoutExtension(filepath);
var item = ScrollItemWidget.Setup(template, var item = ScrollItemWidget.Setup(template,
() => spriteImage.Image == sprite, () => spriteWidget.Image == sprite,
() => {filenameInput.Text = filename; LoadAsset(filename); }); () => {filenameInput.Text = filename; LoadAsset(filename); });
item.Get<LabelWidget>("TITLE").GetText = () => filepath; item.Get<LabelWidget>("TITLE").GetText = () => filepath;
@@ -207,10 +226,10 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var r8 = filename.EndsWith(".r8", true, CultureInfo.InvariantCulture); var r8 = filename.EndsWith(".r8", true, CultureInfo.InvariantCulture);
var sprite = r8 ? filename : Path.GetFileNameWithoutExtension(filename); var sprite = r8 ? filename : Path.GetFileNameWithoutExtension(filename);
spriteImage.Frame = 0; spriteWidget.Frame = 0;
spriteImage.Image = sprite; spriteWidget.Image = sprite;
frameSlider.MaximumValue = (float)spriteImage.FrameCount; frameSlider.MaximumValue = (float)spriteWidget.FrameCount;
frameSlider.Ticks = spriteImage.FrameCount + 1; frameSlider.Ticks = spriteWidget.FrameCount + 1;
return true; return true;
} }
@@ -255,5 +274,42 @@ namespace OpenRA.Mods.RA.Widgets.Logic
} }
} }
} }
bool ShowPaletteDropdown(DropDownButtonWidget dropdown, World world)
{
Func<PaletteFromFile, ScrollItemWidget, ScrollItemWidget> setupItem = (palette, itemTemplate) =>
{
var item = ScrollItemWidget.Setup(itemTemplate,
() => currentPalette.Name == palette.Name,
() => { currentPalette = palette; spriteWidget.Palette = currentPalette.Name; });
item.Get<LabelWidget>("LABEL").GetText = () => palette.Name;
return item;
};
var palettes = world.WorldActor.TraitsImplementing<PaletteFromFile>();
dropdown.ShowDropDown("LABEL_DROPDOWN_TEMPLATE", 280, palettes, setupItem);
return true;
}
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

@@ -5,9 +5,10 @@ Background@ASSETBROWSER_BG:
Width:700 Width:700
Height:410 Height:410
Children: Children:
ColorPreviewManager@COLOR_MANAGER:
Label@ASSETBROWSER_TITLE: Label@ASSETBROWSER_TITLE:
X:0 X:0
Y:20 Y:10
Width:PARENT_RIGHT Width:PARENT_RIGHT
Height:25 Height:25
Text:Game Asset Viewer & Converter Text:Game Asset Viewer & Converter
@@ -51,13 +52,24 @@ Background@ASSETBROWSER_BG:
Text:Load Text:Load
Font:Bold Font:Bold
Key:return Key:return
Label@PREVIEW_TITLE: DropDownButton@PALETTE_SELECTOR:
X:320 X:230
Y:45 Y:45
Width:PARENT_RIGHT Width:150
Height:25 Height:25
Text:Preview
Font:Bold Font:Bold
Text:Palette
DropDownButton@COLOR:
X:380
Y:45
Width:80
Height:25
Children:
ColorBlock@COLORBLOCK:
X:5
Y:6
Width:PARENT_RIGHT-35
Height:PARENT_BOTTOM-12
Background@SPRITE_BG: Background@SPRITE_BG:
X:220 X:220
Y:80 Y:80
@@ -71,7 +83,6 @@ Background@ASSETBROWSER_BG:
Width:246 Width:246
Height:246 Height:246
Image:mouse.r8 Image:mouse.r8
Palette:colorpicker
Label@ACTIONS_TITLE: Label@ACTIONS_TITLE:
X:PARENT_RIGHT - 150 X:PARENT_RIGHT - 150
Y:45 Y:45

View File

@@ -5,9 +5,10 @@ Background@ASSETBROWSER_BG:
Width:700 Width:700
Height:410 Height:410
Children: Children:
ColorPreviewManager@COLOR_MANAGER:
Label@ASSETBROWSER_TITLE: Label@ASSETBROWSER_TITLE:
X:0 X:0
Y:20 Y:10
Width:PARENT_RIGHT Width:PARENT_RIGHT
Height:25 Height:25
Text:Game Asset Viewer & Converter Text:Game Asset Viewer & Converter
@@ -42,7 +43,7 @@ Background@ASSETBROWSER_BG:
Y:280 Y:280
Width:140 Width:140
Height:25 Height:25
Text: mouse.shp Text:mouse.shp
Button@LOAD_BUTTON: Button@LOAD_BUTTON:
X:40 X:40
Y:310 Y:310
@@ -51,13 +52,24 @@ Background@ASSETBROWSER_BG:
Text:Load Text:Load
Font:Bold Font:Bold
Key:return Key:return
Label@PREVIEW_TITLE: DropDownButton@PALETTE_SELECTOR:
X:320 X:230
Y:45 Y:45
Width:PARENT_RIGHT Width:150
Height:25 Height:25
Text:Preview
Font:Bold Font:Bold
Text:Palette
DropDownButton@COLOR:
X:380
Y:45
Width:80
Height:25
Children:
ColorBlock@COLORBLOCK:
X:5
Y:6
Width:PARENT_RIGHT-35
Height:PARENT_BOTTOM-12
Background@SPRITE_BG: Background@SPRITE_BG:
X:220 X:220
Y:80 Y:80
@@ -71,7 +83,6 @@ Background@ASSETBROWSER_BG:
Width:246 Width:246
Height:246 Height:246
Image:mouse Image:mouse
Palette:colorpicker
Label@ACTIONS_TITLE: Label@ACTIONS_TITLE:
X:PARENT_RIGHT - 150 X:PARENT_RIGHT - 150
Y:45 Y:45