From ba5d4adce60a45c2905e97e0c3c50565db3347ae Mon Sep 17 00:00:00 2001 From: David Date: Thu, 13 Oct 2016 23:18:26 -0400 Subject: [PATCH] More XAML customizability, no more wrapping, auto scrolling, and DARK THEME --- Befunge/Editor/CharStyles/BefungeStyler.cs | 66 +++++++++------ Befunge/Editor/CharStyles/ITextStyler.cs | 2 +- Befunge/Editor/CharStyles/TextStyle.cs | 96 ++++++++++++++++++++++ Befunge/Editor/Controls/CodeTextBox.cs | 18 ++-- Befunge/Editor/Editor.csproj | 1 + Befunge/Editor/MainWindow.xaml | 78 ++++++++++++++---- Befunge/Editor/MainWindow.xaml.cs | 3 - Befunge/Editor/Utils/Extensions.cs | 3 +- 8 files changed, 218 insertions(+), 49 deletions(-) create mode 100644 Befunge/Editor/CharStyles/TextStyle.cs diff --git a/Befunge/Editor/CharStyles/BefungeStyler.cs b/Befunge/Editor/CharStyles/BefungeStyler.cs index 2836b6e..4d24986 100644 --- a/Befunge/Editor/CharStyles/BefungeStyler.cs +++ b/Befunge/Editor/CharStyles/BefungeStyler.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; using System.Windows; using System.Windows.Documents; @@ -8,31 +9,48 @@ namespace Befunge.Editor.CharStyles { public class BefungeStyler : ITextStyler { - private string _directionals = "<>^v?r"; + public TextStyle DefaultStyle { get; set; } = new TextStyle(); - public IEnumerable StyledString(string input) + public TextStyle OperatorStyle { get; set; } = new TextStyle(Colors.IndianRed, FontWeights.Bold); + public TextStyle StackOpStyle { get; set; } = new TextStyle(Colors.IndianRed, FontWeights.Bold); + public TextStyle CompareStyle { get; set; } = new TextStyle(Colors.CornflowerBlue, FontWeights.Bold); + public TextStyle IfStyle { get; set; } = new TextStyle(Colors.CornflowerBlue, FontWeights.Bold); + public TextStyle IoStyle { get; set; } = new TextStyle(Colors.SeaGreen, FontWeights.Bold); + public TextStyle IpStyle { get; set; } = new TextStyle(Colors.DarkGoldenrod, FontWeights.Bold); + public TextStyle LiteralStyle { get; set; } = new TextStyle(Colors.Black, FontWeights.Bold); + public TextStyle SourceModStyle { get; set; } = new TextStyle(Colors.SeaGreen, FontWeights.Bold); + public TextStyle TrampStyle { get; set; } = new TextStyle(Colors.Plum, FontWeights.Bold); + public TextStyle DirectionalStyle { get; set; } = new TextStyle(Colors.CornflowerBlue, FontWeights.Bold); + public TextStyle EndStyle { get; set; } = new TextStyle(Colors.OrangeRed, FontWeights.Bold); + + private const string Operators = "!%*+-/"; + private const string StackOps = "\\:$n"; + private const string Compares = "`w"; + private const string Ifs = "|_"; + private const string Ios = ",.&~"; + private const string Ips = "\""; + private const string Literals = "0123456789abcdef"; + private const string SourceMods = "gp"; + private const string Tramps = "#;"; + private const string Directionals = "<>^v[]r?"; + private const string Ends = "@"; + + private TextStyle GetStyle(char c) { - var runs = new List(); - - foreach (var c in input) - { - if (_directionals.Contains(c)) - runs.Add(new Run(c.ToString()) - { - Foreground = new SolidColorBrush(Colors.CornflowerBlue), - FontWeight = FontWeights.Bold - }); - else - { - runs.Add(new Run(c.ToString()) - { - Foreground = new SolidColorBrush(Colors.Black), - FontWeight = FontWeights.Normal - }); - } - } - - return runs; + if (Operators.Contains(c)) return OperatorStyle; + if (StackOps.Contains(c)) return StackOpStyle; + if (Compares.Contains(c)) return CompareStyle; + if (Ifs.Contains(c)) return IfStyle; + if (Ios.Contains(c)) return IoStyle; + if (Ips.Contains(c)) return IpStyle; + if (Literals.Contains(c)) return LiteralStyle; + if (SourceMods.Contains(c)) return SourceModStyle; + if (Tramps.Contains(c)) return TrampStyle; + if (Directionals.Contains(c)) return DirectionalStyle; + if (Ends.Contains(c)) return EndStyle; + return DefaultStyle; } + + public IEnumerable StylizedString(string input) => input.Select(c => GetStyle(c).Stylize(c.ToString())); } } \ No newline at end of file diff --git a/Befunge/Editor/CharStyles/ITextStyler.cs b/Befunge/Editor/CharStyles/ITextStyler.cs index 50aa729..7a9fc46 100644 --- a/Befunge/Editor/CharStyles/ITextStyler.cs +++ b/Befunge/Editor/CharStyles/ITextStyler.cs @@ -8,6 +8,6 @@ namespace Befunge.Editor.CharStyles { public interface ITextStyler { - IEnumerable StyledString(string input); + IEnumerable StylizedString(string input); } } \ No newline at end of file diff --git a/Befunge/Editor/CharStyles/TextStyle.cs b/Befunge/Editor/CharStyles/TextStyle.cs new file mode 100644 index 0000000..bd53e21 --- /dev/null +++ b/Befunge/Editor/CharStyles/TextStyle.cs @@ -0,0 +1,96 @@ +using System.ComponentModel; +using System.Windows; +using System.Windows.Documents; +using System.Windows.Media; + +namespace Befunge.Editor.CharStyles +{ + public class TextStyle + { + public Brush Foreground { get; set; } + public Brush Background { get; set; } + public FontWeight FontWeight { get; set; } + public FontStyle FontStyle { get; set; } + + #region Constructors + + public TextStyle() + { + } + + #region Brushes + + public TextStyle(Brush foreground) + { + Foreground = foreground; + } + + public TextStyle(Brush foreground, FontWeight fontWeight) + { + Foreground = foreground; + FontWeight = fontWeight; + } + + public TextStyle(Brush foreground, FontWeight fontWeight, FontStyle fontStyle) + { + Foreground = foreground; + FontWeight = fontWeight; + FontStyle = fontStyle; + } + + public TextStyle(Brush foreground, FontWeight fontWeight, FontStyle fontStyle, Brush background) + { + Foreground = foreground; + Background = background; + FontWeight = fontWeight; + FontStyle = fontStyle; + } + + #endregion + + #region Colors + + public TextStyle(Color foreground) + { + Foreground = new SolidColorBrush(foreground); + } + + public TextStyle(Color foreground, FontWeight fontWeight) + { + Foreground = new SolidColorBrush(foreground); + FontWeight = fontWeight; + } + + public TextStyle(Color foreground, FontWeight fontWeight, FontStyle fontStyle) + { + Foreground = new SolidColorBrush(foreground); + FontWeight = fontWeight; + FontStyle = fontStyle; + } + + public TextStyle(Color foreground, FontWeight fontWeight, FontStyle fontStyle, Color background) + { + Foreground = new SolidColorBrush(foreground); + Background = new SolidColorBrush(background); + FontWeight = fontWeight; + FontStyle = fontStyle; + } + + #endregion + + #endregion + + public Run Stylize(string text) + { + var run = new Run(text) + { + FontWeight = FontWeight, + FontStyle = FontStyle + }; + if (Foreground != null) run.Foreground = Foreground; + if (Background != null) run.Background = Background; + + return run; + } + } +} \ No newline at end of file diff --git a/Befunge/Editor/Controls/CodeTextBox.cs b/Befunge/Editor/Controls/CodeTextBox.cs index 4d015d0..f722da4 100644 --- a/Befunge/Editor/Controls/CodeTextBox.cs +++ b/Befunge/Editor/Controls/CodeTextBox.cs @@ -1,14 +1,19 @@ -using System.ComponentModel; +using System; +using System.ComponentModel; using System.Data.SqlTypes; +using System.Linq; using System.Windows; using System.Windows.Controls; +using System.Windows.Data; using System.Windows.Documents; +using System.Windows.Markup; using System.Windows.Media; using Befunge.Editor.CharStyles; using Befunge.Editor.Utils; namespace Befunge.Editor.Controls { + [ContentProperty("TextStyler")] public class CodeTextBox : RichTextBox { public static readonly DependencyProperty TextProperty = DependencyProperty.Register( @@ -47,11 +52,11 @@ namespace Befunge.Editor.Controls Resources.Add(typeof(Paragraph), s); FontFamily = new FontFamily("Consolas"); - } + HorizontalScrollBarVisibility = ScrollBarVisibility.Auto; + VerticalScrollBarVisibility = ScrollBarVisibility.Auto; - protected override void OnTextChanged(TextChangedEventArgs e) - { - UpdateColors(); + Loaded += (sender, e) => UpdateColors(); + TextChanged += (sender, e) => UpdateColors(); } public void UpdateColors() @@ -84,6 +89,9 @@ namespace Befunge.Editor.Controls p.AppendStylizedText(TextStyler, afterText); _changingColors = false; + + var m = (allText.Split('\n').Select(x => x.Length).Max() + 2)*Document.FontSize*1126/2048; + Document.PageWidth = m; } } } \ No newline at end of file diff --git a/Befunge/Editor/Editor.csproj b/Befunge/Editor/Editor.csproj index 75bc7b3..37099b0 100644 --- a/Befunge/Editor/Editor.csproj +++ b/Befunge/Editor/Editor.csproj @@ -65,6 +65,7 @@ + diff --git a/Befunge/Editor/MainWindow.xaml b/Befunge/Editor/MainWindow.xaml index 7afe6a3..f926adc 100644 --- a/Befunge/Editor/MainWindow.xaml +++ b/Befunge/Editor/MainWindow.xaml @@ -1,22 +1,70 @@  + xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" + xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:loc="clr-namespace:Befunge.Editor.Controls" + xmlns:local="clr-namespace:Befunge.Editor" + xmlns:charStyles="clr-namespace:Befunge.Editor.CharStyles" + x:Class="Befunge.Editor.MainWindow" + mc:Ignorable="d" + Title="Befunge Editor" Height="350" Width="525"> + + + #333333 + #eeeeff + #222222 + #aaaabb + + + + + + + + + + + + + + + + + + + - + + - - + + - - - + + + + + + + + - + \ No newline at end of file diff --git a/Befunge/Editor/MainWindow.xaml.cs b/Befunge/Editor/MainWindow.xaml.cs index fc229ba..4d17677 100644 --- a/Befunge/Editor/MainWindow.xaml.cs +++ b/Befunge/Editor/MainWindow.xaml.cs @@ -34,9 +34,6 @@ namespace Befunge.Editor public MainWindow() { InitializeComponent(); - - EditRegion.TextStyler = new BefungeStyler(); - EditRegion.UpdateColors(); } } } \ No newline at end of file diff --git a/Befunge/Editor/Utils/Extensions.cs b/Befunge/Editor/Utils/Extensions.cs index 4c592b7..a5d635d 100644 --- a/Befunge/Editor/Utils/Extensions.cs +++ b/Befunge/Editor/Utils/Extensions.cs @@ -1,3 +1,4 @@ +using System.Windows.Controls; using System.Windows.Documents; using Befunge.Editor.CharStyles; @@ -9,6 +10,6 @@ namespace Befunge.Editor.Utils => AppendStylizedText(p, styler, text.ToString()); public static void AppendStylizedText(this Paragraph p, ITextStyler styler, string text) - => p.Inlines.AddRange(styler?.StyledString(text) ?? new[] {new Run(text)}); + => p.Inlines.AddRange(styler?.StylizedString(text) ?? new[] {new Run(text)}); } } \ No newline at end of file