command palette is back, many other fixes i dont remember

This commit is contained in:
Matthew 2025-12-25 21:03:55 +11:00
parent ace749228d
commit 94e64fef9e
9 changed files with 718 additions and 816 deletions

Binary file not shown.

@ -1 +1 @@
Subproject commit 809814577db807d5911e79216d6e114a2d5a2dfd Subproject commit bda9abf8b9c74844cb30edb32e81543cbd5ae89d

View File

@ -397,36 +397,36 @@ Insert(FlatBuffer* fb, u8[] insert, u64 length)
Insert(fb, insert, length, fb.pos); Insert(fb, insert, length, fb.pos);
} }
pragma(inline) u64 pragma(inline) i64
CurrentLine(FlatBuffer* fb) CurrentLine(FlatBuffer* fb)
{ {
return LineFromPos(fb, fb.pos); return LineFromPos(fb, fb.pos);
} }
pragma(inline) u64 pragma(inline) i64
CurrentCol(FlatBuffer* fb) CurrentCol(FlatBuffer* fb)
{ {
return LinePos(fb, fb.pos); return LinePos(fb, fb.pos);
} }
pragma(inline) u64 pragma(inline) i64
LinePos(FlatBuffer* fb, u64 pos) LinePos(FlatBuffer* fb, u64 pos)
{ {
u64 line = LineFromPos(fb, pos); u64 line = LineFromPos(fb, pos);
return pos - fb.lines[line].pos; return pos - fb.lines[line].pos;
} }
pragma(inline) U64Vec2 pragma(inline) I64Vec2
VecPos(FlatBuffer* fb) VecPos(FlatBuffer* fb)
{ {
return U64Vec2(CurrentCol(fb), CurrentLine(fb)); return I64Vec2(CurrentCol(fb), CurrentLine(fb));
} }
pragma(inline) u64 pragma(inline) i64
LineFromPos(FlatBuffer* fb, u64 pos) LineFromPos(FlatBuffer* fb, u64 pos)
{ {
u64 line = 0; i64 line = 0;
for(u64 i = 0; i < fb.line_count; i += 1) for(i64 i = 0; i < fb.line_count; i += 1)
{ {
if(fb.lines[i].pos > pos) if(fb.lines[i].pos > pos)
{ {

View File

@ -8,6 +8,7 @@ import ui;
import parsing; import parsing;
import views; import views;
import std.algorithm.comparison : clamp;
import std.format; import std.format;
import std.stdio; import std.stdio;
import std.exception; import std.exception;
@ -23,8 +24,7 @@ struct EditorCtx
{ {
Arena arena; Arena arena;
PlatformWindow* window; PlatformWindow* window;
UIPanel* base_panel; Panel* base_panel;
u64 panel_id;
EditState state; EditState state;
u8[128] input_buf; u8[128] input_buf;
u32 icount; u32 icount;
@ -32,6 +32,9 @@ struct EditorCtx
Timer timer; Timer timer;
CmdPalette cmd; CmdPalette cmd;
u8[][] file_names; u8[][] file_names;
u64 panel_id;
Editor* focused_editor;
} }
struct CmdPalette struct CmdPalette
@ -55,11 +58,11 @@ struct Editor
FlatBuffer buf; FlatBuffer buf;
Tokenizer tk; Tokenizer tk;
U64Vec2 cursor_pos; I64Vec2 cursor_pos;
Vec2 select_start; Vec2 select_start;
Vec2 select_end; Vec2 select_end;
u64 line_offset; i64 line_offset;
f32 text_size; f32 text_size;
} }
@ -115,7 +118,8 @@ enum EditState
alias ES = EditState; alias ES = EditState;
bool g_input_mode = false; __gshared bool g_input_mode = false;
__gshared EditorCtx g_ed_ctx;
const Command NO_CMD = { const Command NO_CMD = {
name: [], name: [],
@ -123,40 +127,34 @@ const Command NO_CMD = {
}; };
void void
Cycle(EditorCtx* ctx, Inputs* inputs) Cycle(Inputs* inputs)
{ {
ResetScratch(MB(4)); ResetScratch(MB(4));
g_delta = DeltaTime(&ctx.timer); g_delta = DeltaTime(&g_ed_ctx.timer);
debug if(g_frame_step) debug if(g_frame_step)
{ {
g_delta = 0.01; g_delta = 0.01;
} }
assert(Nil(ctx.base_panel.next));
//HandleInputs(ctx, inputs); //HandleInputs(ctx, inputs);
debug if(g_frame_step && !g_frame_continue) return; debug if(g_frame_step && !g_frame_continue) return;
debug g_frame_continue = false; debug g_frame_continue = false;
g_input_mode = ctx.state == ES.InputMode; g_input_mode = g_ed_ctx.state == ES.InputMode;
BeginUI(inputs); BeginUI(inputs);
UICtx* ui_ctx = GetCtx(); UICtx* ui_ctx = GetCtx();
static Editor* ed; EditorView(g_ed_ctx.base_panel);
if(!ed) if(g_ed_ctx.state == ES.CmdOpen)
{ {
ed = CreateEditor(ctx); CommandPalette(&g_ed_ctx.cmd);
OpenFile(ed, CastStr!(u8)("src/editor/ui.d"));
} }
EditorView(ed);
/* /*
UIPanel* root = ctx.base_panel; UIPanel* root = ctx.base_panel;
@ -203,26 +201,22 @@ Cycle(EditorCtx* ctx, Inputs* inputs)
EndUI(); EndUI();
} }
void
EditorCtx
InitEditorCtx(PlatformWindow* window) InitEditorCtx(PlatformWindow* window)
{ {
InitUICtx(window); InitUICtx(window);
EditorCtx ctx = { EditorCtx* ctx = &g_ed_ctx;
window: window,
arena: CreateArena(MB(2)),
cmd: {
arena: CreateArena(MB(1)),
cmd_arena: CreateArena(MB(1)),
buffer: Alloc!(u8)(1024),
},
};
ctx.base_panel = CreatePanel(&ctx); ctx.window = window;
ctx.base_panel.ed = CreateEditor(&ctx); ctx.arena = CreateArena(MB(2));
ctx.timer = CreateTimer(); ctx.cmd.arena = CreateArena(MB(1));
//SetFocusedPanel(ctx.base_panel); ctx.cmd.cmd_arena = CreateArena(MB(1));
ctx.cmd.buffer = Alloc!(u8)(1024);
ctx.base_panel = CreatePanel(ctx, CreateEditor(ctx));
ctx.timer = CreateTimer();
FocusEditor(ctx.base_panel.ed);
if(getcwd() != "/") if(getcwd() != "/")
{ {
@ -254,24 +248,25 @@ InitEditorCtx(PlatformWindow* window)
Logf("failed to open directory for filenames"); Logf("failed to open directory for filenames");
} }
} }
return ctx;
} }
UIPanel* void
CreatePanel(EditorCtx* ctx, SizeType size_type = ST.Percentage) FocusEditor(Editor* ed)
{ {
UIPanel* p = Alloc!(UIPanel)(&ctx.arena); assert(ed);
p.axis = A2D.Y; g_ed_ctx.focused_editor = ed;
p.id = Alloc!(u8)(&ctx.arena, 10); Logf("editor %s", ed.editor_id);
p.pct = 1.0; }
p.scroll_offset = 0.0;
p.scroll_target = 0.0;
(cast(char[])p.id).sformat("##%08s", ctx.panel_id); Panel*
p.parent = p.first = p.last = p.next = p.prev = g_UI_NIL_PANEL; CreatePanel(EditorCtx* ctx, Editor* ed = null)
{
Panel* p = Alloc!(Panel)(&ctx.arena);
ctx.panel_id += 1; p.layout_axis = A2D.Y;
p.ed = ed;
p.id = ctx.panel_id++;
p.parent = p.first = p.last = p.next = p.prev = g_NIL_PANEL;
return p; return p;
} }
@ -409,8 +404,6 @@ OpenFile(Editor* ed, u8[] file_name)
} }
} }
// Load all files then move things into editor after being created when selected
Editor* Editor*
CreateEditor(EditorCtx* ctx) CreateEditor(EditorCtx* ctx)
{ {
@ -423,107 +416,71 @@ CreateEditor(EditorCtx* ctx)
return ed; return ed;
} }
debug
{
__gshared u64 panel_count = 0;
__gshared UIPanel*[1024] panels = null;
}
void void
AddEditor(EditorCtx* ctx, UIPanel* target, Axis2D axis) AddEditor(EditorCtx* ctx, Panel* target, Axis2D axis)
{ {
if(Nil(target.parent) || target.parent.axis != axis) if(CheckNil(g_NIL_PANEL, target.parent) || target.parent.layout_axis != axis)
{ {
UIPanel* first = CreatePanel(ctx); Panel* first = CreatePanel(ctx, target.ed);
UIPanel* second = CreatePanel(ctx); Panel* second = CreatePanel(ctx, CreateEditor(ctx));
first.ed = target.ed; target.layout_axis = axis;
second.ed = CreateEditor(ctx); target.ed = null;
first.pct = second.pct = 0.5; DLLPush(target, first, g_NIL_PANEL);
DLLPush(target, second, g_NIL_PANEL);
target.axis = axis; first.parent = second.parent = target;
target.ed = null;
//PushPanel(target, first); FocusEditor(second.ed);
//PushPanel(target, second);
//SetFocusedPanel(second);
debug
{
panels[panel_count+0] = first;
panels[panel_count+1] = second;
panel_count += 2;
}
} }
else if(target.parent.axis == axis) else if(target.parent.layout_axis == axis)
{ {
UIPanel* panel = CreatePanel(ctx); Panel* panel = CreatePanel(ctx, CreateEditor(ctx));
panel.ed = CreateEditor(ctx);
//InsertPanel(target.parent, target, panel); DLLPush(target.parent, target, panel);
panel.parent = target.parent;
u64 count = 0; UIItem* ed_item = GetEditorItem(panel.ed);
for(UIPanel* p = target.parent.first; !Nil(p); p = p.next, count += 1) {} if(!Nil(ed_item.parent))
f32 pct = 1.0/count;
for(UIPanel* p = target.parent.first; !Nil(p); p = p.next)
{ {
p.pct = pct; u64 count;
} for(UIItem* c = ed_item.parent.first; !Nil(c); c = c.next)
//SetFocusedPanel(panel);
debug
{
panels[panel_count] = panel;
panel_count += 1;
}
}
debug for(u64 i = 0; i < panel_count; i += 1)
{
bool root_found = false;
for(UIPanel* p = panels[i]; !Nil(p); p = p.parent)
{
if(p == ctx.base_panel)
{ {
root_found = true; count += 1;
break; }
f32 pct = 1.0/cast(f32)count;
for(UIItem* c = ed_item.parent.first; !Nil(c); c = c.next)
{
c.resize_pct = pct;
} }
} }
if(!root_found) FocusEditor(panel.ed);
{
Logf("[DEBUG] panel %s unable to reach root", cast(char[])panels[panel_count].id);
assert(root_found);
}
} }
} }
UIItem*
GetEditorItem(Editor* ed)
{
return Get("###ed_%s", ed.editor_id);
}
pragma(inline) void pragma(inline) void
InsertInputToBuf(EditorCtx* ctx) InsertInputToBuf(EditorCtx* ctx, Editor* ed)
{ {
if(ctx.icount > 0) if(ctx.icount > 0)
{ {
/* Insert(&ed.buf, ctx.input_buf, ctx.icount);
UIPanel* p = GetFocusedPanel(); ctx.icount = 0;
if(!Nil(p))
{
Insert(&p.ed.buf, ctx.input_buf, ctx.icount);
ctx.icount = 0;
}
*/
} }
} }
void void
ResetCtx(EditorCtx* ctx) ResetCtx(EditorCtx* ctx, Editor* ed)
{ {
InsertInputToBuf(ctx); InsertInputToBuf(ctx, ed);
ctx.state = ES.NormalMode; ctx.state = ES.NormalMode;
ctx.cmd.icount = 0; ctx.cmd.icount = 0;
@ -586,137 +543,118 @@ MovePanelFocus(A2D axis, bool prev)(UIPanel* panel)
} }
void void
HandleInputs(EditorCtx* ctx, Inputs* inputs) HandleInputs(Panel* p, LinkedList!(UIInput)* inputs)
{ {
Editor* ed = p.ed;
EditorCtx* ctx = &g_ed_ctx;
u8[] cb_text; u8[] cb_text;
UIPanel* panel = g_UI_NIL_PANEL;//GetFocusedPanel(); FlatBuffer* fb = &ed.buf;
FlatBuffer* fb = !Nil(panel) ? &panel.ed.buf : null;
for(auto node = inputs.first; node != null; node = node.next) for(auto node = inputs.first; node != null; node = node.next)
{ {
bool taken = false; bool taken = false;
Input key = node.key; Input key = node.key;
Modifier md = node.md; Modifier md = node.md;
bool pressed = node.pressed;
if (pressed) if(key == Input.Escape)
{ {
if(key == Input.Escape) ResetCtx(ctx, ed);
taken = true;
}
else if(ctx.state == ES.InputMode)
{
taken = HandleInputMode(ctx, p, node);
}
else if(ctx.state == ES.CmdOpen)
{
taken = HandleCmdMode(ctx, p, node);
}
else if(ctx.state == ES.SetPanelFocus)
{
switch(key) with(Input)
{ {
ResetCtx(ctx); case Escape:
taken = true; {
ResetCtx(ctx, ed);
taken = true;
} break;
default: break;
} }
else if(ctx.state == ES.InputMode) }
else
{
switch(key) with(Input)
{ {
taken = HandleInputMode(ctx, node); case a, i:
} {
else if(ctx.state == ES.CmdOpen) if(key == a && Shift(md) && fb)
{
taken = HandleCmdMode(ctx, node);
}
else if(ctx.state == ES.SetPanelFocus)
{
switch(key) with(Input)
{
case Up:
{ {
if(MovePanelFocus!(A2D.Y, true )(panel)) goto case Escape; MoveToEOL(fb);
} break; }
case Down: else if(key == a)
{ {
if(MovePanelFocus!(A2D.Y, false)(panel)) goto case Escape; Move(fb, Right, MD.None);
} break; }
case Left: else if(key == i && Shift(md))
{ {
if(MovePanelFocus!(A2D.X, true )(panel)) goto case Escape; MoveToSOL(fb);
} break; }
case Right:
{
if(MovePanelFocus!(A2D.X, false)(panel)) goto case Escape;
} break;
case Escape:
{
ResetCtx(ctx);
taken = true;
} break;
default: break;
}
}
else
{
switch(key) with(Input)
{
case a, i:
{
if(key == a && Shift(md) && fb)
{
MoveToEOL(fb);
}
else if(key == a)
{
Move(fb, Right, MD.None);
}
else if(key == i && Shift(md))
{
MoveToSOL(fb);
}
ctx.state = ES.InputMode; ctx.state = ES.InputMode;
taken = true;
} break;
case Semicolon:
{
if(Shift(node.md))
{
ctx.state = ES.CmdOpen;
taken = true; taken = true;
} break; }
case Semicolon: } break;
case v:
{
if(Ctrl(md))
{ {
if(Shift(node.md)) cb_text = ClipboardText(ctx.window);
{ }
ctx.state = ES.CmdOpen; if(Shift(md))
taken = true;
}
} break;
case v:
{ {
if(Ctrl(md)) ToggleSelection(fb, SM.Line);
{ }
cb_text = ClipboardText(ctx.window); else
}
if(Shift(md))
{
ToggleSelection(fb, SM.Line);
}
else
{
ToggleSelection(fb, SM.Normal);
}
} break;
case c:
{ {
if(Ctrl(md)) ToggleSelection(fb, SM.Normal);
{ }
// Copy } break;
taken = true; case c:
} {
} break; if(Ctrl(md))
case w:
{ {
if(Ctrl(md)) // Copy
{ taken = true;
ctx.state = ES.SetPanelFocus; }
} } break;
} break; case w:
debug case d: {
if(Ctrl(md))
{ {
g_ui_ctx.dbg = !g_ui_ctx.dbg; ctx.state = ES.SetPanelFocus;
} break; }
debug case g: } break;
{ debug case d:
g_frame_step = !g_frame_step; {
} break; g_ui_ctx.dbg = !g_ui_ctx.dbg;
debug case s: } break;
{ debug case g:
g_frame_continue = true; {
} break; g_frame_step = !g_frame_step;
default: taken = Move(fb, key, md); break; } break;
} debug case s:
{
g_frame_continue = true;
} break;
default: taken = Move(fb, key, md); break;
} }
} }
@ -726,7 +664,7 @@ HandleInputs(EditorCtx* ctx, Inputs* inputs)
} }
} }
InsertInputToBuf(ctx); InsertInputToBuf(ctx, ed);
if(cb_text.length > 0) if(cb_text.length > 0)
{ {
@ -735,20 +673,13 @@ HandleInputs(EditorCtx* ctx, Inputs* inputs)
} }
void void
MoveCursor(InputEvent* ev) MoveCursor(Editor* ed, UIInput* ev)
{ {
UIPanel* p = g_UI_NIL_PANEL;//GetFocusedPanel(); switch(ev.key) with(Input)
if(!Nil(p))
{ {
FlatBuffer* fb = &p.ed.buf; case Up, Down, Left, Right: Move(&ed.buf, ev.key, ev.md); break;
switch(ev.key) with(Input) default: break;
{
case Up, Down, Left, Right: Move(fb, ev.key, ev.md); break;
default: break;
}
} }
} }
pragma(inline) void pragma(inline) void
@ -775,7 +706,7 @@ CharCases()
} }
bool bool
HandleInputMode(EditorCtx* ctx, InputEvent* ev) HandleInputMode(EditorCtx* ctx, Panel* p, UIInput* ev)
{ {
bool taken = false; bool taken = false;
@ -784,17 +715,13 @@ HandleInputMode(EditorCtx* ctx, InputEvent* ev)
mixin(CharCases()); mixin(CharCases());
case Input.Backspace: case Input.Backspace:
{ {
UIPanel* p = g_UI_NIL_PANEL;//GetFocusedPanel(); Backspace(&p.ed.buf);
if(!Nil(p))
{
Backspace(&p.ed.buf);
}
} break; } break;
case Input.Escape: case Input.Escape:
{ {
ctx.state = ES.NormalMode; ctx.state = ES.NormalMode;
} break; } break;
default: MoveCursor(ev); break; default: MoveCursor(p.ed, ev); break;
} }
return taken; return taken;
@ -933,11 +860,11 @@ GetCommands(CmdPalette* cmd)
} }
bool bool
HandleCmdMode(EditorCtx* ctx, InputEvent* ev) HandleCmdMode(EditorCtx* ctx, Panel* panel, UIInput* ev)
{ {
u8 result = 0; bool taken;
bool taken = false;
CmdPalette* cmd = &ctx.cmd; CmdPalette* cmd = &ctx.cmd;
Editor* ed = panel.ed;
u64 prev_count = cmd.icount; u64 prev_count = cmd.icount;
switch(ev.key) with(Input) switch(ev.key) with(Input)
@ -956,36 +883,31 @@ HandleCmdMode(EditorCtx* ctx, InputEvent* ev)
} }
} }
UIPanel* p = g_UI_NIL_PANEL;//GetFocusedPanel();
switch(cmd.current.type) switch(cmd.current.type)
{ {
case CT.OpenFile: case CT.OpenFile:
{ {
if(!Nil(p) && cmd.selected >= 0 && cmd.selected < cmd.opt_strs.length) if(cmd.selected >= 0 && cmd.selected < cmd.opt_strs.length)
{ {
OpenFile(p.ed, cmd.opt_strs[cmd.selected]); OpenFile(ed, cmd.opt_strs[cmd.selected]);
} }
} break; } break;
case CT.SaveFile: case CT.SaveFile:
{ {
if(!Nil(p)) SaveFile(ed, GetParam(cmd));
{
SaveFile(p.ed, GetParam(cmd));
}
} break; } break;
case CT.VSplit, CT.HSplit: case CT.VSplit, CT.HSplit:
{ {
AddEditor(ctx, p, cmd.current.type == CT.VSplit ? A2D.X : A2D.Y); AddEditor(ctx, panel, cmd.current.type == CT.VSplit ? A2D.X : A2D.Y);
} break; } break;
default: break; default: break;
} }
ResetCtx(ctx); ResetCtx(ctx, ed);
} goto CmdInputEnd; } goto CmdInputEnd;
case Backspace: case Backspace:
{ {
if(CondIncr!(-1)(cmd.icount > 0, &cmd.icount) && cmd.buffer[cmd.icount] == ' ') if(cmd.icount > 0 && cmd.buffer[--cmd.icount] == ' ')
{ {
cmd.current = cast(Command)NO_CMD; cmd.current = cast(Command)NO_CMD;
} }
@ -1005,22 +927,18 @@ HandleCmdMode(EditorCtx* ctx, InputEvent* ev)
cmd.buffer[cmd.icount++] = ' '; cmd.buffer[cmd.icount++] = ' ';
} }
} break; } break;
case Up: case Up, Down:
{ {
CondIncr!(-1)(cmd.selected > 0, &cmd.selected); cmd.selected = clamp(cmd.selected+(ev.key == Up ? -1 : +1), 0, cmd.opt_strs.length-1);
} break; } break;
case Down: default:
{ {
CondIncr!(+1)(cmd.selected < cmd.opt_strs.length-1, &cmd.selected); if(ev.text.length)
{
cmd.buffer[cmd.icount .. cmd.icount+ev.text.length] = cast(u8[])ev.text[0 .. $];
cmd.icount += ev.text.length;
}
} break; } break;
mixin(TextLineCharCases());
default: break;
}
if(result != 0)
{
Check(cmd, 1);
cmd.buffer[cmd.icount++] = result;
} }
if(cmd.current.type == CT.None && (cmd.commands.length == 0 || prev_count != cmd.icount)) if(cmd.current.type == CT.None && (cmd.commands.length == 0 || prev_count != cmd.icount))

