123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
-
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace ConFrames
- {
- public class Window
- {
- public Window(string title, Rect frameRectangle)
- {
- Title = title;
- FrameRectangle = frameRectangle;
- _clearAttributes = Attribute.Make(ConsoleColor.White, ConsoleColor.Blue);
- _needsFrameRender = true;
- _needsClientRender = true;
- }
- public int CursorX;
- public int CursorY;
- bool _needsFrameRender;
- bool _needsClientRender;
- bool _frameRenderedActive;
- Rect _frame;
- Desktop _manager;
- CharInfo[] _buf;
-
- public Point CursorPosition
- {
- get { return new Point(CursorX, CursorY); }
- set { CursorX = value.X; CursorY = value.Y; }
- }
-
- public bool CursorVisible
- {
- get;
- set;
- }
-
- ushort _clearAttributes;
- public ushort ClearAttributes
- {
- get
- {
- return _clearAttributes;
- }
- set
- {
- _clearAttributes = value;
- }
- }
-
- string _title;
- public string Title
- {
- get
- {
- return _title;
- }
- set
- {
- _title = value;
- _needsFrameRender = true;
- if (_manager != null)
- _manager.Invalidate(this);
- }
- }
-
- public Rect FrameRectangle
- {
- get
- {
- return _frame;
- }
- set
- {
- if (_frame == value)
- return;
-
- if (_frame.Size != value.Size)
- {
- _needsClientRender = true;
- _needsFrameRender = true;
- _buf = null;
- }
-
- _frame = value;
-
- if (_manager != null)
- {
- _manager.InvalidateDesktop();
- _manager.Invalidate(this);
- }
- }
- }
-
- public Size ClientSize
- {
- get
- {
- return new Size(FrameRectangle.Width - 2, FrameRectangle.Height - 2);
- }
- }
-
- public void Open(ConFrames.Desktop manager)
- {
- if (_manager != null)
- throw new InvalidOperationException();
- _manager = manager;
- _manager.AddWindow(this);
- }
-
- public void Close()
- {
- if (_manager != null)
- {
- _manager.RemoveWindow(this);
- _manager = null;
- }
- }
-
- public virtual void OnPaint(PaintContext ctx)
- {
- }
-
- public virtual bool OnKey(ConsoleKeyInfo key)
- {
- return false;
- }
-
- public void Invalidate()
- {
- _needsClientRender = true;
- if (_manager != null)
- _manager.Invalidate(this);
- }
-
- public bool IsActive
- {
- get
- {
- if (_manager == null)
- return false;
- return _manager.ActiveWindow == this;
- }
- }
-
- public void Activate()
- {
- if (_manager == null)
- return;
- _manager.ActiveWindow = this;
- }
-
- public PaintContext GetPaintContext()
- {
-
- if (_buf == null)
- {
- _buf = new CharInfo[FrameRectangle.Width * FrameRectangle.Height];
- }
- var ctx = new PaintContext(_buf, FrameRectangle.Size, new Rect(1, 1, FrameRectangle.Width - 2, FrameRectangle.Height - 2));
- ctx.Attributes = _clearAttributes;
- _manager.Invalidate(this);
- return ctx;
- }
-
- public virtual CharInfo[] Draw()
- {
-
- if (_buf == null)
- {
- _buf = new CharInfo[FrameRectangle.Width * FrameRectangle.Height];
- _needsFrameRender = true;
- _needsClientRender = true;
- }
-
- var px = new PaintContext(_buf, new Size(FrameRectangle.Width, FrameRectangle.Height), new Rect(0, 0, FrameRectangle.Width, FrameRectangle.Height));
-
- if (_needsFrameRender || _frameRenderedActive != IsActive)
- {
- _frameRenderedActive = IsActive;
-
- if (IsActive)
- {
- px.ForegroundColor = _manager.ActiveBorderLineColor;
- px.BackgroundColor = _manager.ActiveBorderBackgroundColor;
- }
- else
- {
- px.ForegroundColor = _manager.InactiveBorderLineColor;
- px.BackgroundColor = _manager.InactiveBorderBackgroundColor;
- }
- px.DrawBox(new Rect(0, 0, FrameRectangle.Width, FrameRectangle.Height), IsActive);
-
- if (!string.IsNullOrEmpty(Title))
- {
- var title = " " + Title + " ";
- int titleX = (FrameRectangle.Width - title.Length) / 2;
- if (titleX < 2)
- titleX = 2;
- for (int i = 0; i < title.Length && i < FrameRectangle.Width - 4; i++)
- {
- _buf[titleX + i].Char = title[i];
- }
- }
- }
-
- if (_needsClientRender)
- {
- var ctx = new PaintContext(_buf, FrameRectangle.Size, new Rect(1, 1, FrameRectangle.Width - 2, FrameRectangle.Height - 2));
- ctx.Attributes = _clearAttributes;
- ctx.Clear();
- OnPaint(ctx);
- }
- _needsClientRender = false;
- _needsFrameRender = false;
- return _buf;
- }
- }
- }
|