Skip to content

Commit

Permalink
add configurable background colors (#59)
Browse files Browse the repository at this point in the history
* add configurable background colors

* improvements
  • Loading branch information
gmriggs authored Feb 7, 2022
1 parent 1b6635e commit 0920699
Show file tree
Hide file tree
Showing 14 changed files with 492 additions and 16 deletions.
62 changes: 62 additions & 0 deletions ACViewer/Config/BackgroundColors.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using System;
using System.Globalization;
using Microsoft.Xna.Framework;
using Newtonsoft.Json;

namespace ACViewer.Config
{
public class BackgroundColors
{
[JsonConverter(typeof(JsonConverter_Color))]
public Color ModelViewer { get; set; }

[JsonConverter(typeof(JsonConverter_Color))]
public Color ParticleViewer { get; set; }

[JsonConverter(typeof(JsonConverter_Color))]
public Color TextureViewer { get; set; }

[JsonConverter(typeof(JsonConverter_Color))]
public Color WorldViewer { get; set; }

public BackgroundColors()
{
// defaults
ModelViewer = Color.Black;
ParticleViewer = Color.Black;
TextureViewer = Color.Black;
WorldViewer = new Color(32, 32, 32);
}
}

public sealed class JsonConverter_Color : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(Color).Equals(objectType);
}

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var color = (Color)value;

writer.WriteValue($"#{color.R:X2}{color.G:X2}{color.B:X2}");
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var str = reader.Value.ToString();

if (str == null || str.Length != 7 || str[0] != '#')
return Color.Black;

if (!byte.TryParse(str.Substring(1, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var r) ||
!byte.TryParse(str.Substring(3, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var g) ||
!byte.TryParse(str.Substring(5, 2), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out var b))
{
return Color.Black;
}
return new Color(r, g, b);
}
}
}
1 change: 1 addition & 0 deletions ACViewer/Config/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ public class Config
public Database Database { get; set; } = new Database();
public Toggles Toggles { get; set; } = new Toggles();
public MapViewerOptions MapViewer { get; set; } = new MapViewerOptions();
public BackgroundColors BackgroundColors { get; set; } = new BackgroundColors();
}
}
133 changes: 133 additions & 0 deletions ACViewer/Extensions/ColorDialogEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace ACViewer.Extensions
{
public class ColorDialogEx : ColorDialog
{
#region private const
//Windows Message Constants
private const int WM_INITDIALOG = 0x0110;
private const int WM_CTLCOLOREDIT = 0x0133;
private const int WM_CTLCOLORSTATIC = 0x0138;

//Window Controls
private const int COLOR_RED = 706;
private const int COLOR_GREEN = 707;
private const int COLOR_BLUE = 708;

//uFlag Constants
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_SHOWWINDOW = 0x0040;
private const uint SWP_NOZORDER = 0x0004;
private const uint UFLAGS = SWP_NOSIZE | SWP_NOZORDER | SWP_SHOWWINDOW;
#endregion

#region private readonly
//Windows Handle Constants
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private static readonly IntPtr HWND_TOP = new IntPtr(0);
private static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
#endregion

#region private vars
//Module vars
private int _x;
private int _y;
private string _title = null;
#endregion

#region private static methods imports
//WinAPI definitions

/// <summary>
/// Sets the window text.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="text">The text.</param>
/// <returns></returns>
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern bool SetWindowText(IntPtr hWnd, string text);

/// <summary>
/// Sets the window pos.
/// </summary>
/// <param name="hWnd">The h WND.</param>
/// <param name="hWndInsertAfter">The h WND insert after.</param>
/// <param name="x">The x.</param>
/// <param name="y">The y.</param>
/// <param name="cx">The cx.</param>
/// <param name="cy">The cy.</param>
/// <param name="uFlags">The u flags.</param>
/// <returns></returns>
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

[DllImport("user32.dll")]
private static extern IntPtr GetDlgItem(IntPtr hDlg, int nIDDlgItem);

[DllImport("user32.dll")]
private static extern int GetDlgItemInt(IntPtr hDlg, int nIDDlgItem, IntPtr lpTranslated, bool bSigned);
#endregion

#region public constructor
/// <summary>
/// Initializes a new instance of the <see cref="ColorDialogEx"/> class.
/// </summary>
/// <param name="x">The X position</param>
/// <param name="y">The Y position</param>
/// <param name="title">The title of the windows. If set to null(by default), the title will not be changed</param>
public ColorDialogEx(int x, int y, String title = null)
{
_x = x;
_y = y;
_title = title;
}
#endregion

#region protected override methods
/// <summary>
/// Defines the common dialog box hook procedure that is overridden to add specific functionality to a common dialog box.
/// </summary>
/// <param name="hWnd">The handle to the dialog box window.</param>
/// <param name="msg">The message being received.</param>
/// <param name="wparam">Additional information about the message.</param>
/// <param name="lparam">Additional information about the message.</param>
/// <returns>
/// A zero value if the default dialog box procedure processes the message; a nonzero value if the default dialog box procedure ignores the message.
/// </returns>
protected override IntPtr HookProc(IntPtr hWnd, int msg, IntPtr wparam, IntPtr lparam)
{
//We do the base initialization
IntPtr hookProc = base.HookProc(hWnd, msg, wparam, lparam);
//When we init the dialog
if (msg == WM_INITDIALOG)
{
//We change the title
if (!string.IsNullOrEmpty(_title))
{
SetWindowText(hWnd, _title);
}
//We move the position
SetWindowPos(hWnd, HWND_TOP, _x, _y, 0, 0, UFLAGS);
}
else if (msg == WM_CTLCOLOREDIT)
{
if (ColorEditCallback != null)
{
var r = GetDlgItemInt(hWnd, COLOR_RED, IntPtr.Zero, false);
var g = GetDlgItemInt(hWnd, COLOR_GREEN, IntPtr.Zero, false);
var b = GetDlgItemInt(hWnd, COLOR_BLUE, IntPtr.Zero, false);
ColorEditCallback(r, g, b);
}
}
return hookProc;
}
#endregion

public Action<int, int, int> ColorEditCallback { get; set; }
}
}
27 changes: 27 additions & 0 deletions ACViewer/Extensions/ColorExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System.Windows.Media;

