feat: add stroke thickness

This commit is contained in:
2026-03-05 00:51:20 +05:30
parent 8ac783e6e0
commit eefd95e4d2
15 changed files with 379 additions and 356 deletions

View File

@@ -1,42 +1,61 @@
#include "./applicationstate.h"
#include "glib.h"
#include "src/core/primitives.h"
#include "src/core/raster.h"
#include "src/ui/vektorcanvas.h"
typedef struct button_tool_set_data {
VektorAppState* state;
VektorAppState *state;
VektorAppTool tool;
} button_tool_set_data;
static void appstate_set_tool(GtkButton* button, gpointer user_data) {
button_tool_set_data* data = (button_tool_set_data*)user_data;
static void appstate_set_tool(GtkButton *button, gpointer user_data) {
button_tool_set_data *data = (button_tool_set_data *)user_data;
data->state->selectedTool = data->tool;
// setting tool also resets selected primitive
data->state->selectedPrimitive = NULL;
}
static void canvas_onclick(GtkGestureClick* gesture, int n_press, double x, double y, gpointer user_data) {
vektor_appstate_canvas_click(user_data, x, y);
static void canvas_onclick(GtkGestureClick *gesture, int n_press, double x,
double y, gpointer user_data) {
VektorAppState *state = user_data;
GtkWidget *widget =
gtk_event_controller_get_widget(GTK_EVENT_CONTROLLER(gesture));
int widget_w = gtk_widget_get_width(widget);
int widget_h = gtk_widget_get_height(widget);
int canvas_w = state->canvas->width;
int canvas_h = state->canvas->height;
double sx = canvas_w / (double)widget_w;
double sy = canvas_h / (double)widget_h;
g_debug("<%f , %f>", x * sx, y * sy);
vektor_appstate_canvas_click(state, x * sx, y * sy);
}
void vektor_appstate_canvas_click(VektorAppState* state, double x, double y) {
V2 pos = (V2){x,y};
void vektor_appstate_canvas_click(VektorAppState *state, double x, double y) {
V2 pos = (V2){x, y};
begin_click_dispatch:
if(state->selectedTool == VektorLineTool) {
begin_click_dispatch:
if (state->selectedTool == VektorLineTool) {
// create new polyline primitive if none is selected
if(state->selectedPrimitive == NULL) {
VektorPolyline* line = vektor_polyline_new();
VektorPrimitive linePrimitive = (VektorPrimitive){
.kind = VEKTOR_POLYLINE, .polyline = line
};
vektor_primitivebuffer_add_primitive(state->primitiveBuffer, linePrimitive);
state->selectedPrimitive =
&(state->primitiveBuffer->primitives[state->primitiveBuffer->count - 1]);
if (state->selectedPrimitive == NULL) {
VektorPolyline *line = vektor_polyline_new();
VektorPrimitive linePrimitive =
(VektorPrimitive){.kind = VEKTOR_POLYLINE, .polyline = line};
vektor_primitivebuffer_add_primitive(state->primitiveBuffer,
linePrimitive);
state->selectedPrimitive =
&(state->primitiveBuffer
->primitives[state->primitiveBuffer->count - 1]);
} else if (state->selectedPrimitive->kind != VEKTOR_POLYLINE) {
// selecting a tool resets the selection, so this condition
// should not happen
@@ -44,8 +63,7 @@ void vektor_appstate_canvas_click(VektorAppState* state, double x, double y) {
state->selectedPrimitive = NULL;
goto begin_click_dispatch; // retry
}
vektor_polyline_add_point(state->selectedPrimitive->polyline, pos);
}
@@ -54,8 +72,8 @@ void vektor_appstate_canvas_click(VektorAppState* state, double x, double y) {
vektor_canvas_update(state->canvas);
}
void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
button_tool_set_data* data_linetool = malloc(sizeof(button_tool_set_data));
void vektor_appstate_new(VektorWidgetState *wstate, VektorAppState *stateOut) {
button_tool_set_data *data_linetool = malloc(sizeof(button_tool_set_data));
data_linetool->state = stateOut;
data_linetool->tool = VektorLineTool;
@@ -68,18 +86,13 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
vektor_canvas_init(wstate, stateOut->canvas);
// link all the buttons
g_signal_connect(
G_OBJECT(wstate->workspaceButtonLinetool),
"clicked", G_CALLBACK(appstate_set_tool), data_linetool);
g_signal_connect(G_OBJECT(wstate->workspaceButtonLinetool), "clicked",
G_CALLBACK(appstate_set_tool), data_linetool);
// Add click gesture to canvas
GtkGesture* canvasClickGesture = gtk_gesture_click_new();
g_signal_connect(
G_OBJECT(canvasClickGesture),
"pressed", G_CALLBACK(canvas_onclick), stateOut
);
gtk_widget_add_controller(
GTK_WIDGET(wstate->workspaceCanvas),
GTK_EVENT_CONTROLLER(canvasClickGesture)
);
GtkGesture *canvasClickGesture = gtk_gesture_click_new();
g_signal_connect(G_OBJECT(canvasClickGesture), "pressed",
G_CALLBACK(canvas_onclick), stateOut);
gtk_widget_add_controller(GTK_WIDGET(wstate->workspaceCanvas),
GTK_EVENT_CONTROLLER(canvasClickGesture));
}

View File

@@ -1,29 +1,27 @@
#ifndef VKTR_APPSTATE_H
#define VKTR_APPSTATE_H
#include "../ui/uicontroller.h"
#include "../core/primitives.h"
#include "src/core/raster.h"
#include "../ui/uicontroller.h"
#include "../ui/vektorcanvas.h"
#include "src/core/raster.h"
typedef enum VektorAppTool {
VektorLineTool
} VektorAppTool;
typedef enum VektorAppTool { VektorLineTool } VektorAppTool;
typedef struct VektorAppState {
VektorAppTool selectedTool;
VektorPrimitive* selectedPrimitive;
VektorPrimitive *selectedPrimitive;
// Logic space
VektorPrimitiveBuffer* primitiveBuffer;
VektorPrimitiveBuffer *primitiveBuffer;
// Pixel space
VektorFramebuffer* frameBuffer;
VektorFramebuffer *frameBuffer;
// View space
VektorCanvas* canvas;
VektorCanvas *canvas;
} VektorAppState;
void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut);
void vektor_appstate_canvas_click(VektorAppState* state, double x, double y);
void vektor_appstate_new(VektorWidgetState *wstate, VektorAppState *stateOut);
void vektor_appstate_canvas_click(VektorAppState *state, double x, double y);
#endif