refactor(paint): move painting code to paint.c

This commit is contained in:
Jeremy Attali 2019-12-23 18:19:20 -05:00
parent 90958ab8fd
commit 4922feaa51
4 changed files with 138 additions and 125 deletions

11
include/paint.h Normal file
View File

@ -0,0 +1,11 @@
#pragma once
#include "swappy.h"
void paint_add_temporary(struct swappy_state *state, double x, double y,
enum swappy_paint_type type);
void paint_update_temporary(struct swappy_state *state, double x, double y);
void paint_commit_temporary(struct swappy_state *state);
void paint_free(gpointer data);
void paint_free_all(struct swappy_state *state);

View File

@ -39,6 +39,7 @@ executable(
'src/box.c',
'src/config.c',
'src/clipboard.c',
'src/paint.c',
'src/render.c',
'src/screencopy.c',
'src/wayland.c',

View File

@ -6,37 +6,12 @@
#include "clipboard.h"
#include "notification.h"
#include "paint.h"
#include "render.h"
#include "screencopy.h"
#include "swappy.h"
#include "wayland.h"
static void paint_free(gpointer data) {
struct swappy_paint *paint = (struct swappy_paint *)data;
if (paint == NULL) {
return;
}
switch (paint->type) {
case SWAPPY_PAINT_MODE_BRUSH:
g_slist_free_full(paint->content.brush.points, g_free);
break;
default:
g_free(paint);
}
}
static void paint_free_all(struct swappy_state *state) {
if (state->paints) {
g_slist_free_full(state->paints, paint_free);
state->paints = NULL;
}
paint_free(state->temp_paint);
state->temp_paint = NULL;
}
static void switch_mode_to_brush(struct swappy_state *state) {
g_debug("switching mode to brush");
state->mode = SWAPPY_PAINT_MODE_BRUSH;
@ -241,105 +216,6 @@ static void keypress_handler(GtkWidget *widget, GdkEventKey *event,
}
}
static void paint_add_temporary(struct swappy_state *state, double x, double y,
enum swappy_paint_type type) {
struct swappy_paint *paint = g_new(struct swappy_paint, 1);
struct swappy_point *brush;
paint->type = type;
switch (type) {
case SWAPPY_PAINT_MODE_BRUSH:
paint->can_draw = true;
paint->content.brush.r = 1;
paint->content.brush.g = 0;
paint->content.brush.b = 0;
paint->content.brush.a = 1;
paint->content.brush.w = 2;
brush = g_new(struct swappy_point, 1);
brush->x = x;
brush->y = y;
paint->content.brush.points = g_slist_append(NULL, brush);
break;
case SWAPPY_PAINT_MODE_RECTANGLE:
case SWAPPY_PAINT_MODE_ELLIPSE:
case SWAPPY_PAINT_MODE_ARROW:
paint->can_draw = false; // need `to` vector
paint->content.shape.from.x = x;
paint->content.shape.from.y = y;
paint->content.shape.r = 1;
paint->content.shape.g = 0;
paint->content.shape.b = 0;
paint->content.shape.a = 1;
paint->content.shape.w = 2;
paint->content.shape.type = type;
break;
default:
g_info("unable to add temporary paint: %d", type);
break;
}
if (state->temp_paint) {
g_free(state->temp_paint);
}
state->temp_paint = paint;
}
static void paint_update_temporary(struct swappy_state *state, double x,
double y) {
struct swappy_paint *paint = state->temp_paint;
struct swappy_point *brush;
GSList *points;
if (!paint) {
return;
}
switch (paint->type) {
case SWAPPY_PAINT_MODE_BRUSH:
points = paint->content.brush.points;
brush = g_new(struct swappy_point, 1);
brush->x = x;
brush->y = y;
paint->content.brush.points = g_slist_append(points, brush);
break;
case SWAPPY_PAINT_MODE_RECTANGLE:
case SWAPPY_PAINT_MODE_ELLIPSE:
case SWAPPY_PAINT_MODE_ARROW:
paint->content.shape.to.x = x;
paint->content.shape.to.y = y;
paint->can_draw = true; // all set
break;
default:
g_info("unable to update temporary paint: %d", paint->type);
break;
}
}
static void paint_commit_temporary(struct swappy_state *state) {
struct swappy_paint *paint = state->temp_paint;
if (!paint) {
return;
}
if (!paint->can_draw) {
paint_free(paint);
} else {
state->paints = g_slist_append(state->paints, paint);
}
// Set the temporary paint to NULL but keep the content in memory
// because it's now part of the GSList.
state->temp_paint = NULL;
}
static void draw_area_button_press_handler(GtkWidget *widget,
GdkEventButton *event,
struct swappy_state *state) {

125
src/paint.c Normal file
View File

@ -0,0 +1,125 @@
#include "paint.h"
void paint_free(gpointer data) {
struct swappy_paint *paint = (struct swappy_paint *)data;
if (paint == NULL) {
return;
}
switch (paint->type) {
case SWAPPY_PAINT_MODE_BRUSH:
g_slist_free_full(paint->content.brush.points, g_free);
break;
default:
g_free(paint);
}
}
void paint_free_all(struct swappy_state *state) {
if (state->paints) {
g_slist_free_full(state->paints, paint_free);
state->paints = NULL;
}
paint_free(state->temp_paint);
state->temp_paint = NULL;
}
void paint_add_temporary(struct swappy_state *state, double x, double y,
enum swappy_paint_type type) {
struct swappy_paint *paint = g_new(struct swappy_paint, 1);
struct swappy_point *brush;
paint->type = type;
switch (type) {
case SWAPPY_PAINT_MODE_BRUSH:
paint->can_draw = true;
paint->content.brush.r = 1;
paint->content.brush.g = 0;
paint->content.brush.b = 0;
paint->content.brush.a = 1;
paint->content.brush.w = 2;
brush = g_new(struct swappy_point, 1);
brush->x = x;
brush->y = y;
paint->content.brush.points = g_slist_append(NULL, brush);
break;
case SWAPPY_PAINT_MODE_RECTANGLE:
case SWAPPY_PAINT_MODE_ELLIPSE:
case SWAPPY_PAINT_MODE_ARROW:
paint->can_draw = false; // need `to` vector
paint->content.shape.from.x = x;
paint->content.shape.from.y = y;
paint->content.shape.r = 1;
paint->content.shape.g = 0;
paint->content.shape.b = 0;
paint->content.shape.a = 1;
paint->content.shape.w = 2;
paint->content.shape.type = type;
break;
default:
g_info("unable to add temporary paint: %d", type);
break;
}
if (state->temp_paint) {
g_free(state->temp_paint);
}
state->temp_paint = paint;
}
void paint_update_temporary(struct swappy_state *state, double x, double y) {
struct swappy_paint *paint = state->temp_paint;
struct swappy_point *brush;
GSList *points;
if (!paint) {
return;
}
switch (paint->type) {
case SWAPPY_PAINT_MODE_BRUSH:
points = paint->content.brush.points;
brush = g_new(struct swappy_point, 1);
brush->x = x;
brush->y = y;
paint->content.brush.points = g_slist_append(points, brush);
break;
case SWAPPY_PAINT_MODE_RECTANGLE:
case SWAPPY_PAINT_MODE_ELLIPSE:
case SWAPPY_PAINT_MODE_ARROW:
paint->content.shape.to.x = x;
paint->content.shape.to.y = y;
paint->can_draw = true; // all set
break;
default:
g_info("unable to update temporary paint: %d", paint->type);
break;
}
}
void paint_commit_temporary(struct swappy_state *state) {
struct swappy_paint *paint = state->temp_paint;
if (!paint) {
return;
}
if (!paint->can_draw) {
paint_free(paint);
} else {
state->paints = g_slist_append(state->paints, paint);
}
// Set the temporary paint to NULL but keep the content in memory
// because it's now part of the GSList.
state->temp_paint = NULL;
}