Example #1
0
        /// <summary>Change the size of this context. This can potentially trigger a redraw.</summary>
        public void SetSize(int width, int height)
        {
            if (FilterInvalid)
            {
                // Build the filter now:
                FilterInvalid = false;
                BuildFilter();
            }

            // Resize (internally checks if a resize is actually required, and also may create the texture):
            if (DrawInfo.SetSize(width, height))
            {
                // Redraw:
                Draw();
            }
        }
        /// <summary>Connects a filter to the camera's output.
        /// Fires an imagechange event too as this updates Texture to the filtered version.</summary>
        private void ConnectFilter()
        {
            if (Filter_ != null)
            {
                if (FilterDrawInfo == null)
                {
                    FilterDrawInfo = new Loonim.DrawInfo();
                }

                // Update draw info:
                FilterDrawInfo.SetSize(pixelWidth, pixelHeight);

                // Update source:
                Filter_.Set("source0", SourceCamera.targetTexture);

                // Reallocate the filter:
                Filter_.PreAllocate(FilterDrawInfo);

                // Grab the main output (always a RT):
                Texture = Filter_.Texture as RenderTexture;

                if (Texture == null)
                {
                    // This isn't a valid filter!
                    // It either had no nodes or e.g. e.g. a solid colour.
                    Debug.Log("Invalid filter was set to a FlatWorldUI - removed it.");
                    Filter_ = null;
                }
            }

            if (Filter_ == null)
            {
                // Clear draw info:
                FilterDrawInfo = null;

                // Revert to the camera's output:
                Texture = SourceCamera.targetTexture;
            }

            // Fire an imagechange event into the window:
            Dom.Event e = new Dom.Event("imagechange");
            e.SetTrusted(false);
            document.window.dispatchEvent(e);
        }
        /// <summary>Updates the FlatWorldUI so it builds the mesh for this element.</summary>
        private void UpdateRenderer(LayoutBox box, float width, float height)
        {
            // - Set w/h to width and height:
            int w = (int)width;
            int h = (int)height;

            if (Renderer.SetDimensions(w, h))
            {
                // Output texture changed.

                if (Filter == null)
                {
                    // Update output:
                    Output = Renderer.Texture;

                    if (Material != null)
                    {
                        // Hook up the output:
                        Material.SetTexture("_MainTex", Output);
                    }
                }
                else
                {
                    Filter.Set("source0", Renderer.Texture);
                    // Output will be updated shortly.

                    // Always mark as changed:
                    Filter.Changed = true;
                }
            }

            // Redraw:
            if (Filter != null)
            {
                if (FilterDrawInfo != null)
                {
                    FilterDrawInfo.SetSize(w, h);
                }

                // Draw now:
                Output = Filter.Draw(FilterDrawInfo);

                if (Material != null)
                {
                    // Hook up the output:
                    Material.SetTexture("_MainTex", Output);
                }
            }

            // Temporarily set the positioning of box such that it's at the origin:
            float _x   = box.X;
            float _y   = box.Y;
            float _pX  = box.ParentOffsetLeft;
            float _pY  = box.ParentOffsetTop;
            int   _pos = box.PositionMode;

            // Clear:
            box.X = -box.Border.Left;
            box.Y = -box.Border.Top;
            box.ParentOffsetTop  = box.X;
            box.ParentOffsetLeft = box.Y;
            box.PositionMode     = PositionMode.Fixed;

            // Put the RenderData in the render only queue of *Renderer* and ask it to layout now:
            RenderableData _next = RenderData.Next;
            UpdateMode     _mode = RenderData.NextUpdateMode;

            // Clear:
            RenderData.Next           = null;
            RenderData.NextUpdateMode = UpdateMode.Render;

            // Queue:
            Renderer.Renderer.StylesToUpdate = RenderData;

            // Draw now!
            Renderer.Renderer.Update();

            // Restore (box):
            box.X = _x;
            box.Y = _y;
            box.ParentOffsetTop  = _pX;
            box.ParentOffsetLeft = _pY;
            box.PositionMode     = _pos;

            // Restore (queue):
            RenderData.Next           = _next;
            RenderData.NextUpdateMode = _mode;
        }