Skip to content

Commit

Permalink
Header editing added, fix for #2 - @zidle object
Browse files Browse the repository at this point in the history
  • Loading branch information
hostar committed Aug 21, 2022
1 parent ca41f74 commit f817c50
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 37 deletions.
123 changes: 112 additions & 11 deletions MafiaSceneEditor/Helpers/ActiveDncs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,10 @@ private static PanelKind DncToPanelKind(Dnc dnc)
}
case NodeType.InitScript:
return PanelKind.Script;
case NodeType.Header:
return PanelKind.Header;
case NodeType.Unknown:
return PanelKind.Hex;
default:
return PanelKind.Hex;
}
Expand Down Expand Up @@ -291,7 +295,103 @@ private static KryptonPage CreatePage(Dnc dnc, PanelKind panelKind)
page = CreatePageOnly(pageName, pageId);
pageId.KryptonPage = page;
return CreatePageInternal(page, pageId, kryptonPageContainer, tableLayoutPanel, kryptonPanel);
case PanelKind.Header:
HeaderProps headerProps = dnc.DncProps as HeaderProps;

row = 0;
col = 0;
CreateLabel(kryptonPageContainer, col, row, 1, "View distance");

col++;

CreateTextBox(kryptonPageContainer, col, row, headerProps.ViewDistance.ToString(),
(o, control) =>
{
headerProps.ViewDistance = CheckFloatAndSet(o, headerProps.ViewDistance, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) => {
control.Text = (prop as HeaderProps).ViewDistance.ToString();
}, colSpan: 1);

row++;
col = 0;

CreateLabel(kryptonPageContainer, col, row, 1, "Camera distance");

col++;

CreateTextBox(kryptonPageContainer, col, row, headerProps.CameraDistance.ToString(),
(o, control) =>
{
headerProps.CameraDistance = CheckFloatAndSet(o, headerProps.CameraDistance, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) => {
control.Text = (prop as HeaderProps).ViewDistance.ToString();
}, colSpan: 1);

row++;
col = 0;

CreateLabel(kryptonPageContainer, col, row, 1, "Near clipping");

col++;

CreateTextBox(kryptonPageContainer, col, row, headerProps.NearClipping.ToString(),
(o, control) =>
{
headerProps.NearClipping = CheckFloatAndSet(o, headerProps.NearClipping, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) => {
control.Text = (prop as HeaderProps).ViewDistance.ToString();
}, colSpan: 1);

row++;
col = 0;

CreateLabel(kryptonPageContainer, col, row, 1, "Far clipping");

col++;

CreateTextBox(kryptonPageContainer, col, row, headerProps.FarClipping.ToString(),
(o, control) =>
{
headerProps.FarClipping = CheckFloatAndSet(o, headerProps.FarClipping, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) => {
control.Text = (prop as HeaderProps).ViewDistance.ToString();
}, colSpan: 1);

KryptonPanel kryptonPanelHeader = new KryptonPanel { Top = 0, Left = 0, Size = new Size(1000, 200) };

row++;
col = 0;
CreateLabel(kryptonPageContainer, col, row, 1, "Description and signature");

col++;
CreateTextBox(kryptonPageContainer, col, row, headerProps.Text,
(o, control) => { headerProps.Text = o.ToString(); },
(prop, control) => { control.Text = (prop as HeaderProps).Text; }, 1, 1000);

tableLayoutPanel = new TableLayoutPanel
{
BackColor = Color.FromArgb(187, 206, 230),
ColumnCount = 2,
RowCount = 2
};
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 30));
//tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.Absolute, 400));

tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 200));
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 1000));

page = CreatePageOnly(pageName, pageId);
pageId.KryptonPage = page;
return CreatePageInternal(page, pageId, kryptonPageContainer, tableLayoutPanel, kryptonPanelHeader);
default:
throw new InvalidOperationException(nameof(CreatePage));
}
Expand Down Expand Up @@ -355,6 +455,7 @@ static void CreateCheckBox(List<KryptonPageContainer> kryptonPageContainer, int
});
}

