Skip to content

Commit c9ee317

Browse files
committed
Node moving completed
1 parent c274ff2 commit c9ee317

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
lines changed

Diff for: Component.c

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ ComponentData component_create(float x, float y, char *name, float size, float t
117117

118118
void component_free_data(ComponentData *dat) {
119119
free(dat->pinData.pins);
120+
SDL_DestroyTexture(dat->texture);
120121
parser_free_function(&dat->funData);
121122
}
122123

Diff for: NodeVector.c

+20
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,24 @@ void nodev_free(NodeVector *vector) {
7373
for (size_t i = 0; i < vector->count; i++)
7474
node_free(&vector->nodes[i]);
7575
free(vector->nodes);
76+
}
77+
78+
void nodev_reposition(NodeVector *vector, Node *node, Point position) {
79+
if (node->component.x != position.x || node->component.y != position.y) {
80+
node->component.x = position.x;
81+
node->component.y = position.y;
82+
node_reposition_wires(node, vector->nodes);
83+
for (size_t i = 0; i < vector->count; i++) {
84+
Node *origin = nodev_at(vector, i);
85+
for (int w = 0; w < origin->component.funData.outC; w++) {
86+
Wire *wire = &origin->wires[w];
87+
for (size_t c = 0; c < wire->conCount; c++) {
88+
Connection *con = &wire->connections[c];
89+
if (nodev_at(vector, con->dest) == node) {
90+
connection_reposition(con, origin, c, node);
91+
}
92+
}
93+
}
94+
}
95+
}
7696
}

Diff for: NodeVector.h

+9
Original file line numberDiff line numberDiff line change
@@ -98,4 +98,13 @@ void nodev_check_clicks(NodeVector *vector, Point mousePos);
9898
*/
9999
void nodev_free(NodeVector *vector);
100100

101+
/**
102+
* @brief Repositions a node in the vector
103+
*
104+
* @param vector Vector to get the node from
105+
* @param node Node to reposition
106+
* @param position New position
107+
*/
108+
void nodev_reposition(NodeVector *vector, Node *node, Point position);
109+
101110
#endif //HOMEWORK_NODEVECTOR_H

Diff for: Wire.c

+17-10
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,24 @@ void wire_free(Wire *wire) {
5050
free(wire->connections);
5151
}
5252

53+
void connection_reposition(Connection *conn, Node *origin, int originPin, Node *dest) {
54+
component_free_data(&conn->wireComp);
55+
conn->wireComp = component_create_wire_between(
56+
&origin->component,
57+
&dest->component,
58+
originPin + origin->component.funData.inC,
59+
conn->pin,
60+
COMPSIZE,
61+
COMPTHICKNESS,
62+
origin->renderer
63+
);
64+
}
65+
5366
void wire_reposition(Wire *wire, Node *nodes) {
5467
for (size_t i = 0; i < wire->conCount; i++) {
55-
component_free_data(&wire->connections[i].wireComp);
56-
wire->connections[i].wireComp = component_create_wire_between(
57-
&wire->origin->component,
58-
&nodes[wire->connections[i].dest].component,
59-
wire->originPin + wire->origin->component.funData.inC,
60-
wire->connections[i].pin,
61-
COMPSIZE,
62-
COMPTHICKNESS,
63-
wire->origin->renderer
64-
);
68+
connection_reposition(&wire->connections[i],
69+
wire->origin,
70+
wire->originPin,
71+
&nodes[wire->connections[i].dest]);
6572
}
6673
}

Diff for: Wire.h

+10
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,16 @@ void wire_send_value(Wire *wire, bool value, Node *nodes);
7979
*/
8080
void wire_free(Wire *wire);
8181

82+
/**
83+
* @brief Repositions a connection
84+
*
85+
* @param conn Connection to reposition
86+
* @param origin Origin node
87+
* @param originPon Origin pin
88+
* @param dest Destination node
89+
*/
90+
void connection_reposition(Connection *conn, Node *origin, int originPin, Node *dest);
91+
8292
/**
8393
* @brief Repositions the connections of the wire
8494
*

Diff for: main.c

+1-3
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,7 @@ int main(int argc, char **argv) {
163163
case MOVING_COMPONENT: {
164164
//Update
165165
camera_update(&camera, &mainWindow.input, mainWindow.renderer);
166-
moved->component.x = mouseWS.x;
167-
moved->component.y = mouseWS.y;
168-
node_reposition_wires(moved, vec.nodes);
166+
nodev_reposition(&vec, moved, mouseWS);
169167

170168
//Transitions
171169
if (input_get_mouse_button(&mainWindow.input, SDL_BUTTON_LEFT).isPressed) {

0 commit comments

Comments
 (0)