Fixing stuff

This commit is contained in:
Krzosa Karol
2024-07-28 11:42:12 +02:00
parent d6b850178e
commit 0c2683afaa
6 changed files with 77 additions and 75 deletions

View File

@@ -166,14 +166,9 @@ void Command_EvalLua(View *view, String16 string) {
Command_SelectRangeOneCursor(view, {}); Command_SelectRangeOneCursor(view, {});
Command_Replace(view, L"\n"); Command_Replace(view, L"\n");
Command_SelectRangeOneCursor(view, {}); Command_SelectRangeOneCursor(view, {});
} else {
{
Window *window = GetWindow(GetLastActiveWindow());
SetActiveWindow(window->id);
// View *view = GetView(window->active_view);
// Command_Replace(view, eval_result);
} }
else {
Range range = GetLineRangeWithoutNL(*buffer, 0); Range range = GetLineRangeWithoutNL(*buffer, 0);
Command_SelectRangeOneCursor(view, range); Command_SelectRangeOneCursor(view, range);
Command_Replace(view, {}); Command_Replace(view, {});
@@ -551,15 +546,15 @@ void WindowCommand(Event event, Window *window, View *view) {
view->carets[0] = caret; view->carets[0] = caret;
} }
if (Ctrl(SDLK_Q) || Mouse(MIDDLE)) { // if (Ctrl(SDLK_Q) || Mouse(MIDDLE)) {
// @todo: Consider applying this to all cursors // // @todo: Consider applying this to all cursors
if (GetSize(view->carets[0].range) == 0) { // if (GetSize(view->carets[0].range) == 0) {
Command_EvalLuaLine(view); // Command_EvalLuaLine(view);
} else { // } else {
String16 string = GetString(*buffer, view->carets[0].range); // String16 string = GetString(*buffer, view->carets[0].range);
Command_EvalLua(view, string); // Command_EvalLua(view, string);
} // }
} // }
if (window->execute_line) { if (window->execute_line) {
if (Press(SDLK_RETURN)) { if (Press(SDLK_RETURN)) {
@@ -579,12 +574,13 @@ void WindowCommand(Event event, Window *window, View *view) {
String16 string16 = GetString(*buffer); String16 string16 = GetString(*buffer);
Scratch scratch; Scratch scratch;
String string = ToString(scratch, string16); String string = ToString(scratch, string16);
bool success = WriteFile(buffer->name, string); bool success = false;
if (!StartsWith(string, "*")) success = WriteFile(buffer->name, string);
if (success) { if (success) {
buffer->dirty = false; buffer->dirty = false;
} else { } else {
String msg = Format(scratch, "Failed to save file with name: %.*s", FmtString(buffer->name)); ReportWarningf("Failed to save file with name: %.*s", FmtString(buffer->name));
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Failed to save!", msg.data, NULL);
} }
} }
@@ -671,3 +667,52 @@ void WindowCommand(Event event, Window *window, View *view) {
} }
} }
} }
void UpdateScroll(Window *window, bool update_caret_scrolling) {
ProfileFunction();
View *view = GetActiveView(window);
Buffer *buffer = GetBuffer(view->active_buffer);
// Scrolling with caret
if (update_caret_scrolling) {
Caret c = view->carets[0];
Int front = GetFront(c);
XY xy = PosToXY(*buffer, front);
Rect2I visible = GetVisibleCells(window);
Vec2I visible_cells = GetSize(visible);
Vec2I visible_size = visible_cells * Vec2I{FontCharSpacing, FontLineSpacing};
Vec2I rect_size = GetSize(window->document_rect);
if (xy.line >= visible.max.y - 2) {
Int set_view_at_line = xy.line - (visible_cells.y - 1);
Int cut_off_y = Max((Int)0, visible_size.y - rect_size.y);
view->scroll.y = (set_view_at_line * FontLineSpacing) + cut_off_y;
}
if (xy.line < visible.min.y + 1) {
view->scroll.y = xy.line * FontLineSpacing;
}
if (xy.col >= visible.max.x - 1) {
Int set_view_at_line = xy.col - (visible_cells.x - 1);
Int cut_off_x = Max((Int)0, visible_size.x - rect_size.x);
view->scroll.x = (set_view_at_line * FontCharSpacing) + cut_off_x;
}
if (xy.col <= visible.min.x) {
view->scroll.x = xy.col * FontCharSpacing;
}
}
// Clip scroll
{
Int last_line = LastLine(*buffer);
view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing));
// @note:
// GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for
// calculating this value incrementally but do we even need X scrollbar or x clipping?
view->scroll.x = ClampBottom(view->scroll.x, (Int)0);
}
}

View File

@@ -52,7 +52,8 @@ int LuaOpen(lua_State *L) {
Window *window = GetWindow(GetLastActiveWindow()); Window *window = GetWindow(GetLastActiveWindow());
View *view = ViewOpenFile(window, file_path); View *view = ViewOpenFile(window, file_path);
Buffer *buffer = GetBuffer(view->active_buffer); Buffer *buffer = GetBuffer(view->active_buffer);
view->carets[0] = MakeCaret(XYToPos(*buffer, {line - 1, col - 1})); view->carets[0] = MakeCaret(XYToPos(*buffer, {col - 1, line - 1}));
UpdateScroll(window, true);
SetActiveWindow(window->id); SetActiveWindow(window->id);
} else { } else {
// Window *window = GetWindow(GetLastActiveWindow()); // Window *window = GetWindow(GetLastActiveWindow());

View File

@@ -172,11 +172,9 @@ int main()
ReloadFont(16); ReloadFont(16);
InitLua(); InitLua();
{
Allocator sys_allocator = GetSystemAllocator(); Allocator sys_allocator = GetSystemAllocator();
Buffer *buffer = CreateBuffer(sys_allocator, "*scratch*"); Buffer *null_buffer = CreateBuffer(sys_allocator, "*scratch*");
View *view = CreateView(buffer->id); View *null_view = CreateView(null_buffer->id);
}
Buffer *lua_buffer = NULL; Buffer *lua_buffer = NULL;
Int lua_buffer_change_id = 0; Int lua_buffer_change_id = 0;
@@ -216,7 +214,7 @@ int main()
lua_buffer_change_id = lua_buffer->change_id; lua_buffer_change_id = lua_buffer->change_id;
} }
InitWindows(); InitWindows(null_view);
while (AppIsRunning) { while (AppIsRunning) {
FrameID += 1; FrameID += 1;
@@ -289,52 +287,8 @@ int main()
if (!window->visible) continue; if (!window->visible) continue;
} }
View *view = GetActiveView(window); View *view = GetView(window->active_view);
Buffer *buffer = GetBuffer(view->active_buffer); UpdateScroll(window, !AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && view->update_scroll);
// Scrolling with caret
if (!AreEqual(view->main_caret_on_begin_frame, view->carets[0]) && view->update_scroll) {
Caret c = view->carets[0];
Int front = GetFront(c);
XY xy = PosToXY(*buffer, front);
Rect2I visible = GetVisibleCells(window);
Vec2I visible_cells = GetSize(visible);
Vec2I visible_size = visible_cells * Vec2I{FontCharSpacing, FontLineSpacing};
Vec2I rect_size = GetSize(window->document_rect);
if (xy.line >= visible.max.y - 2) {
Int set_view_at_line = xy.line - (visible_cells.y - 1);
Int cut_off_y = Max((Int)0, visible_size.y - rect_size.y);
view->scroll.y = (set_view_at_line * FontLineSpacing) + cut_off_y;
}
if (xy.line < visible.min.y + 1) {
view->scroll.y = xy.line * FontLineSpacing;
}
if (xy.col >= visible.max.x - 1) {
Int set_view_at_line = xy.col - (visible_cells.x - 1);
Int cut_off_x = Max((Int)0, visible_size.x - rect_size.x);
view->scroll.x = (set_view_at_line * FontCharSpacing) + cut_off_x;
}
if (xy.col <= visible.min.x) {
view->scroll.x = xy.col * FontCharSpacing;
}
}
// Clip scroll
{
ProfileScope(clip_scroll);
Int last_line = LastLine(*buffer);
view->scroll.y = Clamp(view->scroll.y, (Int)0, Max((Int)0, (last_line - 1) * FontLineSpacing));
// @note:
// GetCharCountOfLongestLine is a bottleneck, there is probably an algorithm for
// calculating this value incrementally but do we even need X scrollbar or x clipping?
view->scroll.x = ClampBottom(view->scroll.x, (Int)0);
}
DrawWindow(window); DrawWindow(window);
} }

View File

@@ -1,7 +1,7 @@
- Save file (utf16->utf8) - Save file (utf16->utf8)
- make sure we only save file buffers - make sure we only save file buffers
- Some kind of plumbing, linking
- resize windows - resize windows
- page up and down should also scroll and leave you in exactly same scroll
- laying out windows, more choice - laying out windows, more choice
- window borders - window borders
- file dock on left side - file dock on left side
@@ -13,6 +13,7 @@
- word completion - word completion
- Colored strings - Colored strings
- open project files in folder and only show open views in Ctrl+P - open project files in folder and only show open views in Ctrl+P
- Set scroll centered
- font cache and on demand unicode loads - font cache and on demand unicode loads

View File

@@ -6,7 +6,7 @@ Array<Int> GetWindowZOrder(Allocator allocator) {
return order; return order;
} }
void InitWindows() { void InitWindows(View *null_view) {
Allocator sys_allocator = GetSystemAllocator(); Allocator sys_allocator = GetSystemAllocator();
{ {
@@ -15,6 +15,7 @@ void InitWindows() {
View *v = CreateView(b->id); View *v = CreateView(b->id);
LoadTextA(b); LoadTextA(b);
LoadUnicode(b); LoadUnicode(b);
AddView(w, null_view->id);
AddView(w, v->id); AddView(w, v->id);
} }

View File

@@ -173,7 +173,7 @@ void DrawWindow(Window *window) {
Vec2I pos = {0, line * FontLineSpacing}; Vec2I pos = {0, line * FontLineSpacing};
pos.y -= view->scroll.y; pos.y -= view->scroll.y;
pos += window->line_numbers_rect.min; pos += window->line_numbers_rect.min;
String s = Format(scratch, "%lld", (long long)line); String s = Format(scratch, "%lld", (long long)line + 1ll);
String16 string = ToString16(scratch, s); String16 string = ToString16(scratch, s);
float x = GetStringSize(&MainFont, string).x; float x = GetStringSize(&MainFont, string).x;
Vec2 p = ToVec2(pos); Vec2 p = ToVec2(pos);