OSDN Git Service

111
[psychlops/silverlight.git] / dev4 / psychlops / core / graphic / canvas.cs
index a695257..e8d2af8 100644 (file)
@@ -19,11 +19,45 @@ namespace Psychlops
 \r
                public class StackableDrawable : Drawable\r
                {\r
-                       protected System.Collections.Generic.Queue<Internal.PrimitiveFigure> stack;\r
+//                     protected System.Collections.Generic.Queue<Internal.PrimitiveFigure> stack;\r
+                       internal Internal.PrimitiveFigure[] stack;\r
+                       internal int stackN;\r
+                       internal Line[] lineStack;\r
+                       internal int lineStackN;\r
+                       internal Rectangle[] rectStack;\r
+                       internal int rectStackN;\r
+                       internal Ellipse[] ellipseStack;\r
+                       internal int ellipseStackN;\r
+                       internal Polygon[] polygonStack;\r
+                       internal int polygonStackN;\r
+                       internal Letters[] lettersStack;\r
+                       internal int lettersStackN;\r
+                       internal Image[] imageStack;\r
+                       internal int imageStackN;\r
+\r
 \r
                        public StackableDrawable()\r
                        {\r
-                               stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();\r
+//                             stack = new System.Collections.Generic.Queue<Internal.PrimitiveFigure>();\r
+                               stack = new Internal.PrimitiveFigure[40000];\r
+                               lineStack = new Line[10000];\r
+                               rectStack = new Rectangle[10000];\r
+                               ellipseStack = new Ellipse[10000];\r
+                               polygonStack = new Polygon[1000];\r
+                               lettersStack = new Letters[1000];\r
+                               imageStack = new Image[1000];\r
+                               for (int i = 0; i < 10000; i++)\r
+                               {\r
+                                       lineStack[i] = new Line(0,0,0,0);\r
+                                       rectStack[i] = new Rectangle();\r
+                                       ellipseStack[i] = new Ellipse();\r
+                               }\r
+                               for (int i = 0; i < 1000; i++)\r
+                               {\r
+                                       polygonStack[i] = new Polygon();\r
+                                       lettersStack[i] = new Letters();\r
+                                       imageStack[i] = new Image(1,1);\r
+                               }\r
                        }\r
 \r
                        public void clear()\r
@@ -41,32 +75,39 @@ namespace Psychlops
 \r
                        public virtual void line(Line drawee)\r
                        {\r
-                               stack.Enqueue(drawee.clone());\r
+                               //                              stack.Enqueue(drawee.clone());\r
+                               drawee.copyToStack(this);\r
                        }\r
 \r
                        public virtual void rect(Rectangle drawee)\r
                        {\r
-                               stack.Enqueue(drawee.clone());\r
+                               //stack.Enqueue(drawee.clone());\r
+//                             stack.Enqueue(drawee.copyToStack(this));\r
+                               drawee.copyToStack(this);\r
                        }\r
 \r
                        public virtual void ellipse(Ellipse drawee)\r
                        {\r
-                               stack.Enqueue(drawee.clone());\r
+                               //                              stack.Enqueue(drawee.clone());\r
+                               drawee.copyToStack(this);\r
                        }\r
 \r
                        public virtual void polygon(Polygon drawee)\r
                        {\r
-                               stack.Enqueue(drawee.clone());\r
+                               //                              stack.Enqueue(drawee.clone());\r
+                               drawee.copyToStack(this);\r
                        }\r
 \r
                        public virtual void letters(Letters drawee)\r
                        {\r
-                               stack.Enqueue(drawee.clone());\r
+                               //                              stack.Enqueue(drawee.clone());\r
+                               drawee.copyToStack(this);\r
                        }\r
 \r
                        public virtual void image(Image drawee)\r
                        {\r
-                               stack.Enqueue(drawee.clone());\r
+                               //                              stack.Enqueue(drawee.clone());\r
+                               drawee.copyToStack(this);\r
                        }\r
 \r
                        public void msg(string str, double x, double y) { msg(str, x, y, Color.white); }\r
@@ -88,6 +129,24 @@ namespace Psychlops
        public class Canvas : Templates.StackableDrawable\r
        {\r
 \r
+               internal System.Windows.Controls.Canvas masterPool, prevPool;\r
+               internal System.Windows.Point[] pointPool;\r
+               internal int pointPoolN;\r
+               internal SolidColorBrush[] brushPool;\r
+               internal int brushPoolN;\r
+               internal System.Windows.Shapes.Line[] linePool;\r
+               internal int linePoolN;\r
+               internal System.Windows.Shapes.Rectangle[] rectPool;\r
+               internal int rectPoolN;\r
+               internal System.Windows.Shapes.Ellipse[] ellipsePool;\r
+               internal int ellipsePoolN;\r
+               internal System.Windows.Shapes.Polygon[] polygonPool;\r
+               internal int polygonPoolN;\r
+               internal System.Windows.Controls.TextBlock[] lettersPool;\r
+               internal int lettersPoolN;\r
+               internal System.Windows.Controls.Image[] imagePool;\r
+               internal int imagePoolN;\r
+\r
                #region initializer\r
 \r
                internal delegate void TwoIntProcedure(int x, int y);\r
@@ -117,12 +176,17 @@ namespace Psychlops
                        api_canvas = apicnvs;\r
                        initialize(wid, hei);\r
                }\r
+\r
+               protected bool AsyncInitBool;\r
                protected void initialize(int wid, int hei)\r
                {\r
-\r
+                       AsyncInitBool = false;\r
                        width_ = wid;\r
                        height_ = hei;\r
                        api_canvas.Dispatcher.BeginInvoke(new TwoIntProcedure(initialize__), wid, hei);\r
+                       //while(!AsyncInitBool)\r
+                       //{\r
+                       //}\r
                        Mouse._prime = api_canvas;\r
                        Main.drawable = this;\r
                        Main.canvas = this;\r
@@ -147,6 +211,42 @@ namespace Psychlops
                        htmlHost.SetStyleAttribute("width", (wid).ToString()+"px");\r
                        htmlHost.SetStyleAttribute("height", (20+hei).ToString() + "px");\r
                        htmlHost.SetStyleAttribute("margin", "2em auto auto auto");\r
+\r
+\r
+                       pointPool = new System.Windows.Point[10000];\r
+                       brushPool = new SolidColorBrush[10000];\r
+                       linePool = new System.Windows.Shapes.Line[10000];\r
+                       rectPool = new System.Windows.Shapes.Rectangle[10000];\r
+                       ellipsePool = new System.Windows.Shapes.Ellipse[10000];\r
+                       for (int i = 0; i < 10000; i++)\r
+                       {\r
+                               pointPool[i] = new System.Windows.Point();\r
+                               brushPool[i] = new SolidColorBrush();\r
+                               linePool[i] = new System.Windows.Shapes.Line();\r
+                               rectPool[i] = new System.Windows.Shapes.Rectangle();\r
+                               ellipsePool[i] = new System.Windows.Shapes.Ellipse();\r
+                       }\r
+                       polygonPool = new System.Windows.Shapes.Polygon[1000];\r
+                       lettersPool = new System.Windows.Controls.TextBlock[1000];\r
+                       for (int i = 0; i < 1000; i++)\r
+                       {\r
+                               polygonPool[i] = new System.Windows.Shapes.Polygon();\r
+                               lettersPool[i] = new System.Windows.Controls.TextBlock();\r
+                       }\r
+                       /*\r
+                       imagePool = new System.Windows.Controls.Image[100];\r
+                       for (int i = 0; i < 100; i++)\r
+                       {\r
+                               imagePool[i] = new System.Windows.Controls.Image();\r
+                       }\r
+                        * */\r
+\r
+                       masterPool = new System.Windows.Controls.Canvas();\r
+                       prevPool = new System.Windows.Controls.Canvas();\r
+                       api_canvas.Children.Add(masterPool);\r
+                       //api_canvas.Children.Remove(Internal.Main.widgetStack);\r
+\r
+                       AsyncInitBool = true;\r
                }\r
 \r
                #endregion\r
@@ -183,20 +283,42 @@ namespace Psychlops
                public override void clear(Color col)\r
                {\r
                        back_panel.fill = col;\r
+                       stackN = 0;\r
                        rect(back_panel);\r
                }\r
 \r
                int nextIntervalFrame = 1, chacked = 0;\r
+               public void flip(int n)\r
+               {\r
+                       flip();\r
+               }\r
                public void flip()\r
                {\r
-                       //api_canvas.Dispatcher.BeginInvoke(flipexec);\r
-                       //flipexec();\r
-                       //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);\r
                        lock (this)\r
                        {\r
                                nextIntervalFrame = 1;\r
                                chacked = 1;\r
                        }\r
+                       //                      pointPoolN = 0;\r
+                       //                      brushPoolN = 0;\r
+                       linePoolN = 0;\r
+                       rectPoolN = 0;\r
+                       lineStackN = 0;\r
+                       rectStackN = 0;\r
+                       polygonStackN = 0;\r
+                       ellipseStackN = 0;\r
+                       lettersStackN = 0;\r
+                       imageStackN = 0;\r
+\r
+                       pointPoolN = 0;\r
+                       brushPoolN = 0;\r
+                       linePoolN = 0;\r
+                       rectPoolN = 0;\r
+                       ellipsePoolN = 0;\r
+                       polygonPoolN = 0;\r
+                       lettersPoolN = 0;\r
+                       imagePoolN = 0;\r
+\r
                        Internal.Main.canvas_flag.WaitOne();\r
                }\r
                public void executeFlip()\r
@@ -207,20 +329,17 @@ namespace Psychlops
                        }\r
                        if (nextIntervalFrame <= 0)\r
                        {\r
-                               //Psychlops.AppState.statusBar = chacked.ToString();\r
                                if (chacked > 0)\r
                                {\r
-                                       var tmp = new System.Windows.Controls.Canvas();\r
-                                       if (stack.Count > 0)\r
+                                       masterPool.Children.Clear();\r
+                                       if (stackN > 0)\r
                                        {\r
-                                               foreach (Internal.PrimitiveFigure f in stack)\r
+                                               for (int i = 0; i < stackN - 2; i++)\r
                                                {\r
-                                                       tmp.Children.Add(f.toNative());\r
+                                                       masterPool.Children.Add(stack[i].poolNative(this));\r
                                                }\r
+                                               stackN = 0;\r
                                        }\r
-                                       api_canvas.Children.Clear();\r
-                                       api_canvas.Children.Add(tmp);\r
-                                       stack.Clear();\r
                                        lock (this)\r
                                        {\r
                                                chacked = 0;\r
@@ -231,6 +350,8 @@ namespace Psychlops
                        System.Threading.Thread.Sleep(0);\r
                }\r
 \r
+\r
+\r
                #region Properties\r
 \r
                public double width { get { return width_; } }\r
@@ -304,6 +425,35 @@ namespace Psychlops
                        return tmp;\r
                }\r
                public UIElement toNative() { return this; }\r
+\r
+               public void copyToStack(Templates.StackableDrawable d)\r
+               {\r
+                       var tmp = d.lineStack[d.lineStackN];\r
+                       tmp.begin.x = begin.x;\r
+                       tmp.begin.y = begin.y;\r
+                       tmp.end.x = end.x;\r
+                       tmp.end.y = end.y;\r
+                       tmp.fill = fill;\r
+                       tmp.stroke = stroke;\r
+                       d.stack[d.stackN] = tmp;\r
+                       d.lineStackN++;\r
+                       d.stackN++;\r
+               }\r
+               public UIElement poolNative(Canvas d)\r
+               {\r
+                       var tmp = d.linePool[d.linePoolN];\r
+                       tmp.X1 = begin.x;\r
+                       tmp.Y1 = begin.y;\r
+                       tmp.X2 = end.x;\r
+                       tmp.Y2 = end.y;\r
+                       if (stroke.thick == 0.0) tmp.Stroke = fill;\r
+                       else stroke.apply(tmp);\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, top);\r
+                       tmp.Visibility = Visibility.Visible;\r
+                       d.linePoolN++;\r
+                       return tmp;\r
+               }\r
        }\r
 \r
        partial class Rectangle\r
@@ -328,7 +478,30 @@ namespace Psychlops
                        System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
                        return tmp;\r
                }\r
+\r
                public UIElement toNative() { return this; }\r
+               public void copyToStack(Templates.StackableDrawable d)\r
+               {\r
+                       var tmp = d.rectStack[d.rectStackN];\r
+                       tmp.v1 = v1;\r
+                       tmp.v2 = v2;\r
+                       tmp.fill = fill;\r
+                       d.stack[d.stackN] = tmp;\r
+                       d.rectStackN++;\r
+                       d.stackN++;\r
+               }\r
+               public UIElement poolNative(Canvas d)\r
+               {\r
+                       var tmp = d.rectPool[d.rectPoolN];\r
+                       tmp.Width = width;\r
+                       tmp.Height = height;\r
+                       tmp.Fill = fill;\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
+                       System.Windows.Controls.Canvas.SetTop(tmp, top);\r
+                       tmp.Visibility = Visibility.Visible;\r
+                       d.rectPoolN++;\r
+                       return tmp;\r
+               }\r
        }\r
 \r
        partial class Ellipse\r
@@ -349,7 +522,32 @@ namespace Psychlops
                        System.Windows.Controls.Canvas.SetTop(tmp, d.top);\r
                        return tmp;\r
                }\r