// Marks tab as modified
static void AddAsterisk(Control control)
{
if (!control.Text.EndsWith('*'))
Expand Down Expand Up @@ -390,7 +491,7 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
CreateTextBox(kryptonPageContainer, col, row, standardProps.PositionX.ToString(),
(o, control) =>
{
standardProps.PositionX = CheckAndSet(o, standardProps, standardProps.PositionX, pageId);
standardProps.PositionX = CheckFloatAndSet(o, standardProps.PositionX, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) => {
Expand All @@ -402,7 +503,7 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
CreateTextBox(kryptonPageContainer, col, row, standardProps.PositionY.ToString(),
(o, control) =>
{
standardProps.PositionY = CheckAndSet(o, standardProps, standardProps.PositionY, pageId);
standardProps.PositionY = CheckFloatAndSet(o, standardProps.PositionY, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) =>
Expand All @@ -415,7 +516,7 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
CreateTextBox(kryptonPageContainer, col, row, standardProps.PositionZ.ToString(),
(o, control) =>
{
standardProps.PositionZ = CheckAndSet(o, standardProps, standardProps.PositionZ, pageId);
standardProps.PositionZ = CheckFloatAndSet(o, standardProps.PositionZ, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) =>
Expand Down Expand Up @@ -444,7 +545,7 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
CreateTextBox(kryptonPageContainer, col, row, standardProps.RotationX.ToString(),
(o, control) =>
{
standardProps.RotationX = CheckAndSet(o, standardProps, standardProps.RotationX, pageId);
standardProps.RotationX = CheckFloatAndSet(o, standardProps.RotationX, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) =>
Expand All @@ -456,7 +557,7 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
CreateTextBox(kryptonPageContainer, col, row, standardProps.RotationY.ToString(),
(o, control) =>
{
standardProps.RotationY = CheckAndSet(o, standardProps, standardProps.RotationY, pageId);
standardProps.RotationY = CheckFloatAndSet(o, standardProps.RotationY, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) =>
Expand All @@ -468,7 +569,7 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
CreateTextBox(kryptonPageContainer, col, row, standardProps.RotationZ.ToString(),
(o, control) =>
{
standardProps.RotationZ = CheckAndSet(o, standardProps, standardProps.RotationZ, pageId);
standardProps.RotationZ = CheckFloatAndSet(o, standardProps.RotationZ, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) =>
Expand Down Expand Up @@ -497,7 +598,7 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
CreateTextBox(kryptonPageContainer, col, row, standardProps.ScalingX.ToString(),
(o, control) =>
{
standardProps.ScalingX = CheckAndSet(o, standardProps, standardProps.ScalingX, pageId);
standardProps.ScalingX = CheckFloatAndSet(o, standardProps.ScalingX, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) =>
Expand All @@ -509,7 +610,7 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
CreateTextBox(kryptonPageContainer, col, row, standardProps.ScalingY.ToString(),
(o, control) =>
{
standardProps.ScalingY = CheckAndSet(o, standardProps, standardProps.ScalingY, pageId);
standardProps.ScalingY = CheckFloatAndSet(o, standardProps.ScalingY, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) =>
Expand All @@ -521,7 +622,7 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
CreateTextBox(kryptonPageContainer, col, row, standardProps.ScalingZ.ToString(),
(o, control) =>
{
standardProps.ScalingZ = CheckAndSet(o, standardProps, standardProps.ScalingZ, pageId);
standardProps.ScalingZ = CheckFloatAndSet(o, standardProps.ScalingZ, pageId);
AddAsterisk(pageId.KryptonPage);
},
(prop, control) =>
Expand All @@ -531,10 +632,10 @@ static void CreateDefaultTextBoxes(List<KryptonPageContainer> kryptonPageContain
}
}

private static float CheckAndSet(object o, StandardProps standardProps, float originalValue, KryptonPageId pageId)
private static float CheckFloatAndSet(object obj, float originalValue, KryptonPageId pageId)
{
float parsed;
if (float.TryParse(o.ToString(), out parsed))
if (float.TryParse(obj.ToString(), out parsed))
{
pageId.IsDirty = true;
return Convert.ToSingle(parsed);
Expand Down
1 change: 1 addition & 0 deletions MafiaSceneEditor/Helpers/DncMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ public static void BtnSaveClick(object sender, EventArgs eventArgs)
break;
case PanelKind.Standard:
case PanelKind.Model:
case PanelKind.Header:
pageId.Dnc.DncProps.SaveData();
break;
default:
Expand Down
1 change: 1 addition & 0 deletions MafiaSceneEditor/Helpers/KryptonPageId.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public enum PanelKind
Enemy,
Standard,
Model,
Header,
Hex
}

Expand Down
33 changes: 22 additions & 11 deletions MafiaSceneEditor/MainForm2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1085,6 +1085,10 @@ private void PutIntoTreeview(Scene2Data scene2Data)

int i = 0;

// add header
TreeNode objectsTreeNodeHeader = new TreeNode("Header") { Tag = scene2Data.Header.Content };
treeViewMain.Nodes.Add(objectsTreeNodeHeader);

foreach (Scene2Section section in scene2Data.Sections)
{
TreeNode objectsTreeNode = new TreeNode(section.SectionName);
Expand All @@ -1102,17 +1106,7 @@ private void PutIntoTreeview(Scene2Data scene2Data)
i = 0;

List<TreeNode> nodeList = new List<TreeNode>();
foreach (Dnc dnc in item)
{
TreeNode treeNode = new TreeNode
{
Text = dnc.Name,
Tag = dnc
};

nodeList.Add(treeNode);
i++;
}
i = AddAllItems(i, item, nodeList);

// sort nodes
nodeList = nodeList.OrderBy(x => x.Text).ToList();
Expand All @@ -1127,6 +1121,23 @@ private void PutIntoTreeview(Scene2Data scene2Data)
}

logging.Add("Loading of file done.");

static int AddAllItems(int i, IGrouping<DncType, Dnc> item, List<TreeNode> nodeList)
{
foreach (Dnc dnc in item)
{
TreeNode treeNode = new TreeNode
{
Text = dnc.Name,
Tag = dnc
};

nodeList.Add(treeNode);
i++;
}

return i;
}
}

private void Scene2FileSave(object sender, EventArgs e)
Expand Down
3 changes: 2 additions & 1 deletion Scene2Parser/DataLayer/DncType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum DncType
Clock,
Wagon,
Route,
GhostObject
GhostObject,
Zidle
}
}
2 changes: 1 addition & 1 deletion Scene2Parser/DataLayer/Header.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ public class Header
{
public List<byte> Magic { get; set; } = new List<byte>(); // 2 bytes
public List<byte> Size { get; set; } = new List<byte>(); // 4 bytes
public List<byte> Content { get; set; } = new List<byte>();
public Dnc Content { get; set; } = new Dnc() { dncKind = NodeType.Header };
}
}
1 change: 1 addition & 0 deletions Scene2Parser/DataLayer/NodeType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ public enum NodeType
Object,
Definition,
InitScript,
Header,
Unknown
}
}
74 changes: 74 additions & 0 deletions Scene2Parser/DataLayer/Props/HeaderProps.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using YAMSE.Interfaces;
using static System.Net.Mime.MediaTypeNames;

namespace YAMSE.DataLayer
{
public class HeaderProps : IDncProps
{
public int DataBegin { get; set; }

public float ViewDistance { get; set; }
public float CameraDistance { get; set; }
public float NearClipping { get; set; }
public float FarClipping { get; set; }
public int HeaderLength { get; set; }
public string Text { get; set; }
public string TextPrevious { get; set; }

public byte[] RawData { get; set; }

private readonly Dnc _dnc;

public HeaderProps(Dnc dnc)
{
_dnc = dnc;
DataBeginLocator();

RevertData();
}

public int DataBeginLocator()
{
DataBegin = 0;
return DataBegin;
}

public void RevertData()
{
HeaderLength = _dnc.RawData.Skip(DataBegin + 2).Take(1).First();
Text = Scene2Parser.GetStringFromDnc(_dnc, DataBegin, 10);
TextPrevious = Text;
ViewDistance = BitConverter.ToSingle(_dnc.RawData.Skip(DataBegin + Text.Length + 60).Take(4).ToArray(), 0);
CameraDistance = BitConverter.ToSingle(_dnc.RawData.Skip(DataBegin + Text.Length + 50).Take(4).ToArray(), 0);
NearClipping = BitConverter.ToSingle(_dnc.RawData.Skip(DataBegin + Text.Length + 70).Take(4).ToArray(), 0);
FarClipping = BitConverter.ToSingle(_dnc.RawData.Skip(DataBegin + Text.Length + 74).Take(4).ToArray(), 0);
}

public virtual void SaveData()
{
//Scene2Parser.UpdateStringInDnc(_dnc, Text, 10);
// update text
var startOfArray = _dnc.RawDataBackup.Take(10).ToArray();
var endOfArray = _dnc.RawDataBackup.Skip(TextPrevious.Length + 10).ToArray();
var textInBytes = Encoding.UTF8.GetBytes(Text);

HeaderLength = Text.Length + 12;
TextPrevious = Text;

// recalculate array length
_dnc.RawData = startOfArray.Concat(textInBytes).Concat(endOfArray).ToArray();

Scene2Parser.WriteToDnc(_dnc, DataBegin, HeaderLength, 2, false);
Scene2Parser.WriteToDnc(_dnc, DataBegin, ViewDistance, Text.Length + 60);
Scene2Parser.WriteToDnc(_dnc, DataBegin, CameraDistance, Text.Length + 50);
Scene2Parser.WriteToDnc(_dnc, DataBegin, NearClipping, Text.Length + 70);
Scene2Parser.WriteToDnc(_dnc, DataBegin, FarClipping, Text.Length + 74);

}
}
}
Loading

0 comments on commit f817c50

Please sign in to comment.