Moved nodes over to using handles rather than offsets in memory

This commit is contained in:
Peter Slattery 2019-11-02 15:17:29 -07:00
parent f3270918d0
commit 741529a664
3 changed files with 117 additions and 128 deletions

View File

@ -413,18 +413,17 @@ BeginDraggingNode(app_state* State, node_interaction Interaction)
struct node_view_operation_state struct node_view_operation_state
{ {
node_offset SelectedNodeOffset; s32 SelectedNodeHandle;
}; };
FOLDHAUS_INPUT_COMMAND_PROC(NodeViewBeginMouseDragInteraction) FOLDHAUS_INPUT_COMMAND_PROC(NodeViewBeginMouseDragInteraction)
{ {
node_view_operation_state* OpState = GetCurrentOperationState(State->Modes, node_view_operation_state); node_view_operation_state* OpState = GetCurrentOperationState(State->Modes, node_view_operation_state);
node_offset NodeOffset = GetNodeUnderPoint(State->NodeList, Mouse.DownPos, State->NodeRenderSettings); interface_node* Node = GetNodeUnderPoint(State->NodeList, Mouse.DownPos, State->NodeRenderSettings);
if (NodeOffset.Node) if (Node)
{ {
node_interaction NewInteraction = GetNodeInteractionType(NodeOffset.Node, node_interaction NewInteraction = GetNodeInteractionType(Node,
NodeOffset.Offset,
Mouse.Pos, Mouse.Pos,
State->NodeRenderSettings); State->NodeRenderSettings);
if (IsDraggingNodePort(NewInteraction)) if (IsDraggingNodePort(NewInteraction))
@ -440,13 +439,13 @@ FOLDHAUS_INPUT_COMMAND_PROC(NodeViewBeginMouseDragInteraction)
} }
else // IsDraggingNode else // IsDraggingNode
{ {
OpState->SelectedNodeOffset = NodeOffset; OpState->SelectedNodeHandle = Node->Handle;
BeginDraggingNode(State, NewInteraction); BeginDraggingNode(State, NewInteraction);
} }
} }
else else
{ {
OpState->SelectedNodeOffset = InvalidNodeOffset(); OpState->SelectedNodeHandle = 0;
} }
} }
@ -454,16 +453,15 @@ FOLDHAUS_INPUT_COMMAND_PROC(NodeViewBeginMouseSelectInteraction)
{ {
node_view_operation_state* OpState = GetCurrentOperationState(State->Modes, node_view_operation_state); node_view_operation_state* OpState = GetCurrentOperationState(State->Modes, node_view_operation_state);
node_offset NodeOffset = GetNodeUnderPoint(State->NodeList, Mouse.Pos, State->NodeRenderSettings); interface_node* Node = GetNodeUnderPoint(State->NodeList, Mouse.Pos, State->NodeRenderSettings);
if (NodeOffset.Node) if (Node)
{ {
node_interaction NewInteraction = GetNodeInteractionType(NodeOffset.Node, node_interaction NewInteraction = GetNodeInteractionType(Node,
NodeOffset.Offset,
Mouse.Pos, Mouse.Pos,
State->NodeRenderSettings); State->NodeRenderSettings);
if(IsDraggingNodeValue(NewInteraction)) if(IsDraggingNodeValue(NewInteraction))
{ {
node_connection* Connection = NodeOffset.Node->Connections + NewInteraction.InputValue; node_connection* Connection = Node->Connections + NewInteraction.InputValue;
struct_member_type InputType = Connection->Type; struct_member_type InputType = Connection->Type;
if (InputType == MemberType_r32) if (InputType == MemberType_r32)
@ -484,6 +482,10 @@ OPERATION_RENDER_PROC(RenderNodeView)
DEBUG_TRACK_FUNCTION; DEBUG_TRACK_FUNCTION;
MakeStringBuffer(NodeHeaderBuffer, 128);
interface_node* SelectedNode = GetNodeWithHandle(State->NodeList, OpState->SelectedNodeHandle);
node_list_iterator NodeIter = GetNodeListIterator(*State->NodeList); node_list_iterator NodeIter = GetNodeListIterator(*State->NodeList);
while (NodeIteratorIsValid(NodeIter)) while (NodeIteratorIsValid(NodeIter))
{ {
@ -492,14 +494,17 @@ OPERATION_RENDER_PROC(RenderNodeView)
rect NodeBounds = CalculateNodeBounds(Node, State->NodeRenderSettings); rect NodeBounds = CalculateNodeBounds(Node, State->NodeRenderSettings);
b32 DrawFields = PointIsInRect(Mouse.Pos, NodeBounds); b32 DrawFields = PointIsInRect(Mouse.Pos, NodeBounds);
if (Node == OpState->SelectedNodeOffset.Node) if (Node == SelectedNode)
{ {
PushRenderQuad2D(RenderBuffer, NodeBounds.Min - v2{2, 2}, NodeBounds.Max + v2{2, 2}, WhiteV4); PushRenderQuad2D(RenderBuffer, NodeBounds.Min - v2{2, 2}, NodeBounds.Max + v2{2, 2}, WhiteV4);
} }
PushRenderQuad2D(RenderBuffer, NodeBounds.Min, NodeBounds.Max, v4{.5f, .5f, .5f, 1.f}); PushRenderQuad2D(RenderBuffer, NodeBounds.Min, NodeBounds.Max, v4{.5f, .5f, .5f, 1.f});
DrawString(RenderBuffer, Node->Name, State->NodeRenderSettings.Font, // TODO(Peter): This is just for debug purposes. We can remove and go back to just having
// Node->Name in DrawString
PrintF(&NodeHeaderBuffer, "%.*s: %d", Node->Name.Length, Node->Name.Memory, Node->Handle);
DrawString(RenderBuffer, NodeHeaderBuffer, State->NodeRenderSettings.Font,
v2{NodeBounds.Min.x + 5, NodeBounds.Max.y - (State->NodeRenderSettings.Font->PixelHeight + NODE_HEADER_HEIGHT + 5)}, v2{NodeBounds.Min.x + 5, NodeBounds.Max.y - (State->NodeRenderSettings.Font->PixelHeight + NODE_HEADER_HEIGHT + 5)},
WhiteV4); WhiteV4);
@ -578,7 +583,7 @@ OPERATION_RENDER_PROC(RenderNodeView)
FOLDHAUS_INPUT_COMMAND_PROC(NodeViewDeleteNode) FOLDHAUS_INPUT_COMMAND_PROC(NodeViewDeleteNode)
{ {
node_view_operation_state* OpState = GetCurrentOperationState(State->Modes, node_view_operation_state); node_view_operation_state* OpState = GetCurrentOperationState(State->Modes, node_view_operation_state);
if (IsValidOffset(OpState->SelectedNodeOffset)) if (OpState->SelectedNodeHandle > 0)
{ {
} }
@ -589,12 +594,19 @@ FOLDHAUS_INPUT_COMMAND_PROC(CloseNodeView)
DeactivateCurrentOperationMode(&State->Modes); DeactivateCurrentOperationMode(&State->Modes);
} }
FOLDHAUS_INPUT_COMMAND_PROC(DEBUGGetNode)
{
interface_node* Node = GetNodeWithHandle(State->NodeList, 3);
s32 x = 5;
}
input_command NodeViewCommands [] = { input_command NodeViewCommands [] = {
{ KeyCode_Tab, KeyCode_Invalid, Command_Began, CloseNodeView}, { KeyCode_Tab, KeyCode_Invalid, Command_Began, CloseNodeView},
{ KeyCode_A, KeyCode_Invalid, Command_Began, OpenNodeLister}, { KeyCode_A, KeyCode_Invalid, Command_Began, OpenNodeLister},
{ KeyCode_MouseLeftButton, KeyCode_Invalid, Command_Began, NodeViewBeginMouseDragInteraction}, { KeyCode_MouseLeftButton, KeyCode_Invalid, Command_Began, NodeViewBeginMouseDragInteraction},
{ KeyCode_MouseLeftButton, KeyCode_Invalid, Command_Ended, NodeViewBeginMouseSelectInteraction}, { KeyCode_MouseLeftButton, KeyCode_Invalid, Command_Ended, NodeViewBeginMouseSelectInteraction},
{ KeyCode_X, KeyCode_Invalid, Command_Began, NodeViewDeleteNode}, { KeyCode_X, KeyCode_Invalid, Command_Began, NodeViewDeleteNode},
{ KeyCode_B, KeyCode_Invalid, Command_Began, DEBUGGetNode},
}; };
FOLDHAUS_INPUT_COMMAND_PROC(OpenNodeView) FOLDHAUS_INPUT_COMMAND_PROC(OpenNodeView)
@ -605,7 +617,7 @@ FOLDHAUS_INPUT_COMMAND_PROC(OpenNodeView)
node_view_operation_state* OpState = CreateOperationState(NodeViewMode, node_view_operation_state* OpState = CreateOperationState(NodeViewMode,
&State->Modes, &State->Modes,
node_view_operation_state); node_view_operation_state);
OpState->SelectedNodeOffset = InvalidNodeOffset(); OpState->SelectedNodeHandle = 0;
} }
//////////////////////////////////////// ////////////////////////////////////////

View File

@ -1,21 +1,3 @@
inline node_offset
InvalidNodeOffset()
{
node_offset Result = {};
// NOTE(Peter): I'm not sure this is actually invalid. Should be.
// If it is valid, it implies you can have offset from somewhere other than the beginning of
// the array, but where that point is isn't captured here so it seems like that shouldn't be
// correct
Result.Offset = -1;
Result.Node = 0; // NOTE(Peter): Pretty sure this is invalid tho ;)
return Result;
}
#define IsValidOffset(off) ((off.Node != 0) && (off.Offset >= 0))
#define IsInvalidOffset(off) ((off.Node == 0) && (off.Offset < 0))
inline s32 inline s32
GetNodeMemorySize (interface_node Node) GetNodeMemorySize (interface_node Node)
{ {
@ -40,13 +22,6 @@ NodeIteratorIsValid(node_list_iterator Iter)
return Result; return Result;
} }
internal s32
GetCurrentOffset (node_list_iterator Iter)
{
s32 Result = (u8*)Iter.At - Iter.List.Memory;
return Result;
}
internal void internal void
Next (node_list_iterator* Iter) Next (node_list_iterator* Iter)
{ {
@ -74,6 +49,7 @@ AllocateNodeList (memory_arena* Storage, s32 Size)
Result->Max = Size; Result->Max = Size;
Result->Used = 0; Result->Used = 0;
Result->Next = 0; Result->Next = 0;
Result->HandleAccumulator = 0;
return Result; return Result;
} }
@ -93,6 +69,7 @@ PushNodeOnList (node_list* List, s32 NameLength, s32 ConnectionsCount, v2 Min, v
else else
{ {
Result = (interface_node*)(List->Memory + List->Used); Result = (interface_node*)(List->Memory + List->Used);
Result->Handle = ++List->HandleAccumulator;
Result->Name = MakeString((char*)(Result + 1), NameLength); Result->Name = MakeString((char*)(Result + 1), NameLength);
Result->ConnectionsCount = ConnectionsCount; Result->ConnectionsCount = ConnectionsCount;
@ -111,9 +88,9 @@ internal void
InitializeNodeConnection (node_connection* Connection, struct_member_type Type, b32 DirectionMask) InitializeNodeConnection (node_connection* Connection, struct_member_type Type, b32 DirectionMask)
{ {
Connection->Type = Type; Connection->Type = Type;
Connection->UpstreamNodeOffset = -1; Connection->UpstreamNodeHandle = 0;
Connection->UpstreamNodePortIndex = -1; Connection->UpstreamNodePortIndex = -1;
Connection->DownstreamNodeOffset = -1; Connection->DownstreamNodeHandle = 0;
Connection->DownstreamNodePortIndex = -1; Connection->DownstreamNodePortIndex = -1;
Connection->DirectionMask = DirectionMask; Connection->DirectionMask = DirectionMask;
switch (Type) switch (Type)
@ -192,20 +169,24 @@ PushOutputNodeOnList (node_list* List, v2 Min, memory_arena* Storage)
} }
internal interface_node* internal interface_node*
GetNodeAtOffset (node_list* List, s32 Offset) GetNodeWithHandle(node_list* List, s32 Handle)
{ {
DEBUG_TRACK_FUNCTION; DEBUG_TRACK_FUNCTION;
interface_node* Result = 0;
interface_node* Node = 0; u8* At = List->Memory;
if (Offset <= List->Used) while(At - List->Memory < List->Used)
{ {
Node = (interface_node*)(List->Memory + Offset); interface_node* Node = (interface_node*)At;
if(Node->Handle == Handle)
{
Result = Node;
break;
}
At += GetNodeMemorySize(*Node);
} }
else if (List->Next)
{ return Result;
Node = GetNodeAtOffset(List->Next, Offset - List->Max);
}
return Node;
} }
internal rect internal rect
@ -264,7 +245,8 @@ CalculateNodeOutputValueBounds (interface_node* Node, s32 Index, node_render_set
internal rect internal rect
GetBoundsOfPortConnectedToInput (interface_node* Node, s32 PortIndex, node_list* NodeList, node_render_settings RenderSettings) GetBoundsOfPortConnectedToInput (interface_node* Node, s32 PortIndex, node_list* NodeList, node_render_settings RenderSettings)
{ {
interface_node* ConnectedNode = GetNodeAtOffset(NodeList, Node->Connections[PortIndex].UpstreamNodeOffset); interface_node* ConnectedNode =
GetNodeWithHandle(NodeList, Node->Connections[PortIndex].UpstreamNodeHandle);
rect Result = CalculateNodeOutputPortBounds(ConnectedNode, Node->Connections[PortIndex].UpstreamNodePortIndex, RenderSettings); rect Result = CalculateNodeOutputPortBounds(ConnectedNode, Node->Connections[PortIndex].UpstreamNodePortIndex, RenderSettings);
return Result; return Result;
} }
@ -272,7 +254,7 @@ GetBoundsOfPortConnectedToInput (interface_node* Node, s32 PortIndex, node_list*
internal rect internal rect
GetBoundsOfPortConnectedToOutput (interface_node* Node, s32 PortIndex, node_list* NodeList, node_render_settings RenderSettings) GetBoundsOfPortConnectedToOutput (interface_node* Node, s32 PortIndex, node_list* NodeList, node_render_settings RenderSettings)
{ {
interface_node* ConnectedNode = GetNodeAtOffset(NodeList, Node->Connections[PortIndex].DownstreamNodeOffset); interface_node* ConnectedNode = GetNodeWithHandle(NodeList, Node->Connections[PortIndex].DownstreamNodeHandle);
rect Result = CalculateNodeInputPortBounds(ConnectedNode, Node->Connections[PortIndex].DownstreamNodePortIndex, RenderSettings); rect Result = CalculateNodeInputPortBounds(ConnectedNode, Node->Connections[PortIndex].DownstreamNodePortIndex, RenderSettings);
return Result; return Result;
} }
@ -291,7 +273,7 @@ internal node_interaction
NewEmptyNodeInteraction () NewEmptyNodeInteraction ()
{ {
node_interaction Result = {}; node_interaction Result = {};
Result.NodeOffset = -1; Result.NodeHandle = 0;
Result.InputPort = -1; Result.InputPort = -1;
Result.InputValue = -1; Result.InputValue = -1;
Result.OutputPort = -1; Result.OutputPort = -1;
@ -300,10 +282,10 @@ NewEmptyNodeInteraction ()
} }
internal node_interaction internal node_interaction
NewNodeInteraction (s32 NodeOffset, v2 MouseOffset) NewNodeInteraction (s32 NodeHandle, v2 MouseOffset)
{ {
node_interaction Result = {}; node_interaction Result = {};
Result.NodeOffset = NodeOffset; Result.NodeHandle = NodeHandle;
Result.MouseOffset = MouseOffset; Result.MouseOffset = MouseOffset;
Result.InputPort = -1; Result.InputPort = -1;
Result.InputValue = -1; Result.InputValue = -1;
@ -315,7 +297,7 @@ NewNodeInteraction (s32 NodeOffset, v2 MouseOffset)
internal b32 internal b32
IsDraggingNode (node_interaction Interaction) IsDraggingNode (node_interaction Interaction)
{ {
b32 Result = ((Interaction.NodeOffset >= 0) && b32 Result = ((Interaction.NodeHandle > 0) &&
(Interaction.InputPort < 0 && Interaction.InputValue < 0) && (Interaction.InputPort < 0 && Interaction.InputValue < 0) &&
(Interaction.OutputPort < 0 && Interaction.InputValue < 0)); (Interaction.OutputPort < 0 && Interaction.InputValue < 0));
return Result; return Result;
@ -324,7 +306,7 @@ IsDraggingNode (node_interaction Interaction)
internal b32 internal b32
IsDraggingNodePort (node_interaction Interaction) IsDraggingNodePort (node_interaction Interaction)
{ {
b32 Result = ((Interaction.NodeOffset >= 0) && b32 Result = ((Interaction.NodeHandle > 0) &&
(Interaction.InputPort >= 0 || Interaction.OutputPort >= 0) && (Interaction.InputPort >= 0 || Interaction.OutputPort >= 0) &&
(Interaction.InputValue < 0 && Interaction.OutputValue < 0)); (Interaction.InputValue < 0 && Interaction.OutputValue < 0));
return Result; return Result;
@ -333,7 +315,7 @@ IsDraggingNodePort (node_interaction Interaction)
internal b32 internal b32
IsDraggingNodeValue (node_interaction Interaction) IsDraggingNodeValue (node_interaction Interaction)
{ {
b32 Result = ((Interaction.NodeOffset >= 0) && b32 Result = ((Interaction.NodeHandle > 0) &&
(Interaction.InputPort < 0 && Interaction.OutputPort < 0) && (Interaction.InputPort < 0 && Interaction.OutputPort < 0) &&
(Interaction.InputValue >= 0 || Interaction.OutputValue >= 0)); (Interaction.InputValue >= 0 || Interaction.OutputValue >= 0));
return Result; return Result;
@ -342,7 +324,7 @@ IsDraggingNodeValue (node_interaction Interaction)
internal b32 internal b32
IsDraggingNodeInput (node_interaction Interaction) IsDraggingNodeInput (node_interaction Interaction)
{ {
b32 Result = ((Interaction.NodeOffset >= 0) && b32 Result = ((Interaction.NodeHandle > 0) &&
(Interaction.InputPort >= 0 || Interaction.InputValue >= 0) && (Interaction.InputPort >= 0 || Interaction.InputValue >= 0) &&
(Interaction.OutputPort < 0 && Interaction.OutputValue < 0)); (Interaction.OutputPort < 0 && Interaction.OutputValue < 0));
return Result; return Result;
@ -351,7 +333,7 @@ IsDraggingNodeInput (node_interaction Interaction)
internal b32 internal b32
IsDraggingNodeInputPort (node_interaction Interaction) IsDraggingNodeInputPort (node_interaction Interaction)
{ {
b32 Result = ((Interaction.NodeOffset >= 0) && b32 Result = ((Interaction.NodeHandle > 0) &&
(Interaction.InputPort >= 0) && (Interaction.InputValue < 0) && (Interaction.InputPort >= 0) && (Interaction.InputValue < 0) &&
(Interaction.OutputPort < 0 && Interaction.OutputValue < 0)); (Interaction.OutputPort < 0 && Interaction.OutputValue < 0));
return Result; return Result;
@ -360,7 +342,7 @@ IsDraggingNodeInputPort (node_interaction Interaction)
internal b32 internal b32
IsDraggingNodeInputValue (node_interaction Interaction) IsDraggingNodeInputValue (node_interaction Interaction)
{ {
b32 Result = ((Interaction.NodeOffset >= 0) && b32 Result = ((Interaction.NodeHandle > 0) &&
(Interaction.InputPort < 0) && (Interaction.InputValue >= 0) && (Interaction.InputPort < 0) && (Interaction.InputValue >= 0) &&
(Interaction.OutputPort < 0 && Interaction.OutputValue < 0)); (Interaction.OutputPort < 0 && Interaction.OutputValue < 0));
return Result; return Result;
@ -369,7 +351,7 @@ IsDraggingNodeInputValue (node_interaction Interaction)
internal b32 internal b32
IsDraggingNodeOutput (node_interaction Interaction) IsDraggingNodeOutput (node_interaction Interaction)
{ {
b32 Result = ((Interaction.NodeOffset >= 0) && b32 Result = ((Interaction.NodeHandle > 0) &&
(Interaction.OutputPort >= 0 || Interaction.OutputValue >= 0) && (Interaction.OutputPort >= 0 || Interaction.OutputValue >= 0) &&
(Interaction.InputPort < 0 && Interaction.InputValue < 0)); (Interaction.InputPort < 0 && Interaction.InputValue < 0));
return Result; return Result;
@ -378,7 +360,7 @@ IsDraggingNodeOutput (node_interaction Interaction)
internal b32 internal b32
IsDraggingNodeOutputPort (node_interaction Interaction) IsDraggingNodeOutputPort (node_interaction Interaction)
{ {
b32 Result = ((Interaction.NodeOffset >= 0) && b32 Result = ((Interaction.NodeHandle > 0) &&
(Interaction.OutputPort >= 0) && (Interaction.OutputValue < 0) && (Interaction.OutputPort >= 0) && (Interaction.OutputValue < 0) &&
(Interaction.InputPort < 0 && Interaction.InputValue < 0)); (Interaction.InputPort < 0 && Interaction.InputValue < 0));
return Result; return Result;
@ -387,7 +369,7 @@ IsDraggingNodeOutputPort (node_interaction Interaction)
internal b32 internal b32
IsDraggingNodeOutputValue (node_interaction Interaction) IsDraggingNodeOutputValue (node_interaction Interaction)
{ {
b32 Result = ((Interaction.NodeOffset >= 0) && b32 Result = ((Interaction.NodeHandle > 0) &&
(Interaction.OutputPort < 0) && (Interaction.OutputValue >= 0) && (Interaction.OutputPort < 0) && (Interaction.OutputValue >= 0) &&
(Interaction.InputPort < 0 && Interaction.InputValue < 0)); (Interaction.InputPort < 0 && Interaction.InputValue < 0));
return Result; return Result;
@ -396,7 +378,7 @@ IsDraggingNodeOutputValue (node_interaction Interaction)
internal b32 internal b32
ConnectionIsConnected (node_connection Connection) ConnectionIsConnected (node_connection Connection)
{ {
b32 Result = (Connection.UpstreamNodeOffset >= 0) || (Connection.DownstreamNodeOffset >= 0); b32 Result = (Connection.UpstreamNodeHandle > 0) || (Connection.DownstreamNodeHandle > 0);
return Result; return Result;
} }
@ -410,7 +392,7 @@ ConnectionIsConnected (interface_node* Node, s32 Index)
internal b32 internal b32
ConnectionHasUpstreamConnection (node_connection Connection) ConnectionHasUpstreamConnection (node_connection Connection)
{ {
b32 Result = (Connection.UpstreamNodeOffset >= 0); b32 Result = (Connection.UpstreamNodeHandle > 0);
return Result; return Result;
} }
@ -424,7 +406,7 @@ ConnectionHasUpstreamConnection (interface_node* Node, s32 Index)
internal b32 internal b32
ConnectionHasDownstreamConnection (node_connection Connection) ConnectionHasDownstreamConnection (node_connection Connection)
{ {
b32 Result = (Connection.DownstreamNodeOffset >= 0); b32 Result = (Connection.DownstreamNodeHandle > 0);
return Result; return Result;
} }
@ -462,7 +444,7 @@ ConnectionIsOutput (interface_node* Node, s32 ConnectionIdx)
} }
internal b32 internal b32
CheckForRecursion (node_list* NodeList, s32 LookForNode, interface_node* StartNode) CheckForRecursionWithHandle (node_list* NodeList, s32 LookForNodeHandle, interface_node* StartNode)
{ {
DEBUG_TRACK_FUNCTION; DEBUG_TRACK_FUNCTION;
b32 Result = false; b32 Result = false;
@ -471,16 +453,16 @@ CheckForRecursion (node_list* NodeList, s32 LookForNode, interface_node* StartNo
{ {
if (!ConnectionIsOutput(StartNode->Connections[Connection])) { continue; } if (!ConnectionIsOutput(StartNode->Connections[Connection])) { continue; }
if (StartNode->Connections[Connection].DownstreamNodeOffset == LookForNode) if (StartNode->Connections[Connection].DownstreamNodeHandle == LookForNodeHandle)
{ {
Result = true; Result = true;
break; break;
} }
if (StartNode->Connections[Connection].DownstreamNodeOffset >= 0) if (StartNode->Connections[Connection].DownstreamNodeHandle > 0)
{ {
interface_node* NextNode = GetNodeAtOffset(NodeList, StartNode->Connections[Connection].DownstreamNodeOffset); interface_node* NextNode = GetNodeWithHandle(NodeList, StartNode->Connections[Connection].DownstreamNodeHandle);
Result = CheckForRecursion(NodeList, LookForNode, NextNode); Result = CheckForRecursionWithHandle(NodeList, LookForNodeHandle, NextNode);
if (Result) { break; } if (Result) { break; }
} }
} }
@ -499,48 +481,48 @@ PortTypesMatch (interface_node* UpstreamNode, s32 UpstreamNode_OutputPort, inter
internal void internal void
ConnectNodes(node_list* NodeList, ConnectNodes(node_list* NodeList,
s32 UpstreamNodeOffset, s32 UpstreamNodePort, s32 UpstreamNodeHandle, s32 UpstreamNodePort,
s32 DownstreamNodeOffset, s32 DownstreamNodePort) s32 DownstreamNodeHandle, s32 DownstreamNodePort)
{ {
interface_node* DownstreamNode = GetNodeAtOffset(NodeList, DownstreamNodeOffset); interface_node* DownstreamNode = GetNodeWithHandle(NodeList, DownstreamNodeHandle);
if (!CheckForRecursion(NodeList, UpstreamNodeOffset, DownstreamNode)) if (!CheckForRecursionWithHandle(NodeList, UpstreamNodeHandle, DownstreamNode))
{ {
interface_node* UpstreamNode = GetNodeAtOffset(NodeList, UpstreamNodeOffset); interface_node* UpstreamNode = GetNodeWithHandle(NodeList, UpstreamNodeHandle);
if (PortTypesMatch(UpstreamNode, UpstreamNodePort, if (PortTypesMatch(UpstreamNode, UpstreamNodePort,
DownstreamNode, DownstreamNodePort)) DownstreamNode, DownstreamNodePort))
{ {
Assert(ConnectionIsOutput(UpstreamNode, UpstreamNodePort)); Assert(ConnectionIsOutput(UpstreamNode, UpstreamNodePort));
Assert(ConnectionIsInput(DownstreamNode, DownstreamNodePort)); Assert(ConnectionIsInput(DownstreamNode, DownstreamNodePort));
DownstreamNode->Connections[DownstreamNodePort].UpstreamNodeOffset = UpstreamNodeOffset; DownstreamNode->Connections[DownstreamNodePort].UpstreamNodeHandle = UpstreamNodeHandle;
DownstreamNode->Connections[DownstreamNodePort].UpstreamNodePortIndex = UpstreamNodePort; DownstreamNode->Connections[DownstreamNodePort].UpstreamNodePortIndex = UpstreamNodePort;
UpstreamNode->Connections[UpstreamNodePort].DownstreamNodeOffset = DownstreamNodeOffset; UpstreamNode->Connections[UpstreamNodePort].DownstreamNodeHandle = DownstreamNodeHandle;
UpstreamNode->Connections[UpstreamNodePort].DownstreamNodePortIndex = DownstreamNodePort; UpstreamNode->Connections[UpstreamNodePort].DownstreamNodePortIndex = DownstreamNodePort;
} }
} }
} }
internal void internal void
UnconnectNodes (node_list* NodeList, s32 DownstreamNodeOffset, s32 DownstreamNode_OutputPort, s32 UpstreamNodeOffset, s32 UpstreamNode_InputPort) UnconnectNodes (node_list* NodeList,
s32 DownstreamNodeHandle, s32 DownstreamNode_OutputPort, s32 UpstreamNodeHandle, s32 UpstreamNode_InputPort)
{ {
interface_node* DownstreamNode = GetNodeAtOffset(NodeList, DownstreamNodeOffset); interface_node* DownstreamNode = GetNodeWithHandle(NodeList, DownstreamNodeHandle);
interface_node* UpstreamNode = GetNodeAtOffset(NodeList, UpstreamNodeOffset); interface_node* UpstreamNode = GetNodeWithHandle(NodeList, UpstreamNodeHandle);
Assert(ConnectionIsOutput(DownstreamNode, DownstreamNode_OutputPort)); Assert(ConnectionIsOutput(DownstreamNode, DownstreamNode_OutputPort));
Assert(ConnectionIsInput(UpstreamNode, UpstreamNode_InputPort)); Assert(ConnectionIsInput(UpstreamNode, UpstreamNode_InputPort));
DownstreamNode->Connections[DownstreamNode_OutputPort].DownstreamNodeOffset = -1; DownstreamNode->Connections[DownstreamNode_OutputPort].DownstreamNodeHandle = 0;
DownstreamNode->Connections[DownstreamNode_OutputPort].DownstreamNodePortIndex = -1; DownstreamNode->Connections[DownstreamNode_OutputPort].DownstreamNodePortIndex = -1;
UpstreamNode->Connections[UpstreamNode_InputPort].UpstreamNodeOffset = -1; UpstreamNode->Connections[UpstreamNode_InputPort].UpstreamNodeHandle = 0;
UpstreamNode->Connections[UpstreamNode_InputPort].UpstreamNodePortIndex = -1; UpstreamNode->Connections[UpstreamNode_InputPort].UpstreamNodePortIndex = -1;
} }
internal node_offset internal interface_node*
GetNodeUnderPoint (node_list* NodeList, v2 Point, node_render_settings RenderSettings) GetNodeUnderPoint (node_list* NodeList, v2 Point, node_render_settings RenderSettings)
{ {
DEBUG_TRACK_FUNCTION; DEBUG_TRACK_FUNCTION;
interface_node* Result = 0;
node_offset Result = {0, -1};
node_list_iterator NodeIter = GetNodeListIterator(*NodeList); node_list_iterator NodeIter = GetNodeListIterator(*NodeList);
while (NodeIteratorIsValid(NodeIter)) while (NodeIteratorIsValid(NodeIter))
@ -549,8 +531,7 @@ GetNodeUnderPoint (node_list* NodeList, v2 Point, node_render_settings RenderSet
rect NodeBounds = CalculateNodeBounds(Node, RenderSettings); rect NodeBounds = CalculateNodeBounds(Node, RenderSettings);
if (PointIsInRect(Point, NodeBounds)) if (PointIsInRect(Point, NodeBounds))
{ {
Result.Node = Node; Result = Node;
Result.Offset = (s32)((u8*)NodeIter.At - NodeList->Memory);
break; break;
} }
Next(&NodeIter); Next(&NodeIter);
@ -560,11 +541,11 @@ GetNodeUnderPoint (node_list* NodeList, v2 Point, node_render_settings RenderSet
} }
internal node_interaction internal node_interaction
GetNodeInteractionType (interface_node* ActiveNode, s32 NodeOffset, v2 MousePos, node_render_settings RenderSettings) GetNodeInteractionType (interface_node* ActiveNode, v2 MousePos, node_render_settings RenderSettings)
{ {
DEBUG_TRACK_FUNCTION; DEBUG_TRACK_FUNCTION;
node_interaction Interaction = NewNodeInteraction(NodeOffset, ActiveNode->Min - MousePos); node_interaction Interaction = NewNodeInteraction(ActiveNode->Handle, ActiveNode->Min - MousePos);
rect NodeBounds = CalculateNodeBounds(ActiveNode, RenderSettings); rect NodeBounds = CalculateNodeBounds(ActiveNode, RenderSettings);
@ -633,18 +614,18 @@ TryConnectNodes (node_interaction Interaction, v2 Point, node_list* NodeList, no
if (IsDraggingNodeOutput(Interaction)) if (IsDraggingNodeOutput(Interaction))
{ {
node_offset UpstreamNodeOffset = GetNodeUnderPoint(NodeList, Point, RenderSettings); interface_node* UpstreamNode = GetNodeUnderPoint(NodeList, Point, RenderSettings);
if (UpstreamNodeOffset.Node) if (UpstreamNode)
{ {
for (s32 Connection = 0; Connection < UpstreamNodeOffset.Node->ConnectionsCount; Connection++) for (s32 Connection = 0; Connection < UpstreamNode->ConnectionsCount; Connection++)
{ {
if (ConnectionIsOutput(UpstreamNodeOffset.Node, Connection)) { continue; } if (ConnectionIsOutput(UpstreamNode, Connection)) { continue; }
rect InputBounds = CalculateNodeInputPortBounds(UpstreamNodeOffset.Node, Connection, RenderSettings); rect InputBounds = CalculateNodeInputPortBounds(UpstreamNode, Connection, RenderSettings);
if (PointIsInRect(Point, InputBounds)) if (PointIsInRect(Point, InputBounds))
{ {
ConnectNodes(NodeList, Interaction.NodeOffset, Interaction.OutputPort, ConnectNodes(NodeList, Interaction.NodeHandle, Interaction.OutputPort,
UpstreamNodeOffset.Offset, Connection); UpstreamNode->Handle, Connection);
break; break;
} }
} }
@ -652,19 +633,19 @@ TryConnectNodes (node_interaction Interaction, v2 Point, node_list* NodeList, no
} }
else if (IsDraggingNodeInput(Interaction)) else if (IsDraggingNodeInput(Interaction))
{ {
node_offset DownstreamNodeOffset = GetNodeUnderPoint(NodeList, Point, RenderSettings); interface_node* DownstreamNode = GetNodeUnderPoint(NodeList, Point, RenderSettings);
if (DownstreamNodeOffset.Node) if (DownstreamNode)
{ {
for (s32 Connection = 0; Connection < DownstreamNodeOffset.Node->ConnectionsCount; Connection++) for (s32 Connection = 0; Connection < DownstreamNode->ConnectionsCount; Connection++)
{ {
if (ConnectionIsInput(DownstreamNodeOffset.Node, Connection)) { continue; } if (ConnectionIsInput(DownstreamNode, Connection)) { continue; }
rect OutputBounds = CalculateNodeOutputPortBounds(DownstreamNodeOffset.Node, Connection, RenderSettings); rect OutputBounds = CalculateNodeOutputPortBounds(DownstreamNode, Connection, RenderSettings);
if (PointIsInRect(Point, OutputBounds)) if (PointIsInRect(Point, OutputBounds))
{ {
ConnectNodes(NodeList, ConnectNodes(NodeList,
DownstreamNodeOffset.Offset, Connection, DownstreamNode->Handle, Connection,
Interaction.NodeOffset, Interaction.InputPort); Interaction.NodeHandle, Interaction.InputPort);
break; break;
} }
} }
@ -685,10 +666,10 @@ PlaceNode (node_list* NodeList, interface_node* Node, v2 Position, b32 Flags)
{ {
if (!ConnectionIsOutput(Node, Connection)) { continue; } if (!ConnectionIsOutput(Node, Connection)) { continue; }
s32 ConnectionOffset = Node->Connections[Connection].DownstreamNodeOffset; s32 ConnectionHandle = Node->Connections[Connection].DownstreamNodeHandle;
if (ConnectionOffset >= 0) if (ConnectionHandle > 0)
{ {
interface_node* ConnectedNode = GetNodeAtOffset(NodeList, ConnectionOffset); interface_node* ConnectedNode = GetNodeWithHandle(NodeList, ConnectionHandle);
v2 CurrPos = ConnectedNode->Min; v2 CurrPos = ConnectedNode->Min;
v2 NewPos = CurrPos + Offset; v2 NewPos = CurrPos + Offset;
// NOTE(Peter): Have to negate the all downstream component so it doesn't turn around and try // NOTE(Peter): Have to negate the all downstream component so it doesn't turn around and try
@ -704,10 +685,10 @@ PlaceNode (node_list* NodeList, interface_node* Node, v2 Position, b32 Flags)
{ {
if (!ConnectionIsInput(Node, Connection)) { continue; } if (!ConnectionIsInput(Node, Connection)) { continue; }
s32 ConnectionOffset = Node->Connections[Connection].UpstreamNodeOffset; s32 ConnectionHandle = Node->Connections[Connection].UpstreamNodeHandle;
if (ConnectionOffset >= 0) if (ConnectionHandle > 0)
{ {
interface_node* ConnectedNode = GetNodeAtOffset(NodeList, ConnectionOffset); interface_node* ConnectedNode = GetNodeWithHandle(NodeList, ConnectionHandle);
v2 CurrPos = ConnectedNode->Min; v2 CurrPos = ConnectedNode->Min;
v2 NewPos = CurrPos + Offset; v2 NewPos = CurrPos + Offset;
// NOTE(Peter): Have to negate the all upstream component so it doesn't turn around and try // NOTE(Peter): Have to negate the all upstream component so it doesn't turn around and try
@ -727,7 +708,7 @@ UpdateDraggingNode (v2 MousePos, node_interaction Interaction, node_list* NodeLi
if (IsDraggingNode(Interaction)) if (IsDraggingNode(Interaction))
{ {
interface_node* ActiveNode = GetNodeAtOffset(NodeList, Interaction.NodeOffset); interface_node* ActiveNode = GetNodeWithHandle(NodeList, Interaction.NodeHandle);
PlaceNode(NodeList, ActiveNode, MousePos + Interaction.MouseOffset, Interaction.Flags); PlaceNode(NodeList, ActiveNode, MousePos + Interaction.MouseOffset, Interaction.Flags);
} }
} }
@ -739,7 +720,7 @@ UpdateDraggingNodePort (v2 MousePos, node_interaction Interaction, node_list* No
if (IsDraggingNodePort(Interaction)) if (IsDraggingNodePort(Interaction))
{ {
interface_node* ActiveNode = GetNodeAtOffset(NodeList, Interaction.NodeOffset); interface_node* ActiveNode = GetNodeWithHandle(NodeList, Interaction.NodeHandle);
rect PortBounds = {}; rect PortBounds = {};
if (IsDraggingNodeInput(Interaction)) if (IsDraggingNodeInput(Interaction))
{ {
@ -763,7 +744,7 @@ UpdateDraggingNodeValue (v2 MousePos, v2 LastFrameMousePos, node_interaction Int
if(IsDraggingNodeValue(Interaction)) if(IsDraggingNodeValue(Interaction))
{ {
v2 MouseDelta = MousePos - LastFrameMousePos; v2 MouseDelta = MousePos - LastFrameMousePos;
interface_node* Node = GetNodeAtOffset(NodeList, Interaction.NodeOffset); interface_node* Node = GetNodeWithHandle(NodeList, Interaction.NodeHandle);
node_connection* Connection = 0; node_connection* Connection = 0;
if (IsDraggingNodeInputValue(Interaction)) if (IsDraggingNodeInputValue(Interaction))
@ -819,7 +800,7 @@ UpdateNodesConnectedUpstream (interface_node* Node, node_list* NodeList,
if (ConnectionHasUpstreamConnection(*Connection)) if (ConnectionHasUpstreamConnection(*Connection))
{ {
interface_node* UpstreamNode = GetNodeAtOffset(NodeList, Connection->UpstreamNodeOffset); interface_node* UpstreamNode = GetNodeWithHandle(NodeList, Connection->UpstreamNodeHandle);
if (!UpstreamNode->UpdatedThisFrame) if (!UpstreamNode->UpdatedThisFrame)
{ {
UpdateNodeCalculation(UpstreamNode, NodeList, Permanent, Transient, LEDs, ColorsInit, LEDCount, DeltaTime); UpdateNodeCalculation(UpstreamNode, NodeList, Permanent, Transient, LEDs, ColorsInit, LEDCount, DeltaTime);

View File

@ -53,11 +53,10 @@ struct node_led_color_connection
struct node_connection struct node_connection
{ {
struct_member_type Type; struct_member_type Type;
// NOTE(Peter): Offset from the head of the node list that the connected node
// is stored at. See GetNodeAtOffset for example of how this is used s32 UpstreamNodeHandle;
s32 UpstreamNodeOffset;
s32 UpstreamNodePortIndex; s32 UpstreamNodePortIndex;
s32 DownstreamNodeOffset; s32 DownstreamNodeHandle;
s32 DownstreamNodePortIndex; s32 DownstreamNodePortIndex;
b32 DirectionMask; b32 DirectionMask;
@ -75,6 +74,7 @@ struct node_connection
#define NODE_CONNECTIONS_MAX 8 #define NODE_CONNECTIONS_MAX 8
struct interface_node struct interface_node
{ {
s32 Handle;
string Name; string Name;
v2 Min, Dim; v2 Min, Dim;
@ -95,12 +95,8 @@ struct node_list
s32 Used; s32 Used;
node_list* Next; node_list* Next;
};
struct node_offset s32 HandleAccumulator;
{
interface_node* Node;
s32 Offset;
}; };
struct node_list_iterator struct node_list_iterator
@ -117,7 +113,7 @@ enum node_interaction_flag
struct node_interaction struct node_interaction
{ {
s32 NodeOffset; s32 NodeHandle;
v2 MouseOffset; v2 MouseOffset;
b32 Flags; b32 Flags;