From 779e0a2cb8586b575272d8921cf51b5312b01793 Mon Sep 17 00:00:00 2001 From: Xavi Artigas Date: Mon, 23 Jul 2018 13:16:54 -0300 Subject: [PATCH] tutorials: Fix C# Life tutorial Summary: Fix API changes, plus several logic and syntax errors... again, this had never been built. Test Plan: Didn't build before, builds and runs now, although with some runtime issues that will be tracked on a separate issue. Reviewers: felipealmeida, lauromoura, bu5hm4n Differential Revision: https://phab.enlightenment.org/D6649 --- apps/csharp/life/src/life_board.cs | 23 +++++++-------- apps/csharp/life/src/life_main.cs | 45 ++++++++++++++--------------- apps/csharp/life/src/life_render.cs | 26 ++++++++--------- 3 files changed, 45 insertions(+), 49 deletions(-) diff --git a/apps/csharp/life/src/life_board.cs b/apps/csharp/life/src/life_board.cs index ca121156..925a59dc 100644 --- a/apps/csharp/life/src/life_board.cs +++ b/apps/csharp/life/src/life_board.cs @@ -17,9 +17,9 @@ public class LifeBoard private int gen = 0; - private bool[] board, boardPrev; + private bool[] board; private bool[] board1, board2; - private efl.loop.Timer lifeTimer = null; + private efl.ILoop_Timer lifeTimer = null; private LifeRender lifeRender = null; private void CellOn(int x, int y) @@ -56,16 +56,15 @@ public class LifeBoard BoardSetup(); board = board1; - boardPrev = board2; } - public void Run(efl.ui.Win win) + public void Run(efl.ui.IWin win) { - lifeTimer = new efl.loop.Timer(null, (efl.loop.Timer etimer) => { + lifeTimer = new efl.Loop_Timer(win, (efl.ILoop_Timer etimer) => { etimer.SetInterval(0.1); }); - lifeTimer.TICK += (object sender, EventArgs ev) => { + lifeTimer.TickEvt += (object sender, EventArgs ev) => { Nextgen(); if (this.lifeRender != null) this.lifeRender.Refresh(win); @@ -109,7 +108,7 @@ public class LifeBoard public void Nextgen() { - int[] work = null; + bool[] work = null; ++gen; if (board == board1) @@ -117,8 +116,8 @@ public class LifeBoard else work = board1; - for (int y = 0; y < LIFE_BOARD_HEIGHT; y++) - for (int x = 0; x < LIFE_BOARD_WIDTH; x++) + for (int y = 0; y < Height; y++) + for (int x = 0; x < Width; x++) { int i = IndexForPosition(x, y); @@ -139,14 +138,14 @@ public class LifeBoard } } - boardPrev = board; board = work; } - public void TogglePause(efl.ui.Win win) + public void TogglePause(efl.ui.IWin win) { - if (lifeTimer) + if (lifeTimer != null) { + lifeTimer.SetParent(null); lifeTimer.Dispose(); lifeTimer = null; } diff --git a/apps/csharp/life/src/life_main.cs b/apps/csharp/life/src/life_main.cs index 9b9ce357..d0feb6cd 100644 --- a/apps/csharp/life/src/life_main.cs +++ b/apps/csharp/life/src/life_main.cs @@ -5,63 +5,62 @@ public class LifeWindow private LifeBoard lifeBoard; private LifeRender lifeRender; - void ResizeEvt(efl.ui.Win win, EventArgs ev) + void ResizeEvt(object sender, EventArgs ev) { - lifeRender.Layout(win); + lifeRender.RenderLayout((efl.ui.IWin)sender); } - void QuitEvt(efl.ui.Win win, EventArgs ev) + void QuitEvt(object sender, EventArgs ev) { // quit the mainloop efl.ui.Config.Exit(); } - void TouchEvt(efl.ui.Win win, efl.input.Pointer ev) + void TouchEvt(object sender, efl.input.Interface.PointerDownEvt_Args ev) { int cellx, celly; - var position = ev.GetPosition(); + efl.ui.IWin win = (efl.ui.IWin)sender; + var position = ev.arg.GetPosition(); lifeRender.CellForCoords(win, position, out cellx, out celly); - int i = LifeRender.IndexForPosition(cellx, celly); + int i = LifeBoard.IndexForPosition(cellx, celly); lifeBoard.Cells[i] = !lifeBoard.Cells[i]; lifeRender.RenderCell(win, cellx, celly); } - void KeyDownEvt(efl.ui.Win win, efl.input.Key ev) + void KeyDownEvt(object sender, efl.input.Interface.KeyDownEvt_Args ev) { - if (ev.GetKey() == "space") + efl.ui.IWin win = (efl.ui.IWin)sender; + if (ev.arg.GetKey() == "space") lifeBoard.TogglePause(win); } - public void Run() - { - lifeBoard.Run(win); - } - public LifeWindow() { - efl.ui.Win win = new efl.Ui.WinConcrete(null, (efl.ui.Win ewin) => { - ewin.SetWinType(efl.ui.win.Type.Basic); + efl.ui.IWin win = new efl.ui.Win(null, (efl.ui.IWin ewin) => { + ewin.SetWinType(efl.ui.Win_Type.Basic); ewin.SetText("EFL Life"); ewin.SetAutohide(true); }); // when the user clicks "close" on a window there is a request to hide - win.HIDE += QuitEvt; + win.HideEvt += QuitEvt; eina.Size2D sz; - sz.W = 10 * LifeBoard.Width * win.GetScale(); - sz.H = 10 * LifeBoard.Height * win.GetScale(); + 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); - win.RESIZE += ResizeEvt; - win.POINTER_DOWN += TouchEvt; - win.KEY_DOWN += KeyDownEvt; + win.ResizeEvt += ResizeEvt; + win.PointerDownEvt += TouchEvt; + win.KeyDownEvt += KeyDownEvt; win.SetSize(sz); + + lifeBoard.Run(win); } } @@ -71,9 +70,7 @@ public class Example { efl.All.Init(efl.Components.Ui); - var lifeWin = new LifeWin(); - - lifeWin.Run(); + var lifeWin = new LifeWindow(); // start the mainloop efl.ui.Config.Run(); diff --git a/apps/csharp/life/src/life_render.cs b/apps/csharp/life/src/life_render.cs index 0b5348ae..0db4f172 100644 --- a/apps/csharp/life/src/life_render.cs +++ b/apps/csharp/life/src/life_render.cs @@ -2,10 +2,10 @@ using System; public class LifeRender { - private efl.canvas.Rectangle[] lifeCells; + private efl.canvas.IRectangle[] lifeCells; private LifeBoard lifeBoard; - public LifeRender(efl.ui.Win win, LifeBoard board) + public LifeRender(efl.ui.IWin win, LifeBoard board) { lifeBoard = board; lifeBoard.SetRender(this); @@ -14,20 +14,20 @@ public class LifeRender for (int y = 0; y < LifeBoard.Height; ++y) for (int x = 0; x < LifeBoard.Width; ++x) - lifeCells[LifeBoard.IndexForPosition(x, y)] = new efl.canvas.RectangleConcrete(win); + lifeCells[LifeBoard.IndexForPosition(x, y)] = new efl.canvas.Rectangle(win); RenderLayout(win); } - public void CellForCoords(efl.ui.Win win, eina.Position2D coord, out int x, out int y) + public void CellForCoords(efl.ui.IWin win, eina.Position2D coord, out int x, out int y) { eina.Size2D size = win.GetSize(); - x = coord.X / ((double) size.W / LifeBoard.Width); - y = coord.Y / ((double) size.H / LifeBoard.Height); + x = coord.X * LifeBoard.Width / size.W; + y = coord.Y * LifeBoard.Height / size.H; } - public void RenderLayout(efl.ui.Win win) + public void RenderLayout(efl.ui.IWin win) { eina.Size2D size = win.GetSize(); double cw = (double) size.W / LifeBoard.Width; @@ -40,18 +40,18 @@ public class LifeRender // the little +1 here will avoid tearing as we layout non-multiple sizes eina.Size2D sz; - sz.W = cw + 1; - sz.H = ch + 1; + sz.W = (int)(cw + 1); + sz.H = (int)(ch + 1); rect.SetSize(sz); eina.Position2D pos; - pos.X = x * cw; - pos.Y = y * ch; + pos.X = (int)(x * cw); + pos.Y = (int)(y * ch); rect.SetPosition(pos); } } - public void RenderCell(efl.ui.Win win, int x, int y) + public void RenderCell(efl.ui.IWin win, int x, int y) { int i = LifeBoard.IndexForPosition(x, y); var rect = lifeCells[i]; @@ -62,7 +62,7 @@ public class LifeRender rect.SetColor(255, 255, 255, 255); } - public void Refresh(efl.ui.Win win) + public void Refresh(efl.ui.IWin win) { for (int y = 0; y < LifeBoard.Height; ++y) for (int x = 0; x < LifeBoard.Width; ++x)