+\r
                public UIElement toNative() { return this; }\r
+\r
+               public void copyToStack(Templates.StackableDrawable d)\r
+               {\r
+                       var tmp = d.ellipseStack[d.ellipseStackN];\r
+                       tmp.datum = datum;\r
+                       tmp.xdiameter = xdiameter;\r
+                       tmp.ydiameter = ydiameter;\r
+                       tmp.fill = fill;\r
+                       d.stack[d.stackN] = tmp;\r
+                       d.ellipseStackN++;\r
+                       d.stackN++;\r
+               }\r
+               public UIElement poolNative(Canvas d)\r
+               {\r
+                       var tmp = d.ellipsePool[d.ellipsePoolN];\r
+                       tmp.Width = width;\r
+                       tmp.Height = height;\r
+                       tmp.Fill = fill;\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, left);\r
+                       System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], top);\r
+                       tmp.Visibility = Visibility.Visible;\r
+                       d.ellipsePoolN++;\r
+                       return tmp;\r
+               }\r
        }\r
 \r
        partial class Polygon\r
@@ -375,6 +573,37 @@ namespace Psychlops
                        return tmp;\r
                }\r
                public UIElement toNative() { return this; }\r
+\r
+               public void copyToStack(Templates.StackableDrawable d)\r
+               {\r
+                       var tmp = d.polygonStack[d.polygonStackN];\r
+                       tmp.datum = datum;\r
+                       tmp.vertices.Clear();\r
+                       foreach (var v in vertices)\r
+                       {\r
+                               tmp.vertices.Add(v);\r
+                       }\r
+                       tmp.fill = fill;\r
+                       d.stack[d.stackN] = tmp;\r
+                       d.polygonStackN++;\r
+                       d.stackN++;\r
+               }\r
+               public UIElement poolNative(Canvas d)\r
+               {\r
+                       var tmp = d.polygonPool[d.polygonPoolN];\r
+                       tmp.Fill = fill;\r
+                       tmp.Points.Clear();\r
+                       foreach (var v in vertices)\r
+                       {\r
+                               tmp.Points.Add(v);\r
+                       }\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], datum.y);\r
+                       tmp.Visibility = Visibility.Visible;\r
+                       d.polygonPoolN++;\r
+                       return tmp;\r
+               }\r
+\r
        }\r
        \r
        partial class Letters\r
