~ruther/CTU-FEE-B0B35APO-Semestral-project

ref: 08e7267cfef7daf96eb4ccf2ef28c24791399be6 CTU-FEE-B0B35APO-Semestral-project/image-viewer/src/cursor.c -rw-r--r-- 2.6 KiB
08e7267c — František Boháček fix: makefile correct phony 3 years ago
                                                                                
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
#include "cursor.h"
#include "direction.h"
#include "display_utils.h"
#include "image.h"
#include "coords.h"

const display_pixel_t CURSOR_COLOR = {.fields = {.r = (uint8_t)DISPLAY_MAX_RED, .g = 0, .b = 0}};

cursor_t cursor_create() {
  cursor_t cursor = {
    .x = 0,
    .y = 0,
    .shown = false,
    .shown_at = 0,
  };

  return cursor;
}

void cursor_center(cursor_t *cursor, image_region_t region) {
  cursor->x = region.x + region.width / 2;
  cursor->y = region.y + region.height / 2;
}

bool cursor_move(cursor_t *cursor, image_region_t region, direction_t direction, int16_t amount) {
  int32_t x = cursor->x, y = cursor->y;
  direction_move_xy(direction, &x, &y, amount);

  if (x < region.x) {
    x = region.x;
  } else if (x > region.x + region.width - 1) {
    x = region.x + region.width - 1;
  }

  if (y < region.y) {
    y = region.y;
  } else if (y > region.y + region.height - 1) {
    y = region.y + region.height - 1;
  }

  bool moved = cursor->x != x || cursor->y != y;
  cursor->x = x;
  cursor->y = y;
  return moved;
}

void cursor_show(cursor_t *cursor, image_t *image, image_zoom_t zoom,
                 display_t *display) {
  cursor_hide(cursor, image, zoom, display);
  cursor->shown_at = time(NULL);
  cursor->shown = true;

  coords_t screen_coords =
      image_get_screen_coords(image, zoom, coords_create(cursor->x, cursor->y));
  uint16_t base_x = screen_coords.x;
  uint16_t base_y = screen_coords.y;

  uint16_t first_x = base_x - CURSOR_SIZE / 2;
  uint16_t first_y = base_y - CURSOR_SIZE / 2;

  for (int i = 0; i < CURSOR_SIZE; i++) {
    uint16_t x = first_x + i;
    uint16_t y = first_y + i;

    cursor->previous_display_data[i] = display_get_pixel(display, base_x, y);
    cursor->previous_display_data[i + CURSOR_SIZE] = display_get_pixel(display, x, base_y);

    display_set_pixel(display, base_x, y, CURSOR_COLOR);
    display_set_pixel(display, x, base_y, CURSOR_COLOR);
  }
}

void cursor_hide(cursor_t *cursor, image_t *image, image_zoom_t zoom,
                 display_t *display) {
  if (!cursor->shown) {
    return;
  }

  coords_t screen_coords =
      image_get_screen_coords(image, zoom, coords_create(cursor->x, cursor->y));
  uint16_t base_x = screen_coords.x;
  uint16_t base_y = screen_coords.y;

  uint16_t first_x = base_x - CURSOR_SIZE / 2;
  uint16_t first_y = base_y - CURSOR_SIZE / 2;

  for (int i = 0; i < CURSOR_SIZE; i++) {
    uint16_t x = first_x + i;
    uint16_t y = first_y + i;

    display_set_pixel(display, base_x, y, cursor->previous_display_data[i]);
    display_set_pixel(display, x, base_y, cursor->previous_display_data[i + CURSOR_SIZE]);
  }

  cursor->shown = false;
}
Do not follow this link