Skip to content

Commit

Permalink
🌟 functional Dialog element | add enum to list and clarify outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
MorganKryze committed Mar 26, 2024
1 parent 958d2b7 commit 467bcd1
Show file tree
Hide file tree
Showing 2 changed files with 183 additions and 36 deletions.
186 changes: 150 additions & 36 deletions src/ConsoleAppVisuals/elements/interactive_elements/Dialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace ConsoleAppVisuals.InteractiveElements;

/// <summary>
/// A <see cref="Dialog"/> is an interactive element that displays a dialog bow with one or two choices.
/// See <see cref="DialogOption"/> for the possible outputs of a dialog.
/// </summary>
/// <remarks>
/// For more information, refer to the following resources:
Expand All @@ -14,7 +15,7 @@ namespace ConsoleAppVisuals.InteractiveElements;
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public class Dialog : InteractiveElement<int>
public class Dialog : InteractiveElement<DialogOption>
{
#region Fields
private List<string> _lines;
Expand All @@ -33,7 +34,7 @@ public class Dialog : InteractiveElement<int>
/// The width ratio between the two options of the Dialog.
/// It ensures that the options are not too close to each other.
/// </summary>
private const double WIDTH_RATIO = 1.1;
private const double WIDTH_RATIO = 1.2;
#endregion

#region Properties
Expand Down Expand Up @@ -97,6 +98,7 @@ public class Dialog : InteractiveElement<int>
#region Constructor
/// <summary>
/// A <see cref="Dialog"/> is an interactive element that displays a dialog bow with one or two choices.
/// '0' index represents the left option and '1' index represents the right option.
/// </summary>
/// <param name="lines">The text to display.</param>
/// <param name="leftOption">The text of the left option. Null for no button.</param>
Expand Down Expand Up @@ -314,31 +316,15 @@ public void RemoveLine(int index)
{
if (index < 0 || index >= _lines.Count)
{
throw new ArgumentOutOfRangeException("The index is out of range.");
throw new ArgumentOutOfRangeException(nameof(index), "The index is out of range.");
}
_lines.RemoveAt(index);
Build();
}
#endregion

/// <summary>
/// Renders the Dialog.
/// </summary>
#region Rendering
[Visual]
protected override void RenderElementActions()
{
// TODO: UPDATE Code to implement new feature
Build();
Core.WriteMultiplePositionedLines(
false,
TextAlignment,
Placement,
false,
Line,
_textToDisplay!.ToArray()
);
Window.Freeze();
}

private void Build()
{
if (!CheckIntegrity())
Expand Down Expand Up @@ -387,31 +373,159 @@ private void Build()
+ new string(Borders.Horizontal, MaxLineLength + EMBED_MARGIN)
+ Borders.BottomRight
);

void AddOptions()
{
_textToDisplay!.Add(
$"{Borders.Vertical} " + new string(' ', MaxLineLength) + $" {Borders.Vertical}"
);

string optionLine =
$"{Borders.Vertical} " + new string(' ', MaxLineLength) + $" {Borders.Vertical}";

if (_leftOption is not null)
{
optionLine = optionLine.Remove(2, _leftOption.Length);
optionLine = optionLine.Insert(2, _leftOption);
}

if (_rightOption is not null)
{
int insertPosition = optionLine.Length - _rightOption.Length - 2;
optionLine = optionLine.Remove(insertPosition, _rightOption.Length);
optionLine = optionLine.Insert(insertPosition, _rightOption);
}

_textToDisplay.Add(optionLine);
}
}

