correctly renders selection
This commit is contained in:
115
src/main.odin
115
src/main.odin
@@ -87,7 +87,7 @@ main :: proc() {
|
|||||||
|
|
||||||
move_cursor()
|
move_cursor()
|
||||||
|
|
||||||
if repeatable_key_pressed(r.KeyboardKey.ENTER) {
|
if repeatable_key_pressed(.ENTER) {
|
||||||
new_line_cap := len(current_line()) - cursor.char
|
new_line_cap := len(current_line()) - cursor.char
|
||||||
bs := make([dynamic]u8, 0, len(current_line()) == 0 ? 1 : new_line_cap * 2)
|
bs := make([dynamic]u8, 0, len(current_line()) == 0 ? 1 : new_line_cap * 2)
|
||||||
if cursor.char < len(current_line()) {
|
if cursor.char < len(current_line()) {
|
||||||
@@ -101,7 +101,7 @@ main :: proc() {
|
|||||||
cursor.char = 0
|
cursor.char = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
if r.IsKeyDown(r.KeyboardKey.LEFT_SUPER) && r.IsKeyPressed(r.KeyboardKey.S) {
|
if r.IsKeyDown(.LEFT_SUPER) && r.IsKeyPressed(.S) {
|
||||||
builder := strings.builder_make()
|
builder := strings.builder_make()
|
||||||
defer strings.builder_destroy(&builder)
|
defer strings.builder_destroy(&builder)
|
||||||
for line in lines {
|
for line in lines {
|
||||||
@@ -112,7 +112,7 @@ main :: proc() {
|
|||||||
fmt.println("Wrote file!")
|
fmt.println("Wrote file!")
|
||||||
}
|
}
|
||||||
|
|
||||||
skip_backspace: if repeatable_key_pressed(r.KeyboardKey.BACKSPACE) {
|
skip_backspace: if repeatable_key_pressed(.BACKSPACE) {
|
||||||
if cursor.line == 0 && cursor.char == 0 do break skip_backspace
|
if cursor.line == 0 && cursor.char == 0 do break skip_backspace
|
||||||
if cursor.char == 0 {
|
if cursor.char == 0 {
|
||||||
// join lines
|
// join lines
|
||||||
@@ -124,7 +124,7 @@ main :: proc() {
|
|||||||
cursor.line -= 1
|
cursor.line -= 1
|
||||||
cursor.char = old_len
|
cursor.char = old_len
|
||||||
} else {
|
} else {
|
||||||
if r.IsKeyDown(r.KeyboardKey.LEFT_ALT) {
|
if r.IsKeyDown(.LEFT_ALT) {
|
||||||
// delete by word
|
// delete by word
|
||||||
delete_to, found := find_previous_space(cursor.char, current_line()[:])
|
delete_to, found := find_previous_space(cursor.char, current_line()[:])
|
||||||
if found {
|
if found {
|
||||||
@@ -169,14 +169,9 @@ main :: proc() {
|
|||||||
raw_data(line)[len(line)] = 0
|
raw_data(line)[len(line)] = 0
|
||||||
|
|
||||||
if selection.active &&
|
if selection.active &&
|
||||||
line_index == selection.start.line &&
|
selection_earliest().line <= line_index &&
|
||||||
line_index == selection.end.line {
|
line_index <= selection_latest().line {
|
||||||
render_line_with_selection(line[:], line_index, font)
|
render_line_with_selection(line[:], line_index, font)
|
||||||
} else if selection.active && line_index == selection.start.line {
|
|
||||||
} else if selection.active && line_index == selection.end.line {
|
|
||||||
} else if selection.active &&
|
|
||||||
selection.start.line < line_index &&
|
|
||||||
line_index < selection.end.line {
|
|
||||||
} else {
|
} else {
|
||||||
// No selection active
|
// No selection active
|
||||||
cstr := strings.unsafe_string_to_cstring(string(line[:]))
|
cstr := strings.unsafe_string_to_cstring(string(line[:]))
|
||||||
@@ -241,7 +236,7 @@ find_previous_space :: proc(current_position: int, line: []u8) -> (result: int,
|
|||||||
|
|
||||||
handle_selection_start :: proc() {
|
handle_selection_start :: proc() {
|
||||||
was_active := selection.active
|
was_active := selection.active
|
||||||
if r.IsKeyDown(r.KeyboardKey.LEFT_SHIFT) {
|
if r.IsKeyDown(.LEFT_SHIFT) {
|
||||||
selection.active = true
|
selection.active = true
|
||||||
} else {
|
} else {
|
||||||
selection = {}
|
selection = {}
|
||||||
@@ -259,25 +254,8 @@ handle_selection_end :: proc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
render_line_with_selection :: proc(line: []u8, line_index: int, font: r.Font) {
|
render_line_with_selection :: proc(line: []u8, line_index: int, font: r.Font) {
|
||||||
earliest: Position
|
earliest := selection_earliest()
|
||||||
latest: Position
|
latest := selection_latest()
|
||||||
if selection.start.line < selection.end.line {
|
|
||||||
// Started highlighting on a previous line, selecting downwards
|
|
||||||
earliest = selection.start
|
|
||||||
latest = selection.end
|
|
||||||
} else if selection.start.line > selection.end.line {
|
|
||||||
// Started highlighting on a later line, selecting upwards
|
|
||||||
earliest = selection.end
|
|
||||||
latest = selection.start
|
|
||||||
} else if selection.start.char < selection.end.char {
|
|
||||||
// Selection on one line, selecting rightwards
|
|
||||||
earliest = selection.start
|
|
||||||
latest = selection.end
|
|
||||||
} else {
|
|
||||||
// Selection on one line, selecting leftwards
|
|
||||||
earliest = selection.end
|
|
||||||
latest = selection.start
|
|
||||||
}
|
|
||||||
|
|
||||||
selection_begin: int
|
selection_begin: int
|
||||||
selection_end: int
|
selection_end: int
|
||||||
@@ -285,13 +263,14 @@ render_line_with_selection :: proc(line: []u8, line_index: int, font: r.Font) {
|
|||||||
selection_begin = min(selection.start.char, selection.end.char)
|
selection_begin = min(selection.start.char, selection.end.char)
|
||||||
selection_end = max(selection.start.char, selection.end.char)
|
selection_end = max(selection.start.char, selection.end.char)
|
||||||
} else if line_index == earliest.line {
|
} else if line_index == earliest.line {
|
||||||
|
selection_begin = earliest.char
|
||||||
|
selection_end = len(line)
|
||||||
} else if line_index == latest.line {
|
} else if line_index == latest.line {
|
||||||
|
selection_begin = 0
|
||||||
|
selection_end = latest.char
|
||||||
} else if earliest.line < line_index && line_index < latest.line {
|
} else if earliest.line < line_index && line_index < latest.line {
|
||||||
|
selection_begin = 0
|
||||||
} else {
|
selection_end = len(line)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
prefix := line[:selection_begin]
|
prefix := line[:selection_begin]
|
||||||
@@ -328,7 +307,7 @@ render_line_with_selection :: proc(line: []u8, line_index: int, font: r.Font) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
move_cursor :: proc() {
|
move_cursor :: proc() {
|
||||||
if repeatable_key_pressed(r.KeyboardKey.RIGHT) {
|
if repeatable_key_pressed(.RIGHT) {
|
||||||
handle_selection_start()
|
handle_selection_start()
|
||||||
defer handle_selection_end()
|
defer handle_selection_end()
|
||||||
preferred_position := cursor.char + 1
|
preferred_position := cursor.char + 1
|
||||||
@@ -336,7 +315,7 @@ move_cursor :: proc() {
|
|||||||
preferred_position = 0
|
preferred_position = 0
|
||||||
cursor.line += 1
|
cursor.line += 1
|
||||||
}
|
}
|
||||||
if r.IsKeyDown(r.KeyboardKey.LEFT_ALT) && cursor.char + 1 < len(current_line()) {
|
if r.IsKeyDown(.LEFT_ALT) && cursor.char + 1 < len(current_line()) {
|
||||||
seen_space := false
|
seen_space := false
|
||||||
for c, c_index in current_line()[cursor.char + 1:] {
|
for c, c_index in current_line()[cursor.char + 1:] {
|
||||||
if seen_space && !is_whitespace(c) {
|
if seen_space && !is_whitespace(c) {
|
||||||
@@ -354,7 +333,7 @@ move_cursor :: proc() {
|
|||||||
cursor.char = min(preferred_position, len(current_line()))
|
cursor.char = min(preferred_position, len(current_line()))
|
||||||
}
|
}
|
||||||
|
|
||||||
if repeatable_key_pressed(r.KeyboardKey.LEFT) {
|
if repeatable_key_pressed(.LEFT) {
|
||||||
handle_selection_start()
|
handle_selection_start()
|
||||||
defer handle_selection_end()
|
defer handle_selection_end()
|
||||||
preferred_position := cursor.char - 1
|
preferred_position := cursor.char - 1
|
||||||
@@ -362,19 +341,19 @@ move_cursor :: proc() {
|
|||||||
cursor.line -= 1
|
cursor.line -= 1
|
||||||
preferred_position = len(current_line())
|
preferred_position = len(current_line())
|
||||||
}
|
}
|
||||||
if r.IsKeyDown(r.KeyboardKey.LEFT_ALT) && 0 < cursor.char {
|
if r.IsKeyDown(.LEFT_ALT) && 0 < cursor.char {
|
||||||
found := false
|
found := false
|
||||||
preferred_position, found = find_previous_space(cursor.char, current_line()[:])
|
preferred_position, found = find_previous_space(cursor.char, current_line()[:])
|
||||||
}
|
}
|
||||||
cursor.char = max(preferred_position, 0)
|
cursor.char = max(preferred_position, 0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if repeatable_key_pressed(r.KeyboardKey.UP) {
|
if repeatable_key_pressed(.UP) {
|
||||||
handle_selection_start()
|
handle_selection_start()
|
||||||
defer handle_selection_end()
|
defer handle_selection_end()
|
||||||
preferred_line := cursor.line - 1
|
preferred_line := cursor.line - 1
|
||||||
|
|
||||||
if r.IsKeyDown(r.KeyboardKey.LEFT_ALT) && 0 < cursor.line {
|
if r.IsKeyDown(.LEFT_ALT) && 0 < cursor.line {
|
||||||
#reverse for l, l_index in lines[:cursor.line] {
|
#reverse for l, l_index in lines[:cursor.line] {
|
||||||
if len(l) == 0 || all_whitespace(l[:]) {
|
if len(l) == 0 || all_whitespace(l[:]) {
|
||||||
preferred_line = l_index
|
preferred_line = l_index
|
||||||
@@ -389,12 +368,12 @@ move_cursor :: proc() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if repeatable_key_pressed(r.KeyboardKey.DOWN) {
|
if repeatable_key_pressed(.DOWN) {
|
||||||
handle_selection_start()
|
handle_selection_start()
|
||||||
defer handle_selection_end()
|
defer handle_selection_end()
|
||||||
preferred_line := cursor.line + 1
|
preferred_line := cursor.line + 1
|
||||||
|
|
||||||
if r.IsKeyDown(r.KeyboardKey.LEFT_ALT) && cursor.line + 1 < len(lines) {
|
if r.IsKeyDown(.LEFT_ALT) && cursor.line + 1 < len(lines) {
|
||||||
for l, l_index in lines[cursor.line + 1:] {
|
for l, l_index in lines[cursor.line + 1:] {
|
||||||
if len(l) == 0 || all_whitespace(l[:]) {
|
if len(l) == 0 || all_whitespace(l[:]) {
|
||||||
preferred_line = cursor.line + l_index + 1
|
preferred_line = cursor.line + l_index + 1
|
||||||
@@ -409,3 +388,51 @@ move_cursor :: proc() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
selection_earliest :: proc() -> Position {
|
||||||
|
earliest: Position
|
||||||
|
latest: Position
|
||||||
|
if selection.start.line < selection.end.line {
|
||||||
|
// Started highlighting on a previous line, selecting downwards
|
||||||
|
earliest = selection.start
|
||||||
|
latest = selection.end
|
||||||
|
} else if selection.start.line > selection.end.line {
|
||||||
|
// Started highlighting on a later line, selecting upwards
|
||||||
|
earliest = selection.end
|
||||||
|
latest = selection.start
|
||||||
|
} else if selection.start.char < selection.end.char {
|
||||||
|
// Selection on one line, selecting rightwards
|
||||||
|
earliest = selection.start
|
||||||
|
latest = selection.end
|
||||||
|
} else {
|
||||||
|
// Selection on one line, selecting leftwards
|
||||||
|
earliest = selection.end
|
||||||
|
latest = selection.start
|
||||||
|
}
|
||||||
|
|
||||||
|
return earliest
|
||||||
|
}
|
||||||
|
|
||||||
|
selection_latest :: proc() -> Position {
|
||||||
|
earliest: Position
|
||||||
|
latest: Position
|
||||||
|
if selection.start.line < selection.end.line {
|
||||||
|
// Started highlighting on a previous line, selecting downwards
|
||||||
|
earliest = selection.start
|
||||||
|
latest = selection.end
|
||||||
|
} else if selection.start.line > selection.end.line {
|
||||||
|
// Started highlighting on a later line, selecting upwards
|
||||||
|
earliest = selection.end
|
||||||
|
latest = selection.start
|
||||||
|
} else if selection.start.char < selection.end.char {
|
||||||
|
// Selection on one line, selecting rightwards
|
||||||
|
earliest = selection.start
|
||||||
|
latest = selection.end
|
||||||
|
} else {
|
||||||
|
// Selection on one line, selecting leftwards
|
||||||
|
earliest = selection.end
|
||||||
|
latest = selection.start
|
||||||
|
}
|
||||||
|
|
||||||
|
return latest
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user