Skip to content

Commit

Permalink
updated validators and demos
Browse files Browse the repository at this point in the history
  • Loading branch information
RonenNess committed Dec 24, 2023
1 parent d4e8ccb commit 9bdef3f
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
84 changes: 84 additions & 0 deletions GeonBit.UI.Examples/GeonBitUI_Examples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ GeonBit.UI is the UI system of the GeonBit project.
- Sliders & Progressbars
- Text input
- Tooltip Text
- File dialogs
- And more...
"));
}
Expand Down Expand Up @@ -880,6 +881,89 @@ Note that in order to use clipping and scrollbar with Panels you need to set the
panel.AddChild(hideCheckbox);
}

// example: text validators
{
// create panel and add to list of panels and manager
Panel panel = new Panel(new Vector2(650, -1));
panels.Add(panel);
UserInterface.Active.AddEntity(panel);

// add title and text
panel.AddChild(new Header("Text Validators"));
panel.AddChild(new HorizontalLine());
panel.AddChild(new Paragraph(@"GeonBit.UI provide some validators you can attach to text inputs:"));

List<TextInput> _withSpaces = new();

// english characters
EnglishCharactersOnly englishValidator;
{
panel.AddChild(new Label("English Characters Only:"));
var textInput = new TextInput(false);
textInput.Validators.Add(new EnglishCharactersOnly(true));
_withSpaces.Add(textInput);
englishValidator = textInput.Validators[0] as EnglishCharactersOnly;
panel.AddChild(textInput);
}

// filename characters
FilenameValidator filenamValidator;
{
panel.AddChild(new Label("Valid Filename Validator:"));
var textInput = new TextInput(false);
textInput.Validators.Add(new FilenameValidator(true));
_withSpaces.Add(textInput);
filenamValidator = textInput.Validators[0] as FilenameValidator;
panel.AddChild(textInput);
}

// slug
SlugValidator slugValidator;
{
panel.AddChild(new Label("Slug Validator:"));
var textInput = new TextInput(false);
textInput.Validators.Add(new SlugValidator(true));
_withSpaces.Add(textInput);
slugValidator = textInput.Validators[0] as SlugValidator;
panel.AddChild(textInput);
}

// only one space
{
panel.AddChild(new Label("Prevent Double Spaces:"));
var textInput = new TextInput(false);
textInput.Validators.Add(new OnlySingleSpaces());
panel.AddChild(textInput);
}

// Numbers only - no decimal
{
panel.AddChild(new Label("Whole Numbers Only:"));
var textInput = new TextInput(false);
textInput.Validators.Add(new NumbersOnly(false));
panel.AddChild(textInput);
}

// Numbers only - with decimal
{
panel.AddChild(new Label("Numbers Only:"));
var textInput = new TextInput(false);
textInput.Validators.Add(new NumbersOnly(true));
panel.AddChild(textInput);
}

// allow spaces
var allowSpaces = new CheckBox("Allow Spaces", isChecked: true);
panel.AddChild(allowSpaces);
allowSpaces.OnValueChange = (Entity _) =>
{
englishValidator.AllowSpaces = allowSpaces.Checked;
filenamValidator.AllowSpaces = allowSpaces.Checked;
slugValidator.AllowSpaces = allowSpaces.Checked;
foreach (var e in _withSpaces) { e.Value = e.Value.Replace(" ", ""); }
};
}

// example: tooltip text
{
// create panel and add to list of panels and manager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public bool AllowSpaces
/// <param name="allowSpaces">If true, will allow spaces.</param>
public FilenameValidator(bool allowSpaces)
{
AllowSpaces = AllowSpaces;
AllowSpaces = allowSpaces;
}

/// <summary>
Expand Down
6 changes: 6 additions & 0 deletions GeonBit.UI/Source/Entities/TextValidators/NumbersOnly.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ public override bool ValidateText(ref string text, string oldText)
return true;
}

// make sure no spaces
if (text.Contains(' '))
{
return false;
}

// will contain value as number
double num;

Expand Down
2 changes: 1 addition & 1 deletion GeonBit.UI/Source/Entities/TextValidators/SlugValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public bool AllowSpaces
/// <param name="allowSpaces">If true, will allow spaces.</param>
public SlugValidator(bool allowSpaces)
{
AllowSpaces = AllowSpaces;
AllowSpaces = allowSpaces;
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,9 @@ If you want to use the new files dialog, you must include the new textures that
### 4.3.0.1

- Added rotation to paragraphs.
- Fixed text input validators to properly allow spaces.
- Fixed number validators to not allow spaces.
- Added example for text validators.

## Credits

Expand Down

0 comments on commit 9bdef3f

Please sign in to comment.