Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions UoFiddler.Controls/UserControls/MapControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions UoFiddler.Plugin.MapPreview/Facets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/***************************************************************************
*
* "THE BEER-WARE LICENSE"
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a beer in return.
*
***************************************************************************/

using Ultima;
using UoFiddler.Controls.Classes;

namespace UoFiddler.Plugin.MapPreview
{
/// <summary>
/// The six facets by index. A preview keeps the index, not the <see cref="Map"/>, because a
/// client reload can put a new instance in the static field.
/// </summary>
internal static class Facets
{
public const int Count = 6;

/// <summary>The facet's index, or -1 for a map that is not one of the six.</summary>
public static int IndexOf(Map map)
{
for (int i = 0; i < Count; i++)
{
if (ReferenceEquals(Get(i), map))
{
return i;
}
}

return -1;
}

public static Map Get(int index)
{
return index switch
{
0 => Map.Felucca,
1 => Map.Trammel,
2 => Map.Ilshenar,
3 => Map.Malas,
4 => Map.Tokuno,
5 => Map.TerMur,
_ => null
};
}

/// <summary>The name the map tab shows, which the user can change in the options.</summary>
public static string Name(int index, Map map)
{
string[] names = Options.MapNames;
return index >= 0 && names != null && index < names.Length && !string.IsNullOrEmpty(names[index])
? names[index]
: $"Map{map?.FileIndex}";
}
}
}
91 changes: 91 additions & 0 deletions UoFiddler.Plugin.MapPreview/MapPreviewPluginBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/***************************************************************************
*
* "THE BEER-WARE LICENSE"
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a beer in return.
*
***************************************************************************/

using System;
using System.Drawing;
using System.Windows.Forms;
using Ultima;
using UoFiddler.Controls.Plugin;
using UoFiddler.Controls.Plugin.Interfaces;
using UoFiddler.Plugin.MapPreview.UI;

namespace UoFiddler.Plugin.MapPreview
{
/// <summary>Adds "In-game preview here..." to the map tab's context menu.</summary>
public class MapPreviewPluginBase : PluginBase
{
public override string Author { get; } = "UOFiddler";

public override string Description { get; } = "Previews a map area as the client draws it\r\n(Adds a map context menu entry and a settings entry under Plugins)";

public override IPluginHost Host { get; set; }

public override string Name { get; } = "MapPreviewPlugin";

public override string Version { get; } = "1.0.0";

public override void Initialize()
{
// The Manage Plugins form also constructs disabled plugins, so events are
// subscribed here and not in the constructor.
PluginEvents.ModifyMapControlContextMenuEvent += OnMapContextMenu;
}

public override void Unload()
{
PluginEvents.ModifyMapControlContextMenuEvent -= OnMapContextMenu;
_window?.Close();
}

public override void ModifyPluginToolStrip(ToolStripDropDownButton toolStrip)
{
var settings = new ToolStripMenuItem { Text = "In-game preview settings..." };
settings.Click += (_, _) => MapPreviewForm.EditSettings(toolStrip.Owner?.FindForm());
toolStrip.DropDownItems.Add(settings);
}

public override void ModifyTabPages(TabControl tabControl)
{
}

/// <summary>The one preview window, reused for every spot until it is closed.</summary>
private MapPreviewForm _window;

private void OnMapContextMenu(ContextMenuStrip strip, Func<(Map Map, Point Location)> clickedAt)
{
var preview = new ToolStripMenuItem { Text = "In-game preview here" };
preview.Click += (_, _) =>
{
(Map map, Point location) = clickedAt();
if (map == null)
{
return;
}

if (_window == null || _window.IsDisposed)
{
_window = new MapPreviewForm();
_window.FormClosed += (_, _) => _window = null;
_window.Show(strip.SourceControl?.FindForm());
}
else if (_window.WindowState == FormWindowState.Minimized)
{
_window.WindowState = FormWindowState.Normal;
}

_window.ShowArea(map, location);
_window.Activate();
};

// Settings stay under Plugins only: next to this entry they were too easy to hit by mistake.
strip.Items.Add(new ToolStripSeparator());
strip.Items.Add(preview);
}
}
}
155 changes: 155 additions & 0 deletions UoFiddler.Plugin.MapPreview/PreviewSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/***************************************************************************
*
* "THE BEER-WARE LICENSE"
* As long as you retain this notice you can do whatever you want with
* this stuff. If we meet some day, and you think this stuff is worth it,
* you can buy me a beer in return.
*
***************************************************************************/

using System;
using System.Globalization;
using UoFiddler.Controls.Classes;
using UoFiddler.Plugin.MapPreview.Rendering;

