186 lines
6.9 KiB
Lua
186 lines
6.9 KiB
Lua
local BufferSearchOpen = false
|
|
local BufferSearchOpenElapsed = 0
|
|
|
|
local CurrentPreviewBufferIndex = Editor.get_current_buffer_index()
|
|
local BufferSearchIndex = 0
|
|
|
|
local SideBarSmoothedWidth = 128
|
|
local SideBarWidth = 128
|
|
local SideBarClosed = false
|
|
|
|
function buffer_list_iter()
|
|
local idx = 0
|
|
return function ()
|
|
buffer_info = Editor.buffer_info_from_index(idx)
|
|
idx = idx + 1
|
|
|
|
return buffer_info, idx-1
|
|
end
|
|
end
|
|
|
|
function centered(ctx, label, axis, width, height, body)
|
|
UI.push_parent(ctx, UI.push_rect(ctx, label, false, false, UI.Horizontal, UI.Fill, UI.Fill))
|
|
UI.spacer(ctx, "left spacer")
|
|
UI.push_parent(ctx, UI.push_rect(ctx, "halfway centered", false, false, UI.Vertical, width, UI.Fill))
|
|
UI.spacer(ctx, "top spacer")
|
|
UI.push_parent(ctx, UI.push_rect(ctx, "centered container", false, false, axis, UI.Fill, height))
|
|
body()
|
|
UI.pop_parent(ctx)
|
|
UI.spacer(ctx, "bottom spacer")
|
|
UI.pop_parent(ctx)
|
|
UI.spacer(ctx, "right spacer")
|
|
UI.pop_parent(ctx)
|
|
end
|
|
|
|
function lerp(from, to, rate)
|
|
return (1 - rate) * from + rate*to
|
|
end
|
|
|
|
function ui_sidebar(ctx)
|
|
SideBarSmoothedWidth = slerp(SideBarSmoothedWidth, SideBarWidth, 0.3)
|
|
|
|
tabs = UI.push_rect(ctx, "sidebar", false, false, UI.Vertical, UI.Exact(SideBarSmoothedWidth), UI.Fill)
|
|
UI.push_parent(ctx, tabs)
|
|
UI.push_rect(ctx, "padded top open files", false, false, UI.Horizontal, UI.Fill, UI.Exact(8))
|
|
UI.push_parent(ctx, UI.push_rect(ctx, "padded open files", false, false, UI.Horizontal, UI.Fill, UI.ChildrenSum))
|
|
UI.push_rect(ctx, "padded top open files", false, false, UI.Horizontal, UI.Exact(8), UI.Fill)
|
|
UI.label(ctx, "Open Files")
|
|
UI.pop_parent(ctx)
|
|
UI.push_rect(ctx, "padded bottom open files", false, false, UI.Horizontal, UI.Fill, UI.Exact(8))
|
|
|
|
for buffer_info, i in buffer_list_iter() do
|
|
button_container = UI.push_rect(ctx, "button container"..i, false, false, UI.Horizontal, UI.Fill, UI.ChildrenSum)
|
|
UI.push_parent(ctx, button_container)
|
|
flags = {"Clickable", "Hoverable", "DrawText"}
|
|
if i == current_buffer_index then
|
|
table.insert(flags, 1, "DrawBackground")
|
|
end
|
|
|
|
if UI.advanced_button(ctx, " x ", flags, UI.FitText, UI.FitText).clicked then
|
|
print("hahah, you can't close buffers yet silly")
|
|
end
|
|
|
|
tab_button_interaction = UI.advanced_button(ctx, " "..buffer_info.file_path.." ", flags, UI.Fill, UI.FitText)
|
|
if tab_button_interaction.clicked then
|
|
Editor.set_current_buffer_from_index(i)
|
|
end
|
|
if tab_button_interaction.hovering then
|
|
CurrentPreviewBufferIndex = i
|
|
end
|
|
UI.pop_parent(ctx)
|
|
end
|
|
UI.spacer(ctx, "below tabs spacer")
|
|
|
|
UI.pop_parent(ctx)
|
|
end
|
|
|
|
function ui_tabs(ctx)
|
|
UI.buffer(ctx, CurrentPreviewBufferIndex)
|
|
end
|
|
|
|
function render_ui_window(ctx)
|
|
current_buffer_index = Editor.get_current_buffer_index()
|
|
|
|
numFrames = 7
|
|
CurrentPreviewBufferIndex = current_buffer_index
|
|
|
|
if not SidebarClosed or SideBarSmoothedWidth > 2 then
|
|
ui_sidebar(ctx)
|
|
end
|
|
if UI.advanced_button(ctx, "side bar grab handle", {"DrawBorder", "Hoverable"}, UI.Exact(16), UI.Fill).dragging then
|
|
x,y = UI.get_mouse_pos(ctx)
|
|
SideBarWidth = x-8
|
|
|
|
if SideBarWidth < 32 then
|
|
SidebarClosed = true
|
|
SideBarWidth = 0
|
|
elseif SideBarWidth > 128 then
|
|
SidebarClosed = false
|
|
end
|
|
|
|
-- TODO: use some math.max function
|
|
if not SidebarClosed and SideBarWidth < 128 then
|
|
SideBarWidth = 128
|
|
end
|
|
end
|
|
|
|
ui_tabs(ctx)
|
|
render_buffer_search(ctx)
|
|
end
|
|
|
|
function render_buffer_search(ctx)
|
|
if BufferSearchOpen or BufferSearchOpenElapsed > 0 then
|
|
if BufferSearchOpen and BufferSearchOpenElapsed < numFrames then
|
|
BufferSearchOpenElapsed = BufferSearchOpenElapsed + 1
|
|
elseif not BufferSearchOpen and BufferSearchOpenElapsed > 0 then
|
|
BufferSearchOpenElapsed = BufferSearchOpenElapsed - 1
|
|
end
|
|
end
|
|
|
|
if BufferSearchOpen or BufferSearchOpenElapsed > 0 then
|
|
window_percent = 75
|
|
if BufferSearchOpenElapsed > 0 then
|
|
window_percent = ((BufferSearchOpenElapsed/numFrames) * 75)
|
|
end
|
|
|
|
UI.push_parent(ctx, UI.push_floating(ctx, "buffer search canvas", 0, 0))
|
|
centered(ctx, "buffer search window", UI.Horizontal, UI.PercentOfParent(window_percent), UI.PercentOfParent(window_percent), (
|
|
function ()
|
|
UI.push_parent(ctx, UI.push_rect(ctx, "window", true, true, UI.Horizontal, UI.Fill, UI.Fill))
|
|
UI.push_parent(ctx, UI.push_rect(ctx, "buffer list", false, false, UI.Vertical, UI.Fill, UI.Fill))
|
|
for buffer_info, i in buffer_list_iter() do
|
|
flags = {"DrawText"}
|
|
|
|
if i == BufferSearchIndex then
|
|
table.insert(flags, 1, "DrawBorder")
|
|
end
|
|
interaction = UI.advanced_button(ctx, " "..buffer_info.file_path.." ", flags, UI.Fill, UI.FitText)
|
|
end
|
|
UI.pop_parent(ctx)
|
|
UI.buffer(ctx, BufferSearchIndex)
|
|
UI.pop_parent(ctx)
|
|
end
|
|
))
|
|
UI.pop_parent(ctx)
|
|
end
|
|
end
|
|
|
|
function handle_buffer_input()
|
|
-- print("you inputted into a buffer")
|
|
end
|
|
|
|
function OnInit()
|
|
print("Main View plugin initialized")
|
|
Editor.register_key_group({
|
|
{Editor.Key.Space, "", {
|
|
{Editor.Key.B, "Buffer Search", (
|
|
function ()
|
|
BufferSearchOpen = true
|
|
BufferSearchIndex = 0
|
|
end
|
|
),
|
|
{
|
|
{Editor.Key.Escape, "Close Window", (
|
|
function ()
|
|
Editor.request_window_close()
|
|
BufferSearchOpen = false
|
|
end
|
|
)},
|
|
{Editor.Key.Enter, "Switch to Buffer", (
|
|
function ()
|
|
Editor.set_current_buffer_from_index(BufferSearchIndex)
|
|
Editor.request_window_close()
|
|
BufferSearchOpen = false
|
|
end
|
|
)},
|
|
-- TODO: don't scroll past buffers
|
|
{Editor.Key.K, "Move Selection Up", (function () BufferSearchIndex = BufferSearchIndex - 1 end)},
|
|
{Editor.Key.J, "Move Selection Down", (function () BufferSearchIndex = BufferSearchIndex + 1 end)},
|
|
}}
|
|
}}
|
|
})
|
|
|
|
Editor.register_hook(Editor.Hook.OnDraw, render_ui_window)
|
|
Editor.register_hook(Editor.Hook.OnBufferInput, handle_buffer_input)
|
|
end
|