namespace ACViewer.Extensions
{
public static class ColorExtensions
{
public static SolidColorBrush ToSolidColorBrush(this System.Drawing.Color c)
{
return new SolidColorBrush(Color.FromArgb(c.A, c.R, c.G, c.B));
}

public static SolidColorBrush ToSolidColorBrush(this Microsoft.Xna.Framework.Color c)
{
return new SolidColorBrush(Color.FromArgb(c.A, c.R, c.G, c.B));
}

public static System.Drawing.Color ToColor(this SolidColorBrush brush)
{
return System.Drawing.Color.FromArgb(brush.Color.A, brush.Color.R, brush.Color.G, brush.Color.B);
}

public static Microsoft.Xna.Framework.Color ToXNAColor(this SolidColorBrush brush)
{
return new Microsoft.Xna.Framework.Color(brush.Color.R, brush.Color.G, brush.Color.B, brush.Color.A);
}
}
}
54 changes: 54 additions & 0 deletions ACViewer/Extensions/CommandHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Windows.Input;

namespace ACViewer.Extensions
{
public class CommandHandler : ICommand
{
private Action<object> _action;
private Func<object, bool> _canExecute;

/// <summary>
/// Creates instance of the command handler
/// </summary>
/// <param name="action">Action to be executed by the command</param>
/// <param name="canExecute">A bolean property to containing current permissions to execute the command</param>
public CommandHandler(Action<object> action, Func<object, bool> canExecute)
{
if (action == null) throw new ArgumentNullException(nameof(action));
_action = action;
_canExecute = canExecute ?? (x => true);
}
public CommandHandler(Action<object> action) : this(action, null)
{
}

/// <summary>
/// Wires CanExecuteChanged event
/// </summary>
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}

/// <summary>
/// Forcess checking if execute is allowed
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
public bool CanExecute(object parameter)
{
return _canExecute(parameter);
}

public void Execute(object parameter)
{
_action(parameter);
}
public void Refresh()
{
CommandManager.InvalidateRequerySuggested();
}
}
}
4 changes: 0 additions & 4 deletions ACViewer/GameView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,8 @@ protected override void Update(GameTime time)
base.Update(time);
}

