Repository navigation
Expand file tree
/
Copy pathcursor.c
More file actions
69 lines (62 loc) · 2.07 KB
/
Copy pathcursor.c
File metadata and controls
69 lines (62 loc) · 2.07 KB
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
#include "editor.h"
Coord cursor_get_coord_cs(int pos) {
Coord out = {pos % SCREEN.width_cs, pos / SCREEN.width_cs};
return out;
}
Coord cursor_get_coord_px(int pos) {
Coord out = cursor_get_coord_cs(pos);
out.x *= SCREEN.cursor_x_os;
out.x += SCREEN.margin_w_cs * SCREEN.cursor_x_os;
out.y *= SCREEN.cursor_y_os;
out.y += SCREEN.font_px;
return out;
}
// TODO use coord
void cursor_set_pos(Coord pos_px) {
cursor_clear();
int cursor_x_cs =
(pos_px.x - SCREEN.margin_w_cs * SCREEN.cursor_x_os) / SCREEN.cursor_x_os;
int cursor_y_cs = pos_px.y / SCREEN.cursor_y_os;
SCREEN.cursor = cursor_y_cs * SCREEN.width_cs + cursor_x_cs;
if (SCREEN.text[SCREEN.cursor] == 0) {
SCREEN.cursor = line_get_end_str(SCREEN.cursor);
}
cursor_render();
}
void cursor_render() {
Coord cursor_coords = cursor_get_coord_px(SCREEN.cursor);
js_fill_rect(cursor_coords.x, cursor_coords.y - (SCREEN.font_px / 1.2), 4,
SCREEN.font_px, SCREEN.colours.txt);
}
void cursor_clear() {
Coord cursor_coords = cursor_get_coord_px(SCREEN.cursor);
js_fill_rect(cursor_coords.x, cursor_coords.y - (SCREEN.font_px / 1.2), 4,
SCREEN.font_px, SCREEN.colours.bgrd);
}
void cursor_mov_lr(int d) {
int new_cur_pos;
// if cursor is at the end of a line of text
if (SCREEN.text[SCREEN.cursor] == 0 && d > 0) {
new_cur_pos = line_get_end(SCREEN.cursor);
// if cursor is at the start of a line
} else if (SCREEN.cursor % SCREEN.width_cs == 0 && d < 0) {
new_cur_pos = line_get_end_str(SCREEN.cursor - 1);
} else {
new_cur_pos = SCREEN.cursor + d;
}
if (new_cur_pos < 0 || new_cur_pos >= SCREEN.width_cs * SCREEN.height_cs) {
return;
}
SCREEN.cursor = new_cur_pos;
}
void cursor_mov_ud(int d) {
int line = SCREEN.cursor / SCREEN.width_cs;
if ((line == 0 && d < 0) || (line == SCREEN.height_cs - 1 && d > 0)) {
return;
}
int new_cur_pos = SCREEN.cursor + d * SCREEN.width_cs;
if (SCREEN.text[new_cur_pos] == 0 || SCREEN.text[SCREEN.cursor] == 0) {
new_cur_pos = line_get_end_str(new_cur_pos);
}
SCREEN.cursor = new_cur_pos;
}