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);
}
pragma(inline) u64
pragma(inline) i64
CurrentLine(FlatBuffer* fb)
{
return LineFromPos(fb, fb.pos);
}
pragma(inline) u64
pragma(inline) i64
CurrentCol(FlatBuffer* fb)
{
return LinePos(fb, fb.pos);
}
pragma(inline) u64
pragma(inline) i64
LinePos(FlatBuffer* fb, u64 pos)
{
u64 line = LineFromPos(fb, pos);
return pos - fb.lines[line].pos;
}
pragma(inline) U64Vec2
pragma(inline) I64Vec2
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)
{
u64 line = 0;
for(u64 i = 0; i < fb.line_count; i += 1)
i64 line = 0;
for(i64 i = 0; i < fb.line_count; i += 1)
{
if(fb.lines[i].pos > pos)
{

View File

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

View File

@ -27,7 +27,7 @@ void main(string[] argv)
StartPlatformThread(&window);
EditorCtx ctx = InitEditorCtx(&window);
InitEditorCtx(&window);
import ui;
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 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]
SyntaxCols()
{
Vec4[TS.max] colors = [
TS.Op: YELLOW,
TS.Bracket: GREY,
TS.Function: PURPLE,
TS.Macro: PURPLE,
TS.Keyword: BLUE,
TS.String: GREEN,
TS.Char: GREEN,
TS.Import: RED,
TS.Op: YELLOW,
TS.Bracket: GREY,
TS.Function: PURPLE,
TS.Macro: PURPLE,
TS.Keyword: BLUE,
TS.String: GREEN,
TS.Char: GREEN,
TS.Import: RED,
TS.ImportTarget: RED,
TS.Type: TURQUOISE,
TS.Number: PINK,
TS.Comment: GREY,
TS.Type: TURQUOISE,
TS.Number: PINK,
TS.Comment: GREY,
];
Vec4[TS.max] result = Vec4(0.8, 0.8, 0.8, 1.0);
@ -125,47 +126,47 @@ SyntaxCols()
}
const TokenStyle[TT.max] TOKEN_STYLES = [
TT.Dollar: TS.Op,
TT.Hash: TS.Op,
TT.Tilde: TS.Op,
TT.Percent: TS.Op,
TT.Caret: TS.Op,
TT.Ampersand: TS.Op,
TT.Asterisk: TS.Op,
TT.Minus: TS.Op,
TT.Equals: TS.Op,
TT.Plus: TS.Op,
TT.LessThan: TS.Op,
TT.Dollar: TS.Op,
TT.Hash: TS.Op,
TT.Tilde: TS.Op,
TT.Percent: TS.Op,
TT.Caret: TS.Op,
TT.Ampersand: TS.Op,
TT.Asterisk: TS.Op,
TT.Minus: TS.Op,
TT.Equals: TS.Op,
TT.Plus: TS.Op,
TT.LessThan: TS.Op,
TT.GreaterThan: TS.Op,
TT.Exclamation: TS.Op,
TT.Slash: TS.Op,
TT.Slash: TS.Op,
TT.LeftParen: TS.Bracket,
TT.RightParen: TS.Bracket,
TT.LeftSquareBrace: TS.Bracket,
TT.LeftParen: TS.Bracket,
TT.RightParen: TS.Bracket,
TT.LeftSquareBrace: TS.Bracket,
TT.RightSquareBrace: TS.Bracket,
TT.LeftBrace: TS.Bracket,
TT.RightBrace: TS.Bracket,
TT.VertBar: TS.Bracket,
TT.Semicolon: TS.Bracket,
TT.Colon: TS.Bracket,
TT.Question: TS.Bracket,
TT.Comma: TS.Bracket,
TT.LeftBrace: TS.Bracket,
TT.RightBrace: TS.Bracket,
TT.VertBar: TS.Bracket,
TT.Semicolon: TS.Bracket,
TT.Colon: TS.Bracket,
TT.Question: TS.Bracket,
TT.Comma: TS.Bracket,
TT.SingleQuote: TS.Char,
TT.DoubleQuote: TS.String,
TT.BackTick: TS.String,
TT.BackTick: TS.String,
TT.Keyword: TS.Keyword,
TT.TypeKeyword: TS.Keyword,
TT.EnumKeyword: TS.Keyword,
TT.Function: TS.Function,
TT.Macro: TS.Macro,
TT.Type: TS.Type,
TT.Number: TS.Number,
TT.Import: TS.Import,
TT.Keyword: TS.Keyword,
TT.TypeKeyword: TS.Keyword,
TT.EnumKeyword: TS.Keyword,
TT.Function: TS.Function,
TT.Macro: TS.Macro,
TT.Type: TS.Type,
TT.Number: TS.Number,
TT.Import: TS.Import,
TT.ImportTarget: TS.Import,
TT.Comment: TS.Comment,
TT.Comment: TS.Comment,
];
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.conv;
bool
Nil(UIPanel* panel)
__gshared const Panel g_nil_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
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();
UIKey zero = ZeroKey();
@ -35,7 +54,7 @@ LineCounterView(u64 max_line, u64 lines, u64 line_offset, f32 view_offset)
PushParent(line_count);
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);
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
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);
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);
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);
u64 end_line = line_offset+lines;
PushParent(editor);
UIKey zero = ZeroKey();
PushSizeInfoY(ST.TextSize, 1.0);
PushParent(editor);
// Cursor
{
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;
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);
PushSyntaxTokens(cast(u8[])lb.style, true);
UIItem* line = MakeItem(zero, UIF.DrawText);
}
}
void
EditorView(Editor* ed)
EditorView(Panel* p)
{
UICtx* ctx = GetCtx();
UIKey zero = ZeroKey();
UIKey ed_key = MakeKey("###ed_%s", ed.editor_id);
UIItem* editor = Get(ed_key);
Editor* ed = p.ed;
UICtx* ctx = GetCtx();
UIKey zero = ZeroKey();
u64 frame_line_offset = ed.line_offset;
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)
if(CheckNil(g_NIL_ED, ed))
{
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)
for(Panel* c = p.first; !CheckNil(g_NIL_PANEL, c); c = c.next)
{
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);
EditorView(c);
}
}
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"
layout (location = 0) in vec4 in_col_1;
layout (location = 1) in vec4 in_col_2;
layout (location = 2) in vec4 in_col_3;
layout (location = 3) in vec4 in_col_4;
layout (location = 4) in vec2 in_dst_start;
layout (location = 5) in vec2 in_dst_end;
layout (location = 6) in vec2 in_src_start;
layout (location = 7) in vec2 in_src_end;
layout (location = 8) in float border_thickness;
layout (location = 9) in float corner_radius;
layout (location = 0) in vec4 in_col;
layout (location = 1) in float in_corner_radius_x0y0;
layout (location = 2) in float in_corner_radius_x1y0;
layout (location = 3) in float in_corner_radius_x0y1;
layout (location = 4) in float in_corner_radius_x1y1;
layout (location = 5) in vec2 in_dst_start;
layout (location = 6) in vec2 in_dst_end;
layout (location = 7) in vec2 in_src_start;
layout (location = 8) in vec2 in_src_end;
layout (location = 9) in float border_thickness;
layout (location = 10) in float edge_softness;
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;
@ -33,7 +33,6 @@ layout (location = 1) out struct FragDataOut {
float border_thickness;
} FragData;
vec2 Vertices[4] = vec2[4](
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)
);
vec4 cols[4] = vec4[4](
in_col_1,
in_col_2,
in_col_3,
in_col_4
float corner_radius[4] = float[4](
in_corner_radius_x0y0,
in_corner_radius_x0y1,
in_corner_radius_x1y0,
in_corner_radius_x1y1
);
vec2 dst_verts_pct = vec2(bool(gl_VertexIndex >> 1) ? 1.0f : 0.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.dst_pos = pos;
FragData.dst_center = center;
FragData.dst_half_size = half_size;
FragData.corner_radius = corner_radius;
FragData.corner_radius = corner_radius[gl_VertexIndex];
FragData.softness = edge_softness;
FragData.raised = raised;
FragData.border_thickness = border_thickness;