diff options
author | Krow Savcik <krow@savcik.xyz> | 2023-09-26 22:32:35 +0300 |
---|---|---|
committer | Krow Savcik <krow@savcik.xyz> | 2023-09-26 22:32:35 +0300 |
commit | 9e660e594f6d3a43ea1427fb872801a2fcedad93 (patch) | |
tree | 459cd0c6fc5fef0366f7ef249ae19dd67c363772 /src/user.c |
initial commit
Diffstat (limited to 'src/user.c')
-rw-r--r-- | src/user.c | 238 |
1 files changed, 238 insertions, 0 deletions
diff --git a/src/user.c b/src/user.c new file mode 100644 index 0000000..d5a545e --- /dev/null +++ b/src/user.c @@ -0,0 +1,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(); +} + |