Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed LibraryAttribute #9

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions code/Game.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

using Sandbox;

using Warfare.Notifications;
using Warfare.UI;
using ProjectWarfare.Notifications;
using ProjectWarfare.UI;

//
// You don't need to put things in a namespace, but it doesn't hurt.
//
namespace Warfare
namespace ProjectWarfare
{
/// <summary>
/// This is your game class. This is an entity that is created serverside when
Expand All @@ -31,7 +31,7 @@ public MyGame() : base()
{
if (IsClient)
{
_ = new NotificationStack();
_ = new NotificationQueue();
_ = new Hud();
}
}
Expand Down
10 changes: 5 additions & 5 deletions code/Pawn.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Sandbox;

using Warfare.Notifications;
using ProjectWarfare.Notifications;

namespace Warfare
namespace ProjectWarfare
{
public partial class Pawn : AnimEntity
{
Expand Down Expand Up @@ -37,7 +37,7 @@ public override void Simulate(Client cl)
{
Log.Info("Pressed Slot 1 key on server");

NotificationStack.Push(new NotificationData()
NotificationQueue.Enqueue(new NotificationData()
{
NotificationName = Library.GetAttribute(typeof(UI.Notifications.GenericNotification)).Name,
Message = "Notification created by player on server!"
Expand All @@ -51,7 +51,7 @@ public override void Simulate(Client cl)
if (Input.Released(InputButton.Slot2))
{
Log.Info("Pressed Slot 2 key on client");
NotificationStack.Push(new NotificationData()
NotificationQueue.Enqueue(new NotificationData()
{
NotificationName = Library.GetAttribute(typeof(UI.Notifications.HintNotification)).Name,
Title = "HINT",
Expand All @@ -62,7 +62,7 @@ public override void Simulate(Client cl)
if (Input.Released(InputButton.Slot3))
{
Log.Info("Pressed Slot 3 key");
NotificationStack.Push(new ErrorNotificationData()
NotificationQueue.Enqueue(new ErrorNotificationData()
{
Message = "Error created by player on client!"
});
Expand Down
7 changes: 5 additions & 2 deletions code/notifications/ErrorNotificationData.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
namespace Warfare.Notifications
using System;

namespace ProjectWarfare.Notifications
{
[NotificationData("error", typeof(UI.Notifications.ErrorNotification)), Hammer.Skip]
public partial class ErrorNotificationData : NotificationData
{
public override Type NotificationType => typeof(UI.Notifications.ErrorNotification);

public ErrorNotificationData() : base()
{
Title = "Error";
Expand Down
39 changes: 9 additions & 30 deletions code/notifications/NotificationData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,26 @@

using Sandbox;

using Warfare.UI.Notifications;
using ProjectWarfare.UI.Notifications;

namespace Warfare.Notifications
namespace ProjectWarfare.Notifications
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class NotificationDataAttribute : LibraryAttribute
{
public Type NotificationType { get; set; }

public NotificationDataAttribute(string name, Type notificationType) : base("pw_notificationdata_" + name)
{
NotificationType = notificationType;
}
}

[NotificationData("base", typeof(Notification)), Hammer.Skip]
public partial class NotificationData
public partial class NotificationData : LibraryClass
{
public string Title { get; set; }

public string Message { get; set; }

public string Name { get; set; }
public string Name { get; private set; }

public string NotificationName { get; set; }

public NotificationData()
{
LibraryAttribute libraryAttribute = Library.GetAttribute(GetType());

Name = libraryAttribute.Name;

if (libraryAttribute is NotificationDataAttribute notificationDataAttribute)
{
LibraryAttribute attribute = Library.GetAttribute(notificationDataAttribute.NotificationType);
public virtual Type NotificationType => typeof(Notification);

if (attribute is NotificationAttribute)
{
NotificationName = attribute.Name;
}
}
public NotificationData() : base()
{
Name = Library.GetAttribute(GetType()).Name;
NotificationName = Library.GetAttribute(NotificationType).Name;
}

protected virtual void WriteData(BinaryWriter binaryWriter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,22 @@

using Sandbox;

namespace Warfare.Notifications
namespace ProjectWarfare.Notifications
{
/// <summary>
/// Main class for working with notifications
/// </summary>
public partial class NotificationStack
public partial class NotificationQueue
{
public static NotificationStack Instance { get; set; }
public static NotificationQueue Instance { get; set; }

private Stack<NotificationData> Stack { get; set; } = new();
private Queue<NotificationData> Queue { get; set; } = new();

public int Count
{
get => Stack.Count;
}
public int Count => Queue.Count;

public IEnumerable<NotificationData> All
{
get => Stack.AsEnumerable();
}
public IEnumerable<NotificationData> All => Queue.AsEnumerable();

public NotificationStack()
public NotificationQueue()
{
Instance = this;
}
Expand All @@ -70,23 +64,23 @@ public static void AddNotification(string libraryName, byte[] data)

notificationData.Read(data);

Instance?.Stack.Push(notificationData);
Instance?.Queue.Enqueue(notificationData);
}

public static void Push<T>(To to, T notificationData) where T : NotificationData
public static void Enqueue<T>(To to, T notificationData) where T : NotificationData
{
if (Host.IsServer)
{
AddNotification(to, notificationData.Name, notificationData.Write());
}
else
{
Instance?.Stack.Push(notificationData);
Instance?.Queue.Enqueue(notificationData);
}
}

public static void Push<T>(T notificationData) where T : NotificationData => Push(To.Everyone, notificationData);
public static void Enqueue<T>(T notificationData) where T : NotificationData => Enqueue(To.Everyone, notificationData);

public NotificationData Pop() => Stack.Pop();
public NotificationData Dequeue() => Queue.Dequeue();
}
}
2 changes: 1 addition & 1 deletion code/ui/Hud.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Sandbox.UI;
using Sandbox.UI.Construct;

namespace Warfare.UI
namespace ProjectWarfare.UI
{
public partial class Hud : HudEntity<RootPanel>
{
Expand Down
3 changes: 1 addition & 2 deletions code/ui/notifications/ErrorNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
*
*/

namespace Warfare.UI.Notifications
namespace ProjectWarfare.UI.Notifications
{
[Notification("error"), Hammer.Skip]
public class ErrorNotification : Notification
{
public ErrorNotification() : base()
Expand Down
3 changes: 1 addition & 2 deletions code/ui/notifications/GenericNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
*
*/

namespace Warfare.UI.Notifications
namespace ProjectWarfare.UI.Notifications
{
[Notification("generic"), Hammer.Skip]
public class GenericNotification : Notification
{
public GenericNotification() : base()
Expand Down
3 changes: 1 addition & 2 deletions code/ui/notifications/HintNotification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
*
*/

namespace Warfare.UI.Notifications
namespace ProjectWarfare.UI.Notifications
{
[Notification("hint"), Hammer.Skip]
public class HintNotification : Notification
{
public HintNotification() : base()
Expand Down
13 changes: 3 additions & 10 deletions code/ui/notifications/Notification.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,18 @@
*
*/

using System;

using Sandbox;
using Sandbox.UI;

using Warfare.Notifications;
using ProjectWarfare.Notifications;

namespace Warfare.UI.Notifications
namespace ProjectWarfare.UI.Notifications
{
[AttributeUsage(AttributeTargets.Class, Inherited = false)]
public class NotificationAttribute : LibraryAttribute
{
public NotificationAttribute(string name) : base("pw_notification_" + name) { }
}

/// <summary>
/// Base class of notification panel
/// </summary>
[UseTemplate, Notification("base"), Hammer.Skip]
[UseTemplate]
public class Notification : Panel
{
/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions code/ui/notifications/NotificationWrapper.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using Sandbox;
using Sandbox.UI;

using Warfare.Notifications;
using ProjectWarfare.Notifications;

namespace Warfare.UI.Notifications
namespace ProjectWarfare.UI.Notifications
{
public partial class NotificationWrapper : Panel
{
Expand All @@ -21,7 +21,7 @@ public override void Tick()
{
base.Tick();

NotificationStack notificationStack = NotificationStack.Instance;
NotificationQueue notificationStack = NotificationQueue.Instance;

for (int i = ChildrenCount - 1; i < NOTIFICATION_LIMIT - 1; i++)
{
Expand All @@ -30,7 +30,7 @@ public override void Tick()
break;
}

NotificationData notificationData = notificationStack.Pop();
NotificationData notificationData = notificationStack.Dequeue();

Notification baseNotification = Library.Create<Notification>(notificationData.NotificationName);
baseNotification.Data = notificationData;
Expand Down