summaryrefslogtreecommitdiff
path: root/apps/csharp/life/src/life_main.cs
blob: 07dd43abda5531f059b8db56977b846d3e8921ca (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
using System;

public class LifeWindow
{
    private LifeBoard lifeBoard;
    private LifeRender lifeRender;

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

    void QuitEvt(object sender, EventArgs ev)
    {
        // quit the mainloop
        Efl.Ui.Config.Exit();
    }

    void TouchEvt(object sender, Efl.Input.InterfacePointerDownEvt_Args ev)
    {
        int cellx, celly;
        Efl.Ui.Win win = (Efl.Ui.Win)sender;
        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.InterfaceKeyDownEvt_Args ev)
    {
        Efl.Ui.Win win = (Efl.Ui.Win)sender;
        if (ev.arg.GetKey() == "space")
            lifeBoard.TogglePause(win);
    }

    public LifeWindow()
    {
        Efl.Ui.Win 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.Entity)win).HideEvt += 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.Entity)win).ResizeEvt += ResizeEvt;
        ((Efl.Input.Interface)win).PointerDownEvt += TouchEvt;
        ((Efl.Input.Interface)win).KeyDownEvt += KeyDownEvt;

        win.SetSize(sz);

        lifeBoard.Run(win);
    }
}

public class Example
{
    public static void Main()
    {
        Efl.All.Init(Efl.Components.Ui);

        var lifeWin = new LifeWindow();

        // start the mainloop
        Efl.Ui.Config.Run();

        Efl.All.Shutdown();
    }
}