I was working on a admin panel for my team to use in the events, and was trying to make whitelisting easy with input fields but I can't seem to retrieve the text from the input box, am I just doing somthing wrong?
Thanks,
Travis Butts
Sooooo, you'll have to make a couple of changes.
First is to define the inputfield itself:
/// <summary>
/// Input field object
/// </summary>
private class CuiInputField
{
public CuiInputFieldComponent InputField { get; } = new CuiInputFieldComponent();
public CuiRectTransformComponent RectTransform { get; } = new CuiRectTransformComponent();
public float FadeOut { get; set; }
}Once you have this, you'll need to extend the `CuiElementContainer` class to make it support the input field:
/// <summary>
/// Custom version of the CuiElementContainer to add InputFields
/// </summary>
private class CustomCuiElementContainer : CuiElementContainer
{
public string Add(CuiInputField aInputField, string aParent = Cui.ParentHud, string aName = "")
{
if (string.IsNullOrEmpty(aName))
aName = CuiHelper.GetGuid();
if (aInputField == null) {
return string.Empty;
}
Add(new CuiElement {
Name = aName,
Parent = aParent,
FadeOut = aInputField.FadeOut,
Components = {
aInputField.InputField,
aInputField.RectTransform
}
});
return aName;
}
}Then you can add them to the UI (I tend to put them ontop of a panel to give it some shade behind it.
The command it expects is a generic `Command` for `CovalencePlugin` and a `ConsoleCommand` for a rust-specific plugin (Rust-specific should be avoided).
Command example:
private Dictionary<string, string> FExampleInputText= new Dictionary<string, string>(); // Format: <userId, text>
[Command("example.exampleinputtextcb")]
private void ExampleExampleInputTextCallback(IPlayer aPlayer, string aCommand, string[] aArgs)
{
if (aArgs.Count() <= 0) {
if (FExampleInputText.ContainsKey(aPlayer.Id))
FExampleInputText.Remove(aPlayer.Id);
return;
}
if (FExampleInputText.ContainsKey(aPlayer.Id)) {
FExampleInputText[aPlayer.Id] = aArgs[0];
} else {
FExampleInputText.Add(aPlayer.Id, aArgs[0]);
}
} In response to ThibmoRozier ():Sooooo, you'll have to make a couple of changes.
First is to define the inputfield itself:///...
var TestNameInput = new CuiElement
{
Name = "TestNameInput ",
Parent = ParentClass,
Components =
{
new CuiInputFieldComponent
{
Text = string.Empty,
CharsLimit = 40,
Color = "1 1 1 1",
IsPassword = false,
Command = "example.exampleinputtextcb",
Font = "robotocondensed-regular.ttf",
FontSize = 10,
Align = TextAnchor.MiddleLeft
},
new CuiRectTransformComponent
{
AnchorMin = anchormin,
AnchorMax = anchormax
}
}
};
container.Add(TestNameInput);
Then, make sure you add the dictonary at the top.
private Dictionary<string, string> FExampleInputText= new Dictionary<string, string>();
Lastly, for RustPlugin classes you would use the ConsoleCommand which is shown down below.
[ConsoleCommand("example.exampleinputtextcb")]
private void ExampleExampleInputTextCallback(ConsoleSystem.Arg arg)
{
BasePlayer player = arg.Player();
if (arg.Args.Length <= 0)
{
if (FExampleInputText.ContainsKey(player.UserIDString))
FExampleInputText.Remove(player.UserIDString);
return;
}
if (FExampleInputText.ContainsKey(player.UserIDString))
{
FExampleInputText[player.UserIDString] = arg.Args[0];
}
else
{
FExampleInputText.Add(player.UserIDString, arg.Args[0]);
}
}
Thanks for all the help, hope I can help out those that don't want to switch!
Again, avoid the legacy RustPlugin parent.. ;)
Won't be there in uMod