Skip to content

Commit

Permalink
UdonStringEvent, Logger, Keyboard V1
Browse files Browse the repository at this point in the history
  • Loading branch information
FairlySadPanda committed Apr 13, 2020
1 parent fcf7ae5 commit 84e9a6d
Show file tree
Hide file tree
Showing 22 changed files with 460 additions and 2,818 deletions.
13 changes: 12 additions & 1 deletion ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,29 @@
using VRC.Udon.Common.Interfaces;
using UnityEngine.UI;

///<Summary>A controller for a button that will convert the product of an InputField to a ChatMessage event.</Summary>
public class ChatController : UdonSharpBehaviour
{
///<Summary>The text input field for this controller.</Summary>
public InputField input;
///<Summary>The manager we want to handle chat events.</Summary>
public EventEmissionManager manager;

///<Summary>Send a ChatMessage using the product of an InputField.</Summary>
public void SendMessage()
{
if (input.text == "" || input.text == null)
{
return;
}
manager.SendChatMessage(input.text);

if (input.text.Length > 100)
{
input.text = input.text.Substring(0, 100);
}

string message = input.text;
manager.SendEvent("ChatMessage", message.Replace(",", "|"));
input.text = null;
}
}
11 changes: 0 additions & 11 deletions ChatController.cs.meta

This file was deleted.

18 changes: 0 additions & 18 deletions DiceRoller.cs

This file was deleted.

11 changes: 0 additions & 11 deletions DiceRoller.cs.meta

This file was deleted.

62 changes: 40 additions & 22 deletions EventEmissionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,72 @@
using VRC.Udon;
using VRC.Udon.Common.Interfaces;

///<Summary>The point-of-contact for all users of the UdonStringEvent system.</Summary>
public class EventEmissionManager : UdonSharpBehaviour
{
///<Summary>The receiver of events in this event system.</Summary>
public EventReceiver receiver;
private EventEmitter emitter;

private int clock;
private string displayName;
private bool gotEmitter;

public void Start()
{
clock = 0;
displayName = "";
gotEmitter = false;

if (Networking.LocalPlayer != null)
{
displayName = Networking.LocalPlayer.displayName;
}

}

public void Update()
{
if (emitter == null)
if (gotEmitter == false)
{
string displayName = "";
GameObject ownedEmitter = null;

if (Networking.LocalPlayer != null)
{
displayName = Networking.LocalPlayer.displayName;
}

ownedEmitter = receiver.GetEmitter(displayName);

if (ownedEmitter != null)
{
Debug.Log("Emitter object has arrived in our care.");
Networking.SetOwner(Networking.LocalPlayer, ownedEmitter);
emitter = (EventEmitter)ownedEmitter.GetComponent(typeof(UdonBehaviour));
}
GetEmitter();
//TODO: If gotemitter is false for a long time (5 seconds?) panic and alert the receiver that a reallocation has to happen.
}
}

///<Summary>Send a string event to all other players in the world.</Summary>
public void SendEvent(string eventName, string eventPayload)
{
// An extension to this system might queue up events in the instance that we want to make sure they're sent on world load.
// For the time being a good modification might be to change SendEvent to return a bool.

// This can be bad in two ways: either the return is null or the return is not owned.
GameObject emitter = receiver.GetEmitter(displayName);
if (emitter == null)
{
Debug.Log("emitter was null, could not handle " + eventName + " event");
Debug.Log("emitter was null: could not handle " + eventName + " event");
gotEmitter = false;
return;
}

emitter.SetNewEvent(eventName, eventPayload);
if (!Networking.IsOwner(emitter.gameObject))
{
Debug.Log("emitter not owned by player: could not handle " + eventName + " event");
gotEmitter = false;
return;
}

emitter.GetComponent<EventEmitter>().SetNewEvent(eventName, eventPayload);
}

public void SendChatMessage(string message)
private void GetEmitter()
{
SendEvent("ChatMessage", message);
GameObject emitter = receiver.GetEmitter(displayName);

if (emitter != null)
{
Debug.Log("Emitter object has arrived in our care.");
Networking.SetOwner(Networking.LocalPlayer, emitter);
gotEmitter = true;
}
}
}
11 changes: 0 additions & 11 deletions EventEmissionManager.cs.meta

This file was deleted.

22 changes: 19 additions & 3 deletions EventEmitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,29 @@
using VRC.Udon;
using VRC.Udon.Common.Interfaces;

