summaryrefslogtreecommitdiff
path: root/src/gui/pictureframe.c
blob: c6d7bf6889425ccf215f22037af86c7f04349fa7 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "gui/pictureframe.h"
#include <assert.h>
#include <MLV/MLV_all.h>
#include "common/mem.h"

/**
 * File: pictureframe.c
 *
 * Author:
 *   Adam NAILI
 */

static bool pictureframe_is_selected(int x, int y, PictureFrame *pictureFrame) {
  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;
}

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

static 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;
}

static void pictureframe_print(Component *parameterSelf) {
  PictureFrame *self = (PictureFrame *) parameterSelf;
  pictureframe_draw_canvas(self);
  if (mode != WAITING_BUTTON_HIDE && mode != RENDERING) {
    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_origin(p1.x, p1.y, self);
      pointToPrint2 = pictureframe_conversion_to_origin(p2.x, p2.y, self);
      pointToPrint3 = pictureframe_conversion_to_origin(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;
    }
  }
}

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

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

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_pic(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_pic(x_pos, y_pos, self);
    morphing_add_constraint(self->morphing, savedPoint, vector);
    mode = PRINTING;
  }
}

void pictureframe_draw_canvas(PictureFrame *pictureFrame) {
  MLV_draw_image(pictureFrame->canvas->mlv, pictureFrame->component.x_pos, pictureFrame->component.y_pos);
}

PictureFrame *pictureframe_create(int width, int height, int x_pos, int y_pos,
                                  CartesianMappingDivision cartesianMappingDivision, Morphing *morphing, Canvas *canvas,
                                  ClickHandler clickHandler) {
  assert(width > 0);
  assert(height > 0);
  assert(x_pos >= 0);
  assert(y_pos >= 0);
  assert(cartesianMappingDivision != NULL);
  assert(morphing != NULL);
  assert(canvas != NULL);
  PictureFrame *pictureFrame = malloc_or_die(sizeof(PictureFrame));
  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;
  return pictureFrame;
}

void pictureframe_destroy(PictureFrame *pictureFrame) {
  free(pictureFrame);
}