aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrow Savcik <krow@savcik.xyz>2024-02-13 12:00:11 +0200
committerKrow Savcik <krow@savcik.xyz>2024-02-13 12:00:11 +0200
commit8b8e1cab080dfea106bebe9d4eeecf5812182e94 (patch)
tree1ced4cdeecf7b49db78f69b6f65f334221bdac13
parent67abc25311d9dc7d12b28d07aaa37fcaf9b5fee5 (diff)
feature: add basic layer support
-rw-r--r--assets/small.cdrbin65546 -> 65546 bytes
-rw-r--r--assets/small.pngbin801 -> 851 bytes
-rw-r--r--assets/ui.pngbin1648 -> 1741 bytes
-rw-r--r--src/action.h2
-rw-r--r--src/canvas.c28
-rw-r--r--src/canvas.h1
-rw-r--r--src/ui.c65
-rw-r--r--src/user.c16
8 files changed, 92 insertions, 20 deletions
diff --git a/assets/small.cdr b/assets/small.cdr
index 0008801..3c95fa2 100644
--- a/assets/small.cdr
+++ b/assets/small.cdr
Binary files differ
diff --git a/assets/small.png b/assets/small.png
index 8664b55..672df07 100644
--- a/assets/small.png
+++ b/assets/small.png
Binary files differ
diff --git a/assets/ui.png b/assets/ui.png
index 91d3018..2fe1a80 100644
--- a/assets/ui.png
+++ b/assets/ui.png
Binary files differ
diff --git a/src/action.h b/src/action.h
index d9b7698..825057f 100644
--- a/src/action.h
+++ b/src/action.h
@@ -12,7 +12,7 @@ struct action_pixcol {
struct ActionPixelsColors {
long int cnt;
- struct actiton_pixcol *pix;
+ struct action_pixcol *pix;
};
typedef union {
diff --git a/src/canvas.c b/src/canvas.c
index 47f9b92..3a016d7 100644
--- a/src/canvas.c
+++ b/src/canvas.c
@@ -483,22 +483,23 @@ canvas_set_proj_path(Canvas *c, const char *path)
static Layer *
layer_create(uint w, uint h)
{
- Layer *res;
- int i;
+ Layer *res;
+ int i;
/* TODO: better error handling and maybe allocating together? */
- res = malloc(sizeof *res);
+ res = malloc(sizeof *res);
- res->pix = malloc(h * w * sizeof(* res->pix));
- if (res->pix == NULL) {
- fprintf(stderr, "Error creating layer\n");
- layer_destroy(res);
- return NULL;
- }
+ res->visible = 1;
+ res->pix = malloc(h * w * sizeof(* res->pix));
+ if (res->pix == NULL) {
+ fprintf(stderr, "Error creating layer\n");
+ layer_destroy(res);
+ return NULL;
+ }
- for (i = 0; i < w*h; ++i)
- res->pix[i] = 0;
+ for (i = 0; i < w*h; ++i)
+ res->pix[i] = 0;
- return res;
+ return res;
}
static void
@@ -575,7 +576,8 @@ canvas_point_redraw(Canvas *c, long int x, long int y)
c->pres_pix[COORD(x, y)] = 0;
for (i = 0; i < c->layer_arr_cnt; i++)
- c->pres_pix[COORD(x, y)] = canvas_blend_color(c->pres_pix[COORD(x, y)], c->layers[i]->pix[COORD(x, y)]);
+ if (c->layers[i]->visible)
+ c->pres_pix[COORD(x, y)] = canvas_blend_color(c->pres_pix[COORD(x, y)], c->layers[i]->pix[COORD(x, y)]);
}
static void
diff --git a/src/canvas.h b/src/canvas.h
index bc96ab9..7595473 100644
--- a/src/canvas.h
+++ b/src/canvas.h
@@ -5,6 +5,7 @@
struct Layer {
unsigned int *pix;
+ unsigned int visible;
};
struct Canvas {
diff --git a/src/ui.c b/src/ui.c
index 202abcb..f12becc 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -40,6 +40,10 @@ static const SDL_Rect ui_buttons_rect[] = {
{84, 82, 28, 28}, /* Undo */
{84, 110, 28, 28}, /* Redo */
};
+static const SDL_Rect ui_timeline_rect[] = {
+ {112, 0, 20, 20},
+ {112, 20, 20, 20},
+};
struct UIPanel {
uint8 type, redraw;
@@ -136,7 +140,7 @@ ui_create(const char *path)
/* ui_panel_divider_init(&main_ui->p_divs[0], 5, 10, 100, 1, &main_ui->p_canvas, &main_ui->p_timeline); */
ui_panel_divider_init(&main_ui->p_divs[0], 5, 5, 120, 0, &main_ui->p_divs[1], &main_ui->p_divs[2]);
ui_panel_divider_init(&main_ui->p_divs[1], 0, 8, 22+31*((tool_array_size()+2)/3), 3, &main_ui->p_palette, &main_ui->p_buttons);
- ui_panel_divider_init(&main_ui->p_divs[2], 0, 5, 70, 3, &main_ui->p_canvas, &main_ui->p_timeline);
+ ui_panel_divider_init(&main_ui->p_divs[2], 0, 5, 120, 3, &main_ui->p_canvas, &main_ui->p_timeline);
ui_resize();
}
@@ -285,6 +289,19 @@ ui_mousel_down(int x, int y)
break;
}
break;
+ case UI_PANELTYPE_TIMELINE:
+ if (cur_canvas == NULL)
+ return;
+ x = x - fpan->geom.x - 8;
+ y = y - fpan->geom.y - 8;
+ y /= 22;
+ if (x > 22) break;
+ if (y < 0 || y >= cur_canvas->layer_arr_cnt) break;
+ cur_canvas->layers[cur_canvas->layer_arr_cnt-1-y]->visible ^= 1;
+ canvas_refresh(cur_canvas);
+ ui_redraw_panel(UI_PANELTYPE_TIMELINE);
+ ui_redraw_panel(UI_PANELTYPE_CANVAS);
+ break;
}
}
@@ -362,6 +379,11 @@ ui_focus_panel(int x, int y)
&& x - r->x < r->w && y - r->y < r->h)
return &main_ui->p_canvas.head;
+ r = &main_ui->p_timeline.head.geom;
+ if (r->x <= x && r->y <= y
+ && x - r->x < r->w && y - r->y < r->h)
+ return &main_ui->p_timeline.head;
+
return NULL;
}
@@ -455,7 +477,7 @@ ui_panel_redraw(UIPanel *p)
ui_panel_buttons_redraw((UIPanelButtons *)p);
break;
case UI_PANELTYPE_TIMELINE:
- ui_panel_draw_frame(p);
+ ui_panel_timeline_redraw((UIPanelTimeline *)p);
break;
default:
fprintf(stderr, "%s:%d:ui_panel_redraw: No redraw fuction for %u\n", __FILE__, __LINE__, p->type);
@@ -638,6 +660,45 @@ ui_panel_timeline_init(UIPanelTimeline *p)
return ui_panel_init((UIPanel *)p, UI_PANELTYPE_TIMELINE);
}
+static uint8
+ui_panel_timeline_redraw(UIPanelTimeline *p)
+{
+ SDL_Rect dest;
+ int i;
+ dest.x = 6;
+ dest.y = 6;
+ dest.w = p->head.geom.w - 12;
+ dest.h = p->head.geom.h - 12;
+ SDL_RenderSetViewport(ren, &dest);
+ SDL_SetRenderDrawColor(ren, 199, 207, 221, 255);
+ SDL_RenderClear(ren);
+ if (cur_canvas != NULL) {
+ dest.x = 2;
+ dest.w = dest.h = 20;
+ dest.y = (cur_canvas->layer_arr_cnt - 1 - cur_canvas->cur_layer) * 22 + 2;
+ SDL_SetRenderDrawColor(ren, 101, 115, 146, 255);
+ SDL_RenderFillRect(ren, &dest);
+ SDL_SetRenderDrawColor(ren, 19, 19, 19, 255);
+ for (i = 0; i < cur_canvas->layer_arr_cnt; i++) {
+ dest.y = (cur_canvas->layer_arr_cnt - 1 - i) * 22 + 2;
+ if (cur_canvas->layers[i]->visible)
+ SDL_RenderCopy(ren, main_ui->theme, &ui_timeline_rect[0], &dest);
+ else
+ SDL_RenderCopy(ren, main_ui->theme, &ui_timeline_rect[1], &dest);
+ SDL_RenderDrawLine(ren, 2, i*22, 21, i*22);
+ SDL_RenderDrawLine(ren, 2, i*22+1, 21, i*22+1);
+ SDL_RenderDrawLine(ren, 0, i*22+2, 0, i*22+21);
+ SDL_RenderDrawLine(ren, 1, i*22+2, 1, i*22+21);
+ SDL_RenderDrawLine(ren, 2, i*22+22, 21, i*22+22);
+ SDL_RenderDrawLine(ren, 2, i*22+23, 21, i*22+23);
+ SDL_RenderDrawLine(ren, 22, i*22+2, 22, i*22+21);
+ SDL_RenderDrawLine(ren, 23, i*22+2, 23, i*22+21);
+ }
+ }
+ SDL_RenderSetViewport(ren, NULL);
+ ui_panel_draw_frame(&p->head);
+}
+
/* Panel divider functions */
static uint8
ui_panel_divider_init(UIPanelDivider *p, int fw, int lw, int of, uint8 st, UIPanel *p1, UIPanel *p2)
diff --git a/src/user.c b/src/user.c
index 927e852..dc2ffaf 100644
--- a/src/user.c
+++ b/src/user.c
@@ -83,6 +83,7 @@ user_canvas_open(const Arg *x)
if (cur_canvas == NULL)
puts("Error opening file");
ui_redraw_panel(UI_PANELTYPE_CANVAS);
+ ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}
void
@@ -123,6 +124,7 @@ user_canvas_create_new(const Arg *x)
cur_canvas = canvas_init(w, h, ren);
ui_redraw_panel(UI_PANELTYPE_CANVAS);
+ ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}
void
@@ -144,6 +146,7 @@ user_canvas_import_png(const Arg *x)
if (cur_canvas == NULL)
puts("Error opening file");
ui_redraw_panel(UI_PANELTYPE_CANVAS);
+ ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}
void
@@ -165,20 +168,25 @@ void
user_testing_layer_add(const Arg *x)
{
/* TODO: temp function */
- canvas_add_layer(cur_canvas, -1);
+ 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;
+ 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->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_CANVAS);
+ ui_redraw_panel(UI_PANELTYPE_TIMELINE);
}
void