Esempio n. 1
0
        public UIWindow(Scene parentScene, Point contentSize, bool canBeClosed, bool canBeMaximized,
                        bool canbeMinimized, SpriteFrame icon, UIStyle style)
        {
            this.style = style;
            var headerSize = 32;
            var windowRoot = parentScene.AddActor("Window");

            new BoundingRect(windowRoot,
                             contentSize + new Point(0, headerSize) + new Point(this.margin * 2, this.margin * 2));

            var rootGroup = new LayoutGroup(windowRoot, Orientation.Vertical);

            rootGroup.SetMarginSize(new Point(this.margin, this.margin));
            rootGroup.AddHorizontallyStretchedElement("HeaderContent", 32, headerContentActor =>
            {
                new Hoverable(headerContentActor);
                new Draggable(headerContentActor).DragStart +=
                    (position, delta) => OnAnyPartOfWindowClicked(MouseButton.Left);
                new MoveOnDrag(headerContentActor, windowRoot.transform);

                var headerGroup = new LayoutGroup(headerContentActor, Orientation.Horizontal);
                if (icon != null)
                {
                    headerGroup.AddVerticallyStretchedElement("Icon", 32, iconActor =>
                    {
                        new SpriteRenderer(iconActor, icon.spriteSheet).SetAnimation(icon.animation);

                        iconActor.GetComponent <BoundingRect>().SetOffsetToCenter();
                    });
                    headerGroup.PixelSpacer(5); // Spacing between icon and title
                }

                headerGroup.AddBothStretchedElement("Title",
                                                    titleActor =>
                {
                    this.titleTextRenderer = new BoundedTextRenderer(titleActor, "Window title goes here",
                                                                     style.uiElementFont, Color.White, Alignment.CenterLeft,
                                                                     depthOffset: -2).EnableDropShadow(Color.Black);
                });

                if (canbeMinimized)
                {
                    CreateControlButton(headerGroup, windowRoot, win => Minimized?.Invoke(win),
                                        this.style.minimizeButtonFrames);
                }

                if (canBeMaximized)
                {
                    CreateControlButton(headerGroup, windowRoot, win => Maximized?.Invoke(win),
                                        this.style.maximizeButtonFrames);
                }

                if (canBeClosed)
                {
                    CreateControlButton(headerGroup, windowRoot, win => Closed?.Invoke(win),
                                        this.style.closeButtonFrames);
                }
            });

            // These variables with _local at the end of their name are later assigned to readonly values
            // The locals are assigned in lambdas which is illegal for readonly assignment
            SceneRenderer sceneRenderer_local = null;
            Actor         canvasActor_local   = null;
            LayoutGroup   contentGroup_local  = null;

            // "Content" means everything below the header

            rootGroup.AddBothStretchedElement("ContentGroup", contentActor =>
            {
                new NinepatchRenderer(contentActor, style.windowSheet, NinepatchSheet.GenerationDirection.Outer);

                contentGroup_local = new LayoutGroup(contentActor, Orientation.Horizontal)
                                     .AddBothStretchedElement("Canvas", viewActor =>
                {
                    canvasActor_local = viewActor;
                    new BoundedCanvas(viewActor);
                    new Hoverable(viewActor);
                    new Clickable(viewActor).ClickStarted += OnAnyPartOfWindowClicked;
                    sceneRenderer_local = new SceneRenderer(viewActor);
                })
                ;
            })
            ;

            this.sceneRenderer    = sceneRenderer_local;
            this.canvasActor      = canvasActor_local;
            this.rootTransform    = windowRoot.transform;
            this.rootBoundingRect = windowRoot.GetComponent <BoundingRect>();
            this.contentGroup     = contentGroup_local;
        }
