aboutsummaryrefslogtreecommitdiff
path: root/src/user.c
blob: daa3e5dd09af8acb20139837538c3c0da79ba0bb (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <SDL2/SDL.h>

#include "ui.h"
#include "debug.h"
#undef malloc()
#undef realloc()
#undef free()
#define SDL_Window void
#include "cdraw.h"
#include "types.h"
#include "tools.h"
#include "action.h"
#include "canvas.h"
#include "palette.h"

static void choose_file_path(const char *, char *);

void
user_canvas_zoom_change(const Arg *x)
{
	if (cur_canvas == NULL) return;
	int oldzoom = cur_canvas->zoom;
	canvas_zoom_change(cur_canvas, x->i);
	/* canvas_move_x(cur_canvas, (oldzoom - (int)cur_canvas->zoom) * (int)cur_canvas->w / 2); */
	/* canvas_move_y(cur_canvas, (oldzoom - (int)cur_canvas->zoom) * (int)cur_canvas->h / 2); */
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
}

void
user_canvas_move_x(const Arg *x)
{
    if (cur_canvas == NULL) return;
    canvas_move_x(cur_canvas, x->i);
    ui_redraw_panel(UI_PANELTYPE_CANVAS);
}

void
user_canvas_move_y(const Arg *x)
{
    if (cur_canvas == NULL) return;
    canvas_move_y(cur_canvas, x->i);
    ui_redraw_panel(UI_PANELTYPE_CANVAS);
}

void
user_canvas_save(const Arg *x)
{
	char line[1024];
	if (cur_canvas == NULL) return;
	if (cur_canvas->proj_path != NULL)
		goto SAVENONSP;

	choose_file_path("Project File Name (save): ", line);
	if (line[0] == '\0') return;

	printf("Saved project to file %s\n", line);
	canvas_save(cur_canvas, line, 1);
	return;
SAVENONSP:
	canvas_save(cur_canvas, cur_canvas->proj_path, 0);
	printf("Saved project to file %s\n", cur_canvas->proj_path);
}

void
user_canvas_open(const Arg *x)
{
	char line[1024];
	char *z = x->s;

	if (z == NULL) {
		choose_file_path("Project File Name (open): ", line);
		z = line;
	}

	if (z[0] == '\0') return;
	canvas_destroy(cur_canvas);
	cur_canvas = canvas_open(z, ren);

/* TODO: show error window */
	if (cur_canvas == NULL)
		puts("Error opening file");
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
	ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}

void
user_canvas_export_png(const Arg *x)
{
	char line[1024];
	if (cur_canvas == NULL) return;
	choose_file_path("Image File Name (export): ", line);

/* TODO: show error window */
	if (canvas_export_png(cur_canvas, line))
		puts("Error while saving file");
	else
		puts("File saved");
}

void
user_canvas_refresh(const Arg *x)
{
	if (cur_canvas == NULL) return;
	puts("Refreshed canvas");
	canvas_refresh(cur_canvas);
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
}

void
user_canvas_create_new(const Arg *x)
{
	int w, h;
	printf("Width: ");
	fflush(stdout);
	scanf("%d", &w);
	printf("Height: ");
	fflush(stdout);
	scanf("%d", &h);
	if (cur_canvas != NULL)
		canvas_destroy(cur_canvas);

	cur_canvas = canvas_init(w, h, ren);
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
	ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}

void
user_canvas_import_png(const Arg *x)
{
	char line[1024];
	char *z = x->s;

	if (z == NULL) {
		choose_file_path("Image File Name (import): ", line);
		z = line;
	}
	
	if (z[0] == '\0') return;
	canvas_destroy(cur_canvas);
	cur_canvas = canvas_import_png(z, ren);

	/* TODO: show error window */
	if (cur_canvas == NULL)
		puts("Error opening file");
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
	ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}

void
user_canvas_pal_col_chng(const Arg *x)
{
	cur_canvas->cur_col += ((Palette*)def_palette)->num;
	cur_canvas->cur_col += x->i;
	cur_canvas->cur_col %= ((Palette*)def_palette)->num;
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
}

void
user_tool_change(const Arg *x)
{
    tool_change(x->i);
}

void
user_layer_add(const Arg *x)
{
	if (cur_canvas == NULL) return;
	if (canvas_add_layer(cur_canvas, cur_canvas->cur_layer+1)) return;
	cur_canvas->cur_layer++;
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
	ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}

void
user_layer_chng(const Arg *x)
{
	if (cur_canvas == NULL) return;
	canvas_change_layer(cur_canvas, (cur_canvas->cur_layer + (unsigned int)cur_canvas->layer_arr_cnt + (x->i % (int)cur_canvas->layer_arr_cnt)) % cur_canvas->layer_arr_cnt);
	ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}

void
user_frame_add(const Arg *x)
{
	int i;
	if (cur_canvas == NULL) return;
	if (canvas_add_frame(cur_canvas, cur_canvas->cur_frame+1)) return;
	for (i = 0; i < cur_canvas->layer_arr_cnt; i++)
		memcpy(cur_canvas->cells[(cur_canvas->cur_frame + 1) * cur_canvas->layer_arr_cnt+i],
		    cur_canvas->cells[cur_canvas->cur_frame * cur_canvas->layer_arr_cnt+i],
		    cur_canvas->w * cur_canvas->h * sizeof *cur_canvas->cells[0]);
	canvas_change_frame(cur_canvas, cur_canvas->cur_frame+1);
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
	ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}

void
user_frame_chng(const Arg *x)
{
	if (cur_canvas == NULL) return;
	canvas_change_frame(cur_canvas, (cur_canvas->cur_frame + (unsigned int)cur_canvas->frame_arr_cnt + (x->i % (int)cur_canvas->frame_arr_cnt)) % cur_canvas->frame_arr_cnt);
	ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}

void
user_debug_mem_show(const Arg *x)
{
	fprintf(stdout, "DEBUG INFO:\n");
	f_debug_mem_show();
}

void
user_testing_reload_tex(const Arg *_x)
{
/* TODO: temp function */
    /* ui_redraw(); */
    /* return; */
	SDL_Rect dest;
	int i;
	long int x, y;

	if (cur_canvas == NULL) return;
	dest.x = dest.y = 0;
	dest.w = cur_canvas->w * 2;
	dest.h = cur_canvas->h * 2;

	SDL_DestroyTexture(*((void **)main_ui));

	*((void **)main_ui) = SDL_CreateTexture( ren, SDL_PIXELFORMAT_RGBA8888,
			SDL_TEXTUREACCESS_TARGET, dest.w, dest.h);
	SDL_SetTextureBlendMode(*((void **)main_ui), SDL_BLENDMODE_BLEND);
	SDL_SetRenderTarget(ren, *((void **)main_ui));

	for (x = 0; x < cur_canvas->w; ++x) {
		for (y = 0; y < cur_canvas->h; ++y) {
			SDL_SetRenderDrawColor(ren, INTTOCOLA(cur_canvas->pres_pix[x + y * cur_canvas->w]));
			SDL_RenderDrawPoint(ren, 2*x, 2*y);
			SDL_RenderDrawPoint(ren, 2*x+1, 2*y);
			SDL_RenderDrawPoint(ren, 2*x+1, 2*y+1);
			SDL_RenderDrawPoint(ren, 2*x, 2*y+1);
		}
	}

	SDL_SetRenderTarget(ren, NULL);
	ui_redraw();
}

static void
choose_file_path(const char *prompt, char *input)
{
	FILE *ft;
	ft = cmd_file_picker == NULL ? NULL : popen(cmd_file_picker, "r");
	input[0] = '\0';

	if (ft == NULL) {
/*TODO: error; for now using terminal input as backup*/
		printf(prompt);
		fflush(stdout);
		ft = stdin;
	}

	while (fgets(input, 1024, ft) != NULL && strlen(input) < 4);
	input[strcspn(input, "\n")] = '\0';

	if (ft != stdin) pclose(ft);
}

void
user_canvas_undo(const Arg *x)
{
	if (cur_canvas == NULL) return;
	action_undo(cur_canvas);
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
}

void
user_canvas_redo(const Arg *x)
{
	if (cur_canvas == NULL) return;
	action_redo(cur_canvas);
	ui_redraw_panel(UI_PANELTYPE_CANVAS);
}