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

Commit

Permalink
Added texture power tools (randomise shift values, tiling fit). Fixes #8
Browse files Browse the repository at this point in the history
, fixes #9.
  • Loading branch information
LogicAndTrick committed Jun 15, 2014
1 parent 4465950 commit 4c56285
Show file tree
Hide file tree
Showing 16 changed files with 553 additions and 29 deletions.
8 changes: 5 additions & 3 deletions Sledge.DataStructures/MapObjects/Face.cs
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,11 @@ private void MinimiseTextureShiftValues()
if (Texture.YShift < -Texture.Texture.Height / 2m) Texture.YShift += Texture.Texture.Height;
}

public void FitTextureToPointCloud(Cloud cloud)
public void FitTextureToPointCloud(Cloud cloud, int tileX, int tileY)
{
if (Texture.Texture == null) return;
if (tileX <= 0) tileX = 1;
if (tileY <= 0) tileY = 1;

// Scale will change, no need to use it in the calculations
var xvals = cloud.GetExtents().Select(x => x.Dot(Texture.UAxis)).ToList();
Expand All @@ -313,8 +315,8 @@ public void FitTextureToPointCloud(Cloud cloud)
var maxU = xvals.Max();
var maxV = yvals.Max();

Texture.XScale = (maxU - minU) / Texture.Texture.Width;
Texture.YScale = (maxV - minV) / Texture.Texture.Height;
Texture.XScale = (maxU - minU) / (Texture.Texture.Width * tileX);
Texture.YScale = (maxV - minV) / (Texture.Texture.Height * tileY);
Texture.XShift = -minU / Texture.XScale;
Texture.YShift = -minV / Texture.YScale;

Expand Down
25 changes: 17 additions & 8 deletions Sledge.Editor/Sledge.Editor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@
<DependentUpon>SelectToolSidebarPanel.cs</DependentUpon>
</Compile>
<Compile Include="Tools\SketchTool.cs" />
<Compile Include="Tools\TextureTool\TextureToolSidebarPanel.cs">
<SubType>UserControl</SubType>
</Compile>
<Compile Include="Tools\TextureTool\TextureToolSidebarPanel.Designer.cs">
<DependentUpon>TextureToolSidebarPanel.cs</DependentUpon>
</Compile>
<Compile Include="Tools\VMTool\VMErrorsSidebarPanel.cs">
<SubType>UserControl</SubType>
</Compile>
Expand Down Expand Up @@ -325,17 +331,17 @@
<DependentUpon>SelectControl.cs</DependentUpon>
</Compile>
<Compile Include="Tools\EntityTool.cs" />
<Compile Include="Tools\TransformationTools\RotateTool.cs" />
<Compile Include="Tools\TransformationTools\SkewTool.cs" />
<Compile Include="Tools\TransformationTools\ResizeTool.cs" />
<Compile Include="Tools\SelectTool\TransformationTools\RotateTool.cs" />
<Compile Include="Tools\SelectTool\TransformationTools\SkewTool.cs" />
<Compile Include="Tools\SelectTool\TransformationTools\ResizeTool.cs" />
<Compile Include="Tools\SelectTool\SelectTool.cs" />
<Compile Include="Tools\TextureApplicationForm.cs">
<Compile Include="Tools\TextureTool\TextureApplicationForm.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Tools\TextureApplicationForm.Designer.cs">
<Compile Include="Tools\TextureTool\TextureApplicationForm.Designer.cs">
<DependentUpon>TextureApplicationForm.cs</DependentUpon>
</Compile>
<Compile Include="Tools\TransformationTools\TransformationTool.cs" />
<Compile Include="Tools\SelectTool\TransformationTools\TransformationTool.cs" />
<Compile Include="Tools\VMTool\EditFaceTool.cs" />
<Compile Include="Tools\VMTool\ScaleTool.cs" />
<Compile Include="Tools\VMTool\EditFaceControl.cs">
Expand Down Expand Up @@ -567,7 +573,7 @@
<Compile Include="UI\TextureListPanel.cs">
<SubType>Component</SubType>
</Compile>
<Compile Include="Tools\TextureTool.cs" />
<Compile Include="Tools\TextureTool\TextureTool.cs" />
<Compile Include="Tools\ToolManager.cs" />
<Compile Include="UI\Camera2DViewportListener.cs" />
<Compile Include="UI\Camera3DViewportListener.cs" />
Expand Down Expand Up @@ -654,6 +660,9 @@
<EmbeddedResource Include="Tools\SelectTool\SelectToolSidebarPanel.resx">
<DependentUpon>SelectToolSidebarPanel.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Tools\TextureTool\TextureToolSidebarPanel.resx">
<DependentUpon>TextureToolSidebarPanel.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Tools\VMTool\VMErrorsSidebarPanel.resx">
<DependentUpon>VMErrorsSidebarPanel.cs</DependentUpon>
</EmbeddedResource>
Expand All @@ -670,7 +679,7 @@
<EmbeddedResource Include="Tools\DisplacementTools\SelectControl.resx">
<DependentUpon>SelectControl.cs</DependentUpon>
</EmbeddedResource>
<EmbeddedResource Include="Tools\TextureApplicationForm.resx">
<EmbeddedResource Include="Tools\TextureTool\TextureApplicationForm.resx">
<DependentUpon>TextureApplicationForm.cs</DependentUpon>
<SubType>Designer</SubType>
</EmbeddedResource>
Expand Down
2 changes: 1 addition & 1 deletion Sledge.Editor/Tools/SelectTool/SelectTool.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
using Sledge.Editor.Actions.MapObjects.Selection;
using Sledge.Editor.Clipboard;
using Sledge.Editor.Properties;
using Sledge.Editor.Tools.TransformationTools;
using Sledge.Editor.Tools.SelectTool.TransformationTools;
using Sledge.Editor.Tools.Widgets;
using Sledge.Editor.UI.ObjectProperties;
using Sledge.Graphics;
Expand Down
2 changes: 1 addition & 1 deletion Sledge.Editor/Tools/SelectTool/SelectToolSidebarPanel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
using System.Windows.Forms;
using Sledge.Common.Mediator;
using Sledge.Editor.Brushes;
using Sledge.Editor.Tools.TransformationTools;
using Sledge.Editor.Tools.SelectTool.TransformationTools;
using Sledge.Settings;

namespace Sledge.Editor.Tools.SelectTool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Sledge.Editor.Tools.Widgets;
using Sledge.UI;

namespace Sledge.Editor.Tools.TransformationTools
namespace Sledge.Editor.Tools.SelectTool.TransformationTools
{
/// <summary>
/// Allows the selected objects to be scaled and translated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Sledge.Settings;
using Sledge.UI;

namespace Sledge.Editor.Tools.TransformationTools
namespace Sledge.Editor.Tools.SelectTool.TransformationTools
{
/// <summary>
/// Allows the selected objects to be rotated
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Sledge.Editor.Tools.Widgets;
using Sledge.UI;

namespace Sledge.Editor.Tools.TransformationTools
namespace Sledge.Editor.Tools.SelectTool.TransformationTools
{
/// <summary>
/// Allows the selected objects to be skewed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Sledge.Editor.Tools.Widgets;
using Sledge.UI;

namespace Sledge.Editor.Tools.TransformationTools
namespace Sledge.Editor.Tools.SelectTool.TransformationTools
{
public abstract class TransformationTool
{
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
using System.Windows.Forms;
using Sledge.Common.Mediator;
using Sledge.DataStructures.MapObjects;
using Sledge.Providers.Texture;
using Sledge.Editor.UI;
using Sledge.Providers.Texture;
using Sledge.Settings;

namespace Sledge.Editor.Tools
namespace Sledge.Editor.Tools.TextureTool
{
public partial class TextureApplicationForm : HotkeyForm
{
Expand Down Expand Up @@ -539,5 +539,10 @@ private void FocusTextInControl(object sender, EventArgs e)
var nud = sender as NumericUpDown;
if (nud != null) nud.Select(0, nud.Text.Length);
}

public bool ShouldTreatAsOne()
{
return TreatAsOneCheckbox.Checked;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using OpenTK.Graphics.OpenGL;
using Sledge.Common.Mediator;
using Sledge.DataStructures.Geometric;
using Sledge.DataStructures.MapObjects;
using Sledge.Editor.Actions;
using Sledge.Editor.Actions.MapObjects.Operations;
using Sledge.Editor.Actions.MapObjects.Selection;
using Sledge.Editor.Documents;
using Sledge.Editor.History;
using Sledge.Editor.Properties;
using Sledge.Graphics.Helpers;
using Sledge.Providers.Texture;
using Sledge.Settings;
using Sledge.UI;
using Sledge.Editor.Properties;
using Sledge.Graphics.Helpers;
using OpenTK.Graphics.OpenGL;
using Sledge.DataStructures.MapObjects;
using Sledge.DataStructures.Geometric;

namespace Sledge.Editor.Tools
namespace Sledge.Editor.Tools.TextureTool
{
public class TextureTool : BaseTool
{
Expand Down Expand Up @@ -54,6 +54,7 @@ public enum AlignMode
#endregion

private readonly TextureApplicationForm _form;
private readonly TextureToolSidebarPanel _sidebarPanel;

public TextureTool()
{
Expand All @@ -64,6 +65,11 @@ public TextureTool()
_form.TextureApply += TextureApplied;
_form.TextureJustify += TextureJustified;
_form.HideMaskToggled += HideMaskToggled;

_sidebarPanel = new TextureToolSidebarPanel();
_sidebarPanel.TileFit += TileFit;
_sidebarPanel.RandomiseXShiftValues += RandomiseXShiftValues;
_sidebarPanel.RandomiseYShiftValues += RandomiseYShiftValues;
}

public override void DocumentChanged()
Expand All @@ -77,7 +83,43 @@ private void HideMaskToggled(object sender, bool hide)
Mediator.Publish(HotkeysMediator.ToggleHideFaceMask);
}

private void RandomiseXShiftValues(object sender, int min, int max)
{
if (Document.Selection.IsEmpty()) return;

var rand = new Random();
Action<Document, Face> action = (d, f) =>
{
f.Texture.XShift = rand.Next(min, max + 1); // Upper bound is exclusive
f.CalculateTextureCoordinates(true);
};
Document.PerformAction("Randomise X shift values", new EditFace(Document.Selection.GetSelectedFaces(), action, false));
}

private void RandomiseYShiftValues(object sender, int min, int max)
{
if (Document.Selection.IsEmpty()) return;

var rand = new Random();
Action<Document, Face> action = (d, f) =>
{
f.Texture.YShift = rand.Next(min, max + 1); // Upper bound is exclusive
f.CalculateTextureCoordinates(true);
};
Document.PerformAction("Randomise Y shift values", new EditFace(Document.Selection.GetSelectedFaces(), action, false));
}

private void TextureJustified(object sender, JustifyMode justifymode, bool treatasone)
{
TextureJustified(justifymode, treatasone, 1, 1);
}

private void TileFit(object sender, int tileX, int tileY)
{
TextureJustified(JustifyMode.Fit, _form.ShouldTreatAsOne(), tileX, tileY);
}

private void TextureJustified(JustifyMode justifymode, bool treatasone, int tileX, int tileY)
{
if (Document.Selection.IsEmpty()) return;
var boxAlignMode = (justifymode == JustifyMode.Fit)
Expand All @@ -93,7 +135,7 @@ private void TextureJustified(object sender, JustifyMode justifymode, bool treat

if (justifymode == JustifyMode.Fit)
{
action = (d, x) => x.FitTextureToPointCloud(cloud ?? new Cloud(x.Vertices.Select(y => y.Location)));
action = (d, x) => x.FitTextureToPointCloud(cloud ?? new Cloud(x.Vertices.Select(y => y.Location)), tileX, tileY);
}
else
{
Expand Down Expand Up @@ -169,6 +211,11 @@ public override string GetContextualHelp()
"*Shift+Click* to select all faces of a solid";
}

public override IEnumerable<KeyValuePair<string, Control>> GetSidebarControls()
{
yield return new KeyValuePair<string, Control>("Texture Power Tools", _sidebarPanel);
}

public override void ToolSelected(bool preventHistory)
{
_form.Show(Editor.Instance);
Expand Down
Loading

0 comments on commit 4c56285

Please sign in to comment.