Skip to content

Commit

Permalink
Just testing something
Browse files Browse the repository at this point in the history
  • Loading branch information
hlkuss committed Oct 24, 2022
1 parent ea6ceb3 commit 0a5e586
Show file tree
Hide file tree
Showing 6 changed files with 11,159 additions and 1,712 deletions.
115 changes: 105 additions & 10 deletions Assets/SEE/Controls/Actions/MarkAction.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Dissonance;
using SEE.Game;
using SEE.GO;
Expand All @@ -9,7 +10,6 @@

namespace SEE.Controls.Actions
{

internal class NodeNotFoundException : Exception
{

Expand All @@ -18,30 +18,119 @@ internal class NodeNotFoundException : Exception
///
/// <summary>
/// Author: Hannes Kuss
///
/// An action for selecting nodes in a code-city.
///
/// The selected node are marked with a unity-primitive node.
/// The marked nodes can also be deselected with another click.
///
/// An action for selecting nodes in a code-city.
///
/// The Action should also keep full track of the users interactions.
/// So all actions can be undone/redone
/// </summary>
public class MarkAction : AbstractPlayerAction
{
public override HashSet<string> GetChangedObjects()
{
throw new System.NotImplementedException();
}
public override HashSet<string> GetChangedObjects() =>
new HashSet<string>(markedNodes.Select(x => x.Item1.ID()).ToList());

// Tupel (node, markerSphere)
// Internal visibility because GameNodeMarker also uses it.
// This suffix is appended to all node markers GameObject names
internal static string MARKER_NAME_SUFFIX = "-MARKED";

// Tuple for marked nodes (node, markerSphere)
private List<(GameObject, GameObject)> markedNodes = new List<(GameObject, GameObject)>();

// A stack with a tupel (node, marked) for keeping track of the last actions/interactions with nodes.
// marked is true, when a node was marked in that action and false if unmarked
private Stack<(GameObject, bool)> undoMarkers = new Stack<(GameObject, bool)>();

// A stack with a tupel (node, marked)
// marked is true, when a node was marked in that action and false if unmarked
private Stack<(GameObject, bool)> redoMarkers = new Stack<(GameObject, bool)>();

// Is set, when the undo/redo stack should be cleaned up next time
private bool doCleanUpUndoNextTime;

private (GameObject, bool) lastA;

public static MarkAction CreateMarkAction() => new MarkAction();

public static ReversibleAction CreateReversibleAction()
{
return new MarkAction();
}

public override void Undo()
{
base.Undo();
//base.Undo();
doCleanUpUndoNextTime = true;
var lastAction = undoMarkers.Pop();

// When the last action was, to mark a node, then the node should be unmarked
if (lastAction.Item2)
{
GameObject node = lastAction.Item1;
GameObject marker = GetMarkerOfNode(node) ?? throw new ArgumentNullException("GetMarkerOfNode(node)");
// Destroy marker
Destroyer.DestroyGameObject(marker);

// probably redundant but just to make sure.
// TODO: Remove this later
if (IsNodeMarked(node))
{
RemoveNodeFromMarked(node);
// Add the node to the redo list as removed
redoMarkers.Push((node, false));
}
}
// When the last action was, to unmark a node, then the node should be marked again
else
{
GameObject node = lastAction.Item1;
string sphereTag = node.tag += MARKER_NAME_SUFFIX;
GameObject marker = GameNodeMarker.CreateMarker(node);
marker.name = sphereTag;
markedNodes.Add((node, marker));
// Add the node to the redo list as added
redoMarkers.Push((node, true));
}
}

public override void Redo()
{
base.Redo();
var lastAction = undoMarkers.Pop();

// Properly also redundant but also just to make sure.
doCleanUpUndoNextTime = true;

// When the last action was, to mark a node, then the node should be unmarked
if (lastAction.Item2)
{
GameObject node = lastAction.Item1;
GameObject marker = GetMarkerOfNode(node) ?? throw new ArgumentNullException("GetMarkerOfNode(node)");
// Destroy marker
Destroyer.DestroyGameObject(marker);

// probably redundant but just to make sure.
// TODO: Remove this later
if (IsNodeMarked(node))
{
RemoveNodeFromMarked(node);
// Add the node to the redo list as removed
undoMarkers.Push((node, false));
}
}
// When the last action was, to unmark a node, then the node should be marked again
else
{
GameObject node = lastAction.Item1;
string sphereTag = node.name += MARKER_NAME_SUFFIX;
GameObject marker = GameNodeMarker.CreateMarker(node);
marker.name = sphereTag;
markedNodes.Add((node, marker));
// Add the node to the redo list as added
undoMarkers.Push((node, true));
}
}

public override ActionStateType GetActionStateType()
Expand Down Expand Up @@ -109,15 +198,21 @@ public override bool Update()

if (!IsNodeMarked(cnode))
{
currentState = ReversibleAction.Progress.Completed;
// Extract the code city node.
string sphereTag = cnode.tag += "-MARKED";
GameObject marker = GameNodeMarker.CreateMarker(cnode);
marker.name = sphereTag;
markedNodes.Add((cnode, marker));
lastA = (cnode, true);
this.undoMarkers.Push((cnode, true));
}
}
else
{
GameObject marker = GetMarkerOfNode(cnode) ?? throw new ArgumentNullException("GetMarkerOfNode(cnode)");
currentState = ReversibleAction.Progress.Completed;
GameObject marker = GetMarkerOfNode(cnode) ??
throw new ArgumentNullException("GetMarkerOfNode(cnode)");
Destroyer.DestroyGameObject(marker);
RemoveNodeFromMarked(cnode);
}
Expand Down
24 changes: 24 additions & 0 deletions Assets/SEE/Game/GameNodeMarker.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using SEE.Controls.Actions;
using SEE.Utils;
using UnityEngine;

namespace SEE.Game
Expand Down Expand Up @@ -26,5 +28,27 @@ public static GameObject CreateMarker(GameObject node, float yOffset = 1.5F)
newSphere.transform.localScale = Vector3.one;
return newSphere;
}

/// <summary>
/// Deletes a marker sphere if it's exists
/// </summary>
/// <param name="node">The node which marker should be deleted</param>
/// <returns>Returns true if the passed node has a marker sphere. Otherwise false</returns>
public static bool RemoveMarker(GameObject node)
{
var nodeTran = node.transform;
for (int i = 0; i < nodeTran.childCount; i++)
{
GameObject markerCanidate = nodeTran.GetChild(i).gameObject;
// If the child is a marker sphere
if (markerCanidate.tag.EndsWith(MarkAction.MARKER_NAME_SUFFIX))
{
Destroyer.DestroyGameObject(markerCanidate);
return true;
}
}

return false;
}
}
}
Loading

0 comments on commit 0a5e586

Please sign in to comment.