create buffer list window that you can't interact with

plugins
Patrick Cleavelin 2023-12-29 17:31:39 -06:00
parent 845ed4beab
commit cbd0579697
4 changed files with 93 additions and 20 deletions

View File

@ -1,5 +1,7 @@
package core
import "vendor:raylib"
Mode :: enum {
Normal,
Insert,
@ -8,6 +10,13 @@ Mode :: enum {
State :: struct {
mode: Mode,
should_close: bool,
buffers: [dynamic]FileBuffer,
screen_height: i32,
screen_width: i32,
font: raylib.Font,
current_buffer: int,
buffers: [dynamic]FileBuffer,
buffer_list_window_is_visible: bool,
buffer_list_window_selected_buffer: int,
}

View File

@ -732,12 +732,17 @@ update_glyph_buffer :: proc(buffer: ^FileBuffer) {
}
}
draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, font: raylib.Font) {
draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, font: raylib.Font, show_line_numbers: bool = true) {
update_glyph_buffer(buffer);
color_buffer(buffer);
padding := 0;
if show_line_numbers {
padding = line_number_padding;
}
begin := buffer.top_line;
cursor_x := x + line_number_padding + buffer.cursor.col * source_font_width;
cursor_x := x + padding + buffer.cursor.col * source_font_width;
cursor_y := y + buffer.cursor.line * source_font_height;
cursor_y -= begin * source_font_height;
@ -760,7 +765,7 @@ draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, fon
}
if num_line_break > 0 {
cursor_x = x + line_number_padding + line_length * source_font_width;
cursor_x = x + padding + line_length * source_font_width;
cursor_y = cursor_y + num_line_break * source_font_height;
} else {
cursor_x += line_length * source_font_width;
@ -772,11 +777,12 @@ draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, fon
for j in 0..<buffer.glyph_buffer_height {
text_y := y + source_font_height * j;
// Line Numbers
if show_line_numbers {
raylib.DrawTextEx(font, raylib.TextFormat("%d", begin + j + 1), raylib.Vector2 { f32(x), f32(text_y) }, source_font_height, 0, raylib.DARKGRAY);
}
for i in 0..<buffer.glyph_buffer_width {
text_x := x + line_number_padding + i * source_font_width;
text_x := x + padding + i * source_font_width;
glyph := buffer.glyph_buffer[i + j * buffer.glyph_buffer_width];
if glyph.codepoint == 0 { break; }

View File

@ -87,7 +87,9 @@ switch_to_buffer :: proc(state: ^State, item: ^ui.MenuBarItem) {
}
main :: proc() {
state: State;
state := State {
buffer_list_window_is_visible = true,
};
for arg in os.args[1:] {
buffer, err := core.new_file_buffer(context.allocator, arg);
@ -114,6 +116,7 @@ main :: proc() {
raylib.SetExitKey(.KEY_NULL);
font := raylib.LoadFont("../c_editor/Mx437_ToshibaSat_8x16.ttf");
state.font = font;
menu_bar_state := ui.MenuBarState{
items = []ui.MenuBarItem {
ui.MenuBarItem {
@ -124,13 +127,13 @@ main :: proc() {
};
for !raylib.WindowShouldClose() && !state.should_close {
screen_width := raylib.GetScreenWidth();
screen_height := raylib.GetScreenHeight();
state.screen_width = raylib.GetScreenWidth();
state.screen_height = raylib.GetScreenHeight();
mouse_pos := raylib.GetMousePosition();
buffer := &state.buffers[state.current_buffer];
buffer.glyph_buffer_height = math.min(256, int((screen_height - 32 - core.source_font_height) / core.source_font_height));
buffer.glyph_buffer_height = math.min(256, int((state.screen_height - 32 - core.source_font_height) / core.source_font_height));
{
raylib.BeginDrawing();
@ -138,28 +141,31 @@ main :: proc() {
raylib.ClearBackground(theme.get_palette_raylib_color(.Background));
core.draw_file_buffer(&state, buffer, 32, core.source_font_height, font);
ui.draw_menu_bar(&menu_bar_state, 0, 0, screen_width, screen_height, font, core.source_font_height);
ui.draw_menu_bar(&menu_bar_state, 0, 0, state.screen_width, state.screen_height, font, core.source_font_height);
raylib.DrawRectangle(0, screen_height - core.source_font_height, screen_width, core.source_font_height, theme.get_palette_raylib_color(.Background2));
raylib.DrawRectangle(0, state.screen_height - core.source_font_height, state.screen_width, core.source_font_height, theme.get_palette_raylib_color(.Background2));
line_info_text := raylib.TextFormat("Line: %d, Col: %d --- Slice Index: %d, Content Index: %d", buffer.cursor.line + 1, buffer.cursor.col + 1, buffer.cursor.index.slice_index, buffer.cursor.index.content_index);
line_info_width := raylib.MeasureTextEx(font, line_info_text, core.source_font_height, 0).x;
switch state.mode {
case .Normal:
raylib.DrawRectangle(0, screen_height - core.source_font_height, 8 + len("NORMAL")*core.source_font_width, core.source_font_height, theme.get_palette_raylib_color(.Foreground4));
raylib.DrawRectangleV(raylib.Vector2 { f32(screen_width) - line_info_width - 8 , f32(screen_height - core.source_font_height) }, raylib.Vector2 { 8 + line_info_width, f32(core.source_font_height) }, theme.get_palette_raylib_color(.Foreground4));
raylib.DrawRectangle(0, state.screen_height - core.source_font_height, 8 + len("NORMAL")*core.source_font_width, core.source_font_height, theme.get_palette_raylib_color(.Foreground4));
raylib.DrawRectangleV(raylib.Vector2 { f32(state.screen_width) - line_info_width - 8 , f32(state.screen_height - core.source_font_height) }, raylib.Vector2 { 8 + line_info_width, f32(core.source_font_height) }, theme.get_palette_raylib_color(.Foreground4));
raylib.DrawTextEx(font, "NORMAL", raylib.Vector2 { 4, f32(screen_height - core.source_font_height) }, core.source_font_height, 0, theme.get_palette_raylib_color(.Background1));
raylib.DrawTextEx(font, "NORMAL", raylib.Vector2 { 4, f32(state.screen_height - core.source_font_height) }, core.source_font_height, 0, theme.get_palette_raylib_color(.Background1));
case .Insert:
raylib.DrawRectangle(0, screen_height - core.source_font_height, 8 + len("INSERT")*core.source_font_width, core.source_font_height, raylib.SKYBLUE);
raylib.DrawRectangleV(raylib.Vector2 { f32(screen_width) - line_info_width - 8 , f32(screen_height - core.source_font_height) }, raylib.Vector2 { 8 + line_info_width, f32(core.source_font_height) }, raylib.SKYBLUE);
raylib.DrawRectangle(0, state.screen_height - core.source_font_height, 8 + len("INSERT")*core.source_font_width, core.source_font_height, theme.get_palette_raylib_color(.Foreground2));
raylib.DrawRectangleV(raylib.Vector2 { f32(state.screen_width) - line_info_width - 8 , f32(state.screen_height - core.source_font_height) }, raylib.Vector2 { 8 + line_info_width, f32(core.source_font_height) }, theme.get_palette_raylib_color(.Foreground2));
raylib.DrawTextEx(font, "INSERT", raylib.Vector2 { 4, f32(screen_height - core.source_font_height) }, core.source_font_height, 0, raylib.DARKBLUE);
raylib.DrawTextEx(font, "INSERT", raylib.Vector2 { 4, f32(state.screen_height - core.source_font_height) }, core.source_font_height, 0, theme.get_palette_raylib_color(.Background1));
}
raylib.DrawTextEx(font, line_info_text, raylib.Vector2 { f32(state.screen_width) - line_info_width - 4, f32(state.screen_height - core.source_font_height) }, core.source_font_height, 0, theme.get_palette_raylib_color(.Background1));
raylib.DrawTextEx(font, line_info_text, raylib.Vector2 { f32(screen_width) - line_info_width - 4, f32(screen_height - core.source_font_height) }, core.source_font_height, 0, theme.get_palette_raylib_color(.Background1));
if state.buffer_list_window_is_visible {
ui.draw_buffer_list_window(&state);
}
}
switch state.mode {

View File

@ -0,0 +1,52 @@
package ui;
import "core:math"
import "vendor:raylib"
import "../core"
import "../theme"
draw_buffer_list_window :: proc(state: ^core.State) {
win_rec := raylib.Rectangle {
x = f32(state.screen_width/8),
y = f32(state.screen_height/8),
width = f32(state.screen_width - state.screen_width/4),
height = f32(state.screen_height - state.screen_height/4),
};
raylib.DrawRectangleRec(
win_rec,
theme.get_palette_raylib_color(.Background4));
win_margin := raylib.Vector2 { f32(text_padding*2), f32(core.source_font_height) };
buffer_prev_width := (win_rec.width - win_margin.x*2) / 2;
buffer_prev_height := win_rec.height - win_margin.y*2;
glyph_buffer_width := int(buffer_prev_width) / core.source_font_width - 1;
glyph_buffer_height := int(buffer_prev_height) / core.source_font_height;
raylib.DrawRectangle(
i32(win_rec.x + win_rec.width / 2),
i32(win_rec.y + win_margin.y),
i32(buffer_prev_width),
i32(buffer_prev_height),
theme.get_palette_raylib_color(.Background2));
for _, index in state.buffers {
buffer := &state.buffers[index];
raylib.DrawTextEx(
state.font,
raylib.TextFormat("%s:%d", buffer.file_path, buffer.cursor.line+1),
raylib.Vector2 { win_rec.x + win_margin.x, win_rec.y + win_margin.y + f32(index * core.source_font_height) },
core.source_font_height,
0,
theme.get_palette_raylib_color(.Foreground2));
if index == state.buffer_list_window_selected_buffer {
buffer.glyph_buffer_height = glyph_buffer_height;
buffer.glyph_buffer_width = glyph_buffer_width;
core.draw_file_buffer(state, buffer, int(win_rec.x + win_margin.x + win_rec.width / 2), int(win_rec.y + win_margin.y), state.font, show_line_numbers = false);
}
}
}