summaryrefslogtreecommitdiff
path: root/src/gui/pictureframe.c
blob: e4081a75fa8746eaa4650b5cf81de224ba1e991f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <assert.h>
#include <gui/pictureframe.h>
#include <MLV/MLV_all.h>
#include <common/time.h>

CartesianVector pictureframe_origin_split(const CartesianMapping *cartesianMapping) {
  return cartesianMapping->origin;
}

CartesianVector pictureframe_target_split(const CartesianMapping *cartesianMapping) {
  return cartesianMapping->target;
}

void pictureframe_print(Component *parameterSelf) {
  PictureFrame *self = (PictureFrame *) parameterSelf;
  MLV_draw_image(self->canvas->mlv, self->component.x_pos, self->component.y_pos);
  TriangleMap *p = self->morphing->first;
  CartesianVector p1;
  CartesianVector p2;
  CartesianVector p3;
  CartesianVector pointToPrint1;
  CartesianVector pointToPrint2;
  CartesianVector pointToPrint3;
  while(p != NULL){
    p1 = self->cartesianMappingDivision(&(p->vertices[0]));
    p2 = self->cartesianMappingDivision(&(p->vertices[1]));
    p3 = self->cartesianMappingDivision(&(p->vertices[2]));

    pointToPrint1 = pictureframe_conversion_to_picture(p1.x,p1.y,self);
    pointToPrint2 = pictureframe_conversion_to_picture(p2.x,p2.y,self);
    pointToPrint3 = pictureframe_conversion_to_picture(p3.x,p3.y,self);

    MLV_draw_filled_circle(pointToPrint1.x,pointToPrint1.y,2,MLV_COLOR_RED);
    MLV_draw_filled_circle(pointToPrint2.x,pointToPrint2.y,2,MLV_COLOR_RED);
    MLV_draw_filled_circle(pointToPrint3.x,pointToPrint3.y,2,MLV_COLOR_RED);

    MLV_draw_line(pointToPrint1.x,pointToPrint1.y,pointToPrint2.x,pointToPrint2.y,MLV_COLOR_RED);
    MLV_draw_line(pointToPrint1.x,pointToPrint1.y,pointToPrint3.x,pointToPrint3.y,MLV_COLOR_RED);
    MLV_draw_line(pointToPrint3.x,pointToPrint3.y,pointToPrint2.x,pointToPrint2.y,MLV_COLOR_RED);
    p = p->next;
  }
}

bool pictureframe_is_selected(int x, int y, PictureFrame *pictureFrame) {
  assert(x >= 0);
  assert(y >= 0);
  assert(pictureFrame != NULL);
  int x1 = pictureFrame->component.x_pos;
  int y1 = pictureFrame->component.y_pos;
  int x2 = pictureFrame->component.x_pos + pictureFrame->component.width;
  int y2 = pictureFrame->component.y_pos + pictureFrame->component.height;
  if (x >= x1 && x <= x2 && y >= y1 && y <= y2) {
    return true;
  }
  return false;
}

CartesianVector pictureframe_conversion_to_origin(int x, int y, PictureFrame *pictureFrame) {
  CartesianVector vector;
  vector.x = x - pictureFrame->component.x_pos;
  vector.y = y - pictureFrame->component.y_pos;
  return vector;
}

CartesianVector pictureframe_conversion_to_picture(int x, int y, PictureFrame *pictureFrame) {
  CartesianVector vector;
  vector.x = x + pictureFrame->component.x_pos;
  vector.y = y + pictureFrame->component.y_pos;
  return vector;
}

void pictureframe_click_handler_origin(int x_pos, int y_pos, Component *parameterSelf) {
  PictureFrame *self = (PictureFrame *) parameterSelf;
  if (pictureframe_is_selected(x_pos, y_pos, self) && mode == INSERT_ORIGIN) {
    CartesianVector vector = pictureframe_conversion_to_origin(x_pos,y_pos,self);
    MLV_draw_filled_circle(x_pos,y_pos,2,MLV_COLOR_BLUE);
    savedPoint = vector;
    MLV_actualise_window();
    mode = INSERT_TARGET;
  }
}

void pictureframe_click_handler_target(int x_pos, int y_pos, Component *parameterSelf) {
  PictureFrame *self = (PictureFrame *) parameterSelf;
  if (pictureframe_is_selected(x_pos, y_pos, self) && mode == INSERT_TARGET) {
    CartesianVector vector = pictureframe_conversion_to_origin(x_pos,y_pos,self);
    printf("(%d,%d) | (%d,%d)\n",savedPoint.x,savedPoint.y,vector.x,vector.y);
    morphing_add_constraint(self->morphing,savedPoint,vector);
    printf("OK\n");
    mode = PRINTING;
  }
}

void pictureframe_init(PictureFrame *pictureFrame, int width, int height, int x_pos, int y_pos,
                       CartesianMappingDivision cartesianMappingDivision, Morphing *morphing, Canvas *canvas,ClickHandler clickHandler) {
  assert(pictureFrame != NULL);
  assert(width > 0);
  assert(height > 0);
  assert(x_pos >= 0);
  assert(y_pos >= 0);
  assert(cartesianMappingDivision != NULL);
  assert(morphing != NULL);
  assert(canvas != NULL);
  pictureFrame->component.width = width;
  pictureFrame->component.height = height;
  pictureFrame->component.x_pos = x_pos;
  pictureFrame->component.y_pos = y_pos;
  pictureFrame->component.print_method = pictureframe_print;
  pictureFrame->component.click_handler = clickHandler;
  pictureFrame->morphing = morphing;
  pictureFrame->canvas = canvas;
  pictureFrame->cartesianMappingDivision = cartesianMappingDivision;
}