1
0
mirror of synced 2024-12-04 20:08:00 +01:00
Switch-Toolbox/File_Format_Library/GUI/BFFNT/BffntEditor.cs

423 lines
13 KiB
C#
Raw Normal View History

2019-07-17 22:14:55 +02:00
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Toolbox.Library.Forms;
using Toolbox.Library;
using LibEveryFileExplorer.GFX;
2019-07-17 22:14:55 +02:00
namespace FirstPlugin.Forms
{
public partial class BffntEditor : STUserControl, IFIleEditor
2019-07-17 22:14:55 +02:00
{
public BffntEditor()
{
InitializeComponent();
stTabControl1.myBackColor = FormThemes.BaseTheme.FormBackColor;
pictureBoxCustom1.BackColor = Color.Black;
pictureBoxCustom1.BackgroundImage = null;
2019-07-17 22:14:55 +02:00
}
public List<IFileFormat> GetFileFormats()
{
return new List<IFileFormat>() { FileFormat };
}
2019-07-18 04:02:57 +02:00
private Image PanelImage { get; set; }
private BitmapFont bitmapFont;
2019-07-18 04:02:57 +02:00
2019-07-17 22:14:55 +02:00
private FFNT ActiveFile;
private BFFNT FileFormat;
2019-07-17 22:14:55 +02:00
public void LoadFontFile(BFFNT fontFile)
{
FileFormat = fontFile;
2019-07-17 22:14:55 +02:00
ActiveFile = fontFile.bffnt;
fontTypeCB.Bind(typeof(FINF.FontType), ActiveFile.FontSection, "Type");
fontTypeCB.SelectedItem = ActiveFile.FontSection.Type;
encodingTypeCB.Bind(typeof(FINF.CharacterCode), ActiveFile.FontSection, "CharEncoding");
encodingTypeCB.SelectedItem = ActiveFile.FontSection.CharEncoding;
lineFeedUD.Bind(ActiveFile.FontSection, "LineFeed");
leftSpacingUD.Bind(ActiveFile.FontSection, "DefaultLeftWidth");
charWidthUD.Bind(ActiveFile.FontSection, "DefaultCharWidth");
glyphWidthCB.Bind(ActiveFile.FontSection, "DefaultGlyphWidth");
ascentUD.Bind(ActiveFile.FontSection, "Ascent");
fontWidthUD.Bind(ActiveFile.FontSection, "Width");
fontHeightUD.Bind(ActiveFile.FontSection, "Height");
2019-07-18 04:02:57 +02:00
ReloadCharacterCodes();
2019-07-17 22:14:55 +02:00
ReloadTextures();
try {
bitmapFont = fontFile.bffnt.GetBitmapFont();
}
catch
{
}
textPreviewTB.Text = "This is a preview!";
if (bitmapFont != null)
pictureBoxCustom1.Image = bitmapFont.PrintToBitmap(textPreviewTB.Text, new BitmapFont.FontRenderSettings());
}
public override void OnControlClosing()
{
bitmapFont?.Dispose();
2019-07-17 22:14:55 +02:00
}
2019-07-18 04:02:57 +02:00
private void ReloadCharacterCodes()
{
foreach (char entry in ActiveFile.FontSection.CodeMapDictionary.Keys)
characterCodeCB.Items.Add(entry);
if (ActiveFile.FontSection.CodeMapDictionary.Count > 0)
characterCodeCB.SelectedIndex = 0;
}
2019-07-17 22:14:55 +02:00
private void ReloadTextures()
{
imagesCB.Items.Clear();
var textureGlyph = ActiveFile.FontSection.TextureGlyph;
for (int i = 0; i < textureGlyph.SheetCount; i++)
imagesCB.Items.Add($"Image {i}");
if (textureGlyph.SheetCount > 0)
imagesCB.SelectedIndex = 0;
}
2019-07-18 20:31:27 +02:00
private void imagePanel_MouseClick(object sender, MouseEventArgs e)
{
if (ActiveFile == null) return;
int ImageIndex = imagesCB.SelectedIndex;
2019-07-18 20:47:00 +02:00
if (e.Button == MouseButtons.Right && ImageIndex != -1)
2019-07-18 20:31:27 +02:00
{
2019-07-18 20:47:00 +02:00
var image = ActiveFile.FontSection.TextureGlyph.GetImageSheet(ImageIndex);
imageMenuStrip.Items.Clear();
imageMenuStrip.Items.Add(new ToolStripMenuItem("Export", null, ExportImageAction, Keys.Control | Keys.E));
imageMenuStrip.Items.Add(new ToolStripMenuItem("Replace", null, ReplaceImageAction, Keys.Control | Keys.R));
imageMenuStrip.Items.Add(new ToolStripMenuItem("Copy", null, CopyImageAction, Keys.Control | Keys.C));
imageMenuStrip.Items.Add(new ToolStripMenuItem("Open Image Editor", null, ImageEditorAction, Keys.Control | Keys.E));
2019-07-18 20:47:00 +02:00
imageMenuStrip.Show(Cursor.Position);
2019-07-18 20:31:27 +02:00
}
}
2019-07-17 22:14:55 +02:00
private void imagesCB_SelectedIndexChanged(object sender, EventArgs e)
{
int ImageIndex = imagesCB.SelectedIndex;
if (ImageIndex != -1)
2019-07-18 20:47:00 +02:00
UpdateImagePanel(ImageIndex);
}
2019-07-17 22:14:55 +02:00
2019-07-18 20:47:00 +02:00
private void UpdateImagePanel(int ImageIndex)
{
var image = ActiveFile.FontSection.TextureGlyph.GetImageSheet(ImageIndex);
bool IsBntx = ActiveFile.FontSection.TextureGlyph.BinaryTextureFile != null;
2019-07-18 20:47:00 +02:00
if (IsBntx)
{
PanelImage = image.GetBitmap(ImageIndex);
}
else
{
PanelImage = image.GetBitmap();
}
2019-07-18 20:47:00 +02:00
if (PanelImage != null)
{
PanelImage.RotateFlip(RotateFlipType.RotateNoneFlipY);
2019-07-17 22:14:55 +02:00
}
2019-07-18 20:47:00 +02:00
FillCells();
imagePanel.Refresh();
2019-07-17 22:14:55 +02:00
}
2019-07-18 04:02:57 +02:00
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Rectangle ee = new Rectangle(10, 10, 30, 30);
using (Pen pen = new Pen(Color.Red, 2))
{
e.Graphics.DrawRectangle(pen, ee);
}
}
private void ImageEditorAction(object sender, EventArgs e)
{
int ImageIndex = imagesCB.SelectedIndex;
if (ImageIndex != -1)
{
var image = ActiveFile.FontSection.TextureGlyph.GetImageSheet(ImageIndex);
bool IsBntx = ActiveFile.FontSection.TextureGlyph.BinaryTextureFile != null;
ImageEditorForm form = new ImageEditorForm(true);
form.editorBase.Text = Text;
form.editorBase.Dock = DockStyle.Fill;
2019-08-08 03:14:18 +02:00
image.Parameters.FlipY = true;
if (IsBntx)
2019-08-08 03:14:18 +02:00
{
form.editorBase.LoadProperties(((TextureData)image).Texture);
2019-08-08 03:14:18 +02:00
form.editorBase.LoadImage(image, ImageIndex);
}
else
2019-08-08 03:14:18 +02:00
{
form.editorBase.LoadProperties(image.GenericProperties);
2019-08-08 03:14:18 +02:00
form.editorBase.LoadImage(image);
}
if (form.ShowDialog() == DialogResult.OK)
{
UpdateImagePanel(ImageIndex);
}
}
}
2019-07-18 20:47:00 +02:00
private void ReplaceImageAction(object sender, EventArgs e)
{
int ImageIndex = imagesCB.SelectedIndex;
if (ImageIndex != -1)
{
var image = ActiveFile.FontSection.TextureGlyph.GetImageSheet(ImageIndex);
bool IsBntx = ActiveFile.FontSection.TextureGlyph.BinaryTextureFile != null;
image.Parameters.FlipY = true;
2019-07-18 20:47:00 +02:00
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = image.ReplaceFilter;
ofd.Multiselect = false;
if (ofd.ShowDialog() == DialogResult.OK)
{
if (IsBntx)
2019-08-08 03:04:44 +02:00
((TextureData)image).Replace(ofd.FileName, 0, (uint)ImageIndex, image.Format, ((TextureData)image).Texture.SurfaceDim);
else
image.Replace(ofd.FileName);
2019-07-18 20:47:00 +02:00
}
UpdateImagePanel(ImageIndex);
}
}
private void ExportImageAction(object sender, EventArgs e)
2019-07-17 22:14:55 +02:00
{
int ImageIndex = imagesCB.SelectedIndex;
if (ImageIndex != -1)
{
var image = ActiveFile.FontSection.TextureGlyph.GetImageSheet(ImageIndex);
bool IsBntx = ActiveFile.FontSection.TextureGlyph.BinaryTextureFile != null;
image.Parameters.FlipY = true;
if (IsBntx)
image.ExportArrayImage(ImageIndex);
2019-07-17 22:14:55 +02:00
else
image.ExportImage();
2019-07-17 22:14:55 +02:00
}
}
2019-07-18 04:02:57 +02:00
public class FontCell
{
public Rectangle DrawnRectangle;
public Color Color { get; set; }
public FontCell()
{
Color = Color.Cyan;
}
public bool IsHit(int X, int Y)
{
if (DrawnRectangle == null) return false;
if ((X > DrawnRectangle.X) && (X < DrawnRectangle.X + DrawnRectangle.Width) &&
(Y > DrawnRectangle.Y) && (Y < DrawnRectangle.Y + DrawnRectangle.Height))
return true;
else
return false;
}
public bool IsSelected { get; private set; }
public void Select()
{
Color = Color.Blue;
IsSelected = true;
}
public void Unselect()
{
Color = Color.Cyan;
IsSelected = false;
}
2019-07-18 04:02:57 +02:00
}
private FontCell[] FontCells;
public GlyphImage[] GlyphImages;
public class GlyphImage
{
public Image Image { get; set; }
}
private void FillCells()
{
List<GlyphImage> images = new List<GlyphImage>();
List<FontCell> Cells = new List<FontCell>();
var textureGlyph = ActiveFile.FontSection.TextureGlyph;
var fontSection = ActiveFile.FontSection;
PanelImage = BitmapExtension.Resize(PanelImage, textureGlyph.SheetWidth, textureGlyph.SheetHeight);
int y = 0;
for (int c = 0; c < (int)textureGlyph.RowCount; c++)
{
int x = 0;
for (int r = 0; r < (int)textureGlyph.LinesCount; r++)
{
var rect = new Rectangle(x, y, textureGlyph.CellWidth, textureGlyph.CellHeight);
Cells.Add(new FontCell()
{
DrawnRectangle = rect,
});
/* var glyphImage = new GlyphImage();
glyphImage.Image = CopyRegionIntoImage(bitmap, rect);
glyphImage.Image.Save($"Glpyh{c} {r}.png");
images.Add(glyphImage);*/
x += (int)textureGlyph.CellWidth;
}
y += (int)textureGlyph.CellHeight;
}
GlyphImages = images.ToArray();
FontCells = Cells.ToArray();
}
private static Bitmap CopyRegionIntoImage(Image srcBitmap, Rectangle srcRegion)
{
Bitmap destBitmap = new Bitmap(srcRegion.Width, srcRegion.Height);
using (Graphics grD = Graphics.FromImage(destBitmap))
{
grD.DrawImage(srcBitmap, new Rectangle(0,0,destBitmap.Width, destBitmap.Height), srcRegion, GraphicsUnit.Pixel);
}
return destBitmap;
}
2019-07-18 04:02:57 +02:00
private void imagePanel_Paint(object sender, PaintEventArgs e)
{
if (PanelImage == null)
return;
2019-07-18 04:02:57 +02:00
Graphics graphics = e.Graphics;
graphics.DrawImage(PanelImage, 0.0f, 0.0f);
if (ActiveFile == null)
return;
var textureGlyph = ActiveFile.FontSection.TextureGlyph;
if (FontCells == null)
return;
2019-07-18 04:02:57 +02:00
for (int i = 0; i < FontCells.Length; i++) {
if (FontCells[i].IsSelected)
2019-07-18 04:02:57 +02:00
{
SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(70, 0, 255, 255));
2019-07-18 04:02:57 +02:00
graphics.DrawRectangle(new Pen(FontCells[i].Color, 1), FontCells[i].DrawnRectangle);
graphics.FillRectangle(semiTransBrush, FontCells[i].DrawnRectangle);
2019-07-18 04:02:57 +02:00
}
}
graphics.ScaleTransform(textureGlyph.SheetWidth, textureGlyph.SheetHeight);
2019-07-18 04:02:57 +02:00
}
2019-07-18 20:47:00 +02:00
private void CopyImageAction(object sender, EventArgs e)
2019-07-17 22:14:55 +02:00
{
2019-07-18 04:02:57 +02:00
if (PanelImage != null)
Clipboard.SetImage(PanelImage);
2019-07-17 22:14:55 +02:00
}
bool isMouseDown = false;
private void imagePanel_MouseDown(object sender, MouseEventArgs e) {
isMouseDown = true;
}
private void imagePanel_MouseUp(object sender, MouseEventArgs e) {
isMouseDown = false;
}
private void imagePanel_MouseMove(object sender, MouseEventArgs e)
{
if (FontCells != null)
{
for (int i = 0; i < FontCells.Length; i++)
{
if (FontCells[i] == null) continue;
if (FontCells[i].IsHit(e.X, e.Y))
FontCells[i].Select();
else
FontCells[i].Unselect();
}
imagePanel.Refresh();
}
}
private void imagePanel_MouseLeave(object sender, EventArgs e)
{
isMouseDown = false;
if (FontCells != null)
{
for (int i = 0; i < FontCells.Length; i++)
FontCells[i].Unselect();
imagePanel.Refresh();
}
}
private void textPreviewTB_TextChanged(object sender, EventArgs e)
{
if (bitmapFont != null)
pictureBoxCustom1.Image = bitmapFont.PrintToBitmap(textPreviewTB.Text, new BitmapFont.FontRenderSettings());
}
2019-07-17 22:14:55 +02:00
}
public class ImagePaenl : STPanel
{
public ImagePaenl()
{
this.SetStyle(
ControlStyles.AllPaintingInWmPaint |
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer,
true);
}
}
2019-07-17 22:14:55 +02:00
}