Skip to content
Snippets Groups Projects
Commit 365793e7 authored by Younes Oubkis's avatar Younes Oubkis
Browse files

Clicking on Shapes

parent 453c7c0c
No related branches found
No related tags found
No related merge requests found
No preview for this file type
...@@ -7,10 +7,10 @@ GtkWidget *window; ...@@ -7,10 +7,10 @@ GtkWidget *window;
GtkWidget *drawing_area; GtkWidget *drawing_area;
enum INPUTTYPE current_input; enum INPUTTYPE current_input;
t_shape shapes[MAX_SHAPES + 1]; t_shape shapes[MAX_SHAPES + 1];
t_shape selected_shape;
int shape_type; int shape_type;
int click_count; int click_count;
t_vertex click_positions[4]; t_vertex click_positions[4];
GtkWidget *shapes_list;
t_custom_toolbar *toolbar; t_custom_toolbar *toolbar;
...@@ -26,7 +26,7 @@ gboolean on_button_clicked(GtkWidget *button, gpointer user_data) ...@@ -26,7 +26,7 @@ gboolean on_button_clicked(GtkWidget *button, gpointer user_data)
gboolean on_shape_choice(GtkWidget *button, gpointer user_data) gboolean on_shape_choice(GtkWidget *button, gpointer user_data)
{ {
current_input = CREATE_SHAPE; current_input = CREATE_SHAPE;
int d = (int)user_data; int d = user_data;
shape_type = d; shape_type = d;
g_print("Data: %d\n", d); g_print("Data: %d\n", d);
...@@ -83,11 +83,16 @@ int create_window_layout(int argc, char **argv) ...@@ -83,11 +83,16 @@ int create_window_layout(int argc, char **argv)
g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL); g_signal_connect(button, "clicked", G_CALLBACK(gtk_main_quit), NULL);
drawing_area = gtk_drawing_area_new(); drawing_area = gtk_drawing_area_new();
gtk_widget_add_events(drawing_area, GDK_BUTTON_PRESS_MASK); gtk_widget_add_events(drawing_area, GDK_BUTTON_PRESS_MASK);
g_signal_connect(drawing_area, "draw", G_CALLBACK(draw), NULL); g_signal_connect(drawing_area, "draw", G_CALLBACK(draw), NULL);
g_signal_connect(GTK_WIDGET(drawing_area), "button-press-event", G_CALLBACK(drawing_area_click), NULL); g_signal_connect(GTK_WIDGET(drawing_area), "button-press-event", G_CALLBACK(drawing_area_click), NULL);
gtk_widget_set_size_request(drawing_area, width, height); gtk_widget_set_size_request(drawing_area, width, height);
toolbar = create_toolbar(4); toolbar = create_toolbar(4);
GtkToolItem *triangle_btn = create_and_bind_toolbutton(toolbar, "Triangle"); GtkToolItem *triangle_btn = create_and_bind_toolbutton(toolbar, "Triangle");
GtkToolItem *rectangle_btn = create_and_bind_toolbutton(toolbar, "Rectangle"); GtkToolItem *rectangle_btn = create_and_bind_toolbutton(toolbar, "Rectangle");
...@@ -98,8 +103,13 @@ int create_window_layout(int argc, char **argv) ...@@ -98,8 +103,13 @@ int create_window_layout(int argc, char **argv)
bind_click_signal(rectangle_btn, on_shape_choice, RECTANGLE); bind_click_signal(rectangle_btn, on_shape_choice, RECTANGLE);
bind_click_signal(circle_btn, on_shape_choice, CIRCLE); bind_click_signal(circle_btn, on_shape_choice, CIRCLE);
bind_click_signal(clear_btn, on_clear, CIRCLE); bind_click_signal(clear_btn, on_clear, CIRCLE);
shapes_list = gtk_list_box_new();
GtkWidget *lbTest = gtk_label_new("TestItem");
gtk_list_box_insert(GTK_LIST_BOX(shapes_list),lbTest,0);
gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(drawing_area)); gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(drawing_area));
gtk_container_add(GTK_CONTAINER(hbox), GTK_WIDGET(shapes_list));
gtk_container_add(GTK_CONTAINER(vbox), button); gtk_container_add(GTK_CONTAINER(vbox), button);
gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(toolbar->widget)); gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(toolbar->widget));
gtk_container_add(GTK_CONTAINER(vbox), hbox); gtk_container_add(GTK_CONTAINER(vbox), hbox);
...@@ -116,7 +126,6 @@ int main(int argc, char **argv) ...@@ -116,7 +126,6 @@ int main(int argc, char **argv)
shapes[i] = NULL_SHAPE(); shapes[i] = NULL_SHAPE();
} }
shapes[MAX_SHAPES].flag = -1; shapes[MAX_SHAPES].flag = -1;
shapes[0] = create_square(20, 20, 40, 40);
create_window_layout(argc, argv); create_window_layout(argc, argv);
printf("Starting GTK window with dimensions W:%d, H:%d\n", width, height); printf("Starting GTK window with dimensions W:%d, H:%d\n", width, height);
......
...@@ -17,7 +17,8 @@ extern int shape_type; ...@@ -17,7 +17,8 @@ extern int shape_type;
extern int click_count; extern int click_count;
extern t_vertex click_positions[4]; extern t_vertex click_positions[4];
extern t_shape shapes[MAX_SHAPES + 1]; extern t_shape shapes[MAX_SHAPES + 1];
extern t_shape selected_shape; extern t_shape *selected_shape;
extern GtkWidget *shapes_list;
gboolean on_clear(GtkWidget *button, gpointer user_data); gboolean on_clear(GtkWidget *button, gpointer user_data);
gboolean drawing_area_click(GtkWidget *widget, GdkEventButton *event, gpointer data); gboolean drawing_area_click(GtkWidget *widget, GdkEventButton *event, gpointer data);
......
...@@ -8,6 +8,7 @@ GdkRGBA black = {0.0, 0.0, 0.0, 1.0}; ...@@ -8,6 +8,7 @@ GdkRGBA black = {0.0, 0.0, 0.0, 1.0};
int selection_flag = 0; int selection_flag = 0;
float selectionX, selectionY; float selectionX, selectionY;
t_shape *selected_shape;
gboolean drawing_area_click(GtkWidget *widget, GdkEventButton *event, gpointer data) gboolean drawing_area_click(GtkWidget *widget, GdkEventButton *event, gpointer data)
{ {
...@@ -77,13 +78,9 @@ void handle_shape_click(float x, float y) ...@@ -77,13 +78,9 @@ void handle_shape_click(float x, float y)
t_shape new_triangle = create_triangle(p[0], p[1], point(x, y)); t_shape new_triangle = create_triangle(p[0], p[1], point(x, y));
add_shape(new_triangle); add_shape(new_triangle);
click_count = 0; click_count = 0;
return;
} }
return; return;
} }
break;
} }
} }
void handle_freehand_click(float x, float y) void handle_freehand_click(float x, float y)
...@@ -91,19 +88,18 @@ void handle_freehand_click(float x, float y) ...@@ -91,19 +88,18 @@ void handle_freehand_click(float x, float y)
} }
void add_shape(t_shape new_shape) void add_shape(t_shape new_shape)
{ {
t_shape null = NULL_SHAPE();
for (int i = 0; i < MAX_SHAPES; i++) for (int i = 0; i < MAX_SHAPES; i++)
{ {
// Find free Null Shape // Find free Null Shape
if (shapes[i].flag == 0) if (shapes[i].flag > 0)
{ continue;
shapes[i] = new_shape; shapes[i] = new_shape;
gtk_widget_queue_draw(drawing_area); gtk_widget_queue_draw(drawing_area);
click_count = 0; click_count = 0;
return; return;
} }
} }
}
gboolean on_clear(GtkWidget *button, gpointer user_data) gboolean on_clear(GtkWidget *button, gpointer user_data)
{ {
for (int i = 0; i < MAX_SHAPES; i++) for (int i = 0; i < MAX_SHAPES; i++)
...@@ -113,58 +109,61 @@ gboolean on_clear(GtkWidget *button, gpointer user_data) ...@@ -113,58 +109,61 @@ gboolean on_clear(GtkWidget *button, gpointer user_data)
gtk_widget_queue_draw(drawing_area); gtk_widget_queue_draw(drawing_area);
} }
gboolean draw(GtkWidget *widget, cairo_t *c, gpointer data) void draw_shape(cairo_t *c, t_shape *shape)
{ {
set_source_color(c, &red); // set_source_color(c, &shape->color);
for (t_shape *s = shapes; s; s++) if (shape->type == CIRCLE)
{ {
if (!s->flag) float radius = hypotenuse(shape->vertices[0], shape->vertices[1]);
continue; cairo_arc(c, shape->vertices[0].X, shape->vertices[0].Y, radius, 0, G_PI * 2);
if (s->flag == -1) g_print("Circle @ %f,%f\n", shape->vertices[0].X, shape->vertices[0].Y);
break;
t_vertex *v = s->vertices;
if (s->type == CIRCLE)
{
float radius = hypotenuse(s->vertices[0], s->vertices[1]);
cairo_arc(c, s->vertices[0].X, s->vertices[0].Y, radius, 0, G_PI * 2);
} }
if (s->type == TRIANGLE) if (shape->type == TRIANGLE)
{ {
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
cairo_move_to(c, s->vertices[i].X, s->vertices[i].Y); cairo_move_to(c, shape->vertices[i].X, shape->vertices[i].Y);
cairo_arc(c, s->vertices[0].X, s->vertices[0].Y, 5, 0, G_PI * 2); cairo_line_to(c, shape->vertices[i + 1].X, shape->vertices[i + 1].Y);
cairo_line_to(c, s->vertices[i + 1].X, s->vertices[i + 1].Y); g_print("%d X%f Y%f \n", i, shape->vertices[i].X, shape->vertices[i].Y);
g_print("%d X%f Y%f \n", i, s->vertices[i].X, s->vertices[i].Y); g_print("Triangle @ %f,%f\n", shape->vertices[0].X, shape->vertices[0].Y);
} }
cairo_close_path(c); cairo_close_path(c);
} }
} }
if (selection_flag) gboolean draw(GtkWidget *widget, cairo_t *c, gpointer data)
{ {
gboolean isInside = cairo_in_fill(c, selectionX, selectionY); for (t_shape *s = shapes; s; s++)
if (isInside) {
g_print("HIT!");
else if (!s->flag)
g_print("MISS!"); continue;
selection_flag = 0; if (s->flag == -1)
{
break;
} }
cairo_fill(c); set_source_color(c, &red);
for (int i = 0; i < click_count; i++)
draw_shape(c, s);
if (!selection_flag || !cairo_in_fill(c, selectionX, selectionY))
{ {
set_source_color(c, &black);
cairo_arc(c, click_positions[i].X, click_positions[i].Y, 2.0, 0, G_PI * 2);
cairo_fill(c); cairo_fill(c);
continue;
} }
/*set_source_color(c, &yellow); selected_shape = s;
cairo_arc(c, 65, 50, 30, 0, 2 * G_PI); selection_flag = 0;
cairo_set_line_width(c, 5.5);
cairo_set_source_rgb(c, 0, 0, 1);
cairo_stroke(c);
set_source_color(c,&s->color);
cairo_fill(c); cairo_fill(c);
*/
}
return FALSE; /* TRUE to stop other handlers from being invoked for the event. return FALSE; /* TRUE to stop other handlers from being invoked for the event.
FALSE to propagate the event further. */ FALSE to propagate the event further. */
......
...@@ -21,6 +21,7 @@ typedef struct shape ...@@ -21,6 +21,7 @@ typedef struct shape
t_vertex* vertices; t_vertex* vertices;
int vertexCount; int vertexCount;
int flag; int flag;
GdkRGBA color;
} t_shape; } t_shape;
typedef struct custom_toolbar typedef struct custom_toolbar
......
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
GdkRGBA rd = {1.0, 0.0, 0.0, 1.0};
t_vertex point(float x, float y) t_vertex point(float x, float y)
{ {
t_vertex v; t_vertex v;
...@@ -21,11 +23,19 @@ void create_shapes_array(t_shape* arr,int len) ...@@ -21,11 +23,19 @@ void create_shapes_array(t_shape* arr,int len)
} }
} }
t_shape create_square(float x1,float y1,float x2, float y2) t_shape generic(int vertices)
{ {
t_shape shape; t_shape shape;
shape.vertexCount = 4; shape.vertexCount = vertices;
shape.vertices = malloc(sizeof(t_vertex)*4); shape.vertices = malloc(sizeof(t_vertex)*vertices);
shape.color = rd;
return shape;
}
t_shape create_square(float x1,float y1,float x2, float y2)
{
t_shape shape = generic(4);
shape.type = RECTANGLE; shape.type = RECTANGLE;
shape.flag = 1; shape.flag = 1;
t_vertex* v = shape.vertices; t_vertex* v = shape.vertices;
...@@ -39,9 +49,7 @@ t_shape create_square(float x1,float y1,float x2, float y2) ...@@ -39,9 +49,7 @@ t_shape create_square(float x1,float y1,float x2, float y2)
t_shape create_circle(t_vertex start, t_vertex end) t_shape create_circle(t_vertex start, t_vertex end)
{ {
t_shape shape; t_shape shape = generic(2);
shape.vertexCount = 2;
shape.vertices = malloc(sizeof(t_vertex)*2);
shape.type = CIRCLE; shape.type = CIRCLE;
shape.flag = 1; shape.flag = 1;
t_vertex* v = shape.vertices; t_vertex* v = shape.vertices;
...@@ -52,9 +60,7 @@ t_shape create_circle(t_vertex start, t_vertex end) ...@@ -52,9 +60,7 @@ t_shape create_circle(t_vertex start, t_vertex end)
t_shape create_triangle(t_vertex p1,t_vertex p2, t_vertex p3) t_shape create_triangle(t_vertex p1,t_vertex p2, t_vertex p3)
{ {
t_shape shape; t_shape shape = generic(3);
shape.vertexCount = 3;
shape.vertices = malloc(sizeof(t_vertex)*3);
shape.type = TRIANGLE; shape.type = TRIANGLE;
shape.flag = 1; shape.flag = 1;
t_vertex* v = shape.vertices; t_vertex* v = shape.vertices;
...@@ -75,19 +81,6 @@ t_shape NULL_SHAPE() ...@@ -75,19 +81,6 @@ t_shape NULL_SHAPE()
return s; return s;
} }
void order_shape(t_shape* shape)
{
if(shape->vertexCount <= 1)
return;
for(int i = 0; i < shape->vertexCount; i++)
{
}
return;
}
void clear_shape(t_shape *shape) void clear_shape(t_shape *shape)
{ {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment