diff --git a/DolDoc.Editor/Core/Cursor.cs b/DolDoc.Editor/Core/Cursor.cs index 7e48f9d..7625e6d 100644 --- a/DolDoc.Editor/Core/Cursor.cs +++ b/DolDoc.Editor/Core/Cursor.cs @@ -15,11 +15,11 @@ private enum Direction Down } - private readonly ViewerState _viewerState; + private readonly ViewerState viewerState; public Cursor(ViewerState viewerState) { - _viewerState = viewerState; + this.viewerState = viewerState; } /// @@ -30,7 +30,7 @@ public Cursor(ViewerState viewerState) /// /// The row of the cursor in the window (not adjusted to the ViewLine) /// - public int WindowY => (DocumentY - ViewLine).Clamp(0, _viewerState.Rows - 1); + public int WindowY => (DocumentY - ViewLine).Clamp(0, viewerState.Rows - 1); /// /// Gets or sets the offset at which the viewer's top line is placed in the document. @@ -40,12 +40,12 @@ public Cursor(ViewerState viewerState) /// /// The selected column within the document /// - public int DocumentX => DocumentPosition % _viewerState.Columns; + public int DocumentX => DocumentPosition % viewerState.Columns; /// /// The selected row within the document /// - public int DocumentY => DocumentPosition / _viewerState.Columns; + public int DocumentY => DocumentPosition / viewerState.Columns; /// /// The index of the cursor in the document @@ -56,8 +56,8 @@ public void SetPosition(int position) { DocumentPosition = position; - if (DocumentY > ViewLine + _viewerState.Rows) - ViewLine += (DocumentY - (ViewLine + _viewerState.Rows)) + 1; + if (DocumentY > ViewLine + viewerState.Rows) + ViewLine += (DocumentY - (ViewLine + viewerState.Rows)) + 1; else if (DocumentY < ViewLine) ViewLine -= ViewLine - DocumentY; } @@ -65,26 +65,28 @@ public void SetPosition(int position) /// /// Get the that the cursor is currently selecting. /// - public DocumentEntry SelectedEntry => _viewerState.Pages[DocumentPosition].Entry; + public DocumentEntry SelectedEntry => viewerState.Pages[DocumentPosition].Entry; /// - /// Advances the cursor amount of chars. + /// Advances the cursor right. /// - /// Amount of characters to advance. public void Right() { var currentlySelectedEntry = SelectedEntry; - if (_viewerState.Pages[DocumentPosition + 1].HasEntry) - DocumentPosition++; - else - DocumentPosition = FindNearestEntry(Direction.Right) ?? DocumentPosition; - - if (DocumentY > ViewLine + _viewerState.Rows) - ViewLine += (DocumentY - (ViewLine + _viewerState.Rows)); + DocumentPosition++; + if (DocumentY > ViewLine + viewerState.Rows) + ViewLine += (DocumentY - (ViewLine + viewerState.Rows)); if (currentlySelectedEntry != null) currentlySelectedEntry.Selected = false; - SelectedEntry.Selected = true; + if (SelectedEntry == null) + { + var targetEntry = FindNearestEntry(Direction.Right); + if (targetEntry.HasValue) + DocumentPosition = targetEntry.Value; + } + if (SelectedEntry != null) + SelectedEntry.Selected = true; } /// @@ -97,67 +99,81 @@ public void Left() return; var currentlySelectedEntry = SelectedEntry; - if (_viewerState.Pages[DocumentPosition - 1].HasEntry) - DocumentPosition--; - else - DocumentPosition = FindNearestEntry(Direction.Left) ?? DocumentPosition; + DocumentPosition--; if (currentlySelectedEntry != null) currentlySelectedEntry.Selected = false; - SelectedEntry.Selected = true; + if (SelectedEntry == null) + { + var targetEntry = FindNearestEntry(Direction.Left); + if (targetEntry.HasValue) + DocumentPosition = targetEntry.Value; + } + if (SelectedEntry != null) + SelectedEntry.Selected = true; } public void Up() { - if (DocumentPosition < _viewerState.Columns) + if (DocumentPosition < viewerState.Columns) return; var currentlySelectedEntry = SelectedEntry; - if (_viewerState.Pages[DocumentPosition - _viewerState.Columns].HasEntry) - DocumentPosition -= _viewerState.Columns; - else - DocumentPosition = FindNearestEntry(Direction.Up) ?? DocumentPosition; + DocumentPosition -= viewerState.Columns; if (DocumentY < ViewLine) ViewLine -= ViewLine - DocumentY; if (currentlySelectedEntry != null) currentlySelectedEntry.Selected = false; - SelectedEntry.Selected = true; + + if (SelectedEntry == null) + { + var targetEntry = FindNearestEntry(Direction.Left); + if (targetEntry.HasValue) + DocumentPosition = targetEntry.Value; + } + + if (SelectedEntry != null) + SelectedEntry.Selected = true; } public void Down() { - if (!_viewerState.Pages.HasPageForPosition(DocumentPosition + _viewerState.Columns)) + if (!viewerState.Pages.HasPageForPosition(DocumentPosition + viewerState.Columns)) return; var currentlySelectedEntry = SelectedEntry; - if (_viewerState.Pages[DocumentPosition + _viewerState.Columns].HasEntry) - DocumentPosition += _viewerState.Columns; - else - DocumentPosition = FindNearestEntry(Direction.Down) ?? DocumentPosition; + DocumentPosition += viewerState.Columns; - if (DocumentY >= ViewLine + _viewerState.Rows) - ViewLine += (DocumentY - (ViewLine + _viewerState.Rows)) + 1; + if (DocumentY >= ViewLine + viewerState.Rows) + ViewLine += (DocumentY - (ViewLine + viewerState.Rows)) + 1; if (currentlySelectedEntry != null) currentlySelectedEntry.Selected = false; - SelectedEntry.Selected = true; + if (SelectedEntry == null) + { + var targetEntry = FindNearestEntry(Direction.Left); + if (targetEntry.HasValue) + DocumentPosition = targetEntry.Value; + } + if (SelectedEntry != null) + SelectedEntry.Selected = true; } public void PageDown() { - if (!_viewerState.Pages.HasPageForPosition(DocumentPosition + _viewerState.Rows * _viewerState.Columns)) + if (!viewerState.Pages.HasPageForPosition(DocumentPosition + viewerState.Rows * viewerState.Columns)) return; - ViewLine += _viewerState.Rows; - DocumentPosition += _viewerState.Rows * _viewerState.Columns; + ViewLine += viewerState.Rows; + DocumentPosition += viewerState.Rows * viewerState.Columns; } public void PageUp() { - ViewLine -= _viewerState.Rows; - DocumentPosition -= _viewerState.Rows * _viewerState.Columns; + ViewLine -= viewerState.Rows; + DocumentPosition -= viewerState.Rows * viewerState.Columns; if (ViewLine < 0) ViewLine = 0; @@ -168,7 +184,7 @@ public void PageUp() public void Move(int oldX, int oldY, int newX, int newY) { // TODO - _viewerState.Pages[DocumentPosition].Flags ^= CharacterFlags.Inverted; + viewerState.Pages[DocumentPosition].Flags ^= CharacterFlags.Inverted; } /// @@ -183,12 +199,12 @@ public void Move(int oldX, int oldY, int newX, int newY) if (dir == Direction.Up) { - position -= (position % _viewerState.Columns); + position -= (position % viewerState.Columns); if (position < 0) return null; } else if (dir == Direction.Down) - position += (_viewerState.Columns - (position % _viewerState.Columns)); + position += (viewerState.Columns - (position % viewerState.Columns)); do { @@ -207,9 +223,9 @@ public void Move(int oldX, int oldY, int newX, int newY) // If there is no page for the requested position, return null, since we // could not find a entry for it. - if (!_viewerState.Pages.HasPageForPosition(position)) + if (!viewerState.Pages.HasPageForPosition(position)) return null; - } while (!_viewerState.Pages[position].HasEntry); + } while (!viewerState.Pages[position].HasEntry); return position; } diff --git a/DolDoc.Editor/Core/Document.cs b/DolDoc.Editor/Core/Document.cs index 4e0e9fe..5f3f1f8 100644 --- a/DolDoc.Editor/Core/Document.cs +++ b/DolDoc.Editor/Core/Document.cs @@ -32,6 +32,7 @@ public Document(IList binaryChunks = null) parser = new AntlrParser(); entries = new LinkedList(); + // Load("\x05"); } public DocumentEntry[] Entries @@ -105,6 +106,8 @@ public void Write(string content) OnUpdate?.Invoke(this, false); } + public void Insert(int position, string text) => Load(ToPlainText().Insert(position, text)); + public void Write(DocumentEntry entry) { lock (syncRoot) diff --git a/DolDoc.Editor/Core/DocumentEntry.cs b/DolDoc.Editor/Core/DocumentEntry.cs index 8915925..fd8e09b 100644 --- a/DolDoc.Editor/Core/DocumentEntry.cs +++ b/DolDoc.Editor/Core/DocumentEntry.cs @@ -1,5 +1,4 @@ using DolDoc.Editor.Commands; -using DolDoc.Editor.Entries; using DolDoc.Editor.Extensions; using System.Collections.Generic; using System.Linq; @@ -144,15 +143,32 @@ protected int WriteString(EntryRenderContext ctx, string str) if (ch == '\n') { + ctx.State.Pages[renderPosition++].Write( + this, + i, + (byte)' ', + new CombinedColor(ctx.Options.BackgroundColor, ctx.Options.ForegroundColor) + ); + var charsUntilEndOfLine = ctx.State.Columns - (renderPosition % ctx.State.Columns); renderPosition += charsUntilEndOfLine; - charsWritten += charsUntilEndOfLine; + charsWritten += charsUntilEndOfLine + 1; + continue; } else if (ch == '\t') { var charsUntilMultipleOf5 = 8 - (renderPosition % 8); - renderPosition += charsUntilMultipleOf5; + for (var j = 0; j < charsUntilMultipleOf5; j++) + { + ctx.State.Pages[renderPosition++].Write( + this, + i + j, + (byte)' ', + new CombinedColor(ctx.Options.BackgroundColor, ctx.Options.ForegroundColor) + ); + } + // renderPosition += charsUntilMultipleOf5; charsWritten += charsUntilMultipleOf5; continue; } @@ -200,12 +216,12 @@ private int CalculateStartRenderPositionAndCharsWritten(EntryRenderContext ctx, renderPosition = (renderPosition - (renderPosition % ctx.State.Columns)) + ((ctx.State.Columns / 2) - (str.Length / 2)); else if (HasFlag("RX")) { - offset = HasFlag("B", true) ? 3 : 2; + offset = HasFlag("B") ? 3 : 2; renderPosition = (renderPosition - (renderPosition % ctx.State.Columns)) + (ctx.State.Columns - str.Length - offset); } else if (HasFlag("LX")) { - offset = HasFlag("B", true) ? 1 : 0; + offset = HasFlag("B") ? 1 : 0; renderPosition = (renderPosition - (renderPosition % ctx.State.Columns) + offset); } diff --git a/DolDoc.Editor/Entries/Text.cs b/DolDoc.Editor/Entries/Text.cs index dd90a88..912f8c6 100644 --- a/DolDoc.Editor/Entries/Text.cs +++ b/DolDoc.Editor/Entries/Text.cs @@ -28,7 +28,6 @@ public override void KeyPress(ViewerState state, Key key, char? character, int r if (character.HasValue) { Arguments[0].Value = Arguments[0].Value.Insert(relativeOffset, new string(character.Value, 1)); - //state.CursorPosition++; state.Cursor.Right(); } @@ -57,6 +56,8 @@ public override void KeyPress(ViewerState state, Key key, char? character, int r break; } + + state.Document.Reload(); } } } diff --git a/DolDoc.Editor/ViewerState.cs b/DolDoc.Editor/ViewerState.cs index 572d546..474a7e4 100644 --- a/DolDoc.Editor/ViewerState.cs +++ b/DolDoc.Editor/ViewerState.cs @@ -131,6 +131,10 @@ public void KeyPress(Key key) Console.WriteLine($"Dumped document to {output}"); return; } + else if (key == Key.F6) + { + Console.WriteLine(Document.ToPlainText()); + } switch (key) { @@ -175,6 +179,12 @@ public void KeyPress(Key key) var ch = Pages[Cursor.DocumentPosition]; if (ch.HasEntry) ch.Entry.KeyPress(this, key, character, ch.RelativeTextOffset); + else if (character.HasValue) + { + Document.Insert(Cursor.DocumentPosition, new string(character.Value, 1)); + Cursor.Right(); + } + Document.Refresh(); } diff --git a/DolDoc.Renderer.OpenGL/KeyMap.cs b/DolDoc.Renderer.OpenGL/KeyMap.cs index f346cc6..ae82b44 100644 --- a/DolDoc.Renderer.OpenGL/KeyMap.cs +++ b/DolDoc.Renderer.OpenGL/KeyMap.cs @@ -68,6 +68,7 @@ internal static class KeyMap { Keys.LeftBracket, Key.BRACKET_OPEN }, { Keys.RightBracket, Key.BRACKET_CLOSE }, { Keys.Minus, Key.MINUS }, + { Keys.F6, Key.F6 }, { Keys.F11, Key.F11 }, { Keys.F12, Key.F12 } }; diff --git a/DolDoc.Renderer.OpenGL/OpenTKWindow.cs b/DolDoc.Renderer.OpenGL/OpenTKWindow.cs index 818895c..113fa87 100644 --- a/DolDoc.Renderer.OpenGL/OpenTKWindow.cs +++ b/DolDoc.Renderer.OpenGL/OpenTKWindow.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Drawing; @@ -106,8 +107,15 @@ protected override void OnKeyDown(KeyboardKeyEventArgs e) protected override void OnMouseMove(MouseMoveEventArgs e) { - var cursor = compositor.MouseMove(e.X, e.Y); - frameBuffer.SetCursorType(cursor); + try + { + var cursor = compositor.MouseMove(e.X, e.Y); + frameBuffer.SetCursorType(cursor); + } + catch (Exception ex) + { + Console.WriteLine(ex); + } } protected override void OnMouseDown(MouseButtonEventArgs e) => compositor.MouseDown(MousePosition.X, MousePosition.Y); diff --git a/Examples/DolDoc.Examples/Examples.DD b/Examples/DolDoc.Examples/Examples.DD index fbb6ae3..ecf9018 100644 --- a/Examples/DolDoc.Examples/Examples.DD +++ b/Examples/DolDoc.Examples/Examples.DD @@ -1,10 +1,15 @@ +This is a test. +Of a document. -$TX+CX+B,"DolDoc.NET Examples"$ - + Tabbed $MA,LE="FileBrowser","File Browser"$ - A minimalistic file browser. $MA,LE="SimpleForm","Simple Form"$ - The example $FG,GREEN$"SimpleForm"$FG$ shows a demonstration of a form bound to a C# class. $MA,LE="Sprites","Sprites"$ - Renders various sprites on your screen. $MA,LE="TodoList","Todo List"$ - A simple todo list example. +$MA+PU,LE="NewDocument","New document"$ Create an empty document. + + +$TX+CX+B,"DolDoc.NET Examples"$ \ No newline at end of file diff --git a/Examples/DolDoc.Examples/Program.cs b/Examples/DolDoc.Examples/Program.cs index 0b4c6e7..5e826df 100644 --- a/Examples/DolDoc.Examples/Program.cs +++ b/Examples/DolDoc.Examples/Program.cs @@ -40,6 +40,13 @@ public static void Main(string[] args) case "FileBrowser": Compositor.Instance.Root.State.LoadDocument(FileBrowser.GetFileBrowserDocument(), true); break; + + case "NewDocument": + { + var emptyDocument = new Document(); + Compositor.Instance.Root.State.LoadDocument(emptyDocument); + break; + } } };