customizemii-wiidb/CustomizeMii/CustomizeMii_Preview.cs

410 lines
16 KiB
C#
Raw Normal View History

2009-11-08 23:53:22 +01:00
/* This file is part of CustomizeMii
* Copyright (C) 2009 Leathl
*
2009-11-24 01:09:42 +01:00
* CustomizeMii is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CustomizeMii is distributed in the hope that it will be
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2009-11-08 23:53:22 +01:00
* GNU General Public License for more details.
2009-11-24 01:09:42 +01:00
*
2009-11-08 23:53:22 +01:00
* You should have received a copy of the GNU General Public License
2009-11-24 01:09:42 +01:00
* along with this program. If not, see <http://www.gnu.org/licenses/>.
2009-11-08 23:53:22 +01:00
*/
2009-11-08 23:53:22 +01:00
using System;
2010-03-28 20:29:30 +02:00
using System.Collections.Generic;
2009-11-08 23:53:22 +01:00
using System.Drawing;
using System.Windows.Forms;
2010-03-28 20:29:30 +02:00
using libWiiSharp;
2009-11-08 23:53:22 +01:00
namespace CustomizeMii
{
public partial class CustomizeMii_Preview : Form
{
2010-03-28 20:29:30 +02:00
private TplImage[][] images = new TplImage[2][];
private U8 bannerBin;
private U8 iconBin;
private string startTPL;
private bool startIcon = false;
public U8 BannerBin { get { return bannerBin; } set { bannerBin = value; } }
public U8 IconBin { get { return iconBin; } set { iconBin = value; } }
public string StartTPL { get { return startTPL; } set { startTPL = value; } }
public bool StartIcon { get { return startIcon; } set { startIcon = value; } }
2009-11-08 23:53:22 +01:00
public CustomizeMii_Preview()
{
InitializeComponent();
this.Icon = global::CustomizeMii.Properties.Resources.CustomizeMii;
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void Preview_FormClosing(object sender, FormClosingEventArgs e)
{
2010-02-06 00:09:36 +01:00
images = null;
cbBanner.Items.Clear();
cbIcon.Items.Clear();
2009-11-08 23:53:22 +01:00
}
private void Preview_Load(object sender, EventArgs e)
2009-11-08 23:53:22 +01:00
{
this.CenterToParent();
2010-03-28 20:29:30 +02:00
cbBanner.Items.Clear();
cbIcon.Items.Clear();
2010-03-28 20:29:30 +02:00
int startIndex = -1;
2010-03-28 20:29:30 +02:00
List<TplImage> bannerImages = new List<TplImage>();
List<TplImage> iconImages = new List<TplImage>();
2010-03-28 20:29:30 +02:00
for (int i = 0; i < bannerBin.NumOfNodes; i++)
{
2010-03-28 20:29:30 +02:00
if (bannerBin.StringTable[i].ToLower().EndsWith(".tpl"))
{
TplImage tmpImage = new TplImage();
TPL tmpTpl = TPL.Load(bannerBin.Data[i]);
if (i == 10) { }
tmpImage.fileName = bannerBin.StringTable[i];
tmpImage.tplFormat = tmpTpl.GetTextureFormat(0).ToString();
tmpImage.tplImage = tmpTpl.ExtractTexture();
tmpImage.checkerBoard = createCheckerBoard(tmpImage.tplImage.Width, tmpImage.tplImage.Height);
bannerImages.Add(tmpImage);
}
}
2010-03-28 20:29:30 +02:00
for (int i = 0; i < iconBin.NumOfNodes; i++)
{
2010-03-28 20:29:30 +02:00
if (iconBin.StringTable[i].ToLower().EndsWith(".tpl"))
{
TplImage tmpImage = new TplImage();
TPL tmpTpl = TPL.Load(iconBin.Data[i]);
tmpImage.fileName = iconBin.StringTable[i];
tmpImage.tplFormat = tmpTpl.GetTextureFormat(0).ToString();
tmpImage.tplImage = tmpTpl.ExtractTexture();
tmpImage.checkerBoard = createCheckerBoard(tmpImage.tplImage.Width, tmpImage.tplImage.Height);
iconImages.Add(tmpImage);
}
}
2010-03-28 20:29:30 +02:00
images[0] = bannerImages.ToArray();
images[1] = iconImages.ToArray();
for (int i = 0; i < images[0].Length; i++)
{
2010-03-28 20:29:30 +02:00
cbBanner.Items.Add(images[0][i].fileName);
if (!startIcon)
if (images[0][i].fileName.ToLower() == startTPL.ToLower())
startIndex = i;
}
2010-03-28 20:29:30 +02:00
for (int i = 0; i < images[1].Length; i++)
{
2010-03-28 20:29:30 +02:00
cbIcon.Items.Add(images[1][i].fileName);
if (startIcon)
if (images[1][i].fileName.ToLower() == startTPL.ToLower())
startIndex = i;
}
try
{
if (startIndex != -1)
if (!startIcon)
cbBanner.SelectedIndex = startIndex;
else
cbIcon.SelectedIndex = startIndex;
}
catch { }
if (cbBanner.SelectedIndex != -1) cbBanner.Select();
else if (cbIcon.SelectedIndex != -1) cbIcon.Select();
}
private void cbBanner_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbBanner.SelectedIndex != -1)
{
2010-03-28 20:29:30 +02:00
pbPic.Image = images[0][cbBanner.SelectedIndex].tplImage;
lbFormat.Text = images[0][cbBanner.SelectedIndex].tplFormat;
lbSize.Text = string.Format("{0} x {1}", images[0][cbBanner.SelectedIndex].tplImage.Width, images[0][cbBanner.SelectedIndex].tplImage.Height);
2010-01-18 21:13:32 +01:00
2010-03-28 20:29:30 +02:00
if (cbCheckerBoard.Checked) pbPic.BackgroundImage = images[0][cbBanner.SelectedIndex].checkerBoard;
2010-02-24 22:41:37 +01:00
cbIcon.SelectedIndex = -1;
}
}
private void cbIcon_SelectedIndexChanged(object sender, EventArgs e)
{
if (cbIcon.SelectedIndex != -1)
{
2010-03-28 20:29:30 +02:00
pbPic.Image = images[1][cbIcon.SelectedIndex].tplImage;
lbFormat.Text = images[1][cbIcon.SelectedIndex].tplFormat;
lbSize.Text = string.Format("{0} x {1}", images[1][cbIcon.SelectedIndex].tplImage.Width, images[1][cbIcon.SelectedIndex].tplImage.Height);
2010-01-18 21:13:32 +01:00
2010-03-28 20:29:30 +02:00
if (cbCheckerBoard.Checked) pbPic.BackgroundImage = images[1][cbIcon.SelectedIndex].checkerBoard;
2010-02-24 22:41:37 +01:00
cbBanner.SelectedIndex = -1;
}
}
2010-03-28 20:29:30 +02:00
private Image resizeImage(Image img, int x, int y)
{
2010-03-28 20:29:30 +02:00
Image newimage = new Bitmap(x, y);
using (Graphics gfx = Graphics.FromImage(newimage))
gfx.DrawImage(img, 0, 0, x, y);
return newimage;
}
2010-03-28 20:29:30 +02:00
private Image createCheckerBoard(int w, int h)
{
2010-03-28 20:29:30 +02:00
Color darkColor = Color.DarkGray;
Color lightColor = Color.White;
int tileSize = 10;
Bitmap img = new Bitmap(w, h);
using (Graphics g = Graphics.FromImage(img))
{
2010-03-28 20:29:30 +02:00
g.Clear(lightColor);
for (int col = 0; col < w; col += tileSize)
{
for (int row = 0; row < h; row += tileSize)
{
Color curColor;
if ((col / tileSize) % 2 == 0)
curColor = (row / tileSize) % 2 == 0 ? darkColor : lightColor;
else
curColor = (row / tileSize) % 2 == 0 ? lightColor : darkColor;
if (curColor == lightColor) continue;
Rectangle rect = new Rectangle(col, row, tileSize, tileSize);
g.FillRectangle(new SolidBrush(darkColor), rect);
}
}
}
2010-03-28 20:29:30 +02:00
return (Image)img;
}
private void btnReplace_Click(object sender, EventArgs e)
{
cmFormat.Show(MousePosition);
}
private void cmFormat_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "PNG|*.png|JPG|*.jpg|GIF|*.gif|BMP|*.bmp|TPL|*.tpl|All|*.png;*.jpg;*.gif;*.bmp;*.tpl";
ofd.FilterIndex = 6;
if (ofd.ShowDialog() == DialogResult.OK)
{
try
{
2010-03-28 20:29:30 +02:00
string tplName;
TPL tmpTpl = new TPL();
Image newImg;
2010-03-28 20:29:30 +02:00
if (cbIcon.SelectedIndex == -1)
{
tplName = cbBanner.SelectedItem.ToString().ToLower();
tmpTpl.LoadFile(bannerBin.Data[bannerBin.GetNodeIndex(tplName)]);
}
else
{
tplName = cbIcon.SelectedItem.ToString().ToLower();
tmpTpl.LoadFile(iconBin.Data[iconBin.GetNodeIndex(tplName)]);
}
2010-03-28 20:29:30 +02:00
if (!ofd.FileName.ToLower().EndsWith(".tpl")) newImg = Image.FromFile(ofd.FileName);
else
{
2010-03-28 20:29:30 +02:00
TPL newTpl = TPL.Load(ofd.FileName);
newImg = newTpl.ExtractTexture();
}
2010-03-28 20:29:30 +02:00
Size tplSize = tmpTpl.GetTextureSize(0);
TPL_Format tplFormat;
if (newImg.Width != tplSize.Width ||
newImg.Height != tplSize.Height)
newImg = resizeImage(newImg, tplSize.Width, tplSize.Height);
ToolStripMenuItem cmSender = sender as ToolStripMenuItem;
switch (cmSender.Tag.ToString().ToLower())
{
2010-02-06 00:09:36 +01:00
case "i4":
2010-03-28 20:29:30 +02:00
tplFormat = TPL_Format.I4;
2010-02-06 00:09:36 +01:00
break;
case "i8":
2010-03-28 20:29:30 +02:00
tplFormat = TPL_Format.I8;
2010-02-06 00:09:36 +01:00
break;
case "ia4":
2010-03-28 20:29:30 +02:00
tplFormat = TPL_Format.IA4;
2010-02-06 00:09:36 +01:00
break;
case "ia8":
2010-03-28 20:29:30 +02:00
tplFormat = TPL_Format.IA8;
2010-02-06 00:09:36 +01:00
break;
case "rgb565":
2010-03-28 20:29:30 +02:00
tplFormat = TPL_Format.RGB565;
break;
case "rgb5a3":
2010-03-28 20:29:30 +02:00
tplFormat = TPL_Format.RGB5A3;
break;
default:
2010-03-28 20:29:30 +02:00
tplFormat = TPL_Format.RGBA8;
break;
}
2010-03-28 20:29:30 +02:00
tmpTpl.RemoveTexture(0);
tmpTpl.AddTexture(newImg, tplFormat);
2010-02-06 00:09:36 +01:00
if (cbBanner.SelectedIndex != -1)
{
2010-03-28 20:29:30 +02:00
bannerBin.ReplaceFile(bannerBin.GetNodeIndex(tplName), tmpTpl.ToByteArray());
images[0][cbBanner.SelectedIndex].tplImage = tmpTpl.ExtractTexture();
images[0][cbBanner.SelectedIndex].tplFormat = tmpTpl.GetTextureFormat(0).ToString();
2010-02-06 00:09:36 +01:00
2010-03-28 20:29:30 +02:00
pbPic.Image = images[0][cbBanner.SelectedIndex].tplImage;
lbFormat.Text = images[0][cbBanner.SelectedIndex].tplFormat;
lbSize.Text = string.Format("{0} x {1}", images[0][cbBanner.SelectedIndex].tplImage.Width, images[0][cbBanner.SelectedIndex].tplImage.Height);
2010-02-06 00:09:36 +01:00
}
else
{
2010-03-28 20:29:30 +02:00
iconBin.ReplaceFile(iconBin.GetNodeIndex(tplName), tmpTpl.ToByteArray());
images[1][cbIcon.SelectedIndex].tplImage = tmpTpl.ExtractTexture();
images[1][cbIcon.SelectedIndex].tplFormat = tmpTpl.GetTextureFormat(0).ToString();
2010-02-06 00:09:36 +01:00
2010-03-28 20:29:30 +02:00
pbPic.Image = images[1][cbIcon.SelectedIndex].tplImage;
lbFormat.Text = images[1][cbIcon.SelectedIndex].tplFormat;
lbSize.Text = string.Format("{0} x {1}", images[1][cbIcon.SelectedIndex].tplImage.Width, images[1][cbIcon.SelectedIndex].tplImage.Height);
2010-02-06 00:09:36 +01:00
}
if (cbBanner.SelectedIndex != -1) cbBanner.Select();
else if (cbIcon.SelectedIndex != -1) cbIcon.Select();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
2009-11-08 23:53:22 +01:00
}
2010-03-28 20:29:30 +02:00
private void cmChangeFormat_Click(object sender, EventArgs e)
{
try
{
string tplName;
TPL tmpTpl = new TPL();
Image newImg;
if (cbIcon.SelectedIndex == -1)
{
tplName = cbBanner.SelectedItem.ToString().ToLower();
tmpTpl.LoadFile(bannerBin.Data[bannerBin.GetNodeIndex(tplName)]);
}
else
{
tplName = cbIcon.SelectedItem.ToString().ToLower();
tmpTpl.LoadFile(iconBin.Data[iconBin.GetNodeIndex(tplName)]);
}
newImg = tmpTpl.ExtractTexture();
TPL_Format tplFormat;
ToolStripMenuItem cmSender = sender as ToolStripMenuItem;
switch (cmSender.Tag.ToString().ToLower())
{
case "i4":
tplFormat = TPL_Format.I4;
break;
case "i8":
tplFormat = TPL_Format.I8;
break;
case "ia4":
tplFormat = TPL_Format.IA4;
break;
case "ia8":
tplFormat = TPL_Format.IA8;
break;
case "rgb565":
tplFormat = TPL_Format.RGB565;
break;
case "rgb5a3":
tplFormat = TPL_Format.RGB5A3;
break;
default:
tplFormat = TPL_Format.RGBA8;
break;
}
if (tmpTpl.GetTextureFormat(0) == tplFormat) return;
tmpTpl.RemoveTexture(0);
tmpTpl.AddTexture(newImg, tplFormat);
if (cbBanner.SelectedIndex != -1)
{
bannerBin.ReplaceFile(bannerBin.GetNodeIndex(tplName), tmpTpl.ToByteArray());
images[0][cbBanner.SelectedIndex].tplImage = tmpTpl.ExtractTexture();
images[0][cbBanner.SelectedIndex].tplFormat = tmpTpl.GetTextureFormat(0).ToString();
pbPic.Image = images[0][cbBanner.SelectedIndex].tplImage;
lbFormat.Text = images[0][cbBanner.SelectedIndex].tplFormat;
lbSize.Text = string.Format("{0} x {1}", images[0][cbBanner.SelectedIndex].tplImage.Width, images[0][cbBanner.SelectedIndex].tplImage.Height);
}
else
{
iconBin.ReplaceFile(iconBin.GetNodeIndex(tplName), tmpTpl.ToByteArray());
images[1][cbIcon.SelectedIndex].tplImage = tmpTpl.ExtractTexture();
images[1][cbIcon.SelectedIndex].tplFormat = tmpTpl.GetTextureFormat(0).ToString();
pbPic.Image = images[1][cbIcon.SelectedIndex].tplImage;
lbFormat.Text = images[1][cbIcon.SelectedIndex].tplFormat;
lbSize.Text = string.Format("{0} x {1}", images[1][cbIcon.SelectedIndex].tplImage.Width, images[1][cbIcon.SelectedIndex].tplImage.Height);
}
if (cbBanner.SelectedIndex != -1) cbBanner.Select();
else if (cbIcon.SelectedIndex != -1) cbIcon.Select();
}
catch (Exception ex)
{ MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); }
}
private void cbCheckerBoard_CheckedChanged(object sender, EventArgs e)
{
if (cbCheckerBoard.Checked)
{
if (cbBanner.SelectedIndex != -1)
pbPic.BackgroundImage = images[0][cbBanner.SelectedIndex].checkerBoard;
else
pbPic.BackgroundImage = images[1][cbIcon.SelectedIndex].checkerBoard;
}
else pbPic.BackgroundImage = null;
if (cbBanner.SelectedIndex != -1)
cbBanner.Focus();
else
cbIcon.Focus();
}
2009-11-08 23:53:22 +01:00
}
}