namespace UoFiddler.Plugin.MapPreview
{
/// <summary>
/// How a preview is made: the area around the clicked tile and what is drawn. Kept in the
/// profile (<see cref="Options.PluginSettings"/>), so each profile has its own.
/// </summary>
public sealed class PreviewSettings
{
/// <summary>
/// The square areas offered, in tiles a side. The largest is about 11,000 pixels across at
/// 100% and around a hundred thousand sprites in a town: under a second to open.
/// </summary>
public static readonly int[] AreaSizes = { 32, 48, 64, 96, 128, 192, 256 };

/// <summary>How far one move of the area goes, in tiles: a nudge up to a jump.</summary>
public static readonly int[] MoveSteps = { 1, 8, 16, 32 };

private const string Prefix = "MapPreview.";

private static PreviewSettings _current;

/// <summary>Raised after settings were changed and stored.</summary>
public static event Action Changed;

/// <summary>The profile's settings, read on first use.</summary>
public static PreviewSettings Current => _current ??= Load();

public int AreaWidth { get; set; } = 64;

public int AreaHeight { get; set; } = 64;

public bool ShowLand { get; set; } = true;

public bool ShowStatics { get; set; } = true;

public bool Stretch { get; set; } = true;

public bool Lighting { get; set; } = true;

public bool Hues { get; set; } = true;

public bool HideRoofs { get; set; }

/// <summary>Cut everything above the ground at the clicked tile on every preview, as the client does indoors.</summary>
public bool AutoRoofCut { get; set; }

/// <summary>Max Z hides land too, to show statics under the terrain. Set on the preview's toolbar.</summary>
public bool CutLand { get; set; }

/// <summary>Tiles the area moves per arrow, one of <see cref="MoveSteps"/>.</summary>
public int MoveStep { get; set; } = 8;

public PreviewSettings Clone() => (PreviewSettings)MemberwiseClone();

public PreviewOptions ToOptions(int? maxZ) => new PreviewOptions
{
ShowLand = ShowLand,
ShowStatics = ShowStatics,
Stretch = Stretch,
Lighting = Lighting,
Hues = Hues,
HideRoofs = HideRoofs,
MaxZ = maxZ,
CutLand = CutLand
};

/// <summary>Makes these the current settings and puts them in the profile, which the application saves on exit.</summary>
public void Apply()
{
_current = this;

Write(nameof(AreaWidth), AreaWidth.ToString(CultureInfo.InvariantCulture));
Write(nameof(AreaHeight), AreaHeight.ToString(CultureInfo.InvariantCulture));
Write(nameof(ShowLand), ShowLand.ToString());
Write(nameof(ShowStatics), ShowStatics.ToString());
Write(nameof(Stretch), Stretch.ToString());
Write(nameof(Lighting), Lighting.ToString());
Write(nameof(Hues), Hues.ToString());
Write(nameof(HideRoofs), HideRoofs.ToString());
Write(nameof(AutoRoofCut), AutoRoofCut.ToString());
Write(nameof(CutLand), CutLand.ToString());
Write(nameof(MoveStep), MoveStep.ToString(CultureInfo.InvariantCulture));

Changed?.Invoke();
}

/// <summary>Reads the profile's settings; anything missing or unreadable keeps its default.</summary>
private static PreviewSettings Load()
{
var settings = new PreviewSettings();
settings.AreaWidth = NearestAreaSize(ReadInt(nameof(AreaWidth), settings.AreaWidth));
settings.AreaHeight = NearestAreaSize(ReadInt(nameof(AreaHeight), settings.AreaHeight));
settings.ShowLand = ReadBool(nameof(ShowLand), settings.ShowLand);
settings.ShowStatics = ReadBool(nameof(ShowStatics), settings.ShowStatics);
settings.Stretch = ReadBool(nameof(Stretch), settings.Stretch);
settings.Lighting = ReadBool(nameof(Lighting), settings.Lighting);
settings.Hues = ReadBool(nameof(Hues), settings.Hues);
settings.HideRoofs = ReadBool(nameof(HideRoofs), settings.HideRoofs);
settings.AutoRoofCut = ReadBool(nameof(AutoRoofCut), settings.AutoRoofCut);
settings.CutLand = ReadBool(nameof(CutLand), settings.CutLand);
int step = ReadInt(nameof(MoveStep), settings.MoveStep);
settings.MoveStep = Array.IndexOf(MoveSteps, step) >= 0 ? step : 8;
return settings;
}

/// <summary>The offered size closest to <paramref name="size"/>, for sizes saved before the list existed.</summary>
public static int NearestAreaSize(int size)
{
int best = AreaSizes[0];
foreach (int candidate in AreaSizes)
{
if (Math.Abs(candidate - size) < Math.Abs(best - size))
{
best = candidate;
}
}

return best;
}

private static void Write(string name, string value)
{
Options.PluginSettings[Prefix + name] = value;
}

private static int ReadInt(string name, int fallback)
{
return Options.PluginSettings.TryGetValue(Prefix + name, out string text) &&
int.TryParse(text, NumberStyles.Integer, CultureInfo.InvariantCulture, out int value)
? value
: fallback;
}

private static bool ReadBool(string name, bool fallback)
{
return Options.PluginSettings.TryGetValue(Prefix + name, out string text) && bool.TryParse(text, out bool value)
? value
: fallback;
}
}
}
Loading