Skip to content

Commit

Permalink
Notification: Adding double-click to remove a notification, accurate …
Browse files Browse the repository at this point in the history
…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.
  • Loading branch information
L33T committed Feb 17, 2015
1 parent b9ba1ef commit 5f780d4
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 73 deletions.
10 changes: 10 additions & 0 deletions Notifications.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// Validates if current position is first in line
/// </summary>
Expand Down
196 changes: 123 additions & 73 deletions Notifications/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
Expand All @@ -123,6 +118,23 @@ public void Flash(int interval = 0xFA)
}
}

/// <summary>
/// Toggles the notification border
/// </summary>
public void Border()
{
border = !border;
}

/// <summary>
/// Sets the notification border toggle value
/// </summary>
/// <param name="value">bool value</param>
public void Border(bool value)
{
border = value;
}

/// <summary>
/// Calculate the next decreasement tick.
/// </summary>
Expand Down Expand Up @@ -175,7 +187,7 @@ private static Vector2[] GetBorder(float x, float y, float w, float h)
/// Notification's Font
/// </summary>
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");

Expand Down Expand Up @@ -250,11 +262,6 @@ private static Vector2[] GetBorder(float x, float y, float w, float h)
/// </summary>
private Vector2 textFix;

/// <summary>
/// Locally saved float which indicates how much text overflow is allowed.
/// </summary>
private int overflowText;

/// <summary>
/// Locally saved bool which indicates if flashing mode is on or off.
/// </summary>
Expand All @@ -275,6 +282,16 @@ private static Vector2[] GetBorder(float x, float y, float w, float h)
/// </summary>
private int flashTick;

/// <summary>
/// Locally saved int which contains data of the last tick.
/// </summary>
private int clickTick;

/// <summary>
/// Locally saved bool which indicates if border should be drawn
/// </summary>
private bool border;

#endregion

#region Required Functions
Expand All @@ -292,57 +309,79 @@ 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();

#endregion

#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();
Expand Down Expand Up @@ -370,7 +409,9 @@ public void OnUpdate()
{
update = false;
draw = false;

Notifications.Free(handler);

return;
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -454,7 +493,9 @@ public void OnUpdate()
{
update = false;
draw = false;

Notifications.Free(handler);

return;
}
}
Expand All @@ -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)
{
Expand All @@ -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)
Expand All @@ -500,10 +537,6 @@ public void OnUpdate()
{
position.X += 1f;
line.Width -= 1f;
if (overflowText > 0x0)
{
--overflowText;
}
}
else
{
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -640,7 +669,28 @@ public void OnUpdate()
/// <param name="args">WndEventArgs</param>
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
}
}

/// <summary>
Expand Down

0 comments on commit 5f780d4

Please sign in to comment.