feat: add polygon tool

This commit is contained in:
Beriff
2026-03-09 23:26:12 +07:00
parent 9b4248981e
commit 2bdcbfae1f
8 changed files with 93 additions and 41 deletions

View File

@@ -0,0 +1,9 @@
<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" version="1.1">
<defs>
<style id="current-color-scheme" type="text/css">
.ColorScheme-Text { color:#444444; } .ColorScheme-Highlight { color:#4285f4; } .ColorScheme-NeutralText { color:#ff9800; } .ColorScheme-PositiveText { color:#4caf50; } .ColorScheme-NegativeText { color:#f44336; }
</style>
</defs>
<path style="fill:currentColor" class="ColorScheme-Text" d="M 16.98,6.5 5.9551,7.502 5.5,8 v 10 l 0.8203,0.385 5.7481,-4.7912 5.7086,2.8532 0.719,-0.502 -1,-8.9997 z m -0.423,1.043 0.845,7.6 -5.1793,-2.5903 -0.543,0.0625 L 6.5,16.932 V 8.457 Z"/>
<path style="fill:currentColor" class="ColorScheme-Text" d="m 14,13 a 2,2 0 0 1 -2,2 2,2 0 0 1 -2,-2 2,2 0 0 1 2,-2 2,2 0 0 1 2,2 z M 8,8 A 2,2 0 0 1 6,10 2,2 0 0 1 4,8 2,2 0 0 1 6,6 2,2 0 0 1 8,8 Z m 0,10 a 2,2 0 0 1 -2,2 2,2 0 0 1 -2,-2 2,2 0 0 1 2,-2 2,2 0 0 1 2,2 z m 12,-2 a 2,2 0 0 1 -2,2 2,2 0 0 1 -2,-2 2,2 0 0 1 2,-2 2,2 0 0 1 2,2 z M 19,7 a 2,2 0 0 1 -2,2 2,2 0 0 1 -2,-2 2,2 0 0 1 2,-2 2,2 0 0 1 2,2 z"/>
</svg>

After

Width:  |  Height:  |  Size: 1.0 KiB

View File

@@ -34,12 +34,13 @@ 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) {
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) {
if (appstate->selectedShape != NULL) {
appstate->selectedShape->style.stroke_color = c;
}
@@ -48,24 +49,30 @@ static void appstate_on_color_change(VektorColorWheel* wheel, gpointer user_data
str_r = g_strdup_printf("%d", c.r);
str_g = g_strdup_printf("%d", c.g);
str_b = g_strdup_printf("%d", c.b);
gtk_editable_set_text(GTK_EDITABLE(appstate->widgetState->sidepanelEntryR), str_r);
gtk_editable_set_text(GTK_EDITABLE(appstate->widgetState->sidepanelEntryG), str_g);
gtk_editable_set_text(GTK_EDITABLE(appstate->widgetState->sidepanelEntryB), str_b);
gtk_editable_set_text(GTK_EDITABLE(appstate->widgetState->sidepanelEntryR),
str_r);
gtk_editable_set_text(GTK_EDITABLE(appstate->widgetState->sidepanelEntryG),
str_g);
gtk_editable_set_text(GTK_EDITABLE(appstate->widgetState->sidepanelEntryB),
str_b);
gtk_gl_area_queue_render(GTK_GL_AREA(appstate->widgetState->workspaceCanvas));
gtk_gl_area_queue_render(
GTK_GL_AREA(appstate->widgetState->workspaceCanvas));
}
static void appstate_on_entry_update(GtkEntry* entry, gpointer user_data) {
VektorWidgetState* widgetState = (VektorWidgetState*)user_data;
unsigned char r = (unsigned char)atoi(gtk_editable_get_text(GTK_EDITABLE(widgetState->sidepanelEntryR)));
unsigned char g = (unsigned char)atoi(gtk_editable_get_text(GTK_EDITABLE(widgetState->sidepanelEntryG)));
unsigned char b = (unsigned char)atoi(gtk_editable_get_text(GTK_EDITABLE(widgetState->sidepanelEntryB)));
unsigned char r = (unsigned char)atoi(
gtk_editable_get_text(GTK_EDITABLE(widgetState->sidepanelEntryR)));
unsigned char g = (unsigned char)atoi(
gtk_editable_get_text(GTK_EDITABLE(widgetState->sidepanelEntryG)));
unsigned char b = (unsigned char)atoi(
gtk_editable_get_text(GTK_EDITABLE(widgetState->sidepanelEntryB)));
g_print("%d", r);
vektor_color_wheel_set_color(
VEKTOR_COLOR_WHEEL(widgetState->workspaceColorPicker),
(VektorColor){.r = r, .g = g, .b = b}
);
(VektorColor){.r = r, .g = g, .b = b});
}
static void canvas_onclick(GtkGestureClick* gesture, int n_press, double x,
@@ -101,13 +108,11 @@ begin_click_dispatch:
VektorPolyline* line = vektor_polyline_new();
VektorPrimitive linePrimitive =
(VektorPrimitive){.kind = VEKTOR_POLYLINE, .polyline = line};
VektorStyle style =
(VektorStyle){.stroke_color = state->currentColor,
.stroke_width = 0.01};
VektorShape shape = (VektorShape){
.primitive = linePrimitive, .z_index = 0, .style = style};
vektor_shapebuffer_add_shape(state->shapeBuffer, vektor_shape_new(linePrimitive, style, 0));
VektorStyle style = (VektorStyle){
.stroke_color = state->currentColor, .stroke_width = 0.01};
vektor_shapebuffer_add_shape(
state->shapeBuffer, vektor_shape_new(linePrimitive, style, 0));
state->selectedShape =
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
@@ -120,15 +125,32 @@ begin_click_dispatch:
goto begin_click_dispatch; // retry
}
vektor_polyline_add_point(state->selectedShape->primitive.polyline,
pos);
vektor_polyline_add_point(state->selectedShape->primitive.polyline, pos);
vektor_shapes_update_bbox(state->shapeBuffer);
for (size_t i = 0; i < state->shapeBuffer->count; i++) {
g_print("<%f,%f>-<%f,%f>\n", state->shapeBuffer->shapes[i].bbox.min.x, state->shapeBuffer->shapes[i].bbox.min.y, state->shapeBuffer->shapes[i].bbox.max.x, state->shapeBuffer->shapes[i].bbox.max.y);
}
else if (state->selectedTool == VektorPolygonTool) {
// create new polygon shape if none is selected
if (state->selectedShape == NULL) {
VektorPolygon* polygon = vektor_polygon_new();
VektorPrimitive polygonPrimitive =
(VektorPrimitive){.kind = VEKTOR_POLYGON, .polygon = polygon};
VektorStyle style = (VektorStyle){
.stroke_color = state->currentColor, .stroke_width = 0.01};
vektor_shapebuffer_add_shape(
state->shapeBuffer, vektor_shape_new(polygonPrimitive, style, 0));
state->selectedShape =
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
} else if (state->selectedShape->primitive.kind != VEKTOR_POLYGON) {
g_warning("Invalid selected primitive; polygon expected");
state->selectedShape = NULL;
goto begin_click_dispatch; // retry
}
vektor_polygon_add_point(state->selectedShape->primitive.polygon, pos);
vektor_shapes_update_bbox(state->shapeBuffer);
}
}
@@ -138,6 +160,11 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
data_linetool->tool = VektorLineTool;
data_linetool->revealer = wstate->workspaceRevealerShapes;
button_tool_set_data* data_polygontool = malloc(sizeof(button_tool_set_data));
data_polygontool->state = stateOut;
data_polygontool->tool = VektorPolygonTool;
data_polygontool->revealer = wstate->workspaceRevealerShapes;
// populate appstate
stateOut->shapeBuffer = malloc(sizeof(VektorShapeBuffer));
*stateOut->shapeBuffer = (VektorShapeBuffer){0};
@@ -154,6 +181,8 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
G_CALLBACK(appstate_set_tool), data_linetool);
g_signal_connect(G_OBJECT(wstate->workspaceButtonCircletool), "clicked",
G_CALLBACK(appstate_set_tool), data_linetool);
g_signal_connect(G_OBJECT(wstate->workspaceButtonPolygontool), "clicked",
G_CALLBACK(appstate_set_tool), data_polygontool);
// hook subtool revealers to their master buttons
g_signal_connect(G_OBJECT(wstate->workspaceButtonMasterShapes), "clicked",
@@ -166,12 +195,14 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
// hook rgb entries change
g_signal_connect(G_OBJECT(wstate->sidepanelEntryR), "activate",
G_CALLBACK(appstate_on_entry_update), stateOut->widgetState);
G_CALLBACK(appstate_on_entry_update),
stateOut->widgetState);
g_signal_connect(G_OBJECT(wstate->sidepanelEntryG), "activate",
G_CALLBACK(appstate_on_entry_update), stateOut->widgetState);
G_CALLBACK(appstate_on_entry_update),
stateOut->widgetState);
g_signal_connect(G_OBJECT(wstate->sidepanelEntryB), "activate",
G_CALLBACK(appstate_on_entry_update), stateOut->widgetState);
G_CALLBACK(appstate_on_entry_update),
stateOut->widgetState);
// Add click gesture to canvas
GtkGesture* canvasClickGesture = gtk_gesture_click_new();

