#include #include #include #include #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, 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); 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_testing_layer_add(const Arg *x) { /* TODO: temp function */ if (cur_canvas == NULL) return; 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; ui_redraw_panel(UI_PANELTYPE_CANVAS); ui_redraw_panel(UI_PANELTYPE_TIMELINE); } void user_layer_chng(const Arg *x) { if (cur_canvas == NULL) return; cur_canvas->cur_layer += (unsigned int)cur_canvas->layer_arr_cnt; cur_canvas->cur_layer += (x->i % cur_canvas->layer_arr_cnt); 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); 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); }