Exemplo n.º 1
0
        /// <summary>
        /// This is a javascript application.
        /// </summary>
        /// <param name="page">HTML document rendered by the web server which can now be enhanced.</param>
        public Application(IApp page = null)
        {
            // ! start big, then resize to small





            var ScreenWidth = Native.Screen.width;
            var ScreenHeight = Native.Screen.height;

            Console.WriteLine(
                new { ScreenWidth, ScreenHeight, Native.window.Width, Native.window.Height }
            );


            #region workaround for ThreeJS/chrome webgl upscale bug
            // workaround for not knowing how to tell three js to upscale correctly..
            // X:\jsc.svn\examples\javascript\Test\TestNestedIFrameForMoreWidth\TestNestedIFrameForMoreWidth\Application.cs
            // instead of reloading full app
            // could we run part of it instead?
            // like let jsc know that this sub application should be reloadable?
            // this will be like threading
            // the outer code wil just stop doing anything
            // and the inner app will take over.

            var ApplyWorkaround = false;
            var location = "";

            try
            {
                location = Native.Document.location.href;
                //var pl = Native.Window.parent.document.location;

                if (Native.window.Width < Native.screen.width)
                    ApplyWorkaround = true;
            }
            catch
            {

            }

            if (ApplyWorkaround)
            {
                #region make sure the url looks different to make iframe actually load
                Native.window.parent.With(
                    parent =>
                    {
                        // http://stackoverflow.com/questions/5934538/is-there-a-limitation-on-an-iframe-containing-another-iframe-with-the-same-url

                        var parentlocation = "";

                        try
                        {
                            parentlocation = parent.document.location.href;
                        }
                        catch
                        {
                            // parent from another origin
                        }

                        Console.WriteLine(new { parentlocation });

                        if (parentlocation.TakeUntilIfAny("#") == location.TakeUntilIfAny("#"))
                        {
                            var withouthash = location.TakeUntilIfAny("#");
                            var onlyhash = location.SkipUntilOrEmpty("#");

                            withouthash += "?";

                            if (onlyhash != "")
                            {
                                withouthash += "#" + onlyhash;
                            }

                            location = withouthash;
                        }
                    }
                );
                #endregion

                #region ApplyWorkaround

                // this check only looks for default screen width
                // what about height and secondary screens?
                Console.WriteLine("will prepare... " + location);

                var iframe = new IHTMLIFrame
                {
                    frameBorder = "0",
                    allowFullScreen = true
                };

                iframe.style.minWidth = Native.Screen.width + "px";
                iframe.style.minHeight = Native.Screen.height + "px";

                iframe.style.position = IStyle.PositionEnum.absolute;
                iframe.style.left = "0px";
                iframe.style.top = "0px";
                iframe.style.width = "100%";
                iframe.style.height = "100%";

                Native.Document.body.Clear();
                Native.Document.body.style.overflow = IStyle.OverflowEnum.hidden;

                Native.window.onmessage +=
                   e =>
                   {
                       Console.WriteLine("Native.Window.onmessage " + new { e.data });

                       // pure trickery :P
                       if ((string)e.data == "WoodsXmasByRobert.loaded")
                       {
                           iframe.style.minWidth = "";
                           iframe.style.minHeight = "";
                       }
                   };

                iframe.onload +=
                    delegate
                    {
                        if (iframe.src != location)
                            return;

                        Native.window.requestAnimationFrame +=
                          delegate
                          {
                              Console.WriteLine("reload done! " + new { location, iframe.src });
                              iframe.contentWindow.postMessage("ready yet?");
                          };
                    };

                Native.window.requestAnimationFrame +=
                    delegate
                    {
                        Console.WriteLine("will reload... " + location);
                        iframe.AttachToDocument();
                        iframe.src = location;
                    };
                #endregion


                return;
            }




            #endregion


            new AppReferences().With(
                References =>
                {

                    var source = new[]
                    {
                        References.Three,
                        //incompatible
                        // new THREELibrary.opensource.gihtub.three.js.build.three().Content,

                        References.Tween,
                        References.CopyShader,
                        References.FilmShader,
                        References.VignetteShader,
                        References.EffectComposer,
                        References.MaskPass,
                        References.RenderPass,
                        References.ShaderPass,
                        References.FilmPass,
                    };

                    var yield = source.ForEach(
                         (SourceScriptElement, i, MoveNext) =>
                         {
                             //Console.WriteLine("will load: " + SourceScriptElement.src);
                             SourceScriptElement.AttachToDocument().onload +=
                                 delegate
                                 {
                                     //Console.WriteLine("loaded: " + SourceScriptElement.src);
                                     MoveNext();
                                 };
                         }
                     );

                    yield(
                         delegate
                         {
                             //Console.WriteLine("will load WoodsXmasByRobert");

                             InitializeContent();
                         }
                     );

                }
            );

        }