From a8c8be37ca996f3e1b752bca67eee594706bc08f Mon Sep 17 00:00:00 2001 From: Jeremy Attali Date: Thu, 26 Dec 2019 16:44:11 -0500 Subject: [PATCH] feat(ui): life is full of colors and joy --- include/application.h | 9 +++ include/swappy.h | 13 ++++ res/style/swappy.css | 16 +++++ res/swappy.ui | 146 +++++++++++++++++++++++++++++++++++++++++- src/application.c | 58 ++++++++++++++++- src/paint.c | 26 +++++--- 6 files changed, 256 insertions(+), 12 deletions(-) diff --git a/include/application.h b/include/application.h index bbdae59..89eb4b0 100644 --- a/include/application.h +++ b/include/application.h @@ -34,3 +34,12 @@ void arrow_clicked_handler(GtkWidget *widget, struct swappy_state *state); void copy_clicked_handler(GtkWidget *widget, struct swappy_state *state); void save_clicked_handler(GtkWidget *widget, struct swappy_state *state); void clear_clicked_handler(GtkWidget *widget, struct swappy_state *state); + +void color_red_clicked_handler(GtkWidget *widget, struct swappy_state *state); +void color_green_clicked_handler(GtkWidget *widget, struct swappy_state *state); +void color_blue_clicked_handler(GtkWidget *widget, struct swappy_state *state); + +void color_custom_clicked_handler(GtkWidget *widget, + struct swappy_state *state); +void color_custom_color_set_handler(GtkWidget *widget, + struct swappy_state *state); \ No newline at end of file diff --git a/include/swappy.h b/include/swappy.h index e7348fe..8771702 100644 --- a/include/swappy.h +++ b/include/swappy.h @@ -64,6 +64,14 @@ struct swappy_box { int32_t height; }; +struct swappy_state_painting { + double r; + double g; + double b; + double a; + double w; +}; + struct swappy_state_ui { GtkWindow *window; GtkWidget *area; @@ -79,6 +87,9 @@ struct swappy_state_ui { GtkRadioButton *rectangle; GtkRadioButton *ellipse; GtkRadioButton *arrow; + + GtkRadioButton *red; + GtkColorButton *custom; }; struct swappy_state { @@ -114,6 +125,8 @@ struct swappy_state { GSList *redo_paints; struct swappy_paint *temp_paint; + struct swappy_state_painting painting; + int argc; char **argv; }; diff --git a/res/style/swappy.css b/res/style/swappy.css index e5e379c..31a71a5 100644 --- a/res/style/swappy.css +++ b/res/style/swappy.css @@ -1,3 +1,19 @@ .drawing .text-button { font-family: "FontAwesome 5 Free Solid"; } + +.color-box image { + border-radius: 50px; +} + +.color-box .color-red image { + background-color: rgb(255, 0, 0); +} + +.color-box .color-green image { + background-color: rgb(0, 255, 0); +} + +.color-box .color-blue image { + background-color: rgb(0, 0, 255); +} diff --git a/res/swappy.ui b/res/swappy.ui index 10bf9ee..40a188c 100644 --- a/res/swappy.ui +++ b/res/swappy.ui @@ -56,8 +56,8 @@ edit-redo True - + True @@ -233,6 +233,7 @@ True False + 15 True @@ -324,6 +325,149 @@ 1 + + + True + False + center + vertical + + + True + False + start + baseline + 10 + + + True + True + True + center + False + + + + True + False + + + + + + False + True + 0 + + + + + True + True + True + center + False + color-red-button + + + + True + False + + + + + + False + True + 1 + + + + + True + True + True + center + False + color-red-button + + + + True + False + + + + + + False + True + 2 + + + + + True + True + True + center + 20 + False + color-red-button + + + + True + False + gtk-color-picker + + + + + False + True + 3 + + + + + True + False + True + True + center + + + + False + True + 4 + + + + + + False + True + 0 + + + + + False + True + 2 + + False diff --git a/src/application.c b/src/application.c index e3d9a31..b2bb183 100644 --- a/src/application.c +++ b/src/application.c @@ -82,6 +82,13 @@ static void switch_mode_to_arrow(struct swappy_state *state) { state->mode = SWAPPY_PAINT_MODE_ARROW; } +static void action_update_color_state(struct swappy_state *state, double r, + double g, double b) { + state->painting.r = r; + state->painting.g = g; + state->painting.b = b; +} + void brush_clicked_handler(GtkWidget *widget, struct swappy_state *state) { switch_mode_to_brush(state); } @@ -319,9 +326,47 @@ void draw_area_button_release_handler(GtkWidget *widget, GdkEventButton *event, } } +void color_red_clicked_handler(GtkWidget *widget, struct swappy_state *state) { + action_update_color_state(state, 1, 0, 0); + gtk_widget_set_sensitive(GTK_WIDGET(state->ui->custom), false); +} + +void color_green_clicked_handler(GtkWidget *widget, + struct swappy_state *state) { + action_update_color_state(state, 0, 1, 0); + gtk_widget_set_sensitive(GTK_WIDGET(state->ui->custom), false); +} + +void color_blue_clicked_handler(GtkWidget *widget, struct swappy_state *state) { + action_update_color_state(state, 0, 0, 1); + gtk_widget_set_sensitive(GTK_WIDGET(state->ui->custom), false); +} + +void color_custom_clicked_handler(GtkWidget *widget, + struct swappy_state *state) { + gtk_widget_set_sensitive(GTK_WIDGET(state->ui->custom), true); +} + +void color_group_set_inactive(gpointer data, gpointer user_data) { + GtkToggleButton *button = GTK_TOGGLE_BUTTON(data); + + gtk_toggle_button_set_active(button, false); +} + +void color_custom_color_set_handler(GtkWidget *widget, + struct swappy_state *state) { + GdkRGBA color; + gtk_color_chooser_get_rgba(GTK_COLOR_CHOOSER(widget), &color); + + state->painting.r = color.red; + state->painting.g = color.green; + state->painting.b = color.blue; + state->painting.a = color.alpha; +} + static void apply_css(GtkWidget *widget, GtkStyleProvider *provider) { gtk_style_context_add_provider(gtk_widget_get_style_context(widget), provider, - 1); + GTK_STYLE_PROVIDER_PRIORITY_APPLICATION); if (GTK_IS_CONTAINER(widget)) { gtk_container_forall(GTK_CONTAINER(widget), (GtkCallback)apply_css, provider); @@ -371,6 +416,11 @@ static bool load_layout(struct swappy_state *state) { GtkRadioButton *arrow = GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "arrow")); + state->ui->red = + GTK_RADIO_BUTTON(gtk_builder_get_object(builder, "color-red-button")); + state->ui->custom = + GTK_COLOR_BUTTON(gtk_builder_get_object(builder, "custom-color-button")); + // gtk_popover_set_relative_to(ui, area); gtk_widget_set_size_request(area, geometry->width, geometry->height); @@ -462,6 +512,12 @@ bool application_init(struct swappy_state *state) { g_signal_connect(state->app, "command-line", G_CALLBACK(command_line_handler), state); + state->painting.r = 1; + state->painting.g = 0; + state->painting.b = 0; + state->painting.a = 1; + state->painting.w = 2; + return true; } diff --git a/src/paint.c b/src/paint.c index 6cec313..b282a2c 100644 --- a/src/paint.c +++ b/src/paint.c @@ -35,17 +35,23 @@ void paint_add_temporary(struct swappy_state *state, double x, double y, struct swappy_paint *paint = g_new(struct swappy_paint, 1); struct swappy_point *brush; + double r = state->painting.r; + double g = state->painting.g; + double b = state->painting.b; + double a = state->painting.a; + double w = state->painting.w; + 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; + paint->content.brush.r = r; + paint->content.brush.g = g; + paint->content.brush.b = b; + paint->content.brush.a = a; + paint->content.brush.w = w; brush = g_new(struct swappy_point, 1); brush->x = x; @@ -60,11 +66,11 @@ void paint_add_temporary(struct swappy_state *state, double x, double y, 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.r = r; + paint->content.shape.g = g; + paint->content.shape.b = b; + paint->content.shape.a = a; + paint->content.shape.w = w; paint->content.shape.type = type; break; default: