TitleColorHelper/MineworldsTextColorAlternator/Form1.cs

157 lines
5.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace TitleColorHelper
{
public partial class Form1 : Form
{
public class SubstringInfo
{
public string String { get; private set; }
public Color Color { get; private set; }
public SubstringInfo(string str, Color clr)
{
String = str;
Color = clr;
}
}
private static Dictionary<char, Color> COLOR_MAP = new Dictionary<char, Color>()
{
{ '0', Color.FromArgb( 0, 0, 0) },
{ '1', Color.FromArgb( 0, 0, 170) },
{ '2', Color.FromArgb( 0, 170, 0) },
{ '3', Color.FromArgb( 0, 170, 170) },
{ '4', Color.FromArgb(170, 0, 0) },
{ '5', Color.FromArgb(170, 0, 170) },
{ '6', Color.FromArgb(254, 170, 0) },
{ '7', Color.FromArgb(170, 170, 170) },
{ '8', Color.FromArgb( 85, 85, 85) },
{ '9', Color.FromArgb( 85, 85, 254) },
{ 'a', Color.FromArgb( 85, 254, 85) },
{ 'b', Color.FromArgb( 85, 254, 254) },
{ 'c', Color.FromArgb(254, 85, 85) },
{ 'd', Color.FromArgb(254, 85, 254) },
{ 'e', Color.FromArgb(254, 254, 85) },
{ 'f', Color.FromArgb(254, 254, 254) },
};
public static SubstringInfo[] CalculateSubstringInfos(string text)
{
if (text == null || text.Equals(string.Empty))
{
return new SubstringInfo[0];
}
char colorChar = '&';
char? color = null;
string tmpText = "";
List<SubstringInfo> list = new List<SubstringInfo>();
for (int i = 0; i < text.Length - 1; i++)
{
char charAt = text[i];
if (charAt != colorChar)
{
tmpText += charAt;
continue;
}
char charAtNext = text[i + 1];
if (!COLOR_MAP.ContainsKey(charAtNext))
{
continue;
}
if (color != null)
{
list.Add(new SubstringInfo(tmpText, COLOR_MAP[color.Value]));
tmpText = "";
}
color = charAtNext;
i++;
}
tmpText += text[text.Length - 1];
list.Add(new SubstringInfo(tmpText, COLOR_MAP[color.HasValue ? color.Value : 'f']));
return list.ToArray();
}
public Form1()
{
InitializeComponent();
}
private void ButtonCalculate_Click(object sender, EventArgs e)
{
var text = textBoxText.Text;
var textChars = text.ToCharArray();
var newText = "";
int skipped = 0;
for (int i = 0; i < textChars.Length; i++)
{
if (checkBoxSkipSpace.Checked && textChars[i] == ' ') skipped++;
else if (((i - skipped) / (int)numericUpDown1.Value) % 2 == 0) newText += textBoxColorOne.Text;
else newText += textBoxColorTwo.Text;
newText += textChars[i];
}
listBox1.Items.Add(newText);
}
private void ListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex == -1)
{
return;
}
string text = listBox1.SelectedItem as string;
if (text == null || text.Equals(string.Empty))
{
return;
}
Clipboard.SetText(text);
}
private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
{
if (e.Index == -1)
{
return;
}
e.DrawBackground();
using (Graphics g = e.Graphics)
{
g.FillRectangle(new SolidBrush(e.BackColor), e.Bounds);
string text = listBox1.Items[e.Index] as string;
float x = e.Bounds.X;
SubstringInfo[] infos = CalculateSubstringInfos(text);
float removeX = g.MeasureString("..", e.Font).Width;
foreach (var info in infos)
{
g.DrawString(info.String, e.Font, new SolidBrush(info.Color), new PointF(x, e.Bounds.Y));
x += g.MeasureString($".{info.String}.", e.Font).Width - removeX;
}
e.DrawFocusRectangle();
}
}
private void Form1_Resize(object sender, EventArgs e)
{
listBox1.Size = new Size(Size.Width - 320, listBox1.Size.Height);
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
private void AboutToolStripMenuItem_Click(object sender, EventArgs e)
{
new AboutBox1().Show();
}
}
}