View File

@@ -6,7 +6,10 @@
#include "../ui/vektorcanvas.h"
#include "src/core/raster.h"
typedef enum VektorAppTool { VektorLineTool } VektorAppTool;
typedef enum VektorAppTool {
VektorLineTool,
VektorPolygonTool,
} VektorAppTool;
typedef struct VektorAppState {
VektorWidgetState* widgetState;

View File

@@ -20,17 +20,14 @@ void vektor_polyline_flatten(EdgeBuffer* buffer, VektorPolyline* line,
}
}
void vektor_polygon_flatten(EdgeBuffer* buffer, VektorPolygon* pg, size_t j) {
size_t n = pg->count;
if (n < 3)
return;
for (size_t i = 0; i < n; i++) {
V2 p1 = pg->points[i];
V2 p2 = pg->points[(i + 1) % n];
int winding = (p1.y < p2.y) ? +1 : -1;
vektor_edgebuffer_add_edge(buffer, (Edge){p1, p2, winding, j});
void vektor_polygon_flatten(EdgeBuffer* buffer, VektorPolygon* polygon,
size_t j) {
for (size_t i = 0; i + 1 < polygon->count; i++) {
vektor_edgebuffer_add_edge(
buffer, (Edge){polygon->points[i], polygon->points[i + 1], 0, j});
}
vektor_edgebuffer_add_edge(
buffer, (Edge){polygon->points[polygon->count - 1], polygon->points[0], 0, j});
}
void vektor_rasterize(VertexBuffer* vb, VektorShapeBuffer* shapes) {

View File

@@ -56,6 +56,8 @@ void vektor_uictrl_init(GtkApplication* app, VektorWidgetState* stateOut) {
GTK_BUTTON(gtk_builder_get_object(builder, "button_rectangletool"));
stateOut->workspaceButtonCircletool =
GTK_BUTTON(gtk_builder_get_object(builder, "button_circletool"));
stateOut->workspaceButtonPolygontool =
GTK_BUTTON(gtk_builder_get_object(builder, "button_polygontool"));
stateOut->workspaceColorPicker =
VEKTOR_COLOR_WHEEL(gtk_builder_get_object(builder, "color_picker"));

View File

@@ -20,6 +20,7 @@ typedef struct VektorWidgetState {
GtkButton* workspaceButtonLinetool;
GtkButton* workspaceButtonRecttool;
GtkButton* workspaceButtonCircletool;
GtkButton* workspaceButtonPolygontool;
VektorColorWheel* workspaceColorPicker;

View File

@@ -94,6 +94,8 @@ static void init_geometry(void) {
}
static gboolean render(GtkGLArea* area, GdkGLContext* context,
VektorShapeBuffer* prims) {
vb.count = 0;
vektor_rasterize(&vb, prims);
glBufferData(GL_ARRAY_BUFFER, vb.count * sizeof(Vertex), vb.vertices,

View File

@@ -101,6 +101,13 @@
</object>
</child>
<!--Polygon tool-->
<child>
<object class="GtkButton" id="button_polygontool">
<property name="icon-name">tool-polygon-symbolic</property>
</object>
</child>
<!--Circle tool-->
<child>
<object class="GtkButton" id="button_circletool">