private static readonly Color BackgroundColor = new Color(0, 0, 0);

protected override void Draw(GameTime time)
{
GraphicsDevice.Clear(BackgroundColor);

switch (ViewMode)
{
case ViewMode.Texture:
Expand Down
5 changes: 3 additions & 2 deletions ACViewer/MapViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public MouseState PrevMouseState
public Matrix Translate { get; set; } = Matrix.Identity;

public Vector2 ImagePos { get; set; }

public Matrix BlockTranslate { get; set; }

public Texture2D Highlight { get; set; }
Expand Down Expand Up @@ -226,7 +227,7 @@ public void Update(GameTime gameTime)
WorldViewer.Instance.LoadLandblocks(startBlock, endBlock);
}

if (mouseState.RightButton == ButtonState.Pressed)
if (mouseState.RightButton == ButtonState.Pressed && PrevMouseState.RightButton == ButtonState.Pressed)
{
var delta = PrevMouseState.Position - mouseState.Position;
Pos -= new Vector2(delta.X, delta.Y);
Expand Down Expand Up @@ -337,7 +338,7 @@ public void OnZoom(float scrollWheel)
/// <param name="gameTime">Provides a snapshot of timing values.</param>
public void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.Black);
GraphicsDevice.Clear(ConfigManager.Config.BackgroundColors.TextureViewer);

if (WorldMap == null) return;

Expand Down
3 changes: 3 additions & 0 deletions ACViewer/ModelViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

using ACE.Server.Physics.Animation;

using ACViewer.Config;
using ACViewer.Enum;
using ACViewer.Model;
using ACViewer.Render;
Expand Down Expand Up @@ -220,6 +221,8 @@ public void DrawModel()
{
if (Setup == null) return;

GraphicsDevice.Clear(ConfigManager.Config.BackgroundColors.ModelViewer);

Setup.Draw(PolyIdx, PartIdx);

if (ViewObject.PhysicsObj.ParticleManager != null)
Expand Down
3 changes: 2 additions & 1 deletion ACViewer/ParticleViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using ACE.Server.Physics;
using ACE.Server.Physics.Animation;

using ACViewer.Config;
using ACViewer.Enum;
using ACViewer.Model;
using ACViewer.Render;
Expand Down Expand Up @@ -133,7 +134,7 @@ public void Update(GameTime gameTime)

public void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(new Color(0, 0, 0));
GraphicsDevice.Clear(ConfigManager.Config.BackgroundColors.ParticleViewer);

Effect.CurrentTechnique = Effect.Techniques["ColoredNoShading"];
Effect.Parameters["xWorld"].SetValue(Matrix.Identity);
Expand Down
2 changes: 1 addition & 1 deletion ACViewer/Render/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ public void Update(GameTime gameTime)
}
else if (PrevMouseState.RightButton == ButtonState.Pressed)
{
System.Windows.Input.Mouse.OverrideCursor = Cursors.Arrow;
System.Windows.Input.Mouse.OverrideCursor = null;
}

PrevMouseState = mouseState;
Expand Down
7 changes: 3 additions & 4 deletions ACViewer/Render/Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using ACViewer.Config;
using ACViewer.Enum;
using ACViewer.View;

Expand Down Expand Up @@ -57,7 +58,7 @@ public void SetRasterizerState(bool wireframe = true)
var rs = new RasterizerState();

//rs.CullMode = CullMode.CullClockwiseFace;
rs.CullMode = Microsoft.Xna.Framework.Graphics.CullMode.None;
rs.CullMode = CullMode.None;

if (wireframe)
rs.FillMode = FillMode.WireFrame;
Expand All @@ -67,11 +68,9 @@ public void SetRasterizerState(bool wireframe = true)
GraphicsDevice.RasterizerState = rs;
}

private static readonly Color BackgroundColor = new Color(32, 32, 32);

public void Draw()
{
GraphicsDevice.Clear(BackgroundColor);
GraphicsDevice.Clear(ConfigManager.Config.BackgroundColors.WorldViewer);

SetRasterizerState(false);

Expand Down
Loading

0 comments on commit 0920699

Please sign in to comment.