Skip to content

Commit

Permalink
Merge pull request #6 from bookdude13/ui-enhancements
Browse files Browse the repository at this point in the history
UI enhancements
  • Loading branch information
bookdude13 authored Apr 25, 2022
2 parents 65f176a + bda0924 commit b649dd5
Show file tree
Hide file tree
Showing 7 changed files with 237 additions and 129 deletions.
2 changes: 1 addition & 1 deletion PrepareRelease.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

set BUILT_VERSION="1.0.0"
set BUILT_VERSION="1.1.0"
set PROJECT_NAME="SRVoting"

set RELEASE_BUILD_DIR=".\%PROJECT_NAME%\bin\Release"
Expand Down
6 changes: 3 additions & 3 deletions SRVoting/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyVersion("1.1.0.0")]
[assembly: AssemblyFileVersion("1.1.0.0")]

[assembly: MelonInfo(typeof(SRVoting.SRVoting), "SRVoting", "1.0.0", "bookdude13", "https://github.com/bookdude13/SRVoting")]
[assembly: MelonInfo(typeof(SRVoting.SRVoting), "SRVoting", "1.1.0", "bookdude13", "https://github.com/bookdude13/SRVoting")]
[assembly: MelonGame("Kluge Interactive", "SynthRiders")]

9 changes: 6 additions & 3 deletions SRVoting/SRVoting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public override void OnApplicationStart()
{
base.OnApplicationStart();

logger = new MelonLoggerWrapper(LoggerInstance, false);
logger = new MelonLoggerWrapper(LoggerInstance);
}

public override void OnSceneWasInitialized(int buildIndex, string sceneName)
Expand Down Expand Up @@ -50,8 +50,11 @@ public override void OnSceneWasInitialized(int buildIndex, string sceneName)
votingBehavior = votingGO.AddComponent<VotingMonoBehavior>();
votingBehavior.Init(logger, synthriderzService);

Synth.Data.SongsProvider.GetInstance.ItemClicked += (index) => { votingBehavior.OnSongChanged(); };
Synth.Data.SongsProvider.GetInstance.ItemsLoaded += (totalSongs) => { votingBehavior.OnSongChanged(); };
Synth.Data.SongsProvider.GetInstance.ItemClicked += (index) => { votingBehavior.Refresh(); };
Synth.Data.SongsProvider.GetInstance.ItemsLoaded += (totalSongs) => { votingBehavior.Refresh(); };

// Refresh right away to catch the case when returning from a song
votingBehavior.Refresh();
}
}
}
Expand Down
1 change: 1 addition & 0 deletions SRVoting/SRVoting.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
<Compile Include="Services\SynthriderzService.cs" />
<Compile Include="SRVoting.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="UI\VoteDirectionComponent.cs" />
<Compile Include="Util\ILogger.cs" />
<Compile Include="Util\MelonLoggerWrapper.cs" />
<Compile Include="Util\SRScene.cs" />
Expand Down
142 changes: 142 additions & 0 deletions SRVoting/UI/VoteDirectionComponent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
using SRVoting.Util;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using VRTK.UnityEventHelper;

namespace SRVoting.UI
{
class VoteDirectionComponent
{
private Util.ILogger logger;
private UnityAction<object, VRTK.InteractableObjectEventArgs> onArrowUse;
private GameObject arrow;
private VRTK_InteractableObject_UnityEvents arrowEvents;
private TMPro.TMP_Text countText;

public string ArrowName { get; private set; }
public bool IsUiCreated { get; private set; } = false;


public VoteDirectionComponent(Util.ILogger logger, string arrowName, UnityAction<object, VRTK.InteractableObjectEventArgs> onArrowUse)
{
this.logger = logger;
ArrowName = arrowName;
this.onArrowUse = onArrowUse;
}

public void DisableEvents()
{
arrowEvents?.OnUse?.RemoveAllListeners();
}

public void UpdateUI(bool isActive, bool isMyVote, string text)
{
countText.SetText(text);

if (isMyVote)
{
countText.fontStyle = TMPro.FontStyles.Underline;
countText.color = Color.white;
}
else
{
countText.fontStyle = TMPro.FontStyles.Normal;
countText.color = new Color(0.4f, 0.4f, 0.4f, 1.0f);
}

if (isActive)
{
arrowEvents.OnUse.RemoveAllListeners();
arrowEvents.OnUse.AddListener(onArrowUse);

arrow.GetComponent<Synth.Utils.VRTKButtonHelper>().SetActive();
}
else
{
arrowEvents.OnUse.RemoveAllListeners();

arrow.GetComponent<Synth.Utils.VRTKButtonHelper>().SetInactive();
}
}

public void CreateUI(
Transform parent,
Transform leftSideReference,
Transform rightOffsetReference,
Transform arrowToClone,
GameObject textReference
)
{
if (IsUiCreated)
{
return;
}

IsUiCreated = true;

var container = CreateVoteDirectionContainer(parent, leftSideReference, rightOffsetReference);
arrow = CreateVoteArrow(container.transform, arrowToClone);
arrowEvents = arrow.GetComponent<VRTK_InteractableObject_UnityEvents>();
countText = CreateVoteCountText(container.transform, arrow, textReference);
}

private GameObject CreateVoteDirectionContainer(
Transform parent,
Transform leftSideReference,
Transform rightOffsetReference
)
{
var voteContainer = new GameObject("srvoting_container");
voteContainer.transform.SetParent(parent, false);
voteContainer.transform.localPosition = leftSideReference.localPosition + rightOffsetReference.localPosition + new Vector3(2.0f, 0.0f, 0.0f);
voteContainer.transform.localRotation = leftSideReference.localRotation;

return voteContainer;
}

private GameObject CreateVoteArrow(Transform voteContainer, Transform arrowToClone)
{
// Clone arrow used to actually vote
var voteArrow = GameObject.Instantiate(arrowToClone, voteContainer.transform);
voteArrow.name = ArrowName;
voteArrow.localPosition = Vector3.zero;
voteArrow.localEulerAngles = arrowToClone.localEulerAngles + new Vector3(0f, 0f, 90f);

// Replace button event
var buttonEvents = voteArrow.GetComponent<VRTK_InteractableObject_UnityEvents>();
buttonEvents.OnUse.RemoveAllListeners();

// 2 persistent listeners not removed by RemoveAllListeners() exist
// See https://forum.unity.com/threads/documentation-unityevent-removealllisteners-only-removes-non-persistent-listeners.341796/
// After trial and error, the one at index 0 controls volume still and needs to be disabled.
// The one at index 1 still needs to stick around to handle new events
buttonEvents.OnUse.SetPersistentListenerState(0, UnityEventCallState.Off);

voteArrow.gameObject.SetActive(true);
buttonEvents.OnUse.AddListener(onArrowUse);

logger.Debug($"Arrow added");
return voteArrow.gameObject;
}

private TMPro.TMP_Text CreateVoteCountText(Transform voteContainer, GameObject voteArrow, GameObject textReference)
{
var voteCountText = GameObject.Instantiate(textReference, voteContainer);
voteCountText.name = voteArrow.name + "_text";
voteCountText.transform.localPosition = voteArrow.transform.localPosition + new Vector3(1.2f, 0.0f, 0.0f);
voteCountText.transform.eulerAngles = textReference.transform.eulerAngles;

var text = voteCountText.GetComponent<TMPro.TMP_Text>();
text.SetText("#####");
text.alignment = TMPro.TextAlignmentOptions.Left;

logger.Debug("Text added");
return text;
}
}
}
10 changes: 7 additions & 3 deletions SRVoting/Util/MelonLoggerWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ namespace SRVoting.Util
{
public class MelonLoggerWrapper : ILogger
{
#if DEBUG
private readonly bool isDebug = true;
#else
private readonly bool isDebug = false;
#endif

private readonly MelonLogger.Instance melonLogger;
private bool isDebug;

public MelonLoggerWrapper(MelonLogger.Instance melonLogger, bool isDebug = false)
public MelonLoggerWrapper(MelonLogger.Instance melonLogger)
{
this.melonLogger = melonLogger;
this.isDebug = isDebug;
}

public void Msg(string message)
Expand Down
Loading

0 comments on commit b649dd5

Please sign in to comment.