summaryrefslogtreecommitdiff
path: root/apps/csharp/life/src/life_main.cs
blob: 11436a3c9e180b095962627de3cbb113fedb6a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
using System;

public class LifeWindow : Efl.Csharp.Application
{
    private LifeBoard lifeBoard;
    private LifeRender lifeRender;
    private Efl.Ui.Win win;

    void ResizeEvt(object sender, EventArgs ev)
    {
        lifeRender.RenderLayout((Efl.Ui.Win)sender);
    }

    void QuitEvt(object sender, Efl.Gfx.IEntityVisibilityChangedEvt_Args ev)
    {
        // quit the mainloop
        if (ev.arg == false)
            Efl.App.AppMain.Quit(0);
    }

    void TouchEvt(object sender, Efl.Input.IInterfacePointerDownEvt_Args ev)
    {
        int cellx, celly;
        var position = ev.arg.GetPosition();
        lifeRender.CellForCoords(win, position, out cellx, out celly);

        int i = LifeBoard.IndexForPosition(cellx, celly);
        lifeBoard.Cells[i] = !lifeBoard.Cells[i];
        lifeRender.RenderCell(win, cellx, celly);
    }

    void KeyDownEvt(object sender, Efl.Input.IInterfaceKeyDownEvt_Args ev)
    {
        if (ev.arg.GetKey() == "space")
            lifeBoard.TogglePause(win);
    }

    protected override void OnInitialize(string[] args)
    {
        win = new Efl.Ui.Win(parent: null, winName: "Life", winType: Efl.Ui.WinType.Basic);
        win.SetText("EFL Life");
        win.SetAutohide(true);

        // when the user clicks "close" on a window there is a request to hide
        ((Efl.Gfx.IEntity)win).VisibilityChangedEvt += QuitEvt;

        Eina.Size2D sz;
        sz.W = (int)(10 * LifeBoard.Width * win.GetScale());
        sz.H = (int)(10 * LifeBoard.Height * win.GetScale());

        lifeBoard = new LifeBoard();
        lifeRender = new LifeRender(win, lifeBoard);
        lifeRender.Refresh(win);

        ((Efl.Gfx.IEntity)win).SizeChangedEvt += ResizeEvt;
        ((Efl.Input.IInterface)win).PointerDownEvt += TouchEvt;
        ((Efl.Input.IInterface)win).KeyDownEvt += KeyDownEvt;

        win.SetSize(sz);

        lifeBoard.Run(win);
    }

    protected override void OnPause() {
        if (win != null) {
            lifeBoard.TogglePause(win);
        }
    }

    protected override void OnResume() {
        if (win != null) {
            lifeBoard.TogglePause(win);
        }
    }

    protected override void OnTerminate() {
        Console.WriteLine("Goodbye.");
    }
}

public class Example
{
    public static void Main()
    {
        var lifeWin = new LifeWindow();

        lifeWin.Launch();
    }
}