Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[demo-only] implement node flipping #8

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Foreman/Models/Nodes/BaseNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public abstract partial class BaseNode : ISerializable
private RateType rateType;
public RateType RateType { get { return rateType; } set { if (rateType != value) { rateType = value; UpdateState(); } } }

public bool IsFlipped { get; set; }

public double ActualRatePerSec { get; private set; }

private double desiredRatePerSec;
Expand Down Expand Up @@ -122,6 +124,8 @@ public abstract class ReadOnlyBaseNode
public double DesiredRate => MyNode.DesiredRate;
public NodeState State => MyNode.State;

public bool IsFlipped => MyNode.IsFlipped;

public abstract List<string> GetErrors();
public abstract List<string> GetWarnings();

Expand Down
1 change: 1 addition & 0 deletions Foreman/Models/Nodes/ConsumerNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
info.AddValue("RateType", RateType);
if (RateType == RateType.Manual)
info.AddValue("DesiredRate", DesiredRatePerSec);
info.AddValue("IsFlipped", IsFlipped);
}

public override string ToString() { return string.Format("Consumption node for: {0}", ConsumedItem.Name); }
Expand Down
1 change: 1 addition & 0 deletions Foreman/Models/Nodes/PassthroughNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
info.AddValue("RateType", RateType);
if (RateType == RateType.Manual)
info.AddValue("DesiredRate", DesiredRatePerSec);
info.AddValue("IsFlipped", IsFlipped);
}

public override string ToString() { return string.Format("Passthrough node for: {0}", PassthroughItem.Name); }
Expand Down
15 changes: 8 additions & 7 deletions Foreman/Models/Nodes/RecipeNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
info.AddValue("BeaconsPerAssembler", BeaconsPerAssembler);
info.AddValue("BeaconsConst", BeaconsConst);
}
info.AddValue("IsFlipped", IsFlipped);
}

public override string ToString() { return string.Format("Recipe node for: {0}", BaseRecipe.Name); }
Expand Down Expand Up @@ -720,12 +721,12 @@ public override Dictionary<string, Action> GetWarningResolutions()
resolutions.Add("Turn off beacon", new Action(() => SetBeacon(null)));

if ((WarningSet & (RecipeNode.Warnings.BModuleIsDisabled | RecipeNode.Warnings.BModuleIsUnavailable)) != 0)
resolutions.Add("Remove error modules from beacon", new Action(() =>
{
for (int i = MyNode.BeaconModules.Count - 1; i >= 0; i--)
if (!MyNode.BeaconModules[i].Enabled || !MyNode.BeaconModules[i].Available)
RemoveBeaconModule(i);
}));
resolutions.Add("Remove error modules from beacon", new Action(() =>
{
for (int i = MyNode.BeaconModules.Count - 1; i >= 0; i--)
if (!MyNode.BeaconModules[i].Enabled || !MyNode.BeaconModules[i].Available)
RemoveBeaconModule(i);
}));

