Skip to content

Commit

Permalink
Merge pull request #27 from Makinolo/feature/optional_menus
Browse files Browse the repository at this point in the history
Creates interface to communicate with other mods and menus optional
  • Loading branch information
EntenKoeniq-OLD authored Sep 27, 2021
2 parents 9216452 + 7b467ae commit 1db9dd5
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 31 deletions.
3 changes: 2 additions & 1 deletion Client/CoopClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<DefineConstants>TRACE;DEBUG</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<PlatformTarget>x64</PlatformTarget>
Expand Down Expand Up @@ -65,6 +65,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Chat.cs" />
<Compile Include="Interface.cs" />
<Compile Include="Entities\EntitiesNpc.cs" />
<Compile Include="Entities\EntitiesPed.cs" />
<Compile Include="Entities\EntitiesPlayer.cs" />
Expand Down
44 changes: 44 additions & 0 deletions Client/Interface.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using Lidgren.Network;

namespace CoopClient
{
public static class Interface
{
public delegate void ConnectEvent(bool connected, string bye_message);
public static event ConnectEvent OnConnect;
public static event ConnectEvent OnDisconnect;
public delegate void MessageEvent(NetIncomingMessage message);
public static event MessageEvent OnMessage;

public static void Connect(string serverAddress)
{
Main.MainNetworking.DisConnectFromServer(serverAddress);
}

public static void Configure(string playerName, bool shareNpcsWithPlayers, int streamedNpcs, bool debug = false)
{
Main.MainSettings.Username = playerName;
Main.ShareNpcsWithPlayers = shareNpcsWithPlayers;
Main.MainSettings.StreamedNpc = streamedNpcs;
#if DEBUG
Main.UseDebug = debug;
#endif
}

public static void Disconnected( string bye_message)
{
OnDisconnect?.Invoke(false, bye_message);
}

public static void Connected()
{
OnConnect?.Invoke(true, "");
}

public static void MessageReceived(NetIncomingMessage message)
{
OnMessage?.Invoke(message);
}

}
}
10 changes: 8 additions & 2 deletions Client/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ public class Main : Script
private static bool IsGoingToCar = false;

public static Settings MainSettings = Util.ReadSettings();

public static Networking MainNetworking = new Networking();

#if !NON_INTERACTIVE
public static MenusMain MainMenu = new MenusMain();

#endif
public static Chat MainChat = new Chat();

public static long LocalClientID = 0;
Expand All @@ -43,7 +43,9 @@ public Main()
Function.Call((Hash)0x9BAE5AD2508DF078, true); // _ENABLE_MP_DLC_MAPS

Tick += OnTick;
#if !NON_INTERACTIVE
KeyDown += OnKeyDown;
#endif
Aborted += (object sender, EventArgs e) => CleanUp();

Util.NativeMemory();
Expand All @@ -62,7 +64,9 @@ private void OnTick(object sender, EventArgs e)
Game.Player.Character.RelationshipGroup = RelationshipGroup;
}

#if !NON_INTERACTIVE
MainMenu.MenuPool.Process();
#endif

MainNetworking.ReceiveMessages();

Expand Down Expand Up @@ -110,6 +114,7 @@ private void OnTick(object sender, EventArgs e)
LastDataSend = Environment.TickCount;
}

#if !NON_INTERACTIVE
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (MainChat.Focused)
Expand Down Expand Up @@ -169,6 +174,7 @@ private void OnKeyDown(object sender, KeyEventArgs e)
break;
}
}
#endif

public static void CleanUp()
{
Expand Down
28 changes: 28 additions & 0 deletions Client/Menus/MenusMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,33 @@ public void ServerIpActivated(object a, System.EventArgs b)
MenuPool.RefreshAll();
}
}

public void InitiateConnectionMenuSetting()
{
MainMenu.Items[0].Enabled = false;
MainMenu.Items[1].Enabled = false;
MainMenu.Items[2].Enabled = false;
}

public void ConnectedMenuSetting()
{
MainMenu.Items[2].Enabled = true;
MainMenu.Items[2].Title = "Disconnect";
SubSettings.MainMenu.Items[1].Enabled = !Main.DisableTraffic && Main.NpcsAllowed;

MainMenu.Visible = false;
MenuPool.RefreshAll();
}

public void DisconnectedMenuSetting()
{
MainMenu.Items[0].Enabled = true;
MainMenu.Items[1].Enabled = true;
MainMenu.Items[2].Enabled = true;
MainMenu.Items[2].Title = "Connect";
SubSettings.MainMenu.Items[1].Enabled = false;

MenuPool.RefreshAll();
}
}
}
2 changes: 2 additions & 0 deletions Client/Menus/Sub/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ public void StreamedNpcsValueChanged(object a, System.EventArgs b)