private void AddOptions()
/// <summary>
/// Renders the Dialog.
/// </summary>
[Visual]
protected override void RenderElementActions()
{
_textToDisplay!.Add(
$"{Borders.Vertical} " + new string(' ', MaxLineLength) + $" {Borders.Vertical}"
);
Build();

string optionLine =
$"{Borders.Vertical} " + new string(' ', MaxLineLength) + $" {Borders.Vertical}";
DialogOption optionSelectedIndex = SetDefaultValue();

if (_leftOption is not null)
bool loop = true;
while (loop)
{
optionLine = optionLine.Remove(2, _leftOption.Length);
optionLine = optionLine.Insert(2, _leftOption);
UpdateOptionSelected();
Core.WriteMultiplePositionedLines(
false,
TextAlignment,
Placement,
false,
Line,
_textToDisplay!.ToArray()
);

switch (Console.ReadKey(true).Key)
{
case ConsoleKey.Enter:
SendResponse(
this,
new InteractionEventArgs<DialogOption>(Status.Selected, optionSelectedIndex)
);
loop = false;
break;

case ConsoleKey.Escape:
SendResponse(
this,
new InteractionEventArgs<DialogOption>(Status.Escaped, DialogOption.None)
);
loop = false;
break;

case ConsoleKey.Q:
case ConsoleKey.LeftArrow:
SwitchOptions();
break;

case ConsoleKey.D:
case ConsoleKey.RightArrow:
SwitchOptions();
break;

case ConsoleKey.Tab:
SwitchOptions();
break;
}
}
DialogOption SetDefaultValue()
{
if (_leftOption is null && _rightOption is null)
{
return DialogOption.None;
}
else if (_leftOption is not null && _rightOption is null)
{
return DialogOption.Left;
}
else if (_leftOption is null && _rightOption is not null)
{
return DialogOption.Right;
}

if (_rightOption is not null)
return DialogOption.Left;
}
void SwitchOptions()
{
if (_leftOption is not null && _rightOption is not null)
{
optionSelectedIndex =
optionSelectedIndex == DialogOption.Left
? DialogOption.Right
: DialogOption.Left;
}
}
void UpdateOptionSelected()
{
int insertPosition = optionLine.Length - _rightOption.Length - 2;
optionLine = optionLine.Remove(insertPosition, _rightOption.Length);
optionLine = optionLine.Insert(insertPosition, _rightOption);
if (_leftOption is not null || _rightOption is not null)
{
string optionLine =
$"{Borders.Vertical} "
+ new string(' ', MaxLineLength)
+ $" {Borders.Vertical}";

if (_rightOption is not null && optionSelectedIndex == DialogOption.Right)
{
int insertPosition = optionLine.Length - 1 - _rightOption.Length - 1 - 2;
optionLine = optionLine.Remove(insertPosition, _rightOption.Length + 2);
optionLine = optionLine.Insert(
insertPosition,
Core.NEGATIVE_ANCHOR + " " + _rightOption + " " + Core.NEGATIVE_ANCHOR
);
}
else if (_rightOption is not null && optionSelectedIndex == DialogOption.Left)
{
int insertPosition = optionLine.Length - 1 - _rightOption.Length - 1 - 2;
optionLine = optionLine.Remove(insertPosition, _rightOption.Length + 2);
optionLine = optionLine.Insert(insertPosition, " " + _rightOption + " ");
}

if (_leftOption is not null && optionSelectedIndex == DialogOption.Left)
{
optionLine = optionLine.Remove(2, _leftOption.Length + 2);
optionLine = optionLine.Insert(
2,
Core.NEGATIVE_ANCHOR + " " + _leftOption + " " + Core.NEGATIVE_ANCHOR
);
}
else if (_leftOption is not null && optionSelectedIndex == DialogOption.Right)
{
optionLine = optionLine.Remove(2, _leftOption.Length + 2);
optionLine = optionLine.Insert(2, " " + _leftOption + " ");
}

_textToDisplay![^2] = optionLine;
}
}

_textToDisplay.Add(optionLine);
}

#endregion
}
33 changes: 33 additions & 0 deletions src/ConsoleAppVisuals/enums/DialogOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
GNU GPL License 2024 MorganKryze(Yann Vidamment)
For full license information, please visit: https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/LICENSE
*/
namespace ConsoleAppVisuals.Enums;

/// <summary>
/// The <see cref="DialogOption"/> enum defines the outputs of a dialog.
/// </summary>
/// <remarks>
/// For more information, refer to the following resources:
/// <list type="bullet">
/// <item><description><a href="https://morgankryze.github.io/ConsoleAppVisuals/">Documentation</a></description></item>
/// <item><description><a href="https://github.com/MorganKryze/ConsoleAppVisuals/blob/main/example/">Example Project</a></description></item>
/// </list>
/// </remarks>
public enum DialogOption
{
/// <summary>
/// No options are set or escape pressed.
/// </summary>
None,

/// <summary>
/// Left option selected.
/// </summary>
Left,

/// <summary>
/// Right option selected.
/// </summary>
Right,
}

0 comments on commit 467bcd1

Please sign in to comment.