aboutsummaryrefslogtreecommitdiff
path: root/undo-redo.c
blob: ba0444a7aa462f7fc789a1cb9567452972bbc6ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
 *
 * Copyright (C) 2022 Jeremias Stotter <jeremias@stotter.eu>
 *
 * This file is part of network-simulator.
 *
 * network-simulator is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * network-simulator is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with network-simulator. If not, see <https://www.gnu.org/licenses/>. 
*/

#include "undo-redo.h"

#include "decorations.h"

int max_entries = 100;
int cur_entries_n = 0;

// This is an empty entry at the start of the list
struct undo_entry empty_start = {
	.prev = NULL,
	.next = NULL,
	.action = undo_nothing,
	.undo_data = NULL,
	.next_sibling = NULL,
	.prev_sibling = NULL,
	.undone = 0
};

struct undo_entry* cur_entry = &empty_start;

void free_undo_entry(struct undo_entry* fentry) {
	while(fentry) {
		switch(fentry->action) {
			case undo_move:
				free(((struct undo_move_data*)fentry->undo_data)->devices);
				break;
			case undo_move_deco:
				deco_put(((struct undo_move_deco_data*)fentry->undo_data)->deco);
				break;
			case undo_del_dev:
				free_device(((struct undo_del_dev_data*)fentry->undo_data)->dev);
				break;
			case undo_add_dev:
				if(fentry->undone)
					free_device(((struct undo_add_dev_data*)fentry->undo_data)->dev);
				break;
			case undo_add_con:
				break;
			case undo_del_con:
				break;
			case undo_ren_dev:
				free(((struct undo_ren_data*)fentry->undo_data)->old_name);
				free(((struct undo_ren_data*)fentry->undo_data)->new_name);
				break;
			case undo_add_deco:
				deco_put(((struct undo_add_deco_data*)fentry->undo_data)->deco);
				break;
			case undo_del_deco:
				deco_put(((struct undo_del_deco_data*)fentry->undo_data)->deco);
				break;
			case undo_set_deco:
				deco_put(((struct undo_set_deco_data*)fentry->undo_data)->deco);
				free(((struct undo_set_deco_data*)fentry->undo_data)->old_text);
				free(((struct undo_set_deco_data*)fentry->undo_data)->new_text);
				break;
			case undo_nothing:
				break;
		}
		if(fentry->undo_data)
			free(fentry->undo_data);

		struct undo_entry* freeentr = fentry;
		fentry = fentry->next_sibling;
		free(freeentr);
	}
	cur_entries_n--;
}

// This frees all the undo entries
void free_all_undo() {
	struct undo_entry* next_entry = NULL;
	while(cur_entry != &empty_start) {
		next_entry = cur_entry->prev;
		free_undo_entry(cur_entry);
		cur_entry = next_entry;
	}
	empty_start.next = NULL;
}

// Adds entry after the current entry, frees everything following cur_entry before
void undo_add_entry(struct undo_entry* entry) {
	printf("Adding entry of type %d\n", entry->action);

	// Free all the entries not needed anymore
	struct undo_entry* last;
	LL_LAST(cur_entry->next, last);
	if(last) {
		do {
			struct undo_entry* free_entry = last;
			last = free_entry->prev;
			free_undo_entry(free_entry);
		} while(last != cur_entry);
	}

	// Add the entry to the current point
	cur_entry->next = entry;
	entry->prev = cur_entry;
	cur_entry = entry;
	cur_entries_n++;
	if(cur_entries_n > max_entries) {
		struct undo_entry* new_start = empty_start.next->next;
		free_undo_entry(empty_start.next);
		empty_start.next = new_start;
		new_start->prev = &empty_start;
	}
	actions_since_save++;
}

void undo_move_devices(char* names, int dev_n, float X_off, float Y_off) {
	for(int i = 0; i < dev_n; i++) {
		struct device* move_dev = dev_from_name(start_device, names);
		if(!move_dev) continue;
		move_dev->X += X_off;
		move_dev->Y += Y_off;
		for(; *names != '\0'; names++);
		names++;
	}
}

