Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.70 KB | None | 0 0
  1.  
  2. class Program
  3. {
  4.     static void Main(string[] args)
  5.     {
  6.         List<IUIElement> Buttons = new List<IUIElement>(new []{
  7.             new Button(0, 0, 5, 5),
  8.             new Button(19, 4, 7, 3),
  9.         });
  10.  
  11.         Buttons[0].OnClick += () => Console.WriteLine("На кнопку кликнули");
  12.  
  13.         while (true)
  14.         {
  15.             Render(Buttons);
  16.             CheckCollision(Input(), Buttons);
  17.         }
  18.     }
  19.  
  20.     static void CheckCollision(bool isClick, IEnumerable<IUIElement> elements)
  21.     {
  22.         foreach(var e in elements)
  23.         {
  24.             if(e.Contains(Console.CursorLeft, Console.CursorTop) && isClick)
  25.             {
  26.                 e.Click();
  27.             }
  28.         }
  29.     }
  30.  
  31.     static void Render(IEnumerable<IUIElement> elements)
  32.     {
  33.         int cx = Console.CursorLeft, cy = Console.CursorTop;
  34.  
  35.         foreach (var e in elements)
  36.         {
  37.             e.Render();
  38.         }
  39.  
  40.         Console.SetCursorPosition(cx, cy);
  41.     }
  42.  
  43.     static bool Input()
  44.     {
  45.         ConsoleKeyInfo k = Console.ReadKey(true);
  46.         switch (k.Key)
  47.         {
  48.             case ConsoleKey.LeftArrow:
  49.                 Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
  50.                 break;
  51.             case ConsoleKey.RightArrow:
  52.                 Console.SetCursorPosition(Console.CursorLeft + 1, Console.CursorTop);
  53.                 break;
  54.             case ConsoleKey.DownArrow:
  55.                 Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop + 1);
  56.                 break;
  57.             case ConsoleKey.UpArrow:
  58.                 Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop - 1);
  59.                 break;
  60.         }
  61.  
  62.         return k.Key == ConsoleKey.Enter;
  63.     }
  64. }
  65.  
  66.  
  67. public interface IUIElement
  68. {
  69.     event Action OnClick;
  70.     void Click();
  71.     void Render();
  72.     bool Contains(int x, int y);
  73. }
  74.  
  75. public class Button : IUIElement
  76. {
  77.     public int X, Y, Width, Height;
  78.     public event Action OnClick;
  79.  
  80.     public Button(int x, int y, int width, int height)
  81.     {
  82.         X = x;
  83.         Y = y;
  84.         Width = width;
  85.         Height = height;
  86.     }
  87.  
  88.     public void Render()
  89.     {
  90.      
  91.         Console.SetCursorPosition(X, Y);
  92.         for(int x = 0; x < Width; x++)
  93.         {
  94.             for(int y = 0; y < Height; y++)
  95.             {
  96.                 Console.SetCursorPosition(X + x, Y + y);
  97.                 Console.Write('#');
  98.             }
  99.         }
  100.     }
  101.  
  102.     public bool Contains(int x, int y)
  103.     {
  104.         return x > X && x < (X + Width) && y > Y && y < (Y + Height);
  105.     }
  106.  
  107.     public void Click()
  108.     {
  109.         if (OnClick != null)
  110.         {
  111.             OnClick();
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement