Skip to content
This repository has been archived by the owner on Oct 14, 2019. It is now read-only.

Commit

Permalink
Merge pull request #1 from davpsh/dev
Browse files Browse the repository at this point in the history
Dev version 1.4.0
  • Loading branch information
handsomematt authored Jul 19, 2016
2 parents efe54af + 7a29831 commit 52d6911
Show file tree
Hide file tree
Showing 6 changed files with 517 additions and 74 deletions.
2 changes: 1 addition & 1 deletion src/KleiLib/KleiLib.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x86</PlatformTarget>
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand Down
361 changes: 296 additions & 65 deletions src/TEXTool/MainForm.Designer.cs

Large diffs are not rendered by default.

125 changes: 123 additions & 2 deletions src/TEXTool/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,16 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using System.Reflection;
using System.Windows.Forms;

using System.Drawing;
using System.Drawing.Drawing2D;

namespace TEXTool
{
public partial class MainForm : Form
{
public TEXTool Tool;
GraphicsPath graphicsPath;
float offsetX = 0, offsetY = 0, scaleX = 1, scaleY = 1;

public MainForm()
{
Expand All @@ -44,12 +49,41 @@ public MainForm()
InitializeComponent();
FillZoomLevelComboBox();
versionToolStripLabel.Text = string.Format("Version: {0}", Assembly.GetEntryAssembly().GetName().Version);

foreach (PropertyInfo prop in typeof(Color).GetProperties())
{
if (prop.PropertyType.FullName == "System.Drawing.Color")
{
atlasElementBorderColors.ComboBox.Items.Add(prop.Name);
}
}
atlasElementBorderColors.ComboBox.SelectedItem = "Black";

atlasElementsListToolStripComboBox.ComboBox.DisplayMember = "Name";
}

#region

void tool_FileRawImage(object sender, FileRawImageEventArgs e)
{
atlasElementsCountIntToolStripLabel.Text = e.AtlasElements.Count.ToString();
atlasElementsListToolStripComboBox.ComboBox.SelectedIndex = -1;
atlasElementsListToolStripComboBox.ComboBox.Items.Clear();

graphicsPath = null;
atlasElementsListToolStripComboBox.Enabled = atlasElementBorderColors.Enabled = false;
atlasElementWidthToolStrip.Text = atlasElementHeightToolStrip.Text = atlasElementXToolStrip.Text = atlasElementYToolStrip.Text = "0";

if (e.AtlasElements.Count > 0)
{
graphicsPath = new GraphicsPath();
atlasElementsListToolStripComboBox.Enabled = atlasElementBorderColors.Enabled = true;
foreach (KleiTextureAtlasElement el in e.AtlasElements)
{
atlasElementsListToolStripComboBox.Items.Add(el);
}
}

imageBox.Image = e.Image;
zoomLevelToolStripComboBox.Text = string.Format("{0}%", imageBox.Zoom);
}
Expand Down Expand Up @@ -144,12 +178,12 @@ private void infoToolStripButton_Click(object sender, EventArgs e)

#region Misc Form Event Handlers

private void imageBox1_ZoomLevelsChanged(object sender, EventArgs e)
private void imageBox_ZoomLevelsChanged(object sender, EventArgs e)
{
FillZoomLevelComboBox();
}

private void imageBox1_ZoomChanged(object sender, EventArgs e)
private void imageBox_ZoomChanged(object sender, EventArgs e)
{
zoomLevelToolStripComboBox.Text = string.Format("{0}%", imageBox.Zoom);
}
Expand Down Expand Up @@ -181,5 +215,92 @@ protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
}

#endregion

#region Dev Custom Functions

private void DrawRectangle(KleiTextureAtlasElement element)
{
int x, y, width, height;
x = element.ImgHmin;
y = element.ImgVmin;

/* INVERT THE Y-AXIS */
if (element.ImgVmin > element.ImgVmax)
{
y = element.ImgVmax;
}

width = element.ImgHmax - element.ImgHmin;
height = Math.Abs(element.ImgVmax - element.ImgVmin);

graphicsPath = new GraphicsPath();
graphicsPath.AddRectangle(new Rectangle(x, y, width, height));

atlasElementWidthToolStrip.Text = width.ToString();
atlasElementHeightToolStrip.Text = height.ToString();
atlasElementXToolStrip.Text = x.ToString();
atlasElementYToolStrip.Text = y.ToString();

imageBox.Invalidate();
}

#endregion

#region Dev Event Handlers

private void zoomLevelToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (imageBox.Image != null)
{
int z = int.Parse(zoomLevelToolStripComboBox.SelectedItem.ToString().Replace("%", ""));
imageBox.Zoom = z;
}
}

private void atlasElementsListToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
var element = (KleiTextureAtlasElement)atlasElementsListToolStripComboBox.ComboBox.SelectedItem;
if (element != null)
{
DrawRectangle(element);
}
}

private void atlasElementBorderColors_SelectedIndexChanged(object sender, EventArgs e)
{
if (graphicsPath != null)
{
imageBox.Refresh();
}
}

private void imageBox_Paint(object sender, PaintEventArgs e)
{
if (graphicsPath != null)
{
scaleX = imageBox.Zoom / 100f;
scaleY = imageBox.Zoom / 100f;
offsetX = ((imageBox.ClientSize.Width - imageBox.PreferredSize.Width) / 2f);
offsetY = ((imageBox.ClientSize.Height - imageBox.PreferredSize.Height) / 2f);

if (offsetX < 0)
{
offsetX = -imageBox.HorizontalScroll.Value;
}
if (offsetY < 0)
{
offsetY = -imageBox.VerticalScroll.Value;
}

e.Graphics.TranslateTransform(offsetX, offsetY);
e.Graphics.ScaleTransform(scaleX, scaleY);

Color color = Color.FromName(atlasElementBorderColors.ComboBox.SelectedItem.ToString());
Pen pen = new Pen(new SolidBrush(color), 5f);
e.Graphics.DrawPath(pen, graphicsPath);
}
}

#endregion
}
}
9 changes: 6 additions & 3 deletions src/TEXTool/MainForm.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,14 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>375, 17</value>
</metadata>
<metadata name="mainToolStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
<value>255, 17</value>
</metadata>
<metadata name="statusStrip1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>145, 17</value>
<metadata name="atlasElementsToolStrip.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>485, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
Expand Down
2 changes: 1 addition & 1 deletion src/TEXTool/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

[assembly: Guid("76bbf4d2-c278-4dae-a48c-18f660bc6e61")]

[assembly: AssemblyVersion("1.3.0.0")]
[assembly: AssemblyVersion("1.4.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
92 changes: 90 additions & 2 deletions src/TEXTool/TEXTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,29 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
using KleiLib;
using SquishNET;

using System.Collections.Generic;
using System.Xml;

namespace TEXTool
{
public class KleiTextureAtlasElement
{
public string Name { get; set; }
public int ImgHmin { get; set; }
public int ImgHmax { get; set; }
public int ImgVmin { get; set; }
public int ImgVmax { get; set; }

public KleiTextureAtlasElement(string name, int u1, int u2, int v1, int v2)
{
this.Name = name;
this.ImgHmin = u1;
this.ImgHmax = u2;
this.ImgVmin = v1;
this.ImgVmax = v2;
}
}

public class FileOpenedEventArgs : EventArgs
{
public string FileName { get; set; }
Expand All @@ -53,10 +74,12 @@ public FileOpenedEventArgs(string filename) {
public class FileRawImageEventArgs : EventArgs
{
public Bitmap Image { get; set; }
public List<KleiTextureAtlasElement> AtlasElements { get; set; }

public FileRawImageEventArgs(Bitmap image)
public FileRawImageEventArgs(Bitmap image, List<KleiTextureAtlasElement> elements)
{
this.Image = image;
this.AtlasElements = elements;
}
}

Expand Down Expand Up @@ -151,6 +174,18 @@ public void OpenFile(string filename, Stream stream)
throw new Exception("Unknown pixel format?");
}

string atlasExt = "xml";
FileInfo fileInfo = new FileInfo(filename);
string fileDir = fileInfo.DirectoryName;
string fileNameWithoutExt = fileInfo.Name.Replace(fileInfo.Extension, "");
string atlasDataPath = fileDir + @"\" + fileNameWithoutExt + "." + atlasExt;
List<KleiTextureAtlasElement> atlasElements = new List<KleiTextureAtlasElement>();

if (File.Exists(atlasDataPath))
{
atlasElements = ReadAtlasData(atlasDataPath, mipmap.Width, mipmap.Height);
}

var imgReader = new BinaryReader(new MemoryStream(argbData));

Bitmap pt = new Bitmap((int)mipmap.Width, (int)mipmap.Height);
Expand All @@ -169,9 +204,62 @@ public void OpenFile(string filename, Stream stream)

CurrentFileRaw = pt;

OnRawImage(new FileRawImageEventArgs(pt));
OnRawImage(new FileRawImageEventArgs(pt, atlasElements));
}

private List<KleiTextureAtlasElement> ReadAtlasData(string path, int mipmapWidth, int mipmapHeight)
{
var AtlasElements = new List<KleiTextureAtlasElement>();
try
{
XmlDocument xDoc = new XmlDocument();
xDoc.Load(path);

XmlNode xNodeElements = xDoc.SelectSingleNode("Atlas/Elements");
foreach (XmlNode xChild in xNodeElements.ChildNodes)
{
string name = xChild.Attributes.GetNamedItem("name").Value;
double u1 = Convert.ToDouble(xChild.Attributes.GetNamedItem("u1").Value.Replace(".", ","));
double u2 = Convert.ToDouble(xChild.Attributes.GetNamedItem("u2").Value.Replace(".", ","));

/* !!! IMPORTANT TIP !!!
* You may need to invert the y-axis depending on the software you use to check your pixel coordinates.
* Some image softwares count from the bottom left corner and others from the top left corner.
* The former can be used as-is because the game uses the same format.
* But if your software counts pixels starting from the upper left corner, you should not use them directly.
* Instead, subtract your resulting coordinates them from 1.
* E.g. if your y-coordinate is 0.3, you would use 0.7 (1 – 0.3).
* ONLY for Y-coordinates.
*/

/* NORMAL THE Y-AXIS */
double v1 = Convert.ToDouble(xChild.Attributes.GetNamedItem("v1").Value.Replace(".", ","));
double v2 = Convert.ToDouble(xChild.Attributes.GetNamedItem("v2").Value.Replace(".", ","));

/* INVERT THE Y-AXIS */
v1 = 1 - v1;
v2 = 1 - v2;

int imgHmin, imgHmax, imgVmin, imgVmax;
double margin = 0.5;
imgHmin = Convert.ToInt16(u1 * mipmapWidth - margin);
imgHmax = Convert.ToInt16(u2 * mipmapWidth - margin);
imgVmin = Convert.ToInt16(v1 * mipmapHeight - margin);
imgVmax = Convert.ToInt16(v2 * mipmapHeight - margin);

AtlasElements.Add(new KleiTextureAtlasElement(name, imgHmin, imgHmax, imgVmin, imgVmax));
}
}
catch (Exception e)
{
AtlasElements.Clear();
Console.WriteLine(e.Message);
}
return AtlasElements;
}



public void SaveFile(string FilePath)
{
CurrentFileRaw.Save(FilePath);
Expand Down

0 comments on commit 52d6911

Please sign in to comment.