View File

@ -27,7 +27,7 @@ void main(string[] argv)
StartPlatformThread(&window); StartPlatformThread(&window);
EditorCtx ctx = InitEditorCtx(&window); InitEditorCtx(&window);
import ui; import ui;
import vulkan; import vulkan;
@ -58,6 +58,6 @@ void main(string[] argv)
} }
} }
Cycle(&ctx, inputs); Cycle(inputs);
} }
} }

View File

@ -89,24 +89,25 @@ const Vec4 GREEN = Vec4(0.0, 1.0, 0.0, 1.0);
const Vec4 TURQUOISE = Vec4(0.15, 0.59, 0.76, 1.0); const Vec4 TURQUOISE = Vec4(0.15, 0.59, 0.76, 1.0);
const Vec4 PINK = Vec4(0.85, 0.48, 0.60, 1.0); const Vec4 PINK = Vec4(0.85, 0.48, 0.60, 1.0);
const Vec4[TS.max] SYNTAX_COLORS = SyntaxCols(); const Vec4[TS.max] DEFAULT_COLORS = Vec4(1.0);
const Vec4[TS.max] SYNTAX_COLORS = SyntaxCols();
static Vec4[TS.max] static Vec4[TS.max]
SyntaxCols() SyntaxCols()
{ {
Vec4[TS.max] colors = [ Vec4[TS.max] colors = [
TS.Op: YELLOW, TS.Op: YELLOW,
TS.Bracket: GREY, TS.Bracket: GREY,
TS.Function: PURPLE, TS.Function: PURPLE,
TS.Macro: PURPLE, TS.Macro: PURPLE,
TS.Keyword: BLUE, TS.Keyword: BLUE,
TS.String: GREEN, TS.String: GREEN,
TS.Char: GREEN, TS.Char: GREEN,
TS.Import: RED, TS.Import: RED,
TS.ImportTarget: RED, TS.ImportTarget: RED,
TS.Type: TURQUOISE, TS.Type: TURQUOISE,
TS.Number: PINK, TS.Number: PINK,
TS.Comment: GREY, TS.Comment: GREY,
]; ];
Vec4[TS.max] result = Vec4(0.8, 0.8, 0.8, 1.0); Vec4[TS.max] result = Vec4(0.8, 0.8, 0.8, 1.0);
@ -125,47 +126,47 @@ SyntaxCols()
} }
const TokenStyle[TT.max] TOKEN_STYLES = [ const TokenStyle[TT.max] TOKEN_STYLES = [
TT.Dollar: TS.Op, TT.Dollar: TS.Op,
TT.Hash: TS.Op, TT.Hash: TS.Op,
TT.Tilde: TS.Op, TT.Tilde: TS.Op,
TT.Percent: TS.Op, TT.Percent: TS.Op,
TT.Caret: TS.Op, TT.Caret: TS.Op,
TT.Ampersand: TS.Op, TT.Ampersand: TS.Op,
TT.Asterisk: TS.Op, TT.Asterisk: TS.Op,
TT.Minus: TS.Op, TT.Minus: TS.Op,
TT.Equals: TS.Op, TT.Equals: TS.Op,
TT.Plus: TS.Op, TT.Plus: TS.Op,
TT.LessThan: TS.Op, TT.LessThan: TS.Op,
TT.GreaterThan: TS.Op, TT.GreaterThan: TS.Op,
TT.Exclamation: TS.Op, TT.Exclamation: TS.Op,
TT.Slash: TS.Op, TT.Slash: TS.Op,
TT.LeftParen: TS.Bracket, TT.LeftParen: TS.Bracket,
TT.RightParen: TS.Bracket, TT.RightParen: TS.Bracket,
TT.LeftSquareBrace: TS.Bracket, TT.LeftSquareBrace: TS.Bracket,
TT.RightSquareBrace: TS.Bracket, TT.RightSquareBrace: TS.Bracket,
TT.LeftBrace: TS.Bracket, TT.LeftBrace: TS.Bracket,
TT.RightBrace: TS.Bracket, TT.RightBrace: TS.Bracket,
TT.VertBar: TS.Bracket, TT.VertBar: TS.Bracket,
TT.Semicolon: TS.Bracket, TT.Semicolon: TS.Bracket,
TT.Colon: TS.Bracket, TT.Colon: TS.Bracket,
TT.Question: TS.Bracket, TT.Question: TS.Bracket,
TT.Comma: TS.Bracket, TT.Comma: TS.Bracket,
TT.SingleQuote: TS.Char, TT.SingleQuote: TS.Char,
TT.DoubleQuote: TS.String, TT.DoubleQuote: TS.String,
TT.BackTick: TS.String, TT.BackTick: TS.String,
TT.Keyword: TS.Keyword, TT.Keyword: TS.Keyword,
TT.TypeKeyword: TS.Keyword, TT.TypeKeyword: TS.Keyword,
TT.EnumKeyword: TS.Keyword, TT.EnumKeyword: TS.Keyword,
TT.Function: TS.Function, TT.Function: TS.Function,
TT.Macro: TS.Macro, TT.Macro: TS.Macro,
TT.Type: TS.Type, TT.Type: TS.Type,
TT.Number: TS.Number, TT.Number: TS.Number,
TT.Import: TS.Import, TT.Import: TS.Import,
TT.ImportTarget: TS.Import, TT.ImportTarget: TS.Import,
TT.Comment: TS.Comment, TT.Comment: TS.Comment,
]; ];
const TT[128] D_STD_TOKEN = [ const TT[128] D_STD_TOKEN = [

File diff suppressed because it is too large Load Diff

View File

@ -8,14 +8,33 @@ import std.algorithm.comparison : clamp;
import std.format; import std.format;
import std.conv; import std.conv;
bool __gshared const Panel g_nil_panel;
Nil(UIPanel* panel) __gshared Panel* g_NIL_PANEL;
__gshared const Editor g_nil_ed;
__gshared Editor* g_NIL_ED;
Vec4 BG_COL = Vec4(0.18, 0.18, 0.18, 1.0);
Vec4 CMD_COL = Vec4(0.22, 0.22, 0.22, 1.0);
Vec4 HL_BG_COL = Vec4(0.160, 0.533, 0.803, 1.0);
Vec4 HL_BORDER_COL = Vec4(0.172, 0.643, 0.988, 1.0);
shared static this()
{ {
return panel == null || panel == g_UI_NIL_PANEL; g_NIL_PANEL = cast(Panel* )&g_nil_panel;
g_NIL_ED = cast(Editor*)&g_nil_ed;
}
struct Panel
{
Panel* first, last, next, prev, parent;
Axis2D layout_axis;
Editor* ed;
u64 id;
} }
void void
LineCounterView(u64 max_line, u64 lines, u64 line_offset, f32 view_offset) LineCounterView(u64 max_line, u64 lines, i64 line_offset, f32 view_offset)
{ {
UICtx* ctx = GetCtx(); UICtx* ctx = GetCtx();
UIKey zero = ZeroKey(); UIKey zero = ZeroKey();
@ -35,7 +54,7 @@ LineCounterView(u64 max_line, u64 lines, u64 line_offset, f32 view_offset)
PushParent(line_count); PushParent(line_count);
u64 end_line = lines+line_offset; u64 end_line = lines+line_offset;
for(u64 i = line_offset; i < end_line; i += 1) for(u64 i = line_offset; i < end_line && i < max_line; i += 1)
{ {
char[] buf = ScratchAlloc!(char)(ch_width); char[] buf = ScratchAlloc!(char)(ch_width);
Push!("display_string")(ConvToStr(sformat(buf, "%s", i)), true); Push!("display_string")(ConvToStr(sformat(buf, "%s", i)), true);
@ -46,8 +65,10 @@ LineCounterView(u64 max_line, u64 lines, u64 line_offset, f32 view_offset)
} }
void void
EditorTextView(UIItem* editor, Editor* ed, u64 lines, u64 line_offset, f32 view_offset) EditorTextView(UIItem* editor, Panel* p, u64 lines, i64 line_offset, f32 view_offset)
{ {
Editor* ed = p.ed;
PushLayoutAxis(A2D.Y, true); PushLayoutAxis(A2D.Y, true);
f32 clamp_y = cast(f32)(ed.buf.line_count-lines)*TEXT_SIZE; f32 clamp_y = cast(f32)(ed.buf.line_count-lines)*TEXT_SIZE;
@ -60,94 +81,181 @@ EditorTextView(UIItem* editor, Editor* ed, u64 lines, u64 line_offset, f32 view_
PushScrollTargetY(scroll_pos, true); PushScrollTargetY(scroll_pos, true);
PushViewOffsetY(view_offset, true); PushViewOffsetY(view_offset, true);
static bool end;
static f32 count = 0.0;
if(!end)
{
ed.cursor_pos.y = 500;
count += g_delta;
if(count > 0.5)
{
end = !end;
}
}
else
{
ed.cursor_pos.y = 0;
count -= g_delta;
if(count <= 0.0)
{
end = !end;
}
}
editor = MakeItem(editor.key, UIF.DrawBorder|UIF.ScrollY|UIF.ClampY); editor = MakeItem(editor.key, UIF.DrawBorder|UIF.ScrollY|UIF.ClampY);
u64 end_line = line_offset+lines; PushParent(editor);
UIKey zero = ZeroKey(); UIKey zero = ZeroKey();
PushSizeInfoY(ST.TextSize, 1.0); // Cursor
PushParent(editor); {
LineBuffer* lb = GetLine(&ed.buf, ed.cursor_pos.y);
scope(exit) Pop!("size_info", "parent"); f32 cursor_x = CalcTextWidth(lb.text[0 .. ed.cursor_pos.x]);
f32 cursor_y = cast(f32)(ed.cursor_pos.y - line_offset)*TEXT_SIZE + view_offset;
PushSizeInfo(MakeUISize(UISize(ST.Pixels, GetCtx().char_width), UISize(ST.Pixels, TEXT_SIZE)), true);
PushFixedPos(Vec2(cursor_x, cursor_y), true);
PushBgCol(Vec4(1.0), true);
MakeItem(zero, UIF.DrawBackground|UIF.FixedPosition);
}
u64 end_line = line_offset+lines;
PushSizeInfoY(ST.TextSize, 1.0);
PushSyntaxHighlight(UISH.D);
scope(exit) Pop!("size_info", "parent", "syntax_highlight");
u64 i = line_offset; u64 i = line_offset;
for(LineBuffer* lb = GetLine(&ed.buf, i); !CheckNil(g_NIL_LINE_BUF, lb) && i < line_offset+lines; i += 1, lb = GetLine(&ed.buf, i)) for(LineBuffer* lb = GetLine(&ed.buf, i); !CheckNil(g_NIL_LINE_BUF, lb) && i < line_offset+lines; i += 1, lb = GetLine(&ed.buf, i))
{ {
PushDisplayString(ConvToStr(lb.text), true); PushDisplayString(ConvToStr(lb.text), true);
PushSyntaxTokens(cast(u8[])lb.style, true);
UIItem* line = MakeItem(zero, UIF.DrawText); UIItem* line = MakeItem(zero, UIF.DrawText);
} }
} }
void void
EditorView(Editor* ed) EditorView(Panel* p)
{ {
UICtx* ctx = GetCtx(); Editor* ed = p.ed;
UIKey zero = ZeroKey(); UICtx* ctx = GetCtx();
UIKey ed_key = MakeKey("###ed_%s", ed.editor_id); UIKey zero = ZeroKey();
UIItem* editor = Get(ed_key);
u64 frame_line_offset = ed.line_offset; if(CheckNil(g_NIL_ED, ed))
f32 frame_view_offset = -(editor.scroll_offset.y%TEXT_SIZE);
PushBgCol(Vec4(0.2, 0.3, 0.8, 1.0), true);
PushSizeInfoX(ST.Percentage, 1.0, 1.0, true);
UIItem* container = MakeItem(zero, UIF.DrawBackground);
PushParent(container);
PushBorderCol(Vec4(1.0));
scope(exit) Pop!("parent", "border_col");
u64 view_lines;
if(editor.size.y > 0.0)
{ {
view_lines = cast(u64)ceil(editor.size.y/TEXT_SIZE)+1; for(Panel* c = p.first; !CheckNil(g_NIL_PANEL, c); c = c.next)
const u64 SCROLL_BUFFER = 4;
u64 start = ed.line_offset;
u64 end = start+view_lines;
if(ed.cursor_pos.y < start)
{ {
u64 pos = ed.cursor_pos.y > SCROLL_BUFFER ? ed.cursor_pos.y - SCROLL_BUFFER : ed.cursor_pos.y; EditorView(c);
ed.line_offset = clamp(pos, 0, ed.buf.line_count);
}
else if(ed.cursor_pos.y > end)
{
ed.line_offset = clamp(ed.cursor_pos.y + SCROLL_BUFFER - view_lines, 0, ed.buf.line_count);
} }
} }
else
{
UIKey ed_key = MakeKey("###ed_%s", ed.editor_id);
UIItem* editor = Get(ed_key);
u64 start = cast(u64)floor(editor.scroll_offset.y/TEXT_SIZE); u64 frame_line_offset = ed.line_offset;
f32 frame_view_offset = -(editor.scroll_offset.y%TEXT_SIZE);
LineCounterView(ed.buf.line_count, view_lines, start, frame_view_offset); PushBgCol(BG_COL, true);
PushSizeInfoX(ST.Percentage, 1.0, 1.0, true);
EditorTextView(editor, ed, view_lines, start, frame_view_offset); UIItem* container = MakeItem(zero, UIF.DrawBackground);
PushParent(container);
PushBorderCol(Vec4(1.0));
scope(exit) Pop!("parent", "border_col");
u64 view_lines;
if(editor.size.y > 0.0)
{
view_lines = cast(u64)ceil(editor.size.y/TEXT_SIZE)+1;
const u64 SCROLL_BUFFER = 4;
u64 start = ed.line_offset;
u64 end = start+view_lines;
if(ed.cursor_pos.y < start)
{
u64 pos = ed.cursor_pos.y > SCROLL_BUFFER ? ed.cursor_pos.y - SCROLL_BUFFER : ed.cursor_pos.y;
ed.line_offset = clamp(pos, 0, ed.buf.line_count);
}
else if(ed.cursor_pos.y > end)
{
ed.line_offset = clamp(ed.cursor_pos.y + SCROLL_BUFFER - view_lines, 0, ed.buf.line_count);
}
}
u64 start = cast(u64)floor(editor.scroll_offset.y/TEXT_SIZE);
LineCounterView(ed.buf.line_count, view_lines, start, frame_view_offset);
EditorTextView(editor, p, view_lines, start, frame_view_offset);
for(UIInput* i = ctx.events.first; !CheckNil(g_UI_NIL_INPUT, i) && g_ed_ctx.focused_editor == p.ed; i = i.next)
{
bool taken;
if(i.type == UIE.Scroll)
{
ed.line_offset = clamp(ed.line_offset+(i.scroll*2), 0, ed.buf.line_count);
if(ed.cursor_pos.y < ed.line_offset)
{
ed.cursor_pos.y = ed.line_offset;
}
else if(ed.cursor_pos.y > ed.line_offset+view_lines)
{
ed.cursor_pos.y = ed.line_offset+view_lines-1;
}
taken = true;
}
else
{
HandleInputs(p, &ctx.events);
}
if(taken)
{
DLLRemove(&ctx.events, i, g_UI_NIL_INPUT);
}
}
ed.cursor_pos = VecPos(&ed.buf);
}
}
void
CommandPalette(CmdPalette* cmd)
{
UICtx* ctx = GetCtx();
Vec2 ext = GetExtent();
f32 w = ext.x*0.4;
f32 h = ext.y*0.7;
PushFixedPos(Vec2(ext.x*0.3, ext.y*0.1), true);
PushBgCol(BG_COL, true);
PushBorderCol(HL_BORDER_COL, true);
PushBorderThickness(2.0f, true);
PushCornerRadius(8.0f, true);
PushViewOffset(Vec2(-2.0f), true);
PushLayoutAxis(A2D.Y, true);
PushSizeInfo(MakeUISize(UISize(ST.Pixels, w), UISize(ST.Pixels, h)));
UIItem* cmd_item = MakeItem("###cmd_palette", UIF.Window|UIF.FixedPosition|UIF.DrawBackground|UIF.DrawBorder);
f32 padding_y = 8.0;
PushParent(cmd_item);
PushPadding(Vec2(4.0, padding_y), true);
scope(exit) Pop!("parent", "padding")();
PushBgCol(HL_BG_COL, true);
PushBorderCol(HL_BORDER_COL);
PushBorderThickness(2.0f, true);
PushCornerRadius(Vec4(8.0, 8.0, 0.0, 0.0), true);
PushSizeInfo(MakeUISize(UISize(ST.Pixels, w-padding_y), UISize(ST.TextSize, 1.0)), true);
PushDisplayString(ConvToStr(cmd.buffer[0 .. cmd.icount]), true);
UIKey zero = ZeroKey();
MakeItem(zero, UIF.DrawBorder|UIF.DrawBackground|UIF.DrawText|UIF.Overflow);
PushPadding(Vec2(4.0));
PushSizeInfoY(ST.TextSize, 1.0);
scope(exit) Pop!("size_info", "padding")();
Vec4[2] opt_cols = [
Vec4(0.22, 0.22, 0.22, 1.0),
Vec4(0.35, 0.35, 0.35, 1.0),
];
foreach(i; 0 .. cmd.opt_strs.length)
{
PushDisplayString(ConvToStr(cmd.opt_strs[i]), true);
PushBgCol(cmd.selected == i ? HL_BG_COL : opt_cols[i%2], true);
MakeItem(zero, UIF.DrawBackground|UIF.DrawText);
}
//PushDisplayString(ConvToStr(cmd.buffer));
} }
/* /*

View File

@ -4,19 +4,19 @@
#include "gui.layout" #include "gui.layout"
layout (location = 0) in vec4 in_col_1; layout (location = 0) in vec4 in_col;
layout (location = 1) in vec4 in_col_2; layout (location = 1) in float in_corner_radius_x0y0;
layout (location = 2) in vec4 in_col_3; layout (location = 2) in float in_corner_radius_x1y0;
layout (location = 3) in vec4 in_col_4; layout (location = 3) in float in_corner_radius_x0y1;
layout (location = 4) in vec2 in_dst_start; layout (location = 4) in float in_corner_radius_x1y1;
layout (location = 5) in vec2 in_dst_end; layout (location = 5) in vec2 in_dst_start;
layout (location = 6) in vec2 in_src_start; layout (location = 6) in vec2 in_dst_end;
layout (location = 7) in vec2 in_src_end; layout (location = 7) in vec2 in_src_start;
layout (location = 8) in float border_thickness; layout (location = 8) in vec2 in_src_end;
layout (location = 9) in float corner_radius; layout (location = 9) in float border_thickness;
layout (location = 10) in float edge_softness; layout (location = 10) in float edge_softness;
layout (location = 11) in float raised; layout (location = 11) in float raised;
layout (location = 12) in uint in_has_texture; layout (location = 12) in uint in_has_texture;
layout (location = 0) flat out uint out_has_texture; layout (location = 0) flat out uint out_has_texture;
@ -33,7 +33,6 @@ layout (location = 1) out struct FragDataOut {
float border_thickness; float border_thickness;
} FragData; } FragData;
vec2 Vertices[4] = vec2[4]( vec2 Vertices[4] = vec2[4](
vec2(-1.0, -1.0), vec2(-1.0, -1.0),
vec2(-1.0, +1.0), vec2(-1.0, +1.0),
@ -68,22 +67,22 @@ void main()
vec2(in_src_end.x, in_src_end.y) vec2(in_src_end.x, in_src_end.y)
); );
vec4 cols[4] = vec4[4]( float corner_radius[4] = float[4](
in_col_1, in_corner_radius_x0y0,
in_col_2, in_corner_radius_x0y1,
in_col_3, in_corner_radius_x1y0,
in_col_4 in_corner_radius_x1y1
); );
vec2 dst_verts_pct = vec2(bool(gl_VertexIndex >> 1) ? 1.0f : 0.0f, vec2 dst_verts_pct = vec2(bool(gl_VertexIndex >> 1) ? 1.0f : 0.0f,
bool(gl_VertexIndex & 1) ? 0.0f : 1.0f); bool(gl_VertexIndex & 1) ? 0.0f : 1.0f);
FragData.color = cols[gl_VertexIndex]; FragData.color = in_col;
FragData.uv = uvs[gl_VertexIndex] / tex_size; FragData.uv = uvs[gl_VertexIndex] / tex_size;
FragData.dst_pos = pos; FragData.dst_pos = pos;
FragData.dst_center = center; FragData.dst_center = center;
FragData.dst_half_size = half_size; FragData.dst_half_size = half_size;
FragData.corner_radius = corner_radius; FragData.corner_radius = corner_radius[gl_VertexIndex];
FragData.softness = edge_softness; FragData.softness = edge_softness;
FragData.raised = raised; FragData.raised = raised;
FragData.border_thickness = border_thickness; FragData.border_thickness = border_thickness;