public void FlipMenuCheckboxChanged(object a, System.EventArgs b)
{
#if !NON_INTERACTIVE
Main.MainMenu.MainMenu.Alignment = FlipMenuItem.Checked ? GTA.UI.Alignment.Right : GTA.UI.Alignment.Left;
#endif
MainMenu.Alignment = FlipMenuItem.Checked ? GTA.UI.Alignment.Right : GTA.UI.Alignment.Left;

Main.MainSettings.FlipMenu = FlipMenuItem.Checked;
Expand Down
47 changes: 20 additions & 27 deletions Client/Networking.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public void DisConnectFromServer(string address)
new PlayerDisconnectPacket() { ID = Main.LocalClientID }.PacketToNetOutGoingMessage(outgoingMessage);
Client.SendMessage(outgoingMessage, NetDeliveryMethod.ReliableOrdered);
Client.FlushSendQueue();

Client.Disconnect("Bye!");
Interface.Disconnected("Bye!");
}
else
{
Expand Down Expand Up @@ -59,6 +59,7 @@ public void DisConnectFromServer(string address)
}.PacketToNetOutGoingMessage(outgoingMessage);

Client.Connect(ip[0], short.Parse(ip[1]), outgoingMessage);
Interface.Connected();
}
}

Expand Down Expand Up @@ -90,9 +91,9 @@ public void ReceiveMessages()
switch (status)
{
case NetConnectionStatus.InitiatedConnect:
Main.MainMenu.MainMenu.Items[0].Enabled = false;
Main.MainMenu.MainMenu.Items[1].Enabled = false;
Main.MainMenu.MainMenu.Items[2].Enabled = false;
#if !NON_INTERACTIVE
Main.MainMenu.InitiateConnectionMenuSetting();
#endif
GTA.UI.Notification.Show("~y~Trying to connect...");
break;
case NetConnectionStatus.Connected:
Expand Down Expand Up @@ -130,13 +131,9 @@ public void ReceiveMessages()
Client.FlushSendQueue();

GTA.UI.Notification.Show("~g~Connected!");

Main.MainMenu.MainMenu.Items[2].Enabled = true;
Main.MainMenu.MainMenu.Items[2].Title = "Disconnect";
Main.MainMenu.SubSettings.MainMenu.Items[1].Enabled = !Main.DisableTraffic && Main.NpcsAllowed;

Main.MainMenu.MainMenu.Visible = false;
Main.MainMenu.MenuPool.RefreshAll();
#if !NON_INTERACTIVE
Main.MainMenu.ConnectedMenuSetting();
#endif
}
break;
case NetConnectionStatus.Disconnected:
Expand All @@ -153,14 +150,9 @@ public void ReceiveMessages()
}

Main.CleanUp();

Main.MainMenu.MainMenu.Items[0].Enabled = true;
Main.MainMenu.MainMenu.Items[1].Enabled = true;
Main.MainMenu.MainMenu.Items[2].Enabled = true;
Main.MainMenu.MainMenu.Items[2].Title = "Connect";
Main.MainMenu.SubSettings.MainMenu.Items[1].Enabled = false;

Main.MainMenu.MenuPool.RefreshAll();
#if !NON_INTERACTIVE
Main.MainMenu.DisconnectedMenuSetting();
#endif
break;
}
break;
Expand Down Expand Up @@ -242,12 +234,13 @@ public void ReceiveMessages()
break;
}

Interface.MessageReceived(message);
Client.Recycle(message);
}
}

#region -- GET --
#region -- PLAYER --
#region -- GET --
#region -- PLAYER --
private void PlayerConnect(PlayerConnectPacket packet)
{
EntitiesPlayer player = new EntitiesPlayer()
Expand Down Expand Up @@ -448,9 +441,9 @@ private void DecodeNativeCall(NativeCallPacket packet)

Function.Call((Hash)packet.Hash, arguments.ToArray());
}
#endregion // -- PLAYER --
#endregion // -- PLAYER --

#region -- NPC --
#region -- NPC --
private void FullSyncNpc(FullSyncNpcPacket packet)
{
lock (Main.Npcs)
Expand Down Expand Up @@ -570,10 +563,10 @@ private void FullSyncNpcVeh(FullSyncNpcVehPacket packet)
}
}
}
#endregion // -- NPC --
#endregion
#endregion // -- NPC --
#endregion

#region -- SEND --
#region -- SEND --
private int LastPlayerFullSync = 0;
public void SendPlayerData()
{
Expand Down Expand Up @@ -792,6 +785,6 @@ public void SendChatMessage(string message)
}
#endif
}
#endregion
#endregion
}
}
6 changes: 5 additions & 1 deletion Client/PlayerList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ private void OnTick(object sender, EventArgs e)
Update(Main.Players, Main.MainSettings.Username);
}

if ((Environment.TickCount - Pressed) < 5000 && !Main.MainChat.Focused && !Main.MainMenu.MenuPool.AreAnyVisible)
if ((Environment.TickCount - Pressed) < 5000 && !Main.MainChat.Focused
#if !NON_INTERACTIVE
&& !Main.MainMenu.MenuPool.AreAnyVisible
#endif
)
{
Function.Call(Hash.DRAW_SCALEFORM_MOVIE, MainScaleform.Handle, 0.122f, 0.3f, 0.28f, 0.6f, 255, 255, 255, 255, 0);
}
Expand Down

0 comments on commit 1db9dd5

Please sign in to comment.