Esempio n. 2
0
        protected override void OnGameLoad()
        {
            MachinaGame.Fullscreen = true;

            var bgScene  = SceneLayers.AddNewScene();
            var bgScene2 = SceneLayers.AddNewScene();
            var bgScene3 = SceneLayers.AddNewScene();


            var gameScene = SceneLayers.AddNewScene();
            var uiScene   = SceneLayers.AddNewScene();
            // SceneLayers.BackgroundColor = Color.Black;

            var bgActor = bgScene.AddActor("Background");

            new BackgroundRenderer(bgActor, gameScene.camera, 0.6f);

            var bgActor2 = bgScene2.AddActor("Background");

            new BackgroundRenderer(bgActor2, gameScene.camera, 1f);

            void StartGame()
            {
                var harness = gameScene.AddActor("Harness", new Vector2(gameScene.camera.ViewportCenter.X, -256));

                var levelIndex = 0;

                var levelTransition = new LevelTransition(harness, levelIndex);
                var player          = new Player(harness);

                new BubbleSpawner(harness, new MinMax <int>(3, 7));
                new Harness(harness);

                var eye         = harness.transform.AddActorAsChild("Eye");
                var eyeRenderer = new EyeRenderer(eye, player, levelTransition);

                var targetReticalActor = gameScene.AddActor("Redical");

                targetReticalActor.transform.Depth -= 20;
                new TargetRedical(targetReticalActor, player);

                {
                    var groupActor = uiScene.AddActor("UI Parent Group");
                    new BoundingRect(groupActor, uiScene.camera.ViewportWidth, uiScene.camera.ViewportHeight);
                    var grp = new LayoutGroup(groupActor, Orientation.Vertical);

                    grp.PixelSpacer(32, (int)(uiScene.camera.ViewportHeight * 9f / 12f));

                    grp.AddBothStretchedElement("TextGroupParent", act =>
                    {
                        var textGroup = new LayoutGroup(act, Orientation.Horizontal);
                        textGroup.HorizontallyStretchedSpacer();
                        textGroup.AddVerticallyStretchedElement("Text", 900, act =>
                        {
                            new BoundedTextRenderer(act, "", MachinaGame.Assets.GetSpriteFont("Roboto"), Color.White, HorizontalAlignment.Left, VerticalAlignment.Top, Overflow.Ignore).EnableDropShadow(Color.Black);
                            new TextCrawlRenderer(act, levelTransition);
                        });
                        textGroup.HorizontallyStretchedSpacer();
                    });

                    grp.PixelSpacer(32, 32);
                }
            }

            var world = gameScene.AddActor("World");

            new WorldStuff(world);

            var menu = gameScene.AddActor("Main Menu");

            new BoundingRect(menu, gameScene.camera.ViewportWidth, gameScene.camera.ViewportHeight);
            var menuLayout = new LayoutGroup(menu, Orientation.Horizontal);

            menuLayout.PixelSpacer(400);
            menuLayout.AddBothStretchedElement("Menu Inner", innerGroupActor =>
            {
                var innerGroup = new LayoutGroup(innerGroupActor, Orientation.Vertical);

                innerGroup.PixelSpacer(250);
                var titleActGroup = innerGroup.AddHorizontallyStretchedElement("Title", 100, titleAct =>
                {
                    new BoundedTextRenderer(titleAct, "Oculus Leviathan", MachinaGame.Assets.GetSpriteFont("Roboto-Big"));
                });

                innerGroup.AddHorizontallyStretchedElement("Subtitle", 64, subtitleAct =>
                {
                    // This was written hours before the deadline, it's spaghetti

                    new BoundedTextRenderer(subtitleAct, "By NotExplosive\n\nThis game is played entirely with the mouse\nPress F4 to toggle fullscreen\n\nMade for Ludum Dare 48 in 72 hours\nHOLD Left Mouse Button to begin", MachinaGame.Assets.GetSpriteFont("Roboto"));

                    float mouseHeldTimer = 0;
                    var mouseHeld        = false;
                    var totalTimer       = 0f;
                    var thresholdMet     = false;

                    var hoc = new AdHoc(subtitleAct);

                    hoc.onMouseButton += (MouseButton button, Vector2 pos, ButtonState state) =>
                    {
                        mouseHeld = button == MouseButton.Left && state == ButtonState.Pressed;
                    };

                    hoc.onUpdate += (float dt) =>
                    {
                        if (!thresholdMet)
                        {
                            if (mouseHeld)
                            {
                                mouseHeldTimer += dt;
                            }
                            else
                            {
                                if (mouseHeldTimer > 0)
                                {
                                    mouseHeldTimer -= dt;
                                }
                            }


                            gameScene.camera.Position = new Vector2(0, MathF.Sin(totalTimer) * 32);

                            if (mouseHeldTimer > 1f)
                            {
                                subtitleAct.RemoveComponent <BoundedTextRenderer>();
                                titleActGroup.actor.Destroy();
                                thresholdMet = true;
                            }
                        }
                        else
                        {
                            // if thresholdMet:
                            mouseHeldTimer -= dt;

                            if (mouseHeldTimer < 0)
                            {
                                menu.Destroy();
                                StartGame();
                            }
                        }

                        if (mouseHeldTimer > 0)
                        {
                            gameScene.camera.Zoom = 1 + EaseFuncs.CubicEaseOut(mouseHeldTimer) / 3;
                        }

                        totalTimer += dt;
                    };
                });
            });
            menuLayout.PixelSpacer(100);

            CommandLineArgs.RegisterFlagArg("edit", () =>
            {
                var curveBrush = gameScene.AddActor("CurveBrush", gameScene.camera.ViewportCenter);
                new CurveEditor(curveBrush);
            });

            if (DebugLevel >= DebugLevel.Passive)
            {
                var debug = gameScene.AddActor("Debug");
                new PanAndZoomCamera(debug, Keys.LeftControl);
            }
        }