X-Git-Url: http://git.osdn.jp/view?a=blobdiff_plain;f=dev4%2Fpsychlops%2Fcore%2Fgraphic%2Fcanvas.cs;h=4d65ec4c6725bcba89a6bec154fbe86f46a278f2;hb=7bd951bfbca3432f306f4f114cc0b06f47e13ed9;hp=7f6360c3306be02775792d3696a22619f5720efe;hpb=feba6ebcec2fd2c6381c0ee16a784ca9c38a33af;p=psychlops%2Fsilverlight.git diff --git a/dev4/psychlops/core/graphic/canvas.cs b/dev4/psychlops/core/graphic/canvas.cs index 7f6360c..4d65ec4 100644 --- a/dev4/psychlops/core/graphic/canvas.cs +++ b/dev4/psychlops/core/graphic/canvas.cs @@ -9,11 +9,21 @@ 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 { @@ -21,93 +31,61 @@ namespace Psychlops { // protected System.Collections.Generic.Queue stack; internal Internal.PrimitiveFigure[] stack; - internal int stackN; + internal int stackN = 0; internal Line[] lineStack; - internal int lineStackN; + internal int lineStackN = 0; internal Rectangle[] rectStack; - internal int rectStackN; + internal int rectStackN = 0; internal Ellipse[] ellipseStack; - internal int ellipseStackN; + internal int ellipseStackN = 0; internal Polygon[] polygonStack; - internal int polygonStackN; + internal int polygonStackN = 0; internal Letters[] lettersStack; - internal int lettersStackN; + internal int lettersStackN = 0; internal Image[] imageStack; - internal int imageStackN; - + internal int imageStackN = 0; + internal Group[] groupStack; + internal int groupStackN = 0; public StackableDrawable() { -// stack = new System.Collections.Generic.Queue(); - stack = new Internal.PrimitiveFigure[40000]; - lineStack = new Line[10000]; - rectStack = new Rectangle[10000]; - ellipseStack = new Ellipse[10000]; - polygonStack = new Polygon[1000]; - lettersStack = new Letters[1000]; - imageStack = new Image[1000]; - for (int i = 0; i < 10000; i++) + 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 < 1000; i++) + for (int i = 0; i < CONST.COBJ_N; i++) { polygonStack[i] = new Polygon(); lettersStack[i] = new Letters(); - //imageStack[i] = new Image(); + } + for (int i = 0; i < CONST.HOBJ_N; i++) + { + imageStack[i] = new Image(); +// groupStack[i] = new Group(); } } - 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) - { -// stack.Enqueue(drawee.clone()); - } - - public virtual void rect(Rectangle drawee) - { - //stack.Enqueue(drawee.clone()); -// stack.Enqueue(drawee.copyToStack(this)); - drawee.copyToStack(this); - } - - public virtual void ellipse(Ellipse drawee) - { - // stack.Enqueue(drawee.clone()); - drawee.copyToStack(this); - } - - public virtual void polygon(Polygon drawee) - { - // stack.Enqueue(drawee.clone()); - drawee.copyToStack(this); - } - - public virtual void letters(Letters drawee) - { - // stack.Enqueue(drawee.clone()); - drawee.copyToStack(this); - } - - public virtual void image(Image drawee) - { - // stack.Enqueue(drawee.clone()); - drawee.copyToStack(this); - } + 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 oval(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) @@ -127,20 +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 @@ -148,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) { @@ -171,11 +159,22 @@ namespace Psychlops api_canvas = apicnvs; initialize(wid, hei); } + + protected bool AsyncInitBool; protected void initialize(int wid, int hei) { + before = new Clock(); + before.update(); + var after = new Clock(); + AsyncInitBool = false; width_ = wid; height_ = hei; api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei); + while (!AsyncInitBool) + { + after.update(); + if ((after - before).at_msec() > 1000) break; + } Mouse._prime = api_canvas; Main.drawable = this; Main.canvas = this; @@ -187,7 +186,7 @@ namespace Psychlops 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; @@ -198,58 +197,81 @@ namespace Psychlops HtmlElement htmlHost = HtmlPage.Document.GetElementById("silverlightControlHost"); //if (htmlHost != null) HtmlPage.Window.Alert("silverlightControlHost is null"); htmlHost.SetStyleAttribute("width", (wid).ToString()+"px"); - htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px"); + htmlHost.SetStyleAttribute("height", (hei).ToString() + "px"); htmlHost.SetStyleAttribute("margin", "2em auto auto auto"); - - pointPool = new System.Windows.Point[10000]; - brushPool = new SolidColorBrush[10000]; - linePool = new System.Windows.Shapes.Line[10000]; - rectPool = new System.Windows.Shapes.Rectangle[10000]; - for (int i = 0; i < 10000; i++) + 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(); } - for (int i = 0; i < 300; i++) + 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++) { - api_canvas.Children.Add(rectStack[i].poolNative(this)); - api_canvas.Children.Add(rectStack[i].poolNative(this)); + 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(); -// masterPool.Children.Add() + 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 @@ -258,116 +280,338 @@ namespace Psychlops public override void clear(Color col) { back_panel.fill = col; -// stack.Clear(); 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; } -// pointPoolN = 0; -// brushPoolN = 0; -// linePoolN = 0; - rectPoolN = 0; + //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) { - var rr = new Random(); - //Psychlops.AppState.statusBar = chacked.ToString(); if (chacked > 0) { - // prevPool.Children.Clear(); - var tmp = new System.Windows.Controls.Canvas(); - if (stack.Count > 0) - // if (stackN > 0) + if (stackN > 0) { - //foreach (UIElement e in api_canvas.Children) - //{ - //} -// foreach (Internal.PrimitiveFigure f in stack) - for (int i = 0; i < stackN - 2; i++) + for (int i = 0; i < stackN; i++) { - // tmp.Children.Add(f.toNative()); -// tmp.Children.Add(f.poolNative(this)); - tmp.Children.Add(stack[i].poolNative(this)); - // System.Windows.Controls.Canvas.SetLeft(api_canvas.Children[i + 1], rectStack[i].left); - // api_canvas.Children[0] = api_canvas.Children[0]; + 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; - } - api_canvas.Children.Clear(); - api_canvas.Children.Add(tmp); - // stack.Clear(); lock (this) { chacked = 0; } Psychlops.Internal.Main.canvas_flag.Set(); - // prevPool = tmp; } } System.Threading.Thread.Sleep(0); } - */ + #endregion - public void flip() + + + #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 = 1; - chacked = 1; + nextIntervalFrame--; } - // pointPoolN = 0; - // brushPoolN = 0; - // linePoolN = 0; - rectPoolN = 0; - lineStackN = 0; - rectStackN = 0; - Internal.Main.canvas_flag.WaitOne(); + + 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) { if (chacked > 0) { - //api_canvas.Children.Clear(); - //var tmp = new System.Windows.Controls.Canvas(); + //masterPool.Children.Clear(); if (stackN > 0) { for (int i = 0; i < stackN - 2; i++) { - //api_canvas.Children.Add(stack[i].poolNative(this)); - api_canvas.Children.Add(stack[i].poolNative(this)); + 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); lock (this) { chacked = 0; @@ -377,7 +621,8 @@ namespace Psychlops } System.Threading.Thread.Sleep(0); } - + */ + #endregion #region Properties @@ -395,12 +640,61 @@ 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 Canvas(int wid, int hei, Mode mod, Display.DisplayName name) + { + panel = default_panel; + api_canvas = default_api_canvas; + initialize(500, 500); + } + public Canvas(Mode mod, Display.DisplayName name) + : 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) @@ -419,6 +713,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 @@ -433,7 +735,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 { @@ -454,6 +767,19 @@ namespace Psychlops } 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]; @@ -461,7 +787,7 @@ namespace Psychlops tmp.Y1 = begin.y; tmp.X2 = end.x; tmp.Y2 = end.y; - if (stroke.thick == 0.0) tmp.Stroke = fill; + 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); @@ -469,7 +795,23 @@ namespace Psychlops 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 { @@ -510,14 +852,27 @@ namespace Psychlops var tmp = d.rectPool[d.rectPoolN]; tmp.Width = width; tmp.Height = height; - tmp.Fill = fill; + 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 { @@ -548,7 +903,7 @@ namespace Psychlops tmp.ydiameter = ydiameter; tmp.fill = fill; d.stack[d.stackN] = tmp; - d.rectStackN++; + d.ellipseStackN++; d.stackN++; } public UIElement poolNative(Canvas d) @@ -556,15 +911,28 @@ namespace Psychlops var tmp = d.ellipsePool[d.ellipsePoolN]; tmp.Width = width; tmp.Height = height; - tmp.Fill = fill; + tmp.Fill = fill.getNativeFromStack(d); System.Windows.Controls.Canvas.SetLeft(tmp, left); - System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top); + System.Windows.Controls.Canvas.SetTop(tmp, top); tmp.Visibility = Visibility.Visible; d.ellipsePoolN++; - return this; + 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() @@ -600,24 +968,43 @@ namespace Psychlops } tmp.fill = fill; d.stack[d.stackN] = tmp; - d.rectStackN++; + d.polygonStackN++; d.stackN++; } public UIElement poolNative(Canvas d) { var tmp = d.polygonPool[d.polygonPoolN]; - tmp.Fill = fill; - /* - System.Windows.Controls.Canvas.SetLeft(tmp, left); - System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top); - */ + 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 this; + 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 @@ -675,38 +1062,101 @@ namespace Psychlops public void copyToStack(Templates.StackableDrawable d) { - var tmp = d.polygonStack[d.polygonStackN]; + var tmp = d.lettersStack[d.lettersStackN]; + tmp.str = str; tmp.datum = datum; tmp.fill = fill; d.stack[d.stackN] = tmp; - d.rectStackN++; + d.lettersStackN++; d.stackN++; } public UIElement poolNative(Canvas d) { - /* var tmp = d.rectPool[d.rectPoolN]; - tmp.Width = width; - tmp.Height = height; - tmp.Fill = fill; - System.Windows.Controls.Canvas.SetLeft(tmp, left); - System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top); - tmp.Visibility = Visibility.Visible; - d.rectPoolN++; -*/ - return this; + 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); @@ -743,23 +1193,123 @@ namespace Psychlops } 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.rectPool[d.rectPoolN]; - tmp.Width = width; - tmp.Height = height; - tmp.Fill = fill; - System.Windows.Controls.Canvas.SetLeft(tmp, left); - System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top); - tmp.Visibility = Visibility.Visible; - d.rectPoolN++; -*/ - return this; + 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