if ((WarningSet & RecipeNode.Warnings.TemeratureFluidBurnerInvalidLinks) != 0)
resolutions.Add("Remove fuel links", new Action(() =>
Expand Down Expand Up @@ -854,7 +855,7 @@ public void SetAssemblerModules(IEnumerable<Module> modules, bool filterModules)
{
MyNode.AssemblerModules.Clear();
if (modules != null)
{
{
if(filterModules)
{
HashSet<Module> acceptableModules = new HashSet<Module>(MyNode.BaseRecipe.Modules.Intersect(MyNode.SelectedAssembler.Modules));
Expand Down
1 change: 1 addition & 0 deletions Foreman/Models/Nodes/SupplierNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public override void GetObjectData(SerializationInfo info, StreamingContext cont
info.AddValue("RateType", RateType);
if (RateType == RateType.Manual)
info.AddValue("DesiredRate", DesiredRatePerSec);
info.AddValue("IsFlipped", IsFlipped);
}

public override string ToString() { return string.Format("Supply node for: {0}", SuppliedItem.Name); }
Expand Down
10 changes: 10 additions & 0 deletions Foreman/Models/ProductionGraph.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,12 @@ public void DeleteNode(ReadOnlyBaseNode node)
NodeDeleted?.Invoke(this, new NodeEventArgs(node));
}

public void FlipNode(ReadOnlyBaseNode node)
{
BaseNode realNode = roToNode[node];
realNode.IsFlipped = !realNode.IsFlipped;
}

public void DeleteNodes(IEnumerable<ReadOnlyBaseNode> nodes)
{
foreach (ReadOnlyBaseNode node in nodes)
Expand Down Expand Up @@ -521,6 +527,10 @@ public NewNodeCollection InsertNodesFromJson(DataCache cache, JToken json) //cac
newNode.DesiredRatePerSec = (double)nodeJToken["DesiredRate"];
}



newNode.IsFlipped = (bool)(nodeJToken["IsFlipped"] ?? false);

oldNodeIndices.Add((int)nodeJToken["NodeID"], newNode.ReadOnlyNode);
}

Expand Down
16 changes: 14 additions & 2 deletions Foreman/ProductionGraphView/Elements/BaseLinkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public override void UpdateVisibility(Rectangle graph_zone, int xborder, int ybo
}

Visible =
CalculatedBounds.X + CalculatedBounds.Width > graph_zone.X - xborder &&
CalculatedBounds.X + CalculatedBounds.Width > graph_zone.X - xborder &&
CalculatedBounds.X < graph_zone.X + graph_zone.Width + xborder &&
CalculatedBounds.Y + CalculatedBounds.Height > graph_zone.Y - yborder &&
CalculatedBounds.Y < graph_zone.Y + graph_zone.Height + yborder;
Expand Down Expand Up @@ -107,8 +107,20 @@ protected override void Draw(Graphics graphics, bool simple)
{
UpdateCurve();

using (Pen pen = new Pen(Item.AverageColor, LinkWidth) { EndCap = System.Drawing.Drawing2D.LineCap.Round, StartCap = System.Drawing.Drawing2D.LineCap.Round })
using (Pen pen = new Pen(Item.AverageColor, LinkWidth))
{

if (SupplierElement?.DisplayedNode.IsFlipped == true || ConsumerElement?.DisplayedNode.IsFlipped == true)
{
pen.CustomStartCap = new System.Drawing.Drawing2D.AdjustableArrowCap(LinkWidth, LinkWidth);
pen.EndCap = System.Drawing.Drawing2D.LineCap.Round;
}
else
{
pen.StartCap = System.Drawing.Drawing2D.LineCap.Round;
pen.CustomEndCap = new System.Drawing.Drawing2D.AdjustableArrowCap(LinkWidth, LinkWidth);
}

if (linkingUp)
graphics.DrawBeziers(pen, new Point[] {
pointN,
Expand Down
17 changes: 15 additions & 2 deletions Foreman/ProductionGraphView/Elements/BaseNodeElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,18 +118,23 @@ private void UpdateTabOrder()
InputTabs = InputTabs.OrderBy(it => GetItemTabXHeuristic(it)).ThenBy(it => it.Item.Name).ToList(); //then by ensures same result no matter who came first
OutputTabs = OutputTabs.OrderBy(it => GetItemTabXHeuristic(it)).ThenBy(it => it.Item.Name).ToList();

int topRow = (-Height / 2) + 1;
int bottomRow = (Height / 2) - 1;
int outputRow = DisplayedNode.IsFlipped ? bottomRow : topRow;
int inputRow = DisplayedNode.IsFlipped ? topRow : bottomRow;

int x = -GetIconWidths(OutputTabs) / 2;
foreach (ItemTabElement tab in OutputTabs)
{
x += TabPadding;
tab.Location = new Point(x + (tab.Width / 2), (-Height / 2) + 1);
tab.Location = new Point(x + (tab.Width / 2), outputRow);
x += tab.Width;
}
x = -GetIconWidths(InputTabs) / 2;
foreach (ItemTabElement tab in InputTabs)
{
x += TabPadding;
tab.Location = new Point(x + (tab.Width / 2), (Height / 2) - 1);
tab.Location = new Point(x + (tab.Width / 2), inputRow);
x += tab.Width;
}
}
Expand Down Expand Up @@ -269,6 +274,14 @@ protected virtual void MouseUpAction(Point graph_point, MouseButtons button)
graphViewer.Graph.DeleteNode(DisplayedNode);
graphViewer.Graph.UpdateNodeValues();
})));
RightClickMenu.Items.Add(new ToolStripMenuItem("Flip node", null,
new EventHandler((o, e) =>
{
RightClickMenu.Close();
graphViewer.Graph.FlipNode(DisplayedNode);
graphViewer.Graph.UpdateNodeValues();

})));
if (graphViewer.SelectedNodes.Count > 1 && graphViewer.SelectedNodes.Contains(this))
{
RightClickMenu.Items.Add(new ToolStripMenuItem("Delete selected nodes", null,
Expand Down
7 changes: 5 additions & 2 deletions Foreman/ProductionGraphView/Elements/ItemTabElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,13 @@ public ItemTabElement(Item item, LinkType type, ProductionGraphViewer graphViewe

public Point GetConnectionPoint() //in graph coordinates
{
int topRow = (-Height / 2);
int bottomRow = (Height / 2);

if (LinkType == LinkType.Input)
return LocalToGraph(new Point(0, Height / 2));
return LocalToGraph(new Point(0, DisplayedNode.IsFlipped ? topRow : bottomRow));
else //if(LinkType == LinkType.Output)
return LocalToGraph(new Point(0, -Height / 2));
return LocalToGraph(new Point(0, DisplayedNode.IsFlipped ? bottomRow : topRow));
}

public void UpdateValues(double recipeRate, double suppliedRate, bool isOversupplied) //if input then: recipe rate = consume rate; if output then recipe rate = production rate
Expand Down
10 changes: 9 additions & 1 deletion Foreman/ProductionGraphView/Elements/LinkElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ protected override Point[] GetCurveEndpoints()
{
Point pointMupdate = ConsumerTab.GetConnectionPoint();
Point pointNupdate = SupplierTab.GetConnectionPoint();
return new Point[] { pointMupdate, pointNupdate };

if (SupplierElement.DisplayedNode.IsFlipped || ConsumerElement.DisplayedNode.IsFlipped)
{
return new Point[] { pointNupdate, pointMupdate };
}
else
{
return new Point[] { pointMupdate, pointNupdate };
}
}
}
}
5 changes: 5 additions & 0 deletions Foreman/ProductionGraphView/ProductionGraphViewer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,11 @@ public void AddRecipe(Point drawOrigin, Item baseItem, Point newLocation, NewNod
break;
}

if (originElement != null && originElement.DisplayedNode.IsFlipped != newNode.IsFlipped)
{
Graph.FlipNode(newNode);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maintaining the direction of the original node.

}

//this is the offset to take into account multiple recipe additions (holding shift while selecting recipe). First node isnt shifted, all subsequent ones are 'attempted' to be spaced.
//should be updated once the node graphics are updated (so that the node size doesnt depend as much on the text)
BaseNodeElement newNodeElement = NodeElementDictionary[newNode];
Expand Down