Skip to content

enlarge ui and add suggestions #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions demo/.idea/.idea.SofiaConsole/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion demo/SofiaConsole.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Godot.NET.Sdk/4.1.1">
<Project Sdk="Godot.NET.Sdk/4.2.2-rc.2">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<EnableDynamicLoading>true</EnableDynamicLoading>
Expand Down
142 changes: 129 additions & 13 deletions demo/addons/sofiaconsole/Console.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,21 @@ public partial class Console : Node
public static Console Instance;
public List<ConsoleCommandReference> Commands = new();
private readonly List<string> _commandHistory = new();
private List<string> _suggestions = new List<string>();
private int _currentSuggestionIndex = -1;

public bool Open;

[Export] private int _maxSuggestions = 15;
[Export] private CanvasLayer _consoleCanvas;
[Export] private Panel _background;
[Export] private Button _closeButton;
[Export] private LineEdit _commandInput;
[Export] private Button _commandSendButton;
[Export] private ScrollContainer _historyScrollContainer;
[Export] private VBoxContainer _historyContent;
[Export] private VBoxContainer _suggestionsContent;
[Export] private PanelContainer _suggestionsPanel;

public override void _EnterTree()
{
Expand All @@ -36,13 +41,19 @@ public override void _EnterTree()

_closeButton.Pressed += () => { SetConsole(false); };
_commandSendButton.Pressed += () => { ProcessCommand(_commandInput.Text); };
_commandInput.TextSubmitted += ProcessCommand;
_commandInput.TextSubmitted += (t) =>
{
ProcessCommand(t);
UpdateUi(true);
};

LoadCommands();

Print("[SofiaConsole] Version 1.2.0", PrintType.Success);
Space();

GenerateSuggestionsNode();

GD.Print("[SofiaConsole] Done");
}

Expand All @@ -63,22 +74,61 @@ public override void _Input(InputEvent @event)
{
ToggleConsole();
}

// Press Up to toggle between previous commands
if (eventKey.Keycode == Key.Up && _commandInput.HasFocus() && _commandHistory.Count > 0)

if (eventKey.Keycode == Key.Down && _commandInput.HasFocus() && _suggestions.Count > 0)
{
_currentSuggestionIndex = (_currentSuggestionIndex + 1) % _suggestions.Count;
}
else if (eventKey.Keycode == Key.Up && _commandInput.HasFocus() && _suggestions.Count > 0)
{
_currentSuggestionIndex = (_currentSuggestionIndex - 1) % _suggestions.Count;
if (_currentSuggestionIndex < 0)
_currentSuggestionIndex = _suggestions.Count - 1;
}
else if (eventKey.Keycode == Key.Tab || eventKey.Keycode == Key.Enter && _commandInput.HasFocus() && _suggestions.Count > 0 && _currentSuggestionIndex < _suggestions.Count)
{
var historyIndex = _commandHistory.FindIndex(x => x == _commandInput.Text);
if (historyIndex == -1)
if (_suggestions.Count > 0 && _currentSuggestionIndex >= 0)
{
historyIndex = _commandHistory.Count;
_commandInput.Text = "";
_commandInput.InsertTextAtCaret(_suggestions[_currentSuggestionIndex]);
_commandInput.GrabFocus();
}
}
}

if (@event is InputEventKey inputEventKey)
{
string inputText = _commandInput.Text;

if (inputText.Trim() == "")
{
_suggestions.Clear();

if (historyIndex == 0)
// Press Up to toggle between previous commands
if (inputEventKey.Keycode == Key.Up && _commandInput.HasFocus() && _commandHistory.Count > 0)
{
historyIndex = _commandHistory.Count;
var historyIndex = _commandHistory.FindIndex(x => x == _commandInput.Text);
if (historyIndex == -1)
{
historyIndex = _commandHistory.Count;
}

if (historyIndex == 0)
{
historyIndex = _commandHistory.Count;
}

_commandInput.Text = _commandHistory[historyIndex - 1];
}

_commandInput.Text = _commandHistory[historyIndex - 1];
}
else
{
// Filter suggestions
_suggestions = Commands
.Where(cmd => cmd.Command.StartsWith(inputText))
.OrderBy(cmd => cmd.Command.IndexOf(inputText, StringComparison.CurrentCulture))
.Select(cmd => cmd.Command)
.ToList();
}
}

Expand All @@ -88,6 +138,71 @@ public override void _Input(InputEvent @event)
_commandInput.ReleaseFocus();
_commandSendButton.ReleaseFocus();
}

UpdateUi();
}

// Generate suggestions node when enter tree, to improve performance.
private void GenerateSuggestionsNode()
{
for (int i = 0; i < _maxSuggestions; i++)
{
var newLabel = new Label
{
Text = "",
Theme = new Theme
{
DefaultFontSize = 30
}
};

_suggestionsContent.AddChild(newLabel);
newLabel.Hide();
}
}

