From 5f780d458275ac2099f31a39ac03b531efa669f4 Mon Sep 17 00:00:00 2001 From: L33T Date: Tue, 17 Feb 2015 13:00:16 +0200 Subject: [PATCH] Notification: Adding double-click to remove a notification, accurate update positions, text animation fix, text is now based on text width value, adding an option to draw border (default on), Font is by default now not bold. --- Notifications.cs | 10 ++ Notifications/Notification.cs | 196 +++++++++++++++++++++------------- 2 files changed, 133 insertions(+), 73 deletions(-) diff --git a/Notifications.cs b/Notifications.cs index 55192fb8..4e6e5b50 100644 --- a/Notifications.cs +++ b/Notifications.cs @@ -251,6 +251,16 @@ public static int GetLocation() return array[array.Count - 0x1] + 0x1E; } + public static int GetLocation(FileStream stream) + { + var i = stream.Name; + var length = i.IndexOf("Notifications\\", StringComparison.Ordinal) + "Notifications\\".Length; + var str = i.Substring(length, i.Length - length); + var @int = int.Parse(str.Substring(0x0, str.IndexOf('.'))); + + return @int; + } + /// /// Validates if current position is first in line /// diff --git a/Notifications/Notification.cs b/Notifications/Notification.cs index ddd359af..232f6aec 100644 --- a/Notifications/Notification.cs +++ b/Notifications/Notification.cs @@ -62,6 +62,7 @@ public Notification(string text, int duration = -0x1) // Setting main values Text = text; state = NotificationState.Idle; + border = true; // Preload Text Font.PreloadText(text); @@ -85,26 +86,20 @@ public bool Show(int newDuration = -0x1) return false; } - var yAxis = Notifications.GetLocation(); - if (yAxis != -0x1) + handler = Notifications.Reserve(GetId(), handler); + if (handler != null) { - handler = Notifications.Reserve(GetId(), handler); - if (handler != null) - { - duration = newDuration; - - TextColor.A = 0xFF; - BoxColor.A = 0xFF; - BorderColor.A = 0xFF; + duration = newDuration; - position = new Vector2(Drawing.Width - 200f, yAxis); + TextColor.A = 0xFF; + BoxColor.A = 0xFF; + BorderColor.A = 0xFF; - decreasementTick = GetNextDecreasementTick(); + position = new Vector2(Drawing.Width - 200f, Notifications.GetLocation(handler)); - draw = update = true; + decreasementTick = GetNextDecreasementTick(); - return true; - } + return draw = update = true; } return false; @@ -123,6 +118,23 @@ public void Flash(int interval = 0xFA) } } + /// + /// Toggles the notification border + /// + public void Border() + { + border = !border; + } + + /// + /// Sets the notification border toggle value + /// + /// bool value + public void Border(bool value) + { + border = value; + } + /// /// Calculate the next decreasement tick. /// @@ -175,7 +187,7 @@ private static Vector2[] GetBorder(float x, float y, float w, float h) /// Notification's Font /// public Font Font = new Font( - Drawing.Direct3DDevice, 0xE, 0x0, FontWeight.Bold, 0x0, false, FontCharacterSet.Default, + Drawing.Direct3DDevice, 0xE, 0x0, FontWeight.DoNotCare, 0x0, false, FontCharacterSet.Default, FontPrecision.Default, FontQuality.Antialiased, FontPitchAndFamily.DontCare | FontPitchAndFamily.Decorative, "Tahoma"); @@ -250,11 +262,6 @@ private static Vector2[] GetBorder(float x, float y, float w, float h) /// private Vector2 textFix; - /// - /// Locally saved float which indicates how much text overflow is allowed. - /// - private int overflowText; - /// /// Locally saved bool which indicates if flashing mode is on or off. /// @@ -275,6 +282,16 @@ private static Vector2[] GetBorder(float x, float y, float w, float h) /// private int flashTick; + /// + /// Locally saved int which contains data of the last tick. + /// + private int clickTick; + + /// + /// Locally saved bool which indicates if border should be drawn + /// + private bool border; + #endregion #region Required Functions @@ -292,11 +309,13 @@ public void OnDraw() #region Box line.Begin(); + var vertices = new[] { new Vector2(position.X + line.Width / 0x2, position.Y), new Vector2(position.X + line.Width / 0x2, position.Y + 25f) }; + line.Draw(vertices, BoxColor); line.End(); @@ -304,45 +323,65 @@ public void OnDraw() #region Outline - var x = position.X; - var y = position.Y; - var w = line.Width; - const float h = 25f; - const float px = 1f; + if (border) + { + var x = position.X; + var y = position.Y; + var w = line.Width; + const float h = 25f; + const float px = 1f; - line.Begin(); - line.Draw(GetBorder(x, y, w, px), BorderColor); // TOP - line.End(); + line.Begin(); + line.Draw(GetBorder(x, y, w, px), BorderColor); // TOP + line.End(); - var oWidth = line.Width; - line.Width = px; + var oWidth = line.Width; + line.Width = px; - line.Begin(); - line.Draw(GetBorder(x, y, px, h), BorderColor); // LEFT - line.Draw(GetBorder(x + w, y, 1, h), BorderColor); // RIGHT - line.End(); + line.Begin(); + line.Draw(GetBorder(x, y, px, h), BorderColor); // LEFT + line.Draw(GetBorder(x + w, y, 1, h), BorderColor); // RIGHT + line.End(); - line.Width = oWidth; + line.Width = oWidth; - line.Begin(); - line.Draw(GetBorder(x, y + h, w, 1), BorderColor); // BOTTOM - line.End(); + line.Begin(); + line.Draw(GetBorder(x, y + h, w, 1), BorderColor); // BOTTOM + line.End(); + } #endregion #region Text - var text = (Text.Length > 0x1B) - ? Text.Substring(0x0, 0x18 + overflowText) + ((Text.Length - 0x1B != overflowText) ? "..." : "") - : Text; - sprite.Begin(); - var textDimension = Font.MeasureText(sprite, text, 0x0); + var textDimension = Font.MeasureText(sprite, Text, 0x0); + var finalText = Text; + + if (textDimension.Width + 0x5 > line.Width) + { + for (var i = Text.Length; i > 0x0; --i) + { + var text = Text.Substring(0x0, i); + var textWidth = Font.MeasureText(sprite, text, 0x0).Width; + + if (textWidth + 0x5 > line.Width) + { + continue; + } + + finalText = (text == Text) ? text : text.Substring(0x0, text.Length - 0x3) + "..."; + break; + } + } + + textDimension = Font.MeasureText(sprite, finalText, 0x0); + var rectangle = new Rectangle((int) position.X, (int) position.Y, (int) line.Width, 0x19); Font.DrawText( - sprite, text, rectangle.TopLeft.X + (rectangle.Width - textDimension.Width) / 0x2, + sprite, finalText, rectangle.TopLeft.X + (rectangle.Width - textDimension.Width) / 0x2, rectangle.TopLeft.Y + (rectangle.Height - textDimension.Height) / 0x2, TextColor); sprite.End(); @@ -370,7 +409,9 @@ public void OnUpdate() { update = false; draw = false; + Notifications.Free(handler); + return; } @@ -412,18 +453,16 @@ public void OnUpdate() { update = false; draw = false; + Notifications.Free(handler); + return; } } - flashingBytes[0x0] = TextColor.A; - flashingBytes[0x1] = BoxColor.A; - flashingBytes[0x2] = BorderColor.A; - - --flashingBytes[0x0]; - --flashingBytes[0x1]; - --flashingBytes[0x2]; + flashingBytes[0x0] = --TextColor.A; + flashingBytes[0x1] = --BoxColor.A; + flashingBytes[0x2] = --BorderColor.A; TextColor.A = 0x0; BoxColor.A = 0x0; @@ -454,7 +493,9 @@ public void OnUpdate() { update = false; draw = false; + Notifications.Free(handler); + return; } } @@ -474,9 +515,9 @@ public void OnUpdate() BoxColor.A = 0xFF; BorderColor.A = 0xFF; - if (Text.Length > 0x1B) + var textDimension = Font.MeasureText(sprite, Text, 0x0); + if (textDimension.Width + 0x10 > line.Width) { - var textDimension = Font.MeasureText(sprite, Text, 0x0); var extra = textDimension.Width - 0xB4; if (updatePosition == Vector2.Zero) { @@ -488,10 +529,6 @@ public void OnUpdate() position.X -= 1f; line.Width += 1f; } - if (Math.Abs(position.X - updatePosition.X) < float.Epsilon && overflowText < Text.Length - 0x1B) - { - ++overflowText; - } } } else if (updatePosition != Vector2.Zero) @@ -500,10 +537,6 @@ public void OnUpdate() { position.X += 1f; line.Width -= 1f; - if (overflowText > 0x0) - { - --overflowText; - } } else { @@ -529,9 +562,9 @@ public void OnUpdate() position.X = textFix.X; textFix = Vector2.Zero; line.Width = 190f; - overflowText = 0x0; } - updatePosition = new Vector2(position.X, location); + + updatePosition = new Vector2(position.X, Notifications.GetLocation(handler)); state = NotificationState.AnimationMove; } } @@ -569,15 +602,11 @@ public void OnUpdate() if (Math.Abs(line.Width - 0xB9) < float.Epsilon) { - var yAxis = Notifications.GetLocation(); - if (yAxis != -0x1) + handler = Notifications.Reserve(GetId(), handler); + if (handler != null) { - handler = Notifications.Reserve(GetId(), handler); - if (handler != null) - { - state = NotificationState.AnimationShowMove; - updatePosition = new Vector2(position.X, yAxis); - } + state = NotificationState.AnimationShowMove; + updatePosition = new Vector2(position.X, Notifications.GetLocation(handler)); } return; } @@ -640,7 +669,28 @@ public void OnUpdate() /// WndEventArgs public void OnWndProc(WndEventArgs args) { - // Unused Currently. + if (Utils.IsUnderRectangle(Drawing.WorldToScreen(Game.CursorPos), position.X, position.Y, line.Width, 25f)) + { + #region Mouse + + var message = (WindowsMessages) args.Msg; + if (message == WindowsMessages.WM_LBUTTONDOWN) + { + if (Utils.TickCount - clickTick < 0x5DC) + { + clickTick = Utils.TickCount; + + Notifications.Free(handler); + + draw = false; + update = false; + return; + } + clickTick = Utils.TickCount; + } + + #endregion + } } ///