Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 63 additions & 47 deletions DolDoc.Editor/Core/Cursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
Expand All @@ -30,7 +30,7 @@ public Cursor(ViewerState viewerState)
/// <summary>
/// The row of the cursor in the window (not adjusted to the ViewLine)
/// </summary>
public int WindowY => (DocumentY - ViewLine).Clamp(0, _viewerState.Rows - 1);
public int WindowY => (DocumentY - ViewLine).Clamp(0, viewerState.Rows - 1);

/// <summary>
/// Gets or sets the offset at which the viewer's top line is placed in the document.
Expand All @@ -40,12 +40,12 @@ public Cursor(ViewerState viewerState)
/// <summary>
/// The selected column within the document
/// </summary>
public int DocumentX => DocumentPosition % _viewerState.Columns;
public int DocumentX => DocumentPosition % viewerState.Columns;

/// <summary>
/// The selected row within the document
/// </summary>
public int DocumentY => DocumentPosition / _viewerState.Columns;
public int DocumentY => DocumentPosition / viewerState.Columns;

/// <summary>
/// The index of the cursor in the document
Expand All @@ -56,35 +56,37 @@ 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;
}

/// <summary>
/// Get the <seealso cref="DocumentEntry"/> that the cursor is currently selecting.
/// </summary>
public DocumentEntry SelectedEntry => _viewerState.Pages[DocumentPosition].Entry;
public DocumentEntry SelectedEntry => viewerState.Pages[DocumentPosition].Entry;

/// <summary>
/// Advances the cursor <paramref name="characters"/> amount of chars.
/// Advances the cursor right.
/// </summary>
/// <param name="characters">Amount of characters to advance.</param>
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;
}

/// <summary>
Expand All @@ -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;
Expand All @@ -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;
}

/// <summary>
Expand All @@ -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
{
Expand All @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions DolDoc.Editor/Core/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public Document(IList<BinaryChunk> binaryChunks = null)

parser = new AntlrParser();
entries = new LinkedList<DocumentEntry>();
// Load("\x05");
}

public DocumentEntry[] Entries
Expand Down Expand Up @@ -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)
Expand Down
26 changes: 21 additions & 5 deletions DolDoc.Editor/Core/DocumentEntry.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using DolDoc.Editor.Commands;
using DolDoc.Editor.Entries;
using DolDoc.Editor.Extensions;
using System.Collections.Generic;
using System.Linq;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down
3 changes: 2 additions & 1 deletion DolDoc.Editor/Entries/Text.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down Expand Up @@ -57,6 +56,8 @@ public override void KeyPress(ViewerState state, Key key, char? character, int r

break;
}

state.Document.Reload();
}
}
}
10 changes: 10 additions & 0 deletions DolDoc.Editor/ViewerState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
}

Expand Down
1 change: 1 addition & 0 deletions DolDoc.Renderer.OpenGL/KeyMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
};
Expand Down
14 changes: 11 additions & 3 deletions DolDoc.Renderer.OpenGL/OpenTKWindow.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
Expand Down Expand Up @@ -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);

Expand Down
9 changes: 7 additions & 2 deletions Examples/DolDoc.Examples/Examples.DD
Original file line number Diff line number Diff line change
@@ -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"$
Loading