@@ -431,6 +660,35 @@ namespace Psychlops
                        return tmp;\r
                }\r
                public UIElement toNative() { return this; }\r
+\r
+               public void copyToStack(Templates.StackableDrawable d)\r
+               {\r
+                       var tmp = d.lettersStack[d.lettersStackN];\r
+                       tmp.str = str;\r
+                       tmp.datum = datum;\r
+                       tmp.fill = fill;\r
+                       d.stack[d.stackN] = tmp;\r
+                       d.lettersStackN++;\r
+                       d.stackN++;\r
+               }\r
+               public UIElement poolNative(Canvas d)\r
+               {\r
+                       var tmp = d.lettersPool[d.lettersPoolN];\r
+                       tmp.Text = str;\r
+                       tmp.Width = 500;\r
+                       tmp.Height = 500;\r
+                       tmp.FontSize = font.size;\r
+                       //tmp.FontFamily = ,\r
+                       tmp.FontStyle = FONT_STYLE_BRIDGE[font.style];\r
+                       tmp.FontWeight = FONT_WEIGHT_BRIDGE[font.weight];\r
+                       tmp.TextAlignment = LETTERS_H_ALIGN_BRIDGE[align];\r
+                       tmp.Foreground = fill;\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], datum.y);\r
+                       tmp.Visibility = Visibility.Visible;\r
+                       d.lettersPoolN++;\r
+                       return tmp;\r
+               }\r
        }\r
 \r
        partial class Image\r
@@ -443,6 +701,7 @@ namespace Psychlops
                internal void create__(int wid, int hei)\r
                {\r
                        buffer = new WriteableBitmap(wid, hei);\r
+                       AsyncBool = true;\r
                }\r
                delegate void FieldFunc1(System.Func<int, int, System.Windows.Media.Color> func);\r
                delegate void FieldFunc2(System.Func<int, int, System.Windows.Media.Color, System.Windows.Media.Color> func);\r
@@ -478,6 +737,29 @@ namespace Psychlops
                        return tmp;\r
                }\r
                public UIElement toNative() { return this; }\r
+\r
+               public void copyToStack(Templates.StackableDrawable d)\r
+               {\r
+                       var tmp = d.imageStack[d.imageStackN];\r
+                       tmp.datum = datum;\r
+                       tmp.buffer = buffer;\r
+                       tmp.self_rect = self_rect;\r
+                       d.stack[d.stackN] = tmp;\r
+                       d.imageStackN++;\r
+                       d.stackN++;\r
+               }\r
+               public UIElement poolNative(Canvas d)\r
+               {\r
+                       var tmp = d.imagePool[d.imagePoolN];\r
+                       tmp.Source = buffer;\r
+                       System.Windows.Controls.Canvas.SetLeft(tmp, datum.x);\r
+                       System.Windows.Controls.Canvas.SetTop(d.rectPool[d.rectPoolN], datum.y);\r
+                       tmp.Visibility = Visibility.Visible;\r
+                       d.rectPoolN++;\r
+                       throw new Exception("Image.poolNative");\r
+                       return this;\r
+               }\r
+\r
        }\r
 \r
        #endregion\r