From 0537ccf2fe9fd74ef5e93cd6a567b44cd094985e Mon Sep 17 00:00:00 2001 From: "Frank A. Krueger" Date: Mon, 19 Jun 2017 21:55:20 -0700 Subject: [PATCH] Add properties to Context2D --- Ooui/Canvas.cs | 2 +- Ooui/CanvasRenderingContext2D.cs | 80 ++++++++++++++++++++++++++++++++ Ooui/EventTarget.cs | 5 ++ Tests/CanvasTests.cs | 6 +-- 4 files changed, 89 insertions(+), 4 deletions(-) diff --git a/Ooui/Canvas.cs b/Ooui/Canvas.cs index b66aab1..28906ca 100644 --- a/Ooui/Canvas.cs +++ b/Ooui/Canvas.cs @@ -24,7 +24,7 @@ namespace Ooui { } - public Context2d GetContext2d () + public CanvasRenderingContext2D GetContext2D () { if (System.Threading.Interlocked.CompareExchange (ref gotContext2d, 1, 0) == 0) { var mcall = Message.Call (Id, "getContext", "2d"); diff --git a/Ooui/CanvasRenderingContext2D.cs b/Ooui/CanvasRenderingContext2D.cs index a0bcd93..f7723fe 100644 --- a/Ooui/CanvasRenderingContext2D.cs +++ b/Ooui/CanvasRenderingContext2D.cs @@ -2,9 +2,89 @@ namespace Ooui { public class CanvasRenderingContext2D : EventTarget { + object fillStyle = "#000"; + public object FillStyle { + get => fillStyle; + set => SetProperty (ref fillStyle, value, "fillStyle"); + } + + double fontSize = 10; + public double FontSize { + get => fontSize; + set { + if (fontSize != value) { + fontSize = value; + SetProperty (ref font, GetFont (), "font", "Font"); + OnPropertyChanged ("FontSize"); + } + } + } + string fontFamily = "sans-serif"; + public string FontFamily { + get => fontFamily; + set { + if (fontFamily != value) { + fontFamily = value ?? "sans-serif"; + SetProperty (ref font, GetFont (), "font", "Font"); + OnPropertyChanged ("FontFamily"); + } + } + } + string font = "10px sans-serif"; + public string Font => font; + string GetFont () + { + var size = FontSize.ToString (System.Globalization.CultureInfo.InvariantCulture); + return $"{size}px {FontFamily}"; + } + + double globalAlpha = 1; + public double GlobalAlpha { + get => globalAlpha; + set => SetProperty (ref globalAlpha, value, "globalAlpha"); + } + + LineCap lineCap = LineCap.Butt; + public LineCap LineCap { + get => lineCap; + set => SetProperty (ref lineCap, value, "lineCap"); + } + + LineJoin lineJoin = LineJoin.Miter; + public LineJoin LineJoin { + get => lineJoin; + set => SetProperty (ref lineJoin, value, "lineJoin"); + } + + double lineWidth = 1; + public double LineWidth { + get => lineWidth; + set => SetProperty (ref lineWidth, value, "lineWidth"); + } + + object strokeStyle = "#000"; + public object StrokeStyle { + get => strokeStyle; + set => SetProperty (ref strokeStyle, value, "strokeStyle"); + } + public CanvasRenderingContext2D () : base ("#canvasRenderingContext2D") { } } + + public enum LineCap + { + Butt, + Round, + Square + } + + public enum LineJoin + { + Bevel, + Round, + Miter + } } diff --git a/Ooui/EventTarget.cs b/Ooui/EventTarget.cs index ae366c7..30c855e 100644 --- a/Ooui/EventTarget.cs +++ b/Ooui/EventTarget.cs @@ -89,9 +89,14 @@ namespace Ooui return false; backingStore = newValue; SendSet (attributeName, newValue); + OnPropertyChanged (propertyName); return true; } + protected virtual void OnPropertyChanged (string propertyName) + { + } + public const char IdPrefix = '\u2999'; static long idCounter = 0; diff --git a/Tests/CanvasTests.cs b/Tests/CanvasTests.cs index d8eed6b..b181741 100644 --- a/Tests/CanvasTests.cs +++ b/Tests/CanvasTests.cs @@ -9,13 +9,13 @@ namespace Tests public class CanvasTests { [TestMethod] - public void Context2dState () + public void Context2DState () { var c = new Canvas (); Assert.AreEqual (1, c.StateMessages.Count); - var c2d = c.GetContext2d (); + var c2d = c.GetContext2D (); Assert.AreEqual (2, c.StateMessages.Count); - var c2d2 = c.GetContext2d (); + var c2d2 = c.GetContext2D (); Assert.AreEqual (2, c.StateMessages.Count); Assert.AreEqual (c2d, c2d2); }