1using System.Numerics;
2using Fjord.Input;
3using Fjord.Ui;
4using Fjord.Graphics;
5
6namespace Fjord.Scenes;
7
8public class ConsoleScene : Scene
9{
10 string consoleInput = "";
11 float yOffset = 0;
12 int logsLength = 0;
13 float scrollY = 0;
14 float scrollYOffset = 0;
15 bool pressedScroll = false;
16
17 public ConsoleScene(int width, int height) : base(width, height)
18 {
19 ClearColor = UiStyles.Background;
20 UpdateOnlyIfActive = false;
21 }
22
23 public override void Update()
24 {
25 if(MouseInsideScene)
26 {
27 if(Mouse.Pressed(MB.ScrollDown)) {
28 yOffset -= 10;
29 }
30 if(Mouse.Pressed(MB.ScrollUp)) {
31 yOffset += 10;
32 }
33 }
34
35 new UiBuilder(new Vector4(0, yOffset, 0, 0), Mouse.Position)
36 .Title($"Console")
37 .ForEach(Debug.Logs, (val, idx) =>
38 {
39 switch(val.level) {
40 case LogLevel.User: {
41 return new UiText(val.message, new(170, 170, 170, 255));
42 }
43 default: {
44 return new UiDebugLog(val.level, val.time, val.sender, val.message, val.hideInfo, val.repeat);
45 }
46 }
47 })
48 .Render(out int uiHeight);
49
50 // Math.Clamp(yOffset, 0, uiHeight);
51 if(uiHeight > WindowSize.Y) {
52 if(-yOffset < 0) {
53 yOffset = 0;
54 }
55 if(-yOffset > uiHeight - WindowSize.Y + 50) {
56 yOffset = -uiHeight + WindowSize.Y - 50;
57 }
58 } else {
59 yOffset = 0;
60 }
61
62 if(Debug.Logs.Count != logsLength)
63 {
64 yOffset = -uiHeight + WindowSize.Y - 200;
65 }
66
67 var submitCommand = () => {
68 if(consoleInput == "")
69 {
70 return;
71 }
72
73 Debug.Log(LogLevel.User, $"> {consoleInput}");
74 string command = consoleInput.Split(" ")[0];
75 List<object> args = new List<object>();
76
77 string currentWord = "";
78 bool isString = false;
79 string[] boolValues = {"true", "false"};
80
81 void HandleCurrentWord()
82 {
83 if (currentWord != String.Empty)
84 {
85 float value = 0f;
86 if (float.TryParse(currentWord, out value))
87 {
88 args.Add((float)value);
89 }
90 if(boolValues.Contains(currentWord.ToLower())) {
91 args.Add(currentWord.ToLower() == "true");
92 }
93 else
94 {
95 args.Add(currentWord);
96 }
97 }
98 currentWord = "";
99 }
100
101 foreach (char c in String.Join(" ", consoleInput.Split(" ").ToList().Skip(1)))
102 {
103 if (c == '"')
104 {
105 isString = !isString;
106 if (!isString)
107 {
108 HandleCurrentWord();
109 }
110 continue;
111 }
112 if (isString)
113 {
114 currentWord += c;
115 }
116 else if (c != ' ')
117 {
118 currentWord += c;
119 }
120 else
121 {
122 HandleCurrentWord();
123 }
124 }
125 if (currentWord != String.Empty)
126 {
127 HandleCurrentWord();
128 }
129
130 Debug.PerformCommand(command, args.ToArray());
131 consoleInput = "";
132 };
133
134 logsLength = Debug.Logs.Count;
135
136 FUI.OverMousePosition = Mouse.Position;
137
138 float height = 0;
139 if(consoleInput != "" && Debug.commands.Keys.ToList().Where((command) => command == consoleInput).ToList().Count != 1 )
140 {
141 foreach(var i in Debug.commands.Keys.ToList().Where((command) => command.Contains(consoleInput)))
142 {
143 if(GlobalKeyboard.Pressed(Key.TAB))
144 {
145 consoleInput = i;
146 }
147
148 FUI.ButtonExt(new Vector2(10, WindowSize.Y - 40 - height - 55), i, () => {consoleInput = i + " ";}, out Vector2 bSize);
149 height += bSize.Y;
150 }
151 }
152
153 float h = -uiHeight + WindowSize.Y - 70;
154
155 if(pressedScroll && !GlobalMouse.Down(MB.Left))
156 {
157 pressedScroll = false;
158 }
159
160 float hh = WindowSize.Y / (Math.Abs(h) + WindowSize.Y);
161 if(pressedScroll)
162 {
163 scrollY = Mouse.Position.Y + scrollYOffset;
164 scrollY = Math.Clamp(scrollY, 0, WindowSize.Y - (hh * WindowSize.Y));
165 yOffset = (scrollY / (WindowSize.Y - (hh * WindowSize.Y))) * (-uiHeight + WindowSize.Y - 70);
166 } else {
167 scrollY = (yOffset / (-uiHeight + WindowSize.Y - 70)) * (WindowSize.Y - (hh * WindowSize.Y));
168 }
169
170 FUI.TextFieldExt(new(10, WindowSize.Y - 50), WindowSize.X - 20, "consolein", consoleInput, (val) => {consoleInput = val;}, (val) => submitCommand(), null, out Vector2 size);
171 // FUI.Button(new(Math.Min(size.X + 20, WindowSize.X - 88), WindowSize.Y - 50), "Send", submitCommand);
172
173 if(h < 0)
174 {
175 FUI.ButtonExt(new Vector4(WindowSize.X - 20, scrollY, 15, hh * WindowSize.Y), "", () => {
176 pressedScroll = true;
177 scrollYOffset = scrollY - Mouse.Position.Y;
178 });
179 }
180 FUI.OverMousePosition = null;
181 }
182}