allow font size to be configured at runtime

plugins
Patrick Cleavelin 2023-12-30 20:29:37 -06:00
parent 8160038fd3
commit 30729b908c
6 changed files with 162 additions and 85 deletions

View File

@ -11,10 +11,14 @@ Mode :: enum {
State :: struct {
mode: Mode,
should_close: bool,
screen_height: i32,
screen_width: i32,
screen_height: int,
screen_width: int,
font: raylib.Font,
source_font_width: int,
source_font_height: int,
line_number_padding: int,
current_buffer: int,
buffers: [dynamic]FileBuffer,

View File

@ -11,10 +11,6 @@ import "vendor:raylib"
import "../theme"
source_font_width :: 8;
source_font_height :: 16;
line_number_padding :: 5 * source_font_width;
ScrollDir :: enum {
Up,
Down,
@ -738,20 +734,20 @@ draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, fon
padding := 0;
if show_line_numbers {
padding = line_number_padding;
padding = state.source_font_width * 5;
}
begin := buffer.top_line;
cursor_x := x + padding + buffer.cursor.col * source_font_width;
cursor_y := y + buffer.cursor.line * source_font_height;
cursor_x := x + padding + buffer.cursor.col * state.source_font_width;
cursor_y := y + buffer.cursor.line * state.source_font_height;
cursor_y -= begin * source_font_height;
cursor_y -= begin * state.source_font_height;
// draw cursor
if state.mode == .Normal {
raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), source_font_width, source_font_height, raylib.BLUE);
raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), i32(state.source_font_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Background4));
} else if state.mode == .Insert {
raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), source_font_width, source_font_height, raylib.GREEN);
raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), i32(state.source_font_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Green));
num_line_break := 0;
line_length := 0;
@ -765,29 +761,29 @@ draw_file_buffer :: proc(state: ^State, buffer: ^FileBuffer, x: int, y: int, fon
}
if num_line_break > 0 {
cursor_x = x + padding + line_length * source_font_width;
cursor_y = cursor_y + num_line_break * source_font_height;
cursor_x = x + padding + line_length * state.source_font_width;
cursor_y = cursor_y + num_line_break * state.source_font_height;
} else {
cursor_x += line_length * source_font_width;
cursor_x += line_length * state.source_font_width;
}
raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), source_font_width, source_font_height, raylib.BLUE);
raylib.DrawRectangle(i32(cursor_x), i32(cursor_y), i32(state.source_font_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Blue));
}
for j in 0..<buffer.glyph_buffer_height {
text_y := y + source_font_height * j;
text_y := y + state.source_font_height * j;
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);
raylib.DrawTextEx(font, raylib.TextFormat("%d", begin + j + 1), raylib.Vector2 { f32(x), f32(text_y) }, f32(state.source_font_height), 0, theme.get_palette_raylib_color(.Background3));
}
for i in 0..<buffer.glyph_buffer_width {
text_x := x + padding + i * source_font_width;
text_x := x + padding + i * state.source_font_width;
glyph := buffer.glyph_buffer[i + j * buffer.glyph_buffer_width];
if glyph.codepoint == 0 { break; }
raylib.DrawTextCodepoint(font, rune(glyph.codepoint), raylib.Vector2 { f32(text_x), f32(text_y) }, source_font_height, theme.get_palette_raylib_color(glyph.color));
raylib.DrawTextCodepoint(font, rune(glyph.codepoint), raylib.Vector2 { f32(text_x), f32(text_y) }, f32(state.source_font_height), theme.get_palette_raylib_color(glyph.color));
}
}
}

View File

