From: Jean-Philippe Bruyère Date: Mon, 27 Sep 2021 05:25:11 +0000 (+0000) Subject: wip, set v0.9.8 X-Git-Tag: v0.9.8-beta~17 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=0c0359bcbbe32b0222a76d3dcdf036cb48928acc;p=jp%2Fcrow.git wip, set v0.9.8 --- diff --git a/Crow/src/Input/MouseEventArgs.cs b/Crow/src/Input/MouseEventArgs.cs index f84822eb..9b242dd6 100644 --- a/Crow/src/Input/MouseEventArgs.cs +++ b/Crow/src/Input/MouseEventArgs.cs @@ -32,6 +32,10 @@ namespace Crow X = x; Y = y; } + public MouseEventArgs (Point mousePosition) { + X = mousePosition.X; + Y = mousePosition.Y; + } } public class MouseMoveEventArgs : MouseEventArgs { @@ -57,7 +61,7 @@ namespace Crow public class MouseWheelEventArgs : MouseEventArgs { public readonly int Delta; - public MouseWheelEventArgs (int delta) + public MouseWheelEventArgs (int delta, Point mousePosition) : base (mousePosition) { Delta = delta; } diff --git a/Crow/src/Interface.cs b/Crow/src/Interface.cs index c177cd1b..0901b9bc 100644 --- a/Crow/src/Interface.cs +++ b/Crow/src/Interface.cs @@ -1906,7 +1906,7 @@ namespace Crow /// wheel delta public virtual bool OnMouseWheelChanged (float delta) { - MouseWheelEventArgs e = new MouseWheelEventArgs ((int)delta); + MouseWheelEventArgs e = new MouseWheelEventArgs ((int)delta, MousePosition); if (_hoverWidget == null) return false; diff --git a/Crow/src/Mono.Cairo b/Crow/src/Mono.Cairo deleted file mode 100644 index e69de29b..00000000 diff --git a/Crow/src/Text/CharLocation.cs b/Crow/src/Text/CharLocation.cs index cfc537c6..0d07b302 100644 --- a/Crow/src/Text/CharLocation.cs +++ b/Crow/src/Text/CharLocation.cs @@ -6,13 +6,13 @@ using System.Diagnostics; namespace Crow.Text { - [DebuggerDisplay ("{Line}, {Column}, {VisualCharXPosition}")] + [DebuggerDisplay ("{Line}, {Column}, {VisualCharXPosition}")] public struct CharLocation : IEquatable { public readonly int Line; /// /// Character position in current line. If equals '-1', the visualX must contains the on screen position. - /// + /// /// public int Column; public double VisualCharXPosition; @@ -21,7 +21,7 @@ namespace Crow.Text Column = column; VisualCharXPosition = visualX; } - public bool HasVisualX => Column >= 0 && VisualCharXPosition >= 0; + public bool HasVisualX => Column >= 0 && VisualCharXPosition >= 0; public void ResetVisualX () => VisualCharXPosition = -1; public static bool operator == (CharLocation a, CharLocation b) => a.Equals (b); @@ -40,5 +40,5 @@ namespace Crow.Text } public override string ToString () => $"{Line}, {Column}"; - } + } } diff --git a/Crow/src/Text/TextLine.cs b/Crow/src/Text/TextLine.cs index e3565083..d757ed77 100644 --- a/Crow/src/Text/TextLine.cs +++ b/Crow/src/Text/TextLine.cs @@ -41,6 +41,8 @@ namespace Crow.Text /// Absolute end character position just before linebreak if any. /// public int End => Start + Length; + public TextSpan Span => new TextSpan (Start, End); + public TextSpan SpanIncludingLineBreak => new TextSpan (Start, EndIncludingLineBreak); /// /// Absolute line's end position after linebreak if any. /// @@ -85,7 +87,7 @@ namespace Crow.Text /// /// /// - public TextLine WithStartOffset (int start) => new TextLine (Start + start, End, EndIncludingLineBreak); + public TextLine WithStartOffset (int start) => new TextLine (Start + start, End, EndIncludingLineBreak); public int CompareTo (TextLine other) => Start - other.Start; } } diff --git a/Crow/src/Text/TextLineCollection.cs b/Crow/src/Text/TextLineCollection.cs index 19696867..19be9c9e 100644 --- a/Crow/src/Text/TextLineCollection.cs +++ b/Crow/src/Text/TextLineCollection.cs @@ -8,203 +8,203 @@ using System.Text; namespace Crow.Text { - public class LineCollection : IList - { - TextLine[] lines; - int length; - - #region CTOR - public LineCollection (int capacity) { - lines = new TextLine[capacity]; - length = 0; - } - public LineCollection (TextLine[] _lines, int capacity = -1) { - if (capacity >= _lines.Length) { - lines = new TextLine[capacity]; - _lines.AsSpan ().CopyTo (lines); - } else - lines =_lines; - - length = _lines.Length; - } - - public LineCollection (string _text, int capacity = 4) : this (capacity) { - Update (_text.AsSpan ()); - } - #endregion - - public void Update (ReadOnlySpan _text) { - length = 0; - int start = 0, i = 0; - while (i < _text.Length) { - char c = _text[i]; - if (c == '\r') { - if (++i < _text.Length) { - if (_text[i] == '\n') - Add (new TextLine (start, i - 1, ++i)); - else - Add (new TextLine (start, i - 1, i)); - } else - Add (new TextLine (start, i - 1, i)); - start = i; - } else if (c == '\n') { - if (++i < _text.Length) { - if (_text[i] == '\r') - Add (new TextLine (start, i - 1, ++i)); - else - Add (new TextLine (start, i - 1, i)); - } else - Add (new TextLine (start, i - 1, i)); - start = i; - - } else if (c == '\u0085' || c == '\u2028' || c == '\u2029') - Add (new TextLine (start, i - 1, i)); - else - i++; - } - - if (start < i) - Add (new TextLine (start, _text.Length, _text.Length)); - else - Add (new TextLine (_text.Length, _text.Length, _text.Length)); - } - - public void Update (TextChange change) { - CharLocation locStart = GetLocation (change.Start); - int charsDiff = change.ChangedText.Length - change.Length; - int lineEnd = locStart.Line; - while (lineEnd < length - 1 && change.End >= lines[lineEnd + 1].Start) - lineEnd++; - int columnEnd = change.End - lines[lineEnd].Start; - int lineEndLineBreakLength = lines[lineEnd].LineBreakLength; - - LineCollection newLines = new LineCollection (change.ChangedText); - int linesDiff = newLines.length - 1 - (lineEnd - locStart.Line); - TextLine endTl = lines[lineEnd]; - - if (linesDiff < 0) - RemoveAt (locStart.Line + 1, -linesDiff); - else if (linesDiff > 0) { - for (int i = 0; i < linesDiff; i++) - Insert (locStart.Line + 1, default); - } - - int remainingColumns = endTl.Length - columnEnd; - lineEnd += linesDiff; - lines[lineEnd].SetLength (0); - lines[locStart.Line].SetLength (locStart.Column + newLines[0].Length); - lines[lineEnd].Length += remainingColumns; - if (newLines.Count > 1) { - lines[lineEnd].Length += newLines[newLines.Count - 1].Length; - lines[locStart.Line].LengthIncludingLineBreak = lines[locStart.Line].Length + newLines[0].LineBreakLength; - } - lines[lineEnd].LengthIncludingLineBreak = lines[lineEnd].Length + endTl.LineBreakLength; - - for (int i = 1; i < newLines.Count - 1; i++) { - int l = locStart.Line + i; - lines[l] = newLines[i]; - lines[l].Start = lines[l - 1].EndIncludingLineBreak; - } - if (lineEnd > 0) - lines[lineEnd].Start = lines[lineEnd - 1].EndIncludingLineBreak; - - //shift start for remaining lines - for (int i = lineEnd + 1; i < length; i++) - lines[i].Start += charsDiff; - } - public int GetAbsolutePosition (CharLocation loc) => lines[loc.Line].Start + loc.Column; - public CharLocation GetLocation (int absolutePosition) { - TextLine tl = new TextLine (absolutePosition); - int result = lines.AsSpan (0, length).BinarySearch (tl); - if (result < 0) { - result = ~result; - return result == 0 ? - new CharLocation (0, absolutePosition) : - new CharLocation (result - 1, absolutePosition - lines[result - 1].Start); - } - return new CharLocation (result, absolutePosition - lines[result].Start); - } - public void UpdateLineLengthInPixel (int index, int lengthInPixel) { - lines[index].LengthInPixel = lengthInPixel; - } - public int Count => length; - public bool IsReadOnly => false; - public bool IsEmpty => length == 0; - - public TextLine this[int index] { get => lines[index]; set => lines[index] = value; } - - public void Add (TextLine item) { - if (lines.Length < length + 1) { - TextLine[] tmp = new TextLine[length * 2]; - lines.AsSpan ().CopyTo (tmp); - lines = tmp; - } - lines[length] = item; - length++; - } - - public void Clear () { - length = 0; - } - - public bool Contains (TextLine item) => Array.IndexOf (lines, item) >= 0; - - public void CopyTo (TextLine[] array, int arrayIndex) { - lines.AsSpan (0, length).CopyTo (array.AsSpan (arrayIndex)); - } - - public bool Remove (TextLine item) { - int idx = Array.IndexOf (lines, item); - if (idx < 0) - return false; - if (idx + 1 < length) - lines.AsSpan (idx + 1, length - idx - 1).CopyTo (lines.AsSpan (idx)); - length--; - return true; - } - public IEnumerator GetEnumerator () => new Enumerator (this); - IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); - - public int IndexOf (TextLine item) => Array.IndexOf (lines, item); - - public void Insert (int index, TextLine item) { - if (lines.Length < length + 1) { - TextLine[] tmp = new TextLine[length * 2]; - lines.AsSpan (0, index).CopyTo (tmp); - lines.AsSpan (index).CopyTo (tmp.AsSpan (index + 1)); - lines = tmp; - }else - lines.AsSpan (index, length - index).CopyTo (lines.AsSpan (index + 1)); - lines[index] = item; - length++; - } - - public void RemoveAt (int index) { - if (index + 1 < length) - lines.AsSpan (index + 1, length - index - 1).CopyTo (lines.AsSpan (index)); - length--; - } - public void RemoveAt (int index, int count) { - if (index + count < length) - lines.AsSpan (index + count, length - index - count).CopyTo (lines.AsSpan (index)); - length -= count; - } - - public class Enumerator : IEnumerator - { - TextLine[] lines; - int length, position = -1; - public Enumerator (LineCollection coll) { - lines = coll.lines; - length = coll.length; - } - public TextLine Current => lines[position]; - object IEnumerator.Current => Current; - public void Dispose () { } - public bool MoveNext () => ++position < length; - public void Reset () { - position = -1; - } - } - } + public class LineCollection : IList + { + TextLine[] lines; + int length; + + #region CTOR + public LineCollection (int capacity) { + lines = new TextLine[capacity]; + length = 0; + } + public LineCollection (TextLine[] _lines, int capacity = -1) { + if (capacity >= _lines.Length) { + lines = new TextLine[capacity]; + _lines.AsSpan ().CopyTo (lines); + } else + lines =_lines; + + length = _lines.Length; + } + + public LineCollection (string _text, int capacity = 4) : this (capacity) { + Update (_text.AsSpan ()); + } + #endregion + + public void Update (ReadOnlySpan _text) { + length = 0; + int start = 0, i = 0; + while (i < _text.Length) { + char c = _text[i]; + if (c == '\r') { + if (++i < _text.Length) { + if (_text[i] == '\n') + Add (new TextLine (start, i - 1, ++i)); + else + Add (new TextLine (start, i - 1, i)); + } else + Add (new TextLine (start, i - 1, i)); + start = i; + } else if (c == '\n') { + if (++i < _text.Length) { + if (_text[i] == '\r') + Add (new TextLine (start, i - 1, ++i)); + else + Add (new TextLine (start, i - 1, i)); + } else + Add (new TextLine (start, i - 1, i)); + start = i; + + } else if (c == '\u0085' || c == '\u2028' || c == '\u2029') + Add (new TextLine (start, i - 1, i)); + else + i++; + } + + if (start < i) + Add (new TextLine (start, _text.Length, _text.Length)); + else + Add (new TextLine (_text.Length, _text.Length, _text.Length)); + } + + public void Update (TextChange change) { + CharLocation locStart = GetLocation (change.Start); + int charsDiff = change.ChangedText.Length - change.Length; + int lineEnd = locStart.Line; + while (lineEnd < length - 1 && change.End >= lines[lineEnd + 1].Start) + lineEnd++; + int columnEnd = change.End - lines[lineEnd].Start; + int lineEndLineBreakLength = lines[lineEnd].LineBreakLength; + + LineCollection newLines = new LineCollection (change.ChangedText); + int linesDiff = newLines.length - 1 - (lineEnd - locStart.Line); + TextLine endTl = lines[lineEnd]; + + if (linesDiff < 0) + RemoveAt (locStart.Line + 1, -linesDiff); + else if (linesDiff > 0) { + for (int i = 0; i < linesDiff; i++) + Insert (locStart.Line + 1, default); + } + + int remainingColumns = endTl.Length - columnEnd; + lineEnd += linesDiff; + lines[lineEnd].SetLength (0); + lines[locStart.Line].SetLength (locStart.Column + newLines[0].Length); + lines[lineEnd].Length += remainingColumns; + if (newLines.Count > 1) { + lines[lineEnd].Length += newLines[newLines.Count - 1].Length; + lines[locStart.Line].LengthIncludingLineBreak = lines[locStart.Line].Length + newLines[0].LineBreakLength; + } + lines[lineEnd].LengthIncludingLineBreak = lines[lineEnd].Length + endTl.LineBreakLength; + + for (int i = 1; i < newLines.Count - 1; i++) { + int l = locStart.Line + i; + lines[l] = newLines[i]; + lines[l].Start = lines[l - 1].EndIncludingLineBreak; + } + if (lineEnd > 0) + lines[lineEnd].Start = lines[lineEnd - 1].EndIncludingLineBreak; + + //shift start for remaining lines + for (int i = lineEnd + 1; i < length; i++) + lines[i].Start += charsDiff; + } + public int GetAbsolutePosition (CharLocation loc) => lines[loc.Line].Start + loc.Column; + public CharLocation GetLocation (int absolutePosition) { + TextLine tl = new TextLine (absolutePosition); + int result = lines.AsSpan (0, length).BinarySearch (tl); + if (result < 0) { + result = ~result; + return result == 0 ? + new CharLocation (0, absolutePosition) : + new CharLocation (result - 1, absolutePosition - lines[result - 1].Start); + } + return new CharLocation (result, absolutePosition - lines[result].Start); + } + public void UpdateLineLengthInPixel (int index, int lengthInPixel) { + lines[index].LengthInPixel = lengthInPixel; + } + public int Count => length; + public bool IsReadOnly => false; + public bool IsEmpty => length == 0; + + public TextLine this[int index] { get => lines[index]; set => lines[index] = value; } + + public void Add (TextLine item) { + if (lines.Length < length + 1) { + TextLine[] tmp = new TextLine[length * 2]; + lines.AsSpan ().CopyTo (tmp); + lines = tmp; + } + lines[length] = item; + length++; + } + + public void Clear () { + length = 0; + } + + public bool Contains (TextLine item) => Array.IndexOf (lines, item) >= 0; + + public void CopyTo (TextLine[] array, int arrayIndex) { + lines.AsSpan (0, length).CopyTo (array.AsSpan (arrayIndex)); + } + + public bool Remove (TextLine item) { + int idx = Array.IndexOf (lines, item); + if (idx < 0) + return false; + if (idx + 1 < length) + lines.AsSpan (idx + 1, length - idx - 1).CopyTo (lines.AsSpan (idx)); + length--; + return true; + } + public IEnumerator GetEnumerator () => new Enumerator (this); + IEnumerator IEnumerable.GetEnumerator () => GetEnumerator (); + + public int IndexOf (TextLine item) => Array.IndexOf (lines, item); + + public void Insert (int index, TextLine item) { + if (lines.Length < length + 1) { + TextLine[] tmp = new TextLine[length * 2]; + lines.AsSpan (0, index).CopyTo (tmp); + lines.AsSpan (index).CopyTo (tmp.AsSpan (index + 1)); + lines = tmp; + }else + lines.AsSpan (index, length - index).CopyTo (lines.AsSpan (index + 1)); + lines[index] = item; + length++; + } + + public void RemoveAt (int index) { + if (index + 1 < length) + lines.AsSpan (index + 1, length - index - 1).CopyTo (lines.AsSpan (index)); + length--; + } + public void RemoveAt (int index, int count) { + if (index + count < length) + lines.AsSpan (index + count, length - index - count).CopyTo (lines.AsSpan (index)); + length -= count; + } + + public class Enumerator : IEnumerator + { + TextLine[] lines; + int length, position = -1; + public Enumerator (LineCollection coll) { + lines = coll.lines; + length = coll.length; + } + public TextLine Current => lines[position]; + object IEnumerator.Current => Current; + public void Dispose () { } + public bool MoveNext () => ++position < length; + public void Reset () { + position = -1; + } + } + } } diff --git a/Crow/src/Text/TextSpan.cs b/Crow/src/Text/TextSpan.cs index ddc21c47..929d5054 100644 --- a/Crow/src/Text/TextSpan.cs +++ b/Crow/src/Text/TextSpan.cs @@ -27,9 +27,11 @@ namespace Crow.Text public override int GetHashCode() => HashCode.Combine(Start, End); public static bool operator ==(TextSpan left, TextSpan right) - => left.Equals (right); + => left.Equals (right); public static bool operator !=(TextSpan left, TextSpan right) => !left.Equals (right); public override string ToString() => $"{Start},{End}"; + public bool Contains (int absolutePosition) + => absolutePosition >= Start && absolutePosition < End; } } diff --git a/Directory.Build.props b/Directory.Build.props index a0ea5fb5..c0160dcb 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -6,7 +6,7 @@ Jean-Philippe Bruyère 7.3 - 0.9.7 + 0.9.8 $(CrowVersion)-beta