private void UpdateUi(bool close = false)
{
if (!Open) return;
if (close) return;

foreach (var node in _suggestionsContent.GetChildren())
{
if (node is Label label)
{
label.Hide();
}
}

if (_suggestions.Count == 0)
{
_suggestionsPanel.Hide();
return;
}

_suggestionsPanel.Show();

for (int i = 0; i < Mathf.Min(_maxSuggestions, _suggestions.Count); i++)
{
Label suggestionLabel = _suggestionsContent.GetChild(i) as Label;
if (suggestionLabel == null) continue;

suggestionLabel.Text = _suggestions[i];
suggestionLabel.Show();

if (_currentSuggestionIndex >= 0 && _suggestions.Count > 0 &&
_currentSuggestionIndex < _suggestions.Count &&
_suggestions[_currentSuggestionIndex] == _suggestions[i])
{
// Background color for selected suggestion
suggestionLabel.AddThemeStyleboxOverride("normal", GD.Load<StyleBoxFlat>("res://addons/sofiaconsole/SelectedSuggestion.tres"));
}
else
{
// Background color for suggestion
suggestionLabel.RemoveThemeStyleboxOverride("normal");
}
}
}

public void ToggleConsole()
Expand All @@ -101,7 +216,8 @@ private void SetConsole(bool open)

_consoleCanvas.Visible = Open;
_background.MouseFilter = Open ? Control.MouseFilterEnum.Stop : Control.MouseFilterEnum.Ignore;

GetTree().Paused = Open;

if (Open)
{
_commandInput.GrabFocus();
Expand Down Expand Up @@ -229,7 +345,7 @@ public async void Print(string text, PrintType type = PrintType.Default)
Text = text,
Theme = new Theme
{
DefaultFontSize = 12
DefaultFontSize = 30
}
};

Expand Down
91 changes: 61 additions & 30 deletions demo/addons/sofiaconsole/Console.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=13 format=3 uid="uid://dyya4j4ywm1k6"]
[gd_scene load_steps=14 format=3 uid="uid://dyya4j4ywm1k6"]

[ext_resource type="Script" path="res://addons/sofiaconsole/Console.cs" id="1_dw41g"]
[ext_resource type="Script" path="res://addons/sofiaconsole/Commands/FpsCounterCommand.cs" id="2_nkfel"]
Expand Down Expand Up @@ -39,6 +39,9 @@ content_margin_right = 10.0
content_margin_bottom = 10.0
bg_color = Color(0.133333, 0.133333, 0.133333, 0)

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_wpanm"]
bg_color = Color(0.0900985, 0.0900985, 0.0900985, 1)

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_25al0"]
content_margin_left = 10.0
content_margin_top = 10.0
Expand All @@ -58,15 +61,18 @@ outline_color = Color(0, 0, 0, 1)
shadow_size = 5
shadow_color = Color(0, 0, 0, 1)

[node name="Console" type="Node" node_paths=PackedStringArray("_consoleCanvas", "_background", "_closeButton", "_commandInput", "_commandSendButton", "_historyScrollContainer", "_historyContent")]
[node name="Console" type="Node" node_paths=PackedStringArray("_consoleCanvas", "_background", "_closeButton", "_commandInput", "_commandSendButton", "_historyScrollContainer", "_historyContent", "_suggestionsContent", "_suggestionsPanel")]
process_mode = 3
script = ExtResource("1_dw41g")
_consoleCanvas = NodePath("ConsoleCanvas")
_background = NodePath("ConsoleCanvas/Background")
_closeButton = NodePath("ConsoleCanvas/Background/Window/Content/Header/HBoxContainer/CloseButton")
_commandInput = NodePath("ConsoleCanvas/Background/Window/Content/Input/HBoxContainer/CommandInput")
_commandSendButton = NodePath("ConsoleCanvas/Background/Window/Content/Input/HBoxContainer/CommandSendButton")
_historyScrollContainer = NodePath("ConsoleCanvas/Background/Window/Content/History/ScrollContainer")
_historyContent = NodePath("ConsoleCanvas/Background/Window/Content/History/ScrollContainer/Content")
_closeButton = NodePath("ConsoleCanvas/Background/MarginContainer/Window/Content/Header/HBoxContainer/CloseButton")
_commandInput = NodePath("ConsoleCanvas/Background/MarginContainer/Window/Content/Input/HBoxContainer/CommandInput")
_commandSendButton = NodePath("ConsoleCanvas/Background/MarginContainer/Window/Content/Input/HBoxContainer/CommandSendButton")
_historyScrollContainer = NodePath("ConsoleCanvas/Background/MarginContainer/Window/Content/History/ScrollContainer")
_historyContent = NodePath("ConsoleCanvas/Background/MarginContainer/Window/Content/History/ScrollContainer/Content")
_suggestionsContent = NodePath("ConsoleCanvas/Background/MarginContainer/Window/Content/History/SuggestionsPanel/Suggestions/Suggestions")
_suggestionsPanel = NodePath("ConsoleCanvas/Background/MarginContainer/Window/Content/History/SuggestionsPanel")

[node name="ConsoleCanvas" type="CanvasLayer" parent="."]
layer = 128
Expand All @@ -81,23 +87,24 @@ grow_vertical = 2
mouse_filter = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_cvk4f")

