diff --git a/src/application/applicationstate.c b/src/application/applicationstate.c index 3993b0d..5f973fb 100644 --- a/src/application/applicationstate.c +++ b/src/application/applicationstate.c @@ -24,14 +24,12 @@ static void appstate_set_tool(GtkButton* button, gpointer user_data) { // setting tool makes the sub-tools menu to close // (ADD NEW REVEALERS HERE) gtk_revealer_set_reveal_child( - data->state->widgetState->workspaceRevealerShapes, - FALSE - ); + data->state->widgetState->workspaceRevealerShapes, FALSE); // setting tool also resets selected shape // NOTE: isn't needed anymore, as you would // want to be able to select & edit existing shapes - //data->state->selectedShape = NULL; + // data->state->selectedShape = NULL; } static void appstate_reveal_subtools(GtkButton* button, gpointer user_data) { @@ -100,7 +98,7 @@ static void canvas_onclick(GtkGestureClick* gesture, int n_press, double x, vektor_appstate_canvas_click(state, normalized_coords.x, normalized_coords.y); - //gtk_gl_area_queue_render(GTK_GL_AREA(widget)); + // gtk_gl_area_queue_render(GTK_GL_AREA(widget)); } void vektor_appstate_canvas_click(VektorAppState* state, double x, double y) { @@ -131,10 +129,10 @@ 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); - } - else if (state->selectedTool == VektorPolygonTool) { + } else if (state->selectedTool == VektorPolygonTool) { // create new polygon shape if none is selected if (state->selectedShape == NULL) { @@ -144,7 +142,8 @@ begin_click_dispatch: VektorStyle style = (VektorStyle){ .stroke_color = state->currentColor, .stroke_width = 0.01}; vektor_shapebuffer_add_shape( - state->shapeBuffer, vektor_shape_new(polygonPrimitive, style, 0)); + state->shapeBuffer, + vektor_shape_new(polygonPrimitive, style, 0)); state->selectedShape = &(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]); @@ -157,36 +156,32 @@ begin_click_dispatch: vektor_polygon_add_point(state->selectedShape->primitive.polygon, pos); vektor_shapes_update_bbox(state->shapeBuffer); - } - else if (state->selectedTool == VektorRectangleTool) { + } else if (state->selectedTool == VektorRectangleTool) { VektorRectangle* rect = vektor_rectangle_new(); VektorPrimitive rectPrimitive = (VektorPrimitive){.kind = VEKTOR_RECTANGLE, .rectangle = *rect}; - VektorStyle style = (VektorStyle){ - .stroke_color = state->currentColor, .stroke_width = 0.01}; - vektor_shapebuffer_add_shape( - state->shapeBuffer, vektor_shape_new(rectPrimitive, style, 0)); + VektorStyle style = (VektorStyle){.stroke_color = state->currentColor, + .stroke_width = 0.01}; + vektor_shapebuffer_add_shape(state->shapeBuffer, + vektor_shape_new(rectPrimitive, style, 0)); state->selectedShape = &(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]); vektor_rectangle_free(rect); - - vektor_rectangle_set_start(&state->selectedShape->primitive.rectangle, pos); - vektor_rectangle_set_end( - &state->selectedShape->primitive.rectangle, - vec2_add(pos, (V2){0.1f,0.1f}) - ); - //state->selectedShape = NULL; + vektor_rectangle_set_start(&state->selectedShape->primitive.rectangle, + pos); + vektor_rectangle_set_end(&state->selectedShape->primitive.rectangle, + vec2_add(pos, (V2){0.1f, 0.1f})); + // state->selectedShape = NULL; vektor_shapes_update_bbox(state->shapeBuffer); - } - else if (state->selectedTool == VektorSelectionTool) { - for(size_t i = 0; i < state->shapeBuffer->count; i++) { - VektorBBox bbox = - vektor_primitive_get_bbox(state->shapeBuffer->shapes[i].primitive); - if(vektor_bbox_isinside(bbox, pos)) { + } else if (state->selectedTool == VektorSelectionTool) { + for (size_t i = 0; i < state->shapeBuffer->count; i++) { + VektorBBox bbox = vektor_primitive_get_bbox( + state->shapeBuffer->shapes[i].primitive); + if (vektor_bbox_isinside(bbox, pos)) { state->selectedShape = &(state->shapeBuffer->shapes[i]); g_print("%d", state->selectedShape == NULL); return; @@ -203,17 +198,20 @@ 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)); + 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; - button_tool_set_data* data_rectangletool = malloc(sizeof(button_tool_set_data)); + button_tool_set_data* data_rectangletool = + malloc(sizeof(button_tool_set_data)); data_rectangletool->state = stateOut; data_rectangletool->tool = VektorRectangleTool; data_rectangletool->revealer = wstate->workspaceRevealerShapes; - button_tool_set_data* data_selecttool = malloc(sizeof(button_tool_set_data)); + button_tool_set_data* data_selecttool = + malloc(sizeof(button_tool_set_data)); data_selecttool->state = stateOut; data_selecttool->tool = VektorSelectionTool; diff --git a/src/core/primitives.c b/src/core/primitives.c index 08185ab..c659690 100644 --- a/src/core/primitives.c +++ b/src/core/primitives.c @@ -17,6 +17,11 @@ void vektor_polyline_add_point(VektorPolyline* pl, V2 point) { pl->points = realloc(pl->points, sizeof(V2) * pl->capacity); } pl->points[pl->count++] = point; + + if (pl->count <= pl->capacity / 4) { + pl->capacity /= 2; + pl->points = realloc(pl->points, sizeof(V2) * pl->capacity); + } } void vektor_polyline_free(VektorPolyline* pl) { @@ -40,6 +45,11 @@ void vektor_polygon_add_point(VektorPolygon* pg, V2 point) { pg->points = realloc(pg->points, sizeof(V2) * pg->capacity); } pg->points[pg->count++] = point; + + if (pg->count <= pg->capacity / 4) { + pg->capacity /= 2; + pg->points = realloc(pg->points, sizeof(V2) * pg->capacity); + } } void vektor_polygon_free(VektorPolygon* pg) { @@ -63,9 +73,7 @@ void vektor_rectangle_set_start(VektorRectangle* rct, V2 point) { rct->start = point; } -void vektor_rectangle_free(VektorRectangle* rct) { - free(rct); -} +void vektor_rectangle_free(VektorRectangle* rct) { free(rct); } void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer, VektorShape shape) { @@ -75,6 +83,12 @@ void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer, realloc(buffer->shapes, sizeof(VektorShape) * buffer->capacity); } buffer->shapes[buffer->count++] = shape; + + if (buffer->count <= buffer->capacity / 4) { + buffer->capacity /= 2; + buffer->shapes = + realloc(buffer->shapes, sizeof(VektorShape) * buffer->capacity); + } } VektorBBox vektor_polyline_get_bbox(VektorPrimitive prim) { @@ -136,17 +150,21 @@ VektorBBox vektor_primitive_get_bbox(VektorPrimitive prim) { } bool vektor_bbox_isinside(VektorBBox bbox, V2 point) { - return point.x >= bbox.min.x && point.y >= bbox.min.y - && point.x <= bbox.max.x && point.y <= bbox.max.y; + return point.x >= bbox.min.x && point.y >= bbox.min.y && + point.x <= bbox.max.x && point.y <= bbox.max.y; } VektorShape vektor_shape_new(VektorPrimitive prim, VektorStyle style, int z_index) { - return (VektorShape){.primitive = prim, .style = style, .z_index = z_index, .bbox=vektor_primitive_get_bbox(prim)}; + return (VektorShape){.primitive = prim, + .style = style, + .z_index = z_index, + .bbox = vektor_primitive_get_bbox(prim)}; } void vektor_shapes_update_bbox(VektorShapeBuffer* buffer) { for (size_t i = 0; i < buffer->count; i++) { - buffer->shapes[i].bbox = vektor_primitive_get_bbox(buffer->shapes[i].primitive); + buffer->shapes[i].bbox = + vektor_primitive_get_bbox(buffer->shapes[i].primitive); } } \ No newline at end of file diff --git a/src/core/raster.c b/src/core/raster.c index 02d746d..a2c6350 100644 --- a/src/core/raster.c +++ b/src/core/raster.c @@ -14,7 +14,7 @@ void vektor_edgebuffer_add_edge(EdgeBuffer* buffer, Edge edge) { } void vektor_polyline_tessellate(EdgeBuffer* buffer, VektorPolyline* line, - size_t j) { + size_t j) { for (size_t i = 0; i + 1 < line->count; i++) { vektor_edgebuffer_add_edge( buffer, (Edge){line->points[i], line->points[i + 1], 0, j}); @@ -22,22 +22,26 @@ void vektor_polyline_tessellate(EdgeBuffer* buffer, VektorPolyline* line, } void vektor_polygon_tessellate(EdgeBuffer* buffer, VektorPolygon* polygon, - size_t j) { + 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}); + buffer, + (Edge){polygon->points[polygon->count - 1], polygon->points[0], 0, j}); } -void vektor_rectangle_tessellate(EdgeBuffer* buffer, VektorRectangle* rct, size_t j) { - if (vec2_equals(rct->end, rct->start)) {return;} +void vektor_rectangle_tessellate(EdgeBuffer* buffer, VektorRectangle* rct, + size_t j) { + if (vec2_equals(rct->end, rct->start)) { + return; + } - Edge top = (Edge){rct->start, (V2){rct->end.x, rct->start.y}, 0, j }; + Edge top = (Edge){rct->start, (V2){rct->end.x, rct->start.y}, 0, j}; Edge right = (Edge){(V2){rct->end.x, rct->start.y}, rct->end, 0, j}; Edge bottom = (Edge){(V2){rct->start.x, rct->end.y}, rct->end, 0, j}; - Edge left = (Edge){rct->start, (V2){rct->start.x, rct->end.y}, 0, j }; + Edge left = (Edge){rct->start, (V2){rct->start.x, rct->end.y}, 0, j}; vektor_edgebuffer_add_edge(buffer, top); vektor_edgebuffer_add_edge(buffer, right); @@ -72,7 +76,8 @@ void vektor_rasterize(VertexBuffer* vb, VektorShapeBuffer* shapes) { vektor_edges_to_triangles(vb, &edges, shapes); } -void vektor_vb_add_triangle(VertexBuffer* vb, V2 v0, V2 v1, V2 v2, VektorColor color) { +void vektor_vb_add_triangle(VertexBuffer* vb, V2 v0, V2 v1, V2 v2, + VektorColor color) { if (vb->count + 3 >= vb->capacity) { vb->capacity = vb->capacity ? vb->capacity * 2 : 8; vb->vertices = realloc(vb->vertices, sizeof(Vertex) * vb->capacity); @@ -80,6 +85,11 @@ void vektor_vb_add_triangle(VertexBuffer* vb, V2 v0, V2 v1, V2 v2, VektorColor c vb->vertices[vb->count++] = (Vertex){v0, color}; vb->vertices[vb->count++] = (Vertex){v1, color}; vb->vertices[vb->count++] = (Vertex){v2, color}; + + if (vb->count <= vb->capacity / 4) { + vb->capacity /= 2; + vb->vertices = realloc(vb->vertices, sizeof(Vertex) * vb->capacity); + } } void vektor_vb_add_quad(VertexBuffer* vb, V2 a, V2 b, VektorColor color) { @@ -117,9 +127,9 @@ void vektor_edge_to_triangles(VertexBuffer* vb, Edge e, V2 v3 = {e.p2.x - px, e.p2.y - py}; vektor_vb_add_triangle(vb, v0, v1, v2, - shape_buffer->shapes[e.shape_id].style.stroke_color); + shape_buffer->shapes[e.shape_id].style.stroke_color); vektor_vb_add_triangle(vb, v2, v1, v3, - shape_buffer->shapes[e.shape_id].style.stroke_color); + shape_buffer->shapes[e.shape_id].style.stroke_color); } void vektor_edges_to_triangles(VertexBuffer* vb, EdgeBuffer* edges, diff --git a/src/core/raster.h b/src/core/raster.h index 6864b43..c93477a 100644 --- a/src/core/raster.h +++ b/src/core/raster.h @@ -23,9 +23,12 @@ typedef struct { void vektor_edgebuffer_add_edge(EdgeBuffer* edges, Edge edge); -void vektor_polyline_tessellate(EdgeBuffer* edges, VektorPolyline* line, size_t i); -void vektor_polygon_tessellate(EdgeBuffer* buffer, VektorPolygon* polygon, size_t i); -void vektor_rectangle_tessellate(EdgeBuffer* buffer, VektorRectangle* rct, size_t i); +void vektor_polyline_tessellate(EdgeBuffer* edges, VektorPolyline* line, + size_t i); +void vektor_polygon_tessellate(EdgeBuffer* buffer, VektorPolygon* polygon, + size_t i); +void vektor_rectangle_tessellate(EdgeBuffer* buffer, VektorRectangle* rct, + size_t i); typedef struct { V2 coords; @@ -38,7 +41,8 @@ typedef struct { size_t capacity; } VertexBuffer; -void vektor_vb_add_triangle(VertexBuffer* vb, V2 v0, V2 v1, V2 v2, VektorColor color); +void vektor_vb_add_triangle(VertexBuffer* vb, V2 v0, V2 v1, V2 v2, + VektorColor color); void vektor_vb_add_quad(VertexBuffer* vb, V2 v0, V2 v1, VektorColor color); void vektor_edge_to_triangles(VertexBuffer* vb, Edge e, diff --git a/src/ui/uicontroller.c b/src/ui/uicontroller.c index 3b076d9..5b12982 100644 --- a/src/ui/uicontroller.c +++ b/src/ui/uicontroller.c @@ -30,7 +30,7 @@ void vektor_uictrl_init(GtkApplication* app, VektorWidgetState* stateOut) { GtkIconTheme* theme = gtk_icon_theme_get_for_display(gdk_display_get_default()); - + /*if (gtk_icon_theme_has_icon(theme, "vektor-circle-symbolic")) g_print("GTK sees it!\n"); else @@ -63,11 +63,11 @@ void vektor_uictrl_init(GtkApplication* app, VektorWidgetState* stateOut) { stateOut->workspaceColorPicker = VEKTOR_COLOR_WHEEL(gtk_builder_get_object(builder, "color_picker")); - stateOut->sidepanelEntryR = + stateOut->sidepanelEntryR = GTK_ENTRY(gtk_builder_get_object(builder, "spin_color_r")); - stateOut->sidepanelEntryG = + stateOut->sidepanelEntryG = GTK_ENTRY(gtk_builder_get_object(builder, "spin_color_g")); - stateOut->sidepanelEntryB = + stateOut->sidepanelEntryB = GTK_ENTRY(gtk_builder_get_object(builder, "spin_color_b")); // Set window properties diff --git a/src/ui/vektorcanvas.c b/src/ui/vektorcanvas.c index c0020a9..35f092e 100644 --- a/src/ui/vektorcanvas.c +++ b/src/ui/vektorcanvas.c @@ -92,16 +92,24 @@ static void init_shader(void) { g_error("Failed to load shader files"); standard_shader_program = create_shader_program(frag_src, vert_src); - selection_shader_program = create_shader_program(selection_frag_src, vert_src); + selection_shader_program = + create_shader_program(selection_frag_src, vert_src); - shader_standard_uProjMatrixLoc = glGetUniformLocation(standard_shader_program, "uProjection"); - shader_selection_uProjMatrixLoc = glGetUniformLocation(selection_shader_program, "uProjection"); - shader_selection_uTimeLoc = glGetUniformLocation(selection_shader_program, "uTime"); - shader_selection_uC1Loc = glGetUniformLocation(selection_shader_program, "uColor1"); - shader_selection_uC2Loc = glGetUniformLocation(selection_shader_program, "uColor2"); + shader_standard_uProjMatrixLoc = + glGetUniformLocation(standard_shader_program, "uProjection"); + shader_selection_uProjMatrixLoc = + glGetUniformLocation(selection_shader_program, "uProjection"); + shader_selection_uTimeLoc = + glGetUniformLocation(selection_shader_program, "uTime"); + shader_selection_uC1Loc = + glGetUniformLocation(selection_shader_program, "uColor1"); + shader_selection_uC2Loc = + glGetUniformLocation(selection_shader_program, "uColor2"); - shader_selection_uMinLoc = glGetUniformLocation(selection_shader_program, "uMin"); - shader_selection_uMaxLoc = glGetUniformLocation(selection_shader_program, "uMax"); + shader_selection_uMinLoc = + glGetUniformLocation(selection_shader_program, "uMin"); + shader_selection_uMaxLoc = + glGetUniformLocation(selection_shader_program, "uMax"); if (shader_selection_uMinLoc == -1 || shader_selection_uMaxLoc == -1) g_warning("Selection shader: uMin/uMax uniform not found in shader!"); @@ -126,41 +134,38 @@ static void init_geometry(void) { glBindVertexArray(0); } -static gboolean render(GtkGLArea* a, GdkGLContext* ctx, VektorCanvasRenderInfo* renderInfo) { +static gboolean render(GtkGLArea* a, GdkGLContext* ctx, + VektorCanvasRenderInfo* renderInfo) { vb.count = 0; vektor_rasterize(&vb, renderInfo->shapes); - size_t shape_vertex_count = vb.count; // remember how many vertices belong to shapes + size_t shape_vertex_count = + vb.count; // remember how many vertices belong to shapes // create selection quad if a shape is selected - if(renderInfo->selectedShape != NULL && + if (renderInfo->selectedShape != NULL && *(renderInfo->selectedShape) != NULL) { VektorBBox bbox = vektor_primitive_get_bbox( - (*(renderInfo->selectedShape))->primitive - ); - - vektor_vb_add_quad( - &vb, - bbox.min, - bbox.max, - vektor_color_new(255,255,255,255) - ); + (*(renderInfo->selectedShape))->primitive); + + vektor_vb_add_quad(&vb, bbox.min, bbox.max, + vektor_color_new(255, 255, 255, 255)); } glBufferData(GL_ARRAY_BUFFER, vb.count * sizeof(Vertex), vb.vertices, GL_STATIC_DRAW); - // PASS 1 - draw shape vertices glUseProgram(standard_shader_program); float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}; - glUniformMatrix4fv(shader_standard_uProjMatrixLoc, 1, GL_FALSE, projectionMatrix); + glUniformMatrix4fv(shader_standard_uProjMatrixLoc, 1, GL_FALSE, + projectionMatrix); glBindVertexArray(vao); glDisable(GL_CULL_FACE); - + glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); @@ -168,26 +173,28 @@ static gboolean render(GtkGLArea* a, GdkGLContext* ctx, VektorCanvasRenderInfo* // PASS 2 - draw selection quads if (vb.count > shape_vertex_count) { - float time = (g_get_monotonic_time() - renderInfo->startupTime) / 10000000.0f; + float time = + (g_get_monotonic_time() - renderInfo->startupTime) / 10000000.0f; // re-fetch bbox (we know a shape is selected) VektorBBox bbox = vektor_primitive_get_bbox( - (*(renderInfo->selectedShape))->primitive - ); + (*(renderInfo->selectedShape))->primitive); glUseProgram(selection_shader_program); float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0, - 0, 0, 1, 0, 0, 0, 0, 1}; + 0, 0, 1, 0, 0, 0, 0, 1}; - glUniformMatrix4fv(shader_selection_uProjMatrixLoc, 1, GL_FALSE, projectionMatrix); + glUniformMatrix4fv(shader_selection_uProjMatrixLoc, 1, GL_FALSE, + projectionMatrix); glUniform1f(shader_selection_uTimeLoc, time); glUniform2f(shader_selection_uMinLoc, bbox.min.x, bbox.min.y); glUniform2f(shader_selection_uMaxLoc, bbox.max.x, bbox.max.y); glUniform4f(shader_selection_uC1Loc, 0, 0, 0, 1); glUniform4f(shader_selection_uC2Loc, 0.46, 0.46, 1, 1); - glDrawArrays(GL_TRIANGLES, shape_vertex_count, vb.count - shape_vertex_count); + glDrawArrays(GL_TRIANGLES, shape_vertex_count, + vb.count - shape_vertex_count); } glBindVertexArray(0); diff --git a/src/ui/vektorcanvas.h b/src/ui/vektorcanvas.h index 374341e..540e000 100644 --- a/src/ui/vektorcanvas.h +++ b/src/ui/vektorcanvas.h @@ -7,7 +7,6 @@ #include "src/core/primitives.h" #include "uicontroller.h" - typedef struct VektorCanvas { GtkGLArea* canvasWidget; @@ -23,7 +22,7 @@ typedef struct VektorCanvas { typedef struct VektorCanvasRenderInfo { gint64 startupTime; VektorShapeBuffer* shapes; - + // a pointer to appstate->selectedShape VektorShape** selectedShape; } VektorCanvasRenderInfo; diff --git a/src/ui/widgets/colorwheel.c b/src/ui/widgets/colorwheel.c index a0400f8..46e8865 100644 --- a/src/ui/widgets/colorwheel.c +++ b/src/ui/widgets/colorwheel.c @@ -21,82 +21,76 @@ struct _VektorColorWheel { G_DEFINE_TYPE(VektorColorWheel, vektor_color_wheel, GTK_TYPE_DRAWING_AREA) -static gboolean point_in_triangle( - double px, double py, - double ax, double ay, - double bx, double by, - double cx, double cy, - double* u, double* v, double* w) -{ - double denom = - (by - cy)*(ax - cx) + - (cx - bx)*(ay - cy); +static gboolean point_in_triangle(double px, double py, double ax, double ay, + double bx, double by, double cx, double cy, + double* u, double* v, double* w) { + double denom = (by - cy) * (ax - cx) + (cx - bx) * (ay - cy); - *u = - ((by - cy)*(px - cx) + - (cx - bx)*(py - cy)) / denom; + *u = ((by - cy) * (px - cx) + (cx - bx) * (py - cy)) / denom; - *v = - ((cy - ay)*(px - cx) + - (ax - cx)*(py - cy)) / denom; + *v = ((cy - ay) * (px - cx) + (ax - cx) * (py - cy)) / denom; *w = 1 - *u - *v; return (*u >= 0 && *v >= 0 && *w >= 0); } -static void closest_point_on_segment( - double px, double py, - double ax, double ay, - double bx, double by, - double *rx, double *ry) -{ +static void closest_point_on_segment(double px, double py, double ax, double ay, + double bx, double by, double* rx, + double* ry) { double abx = bx - ax; double aby = by - ay; double apx = px - ax; double apy = py - ay; - double t = (apx*abx + apy*aby) / (abx*abx + aby*aby); + double t = (apx * abx + apy * aby) / (abx * abx + aby * aby); - if (t < 0) t = 0; - if (t > 1) t = 1; + if (t < 0) + t = 0; + if (t > 1) + t = 1; *rx = ax + abx * t; *ry = ay + aby * t; } -static void closest_point_on_triangle( - double px, double py, - double ax, double ay, - double bx, double by, - double cx, double cy, - double *rx, double *ry) -{ - double p1x,p1y; - double p2x,p2y; - double p3x,p3y; +static void closest_point_on_triangle(double px, double py, double ax, + double ay, double bx, double by, + double cx, double cy, double* rx, + double* ry) { + double p1x, p1y; + double p2x, p2y; + double p3x, p3y; - closest_point_on_segment(px,py, ax,ay, bx,by, &p1x,&p1y); - closest_point_on_segment(px,py, bx,by, cx,cy, &p2x,&p2y); - closest_point_on_segment(px,py, cx,cy, ax,ay, &p3x,&p3y); + closest_point_on_segment(px, py, ax, ay, bx, by, &p1x, &p1y); + closest_point_on_segment(px, py, bx, by, cx, cy, &p2x, &p2y); + closest_point_on_segment(px, py, cx, cy, ax, ay, &p3x, &p3y); - double d1 = (px-p1x)*(px-p1x) + (py-p1y)*(py-p1y); - double d2 = (px-p2x)*(px-p2x) + (py-p2y)*(py-p2y); - double d3 = (px-p3x)*(px-p3x) + (py-p3y)*(py-p3y); + double d1 = (px - p1x) * (px - p1x) + (py - p1y) * (py - p1y); + double d2 = (px - p2x) * (px - p2x) + (py - p2y) * (py - p2y); + double d3 = (px - p3x) * (px - p3x) + (py - p3y) * (py - p3y); - if (d1 <= d2 && d1 <= d3) { *rx = p1x; *ry = p1y; } - else if (d2 <= d3) { *rx = p2x; *ry = p2y; } - else { *rx = p3x; *ry = p3y; } + if (d1 <= d2 && d1 <= d3) { + *rx = p1x; + *ry = p1y; + } else if (d2 <= d3) { + *rx = p2x; + *ry = p2y; + } else { + *rx = p3x; + *ry = p3y; + } } -static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot) { +static void vektor_color_wheel_snapshot(GtkWidget* widget, + GtkSnapshot* snapshot) { VektorColorWheel* self = VEKTOR_COLOR_WHEEL(widget); int width = gtk_widget_get_width(widget); int height = gtk_widget_get_height(widget); - graphene_rect_t bounds = GRAPHENE_RECT_INIT(0,0,width,height); + graphene_rect_t bounds = GRAPHENE_RECT_INIT(0, 0, width, height); cairo_t* cr = gtk_snapshot_append_cairo(snapshot, &bounds); double cx = width / 2.0; @@ -109,8 +103,8 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot double triangle_radius = wheel_radius * 0.75; // wheel draw - for(int a = 0; a < 360; a++) { - double angle_1 = a*(M_PI / 180.0); + for (int a = 0; a < 360; a++) { + double angle_1 = a * (M_PI / 180.0); double angle_2 = (a + 1) * (M_PI / 180.0); cairo_new_path(cr); @@ -118,7 +112,7 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot cairo_arc_negative(cr, cx, cy, inner_radius, angle_2, angle_1); cairo_close_path(cr); - float r,g,b; + float r, g, b; gtk_hsv_to_rgb(a / 360.0, 1.0, 1.0, &r, &g, &b); cairo_set_source_rgb(cr, r, g, b); @@ -126,7 +120,7 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot } // triangle draw - double ax = cx + triangle_radius; + double ax = cx + triangle_radius; double ay = cy; double bx = cx - 0.5 * triangle_radius; @@ -153,16 +147,20 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot // White gradient: from pure hue (right) → white (bottom) cairo_pattern_t* white = cairo_pattern_create_linear(ax, ay, bx, by); - cairo_pattern_add_color_stop_rgba(white, 0.0, 1,1,1, 0.0); // transparent at pure hue - cairo_pattern_add_color_stop_rgba(white, 1.0, 1,1,1, 1.0); // opaque white at bottom + cairo_pattern_add_color_stop_rgba(white, 0.0, 1, 1, 1, + 0.0); // transparent at pure hue + cairo_pattern_add_color_stop_rgba(white, 1.0, 1, 1, 1, + 1.0); // opaque white at bottom cairo_set_source(cr, white); cairo_paint(cr); cairo_pattern_destroy(white); // Black gradient: from pure hue (right) → black (top) cairo_pattern_t* black = cairo_pattern_create_linear(ax, ay, cx2, cy2); - cairo_pattern_add_color_stop_rgba(black, 0.0, 0,0,0, 0.0); // transparent at pure hue - cairo_pattern_add_color_stop_rgba(black, 1.0, 0,0,0, 1.0); // opaque black at top + cairo_pattern_add_color_stop_rgba(black, 0.0, 0, 0, 0, + 0.0); // transparent at pure hue + cairo_pattern_add_color_stop_rgba(black, 1.0, 0, 0, 0, + 1.0); // opaque black at top cairo_set_source(cr, black); cairo_paint(cr); cairo_pattern_destroy(black); @@ -170,26 +168,26 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot cairo_restore(cr); // triangle outline - cairo_set_source_rgb(cr,.1,.1,.1); + cairo_set_source_rgb(cr, .1, .1, .1); cairo_move_to(cr, ax, ay); cairo_line_to(cr, bx, by); cairo_line_to(cr, cx2, cy2); cairo_close_path(cr); - cairo_set_source_rgb(cr,.1,.1,.1); + cairo_set_source_rgb(cr, .1, .1, .1); cairo_stroke(cr); // selectors draw // triangle selector double chroma_weight = self->saturation * self->lightness; - double white_weight = (1.0 - self->saturation) * self->lightness; - double black_weight = 1.0 - self->lightness; + double white_weight = (1.0 - self->saturation) * self->lightness; + double black_weight = 1.0 - self->lightness; double px = ax * chroma_weight + bx * white_weight + cx2 * black_weight; double py = ay * chroma_weight + by * white_weight + cy2 * black_weight; - cairo_arc(cr, px, py, 5, 0, 2*M_PI); + cairo_arc(cr, px, py, 5, 0, 2 * M_PI); float fr, fg, fb; vektor_color_wheel_get_colorout(self, &fr, &fg, &fb); @@ -205,17 +203,12 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot cairo_new_path(cr); - cairo_arc(cr, - cx, cy, - wheel_radius, - selector_angle - selector_width, - selector_angle + selector_width); + cairo_arc(cr, cx, cy, wheel_radius, selector_angle - selector_width, + selector_angle + selector_width); - cairo_arc_negative(cr, - cx, cy, - inner_radius, - selector_angle + selector_width, - selector_angle - selector_width); + cairo_arc_negative(cr, cx, cy, inner_radius, + selector_angle + selector_width, + selector_angle - selector_width); cairo_close_path(cr); @@ -225,7 +218,8 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot cairo_destroy(cr); } -static void on_click(GtkGestureClick* gesture, int n_press, double x, double y, gpointer data) { +static void on_click(GtkGestureClick* gesture, int n_press, double x, double y, + gpointer data) { VektorColorWheel* wheel = VEKTOR_COLOR_WHEEL(data); GtkWidget* widget = GTK_WIDGET(wheel); @@ -240,7 +234,7 @@ static void on_click(GtkGestureClick* gesture, int n_press, double x, double y, double cx = width / 2.0; double cy = height / 2.0; - double ax = cx + triangle_radius; + double ax = cx + triangle_radius; double ay = cy; double bx = cx - 0.5 * triangle_radius; @@ -249,33 +243,28 @@ static void on_click(GtkGestureClick* gesture, int n_press, double x, double y, double cx2 = cx - 0.5 * triangle_radius; double cy2 = cy - 0.866 * triangle_radius; - double u,v,w; - gboolean inside = point_in_triangle(x,y, ax,ay, bx,by, cx2,cy2, &u,&v,&w); + double u, v, w; + gboolean inside = + point_in_triangle(x, y, ax, ay, bx, by, cx2, cy2, &u, &v, &w); - if(wheel->dragging_triangle) { + if (wheel->dragging_triangle) { - if(!inside) { // if outside triangle, snap to its edge - double sx,sy; + if (!inside) { // if outside triangle, snap to its edge + double sx, sy; - closest_point_on_triangle( - x,y, - ax,ay, - bx,by, - cx2,cy2, - &sx,&sy - ); + closest_point_on_triangle(x, y, ax, ay, bx, by, cx2, cy2, &sx, &sy); x = sx; y = sy; - point_in_triangle(x,y, ax,ay, bx,by, cx2,cy2, &u,&v,&w); + point_in_triangle(x, y, ax, ay, bx, by, cx2, cy2, &u, &v, &w); } double denom = u + v; - if (denom > 0.0001) { // avoid div-by-zero at black vertex + if (denom > 0.0001) { // avoid div-by-zero at black vertex wheel->saturation = u / denom; } else { - wheel->saturation = 0.0; // arbitrary, since S irrelevant at V=0 + wheel->saturation = 0.0; // arbitrary, since S irrelevant at V=0 } wheel->lightness = denom; @@ -284,27 +273,30 @@ static void on_click(GtkGestureClick* gesture, int n_press, double x, double y, double dy = y - cy; double angle = atan2(dy, dx); - if(angle < 0) { angle += 2 * M_PI; } + if (angle < 0) { + angle += 2 * M_PI; + } - wheel->hue = angle / (2*M_PI); - + wheel->hue = angle / (2 * M_PI); } g_signal_emit(wheel, signals[COLOR_CHANGED], 0); gtk_widget_queue_draw(widget); } -static void on_drag(GtkGestureDrag* gesture, double offset_x, double offset_y, gpointer data) { - double x,y; - gtk_gesture_drag_get_start_point(gesture,&x,&y); +static void on_drag(GtkGestureDrag* gesture, double offset_x, double offset_y, + gpointer data) { + double x, y; + gtk_gesture_drag_get_start_point(gesture, &x, &y); x += offset_x; y += offset_y; - on_click(NULL,0,x,y,data); + on_click(NULL, 0, x, y, data); } -static void on_drag_begin(GtkGestureDrag* gesture, gdouble start_x, gdouble start_y, gpointer user_data) { +static void on_drag_begin(GtkGestureDrag* gesture, gdouble start_x, + gdouble start_y, gpointer user_data) { // set dragging_wheel or dragging_triangle which are used // to determine to where the cursor should snap to @@ -319,13 +311,13 @@ static void on_drag_begin(GtkGestureDrag* gesture, gdouble start_x, gdouble star double dx = start_x - cx; double dy = start_y - cy; - double dist = sqrt(dx*dx+dy*dy); + double dist = sqrt(dx * dx + dy * dy); double outer_radius = MIN(width, height) / 2.0; double wheel_radius = outer_radius * 0.95; double inner_radius = wheel_radius * 0.9; - if(dist > inner_radius) { + if (dist > inner_radius) { wheel->dragging_wheel = TRUE; wheel->dragging_triangle = FALSE; } else if (dist < inner_radius) { @@ -355,17 +347,8 @@ static void vektor_color_wheel_class_init(VektorColorWheelClass* klass) { widget_class->snapshot = vektor_color_wheel_snapshot; signals[COLOR_CHANGED] = - g_signal_new( - "color-changed", - G_TYPE_FROM_CLASS(klass), - G_SIGNAL_RUN_FIRST, - 0, - NULL, - NULL, - NULL, - G_TYPE_NONE, - 0 - ); + g_signal_new("color-changed", G_TYPE_FROM_CLASS(klass), + G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0); } GtkWidget* vektor_color_wheel_new(void) { @@ -373,30 +356,23 @@ GtkWidget* vektor_color_wheel_new(void) { } VektorColor vektor_color_wheel_get_color(VektorColorWheel* wheel) { - float r,g,b; - gtk_hsv_to_rgb(wheel->hue, - wheel->saturation, - wheel->lightness, - &r, &g, &b); - - return (VektorColor) { - .r = (unsigned char)(r*255), - .g = (unsigned char)(g*255), - .b = (unsigned char)(b*255) , - .a = 255 - }; + float r, g, b; + gtk_hsv_to_rgb(wheel->hue, wheel->saturation, wheel->lightness, &r, &g, &b); + + return (VektorColor){.r = (unsigned char)(r * 255), + .g = (unsigned char)(g * 255), + .b = (unsigned char)(b * 255), + .a = 255}; } -void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r, float* g, float* b) { - gtk_hsv_to_rgb(wheel->hue, - wheel->saturation, - wheel->lightness, - r, g, b); +void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r, + float* g, float* b) { + gtk_hsv_to_rgb(wheel->hue, wheel->saturation, wheel->lightness, r, g, b); } void vektor_color_wheel_set_color(VektorColorWheel* wheel, VektorColor c) { - float h,s,v; - gtk_rgb_to_hsv(c.r/255.0, c.g/255.0, c.b/255.0, &h, &s, &v); + float h, s, v; + gtk_rgb_to_hsv(c.r / 255.0, c.g / 255.0, c.b / 255.0, &h, &s, &v); wheel->hue = (float)h; wheel->saturation = (float)s; wheel->lightness = (float)v; diff --git a/src/ui/widgets/colorwheel.h b/src/ui/widgets/colorwheel.h index 0259d7c..9da6c6e 100644 --- a/src/ui/widgets/colorwheel.h +++ b/src/ui/widgets/colorwheel.h @@ -5,11 +5,13 @@ #include "src/util/color.h" #define VEKTOR_TYPE_COLOR_WHEEL vektor_color_wheel_get_type() -G_DECLARE_FINAL_TYPE(VektorColorWheel, vektor_color_wheel, VEKTOR, COLOR_WHEEL, GtkDrawingArea) +G_DECLARE_FINAL_TYPE(VektorColorWheel, vektor_color_wheel, VEKTOR, COLOR_WHEEL, + GtkDrawingArea) GtkWidget* vektor_color_wheel_new(void); VektorColor vektor_color_wheel_get_color(VektorColorWheel* wheel); -void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r, float* g, float* b); +void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r, + float* g, float* b); void vektor_color_wheel_set_color(VektorColorWheel* wheel, VektorColor c); #endif \ No newline at end of file