Ooui-tws-port/Ooui/Canvas.cs

50 lines
1.5 KiB
C#
Raw Normal View History

2017-06-19 07:08:33 +00:00
using System;
namespace Ooui
{
public class Canvas : Element
{
CanvasRenderingContext2D context2d = new CanvasRenderingContext2D ();
2017-06-19 07:08:33 +00:00
int gotContext2d = 0;
2017-06-19 07:18:25 +00:00
public int Width {
2018-02-02 02:43:23 +00:00
get => GetAttribute ("width", 300);
set => SetAttributeProperty ("width", value < 0 ? 0 : value);
2017-06-19 07:18:25 +00:00
}
public int Height {
2018-02-02 02:43:23 +00:00
get => GetAttribute ("height", 150);
2018-02-02 02:51:00 +00:00
set => SetAttributeProperty ("height", value < 0 ? 0 : value);
2017-06-19 07:18:25 +00:00
}
2017-06-19 07:08:33 +00:00
public Canvas ()
: base ("canvas")
{
2017-07-08 05:53:32 +00:00
context2d.MessageSent += Send;
2017-06-19 07:08:33 +00:00
}
2017-06-20 04:55:20 +00:00
public CanvasRenderingContext2D GetContext2D ()
2017-06-19 07:08:33 +00:00
{
if (System.Threading.Interlocked.CompareExchange (ref gotContext2d, 1, 0) == 0) {
var mcall = Message.Call (Id, "getContext", "2d");
mcall.ResultId = context2d.Id;
Send (mcall);
}
return context2d;
}
2017-06-24 20:34:47 +00:00
protected override bool SaveStateMessageIfNeeded (Message message)
2017-06-19 07:08:33 +00:00
{
2017-06-24 20:34:47 +00:00
if (message.TargetId == Id) {
switch (message.MessageType) {
2018-03-03 06:06:46 +00:00
case MessageType.Call:
if (message.Key == "getContext" && message.Value is Array a && a.Length == 1 && "2d".Equals (a.GetValue (0))) {
AddStateMessage (message);
}
2017-06-24 20:34:47 +00:00
break;
}
2017-06-19 07:08:33 +00:00
}
2017-06-24 20:34:47 +00:00
return base.SaveStateMessageIfNeeded (message);
2017-06-19 07:08:33 +00:00
}
}
}