aboutsummaryrefslogtreecommitdiff
path: root/src/user.c
blob: d5a545e68e3a9b7206331b790da9c7f090e6233d (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
#include <stdio.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 "canvas.h"
#include "palette.h"

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;
	printf("Project File Name (save): ");
	fflush(stdout);

	do {
		fgets(line, 1024, stdin);
	} while (strlen(line) < 4);
	line[strcspn(line, "\n")] = '\0';
	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];
	canvas_destroy(cur_canvas);

	if (x->s == NULL) {
		printf("File Name (open): ");
		fflush(stdout);

		do {
			fgets(line, 1024, stdin);
		} while (strlen(line) < 4);
		line[strcspn(line, "\n")] = '\0';
		cur_canvas = canvas_open(line, ren);
	} else {
		cur_canvas = canvas_open(x->s, ren);
	}

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

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

    do {
        fgets(line, 1024, stdin);
    } while (strlen(line) < 4);
    line[strcspn(line, "\n")] = '\0';
/* TODO: show error window */
    if (canvas_export_png(cur_canvas, line, ren))
        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);
}

void
user_canvas_import_png(const Arg *x)
{
    char line[1024];
    canvas_destroy(cur_canvas);

    if (x->s == NULL) {
        printf("File Name (i): ");
        fflush(stdout);

        do {
            fgets(line, 1024, stdin);
        } while (strlen(line) < 4);
        line[strcspn(line, "\n")] = '\0';
        cur_canvas = canvas_import_png(line, ren);
    } else {
        cur_canvas = canvas_import_png(x->s, ren);
    }

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

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_testing_layer_add(const Arg *x)
{
/* TODO: temp function */
    canvas_add_layer(cur_canvas, -1);
	fprintf(stdout, "%s:%u: added layer\n", __FILE__, __LINE__);
    cur_canvas->cur_layer = cur_canvas->layer_arr_cnt-1;
}

void
user_layer_chng(const Arg *x)
{
    cur_canvas->cur_layer += (unsigned int)cur_canvas->layer_arr_cnt;
    cur_canvas->cur_layer += x->i;
    cur_canvas->cur_layer %= cur_canvas->layer_arr_cnt;

	fprintf(stdout, "%s:%u: changed to layer %u\n", __FILE__, __LINE__, cur_canvas->cur_layer);
    ui_redraw_panel(UI_PANELTYPE_CANVAS);
}

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();
}