fix: shrink buffers when unused fraction gets too large
This commit is contained in:
@@ -24,14 +24,12 @@ static void appstate_set_tool(GtkButton* button, gpointer user_data) {
|
|||||||
// setting tool makes the sub-tools menu to close
|
// setting tool makes the sub-tools menu to close
|
||||||
// (ADD NEW REVEALERS HERE)
|
// (ADD NEW REVEALERS HERE)
|
||||||
gtk_revealer_set_reveal_child(
|
gtk_revealer_set_reveal_child(
|
||||||
data->state->widgetState->workspaceRevealerShapes,
|
data->state->widgetState->workspaceRevealerShapes, FALSE);
|
||||||
FALSE
|
|
||||||
);
|
|
||||||
|
|
||||||
// setting tool also resets selected shape
|
// setting tool also resets selected shape
|
||||||
// NOTE: isn't needed anymore, as you would
|
// NOTE: isn't needed anymore, as you would
|
||||||
// want to be able to select & edit existing shapes
|
// 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) {
|
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,
|
vektor_appstate_canvas_click(state, normalized_coords.x,
|
||||||
normalized_coords.y);
|
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) {
|
void vektor_appstate_canvas_click(VektorAppState* state, double x, double y) {
|
||||||
@@ -131,10 +129,10 @@ begin_click_dispatch:
|
|||||||
goto begin_click_dispatch; // retry
|
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);
|
vektor_shapes_update_bbox(state->shapeBuffer);
|
||||||
}
|
} else if (state->selectedTool == VektorPolygonTool) {
|
||||||
else if (state->selectedTool == VektorPolygonTool) {
|
|
||||||
// create new polygon shape if none is selected
|
// create new polygon shape if none is selected
|
||||||
if (state->selectedShape == NULL) {
|
if (state->selectedShape == NULL) {
|
||||||
|
|
||||||
@@ -144,7 +142,8 @@ begin_click_dispatch:
|
|||||||
VektorStyle style = (VektorStyle){
|
VektorStyle style = (VektorStyle){
|
||||||
.stroke_color = state->currentColor, .stroke_width = 0.01};
|
.stroke_color = state->currentColor, .stroke_width = 0.01};
|
||||||
vektor_shapebuffer_add_shape(
|
vektor_shapebuffer_add_shape(
|
||||||
state->shapeBuffer, vektor_shape_new(polygonPrimitive, style, 0));
|
state->shapeBuffer,
|
||||||
|
vektor_shape_new(polygonPrimitive, style, 0));
|
||||||
|
|
||||||
state->selectedShape =
|
state->selectedShape =
|
||||||
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
|
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
|
||||||
@@ -157,36 +156,32 @@ begin_click_dispatch:
|
|||||||
|
|
||||||
vektor_polygon_add_point(state->selectedShape->primitive.polygon, pos);
|
vektor_polygon_add_point(state->selectedShape->primitive.polygon, pos);
|
||||||
vektor_shapes_update_bbox(state->shapeBuffer);
|
vektor_shapes_update_bbox(state->shapeBuffer);
|
||||||
}
|
} else if (state->selectedTool == VektorRectangleTool) {
|
||||||
else if (state->selectedTool == VektorRectangleTool) {
|
|
||||||
|
|
||||||
VektorRectangle* rect = vektor_rectangle_new();
|
VektorRectangle* rect = vektor_rectangle_new();
|
||||||
VektorPrimitive rectPrimitive =
|
VektorPrimitive rectPrimitive =
|
||||||
(VektorPrimitive){.kind = VEKTOR_RECTANGLE, .rectangle = *rect};
|
(VektorPrimitive){.kind = VEKTOR_RECTANGLE, .rectangle = *rect};
|
||||||
VektorStyle style = (VektorStyle){
|
VektorStyle style = (VektorStyle){.stroke_color = state->currentColor,
|
||||||
.stroke_color = state->currentColor, .stroke_width = 0.01};
|
.stroke_width = 0.01};
|
||||||
vektor_shapebuffer_add_shape(
|
vektor_shapebuffer_add_shape(state->shapeBuffer,
|
||||||
state->shapeBuffer, vektor_shape_new(rectPrimitive, style, 0));
|
vektor_shape_new(rectPrimitive, style, 0));
|
||||||
|
|
||||||
state->selectedShape =
|
state->selectedShape =
|
||||||
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
|
&(state->shapeBuffer->shapes[state->shapeBuffer->count - 1]);
|
||||||
|
|
||||||
vektor_rectangle_free(rect);
|
vektor_rectangle_free(rect);
|
||||||
|
|
||||||
|
vektor_rectangle_set_start(&state->selectedShape->primitive.rectangle,
|
||||||
vektor_rectangle_set_start(&state->selectedShape->primitive.rectangle, pos);
|
pos);
|
||||||
vektor_rectangle_set_end(
|
vektor_rectangle_set_end(&state->selectedShape->primitive.rectangle,
|
||||||
&state->selectedShape->primitive.rectangle,
|
vec2_add(pos, (V2){0.1f, 0.1f}));
|
||||||
vec2_add(pos, (V2){0.1f,0.1f})
|
// state->selectedShape = NULL;
|
||||||
);
|
|
||||||
//state->selectedShape = NULL;
|
|
||||||
vektor_shapes_update_bbox(state->shapeBuffer);
|
vektor_shapes_update_bbox(state->shapeBuffer);
|
||||||
}
|
} else if (state->selectedTool == VektorSelectionTool) {
|
||||||
else if (state->selectedTool == VektorSelectionTool) {
|
for (size_t i = 0; i < state->shapeBuffer->count; i++) {
|
||||||
for(size_t i = 0; i < state->shapeBuffer->count; i++) {
|
VektorBBox bbox = vektor_primitive_get_bbox(
|
||||||
VektorBBox bbox =
|
state->shapeBuffer->shapes[i].primitive);
|
||||||
vektor_primitive_get_bbox(state->shapeBuffer->shapes[i].primitive);
|
if (vektor_bbox_isinside(bbox, pos)) {
|
||||||
if(vektor_bbox_isinside(bbox, pos)) {
|
|
||||||
state->selectedShape = &(state->shapeBuffer->shapes[i]);
|
state->selectedShape = &(state->shapeBuffer->shapes[i]);
|
||||||
g_print("%d", state->selectedShape == NULL);
|
g_print("%d", state->selectedShape == NULL);
|
||||||
return;
|
return;
|
||||||
@@ -203,17 +198,20 @@ void vektor_appstate_new(VektorWidgetState* wstate, VektorAppState* stateOut) {
|
|||||||
data_linetool->tool = VektorLineTool;
|
data_linetool->tool = VektorLineTool;
|
||||||
data_linetool->revealer = wstate->workspaceRevealerShapes;
|
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->state = stateOut;
|
||||||
data_polygontool->tool = VektorPolygonTool;
|
data_polygontool->tool = VektorPolygonTool;
|
||||||
data_polygontool->revealer = wstate->workspaceRevealerShapes;
|
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->state = stateOut;
|
||||||
data_rectangletool->tool = VektorRectangleTool;
|
data_rectangletool->tool = VektorRectangleTool;
|
||||||
data_rectangletool->revealer = wstate->workspaceRevealerShapes;
|
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->state = stateOut;
|
||||||
data_selecttool->tool = VektorSelectionTool;
|
data_selecttool->tool = VektorSelectionTool;
|
||||||
|
|
||||||
|
|||||||
@@ -17,6 +17,11 @@ void vektor_polyline_add_point(VektorPolyline* pl, V2 point) {
|
|||||||
pl->points = realloc(pl->points, sizeof(V2) * pl->capacity);
|
pl->points = realloc(pl->points, sizeof(V2) * pl->capacity);
|
||||||
}
|
}
|
||||||
pl->points[pl->count++] = point;
|
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) {
|
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 = realloc(pg->points, sizeof(V2) * pg->capacity);
|
||||||
}
|
}
|
||||||
pg->points[pg->count++] = point;
|
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) {
|
void vektor_polygon_free(VektorPolygon* pg) {
|
||||||
@@ -63,9 +73,7 @@ void vektor_rectangle_set_start(VektorRectangle* rct, V2 point) {
|
|||||||
rct->start = point;
|
rct->start = point;
|
||||||
}
|
}
|
||||||
|
|
||||||
void vektor_rectangle_free(VektorRectangle* rct) {
|
void vektor_rectangle_free(VektorRectangle* rct) { free(rct); }
|
||||||
free(rct);
|
|
||||||
}
|
|
||||||
|
|
||||||
void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer,
|
void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer,
|
||||||
VektorShape shape) {
|
VektorShape shape) {
|
||||||
@@ -75,6 +83,12 @@ void vektor_shapebuffer_add_shape(VektorShapeBuffer* buffer,
|
|||||||
realloc(buffer->shapes, sizeof(VektorShape) * buffer->capacity);
|
realloc(buffer->shapes, sizeof(VektorShape) * buffer->capacity);
|
||||||
}
|
}
|
||||||
buffer->shapes[buffer->count++] = shape;
|
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) {
|
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) {
|
bool vektor_bbox_isinside(VektorBBox bbox, V2 point) {
|
||||||
return point.x >= bbox.min.x && point.y >= bbox.min.y
|
return point.x >= bbox.min.x && point.y >= bbox.min.y &&
|
||||||
&& point.x <= bbox.max.x && point.y <= bbox.max.y;
|
point.x <= bbox.max.x && point.y <= bbox.max.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
VektorShape vektor_shape_new(VektorPrimitive prim, VektorStyle style,
|
VektorShape vektor_shape_new(VektorPrimitive prim, VektorStyle style,
|
||||||
int z_index) {
|
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) {
|
void vektor_shapes_update_bbox(VektorShapeBuffer* buffer) {
|
||||||
for (size_t i = 0; i < buffer->count; i++) {
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -14,7 +14,7 @@ void vektor_edgebuffer_add_edge(EdgeBuffer* buffer, Edge edge) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void vektor_polyline_tessellate(EdgeBuffer* buffer, VektorPolyline* line,
|
void vektor_polyline_tessellate(EdgeBuffer* buffer, VektorPolyline* line,
|
||||||
size_t j) {
|
size_t j) {
|
||||||
for (size_t i = 0; i + 1 < line->count; i++) {
|
for (size_t i = 0; i + 1 < line->count; i++) {
|
||||||
vektor_edgebuffer_add_edge(
|
vektor_edgebuffer_add_edge(
|
||||||
buffer, (Edge){line->points[i], line->points[i + 1], 0, j});
|
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,
|
void vektor_polygon_tessellate(EdgeBuffer* buffer, VektorPolygon* polygon,
|
||||||
size_t j) {
|
size_t j) {
|
||||||
for (size_t i = 0; i + 1 < polygon->count; i++) {
|
for (size_t i = 0; i + 1 < polygon->count; i++) {
|
||||||
vektor_edgebuffer_add_edge(
|
vektor_edgebuffer_add_edge(
|
||||||
buffer, (Edge){polygon->points[i], polygon->points[i + 1], 0, j});
|
buffer, (Edge){polygon->points[i], polygon->points[i + 1], 0, j});
|
||||||
}
|
}
|
||||||
vektor_edgebuffer_add_edge(
|
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) {
|
void vektor_rectangle_tessellate(EdgeBuffer* buffer, VektorRectangle* rct,
|
||||||
if (vec2_equals(rct->end, rct->start)) {return;}
|
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 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 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, top);
|
||||||
vektor_edgebuffer_add_edge(buffer, right);
|
vektor_edgebuffer_add_edge(buffer, right);
|
||||||
@@ -72,7 +76,8 @@ void vektor_rasterize(VertexBuffer* vb, VektorShapeBuffer* shapes) {
|
|||||||
vektor_edges_to_triangles(vb, &edges, 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) {
|
if (vb->count + 3 >= vb->capacity) {
|
||||||
vb->capacity = vb->capacity ? vb->capacity * 2 : 8;
|
vb->capacity = vb->capacity ? vb->capacity * 2 : 8;
|
||||||
vb->vertices = realloc(vb->vertices, sizeof(Vertex) * vb->capacity);
|
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){v0, color};
|
||||||
vb->vertices[vb->count++] = (Vertex){v1, color};
|
vb->vertices[vb->count++] = (Vertex){v1, color};
|
||||||
vb->vertices[vb->count++] = (Vertex){v2, 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) {
|
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};
|
V2 v3 = {e.p2.x - px, e.p2.y - py};
|
||||||
|
|
||||||
vektor_vb_add_triangle(vb, v0, v1, v2,
|
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,
|
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,
|
void vektor_edges_to_triangles(VertexBuffer* vb, EdgeBuffer* edges,
|
||||||
|
|||||||
@@ -23,9 +23,12 @@ typedef struct {
|
|||||||
|
|
||||||
void vektor_edgebuffer_add_edge(EdgeBuffer* edges, Edge edge);
|
void vektor_edgebuffer_add_edge(EdgeBuffer* edges, Edge edge);
|
||||||
|
|
||||||
void vektor_polyline_tessellate(EdgeBuffer* edges, VektorPolyline* line, size_t i);
|
void vektor_polyline_tessellate(EdgeBuffer* edges, VektorPolyline* line,
|
||||||
void vektor_polygon_tessellate(EdgeBuffer* buffer, VektorPolygon* polygon, size_t i);
|
size_t i);
|
||||||
void vektor_rectangle_tessellate(EdgeBuffer* buffer, VektorRectangle* rct, 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 {
|
typedef struct {
|
||||||
V2 coords;
|
V2 coords;
|
||||||
@@ -38,7 +41,8 @@ typedef struct {
|
|||||||
size_t capacity;
|
size_t capacity;
|
||||||
} VertexBuffer;
|
} 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_vb_add_quad(VertexBuffer* vb, V2 v0, V2 v1, VektorColor color);
|
||||||
|
|
||||||
void vektor_edge_to_triangles(VertexBuffer* vb, Edge e,
|
void vektor_edge_to_triangles(VertexBuffer* vb, Edge e,
|
||||||
|
|||||||
@@ -92,16 +92,24 @@ static void init_shader(void) {
|
|||||||
g_error("Failed to load shader files");
|
g_error("Failed to load shader files");
|
||||||
|
|
||||||
standard_shader_program = create_shader_program(frag_src, vert_src);
|
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_standard_uProjMatrixLoc =
|
||||||
shader_selection_uProjMatrixLoc = glGetUniformLocation(selection_shader_program, "uProjection");
|
glGetUniformLocation(standard_shader_program, "uProjection");
|
||||||
shader_selection_uTimeLoc = glGetUniformLocation(selection_shader_program, "uTime");
|
shader_selection_uProjMatrixLoc =
|
||||||
shader_selection_uC1Loc = glGetUniformLocation(selection_shader_program, "uColor1");
|
glGetUniformLocation(selection_shader_program, "uProjection");
|
||||||
shader_selection_uC2Loc = glGetUniformLocation(selection_shader_program, "uColor2");
|
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_uMinLoc =
|
||||||
shader_selection_uMaxLoc = glGetUniformLocation(selection_shader_program, "uMax");
|
glGetUniformLocation(selection_shader_program, "uMin");
|
||||||
|
shader_selection_uMaxLoc =
|
||||||
|
glGetUniformLocation(selection_shader_program, "uMax");
|
||||||
|
|
||||||
if (shader_selection_uMinLoc == -1 || shader_selection_uMaxLoc == -1)
|
if (shader_selection_uMinLoc == -1 || shader_selection_uMaxLoc == -1)
|
||||||
g_warning("Selection shader: uMin/uMax uniform not found in shader!");
|
g_warning("Selection shader: uMin/uMax uniform not found in shader!");
|
||||||
@@ -126,37 +134,34 @@ static void init_geometry(void) {
|
|||||||
|
|
||||||
glBindVertexArray(0);
|
glBindVertexArray(0);
|
||||||
}
|
}
|
||||||
static gboolean render(GtkGLArea* a, GdkGLContext* ctx, VektorCanvasRenderInfo* renderInfo) {
|
static gboolean render(GtkGLArea* a, GdkGLContext* ctx,
|
||||||
|
VektorCanvasRenderInfo* renderInfo) {
|
||||||
vb.count = 0;
|
vb.count = 0;
|
||||||
|
|
||||||
vektor_rasterize(&vb, renderInfo->shapes);
|
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
|
// create selection quad if a shape is selected
|
||||||
if(renderInfo->selectedShape != NULL &&
|
if (renderInfo->selectedShape != NULL &&
|
||||||
*(renderInfo->selectedShape) != NULL) {
|
*(renderInfo->selectedShape) != NULL) {
|
||||||
VektorBBox bbox = vektor_primitive_get_bbox(
|
VektorBBox bbox = vektor_primitive_get_bbox(
|
||||||
(*(renderInfo->selectedShape))->primitive
|
(*(renderInfo->selectedShape))->primitive);
|
||||||
);
|
|
||||||
|
|
||||||
vektor_vb_add_quad(
|
vektor_vb_add_quad(&vb, bbox.min, bbox.max,
|
||||||
&vb,
|
vektor_color_new(255, 255, 255, 255));
|
||||||
bbox.min,
|
|
||||||
bbox.max,
|
|
||||||
vektor_color_new(255,255,255,255)
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
glBufferData(GL_ARRAY_BUFFER, vb.count * sizeof(Vertex), vb.vertices,
|
glBufferData(GL_ARRAY_BUFFER, vb.count * sizeof(Vertex), vb.vertices,
|
||||||
GL_STATIC_DRAW);
|
GL_STATIC_DRAW);
|
||||||
|
|
||||||
|
|
||||||
// PASS 1 - draw shape vertices
|
// PASS 1 - draw shape vertices
|
||||||
glUseProgram(standard_shader_program);
|
glUseProgram(standard_shader_program);
|
||||||
|
|
||||||
float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0,
|
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_standard_uProjMatrixLoc, 1, GL_FALSE, projectionMatrix);
|
glUniformMatrix4fv(shader_standard_uProjMatrixLoc, 1, GL_FALSE,
|
||||||
|
projectionMatrix);
|
||||||
|
|
||||||
glBindVertexArray(vao);
|
glBindVertexArray(vao);
|
||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
@@ -168,26 +173,28 @@ static gboolean render(GtkGLArea* a, GdkGLContext* ctx, VektorCanvasRenderInfo*
|
|||||||
|
|
||||||
// PASS 2 - draw selection quads
|
// PASS 2 - draw selection quads
|
||||||
if (vb.count > shape_vertex_count) {
|
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)
|
// re-fetch bbox (we know a shape is selected)
|
||||||
VektorBBox bbox = vektor_primitive_get_bbox(
|
VektorBBox bbox = vektor_primitive_get_bbox(
|
||||||
(*(renderInfo->selectedShape))->primitive
|
(*(renderInfo->selectedShape))->primitive);
|
||||||
);
|
|
||||||
|
|
||||||
glUseProgram(selection_shader_program);
|
glUseProgram(selection_shader_program);
|
||||||
|
|
||||||
float projectionMatrix[16] = {1, 0, 0, 0, 0, 1, 0, 0,
|
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);
|
glUniform1f(shader_selection_uTimeLoc, time);
|
||||||
glUniform2f(shader_selection_uMinLoc, bbox.min.x, bbox.min.y);
|
glUniform2f(shader_selection_uMinLoc, bbox.min.x, bbox.min.y);
|
||||||
glUniform2f(shader_selection_uMaxLoc, bbox.max.x, bbox.max.y);
|
glUniform2f(shader_selection_uMaxLoc, bbox.max.x, bbox.max.y);
|
||||||
glUniform4f(shader_selection_uC1Loc, 0, 0, 0, 1);
|
glUniform4f(shader_selection_uC1Loc, 0, 0, 0, 1);
|
||||||
glUniform4f(shader_selection_uC2Loc, 0.46, 0.46, 1, 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);
|
glBindVertexArray(0);
|
||||||
|
|||||||
@@ -7,7 +7,6 @@
|
|||||||
#include "src/core/primitives.h"
|
#include "src/core/primitives.h"
|
||||||
#include "uicontroller.h"
|
#include "uicontroller.h"
|
||||||
|
|
||||||
|
|
||||||
typedef struct VektorCanvas {
|
typedef struct VektorCanvas {
|
||||||
GtkGLArea* canvasWidget;
|
GtkGLArea* canvasWidget;
|
||||||
|
|
||||||
|
|||||||
@@ -21,82 +21,76 @@ struct _VektorColorWheel {
|
|||||||
|
|
||||||
G_DEFINE_TYPE(VektorColorWheel, vektor_color_wheel, GTK_TYPE_DRAWING_AREA)
|
G_DEFINE_TYPE(VektorColorWheel, vektor_color_wheel, GTK_TYPE_DRAWING_AREA)
|
||||||
|
|
||||||
static gboolean point_in_triangle(
|
static gboolean point_in_triangle(double px, double py, double ax, double ay,
|
||||||
double px, double py,
|
double bx, double by, double cx, double cy,
|
||||||
double ax, double ay,
|
double* u, double* v, double* w) {
|
||||||
double bx, double by,
|
double denom = (by - cy) * (ax - cx) + (cx - bx) * (ay - cy);
|
||||||
double cx, double cy,
|
|
||||||
double* u, double* v, double* w)
|
|
||||||
{
|
|
||||||
double denom =
|
|
||||||
(by - cy)*(ax - cx) +
|
|
||||||
(cx - bx)*(ay - cy);
|
|
||||||
|
|
||||||
*u =
|
*u = ((by - cy) * (px - cx) + (cx - bx) * (py - cy)) / denom;
|
||||||
((by - cy)*(px - cx) +
|
|
||||||
(cx - bx)*(py - cy)) / denom;
|
|
||||||
|
|
||||||
*v =
|
*v = ((cy - ay) * (px - cx) + (ax - cx) * (py - cy)) / denom;
|
||||||
((cy - ay)*(px - cx) +
|
|
||||||
(ax - cx)*(py - cy)) / denom;
|
|
||||||
|
|
||||||
*w = 1 - *u - *v;
|
*w = 1 - *u - *v;
|
||||||
|
|
||||||
return (*u >= 0 && *v >= 0 && *w >= 0);
|
return (*u >= 0 && *v >= 0 && *w >= 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void closest_point_on_segment(
|
static void closest_point_on_segment(double px, double py, double ax, double ay,
|
||||||
double px, double py,
|
double bx, double by, double* rx,
|
||||||
double ax, double ay,
|
double* ry) {
|
||||||
double bx, double by,
|
|
||||||
double *rx, double *ry)
|
|
||||||
{
|
|
||||||
double abx = bx - ax;
|
double abx = bx - ax;
|
||||||
double aby = by - ay;
|
double aby = by - ay;
|
||||||
|
|
||||||
double apx = px - ax;
|
double apx = px - ax;
|
||||||
double apy = py - ay;
|
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 < 0)
|
||||||
if (t > 1) t = 1;
|
t = 0;
|
||||||
|
if (t > 1)
|
||||||
|
t = 1;
|
||||||
|
|
||||||
*rx = ax + abx * t;
|
*rx = ax + abx * t;
|
||||||
*ry = ay + aby * t;
|
*ry = ay + aby * t;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void closest_point_on_triangle(
|
static void closest_point_on_triangle(double px, double py, double ax,
|
||||||
double px, double py,
|
double ay, double bx, double by,
|
||||||
double ax, double ay,
|
double cx, double cy, double* rx,
|
||||||
double bx, double by,
|
double* ry) {
|
||||||
double cx, double cy,
|
double p1x, p1y;
|
||||||
double *rx, double *ry)
|
double p2x, p2y;
|
||||||
{
|
double p3x, p3y;
|
||||||
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, ax, ay, bx, by, &p1x, &p1y);
|
||||||
closest_point_on_segment(px,py, bx,by, cx,cy, &p2x,&p2y);
|
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, cx, cy, ax, ay, &p3x, &p3y);
|
||||||
|
|
||||||
double d1 = (px-p1x)*(px-p1x) + (py-p1y)*(py-p1y);
|
double d1 = (px - p1x) * (px - p1x) + (py - p1y) * (py - p1y);
|
||||||
double d2 = (px-p2x)*(px-p2x) + (py-p2y)*(py-p2y);
|
double d2 = (px - p2x) * (px - p2x) + (py - p2y) * (py - p2y);
|
||||||
double d3 = (px-p3x)*(px-p3x) + (py-p3y)*(py-p3y);
|
double d3 = (px - p3x) * (px - p3x) + (py - p3y) * (py - p3y);
|
||||||
|
|
||||||
if (d1 <= d2 && d1 <= d3) { *rx = p1x; *ry = p1y; }
|
if (d1 <= d2 && d1 <= d3) {
|
||||||
else if (d2 <= d3) { *rx = p2x; *ry = p2y; }
|
*rx = p1x;
|
||||||
else { *rx = p3x; *ry = p3y; }
|
*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);
|
VektorColorWheel* self = VEKTOR_COLOR_WHEEL(widget);
|
||||||
|
|
||||||
int width = gtk_widget_get_width(widget);
|
int width = gtk_widget_get_width(widget);
|
||||||
int height = gtk_widget_get_height(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);
|
cairo_t* cr = gtk_snapshot_append_cairo(snapshot, &bounds);
|
||||||
|
|
||||||
double cx = width / 2.0;
|
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;
|
double triangle_radius = wheel_radius * 0.75;
|
||||||
|
|
||||||
// wheel draw
|
// wheel draw
|
||||||
for(int a = 0; a < 360; a++) {
|
for (int a = 0; a < 360; a++) {
|
||||||
double angle_1 = a*(M_PI / 180.0);
|
double angle_1 = a * (M_PI / 180.0);
|
||||||
double angle_2 = (a + 1) * (M_PI / 180.0);
|
double angle_2 = (a + 1) * (M_PI / 180.0);
|
||||||
|
|
||||||
cairo_new_path(cr);
|
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_arc_negative(cr, cx, cy, inner_radius, angle_2, angle_1);
|
||||||
cairo_close_path(cr);
|
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);
|
gtk_hsv_to_rgb(a / 360.0, 1.0, 1.0, &r, &g, &b);
|
||||||
|
|
||||||
cairo_set_source_rgb(cr, 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
|
// triangle draw
|
||||||
double ax = cx + triangle_radius;
|
double ax = cx + triangle_radius;
|
||||||
double ay = cy;
|
double ay = cy;
|
||||||
|
|
||||||
double bx = cx - 0.5 * triangle_radius;
|
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)
|
// White gradient: from pure hue (right) → white (bottom)
|
||||||
cairo_pattern_t* white = cairo_pattern_create_linear(ax, ay, bx, by);
|
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, 0.0, 1, 1, 1,
|
||||||
cairo_pattern_add_color_stop_rgba(white, 1.0, 1,1,1, 1.0); // opaque white at bottom
|
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_set_source(cr, white);
|
||||||
cairo_paint(cr);
|
cairo_paint(cr);
|
||||||
cairo_pattern_destroy(white);
|
cairo_pattern_destroy(white);
|
||||||
|
|
||||||
// Black gradient: from pure hue (right) → black (top)
|
// Black gradient: from pure hue (right) → black (top)
|
||||||
cairo_pattern_t* black = cairo_pattern_create_linear(ax, ay, cx2, cy2);
|
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, 0.0, 0, 0, 0,
|
||||||
cairo_pattern_add_color_stop_rgba(black, 1.0, 0,0,0, 1.0); // opaque black at top
|
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_set_source(cr, black);
|
||||||
cairo_paint(cr);
|
cairo_paint(cr);
|
||||||
cairo_pattern_destroy(black);
|
cairo_pattern_destroy(black);
|
||||||
@@ -170,26 +168,26 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
|
|||||||
cairo_restore(cr);
|
cairo_restore(cr);
|
||||||
|
|
||||||
// triangle outline
|
// 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_move_to(cr, ax, ay);
|
||||||
cairo_line_to(cr, bx, by);
|
cairo_line_to(cr, bx, by);
|
||||||
cairo_line_to(cr, cx2, cy2);
|
cairo_line_to(cr, cx2, cy2);
|
||||||
cairo_close_path(cr);
|
cairo_close_path(cr);
|
||||||
|
|
||||||
cairo_set_source_rgb(cr,.1,.1,.1);
|
cairo_set_source_rgb(cr, .1, .1, .1);
|
||||||
cairo_stroke(cr);
|
cairo_stroke(cr);
|
||||||
|
|
||||||
// selectors draw
|
// selectors draw
|
||||||
|
|
||||||
// triangle selector
|
// triangle selector
|
||||||
double chroma_weight = self->saturation * self->lightness;
|
double chroma_weight = self->saturation * self->lightness;
|
||||||
double white_weight = (1.0 - self->saturation) * self->lightness;
|
double white_weight = (1.0 - self->saturation) * self->lightness;
|
||||||
double black_weight = 1.0 - self->lightness;
|
double black_weight = 1.0 - self->lightness;
|
||||||
|
|
||||||
double px = ax * chroma_weight + bx * white_weight + cx2 * black_weight;
|
double px = ax * chroma_weight + bx * white_weight + cx2 * black_weight;
|
||||||
double py = ay * chroma_weight + by * white_weight + cy2 * 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;
|
float fr, fg, fb;
|
||||||
vektor_color_wheel_get_colorout(self, &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_new_path(cr);
|
||||||
|
|
||||||
cairo_arc(cr,
|
cairo_arc(cr, cx, cy, wheel_radius, selector_angle - selector_width,
|
||||||
cx, cy,
|
selector_angle + selector_width);
|
||||||
wheel_radius,
|
|
||||||
selector_angle - selector_width,
|
|
||||||
selector_angle + selector_width);
|
|
||||||
|
|
||||||
cairo_arc_negative(cr,
|
cairo_arc_negative(cr, cx, cy, inner_radius,
|
||||||
cx, cy,
|
selector_angle + selector_width,
|
||||||
inner_radius,
|
selector_angle - selector_width);
|
||||||
selector_angle + selector_width,
|
|
||||||
selector_angle - selector_width);
|
|
||||||
|
|
||||||
cairo_close_path(cr);
|
cairo_close_path(cr);
|
||||||
|
|
||||||
@@ -225,7 +218,8 @@ static void vektor_color_wheel_snapshot(GtkWidget* widget, GtkSnapshot* snapshot
|
|||||||
cairo_destroy(cr);
|
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);
|
VektorColorWheel* wheel = VEKTOR_COLOR_WHEEL(data);
|
||||||
GtkWidget* widget = GTK_WIDGET(wheel);
|
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 cx = width / 2.0;
|
||||||
double cy = height / 2.0;
|
double cy = height / 2.0;
|
||||||
|
|
||||||
double ax = cx + triangle_radius;
|
double ax = cx + triangle_radius;
|
||||||
double ay = cy;
|
double ay = cy;
|
||||||
|
|
||||||
double bx = cx - 0.5 * triangle_radius;
|
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 cx2 = cx - 0.5 * triangle_radius;
|
||||||
double cy2 = cy - 0.866 * triangle_radius;
|
double cy2 = cy - 0.866 * triangle_radius;
|
||||||
|
|
||||||
double u,v,w;
|
double u, v, w;
|
||||||
gboolean inside = point_in_triangle(x,y, ax,ay, bx,by, cx2,cy2, &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
|
if (!inside) { // if outside triangle, snap to its edge
|
||||||
double sx,sy;
|
double sx, sy;
|
||||||
|
|
||||||
closest_point_on_triangle(
|
closest_point_on_triangle(x, y, ax, ay, bx, by, cx2, cy2, &sx, &sy);
|
||||||
x,y,
|
|
||||||
ax,ay,
|
|
||||||
bx,by,
|
|
||||||
cx2,cy2,
|
|
||||||
&sx,&sy
|
|
||||||
);
|
|
||||||
|
|
||||||
x = sx;
|
x = sx;
|
||||||
y = sy;
|
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;
|
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;
|
wheel->saturation = u / denom;
|
||||||
} else {
|
} 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;
|
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 dy = y - cy;
|
||||||
|
|
||||||
double angle = atan2(dy, dx);
|
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);
|
g_signal_emit(wheel, signals[COLOR_CHANGED], 0);
|
||||||
gtk_widget_queue_draw(widget);
|
gtk_widget_queue_draw(widget);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void on_drag(GtkGestureDrag* gesture, double offset_x, double offset_y, gpointer data) {
|
static void on_drag(GtkGestureDrag* gesture, double offset_x, double offset_y,
|
||||||
double x,y;
|
gpointer data) {
|
||||||
gtk_gesture_drag_get_start_point(gesture,&x,&y);
|
double x, y;
|
||||||
|
gtk_gesture_drag_get_start_point(gesture, &x, &y);
|
||||||
|
|
||||||
x += offset_x;
|
x += offset_x;
|
||||||
y += offset_y;
|
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
|
// set dragging_wheel or dragging_triangle which are used
|
||||||
// to determine to where the cursor should snap to
|
// 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 dx = start_x - cx;
|
||||||
double dy = start_y - cy;
|
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 outer_radius = MIN(width, height) / 2.0;
|
||||||
double wheel_radius = outer_radius * 0.95;
|
double wheel_radius = outer_radius * 0.95;
|
||||||
double inner_radius = wheel_radius * 0.9;
|
double inner_radius = wheel_radius * 0.9;
|
||||||
|
|
||||||
if(dist > inner_radius) {
|
if (dist > inner_radius) {
|
||||||
wheel->dragging_wheel = TRUE;
|
wheel->dragging_wheel = TRUE;
|
||||||
wheel->dragging_triangle = FALSE;
|
wheel->dragging_triangle = FALSE;
|
||||||
} else if (dist < inner_radius) {
|
} 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;
|
widget_class->snapshot = vektor_color_wheel_snapshot;
|
||||||
|
|
||||||
signals[COLOR_CHANGED] =
|
signals[COLOR_CHANGED] =
|
||||||
g_signal_new(
|
g_signal_new("color-changed", G_TYPE_FROM_CLASS(klass),
|
||||||
"color-changed",
|
G_SIGNAL_RUN_FIRST, 0, NULL, NULL, NULL, G_TYPE_NONE, 0);
|
||||||
G_TYPE_FROM_CLASS(klass),
|
|
||||||
G_SIGNAL_RUN_FIRST,
|
|
||||||
0,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
NULL,
|
|
||||||
G_TYPE_NONE,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
GtkWidget* vektor_color_wheel_new(void) {
|
GtkWidget* vektor_color_wheel_new(void) {
|
||||||
@@ -373,30 +356,23 @@ GtkWidget* vektor_color_wheel_new(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
VektorColor vektor_color_wheel_get_color(VektorColorWheel* wheel) {
|
VektorColor vektor_color_wheel_get_color(VektorColorWheel* wheel) {
|
||||||
float r,g,b;
|
float r, g, b;
|
||||||
gtk_hsv_to_rgb(wheel->hue,
|
gtk_hsv_to_rgb(wheel->hue, wheel->saturation, wheel->lightness, &r, &g, &b);
|
||||||
wheel->saturation,
|
|
||||||
wheel->lightness,
|
|
||||||
&r, &g, &b);
|
|
||||||
|
|
||||||
return (VektorColor) {
|
return (VektorColor){.r = (unsigned char)(r * 255),
|
||||||
.r = (unsigned char)(r*255),
|
.g = (unsigned char)(g * 255),
|
||||||
.g = (unsigned char)(g*255),
|
.b = (unsigned char)(b * 255),
|
||||||
.b = (unsigned char)(b*255) ,
|
.a = 255};
|
||||||
.a = 255
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r, float* g, float* b) {
|
void vektor_color_wheel_get_colorout(VektorColorWheel* wheel, float* r,
|
||||||
gtk_hsv_to_rgb(wheel->hue,
|
float* g, float* b) {
|
||||||
wheel->saturation,
|
gtk_hsv_to_rgb(wheel->hue, wheel->saturation, wheel->lightness, r, g, b);
|
||||||
wheel->lightness,
|
|
||||||
r, g, b);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void vektor_color_wheel_set_color(VektorColorWheel* wheel, VektorColor c) {
|
void vektor_color_wheel_set_color(VektorColorWheel* wheel, VektorColor c) {
|
||||||
float 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);
|
gtk_rgb_to_hsv(c.r / 255.0, c.g / 255.0, c.b / 255.0, &h, &s, &v);
|
||||||
wheel->hue = (float)h;
|
wheel->hue = (float)h;
|
||||||
wheel->saturation = (float)s;
|
wheel->saturation = (float)s;
|
||||||
wheel->lightness = (float)v;
|
wheel->lightness = (float)v;
|
||||||
|
|||||||
@@ -5,11 +5,13 @@
|
|||||||
#include "src/util/color.h"
|
#include "src/util/color.h"
|
||||||
|
|
||||||
#define VEKTOR_TYPE_COLOR_WHEEL vektor_color_wheel_get_type()
|
#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);
|
GtkWidget* vektor_color_wheel_new(void);
|
||||||
VektorColor vektor_color_wheel_get_color(VektorColorWheel* wheel);
|
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);
|
void vektor_color_wheel_set_color(VektorColorWheel* wheel, VektorColor c);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
Reference in New Issue
Block a user