diff options
Diffstat (limited to 'src/gui/pictureframe.c')
-rw-r--r-- | src/gui/pictureframe.c | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/src/gui/pictureframe.c b/src/gui/pictureframe.c new file mode 100644 index 0000000..4126f59 --- /dev/null +++ b/src/gui/pictureframe.c | |||
@@ -0,0 +1,117 @@ | |||
1 | #include <assert.h> | ||
2 | #include <gui/pictureframe.h> | ||
3 | #include <MLV/MLV_all.h> | ||
4 | |||
5 | CartesianVector pictureframe_origin_split(const CartesianMapping *cartesianMapping) { | ||
6 | return cartesianMapping->origin; | ||
7 | } | ||
8 | |||
9 | CartesianVector pictureframe_target_split(const CartesianMapping *cartesianMapping) { | ||
10 | return cartesianMapping->target; | ||
11 | } | ||
12 | |||
13 | void pictureframe_draw_canvas(PictureFrame *pictureFrame){ | ||
14 | MLV_draw_image(pictureFrame->canvas->mlv, pictureFrame->component.x_pos, pictureFrame->component.y_pos); | ||
15 | } | ||
16 | |||
17 | void pictureframe_print(Component *parameterSelf) { | ||
18 | PictureFrame *self = (PictureFrame *) parameterSelf; | ||
19 | pictureframe_draw_canvas(self); | ||
20 | if (mode != WAITING_BUTTON_HIDE) { | ||
21 | TriangleMap *p = self->morphing->first; | ||
22 | CartesianVector p1; | ||
23 | CartesianVector p2; | ||
24 | CartesianVector p3; | ||
25 | CartesianVector pointToPrint1; | ||
26 | CartesianVector pointToPrint2; | ||
27 | CartesianVector pointToPrint3; | ||
28 | while (p != NULL) { | ||
29 | p1 = self->cartesianMappingDivision(&(p->vertices[0])); | ||
30 | p2 = self->cartesianMappingDivision(&(p->vertices[1])); | ||
31 | p3 = self->cartesianMappingDivision(&(p->vertices[2])); | ||
32 | |||
33 | pointToPrint1 = pictureframe_conversion_to_picture(p1.x, p1.y, self); | ||
34 | pointToPrint2 = pictureframe_conversion_to_picture(p2.x, p2.y, self); | ||
35 | pointToPrint3 = pictureframe_conversion_to_picture(p3.x, p3.y, self); | ||
36 | |||
37 | MLV_draw_filled_circle(pointToPrint1.x, pointToPrint1.y, 2, MLV_COLOR_RED); | ||
38 | MLV_draw_filled_circle(pointToPrint2.x, pointToPrint2.y, 2, MLV_COLOR_RED); | ||
39 | MLV_draw_filled_circle(pointToPrint3.x, pointToPrint3.y, 2, MLV_COLOR_RED); | ||
40 | |||
41 | MLV_draw_line(pointToPrint1.x, pointToPrint1.y, pointToPrint2.x, pointToPrint2.y, MLV_COLOR_RED); | ||
42 | MLV_draw_line(pointToPrint1.x, pointToPrint1.y, pointToPrint3.x, pointToPrint3.y, MLV_COLOR_RED); | ||
43 | MLV_draw_line(pointToPrint3.x, pointToPrint3.y, pointToPrint2.x, pointToPrint2.y, MLV_COLOR_RED); | ||
44 | p = p->next; | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | bool pictureframe_is_selected(int x, int y, PictureFrame *pictureFrame) { | ||
50 | assert(x >= 0); | ||
51 | assert(y >= 0); | ||
52 | assert(pictureFrame != NULL); | ||
53 | int x1 = pictureFrame->component.x_pos; | ||
54 | int y1 = pictureFrame->component.y_pos; | ||
55 | int x2 = pictureFrame->component.x_pos + pictureFrame->component.width; | ||
56 | int y2 = pictureFrame->component.y_pos + pictureFrame->component.height; | ||
57 | if (x >= x1 && x <= x2 && y >= y1 && y <= y2) { | ||
58 | return true; | ||
59 | } | ||
60 | return false; | ||
61 | } | ||
62 | |||
63 | CartesianVector pictureframe_conversion_to_origin(int x, int y, PictureFrame *pictureFrame) { | ||
64 | CartesianVector vector; | ||
65 | vector.x = x - pictureFrame->component.x_pos; | ||
66 | vector.y = y - pictureFrame->component.y_pos; | ||
67 | return vector; | ||
68 | } | ||
69 | |||
70 | CartesianVector pictureframe_conversion_to_picture(int x, int y, PictureFrame *pictureFrame) { | ||
71 | CartesianVector vector; | ||
72 | vector.x = x + pictureFrame->component.x_pos; | ||
73 | vector.y = y + pictureFrame->component.y_pos; | ||
74 | return vector; | ||
75 | } | ||
76 | |||
77 | void pictureframe_click_handler_origin(int x_pos, int y_pos, Component *parameterSelf) { | ||
78 | PictureFrame *self = (PictureFrame *) parameterSelf; | ||
79 | if (pictureframe_is_selected(x_pos, y_pos, self) && mode == INSERT_ORIGIN) { | ||
80 | CartesianVector vector = pictureframe_conversion_to_origin(x_pos, y_pos, self); | ||
81 | MLV_draw_filled_circle(x_pos, y_pos, 2, MLV_COLOR_BLUE); | ||
82 | savedPoint = vector; | ||
83 | MLV_actualise_window(); | ||
84 | mode = INSERT_TARGET; | ||
85 | } | ||
86 | } | ||
87 | |||
88 | void pictureframe_click_handler_target(int x_pos, int y_pos, Component *parameterSelf) { | ||
89 | PictureFrame *self = (PictureFrame *) parameterSelf; | ||
90 | if (pictureframe_is_selected(x_pos, y_pos, self) && mode == INSERT_TARGET) { | ||
91 | CartesianVector vector = pictureframe_conversion_to_origin(x_pos, y_pos, self); | ||
92 | morphing_add_constraint(self->morphing, savedPoint, vector); | ||
93 | mode = PRINTING; | ||
94 | } | ||
95 | } | ||
96 | |||
97 | void pictureframe_init(PictureFrame *pictureFrame, int width, int height, int x_pos, int y_pos, | ||
98 | CartesianMappingDivision cartesianMappingDivision, Morphing *morphing, Canvas *canvas, | ||
99 | ClickHandler clickHandler) { | ||
100 | assert(pictureFrame != NULL); | ||
101 | assert(width > 0); | ||
102 | assert(height > 0); | ||
103 | assert(x_pos >= 0); | ||
104 | assert(y_pos >= 0); | ||
105 | assert(cartesianMappingDivision != NULL); | ||
106 | assert(morphing != NULL); | ||
107 | assert(canvas != NULL); | ||
108 | pictureFrame->component.width = width; | ||
109 | pictureFrame->component.height = height; | ||
110 | pictureFrame->component.x_pos = x_pos; | ||
111 | pictureFrame->component.y_pos = y_pos; | ||
112 | pictureFrame->component.print_method = pictureframe_print; | ||
113 | pictureFrame->component.click_handler = clickHandler; | ||
114 | pictureFrame->morphing = morphing; | ||
115 | pictureFrame->canvas = canvas; | ||
116 | pictureFrame->cartesianMappingDivision = cartesianMappingDivision; | ||
117 | } \ No newline at end of file | ||