[node name="Window" type="Panel" parent="ConsoleCanvas/Background"]
custom_minimum_size = Vector2(600, 400)
[node name="MarginContainer" type="MarginContainer" parent="ConsoleCanvas/Background"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -350.0
offset_top = -250.0
offset_right = 350.0
offset_bottom = 250.0
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
theme_override_constants/margin_left = 50
theme_override_constants/margin_top = 50
theme_override_constants/margin_right = 50
theme_override_constants/margin_bottom = 50

[node name="Window" type="Panel" parent="ConsoleCanvas/Background/MarginContainer"]
custom_minimum_size = Vector2(600, 400)
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_lpjvy")

[node name="Content" type="VBoxContainer" parent="ConsoleCanvas/Background/Window"]
[node name="Content" type="VBoxContainer" parent="ConsoleCanvas/Background/MarginContainer/Window"]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
Expand All @@ -106,63 +113,87 @@ grow_horizontal = 2
grow_vertical = 2
theme_override_constants/separation = 0

[node name="Header" type="PanelContainer" parent="ConsoleCanvas/Background/Window/Content"]
[node name="Header" type="PanelContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content"]
custom_minimum_size = Vector2(0, 80)
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_t5a6w")

[node name="HBoxContainer" type="HBoxContainer" parent="ConsoleCanvas/Background/Window/Content/Header"]
[node name="HBoxContainer" type="HBoxContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/Header"]
layout_mode = 2
theme_override_constants/separation = 15
alignment = 1

[node name="Label" type="Label" parent="ConsoleCanvas/Background/Window/Content/Header/HBoxContainer"]
[node name="Label" type="Label" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/Header/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
theme_override_font_sizes/font_size = 40
text = "Console"

[node name="CloseButton" type="Button" parent="ConsoleCanvas/Background/Window/Content/Header/HBoxContainer"]
[node name="CloseButton" type="Button" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/Header/HBoxContainer"]
custom_minimum_size = Vector2(65, 0)
layout_mode = 2
size_flags_horizontal = 8
focus_mode = 0
text = "Close"

[node name="History" type="PanelContainer" parent="ConsoleCanvas/Background/Window/Content"]
[node name="History" type="PanelContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content"]
layout_mode = 2
size_flags_vertical = 3
theme_override_styles/panel = SubResource("StyleBoxFlat_rquom")

[node name="ScrollContainer" type="ScrollContainer" parent="ConsoleCanvas/Background/Window/Content/History"]
[node name="ScrollContainer" type="ScrollContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/History"]
layout_mode = 2
size_flags_vertical = 3
theme_override_styles/panel = SubResource("StyleBoxFlat_fbsm3")
horizontal_scroll_mode = 0
vertical_scroll_mode = 2

[node name="Content" type="VBoxContainer" parent="ConsoleCanvas/Background/Window/Content/History/ScrollContainer"]
[node name="Content" type="VBoxContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/History/ScrollContainer"]
layout_mode = 2
size_flags_horizontal = 3
theme_override_constants/separation = 0

[node name="Input" type="PanelContainer" parent="ConsoleCanvas/Background/Window/Content"]
[node name="SuggestionsPanel" type="PanelContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/History"]
visible = false
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_wpanm")

[node name="Suggestions" type="ScrollContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/History/SuggestionsPanel"]
layout_mode = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_fbsm3")
horizontal_scroll_mode = 0
vertical_scroll_mode = 2

[node name="Suggestions" type="VBoxContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/History/SuggestionsPanel/Suggestions"]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
theme_override_constants/separation = 0
alignment = 2

[node name="Input" type="PanelContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content"]
layout_mode = 2
size_flags_vertical = 8
theme_override_styles/panel = SubResource("StyleBoxFlat_25al0")

[node name="HBoxContainer" type="HBoxContainer" parent="ConsoleCanvas/Background/Window/Content/Input"]
[node name="HBoxContainer" type="HBoxContainer" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/Input"]
custom_minimum_size = Vector2(0, 40)
layout_mode = 2
theme_override_constants/separation = 15

[node name="CommandInput" type="LineEdit" parent="ConsoleCanvas/Background/Window/Content/Input/HBoxContainer"]
[node name="CommandInput" type="LineEdit" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/Input/HBoxContainer"]
layout_mode = 2
size_flags_horizontal = 3
theme_override_font_sizes/font_size = 40
placeholder_text = "Enter \"help\" for a list of commands"
flat = true
caret_blink = true

[node name="CommandSendButton" type="Button" parent="ConsoleCanvas/Background/Window/Content/Input/HBoxContainer"]
[node name="CommandSendButton" type="Button" parent="ConsoleCanvas/Background/MarginContainer/Window/Content/Input/HBoxContainer"]
custom_minimum_size = Vector2(80, 0)
layout_mode = 2
size_flags_horizontal = 8
focus_mode = 0
text = "Send"

[node name="BuiltinCommands" type="Node" parent="."]
Expand Down
Loading