Beispiel #1
0
    static void Main()
    {
        Application.Init();
        var top = Application.Top;

        // Creates the top-level window to show
        var win = new Window(new Rect(0, 1, top.Frame.Width, top.Frame.Height - 1), "Conway's Game of Life");

        top.Add(win);

        var gameRect = CalcGameRect(top.Frame);

        var editBtn        = new Button(gameRect.X, gameRect.Bottom + 3, "Edit");
        var moveBtn        = new Button(editBtn.Frame.Right + 3, gameRect.Bottom + 3, "Move");
        var startBtn       = new Button(moveBtn.Frame.Right + 3, gameRect.Bottom + 3, "Start");
        var changeSpeedBtn = new Button(startBtn.Frame.Right + 3, gameRect.Bottom + 3, "Change Speed");

        win.Add(
            editBtn,
            moveBtn,
            startBtn,
            changeSpeedBtn
            );

        var frame     = new FrameView(gameRect, "Life");
        var BoardView = new BoardView(0, 0, gameRect.Width - 2, gameRect.Height - 2);

        var statsView = new StatView(new Rect(
                                         top.Frame.Width - 30,
                                         top.Frame.Top + 2,
                                         28,
                                         top.Frame.Height - 12),
                                     BoardView
                                     );

        BoardView.Updated += statsView.UpdateStats;

        frame.Add(BoardView);
        win.Add(statsView);
        win.Add(frame);

        // Creates a menubar
        var menu = new MenuBar(new MenuBarItem [] {
            new MenuBarItem("_File", new MenuItem [] {
                new MenuItem("_New", "", NewGame),
                new MenuItem("_Open", "", OpenGame),
                new MenuItem("_Save", "", SaveGame),
                new MenuItem("_Export", "", Export),
                new MenuItem("_Close", "", CloseGame),
                new MenuItem("_Quit", "", () => { if (Quit())
                                                  {
                                                      top.Running = false;
                                                  }
                             })
            }),
            new MenuBarItem("_Insert", new MenuItem [] {
                new MenuItem("Still Life", "", Insert(win, BoardView, Entities.StillLife)),
                new MenuItem("Oscillator", "", Insert(win, BoardView, Entities.Oscillator)),
                new MenuItem("Glider", "", Insert(win, BoardView, Entities.Glider)),
                new MenuItem("Glider Gun", "", Insert(win, BoardView, Entities.GliderGun)),
                new MenuItem("Puffer Train", "", Insert(win, BoardView, Entities.PufferTrain)),
                new MenuItem("R-Pentomino", "", Insert(win, BoardView, Entities.RPentomino))
            }),
            new MenuBarItem("_Help", new MenuItem [] {
                new MenuItem("_About", "", HelpAbout)
            })
        });

        top.Add(menu);


        editBtn.Clicked += () => BoardView.BeginEditMode(win);
        moveBtn.Clicked += () => BoardView.BeginMoveMode(win);

        var started = false;

        startBtn.Clicked += () =>
        {
            started = !started;
            if (started)
            {
                startBtn.Text = "Stop";
                DisableTheMouse();
            }
            else
            {
                startBtn.Text = "Start";
                EnableTheMouse();
            }
        };

        var stateTimer = new Timer();

        stateTimer.AutoReset = false;
        stateTimer.Elapsed  += (sender, e) =>
        {
            if (started)
            {
                BoardView.Update();
                Application.Refresh();
            }
            stateTimer.Start();
        };
        stateTimer.Interval = 50;
        stateTimer.Start();
        Application.Run();
    }