int undo() {
	if(!cur_entry) return 0;
	for(struct undo_entry* uentry = cur_entry; uentry; uentry = uentry->next_sibling) {
		switch(uentry->action) {
			case undo_move:
				{
					struct undo_move_data* move_data = uentry->undo_data;
					undo_move_devices(move_data->devices, move_data->dev_n, -move_data->X_off, -move_data->Y_off);
				}
				break;
			case undo_move_deco:
				{
					struct undo_move_deco_data* move_deco_data = uentry->undo_data;
					move_deco_data->deco->X -= move_deco_data->X_off;
					move_deco_data->deco->Y -= move_deco_data->Y_off;
				}
				break;
			case undo_add_dev:
				{
					struct undo_add_dev_data* add_dev_data = uentry->undo_data;
					delete_device(&start_device, add_dev_data->dev);
				}
				break;
			case undo_del_dev:
				{
					struct undo_del_dev_data* del_dev_data = uentry->undo_data;
					append_device(&start_device, del_dev_data->dev);
				}
				break;
			case undo_add_con:
				{
					struct undo_add_con_data* add_con_data = uentry->undo_data;
					delete_connection(add_con_data->con);
				}
				break;
			case undo_del_con:
				{
					struct undo_del_con_data* del_con_data = uentry->undo_data;
					new_connection(del_con_data->int1, del_con_data->int2, del_con_data->template);
				}
				break;
			case undo_ren_dev:
				{
					struct undo_ren_data* ren_dev_data = uentry->undo_data;
					rename_device(ren_dev_data->dev, ren_dev_data->old_name);
				}
				break;
			case undo_add_deco:
				{
					struct undo_add_deco_data* deco_data = uentry->undo_data;
					deco_unappend(deco_data->deco);
				}
				break;
			case undo_del_deco:
				{
					struct undo_del_deco_data* deco_data = uentry->undo_data;
					deco_append(deco_data->deco);
				}
				break;
			case undo_set_deco:
				{
					struct undo_set_deco_data* deco_data = uentry->undo_data;
					deco_text_set(deco_data->deco, deco_data->old_text);
				}
				break;
			case undo_nothing:
				break;
		}
	}
	cur_entry->undone = 1;
	if(cur_entry->prev)
		cur_entry = cur_entry->prev;

	actions_since_save--;
	return 0;
}

int redo() {
	if(!cur_entry || !cur_entry->next) return 0;
	cur_entry = cur_entry->next;
	// Start from the back
	struct undo_entry* rentry = NULL;
	for(struct undo_entry* test_entry = cur_entry; test_entry; test_entry = test_entry->next_sibling) {
		rentry = test_entry;
	}
	for(; rentry; rentry = rentry->prev_sibling) {
		switch(rentry->action) {
			case undo_move:
				{
					struct undo_move_data* move_data = rentry->undo_data;
					undo_move_devices(move_data->devices, move_data->dev_n, move_data->X_off, move_data->Y_off);
				}
				break;
			case undo_move_deco:
				{
					struct undo_move_deco_data* move_deco_data = rentry->undo_data;
					move_deco_data->deco->X += move_deco_data->X_off;
					move_deco_data->deco->Y += move_deco_data->Y_off;
				}
				break;
			case undo_add_dev:
				{
					struct undo_add_dev_data* add_dev_data = rentry->undo_data;
					append_device(&start_device, add_dev_data->dev);
				}
				break;
			case undo_del_dev:
				{
					struct undo_del_dev_data* del_dev_data = rentry->undo_data;
					delete_device(&start_device, del_dev_data->dev);
				}
				break;
			case undo_add_con:
				{
					struct undo_add_con_data* add_con_data = rentry->undo_data;
					new_connection(add_con_data->int1, add_con_data->int2, add_con_data->template);
					add_con_data->con = add_con_data->int1->connection;
				}
				break;
			case undo_del_con:
				{
					struct undo_del_con_data* del_con_data = rentry->undo_data;
					delete_connection(del_con_data->int1->connection);
				}
				break;
			case undo_ren_dev:
				{
					struct undo_ren_data* ren_dev_data = rentry->undo_data;
					rename_device(ren_dev_data->dev, ren_dev_data->new_name);
				}
				break;
			case undo_add_deco:
				{
					struct undo_add_deco_data* deco_data = rentry->undo_data;
					deco_append(deco_data->deco);
				}
				break;
			case undo_del_deco:
				{
					struct undo_del_deco_data* deco_data = rentry->undo_data;
					deco_unappend(deco_data->deco);
				}
				break;
			case undo_set_deco:
				{
					struct undo_set_deco_data* deco_data = rentry->undo_data;
					deco_text_set(deco_data->deco, deco_data->new_text);
				}
				break;
			case undo_nothing:
				break;
		}
	}

	actions_since_save++;
	cur_entry->undone = 0;
	return 0;
}
Jeremias Stotters git repositories generated by CGIT