2014-12-22 21:36:49 +00:00
|
|
|
|
#region ================== Namespaces
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Drawing.Imaging;
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
using CodeImp.DoomBuilder.Windows;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
namespace CodeImp.DoomBuilder.BuilderModes
|
|
|
|
|
{
|
|
|
|
|
internal struct FitTextureOptions
|
|
|
|
|
{
|
2017-07-18 11:56:27 +00:00
|
|
|
|
public float HorizontalRepeat;
|
|
|
|
|
public float VerticalRepeat;
|
|
|
|
|
public int PatternWidth;
|
|
|
|
|
public int PatternHeight;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
public bool FitWidth;
|
|
|
|
|
public bool FitHeight;
|
|
|
|
|
public bool FitAcrossSurfaces;
|
2017-07-18 11:56:27 +00:00
|
|
|
|
public bool AutoWidth;
|
|
|
|
|
public bool AutoHeight;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
public Rectangle GlobalBounds;
|
|
|
|
|
public Rectangle Bounds;
|
|
|
|
|
|
|
|
|
|
//Initial texture coordinats
|
|
|
|
|
public float InitialOffsetX;
|
|
|
|
|
public float InitialOffsetY;
|
2015-03-10 18:49:29 +00:00
|
|
|
|
public float ControlSideOffsetX;
|
|
|
|
|
public float ControlSideOffsetY;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
public float InitialScaleX;
|
|
|
|
|
public float InitialScaleY;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal partial class FitTexturesForm : DelayedForm
|
|
|
|
|
{
|
|
|
|
|
#region ================== Event handlers
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Variables
|
|
|
|
|
|
|
|
|
|
private static Point location = Point.Empty;
|
|
|
|
|
private bool blockupdate;
|
2014-12-23 12:32:08 +00:00
|
|
|
|
private int prevhorizrepeat;
|
|
|
|
|
private int prevvertrepeat;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
|
|
|
|
|
// Settings
|
|
|
|
|
private static int horizontalrepeat = 1;
|
|
|
|
|
private static int verticalrepeat = 1;
|
|
|
|
|
private static bool fitacrosssurfaces = true;
|
|
|
|
|
private static bool fitwidth = true;
|
|
|
|
|
private static bool fitheight = true;
|
|
|
|
|
|
|
|
|
|
//Surface stuff
|
|
|
|
|
private List<SortedVisualSide> strips;
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Constructor
|
|
|
|
|
|
|
|
|
|
public FitTexturesForm()
|
|
|
|
|
{
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
2015-12-28 15:01:53 +00:00
|
|
|
|
if(!location.IsEmpty)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
{
|
|
|
|
|
this.StartPosition = FormStartPosition.Manual;
|
|
|
|
|
this.Location = location;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Methods
|
|
|
|
|
|
2015-03-10 18:49:29 +00:00
|
|
|
|
public bool Setup(IEnumerable<BaseVisualGeometrySidedef> sides)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
{
|
|
|
|
|
// Get shapes
|
|
|
|
|
strips = BuilderModesTools.SortVisualSides(sides);
|
|
|
|
|
|
|
|
|
|
// No dice...
|
|
|
|
|
if(strips.Count == 0)
|
|
|
|
|
{
|
|
|
|
|
General.Interface.DisplayStatus(StatusType.Warning, "Failed to setup sidedef chains...");
|
|
|
|
|
this.DialogResult = DialogResult.Cancel;
|
|
|
|
|
this.Close();
|
2015-03-10 18:49:29 +00:00
|
|
|
|
return false;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
//debug
|
|
|
|
|
DrawDebugUV();
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// Restore settings
|
|
|
|
|
blockupdate = true;
|
|
|
|
|
|
2017-07-18 11:56:27 +00:00
|
|
|
|
horizrepeat.Text = horizontalrepeat.ToString();
|
|
|
|
|
vertrepeat.Text = verticalrepeat.ToString();
|
2014-12-23 12:32:08 +00:00
|
|
|
|
prevhorizrepeat = horizontalrepeat;
|
|
|
|
|
prevvertrepeat = verticalrepeat;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
cbfitconnected.Checked = fitacrosssurfaces;
|
2015-01-29 21:41:16 +00:00
|
|
|
|
cbfitconnected.Enabled = (strips.Count > 1);
|
2014-12-22 21:36:49 +00:00
|
|
|
|
cbfitwidth.Checked = fitwidth;
|
|
|
|
|
cbfitheight.Checked = fitheight;
|
|
|
|
|
UpdateRepeatGroup();
|
|
|
|
|
|
|
|
|
|
blockupdate = false;
|
|
|
|
|
|
|
|
|
|
//trigger update
|
|
|
|
|
UpdateChanges();
|
2015-03-10 18:49:29 +00:00
|
|
|
|
|
|
|
|
|
return true;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateChanges()
|
|
|
|
|
{
|
|
|
|
|
// Apply changes
|
|
|
|
|
FitTextureOptions options = new FitTextureOptions
|
|
|
|
|
{
|
2015-01-29 21:41:16 +00:00
|
|
|
|
FitAcrossSurfaces = (cbfitconnected.Enabled && cbfitconnected.Checked),
|
2014-12-22 21:36:49 +00:00
|
|
|
|
FitWidth = cbfitwidth.Checked,
|
|
|
|
|
FitHeight = cbfitheight.Checked,
|
2017-07-18 11:56:27 +00:00
|
|
|
|
PatternWidth = (int)patternwidth.GetResultFloat(0),
|
|
|
|
|
PatternHeight = (int)patternheight.GetResultFloat(0),
|
|
|
|
|
AutoWidth = cbautowidth.Checked,
|
|
|
|
|
AutoHeight = cbautoheight.Checked,
|
|
|
|
|
HorizontalRepeat = (float)horizrepeat.GetResultFloat(0),
|
|
|
|
|
VerticalRepeat = (float)vertrepeat.GetResultFloat(0)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
foreach(SortedVisualSide side in strips) side.OnTextureFit(options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void UpdateRepeatGroup()
|
|
|
|
|
{
|
|
|
|
|
// Disable whole group?
|
|
|
|
|
repeatgroup.Enabled = cbfitwidth.Checked || cbfitheight.Checked;
|
|
|
|
|
if(!repeatgroup.Enabled) return;
|
|
|
|
|
|
|
|
|
|
// Update control status
|
2017-07-18 11:56:27 +00:00
|
|
|
|
cbautowidth.Enabled = cbfitwidth.Checked;
|
|
|
|
|
patternwidth.Enabled = cbfitwidth.Checked && (cbautowidth.Enabled && cbautowidth.Checked);
|
|
|
|
|
labelpatternwidth.Enabled = patternwidth.Enabled;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
labelhorizrepeat.Enabled = cbfitwidth.Checked;
|
2017-07-18 11:56:27 +00:00
|
|
|
|
horizrepeat.Enabled = cbfitwidth.Checked && !cbautowidth.Checked;
|
|
|
|
|
resethoriz.Enabled = cbfitwidth.Checked && !cbautowidth.Checked;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
|
2017-07-18 11:56:27 +00:00
|
|
|
|
cbautoheight.Enabled = cbfitheight.Checked;
|
|
|
|
|
patternheight.Enabled = cbfitheight.Checked && (cbautoheight.Enabled && cbautoheight.Checked);
|
|
|
|
|
labelpatternheight.Enabled = patternheight.Enabled;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
labelvertrepeat.Enabled = cbfitheight.Checked;
|
2017-07-18 11:56:27 +00:00
|
|
|
|
vertrepeat.Enabled = cbfitheight.Checked && !cbautoheight.Checked;
|
|
|
|
|
resetvert.Enabled = cbfitheight.Checked && !cbautoheight.Checked;
|
|
|
|
|
|
|
|
|
|
|
2014-12-22 21:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Events
|
|
|
|
|
|
|
|
|
|
private void FitTexturesForm_FormClosing(object sender, FormClosingEventArgs e)
|
|
|
|
|
{
|
|
|
|
|
location = this.Location;
|
|
|
|
|
|
|
|
|
|
// Store settings
|
2015-12-28 15:01:53 +00:00
|
|
|
|
if(this.DialogResult == DialogResult.OK)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
{
|
2017-07-18 11:56:27 +00:00
|
|
|
|
horizontalrepeat = (int)horizrepeat.GetResultFloat(0);
|
|
|
|
|
verticalrepeat = (int)vertrepeat.GetResultFloat(0);
|
2014-12-22 21:36:49 +00:00
|
|
|
|
fitacrosssurfaces = cbfitwidth.Checked;
|
|
|
|
|
fitwidth = cbfitwidth.Checked;
|
|
|
|
|
fitheight = cbfitheight.Checked;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void resethoriz_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-12-23 12:32:08 +00:00
|
|
|
|
prevhorizrepeat = 1;
|
2017-07-18 11:56:27 +00:00
|
|
|
|
horizrepeat.Text = "1";
|
2014-12-22 21:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void resetvert_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
2014-12-23 12:32:08 +00:00
|
|
|
|
prevvertrepeat = 1;
|
2017-07-18 11:56:27 +00:00
|
|
|
|
vertrepeat.Text = "1";
|
2014-12-22 21:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-23 12:32:08 +00:00
|
|
|
|
private void horizrepeat_ValueChanged(object sender, EventArgs e)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
{
|
|
|
|
|
if(blockupdate) return;
|
2014-12-23 12:32:08 +00:00
|
|
|
|
|
2017-07-18 11:56:27 +00:00
|
|
|
|
if(horizrepeat.GetResultFloat(0) == 0)
|
2014-12-23 12:32:08 +00:00
|
|
|
|
{
|
2017-07-18 11:56:27 +00:00
|
|
|
|
horizrepeat.Text = prevhorizrepeat > 0 ? "-1" : "1";
|
2014-12-23 12:32:08 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 11:56:27 +00:00
|
|
|
|
prevhorizrepeat = (int)horizrepeat.GetResultFloat(0);
|
2014-12-23 12:32:08 +00:00
|
|
|
|
UpdateChanges();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void vertrepeat_ValueChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if(blockupdate) return;
|
|
|
|
|
|
2017-07-18 11:56:27 +00:00
|
|
|
|
if(vertrepeat.GetResultFloat(0) == 0)
|
2014-12-23 12:32:08 +00:00
|
|
|
|
{
|
2017-07-18 11:56:27 +00:00
|
|
|
|
vertrepeat.Text = prevvertrepeat > 0 ? "-1" : "1";
|
2014-12-23 12:32:08 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 11:56:27 +00:00
|
|
|
|
prevvertrepeat = (int)vertrepeat.GetResultFloat(0);
|
2014-12-22 21:36:49 +00:00
|
|
|
|
UpdateChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 11:56:27 +00:00
|
|
|
|
private void accept_Click(object sender, EventArgs e)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
{
|
|
|
|
|
this.DialogResult = DialogResult.OK;
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void cancel_Click(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
this.DialogResult = DialogResult.Cancel;
|
|
|
|
|
this.Close();
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-12 18:18:48 +00:00
|
|
|
|
private void cb_CheckedChanged(object sender, EventArgs e)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
{
|
|
|
|
|
if(blockupdate) return;
|
|
|
|
|
UpdateRepeatGroup();
|
|
|
|
|
UpdateChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-18 11:56:27 +00:00
|
|
|
|
private void patternsize_ValueChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
if (blockupdate) return;
|
|
|
|
|
UpdateChanges();
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-22 21:36:49 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region ================== Debug
|
|
|
|
|
|
|
|
|
|
#if DEBUG
|
|
|
|
|
private void DrawDebugUV()
|
|
|
|
|
{
|
|
|
|
|
const int margin = 20;
|
2014-12-23 12:32:08 +00:00
|
|
|
|
|
|
|
|
|
//sort by texture...
|
|
|
|
|
Dictionary<long, List<SortedVisualSide>> sortedstrips = new Dictionary<long, List<SortedVisualSide>>();
|
|
|
|
|
foreach(SortedVisualSide side in strips)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
{
|
2014-12-23 12:32:08 +00:00
|
|
|
|
if(!sortedstrips.ContainsKey(side.Side.Texture.LongName))
|
|
|
|
|
sortedstrips.Add(side.Side.Texture.LongName, new List<SortedVisualSide>());
|
2014-12-22 21:36:49 +00:00
|
|
|
|
|
2014-12-23 12:32:08 +00:00
|
|
|
|
sortedstrips[side.Side.Texture.LongName].Add(side);
|
|
|
|
|
}
|
2014-12-22 21:36:49 +00:00
|
|
|
|
|
2015-12-28 15:01:53 +00:00
|
|
|
|
foreach(KeyValuePair<long, List<SortedVisualSide>> pair in sortedstrips)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
{
|
2014-12-23 12:32:08 +00:00
|
|
|
|
//find bounds
|
|
|
|
|
int minx = int.MaxValue;
|
|
|
|
|
int maxx = int.MinValue;
|
|
|
|
|
int miny = int.MaxValue;
|
|
|
|
|
int maxy = int.MinValue;
|
|
|
|
|
|
|
|
|
|
foreach(SortedVisualSide side in pair.Value)
|
2014-12-22 21:36:49 +00:00
|
|
|
|
{
|
2014-12-23 12:32:08 +00:00
|
|
|
|
if(side.Bounds.X < minx) minx = side.Bounds.X;
|
|
|
|
|
if(side.Bounds.X + side.Bounds.Width > maxx) maxx = side.Bounds.X + side.Bounds.Width;
|
|
|
|
|
if(side.Bounds.Y < miny) miny = side.Bounds.Y;
|
|
|
|
|
if(side.Bounds.Y + side.Bounds.Height > maxy) maxy = side.Bounds.Y + side.Bounds.Height;
|
2014-12-22 21:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
2014-12-23 12:32:08 +00:00
|
|
|
|
Bitmap bmp = new Bitmap(maxx - minx + margin * 2, maxy - miny + margin * 2);
|
|
|
|
|
|
|
|
|
|
using(Graphics g = Graphics.FromImage(bmp))
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
|
|
foreach(SortedVisualSide side in pair.Value)
|
|
|
|
|
{
|
|
|
|
|
Color c = General.Colors.BrightColors[General.Random(0, General.Colors.BrightColors.Length - 1)].ToColor();
|
|
|
|
|
int x = side.Bounds.X - minx + margin;
|
|
|
|
|
int y = side.Bounds.Y - miny + margin;
|
|
|
|
|
|
2016-02-01 22:04:00 +00:00
|
|
|
|
using(Pen p = new Pen(c))
|
2015-05-27 10:34:25 +00:00
|
|
|
|
{
|
|
|
|
|
g.DrawRectangle(p, x, y, side.Bounds.Width, side.Bounds.Height);
|
|
|
|
|
}
|
2016-02-01 22:04:00 +00:00
|
|
|
|
using(Brush b = new SolidBrush(c))
|
2015-05-27 10:34:25 +00:00
|
|
|
|
{
|
|
|
|
|
g.DrawString(i++ + ": line " + side.Side.Sidedef.Line.Index + "; x:" + side.Bounds.X + " y:" + side.Bounds.Y, this.Font, b, x + 2, y + 2);
|
|
|
|
|
}
|
2014-12-23 12:32:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bmp.Save("testuv_" + pair.Value[0].Side.Texture.ShortName + ".png", ImageFormat.Png);
|
|
|
|
|
}
|
2014-12-22 21:36:49 +00:00
|
|
|
|
|
|
|
|
|
General.Interface.DisplayStatus(StatusType.Info, "Saved test image!");
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#endregion
|
2017-07-18 11:56:27 +00:00
|
|
|
|
|
|
|
|
|
|
2014-12-22 21:36:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|