feat: color picker

This commit is contained in:
beriff
2026-03-07 22:25:28 +07:00
parent 3a292ea351
commit e48858defe
9 changed files with 385 additions and 21 deletions

View File

@@ -1,9 +1,11 @@
#include "./applicationstate.h"
#include "glib.h"
#include "gtk/gtk.h"
#include "gtk/gtkrevealer.h"
#include "src/core/primitives.h"
#include "src/core/raster.h"
#include "src/ui/vektorcanvas.h"
#include "src/ui/widgets/colorwheel.h"
#include "src/util/color.h"
typedef struct button_tool_set_data {
@@ -19,7 +21,7 @@ static void appstate_set_tool(GtkButton* button, gpointer user_data) {
// setting tool makes the sub-tools menu to close
gtk_revealer_set_reveal_child(data->revealer, FALSE);
// setting tool also resets selected primitive
// setting tool also resets selected shape
data->state->selectedShape = NULL;
}
@@ -29,6 +31,18 @@ static void appstate_reveal_subtools(GtkButton* button, gpointer user_data) {
gtk_revealer_set_reveal_child(revealer, !visible);
}
static void appstate_on_color_change(VektorColorWheel* wheel, gpointer user_data) {
VektorColor c = vektor_color_wheel_get_color(wheel);
VektorAppState* appstate = (VektorAppState*)user_data;
appstate->currentColor = c;
if(appstate->selectedShape != NULL) {
appstate->selectedShape->style.stroke_color = c;
}
gtk_gl_area_queue_render(GTK_GL_AREA(appstate->widgetState->workspaceCanvas));
}
static void canvas_onclick(GtkGestureClick* gesture, int n_press, double x,
double y, gpointer user_data) {
@@ -46,7 +60,6 @@ static void canvas_onclick(GtkGestureClick* gesture, int n_press, double x,
V2 normalized_coords =
(V2){(2 * (x / widget_w)) - 1, 1 - (2 * (y / widget_h))};
g_debug("<%f , %f>", normalized_coords.x, normalized_coords.y);
vektor_appstate_canvas_click(state, normalized_coords.x,
normalized_coords.y);
gtk_gl_area_queue_render(GTK_GL_AREA(widget));
@@ -57,14 +70,14 @@ void vektor_appstate_canvas_click(VektorAppState* state, double x, double y) {
begin_click_dispatch:
if (state->selectedTool == VektorLineTool) {
// create new polyline primitive if none is selected
// create new polyline shape if none is selected
if (state->selectedShape == NULL) {
VektorPolyline* line = vektor_polyline_new();
VektorPrimitive linePrimitive =
(VektorPrimitive){.kind = VEKTOR_POLYLINE, .polyline = line};
VektorStyle style =
(VektorStyle){.stroke_color = (VektorColor){255, 0, 0, 1},
(VektorStyle){.stroke_color = state->currentColor,
.stroke_width = 0.01};
VektorShape shape = (VektorShape){
.primitive = linePrimitive, .z_index = 0, .style = style};
@@ -96,6 +109,8 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
stateOut->shapeBuffer = malloc(sizeof(VektorShapeBuffer));
*stateOut->shapeBuffer = (VektorShapeBuffer){0};
stateOut->canvas = malloc(sizeof(VektorCanvas));
stateOut->widgetState = wstate;
stateOut->currentColor = vektor_color_blank;
vektor_canvas_init(wstate, stateOut->canvas, stateOut->shapeBuffer);
// link all the buttons
@@ -111,6 +126,10 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
G_CALLBACK(appstate_reveal_subtools),
wstate->workspaceRevealerShapes);
// hook relevant stuff to master color picker
g_signal_connect(G_OBJECT(wstate->workspaceColorPicker), "color-changed",
G_CALLBACK(appstate_on_color_change), stateOut);
// Add click gesture to canvas
GtkGesture* canvasClickGesture = gtk_gesture_click_new();
g_signal_connect(G_OBJECT(canvasClickGesture), "pressed",

View File

@@ -9,9 +9,13 @@
typedef enum VektorAppTool { VektorLineTool } VektorAppTool;
typedef struct VektorAppState {
VektorWidgetState* widgetState;
VektorAppTool selectedTool;
VektorShape* selectedShape;
VektorColor currentColor;
// Logic space
VektorShapeBuffer* shapeBuffer;
// View space