change a bit how panels work

memory-refactor
Patrick Cleaveliln 2025-07-05 23:19:22 +00:00
parent d5db929b3f
commit 342f31478e
4 changed files with 120 additions and 13 deletions

View File

@ -8,6 +8,8 @@ import "core:log"
import "vendor:sdl2" import "vendor:sdl2"
import lua "vendor:lua/5.4" import lua "vendor:lua/5.4"
import "../util"
Mode :: enum { Mode :: enum {
Normal, Normal,
Insert, Insert,
@ -48,8 +50,7 @@ State :: struct {
command_arena: runtime.Allocator, command_arena: runtime.Allocator,
command_args: [dynamic]EditorCommandArgument, command_args: [dynamic]EditorCommandArgument,
active_panels: [128]Maybe(Panel), panels: util.StaticList(Panel),
panel_catalog: [dynamic]PanelId,
} }
EditorCommand :: struct { EditorCommand :: struct {
@ -68,16 +69,19 @@ EditorCommandArgument :: union #no_nil {
i32 i32
} }
PanelId :: union { PanelRenderProc :: proc(state: ^State, panel_state: ^PanelState) -> (ok: bool)
LibPanelId, Panel :: struct {
} panel_state: PanelState,
Panel :: union { render_proc: PanelRenderProc,
LibPanel,
} }
// TODO PanelState :: union {
LibPanelId :: struct {} FileBufferPanel
LibPanel :: struct {} }
FileBufferPanel :: struct {
buffer_index: int,
}
current_buffer :: proc(state: ^State) -> ^FileBuffer { current_buffer :: proc(state: ^State) -> ^FileBuffer {
if state.current_buffer == -2 { if state.current_buffer == -2 {

View File

@ -13,7 +13,9 @@ import "core:slice"
import "vendor:sdl2" import "vendor:sdl2"
import "vendor:sdl2/ttf" import "vendor:sdl2/ttf"
import "util"
import "core" import "core"
import "panels"
import "theme" import "theme"
import "ui" import "ui"
@ -263,9 +265,24 @@ draw :: proc(state: ^State) {
new_ui.max_size.y = state.screen_height new_ui.max_size.y = state.screen_height
// TODO: use the new panels stuff // TODO: use the new panels stuff
if file_buffer := core.current_buffer(state); file_buffer != nil { // if file_buffer := core.current_buffer(state); file_buffer != nil {
ui_file_buffer(new_ui, file_buffer) // ui_file_buffer(new_ui, file_buffer)
// }
ui.open_element(new_ui, nil, {
dir = .LeftToRight,
kind = {ui.Grow{}, ui.Grow{}},
})
{
for i in 0..<state.panels.len {
panel := &state.panels.data[i]
if panel.render_proc != nil {
panel.render_proc(state, &panel.panel_state)
} }
}
}
ui.close_element(new_ui)
ui.compute_layout_2(new_ui) ui.compute_layout_2(new_ui)
ui.draw(new_ui, state) ui.draw(new_ui, state)
@ -398,7 +415,7 @@ main :: proc() {
commands = make(core.EditorCommandList), commands = make(core.EditorCommandList),
command_arena = mem.arena_allocator(&_command_arena), command_arena = mem.arena_allocator(&_command_arena),
panel_catalog = make([dynamic]core.PanelId), panels = util.make_static_list(core.Panel, 128),
directory = os.get_current_directory(), directory = os.get_current_directory(),
log_buffer = core.new_virtual_file_buffer(context.allocator), log_buffer = core.new_virtual_file_buffer(context.allocator),
@ -516,10 +533,13 @@ main :: proc() {
continue; continue;
} }
util.append_static_list(&state.panels, panels.make_file_buffer_panel(len(state.buffers)))
runtime.append(&state.buffers, buffer); runtime.append(&state.buffers, buffer);
} }
} else { } else {
buffer := core.new_virtual_file_buffer(context.allocator); buffer := core.new_virtual_file_buffer(context.allocator);
util.append_static_list(&state.panels, panels.make_file_buffer_panel(len(state.buffers)))
runtime.append(&state.buffers, buffer); runtime.append(&state.buffers, buffer);
} }

53
src/panels/panels.odin Normal file
View File

@ -0,0 +1,53 @@
package panels
import "core:path/filepath"
import "core:fmt"
import "../core"
import "../ui"
make_file_buffer_panel :: proc(buffer_index: int) -> core.Panel {
return core.Panel {
panel_state = core.FileBufferPanel { buffer_index = buffer_index },
render_proc = proc(state: ^core.State, panel_state: ^core.PanelState) -> (ok: bool) {
panel_state := panel_state.(core.FileBufferPanel) or_return;
draw_func := proc(state: ^core.State, e: ui.UI_Element, user_data: rawptr) {
buffer := transmute(^core.FileBuffer)user_data;
if buffer != nil {
buffer.glyph_buffer_width = e.layout.size.x / state.source_font_width;
buffer.glyph_buffer_height = e.layout.size.y / state.source_font_height + 1;
core.draw_file_buffer(state, buffer, e.layout.pos.x, e.layout.pos.y);
}
};
s := transmute(^ui.State)state.ui
buffer := &state.buffers[panel_state.buffer_index]
relative_file_path, _ := filepath.rel(state.directory, buffer.file_path, context.temp_allocator)
ui.open_element(s, nil, {
dir = .TopToBottom,
kind = {ui.Grow{}, ui.Grow{}},
})
{
ui.open_element(s, ui.UI_Element_Kind_Custom{fn = draw_func, user_data = transmute(rawptr)buffer}, {
kind = {ui.Grow{}, ui.Grow{}}
})
ui.close_element(s)
ui.open_element(s, nil, {
kind = {ui.Grow{}, ui.Exact(state.source_font_height)}
})
{
ui.open_element(s, fmt.tprintf("%s", state.mode), {})
ui.close_element(s)
}
ui.close_element(s)
}
ui.close_element(s)
return true
}
}
}

30
src/util/list.odin Normal file
View File

@ -0,0 +1,30 @@
package util
import "base:runtime"
StaticList :: struct($T: typeid) {
data: []T,
len: int,
}
append_static_list :: proc(list: ^StaticList($T), value: T) -> bool {
if list.len >= len(list.data) {
return false
}
list.data[list.len] = value
list.len += 1
return true
}
append :: proc{append_static_list}
make_static_list :: proc($T: typeid, len: int) -> StaticList(T) {
list := StaticList(T) {
data = runtime.make_slice([]T, len)
}
return list
}
make :: proc{make_static_list}