/// <Summary>A Behaviour that can emit events. An Emitter will check if this Emitter has emitted each update.</Summary>
public class EventEmitter : UdonSharpBehaviour
{
// Designates who owns this Emitter.
[UdonSynced]
private string characterName;

// If newEvent doesn't match oldEvent, then a new event has been emitted.
[UdonSynced]
private string newEvent;
private string oldEvent;

private int clock;

public void Start()
{
characterName = "";
newEvent = "";
oldEvent = "";
clock = 0;
}

/// <Summary>If a new event has been emitted, return the event. Otherwise return an empty string.</Summary>
public string GetNewEvent()
{
if (newEvent == oldEvent)
Expand All @@ -33,20 +40,29 @@ public string GetNewEvent()
return newEvent;
}

/// <Summary>Set a new event to be emitted.</Summary>
public void SetNewEvent(string eventName, string payload)
{
// Leave this debug log on unless you have a lot of spam. It'll help with debugging.
Debug.Log("Sending event: " + eventName + ": " + payload);
newEvent = eventName + "," + payload;
newEvent = eventName + "," + payload + "," + clock;

clock++;
}

/// <Summary>Get the name of the character that owns this emitter.</Summary>
public string GetCharacterName()
{
// Note that for Udon, even though we *can* get variables directly, don't. It's cleaner to rely on functions.
return characterName;
}

/// <Summary>Set the owner of the emitter to the provided name.</Summary>
public void SetCharacterName(string c)
{
characterName = c;
Debug.Log("Emitter's Character Name has been set to " + c);
if (Networking.IsOwner(gameObject))
{
characterName = c;
}
}
}
11 changes: 0 additions & 11 deletions EventEmitter.cs.meta

This file was deleted.

23 changes: 19 additions & 4 deletions EventReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,35 @@
using VRC.Udon;
using VRC.Udon.Common.Interfaces;

///<Summary>EventReceiver is the core of the UdonStringEvent system. One must exist per system.</Summary>
public class EventReceiver : UdonSharpBehaviour
{
/// <Summary>An array of all Emitters in the system.</Summary>
public GameObject[] emitters;

/// <Summary>A logger that events can be output to. This is optional.</Summary>
public UdonLogger logger;

public void Update()
{
for (int i = 0; i < 4; i++)
for (int i = 0; i < emitters.Length; i++)
{
// UdonSharp limitation - this can be refactored once the generic is handled correctly.
EventEmitter emitter = ((EventEmitter)emitters[i].GetComponent(typeof(UdonBehaviour)));
string newEv = emitter.GetNewEvent();

// GetNewEvent returns either a nil event (empty string) or an event.
if (newEv != "")
{
HandleUpdate(emitter.GetCharacterName(), newEv);
}
}
}

/// <Summary>Retrieve an Emitter if one is found that belongs to the provied character name, or null if one isn't.</Summary>
public GameObject GetEmitter(string characterName)
{
for (int i = 0; i < 4; i++)
for (int i = 0; i < emitters.Length; i++)
{
if (emitters[i].GetComponent<EventEmitter>().GetCharacterName() == characterName)
{
Expand All @@ -39,19 +47,25 @@ public GameObject GetEmitter(string characterName)

private void HandleUpdate(string characterName, string eventString)
{
// As it stands, hard-code your events in this function.
// This is pretty basic. Once maps and lists exist in Udon, this can be improved.
string[] e = eventString.Split(',');
Debug.Log("Got an event named " + e[0] + "with payload " + eventString);
Debug.Log("Got an event named " + e[0] + " with payload " + eventString);
switch (e[0])
{
case "ChatMessage":
logger.Notice(characterName + ": " + e[1]);
string message = characterName + ": " + e[1];
message = message.Replace("|", ",");
Debug.Log("Notice: " + message);
logger.Notice(message);
break;
default:
logger.Notice("Got an event named " + e[0] + " but didn't know what to do with it.");
break;
}
}

/// <Summary>Get an empty emitter and assign it to the new player.</Summary>
public override void OnPlayerJoined(VRCPlayerApi player)
{
if (Networking.IsOwner(gameObject))
Expand All @@ -61,6 +75,7 @@ public override void OnPlayerJoined(VRCPlayerApi player)
}
}

/// <Summary>Get the player's emitter and assign it to nobody.</Summary>
public override void OnPlayerLeft(VRCPlayerApi player)
{
if (Networking.IsOwner(gameObject))
Expand Down
11 changes: 0 additions & 11 deletions EventReceiver.cs.meta

This file was deleted.

27 changes: 27 additions & 0 deletions KeyEventListener.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using UdonSharp;
using UnityEngine;
using UnityEngine.UI;
using VRC.SDKBase;
using VRC.Udon;
using VRC.Udon.Common.Interfaces;

public class KeyEventListener : UdonSharpBehaviour
{
public KeyboardManager keyboard;
public UdonLogger logger;

private void Update()
{
var player = Networking.LocalPlayer;
if (player != null && player.IsUserInVR())
{
return;
}

if (Input.GetKeyDown(KeyCode.K))
{
keyboard.Toggle();
logger.Toggle();
}
}
}
16 changes: 16 additions & 0 deletions Keyboard/Backspace.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using UdonSharp;
using UnityEngine;
using VRC.SDKBase;
using VRC.Udon;
using VRC.Udon.Common.Interfaces;
using UnityEngine.UI;

public class BackspaceKey : UdonSharpBehaviour
{
public KeyboardManager manager;

public void PressKey()
{
manager.Backspace();
}
}
Loading

0 comments on commit 84e9a6d

Please sign in to comment.