@ -88,6 +88,9 @@ register_default_leader_actions :: proc(input_map: ^core.InputMap) {
state.buffer_list_window_is_visible = true;
state.current_input_map = &state.buffer_list_window_input_map;
}, "show list of open buffers");
core.register_key_action(input_map, .Q, proc(state: ^State) {
state.current_input_map = &state.input_map;
}, "close this help");
}
register_default_input_actions :: proc(input_map: ^core.InputMap) {
@ -115,6 +118,24 @@ register_default_input_actions :: proc(input_map: ^core.InputMap) {
core.scroll_file_buffer(&state.buffers[state.current_buffer], .Down);
}, "scroll buffer up");
// Scale font size
core.register_ctrl_key_action(input_map, .MINUS, proc(state: ^State) {
if state.source_font_height > 16 {
state.source_font_height -= 2;
state.source_font_width = state.source_font_height / 2;
state.font = raylib.LoadFontEx("/Users/temp/Library/Fonts/JetBrainsMono-Regular.ttf", i32(state.source_font_height*2), nil, 0);
raylib.SetTextureFilter(state.font.texture, .BILINEAR);
}
}, "increase font size");
core.register_ctrl_key_action(input_map, .EQUAL, proc(state: ^State) {
state.source_font_height += 2;
state.source_font_width = state.source_font_height / 2;
state.font = raylib.LoadFontEx("/Users/temp/Library/Fonts/JetBrainsMono-Regular.ttf", i32(state.source_font_height*2), nil, 0);
raylib.SetTextureFilter(state.font.texture, .BILINEAR);
}, "decrease font size");
core.register_key_action(input_map, .I, proc(state: ^State) {
state.mode = .Insert;
}, "enter insert mode");
@ -153,6 +174,8 @@ register_buffer_list_input_actions :: proc(input_map: ^core.InputMap) {
main :: proc() {
state := State {
source_font_width = 8,
source_font_height = 16,
input_map = core.new_input_map(),
buffer_list_window_input_map = core.new_input_map(),
};
@ -184,8 +207,8 @@ main :: proc() {
raylib.SetTargetFPS(60);
raylib.SetExitKey(.KEY_NULL);
font := raylib.LoadFont("../c_editor/Mx437_ToshibaSat_8x16.ttf");
state.font = font;
state.font = raylib.LoadFont("../c_editor/Mx437_ToshibaSat_8x16.ttf");
raylib.SetTextureFilter(state.font.texture, .BILINEAR);
menu_bar_state := ui.MenuBarState{
items = []ui.MenuBarItem {
ui.MenuBarItem {
@ -196,24 +219,24 @@ main :: proc() {
};
for !raylib.WindowShouldClose() && !state.should_close {
state.screen_width = raylib.GetScreenWidth();
state.screen_height = raylib.GetScreenHeight();
state.screen_width = int(raylib.GetScreenWidth());
state.screen_height = int(raylib.GetScreenHeight());
mouse_pos := raylib.GetMousePosition();
buffer := &state.buffers[state.current_buffer];
buffer.glyph_buffer_height = math.min(256, int((state.screen_height - core.source_font_height*2) / core.source_font_height)) + 1;
buffer.glyph_buffer_width = math.min(256, int((state.screen_width - core.source_font_width) / core.source_font_width));
buffer.glyph_buffer_height = math.min(256, int((state.screen_height - state.source_font_height*2) / state.source_font_height)) + 1;
buffer.glyph_buffer_width = math.min(256, int((state.screen_width - state.source_font_width) / state.source_font_width));
{
raylib.BeginDrawing();
defer raylib.EndDrawing();
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, state.screen_width, state.screen_height, font, core.source_font_height);
core.draw_file_buffer(&state, buffer, 32, state.source_font_height, state.font);
ui.draw_menu_bar(&state, &menu_bar_state, 0, 0, i32(state.screen_width), i32(state.screen_height), state.source_font_height);
raylib.DrawRectangle(0, state.screen_height - core.source_font_height, state.screen_width, core.source_font_height, theme.get_palette_raylib_color(.Background2));
raylib.DrawRectangle(0, i32(state.screen_height - state.source_font_height), i32(state.screen_width), i32(state.source_font_height), theme.get_palette_raylib_color(.Background2));
line_info_text := raylib.TextFormat(
"Line: %d, Col: %d --- Slice Index: %d, Content Index: %d",
@ -221,52 +244,52 @@ main :: proc() {
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;
line_info_width := raylib.MeasureTextEx(state.font, line_info_text, f32(state.source_font_height), 0).x;
switch state.mode {
case .Normal:
raylib.DrawRectangle(
0,
state.screen_height - core.source_font_height,
8 + len("NORMAL")*core.source_font_width,
core.source_font_height,
i32(state.screen_height - state.source_font_height),
i32(8 + len("NORMAL")*state.source_font_width),
i32(state.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) },
raylib.Vector2 { f32(state.screen_width) - line_info_width - 8, f32(state.screen_height - state.source_font_height) },
raylib.Vector2 { 8 + line_info_width, f32(state.source_font_height) },
theme.get_palette_raylib_color(.Foreground4));
raylib.DrawTextEx(
font,
state.font,
"NORMAL",
raylib.Vector2 { 4, f32(state.screen_height - core.source_font_height) },
core.source_font_height,
raylib.Vector2 { 4, f32(state.screen_height - state.source_font_height) },
f32(state.source_font_height),
0,
theme.get_palette_raylib_color(.Background1));
case .Insert:
raylib.DrawRectangle(
0,
state.screen_height - core.source_font_height,
8 + len("INSERT")*core.source_font_width,
core.source_font_height,
i32(state.screen_height - state.source_font_height),
i32(8 + len("INSERT")*state.source_font_width),
i32(state.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) },
raylib.Vector2 { f32(state.screen_width) - line_info_width - 8, f32(state.screen_height - state.source_font_height) },
raylib.Vector2 { 8 + line_info_width, f32(state.source_font_height) },
theme.get_palette_raylib_color(.Foreground2));
raylib.DrawTextEx(
font,
state.font,
"INSERT",
raylib.Vector2 { 4, f32(state.screen_height - core.source_font_height) },
core.source_font_height,
raylib.Vector2 { 4, f32(state.screen_height - state.source_font_height) },
f32(state.source_font_height),
0,
theme.get_palette_raylib_color(.Background1));
}
raylib.DrawTextEx(
font,
state.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,
raylib.Vector2 { f32(state.screen_width) - line_info_width - 4, f32(state.screen_height - state.source_font_height) },
f32(state.source_font_height),
0,
theme.get_palette_raylib_color(.Background1));
@ -281,24 +304,40 @@ main :: proc() {
longest_description = len(action.description);
}
}
longest_description += 4;
for key, action in state.current_input_map.ctrl_key_actions {
if len(action.description) > longest_description {
longest_description = len(action.description);
}
}
longest_description += 8;
helper_height := i32(core.source_font_height * len(state.current_input_map.key_actions));
helper_height := state.source_font_height * (len(state.current_input_map.key_actions) + len(state.current_input_map.ctrl_key_actions));
offset_from_bottom := state.source_font_height * 2;
raylib.DrawRectangle(
state.screen_width - i32(longest_description * core.source_font_width),
state.screen_height - helper_height - 20,
i32(longest_description*core.source_font_width),
helper_height,
i32(state.screen_width - longest_description * state.source_font_width),
i32(state.screen_height - helper_height - offset_from_bottom),
i32(longest_description*state.source_font_width),
i32(helper_height),
theme.get_palette_raylib_color(.Background2));
index := 0;
for key, action in state.current_input_map.key_actions {
raylib.DrawTextEx(
font,
state.font,
raylib.TextFormat("%s - %s", key, action.description),
raylib.Vector2 { f32(state.screen_width - i32(longest_description * core.source_font_width)), f32(state.screen_height - helper_height + i32((index) * core.source_font_height) - 20) },
core.source_font_height,
raylib.Vector2 { f32(state.screen_width - longest_description * state.source_font_width), f32(state.screen_height - helper_height + index * state.source_font_height - offset_from_bottom) },
f32(state.source_font_height),
0,
theme.get_palette_raylib_color(.Foreground1));
index += 1;
}
for key, action in state.current_input_map.ctrl_key_actions {
raylib.DrawTextEx(
state.font,
raylib.TextFormat("<C>-%s - %s", key, action.description),
raylib.Vector2 { f32(state.screen_width - longest_description * state.source_font_width), f32(state.screen_height - helper_height + index * state.source_font_height - offset_from_bottom) },
f32(state.source_font_height),
0,
theme.get_palette_raylib_color(.Foreground1));
index += 1;
@ -313,6 +352,6 @@ main :: proc() {
do_insert_mode(&state, buffer);
}
ui.test_menu_bar(&state, &menu_bar_state, 0,0, mouse_pos, raylib.IsMouseButtonReleased(.LEFT), font, core.source_font_height);
ui.test_menu_bar(&state, &menu_bar_state, 0,0, mouse_pos, raylib.IsMouseButtonReleased(.LEFT), state.source_font_height);
}
}

View File

@ -65,7 +65,39 @@ palette := []u32 {
0x928374ff,
};
light_palette := []u32 {
0xfbf1c7ff,
0x3c3836ff,
0xebdbb2ff,
0xd5c4a1ff,
0xbdae93ff,
0xa89984ff,
0x3c3836ff,
0x504945ff,
0x665c54ff,
0x7c6f64ff,
0xcc241dff,
0x98971aff,
0xd79921ff,
0x458588ff,
0xb16286ff,
0x689d6aff,
0x7c6f64ff,
0x9d0006ff,
0x79740eff,
0xb57614ff,
0x076678ff,
0x8f3f71ff,
0x427b58ff,
0x928374ff,
};
get_palette_raylib_color :: proc(palette_color: PaletteColor) -> raylib.Color {
return raylib.GetColor(palette[palette_color]);
return raylib.GetColor(light_palette[palette_color]);
}

View File

@ -17,13 +17,13 @@ draw_buffer_list_window :: proc(state: ^core.State) {
win_rec,
theme.get_palette_raylib_color(.Background4));
win_margin := raylib.Vector2 { f32(text_padding*2), f32(core.source_font_height) };
win_margin := raylib.Vector2 { f32(text_padding*2), f32(state.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;
glyph_buffer_width := int(buffer_prev_width) / state.source_font_width - 1;
glyph_buffer_height := int(buffer_prev_height) / state.source_font_height;
raylib.DrawRectangle(
i32(win_rec.x + win_rec.width / 2),
@ -35,26 +35,32 @@ draw_buffer_list_window :: proc(state: ^core.State) {
for _, index in state.buffers {
buffer := &state.buffers[index];
text := raylib.TextFormat("%s:%d", buffer.file_path, buffer.cursor.line+1);
text_width := raylib.MeasureTextEx(state.font, text, core.source_font_height, 0);
text_width := raylib.MeasureTextEx(state.font, text, f32(state.source_font_height), 0);
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);
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);
raylib.DrawRectangle(
i32(win_rec.x + win_margin.x),
i32(win_rec.y + win_margin.y) + i32(index * core.source_font_height),
i32(win_rec.y + win_margin.y) + i32(index * state.source_font_height),
i32(text_width.x),
core.source_font_height,
i32(state.source_font_height),
theme.get_palette_raylib_color(.Background2));
}
raylib.DrawTextEx(
state.font,
text,
raylib.Vector2 { win_rec.x + win_margin.x, win_rec.y + win_margin.y + f32(index * core.source_font_height) },
core.source_font_height,
raylib.Vector2 { win_rec.x + win_margin.x, win_rec.y + win_margin.y + f32(index * state.source_font_height) },
f32(state.source_font_height),
0,
theme.get_palette_raylib_color(.Foreground2));
}

View File

@ -21,7 +21,7 @@ MenuBarState :: struct {
items: []MenuBarItem,
}
draw_menu_bar_item :: proc(item: ^MenuBarItem, x, y: i32, parent_width, parent_height: i32, font: raylib.Font, font_height: int, horizontal: bool = false) {
draw_menu_bar_item :: proc(state: ^core.State, item: ^MenuBarItem, x, y: i32, parent_width, parent_height: i32, font_height: int, horizontal: bool = false) {
foreground_color := theme.PaletteColor.Foreground3;
if horizontal {
if item.selected {
@ -32,10 +32,10 @@ draw_menu_bar_item :: proc(item: ^MenuBarItem, x, y: i32, parent_width, parent_h
}
item_text := raylib.TextFormat("%s", item.text);
item_width := raylib.MeasureTextEx(font, item_text, f32(font_height), 0).x;
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
raylib.DrawRectangle(x, y, parent_width, i32(font_height), theme.get_palette_raylib_color(foreground_color));
raylib.DrawTextEx(font, item_text, raylib.Vector2 { f32(x + text_padding), f32(y) }, f32(font_height), 0, theme.get_palette_raylib_color(.Background1));
raylib.DrawTextEx(state.font, item_text, raylib.Vector2 { f32(x + text_padding), f32(y) }, f32(font_height), 0, theme.get_palette_raylib_color(.Background1));
if item.selected {
// TODO: change to parent_width
@ -44,7 +44,7 @@ draw_menu_bar_item :: proc(item: ^MenuBarItem, x, y: i32, parent_width, parent_h
largest_sub_item = math.max(len(sub_item.text), largest_sub_item);
}
this_width := i32(largest_sub_item) * 8 + text_padding*2;
this_width := i32(largest_sub_item * state.source_font_width + text_padding*2);
sub_list_x := x;
if horizontal {
sub_list_x += parent_width;
@ -52,32 +52,32 @@ draw_menu_bar_item :: proc(item: ^MenuBarItem, x, y: i32, parent_width, parent_h
for _, index in item.sub_items {
sub_item := &item.sub_items[index];
item_text := raylib.TextFormat("%s", sub_item.text);
item_width := raylib.MeasureTextEx(font, item_text, f32(font_height), 0).x;
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
index_offset := 1;
if horizontal {
index_offset = 0;
}
item_y := y + i32(font_height * (index+index_offset));
draw_menu_bar_item(sub_item, sub_list_x, item_y, this_width, 0, font, font_height, true);
draw_menu_bar_item(state, sub_item, sub_list_x, item_y, this_width, 0, font_height, true);
}
}
}
draw_menu_bar :: proc(data: ^MenuBarState, x, y: i32, parent_width, parent_height: i32, font: raylib.Font, font_height: int) {
draw_menu_bar :: proc(state: ^core.State, data: ^MenuBarState, x, y: i32, parent_width, parent_height: i32, font_height: int) {
raylib.DrawRectangle(x, y, parent_width, i32(font_height), theme.get_palette_raylib_color(.Background3));
for _, index in data.items {
item := &data.items[index];
item_text := raylib.TextFormat("%s", item.text);
item_width := raylib.MeasureTextEx(font, item_text, f32(font_height), 0).x;
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
item_x := x + (i32(item_width) + text_padding*2) * i32(index);
draw_menu_bar_item(item, item_x, y, i32(item_width + text_padding*2), i32(font_height), font, font_height);
draw_menu_bar_item(state, item, item_x, y, i32(item_width + text_padding*2), i32(font_height), font_height);
}
}
test_menu_item :: proc(state: ^core.State, item: ^MenuBarItem, rect: raylib.Rectangle, mouse_pos: raylib.Vector2, mouse_has_clicked: bool, font: raylib.Font, font_height: int, horizontal: bool) -> bool {
test_menu_item :: proc(state: ^core.State, item: ^MenuBarItem, rect: raylib.Rectangle, mouse_pos: raylib.Vector2, mouse_has_clicked: bool, font_height: int, horizontal: bool) -> bool {
if raylib.CheckCollisionPointRec(mouse_pos, rect) {
item.selected = true;
@ -90,7 +90,7 @@ test_menu_item :: proc(state: ^core.State, item: ^MenuBarItem, rect: raylib.Rect
largest_sub_item = math.max(len(sub_item.text), largest_sub_item);
}
this_width := i32(largest_sub_item) * 8 + text_padding*2;
this_width := i32(largest_sub_item * state.source_font_width + text_padding*2);
sub_list_x := rect.x;
if horizontal {
sub_list_x += rect.width;
@ -100,7 +100,7 @@ test_menu_item :: proc(state: ^core.State, item: ^MenuBarItem, rect: raylib.Rect
for _, index in item.sub_items {
sub_item := &item.sub_items[index];
item_text := raylib.TextFormat("%s", sub_item.text);
item_width := raylib.MeasureTextEx(font, item_text, f32(font_height), 0).x;
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
index_offset := 1;
if horizontal {
@ -115,7 +115,7 @@ test_menu_item :: proc(state: ^core.State, item: ^MenuBarItem, rect: raylib.Rect
height = f32(font_height),
};
if test_menu_item(state, sub_item, sub_rec, mouse_pos, mouse_has_clicked, font, font_height, true) {
if test_menu_item(state, sub_item, sub_rec, mouse_pos, mouse_has_clicked, font_height, true) {
has_sub_item_selected = true;
}
}
@ -128,11 +128,11 @@ test_menu_item :: proc(state: ^core.State, item: ^MenuBarItem, rect: raylib.Rect
return item.selected;
}
test_menu_bar :: proc(state: ^core.State, menu_bar: ^MenuBarState, x, y: i32, mouse_pos: raylib.Vector2, mouse_has_clicked: bool, font: raylib.Font, font_height: int) {
test_menu_bar :: proc(state: ^core.State, menu_bar: ^MenuBarState, x, y: i32, mouse_pos: raylib.Vector2, mouse_has_clicked: bool, font_height: int) {
for _, index in menu_bar.items {
item := &menu_bar.items[index];
item_text := raylib.TextFormat("%s", item.text);
item_width := raylib.MeasureTextEx(font, item_text, f32(font_height), 0).x;
item_width := raylib.MeasureTextEx(state.font, item_text, f32(font_height), 0).x;
item_rec := raylib.Rectangle {
x = f32(x) + (item_width + f32(text_padding*2)) * f32(index),
@ -141,6 +141,6 @@ test_menu_bar :: proc(state: ^core.State, menu_bar: ^MenuBarState, x, y: i32, mo
height = f32(font_height),
};
test_menu_item(state, item, item_rec, mouse_pos, mouse_has_clicked, font, font_height, false);
test_menu_item(state, item, item_rec, mouse_pos, mouse_has_clicked, font_height, false);
}
}