Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Program
- {
- static void Main(string[] args)
- {
- List<IUIElement> Buttons = new List<IUIElement>(new []{
- new Button(0, 0, 5, 5),
- new Button(19, 4, 7, 3),
- });
- Buttons[0].OnClick += () => Console.WriteLine("На кнопку кликнули");
- while (true)
- {
- Render(Buttons);
- CheckCollision(Input(), Buttons);
- }
- }
- static void CheckCollision(bool isClick, IEnumerable<IUIElement> elements)
- {
- foreach(var e in elements)
- {
- if(e.Contains(Console.CursorLeft, Console.CursorTop) && isClick)
- {
- e.Click();
- }
- }
- }
- static void Render(IEnumerable<IUIElement> elements)
- {
- int cx = Console.CursorLeft, cy = Console.CursorTop;
- foreach (var e in elements)
- {
- e.Render();
- }
- Console.SetCursorPosition(cx, cy);
- }
- static bool Input()
- {
- ConsoleKeyInfo k = Console.ReadKey(true);
- switch (k.Key)
- {
- case ConsoleKey.LeftArrow:
- Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
- break;
- case ConsoleKey.RightArrow:
- Console.SetCursorPosition(Console.CursorLeft + 1, Console.CursorTop);
- break;
- case ConsoleKey.DownArrow:
- Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop + 1);
- break;
- case ConsoleKey.UpArrow:
- Console.SetCursorPosition(Console.CursorLeft, Console.CursorTop - 1);
- break;
- }
- return k.Key == ConsoleKey.Enter;
- }
- }
- public interface IUIElement
- {
- event Action OnClick;
- void Click();
- void Render();
- bool Contains(int x, int y);
- }
- public class Button : IUIElement
- {
- public int X, Y, Width, Height;
- public event Action OnClick;
- public Button(int x, int y, int width, int height)
- {
- X = x;
- Y = y;
- Width = width;
- Height = height;
- }
- public void Render()
- {
- Console.SetCursorPosition(X, Y);
- for(int x = 0; x < Width; x++)
- {
- for(int y = 0; y < Height; y++)
- {
- Console.SetCursorPosition(X + x, Y + y);
- Console.Write('#');
- }
- }
- }
- public bool Contains(int x, int y)
- {
- return x > X && x < (X + Width) && y > Y && y < (Y + Height);
- }
- public void Click()
- {
- if (OnClick != null)
- {
- OnClick();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement