diff --git a/TBO/UI/Shell.cs b/TBO/UI/Shell.cs index 138d8bd..1acbd23 100644 --- a/TBO/UI/Shell.cs +++ b/TBO/UI/Shell.cs @@ -1,4 +1,9 @@ -using System.Collections.Generic; +#region Options +//#define SHOW_UPDATES +#endregion + +using System; +using System.Collections.Generic; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Text; @@ -8,34 +13,57 @@ { static class Shell { + public const int STANDARD_DPI = 96; + private static Color bgColor = Color.FromArgb(255, 195, 0); private static Image bgImage = Properties.Resources.comicbg; private static Dictionary applications = new Dictionary(); private static Application current; private static Size size; + private static bool fullScreen = false; + private static PointF BaseZoom = new PointF(1, 1); + //private static Point MousePos = new Point(0, 0); + private static Control hoverControl; + private static Control leftDownControl; - public delegate void InvalidateDelegate(); + public delegate void BasicDelegate(); public delegate void InvalidateRegionDelegate(Rectangle r); - public static InvalidateDelegate Invalidate; - public static InvalidateRegionDelegate InvalidateRegion; + public delegate void SetFullScreenDelegate(bool fs); + public static BasicDelegate DoInvalidate; + public static InvalidateRegionDelegate DoInvalidateRegion; + public static SetFullScreenDelegate DoSetFullScreen; + public static BasicDelegate DoClose; + public static Random Random = new Random(); static Shell() { AddApp(new ApplicationLibrary()); } - private static void AddApp(Application app) + public static void Paint(Graphics g, Rectangle clip) { - applications[app.Id] = app; + // Clipping and background + g.SetClip(clip); +#if SHOW_UPDATES + Color rColor = Color.FromArgb(Random.Next(255), Random.Next(255), Random.Next(255)); + g.Clear(rColor); +#else + g.Clear(bgColor); + g.DrawImage(bgImage, 0, 0); +#endif + g.SmoothingMode = SmoothingMode.AntiAlias; + g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; + g.InterpolationMode = InterpolationMode.High; + + g.ScaleTransform(BaseZoom.X, BaseZoom.Y); + //g.DrawImage(Properties.Resources.tbo_logo ); + //g.DrawImageKeepRatioEx(Properties.Resources.tbo_logo, size.Width - 138, size.Height - 98, 128, 88, 0.3f); + + // Compose application + current?.Paint(g, UnZoom(clip)); } - private static void SetSize(Size s) - { - size = s; - current?.ResizeTo(s); - Invalidate(); - } - + #region Public API public static bool GoTo(string id) { Application app; @@ -43,32 +71,113 @@ { current = app; current.ResizeTo(size); - Invalidate(); + DoInvalidate(); return true; } return false; } - public static void Paint(Graphics g, Rectangle clip) + public static void Invalidate() { - // Clipping and background - g.SetClip(clip); - g.Clear(bgColor); - g.DrawImage(bgImage, 0, 0); - - g.SmoothingMode = SmoothingMode.AntiAlias; - g.TextRenderingHint = TextRenderingHint.AntiAliasGridFit; - g.InterpolationMode = InterpolationMode.High; - - // Compose application - current?.Paint(g, clip); + DoInvalidate(); } + public static void Invalidate(Rectangle r) + { + DoInvalidateRegion(Zoom(r)); + } + #endregion + + #region UI Notifications public static void NotifyMousePos(int x, int y) { - Control c = current?.Get(x, y, true); + Control c = current?.Get((int)(x / BaseZoom.X), (int)(y / BaseZoom.Y), true); + if (hoverControl != c) + { + hoverControl?.Handle(Event.AtMouseLeave()); + if (leftDownControl == hoverControl) + leftDownControl?.Handle(Event.AtMouseUp(Event.MouseButtonLeft)); + hoverControl = c; + hoverControl?.Handle(Event.AtMouseEnter()); + } } + public static void NotifyMouseButton(int button, bool down) + { + if (down) + { + hoverControl?.Handle(Event.AtMouseDown(button)); + if (button == Event.MouseButtonLeft) + leftDownControl = hoverControl; + } + else + { + hoverControl?.Handle(Event.AtMouseUp(button)); + if (button == Event.MouseButtonLeft && hoverControl == leftDownControl) + hoverControl?.Handle(Event.AtClick()); + } + } + #endregion + + #region Tools + private static void AddApp(Application app) + { + applications[app.Id] = app; + } + + private static Rectangle Zoom(Rectangle r) + { + if (BaseZoom.X != 1f || BaseZoom.Y != 1f) + { + r.X = (int)(r.X * BaseZoom.X); + r.Y = (int)(r.Y * BaseZoom.Y); + r.Width = (int)(r.Width * BaseZoom.X); + r.Height = (int)(r.Height * BaseZoom.Y); + } + return r; + } + + private static Rectangle UnZoom(Rectangle r) + { + if (BaseZoom.X != 1f || BaseZoom.Y != 1f) + { + r.X = (int)(r.X / BaseZoom.X); + r.Y = (int)(r.Y / BaseZoom.Y); + r.Width = (int)(r.Width / BaseZoom.X); + r.Height = (int)(r.Height / BaseZoom.Y); + } + return r; + } + #endregion + + #region GET/SET + private static void SetSize(Size s) + { + float w = s.Width, h = s.Height; + w /= BaseZoom.X; + h /= BaseZoom.Y; + size = new Size((int)w, (int)h); + current?.ResizeTo(size); + DoInvalidate(); + } + + private static void SetDPI(PointF p) + { + BaseZoom = new PointF(p.X / STANDARD_DPI, p.Y / STANDARD_DPI); + DoInvalidate(); + } + + private static void SetFullScreen(bool fs) + { + if (fs == fullScreen) + return; + fullScreen = fs; + DoSetFullScreen(fs); + } + #endregion + public static Size Size { get => size; set => SetSize(value); } + public static PointF DPI { get => new PointF(BaseZoom.X * STANDARD_DPI, BaseZoom.Y * STANDARD_DPI); set => SetDPI(value); } + public static bool FullScreen { get => fullScreen; set => SetFullScreen(value); } } } diff --git a/TBO/UI/Windows/Reader.Designer.cs b/TBO/UI/Windows/Reader.Designer.cs index 3db8a4c..6f05589 100644 --- a/TBO/UI/Windows/Reader.Designer.cs +++ b/TBO/UI/Windows/Reader.Designer.cs @@ -33,17 +33,20 @@ // // Reader // - this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); + this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None; - this.ClientSize = new System.Drawing.Size(800, 450); + this.ClientSize = new System.Drawing.Size(1200, 692); this.DoubleBuffered = true; this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); + this.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5); this.Name = "Reader"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "TBO"; this.Paint += new System.Windows.Forms.PaintEventHandler(this.Reader_Paint); + this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Reader_MouseDown); this.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Reader_MouseMove); + this.MouseUp += new System.Windows.Forms.MouseEventHandler(this.Reader_MouseUp); this.Resize += new System.EventHandler(this.Reader_Resize); this.ResumeLayout(false); diff --git a/TBO/UI/Windows/Reader.cs b/TBO/UI/Windows/Reader.cs index 0c4f9da..f8f4b4d 100644 --- a/TBO/UI/Windows/Reader.cs +++ b/TBO/UI/Windows/Reader.cs @@ -6,23 +6,29 @@ { public partial class Reader : Form { - //private TBOFile tbo; - private bool inFS = true; - //private bool inLibrary=true; - public Reader(string path) { + Shell.DoClose = ShellClose; + Shell.DoInvalidate = ShellInvalidate; + Shell.DoInvalidateRegion = ShellInvalidateRegion; + Shell.DoSetFullScreen = ShellSetFullScreen; InitializeComponent(); - Shell.Invalidate = ShellInvalidate; - Shell.InvalidateRegion = ShellInvalidateRegion; - SwapFullScreen(); + ShellSetFullScreen(false); if (path != null) LoadTBO(path); else ShowLibrary(); + using (Graphics graphics = this.CreateGraphics()) + Shell.DPI = new PointF(graphics.DpiX, graphics.DpiY); Shell.Size = ClientSize; } + #region Shell Events + private void ShellClose() + { + Close(); + } + private void ShellInvalidate() { Invalidate(); @@ -33,6 +39,30 @@ Invalidate(r); } + private void ShellSetFullScreen(bool inFS) + { + if (inFS) + { + SuspendLayout(); + FormBorderStyle = FormBorderStyle.None; + Rectangle scr = Screen.FromControl(this).Bounds; + Location = scr.Location; + ClientSize = scr.Size; + ResumeLayout(); + } + else + { + SuspendLayout(); + FormBorderStyle = FormBorderStyle.Sizable; + Rectangle scr = Screen.FromControl(this).WorkingArea; + int w = scr.Width * 2 / 3; + Location = new Point(scr.Left + ((scr.Width - w) / 2), scr.Top); + Size = new Size(w, scr.Height); + ResumeLayout(); + } + } + #endregion + private void ShowLibrary() { Shell.GoTo(ApplicationLibrary.ApplicationId); @@ -68,35 +98,6 @@ //} } - private void SwapFullScreen() - { - inFS = !inFS; - if (inFS) - { - SuspendLayout(); - FormBorderStyle = FormBorderStyle.None; - Rectangle scr = Screen.FromControl(this).Bounds; - Location = scr.Location; - ClientSize = scr.Size; - ResumeLayout(); - } - else - { - SuspendLayout(); - FormBorderStyle = FormBorderStyle.Sizable; - Rectangle scr = Screen.FromControl(this).WorkingArea; - int w = scr.Width * 2 / 3; - Location = new Point(scr.Left + ((scr.Width - w) / 2), scr.Top); - Size = new Size(w, scr.Height); - ResumeLayout(); - } - } - - private void pbFullScreen_Click(object sender, EventArgs e) - { - SwapFullScreen(); - } - private void Reader_Paint(object sender, PaintEventArgs e) { Shell.Paint(e.Graphics, e.ClipRectangle); @@ -111,5 +112,47 @@ { Shell.NotifyMousePos(e.X, e.Y); } + + private void Reader_MouseDown(object sender, MouseEventArgs e) + { + int bt; + switch (e.Button) + { + case MouseButtons.Left: + bt = tboTK.Event.MouseButtonLeft; + break; + case MouseButtons.Middle: + bt = tboTK.Event.MouseButtonMiddle; + break; + case MouseButtons.Right: + bt = tboTK.Event.MouseButtonRight; + break; + default: + bt = 0; + break; + } + Shell.NotifyMouseButton(bt, true); + } + + private void Reader_MouseUp(object sender, MouseEventArgs e) + { + int bt; + switch (e.Button) + { + case MouseButtons.Left: + bt = tboTK.Event.MouseButtonLeft; + break; + case MouseButtons.Middle: + bt = tboTK.Event.MouseButtonMiddle; + break; + case MouseButtons.Right: + bt = tboTK.Event.MouseButtonRight; + break; + default: + bt = 0; + break; + } + Shell.NotifyMouseButton(bt, false); + } } }