X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=dev4%2Fpsychlops%2Fcore%2Fgraphic%2Fcanvas.cs;h=c46f9fbc18e078514aa01f33cee41d504bfff6bf;hb=97a97a46267462940a24f18ad5bbd0f77006dfd9;hp=a695257b23bc4bbfe2181ca9c984dcf798b2b862;hpb=2d16c7bc232e1d8e0f57404c4b37e3a8db6ab207;p=psychlops%2Fsilverlight.git diff --git a/dev4/psychlops/core/graphic/canvas.cs b/dev4/psychlops/core/graphic/canvas.cs index a695257..c46f9fb 100644 --- a/dev4/psychlops/core/graphic/canvas.cs +++ b/dev4/psychlops/core/graphic/canvas.cs @@ -9,65 +9,83 @@ using System.Windows.Media.Imaging; using System.Windows.Shapes; using System.Windows.Browser; +using System.Collections.Generic; + namespace Psychlops { + internal static partial class CONST + { + internal static readonly Int32 MAX_OBJ_N = 1500; + internal static readonly Int32 MOBJ_N = 1000; + internal static readonly Int32 COBJ_N = 300; + internal static readonly Int32 HOBJ_N = 100; + } + namespace Templates { public class StackableDrawable : Drawable { - protected System.Collections.Generic.Queue stack; - - public StackableDrawable() - { - stack = new System.Collections.Generic.Queue(); - } - - public void clear() - { - clear(Color.black); - } - public virtual void clear(Color col) - { - //rect(back_panel, col); - } +// protected System.Collections.Generic.Queue stack; + internal Internal.PrimitiveFigure[] stack; + internal int stackN = 0; + internal Line[] lineStack; + internal int lineStackN = 0; + internal Rectangle[] rectStack; + internal int rectStackN = 0; + internal Ellipse[] ellipseStack; + internal int ellipseStackN = 0; + internal Polygon[] polygonStack; + internal int polygonStackN = 0; + internal Letters[] lettersStack; + internal int lettersStackN = 0; + internal Image[] imageStack; + internal int imageStackN = 0; + internal Group[] groupStack; + internal int groupStackN = 0; - public virtual void pix(int x, int y, Color col) - { - } - public virtual void line(Line drawee) - { - stack.Enqueue(drawee.clone()); - } - - public virtual void rect(Rectangle drawee) - { - stack.Enqueue(drawee.clone()); - } - - public virtual void ellipse(Ellipse drawee) - { - stack.Enqueue(drawee.clone()); - } - - public virtual void polygon(Polygon drawee) - { - stack.Enqueue(drawee.clone()); - } - - public virtual void letters(Letters drawee) + public StackableDrawable() { - stack.Enqueue(drawee.clone()); + stack = new Internal.PrimitiveFigure[CONST.MAX_OBJ_N]; + lineStack = new Line[CONST.MOBJ_N]; + rectStack = new Rectangle[CONST.MOBJ_N]; + ellipseStack = new Ellipse[CONST.MOBJ_N]; + polygonStack = new Polygon[CONST.COBJ_N]; + lettersStack = new Letters[CONST.COBJ_N]; + imageStack = new Image[CONST.HOBJ_N]; + groupStack = new Group[CONST.HOBJ_N]; + for (int i = 0; i < CONST.MOBJ_N; i++) + { + lineStack[i] = new Line(0,0,0,0); + rectStack[i] = new Rectangle(); + ellipseStack[i] = new Ellipse(); + } + for (int i = 0; i < CONST.COBJ_N; i++) + { + polygonStack[i] = new Polygon(); + lettersStack[i] = new Letters(); + } + for (int i = 0; i < CONST.HOBJ_N; i++) + { + imageStack[i] = new Image(1, 1); + groupStack[i] = new Group(); + } } - public virtual void image(Image drawee) - { - stack.Enqueue(drawee.clone()); - } + public void clear() { clear(Color.black); } + public virtual void clear(Color col) { } //rect(back_panel, col); } + public virtual void pix(int x, int y, Color col) { } + public virtual void line(Line drawee) { drawee.copyToStack(this); } + public virtual void rect(Rectangle drawee) { drawee.copyToStack(this); } + public virtual void ellipse(Ellipse drawee) { drawee.copyToStack(this); } + public virtual void polygon(Polygon drawee) { drawee.copyToStack(this); } + public virtual void letters(Letters drawee) { drawee.copyToStack(this); } + public virtual void image(Image drawee) { drawee.copyToStack(this); } + public virtual void group(Group drawee) { drawee.copyToStack(this); } public void msg(string str, double x, double y) { msg(str, x, y, Color.white); } public virtual void msg(string dstr, double x, double y, Color col) @@ -87,6 +105,32 @@ namespace Psychlops public class Canvas : Templates.StackableDrawable { + internal System.Windows.Controls.Canvas masterPool, prevPool; + internal System.Windows.Point[] pointPool; + internal int pointPoolN; + internal SolidColorBrush[] brushPool; + internal int brushPoolN; + + internal System.Windows.Controls.Canvas[] UIElementPool; + internal int UIElementPoolN; + internal int lastVisibleN; + + internal System.Windows.Shapes.Line[] linePool; + internal int linePoolN; + internal System.Windows.Shapes.Rectangle[] dummyRectPool; + internal System.Windows.Shapes.Rectangle[] rectPool; + internal int rectPoolN; + internal System.Windows.Shapes.Ellipse[] ellipsePool; + internal int ellipsePoolN; + internal System.Windows.Shapes.Polygon[] polygonPool; + internal int polygonPoolN; + internal System.Windows.Controls.TextBlock[] lettersPool; + internal int lettersPoolN; + internal System.Windows.Controls.Image[] imagePool; + internal int imagePoolN; + internal Dictionary imagePoolT; + internal System.Windows.Controls.Canvas[] groupPool; + internal int groupPoolN; #region initializer @@ -94,16 +138,14 @@ namespace Psychlops internal delegate void SimpleProcedure(); SimpleProcedure flipexec; - //public static System.Windows.Controls.Image default_buffer_frame; public static System.Windows.Controls.UserControl default_panel; public static System.Windows.Controls.Canvas default_api_canvas; public static WriteableBitmap default_buffer; - //WriteableBitmap buffer; - //System.Windows.Controls.Image instance; - System.Windows.Controls.Canvas api_canvas; - System.Windows.Controls.UserControl panel; + internal System.Windows.Controls.Canvas api_canvas; + internal System.Windows.Controls.UserControl panel; Rectangle back_panel; double width_, height_; + Clock before; public Canvas(int wid, int hei) { @@ -117,12 +159,19 @@ namespace Psychlops api_canvas = apicnvs; initialize(wid, hei); } + + protected bool AsyncInitBool; protected void initialize(int wid, int hei) { - + before = new Clock(); + before.update(); + AsyncInitBool = false; width_ = wid; height_ = hei; api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei); + while(!AsyncInitBool) + { + } Mouse._prime = api_canvas; Main.drawable = this; Main.canvas = this; @@ -130,11 +179,12 @@ namespace Psychlops back_panel = new Rectangle(wid, hei); flipexec = new SimpleProcedure(executeFlip); + AppState.statusBar = ""; } protected void initialize__(int wid, int hei) { api_canvas.Width = wid; - api_canvas.Height = hei; + api_canvas.Height = hei+20; api_canvas.MouseMove += Mouse.Canvas_MousePos; api_canvas.MouseLeftButtonDown += Mouse.Canvas_LDown; api_canvas.MouseLeftButtonUp += Mouse.Canvas_LUp; @@ -147,34 +197,80 @@ namespace Psychlops htmlHost.SetStyleAttribute("width", (wid).ToString()+"px"); htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px"); htmlHost.SetStyleAttribute("margin", "2em auto auto auto"); + + pointPool = new System.Windows.Point[CONST.MOBJ_N]; + brushPool = new SolidColorBrush[CONST.MOBJ_N]; + linePool = new System.Windows.Shapes.Line[CONST.MOBJ_N]; + rectPool = new System.Windows.Shapes.Rectangle[CONST.MOBJ_N]; + ellipsePool = new System.Windows.Shapes.Ellipse[CONST.MOBJ_N]; + for (int i = 0; i < CONST.MOBJ_N; i++) + { + pointPool[i] = new System.Windows.Point(); + brushPool[i] = new SolidColorBrush(); + linePool[i] = new System.Windows.Shapes.Line(); + rectPool[i] = new System.Windows.Shapes.Rectangle(); + ellipsePool[i] = new System.Windows.Shapes.Ellipse(); + } + polygonPool = new System.Windows.Shapes.Polygon[CONST.COBJ_N]; + lettersPool = new System.Windows.Controls.TextBlock[CONST.COBJ_N]; + for (int i = 0; i < CONST.COBJ_N; i++) + { + polygonPool[i] = new System.Windows.Shapes.Polygon(); + lettersPool[i] = new System.Windows.Controls.TextBlock(); + } + imagePool = new System.Windows.Controls.Image[CONST.HOBJ_N]; + imagePoolT = new Dictionary(CONST.HOBJ_N); + groupPool = new System.Windows.Controls.Canvas[CONST.HOBJ_N]; + for (int i = 0; i < CONST.HOBJ_N; i++) + { + imagePool[i] = new System.Windows.Controls.Image(); + imagePoolT.Add(imagePool[i].GetHashCode(), false); + groupPool[i] = new System.Windows.Controls.Canvas(); + } + + masterPool = new System.Windows.Controls.Canvas(); + prevPool = new System.Windows.Controls.Canvas(); + api_canvas.Children.Add(masterPool); + + //api_canvas.Children.Remove(Internal.Main.widgetStack); + Psychlops.Internal.Main.widgetStack = new StackPanel(); + Psychlops.Internal.Main.widgetStack.Orientation = Orientation.Vertical; + Psychlops.Internal.Main.widgetStack.Height = hei; + api_canvas.Children.Add(Psychlops.Internal.Main.widgetStack); + Internal.Main.statusBar.Visibility = Visibility.Collapsed; + + + UIElementPool = new System.Windows.Controls.Canvas[CONST.MAX_OBJ_N]; + dummyRectPool = new System.Windows.Shapes.Rectangle[CONST.MAX_OBJ_N]; + for (int i = 0; i < CONST.MAX_OBJ_N; i++) + { + UIElementPool[i] = new System.Windows.Controls.Canvas(); + masterPool.Children.Add(UIElementPool[i]); + dummyRectPool[i] = new System.Windows.Shapes.Rectangle(); + UIElementPool[i].Children.Add(dummyRectPool[i]); + dummyRectPool[i].Visibility = Visibility.Collapsed; + } + + + AsyncInitBool = true; + } + + internal int findEmptyInPool(Dictionary pool) + { + /* + foreach( KeyValuePair elem in pool) + { + if(elem) + }*/ + return 0; } #endregion #region static initializer /* - static System.Windows.Shapes.Line api_line; - static System.Windows.Shapes.Path api_curve; - static System.Windows.Shapes.Rectangle api_rect; - static System.Windows.Shapes.Ellipse api_ellipse; - static System.Windows.Shapes.Polygon api_polygon; - static System.Windows.Shapes.Polyline api_polyline; - static System.Windows.Media.Color api_color; - static System.Windows.Media.SolidColorBrush api_fill; - static System.Windows.Media.SolidColorBrush api_stroke; - static System.Windows.Media.TranslateTransform api_translation; static Canvas() { - api_line = new System.Windows.Shapes.Line(); - api_curve = new System.Windows.Shapes.Path(); - api_rect = new System.Windows.Shapes.Rectangle(); - api_ellipse = new System.Windows.Shapes.Ellipse(); - api_polygon = new System.Windows.Shapes.Polygon(); - api_polyline = new System.Windows.Shapes.Polyline(); - api_color = new System.Windows.Media.Color(); - api_fill = new System.Windows.Media.SolidColorBrush(); - api_stroke = new System.Windows.Media.SolidColorBrush(); - api_translation = new System.Windows.Media.TranslateTransform(); } */ #endregion @@ -183,44 +279,336 @@ namespace Psychlops public override void clear(Color col) { back_panel.fill = col; + stackN = 0; rect(back_panel); } int nextIntervalFrame = 1, chacked = 0; - public void flip() + public void flip(int n) { - //api_canvas.Dispatcher.BeginInvoke(flipexec); - //flipexec(); - //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); lock (this) { - nextIntervalFrame = 1; + nextIntervalFrame = n; chacked = 1; } + //pointStackN = 0; + lineStackN = 0; + rectStackN = 0; + polygonStackN = 0; + ellipseStackN = 0; + lettersStackN = 0; + imageStackN = 0; + groupStackN = 0; + + UIElementPoolN = 0; + brushPoolN = 0; + /* + pointPoolN = 0; + brushPoolN = 0; + linePoolN = 0; + rectPoolN = 0; + ellipsePoolN = 0; + polygonPoolN = 0; + lettersPoolN = 0; + imagePoolN = 0; + groupPoolN = 0; + * */ + + //executeFlip(); Internal.Main.canvas_flag.WaitOne(); } + public void flip() + { + flip(1); + } + + + #region version modifyNative2 + public void executeFlip() + { + Clock after = new Clock(); + after.update(); + AppState.statusBar = ((after - before).at_msec().ToString()) + " msec"; + + Line lineS; + Rectangle rectS; + Ellipse ellipseS; + Polygon polygonS; + Letters lettersS; + Image imageS; + Group groupS; + System.Windows.Shapes.Line lineP; + System.Windows.Shapes.Rectangle rectP; + System.Windows.Shapes.Ellipse ellipseP; + System.Windows.Shapes.Polygon polygonP; + System.Windows.Controls.TextBlock lettersP; + System.Windows.Controls.Image imageP; + System.Windows.Controls.Canvas groupP; + + lock (this) + { + nextIntervalFrame--; + } + + var cnv = UIElementPool[0]; + if (nextIntervalFrame <= 0) + { + if (chacked > 0) + { + if (stackN > 0) + { + for (int i = 0; i < stackN; i++) + { + if( null != (rectS = stack[i] as Rectangle) ) + { + if (null != (rectP = cnv.Children[0] as System.Windows.Shapes.Rectangle)) + { + rectS.modifyNative(rectP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (lineS = stack[i] as Line)) + { + if (null != (lineP = cnv.Children[0] as System.Windows.Shapes.Line)) + { + lineS.modifyNative(lineP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (ellipseS = stack[i] as Ellipse)) + { + if (null != (ellipseP = cnv.Children[0] as System.Windows.Shapes.Ellipse)) + { + ellipseS.modifyNative(ellipseP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (polygonS = stack[i] as Polygon)) + { + if (null != (polygonP = cnv.Children[0] as System.Windows.Shapes.Polygon)) + { + polygonS.modifyNative(polygonP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (lettersS = stack[i] as Letters)) + { + if (null != (lettersP = cnv.Children[0] as System.Windows.Controls.TextBlock)) + { + lettersS.modifyNative(lettersP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (imageS = stack[i] as Image)) + { + if (null != (imageP = cnv.Children[0] as System.Windows.Controls.Image)) + { + imageS.modifyNative(imageP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (groupS = stack[i] as Group)) + { + if (null != (groupP = cnv.Children[0] as System.Windows.Controls.Canvas)) + { + groupS.modifyNative(groupP, this); + } + else + { + cnv.Children.Clear(); + cnv.Children.Add(stack[i].poolNative(this)); + } + } + cnv.Visibility = Visibility.Visible; + UIElementPoolN++; + cnv = UIElementPool[UIElementPoolN]; + } + for (int i = UIElementPoolN; i < lastVisibleN; i++) + { + cnv = UIElementPool[i]; + cnv.Visibility = Visibility.Collapsed; + } + lastVisibleN = UIElementPoolN; + stackN = 0; + } + lock (this) + { + chacked = 0; + } + Psychlops.Internal.Main.canvas_flag.Set(); + } + } + System.Threading.Thread.Sleep(0); + } + #endregion + + #region version modifyNative + /* + public void executeFlip() + { + Line lineS; + Rectangle rectS; + Ellipse ellipseS; + Polygon polygonS; + Letters lettersS; + Image imageS; + Group groupS; + System.Windows.Shapes.Line lineP; + System.Windows.Shapes.Rectangle rectP; + System.Windows.Shapes.Ellipse ellipseP; + System.Windows.Shapes.Polygon polygonP; + System.Windows.Controls.TextBlock lettersP; + System.Windows.Controls.Image imageP; + System.Windows.Controls.Canvas groupP; + + lock (this) + { + nextIntervalFrame--; + } + + var en = masterPool.Children.GetEnumerator(); + bool full = en.MoveNext(); + if (nextIntervalFrame <= 0) + { + if (chacked > 0) + { + //masterPool.Children.Clear(); + if (stackN > 0) + { + for (int i = 0; i < stackN - 2; i++) + { + if (full == false) + { + masterPool.Children.Add(stack[i].poolNative(this)); + } + else + { + if( null != (rectS = stack[i] as Rectangle) ) + { + if (null != (rectP = en.Current as System.Windows.Shapes.Rectangle)) + { + rectS.modifyNative(rectP, this); + } + } + else if (null != (lineS = stack[i] as Line)) + { + if (null != (lineP = en.Current as System.Windows.Shapes.Line)) + { + lineS.modifyNative(lineP, this); + } + } + else if (null != (ellipseS = stack[i] as Ellipse)) + { + if (null != (ellipseP = en.Current as System.Windows.Shapes.Ellipse)) + { + ellipseS.modifyNative(ellipseP, this); + } + else + { + masterPool.Children.Add(stack[i].poolNative(this)); + } + } + else if (null != (polygonS = stack[i] as Polygon)) + { + if (null != (polygonP = en.Current as System.Windows.Shapes.Polygon)) + { + polygonS.modifyNative(polygonP, this); + } + } + else if (null != (lettersS = stack[i] as Letters)) + { + if (null != (lettersP = en.Current as System.Windows.Controls.TextBlock)) + { + lettersS.modifyNative(lettersP, this); + } + } + else if (null != (imageS = stack[i] as Image)) + { + if (null != (imageP = en.Current as System.Windows.Controls.Image)) + { + imageS.modifyNative(imageP, this); + } + } + else if (null != (groupS = stack[i] as Group)) + { + if (null != (groupP = en.Current as System.Windows.Controls.Canvas)) + { + groupS.modifyNative(groupP, this); + } + } + full = en.MoveNext(); + } + } + stackN = 0; + } + lock (this) + { + chacked = 0; + } + Psychlops.Internal.Main.canvas_flag.Set(); + } + } + System.Threading.Thread.Sleep(0); + } + * */ + #endregion + + #region version poolNative 2 + /* public void executeFlip() { + lock (this) { nextIntervalFrame--; } + UIElementPoolN = 0; if (nextIntervalFrame <= 0) { - //Psychlops.AppState.statusBar = chacked.ToString(); if (chacked > 0) { - var tmp = new System.Windows.Controls.Canvas(); - if (stack.Count > 0) + //masterPool.Children.Clear(); + if (stackN > 0) { - foreach (Internal.PrimitiveFigure f in stack) + for (int i = 0; i < stackN - 2; i++) { - tmp.Children.Add(f.toNative()); + UIElementPool[UIElementPoolN] = stack[i].poolNative(this); + UIElementPool[UIElementPoolN].Visibility = Visibility.Visible; + UIElementPoolN++; + + } + for (int i = stackN - 2; i < 10000; i++) + { + UIElementPool[UIElementPoolN] = rectPool[i]; + UIElementPool[UIElementPoolN].Visibility = Visibility.Collapsed; + UIElementPoolN++; } + stackN = 0; } - api_canvas.Children.Clear(); - api_canvas.Children.Add(tmp); - stack.Clear(); lock (this) { chacked = 0; @@ -230,6 +618,9 @@ namespace Psychlops } System.Threading.Thread.Sleep(0); } + */ + #endregion + #region Properties @@ -246,12 +637,46 @@ namespace Psychlops #endregion + #region compatibitily trick + + public enum Mode { window, fullscreen } + public static readonly Mode window = Mode.window, fullscreen = Mode.fullscreen; + + public Canvas(int wid, int hei, Mode mod) + { + panel = default_panel; + api_canvas = default_api_canvas; + initialize(500, 500); + } + public Canvas(Mode mod) : base() + { + panel = default_panel; + api_canvas = default_api_canvas; + initialize(500, 500); + } + + + public void showFPS(bool sw = true) { } + public void watchFPS(bool sw = true) { } + + + public void clear(double lum) + { + clear(new Color(lum)); + } + + #endregion + + } #region primitive tokenizer + + #region primitive + partial struct Point { public static implicit operator System.Windows.Point(Point d) @@ -270,6 +695,14 @@ namespace Psychlops { return new SolidColorBrush { Color = d }; } + public System.Windows.Media.SolidColorBrush getNativeFromStack(Canvas d) + { + var tmp = d.brushPool[d.brushPoolN]; + tmp.Color = this; + d.brushPoolN++; + return tmp; + } + } partial struct Stroke @@ -284,7 +717,18 @@ namespace Psychlops { return new SolidColorBrush { Color = d.color }; } + public System.Windows.Media.SolidColorBrush getNativeFromStack(Canvas d) + { + var tmp = d.brushPool[d.brushPoolN]; + tmp.Color = this.color; + d.brushPoolN++; + return tmp; + } } + + #endregion + + #region Line partial class Line { @@ -304,7 +748,52 @@ namespace Psychlops return tmp; } public UIElement toNative() { return this; } + + public void copyToStack(Templates.StackableDrawable d) + { + var tmp = d.lineStack[d.lineStackN]; + tmp.begin.x = begin.x; + tmp.begin.y = begin.y; + tmp.end.x = end.x; + tmp.end.y = end.y; + tmp.fill = fill; + tmp.stroke = stroke; + d.stack[d.stackN] = tmp; + d.lineStackN++; + d.stackN++; + } + public UIElement poolNative(Canvas d) + { + var tmp = d.linePool[d.linePoolN]; + tmp.X1 = begin.x; + tmp.Y1 = begin.y; + tmp.X2 = end.x; + tmp.Y2 = end.y; + if (stroke.thick == 0.0) tmp.Stroke = fill.getNativeFromStack(d); + else stroke.apply(tmp); + System.Windows.Controls.Canvas.SetLeft(tmp, left); + System.Windows.Controls.Canvas.SetTop(tmp, top); + tmp.Visibility = Visibility.Visible; + d.linePoolN++; + return tmp; + } + public void modifyNative(System.Windows.Shapes.Line tmp, Canvas d) + { + tmp.X1 = begin.x; + tmp.Y1 = begin.y; + tmp.X2 = end.x; + tmp.Y2 = end.y; + if (stroke.thick == 0.0) tmp.Stroke = fill.getNativeFromStack(d); + else stroke.apply(tmp); + System.Windows.Controls.Canvas.SetLeft(tmp, left); + System.Windows.Controls.Canvas.SetTop(tmp, top); + tmp.Visibility = Visibility.Visible; + } } + + #endregion + + #region Rectangle partial class Rectangle { @@ -328,8 +817,44 @@ namespace Psychlops System.Windows.Controls.Canvas.SetTop(tmp, d.top); return tmp; } + public UIElement toNative() { return this; } + public void copyToStack(Templates.StackableDrawable d) + { + var tmp = d.rectStack[d.rectStackN]; + tmp.v1 = v1; + tmp.v2 = v2; + tmp.fill = fill; + d.stack[d.stackN] = tmp; + d.rectStackN++; + d.stackN++; + } + public UIElement poolNative(Canvas d) + { + var tmp = d.rectPool[d.rectPoolN]; + tmp.Width = width; + tmp.Height = height; + tmp.Fill = fill.getNativeFromStack(d); + System.Windows.Controls.Canvas.SetLeft(tmp, left); + System.Windows.Controls.Canvas.SetTop(tmp, top); + tmp.Visibility = Visibility.Visible; + d.rectPoolN++; + return tmp; + } + public void modifyNative(System.Windows.Shapes.Rectangle tmp, Canvas d) + { + tmp.Width = width; + tmp.Height = height; + tmp.Fill = fill.getNativeFromStack(d); + System.Windows.Controls.Canvas.SetLeft(tmp, left); + System.Windows.Controls.Canvas.SetTop(tmp, top); + tmp.Visibility = Visibility.Visible; + } } + + #endregion + + #region Ellipse partial class Ellipse { @@ -349,9 +874,47 @@ namespace Psychlops System.Windows.Controls.Canvas.SetTop(tmp, d.top); return tmp; } + public UIElement toNative() { return this; } + + public void copyToStack(Templates.StackableDrawable d) + { + var tmp = d.ellipseStack[d.ellipseStackN]; + tmp.datum = datum; + tmp.xdiameter = xdiameter; + tmp.ydiameter = ydiameter; + tmp.fill = fill; + d.stack[d.stackN] = tmp; + d.ellipseStackN++; + d.stackN++; + } + public UIElement poolNative(Canvas d) + { + var tmp = d.ellipsePool[d.ellipsePoolN]; + tmp.Width = width; + tmp.Height = height; + tmp.Fill = fill.getNativeFromStack(d); + System.Windows.Controls.Canvas.SetLeft(tmp, left); + System.Windows.Controls.Canvas.SetTop(tmp, top); + tmp.Visibility = Visibility.Visible; + d.ellipsePoolN++; + return tmp; + } + public void modifyNative(System.Windows.Shapes.Ellipse tmp, Canvas d) + { + tmp.Width = width; + tmp.Height = height; + tmp.Fill = fill.getNativeFromStack(d); + System.Windows.Controls.Canvas.SetLeft(tmp, left); + System.Windows.Controls.Canvas.SetTop(tmp, top); + tmp.Visibility = Visibility.Visible; + } } + #endregion + + #region Polygon + partial class Polygon { public Polygon dup() @@ -375,8 +938,55 @@ namespace Psychlops return tmp; } public UIElement toNative() { return this; } + + public void copyToStack(Templates.StackableDrawable d) + { + var tmp = d.polygonStack[d.polygonStackN]; + tmp.datum = datum; + tmp.vertices.Clear(); + foreach (var v in vertices) + { + tmp.vertices.Add(v); + } + tmp.fill = fill; + d.stack[d.stackN] = tmp; + d.polygonStackN++; + d.stackN++; + } + public UIElement poolNative(Canvas d) + { + var tmp = d.polygonPool[d.polygonPoolN]; + tmp.Fill = fill.getNativeFromStack(d); + tmp.Points.Clear(); + foreach (var v in vertices) + { + tmp.Points.Add(v); + } + System.Windows.Controls.Canvas.SetLeft(tmp, datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, datum.y); + tmp.Visibility = Visibility.Visible; + d.polygonPoolN++; + return tmp; + } + public void modifyNative(System.Windows.Shapes.Polygon tmp, Canvas d) + { + tmp.Fill = fill.getNativeFromStack(d); + tmp.Points.Clear(); + foreach (var v in vertices) + { + tmp.Points.Add(v); + } + System.Windows.Controls.Canvas.SetLeft(tmp, datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, datum.y); + tmp.Visibility = Visibility.Visible; + } + } - + + #endregion + + #region Letters + partial class Letters { #region static initializer @@ -431,18 +1041,104 @@ namespace Psychlops return tmp; } public UIElement toNative() { return this; } + + public void copyToStack(Templates.StackableDrawable d) + { + var tmp = d.lettersStack[d.lettersStackN]; + tmp.str = str; + tmp.datum = datum; + tmp.fill = fill; + d.stack[d.stackN] = tmp; + d.lettersStackN++; + d.stackN++; + } + public UIElement poolNative(Canvas d) + { + var tmp = d.lettersPool[d.lettersPoolN]; + tmp.Text = str; + tmp.Width = 500; + tmp.Height = 500; + tmp.FontSize = font.size; + //tmp.FontFamily = , + tmp.FontStyle = FONT_STYLE_BRIDGE[font.style]; + tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight]; + tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align]; + tmp.Foreground = fill.getNativeFromStack(d); + System.Windows.Controls.Canvas.SetLeft(tmp, datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, datum.y); + tmp.Visibility = Visibility.Visible; + d.lettersPoolN++; + return tmp; + } + public void modifyNative(System.Windows.Controls.TextBlock tmp, Canvas d) + { + tmp.Text = str; + tmp.Width = 500; + tmp.Height = 500; + tmp.FontSize = font.size; + //tmp.FontFamily = , + tmp.FontStyle = FONT_STYLE_BRIDGE[font.style]; + tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight]; + tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align]; + tmp.Foreground = fill.getNativeFromStack(d); + System.Windows.Controls.Canvas.SetLeft(tmp, datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, datum.y); + tmp.Visibility = Visibility.Visible; + } } + + #endregion + + #region Image partial class Image { internal void initialize__(int wid, int hei) { + AsyncBool = false; Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.TwoIntProcedure(create__), wid, hei); - //buffer = new WriteableBitmap(wid, hei); + while (!AsyncBool) { System.Threading.Thread.Sleep(10); } } internal void create__(int wid, int hei) { buffer = new WriteableBitmap(wid, hei); + AsyncBool = true; + } + internal void load__(string uri) + { + AsyncBool = false; + var ur = new System.Uri(uri, System.UriKind.RelativeOrAbsolute); + Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Action(load_), ur); + while (!AsyncBool) { System.Threading.Thread.Sleep(10); } + } + internal void load_(Uri uri) + { + var bitmap = new BitmapImage(); + bitmap.CreateOptions = BitmapCreateOptions.None; + bitmap.UriSource = uri; + try + { + var wbm = new System.Windows.Media.Imaging.WriteableBitmap(bitmap); + buffer = wbm; + } + catch (Exception e) + { + buffer = new WriteableBitmap(64, 64); + buffer.ForEach(bitmap_drawChecker); + } + self_rect.set(buffer.PixelWidth, buffer.PixelHeight); + AsyncBool = true; + } + static System.Windows.Media.Color[] CHECKER_C; + static Image() + { + CHECKER_C = new System.Windows.Media.Color[2]; + CHECKER_C[0] = System.Windows.Media.Color.FromArgb(0, 0, 0, 0); + CHECKER_C[1] = System.Windows.Media.Color.FromArgb(128,128,128,128); + } + static System.Windows.Media.Color bitmap_drawChecker(int x, int y) + { + return ((x / 4) + (y / 4)) % 2 == 0 ? CHECKER_C[0] : CHECKER_C[1]; } delegate void FieldFunc1(System.Func func); delegate void FieldFunc2(System.Func func); @@ -478,9 +1174,124 @@ namespace Psychlops return tmp; } public UIElement toNative() { return this; } + + public void copyToStack(Templates.StackableDrawable d) + { + var tmp = d.imageStack[d.imageStackN]; + tmp.datum = datum; + tmp.buffer = buffer; + tmp.self_rect = self_rect; + d.stack[d.stackN] = tmp; + d.imageStackN++; + d.stackN++; + } + public UIElement poolNative(Canvas d) + { + var tmp = d.imagePool[d.imagePoolN]; + tmp.Source = buffer; + System.Windows.Controls.Canvas.SetLeft(tmp, datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, datum.y); + tmp.Visibility = Visibility.Visible; + d.imagePoolN++; + return tmp; + } + public void modifyNative(System.Windows.Controls.Image tmp, Canvas d) + { + tmp.Source = buffer; + System.Windows.Controls.Canvas.SetLeft(tmp, datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, datum.y); + tmp.Visibility = Visibility.Visible; + } + } #endregion + #region Group + + partial class Group + { + internal void initialize__() + { + Canvas.default_api_canvas.Dispatcher.BeginInvoke(new Canvas.SimpleProcedure(create__)); + } + internal void create__() + { + cnvs = new System.Windows.Controls.Canvas(); + trans = new System.Windows.Media.TransformGroup(); + transF = new System.Windows.Media.TransformCollection(); + rotateF = new System.Windows.Media.RotateTransform(); + scaleF = new System.Windows.Media.ScaleTransform(); + translateF = new System.Windows.Media.TranslateTransform(); + transF.Add(rotateF); + transF.Add(scaleF); + transF.Add(translateF); + trans.Children = transF; + cnvs.RenderTransform = trans; + AsyncBool = true; + } + public Group clone() + { + return (Group)MemberwiseClone(); + } + + delegate void AppendFunc1(Internal.PrimitiveFigure func); + void append__(Internal.PrimitiveFigure fig) + { + fig.centering(0, 0); + UIElement e = fig.toNative(); + cnvs.Children.Add(e); + System.Windows.Controls.Canvas.SetLeft(e, fig.datum.x); + System.Windows.Controls.Canvas.SetTop(e, fig.datum.y); + } + delegate void SimpleProcedure(); + void getRotation__() { rotation_ = rotateF.Angle; } + void setRotation__() { rotateF.Angle = rotation_; } + //void getTranslation__() { rotation_ = rotateF.Angle; } + void setTranslation__() { translateF.X = datum.x; translateF.Y = datum.y; } + void setScaling__() { scaleF.ScaleX = scaling_.x; scaleF.ScaleY = scaling_.y; } + + public static implicit operator System.Windows.Controls.Canvas(Group d) + { + var tmp = d.cnvs;//new System.Windows.Controls.Canvas(); + System.Windows.Controls.Canvas.SetLeft(d.cnvs, d.datum.x); + System.Windows.Controls.Canvas.SetTop(d.cnvs, d.datum.y); + return tmp; + } + public UIElement toNative() { return this; } + + public void copyToStack(Templates.StackableDrawable d) + { + var tmp = d.groupStack[d.groupStackN]; + tmp.datum = datum; + tmp.cnvs = cnvs; + d.stack[d.stackN] = tmp; + d.groupStackN++; + d.stackN++; + } + public UIElement poolNative(Canvas d) + { + //d.groupPool[d.groupPoolN] = cnvs; + //var tmp = d.groupPool[d.groupPoolN]; + var tmp = cnvs; + System.Windows.Controls.Canvas.SetLeft(tmp, datum.x); + System.Windows.Controls.Canvas.SetTop(tmp, datum.y); + tmp.Visibility = Visibility.Visible; + //d.groupPoolN++; + return tmp; + } + public void modifyNative(System.Windows.Controls.Canvas tmp, Canvas d) + { + d.groupPool[d.groupPoolN] = cnvs; + System.Windows.Controls.Canvas.SetLeft(cnvs, datum.x); + System.Windows.Controls.Canvas.SetTop(cnvs, datum.y); + tmp.Visibility = Visibility.Visible; + } + + } + #endregion + + #endregion + } \ No newline at end of file