78 lines
1.7 KiB
C
78 lines
1.7 KiB
C
//
|
|
// Created by william on 1/12/24.
|
|
//
|
|
|
|
#include <assert.h>
|
|
#include "cursor.h"
|
|
|
|
void cursor_init(Cursor *cursor, WINDOW *window, int max_x, int max_y) {
|
|
cursor->window = window;
|
|
cursor->min_x = 1;
|
|
cursor->min_y = 1;
|
|
cursor->max_x = max_x;
|
|
cursor->max_y = max_y;
|
|
cursor->multiplier_x = 1;
|
|
cursor->multiplier_y = 1;
|
|
cursor->width = 1;
|
|
cursor->pos_x = 0;
|
|
cursor->pos_y = 0;
|
|
cursor->enabled = false;
|
|
}
|
|
|
|
void cursor_set_at(Cursor *cursor, int at) {
|
|
int win_x = cursor->min_x + cursor->pos_x * cursor->multiplier_x;
|
|
int win_y = cursor->min_y + cursor->pos_y * cursor->multiplier_y;
|
|
|
|
mvwchgat(cursor->window, win_y, win_x, cursor->width, at, 0, NULL);
|
|
}
|
|
|
|
void cursor_enable(Cursor *cursor) {
|
|
cursor_set_at(cursor, CURSOR_AT_ENABLED);
|
|
cursor->enabled = true;
|
|
}
|
|
|
|
void cursor_disable(Cursor *cursor) {
|
|
cursor_set_at(cursor, CURSOR_AT_DISABLED);
|
|
cursor->enabled = false;
|
|
}
|
|
|
|
void cursor_set_pos(Cursor *cursor, int x, int y) {
|
|
assert(x >= 0);
|
|
assert(y >= 0);
|
|
|
|
bool enabled = cursor->enabled;
|
|
if (enabled) {
|
|
// Remove the cursor from its old position
|
|
cursor_disable(cursor);
|
|
}
|
|
|
|
cursor->pos_x = x;
|
|
cursor->pos_y = y;
|
|
|
|
if (enabled) {
|
|
// Display the cursor in the new position
|
|
cursor_enable(cursor);
|
|
}
|
|
}
|
|
|
|
int cursor_limit(int value, int limit) {
|
|
if (value < 0) {
|
|
return 0;
|
|
}
|
|
|
|
if (value > limit) {
|
|
return limit;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
void cursor_move(Cursor *cursor, int offset_x, int offset_y) {
|
|
int x = cursor->pos_x + offset_x;
|
|
x = cursor_limit(x, cursor->max_x);
|
|
|
|
int y = cursor->pos_y + offset_y;
|
|
y = cursor_limit(y, cursor->max_y);
|
|
|
|
cursor_set_pos(cursor, x, y);
|
|
} |