Skip to content

Commit

Permalink
Minor fixes and improvements
Browse files Browse the repository at this point in the history
Fixed main menu highlight bug
added random server generation for testing
fixed gridview element layout
implemented a server data object
  • Loading branch information
AMacro committed Jun 16, 2024
1 parent 3edb410 commit af9cd6d
Show file tree
Hide file tree
Showing 16 changed files with 937 additions and 270 deletions.
29 changes: 29 additions & 0 deletions Multiplayer/Components/MainMenu/IServerBrowserGameDetails.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.CompilerServices;
using Newtonsoft.Json.Linq;
using UnityEngine;

namespace Multiplayer.Components.MainMenu
{
//
public interface IServerBrowserGameDetails : IDisposable
{
//
//
int ServerID { get; }

//
//
//
string Name { get; set; }
int MaxPlayers { get; set; }
int CurrentPlayers { get; set; }
int Ping { get; set; }
bool HasPassword { get; set; }

}
}
1 change: 1 addition & 0 deletions Multiplayer/Components/MainMenu/MainMenuThingsAndStuff.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void SwitchToMenu(byte index)
[CanBeNull]
public Popup ShowRenamePopup()
{
Debug.Log("public Popup ShowRenamePopup() ...");
return ShowPopup(renamePopupPrefab);
}

Expand Down
320 changes: 253 additions & 67 deletions Multiplayer/Components/MainMenu/MultiplayerPane.cs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
using System;
using System.Reflection;
using DV.UIFramework;
using TMPro;
using UnityEngine;
using UnityEngine.Events;

namespace Multiplayer.Components.MainMenu
{
public class PopupTextInputFieldControllerNoValidation : MonoBehaviour, IPopupSubmitHandler
{
public Popup popup;
public TMP_InputField field;
public ButtonDV confirmButton;

private void Awake()
{
// Find the components
popup = this.GetComponentInParent<Popup>();
field = popup.GetComponentInChildren<TMP_InputField>();

foreach (ButtonDV btn in popup.GetComponentsInChildren<ButtonDV>())
{
if (btn.name == "ButtonYes")
{
confirmButton = btn;
}
}

// Set this instance as the new handler for the dialog
typeof(Popup).GetField("handler", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(popup, this);
}

private void Start()
{
// Add listener for input field value changes
field.onValueChanged.AddListener(new UnityAction<string>(OnInputValueChanged));
OnInputValueChanged(field.text);
field.Select();
field.ActivateInputField();
}

private void OnInputValueChanged(string value)
{
// Toggle confirm button interactability based on input validity
confirmButton.ToggleInteractable(IsInputValid(value));
}

public void HandleAction(PopupClosedByAction action)
{
switch (action)
{
case PopupClosedByAction.Positive:
if (IsInputValid(field.text))
{
RequestPositive();
return;
}
break;
case PopupClosedByAction.Negative:
RequestNegative();
return;
case PopupClosedByAction.Abortion:
RequestAbortion();
return;
default:
Debug.LogError(string.Format("Unhandled action {0}", action), this);
break;
}
}

private bool IsInputValid(string value)
{
// Always return true to disable validation
return true;
}

private void RequestPositive()
{
this.popup.RequestClose(PopupClosedByAction.Positive, this.field.text);
}

private void RequestNegative()
{
this.popup.RequestClose(PopupClosedByAction.Negative, null);
}

private void RequestAbortion()
{
this.popup.RequestClose(PopupClosedByAction.Abortion, null);
}
}
}
90 changes: 90 additions & 0 deletions Multiplayer/Components/MainMenu/ServerBrowserElement.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
using DV.UIFramework;
using Multiplayer.Utils;
using System.ComponentModel;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

namespace Multiplayer.Components.MainMenu;


//
public class ServerBrowserElement : AViewElement<IServerBrowserGameDetails>
{
private TextMeshProUGUI networkName;
private TextMeshProUGUI playerCount;
private TextMeshProUGUI ping;
private GameObject goIcon;
private Image icon;
private IServerBrowserGameDetails data;

private const int PING_WIDTH = 62 * 2;
private const int PING_POS_X = 650;
private void Awake()
{
//Find existing fields to duplicate
networkName = this.FindChildByName("name [noloc]").GetComponent<TextMeshProUGUI>();
playerCount = this.FindChildByName("date [noloc]").GetComponent<TextMeshProUGUI>();
ping = this.FindChildByName("time [noloc]").GetComponent<TextMeshProUGUI>();
goIcon = this.FindChildByName("autosave icon");
icon = goIcon.GetComponent<Image>();

//Fix alignment
Vector3 namePos = networkName.transform.position;
Vector2 nameSize = networkName.rectTransform.sizeDelta;

playerCount.transform.position = new Vector3(namePos.x + nameSize.x, namePos.y, namePos.z);


Vector2 rowSize = this.transform.GetComponentInParent<RectTransform>().sizeDelta;
Vector3 pingPos = ping.transform.position;
Vector2 pingSize = ping.rectTransform.sizeDelta;


ping.rectTransform.sizeDelta = new Vector2(PING_WIDTH, pingSize.y);
pingSize = ping.rectTransform.sizeDelta;

ping.transform.position = new Vector3(PING_POS_X, pingPos.y, pingPos.z);

ping.alignment = TextAlignmentOptions.Right;


//Update clock Icon
icon.sprite = Sprites.Padlock;



/*
networkName.text = "Test Network";
playerCount.text = "1/4";
ping.text = "102";
*/
}

public override void SetData(IServerBrowserGameDetails data, AGridView<IServerBrowserGameDetails> _)
{
if (this.data != null)
{
this.data = null;
}
if (data != null)
{
this.data = data;
}
UpdateView(null, null);
}

//
private void UpdateView(object sender = null, PropertyChangedEventArgs e = null)
{
networkName.text = data.Name;
playerCount.text = $"{data.CurrentPlayers} / {data.MaxPlayers}";
ping.text = $"{data.Ping} ms";

if (!data.HasPassword)
{
goIcon.SetActive(false);
}
}

}
36 changes: 36 additions & 0 deletions Multiplayer/Components/MainMenu/ServerBrowserGridView.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DV.Common;
using DV.UI;
using DV.UIFramework;
using UnityEngine;
using UnityEngine.UI;

namespace Multiplayer.Components.MainMenu
{
[RequireComponent(typeof(ContentSizeFitter))]
[RequireComponent(typeof(VerticalLayoutGroup))]
//
public class ServerBrowserGridView : AGridView<IServerBrowserGameDetails>
{

private void Awake()
{
Debug.Log("serverBrowserGridview Awake");

this.dummyElementPrefab.SetActive(false);

//swap controller
GameObject.Destroy(this.dummyElementPrefab.GetComponent<SaveLoadViewElement>());
this.dummyElementPrefab.AddComponent<ServerBrowserElement>();

this.dummyElementPrefab.SetActive(true);
this.viewElementPrefab = this.dummyElementPrefab;
// GameObject defaultPrefab = GameObject.Find("SaveLoadViewElement");
// this.dummyElementPrefab = Instantiate(defaultPrefab);
}
}
}
Loading

0 comments on commit af9cd6d

Please sign in to comment.