Implement decently fast selection

This commit is contained in:
Krzosa Karol
2024-07-20 07:56:56 +02:00
parent 0071136146
commit adeb2de366
2 changed files with 29 additions and 17 deletions

View File

@@ -27,14 +27,23 @@ void HandleKeybindings(View *_view) {
view.scroll.y -= GetMouseWheelMove() * 48;
}
if (IsKeyPressed(KEY_DOWN) || IsKeyPressedRepeat(KEY_DOWN)) {
For(view.cursors) {
if (IsKeyDown(KEY_LEFT_SHIFT)) {
Int front = GetFront(it);
Int new_front = MovePos(buf, front, {0, 1});
it = ChangeFront(it, new_front);
} else {
it.range.max = it.range.min = MovePos(buf, it.range.max, {0, 1});
int keys[4] = {KEY_RIGHT, KEY_LEFT, KEY_DOWN, KEY_UP};
XY keys_offset[4] = {
{ 1, 0},
{-1, 0},
{ 0, 1},
{ 0, -1}
};
for (int i = 0; i < 4; i += 1) {
if (IsKeyPressed(keys[i]) || IsKeyPressedRepeat(keys[i])) {
For(view.cursors) {
if (IsKeyDown(KEY_LEFT_SHIFT)) {
Int front = GetFront(it);
Int new_front = MovePos(buf, front, keys_offset[i]);
it = ChangeFront(it, new_front);
} else {
it.range.max = it.range.min = MovePos(buf, it.range.max, keys_offset[i]);
}
}
}
}

View File

@@ -45,25 +45,28 @@ void DrawCursor(const View &view, Cursor it) {
DrawCursor(view, bxy, 1, GREEN);
// Draw selection
{
if (front != back) {
XY min = PosToXY(buf, it.range.min);
XY max = PosToXY(buf, it.range.max);
Rect2I vlines = GetVisibleCells(view);
vlines.min.y = Clamp(vlines.min.y, min.line, max.line);
vlines.max.y = Clamp(vlines.max.y, min.line, max.line);
for (Int line = vlines.min.y; line < vlines.max.y; line += 1) {
for (Int line = vlines.min.y; line <= vlines.max.y; line += 1) {
Range range = GetLine(buf, line);
range -= range.min;
range.min = Clamp(range.min, vlines.min.x, vlines.max.x);
range.max = Clamp(range.max, vlines.min.x, vlines.max.x);
for (Int col = range.min; col < range.max; col += 1) {
Vec2 pos = {col * view.char_spacing, line * view.line_spacing};
pos -= view.scroll;
Rectangle rectangle = {pos.x, pos.y, view.char_spacing, view.line_spacing};
DrawRectangleRec(rectangle, {0, 50, 150, 20});
bool a = line > min.line && line < max.line;
bool b = line == min.line && col >= min.col;
bool c = line == max.line && col < max.col;
if (a || b || c) {
Vec2 pos = {col * view.char_spacing, line * view.line_spacing};
pos -= view.scroll;
Rectangle rectangle = {pos.x, pos.y, view.char_spacing, view.line_spacing};
DrawRectangleRec(rectangle, {0, 50, 150, 20});
}
}
}
}