Skip to content
This repository was archived by the owner on Aug 3, 2023. It is now read-only.

Commit 26aac67

Browse files
committed
Block select autocomplete
1 parent 2cbbc52 commit 26aac67

File tree

6 files changed

+766
-20
lines changed

6 files changed

+766
-20
lines changed

BlockToPlace.cs

Lines changed: 165 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.IO;
45
using System.Linq;
56
using System.Text;
67
using System.Threading;
@@ -15,17 +16,35 @@ public static class BlockToPlace
1516

1617
public static Thread loopThread;
1718

19+
public const int MaxResults = 8;
20+
public const int MinCorrect = 1;
21+
public const float SkipPenalty = -0.125f;
22+
1823
private static int startTop;
19-
private static int lastNumbLines;
24+
25+
private static string[] autocomplete;
2026

2127
public static void Init()
2228
{
29+
string[] lines = File.ReadAllLines(Program.baseDir + "Data\\Blocks.txt");
30+
autocomplete = new string[lines.Length];
31+
32+
Parallel.For(0, lines.Length, Util.DefaultParallelOptions, (int i) =>
33+
{
34+
autocomplete[i] = lines[i].Split(',')[1].Split(':')[1];
35+
});
36+
2337
loopThread = new Thread(() => Loop());
2438
loopThread.Start();
2539
}
2640

2741
private static void Loop()
2842
{
43+
string bestAutoComp = string.Empty;
44+
int autoSelected = 0;
45+
46+
string[] autocomp = new string[0];
47+
2948
while (true) {
3049
start:
3150
if (!ShouldBeTakingInput)
@@ -37,35 +56,169 @@ private static void Loop()
3756

3857
if (key == ConsoleKey.Enter) {
3958
ShouldBeTakingInput = false;
59+
for (int i = 0; i < MaxResults + 2; i++) {
60+
Console.SetCursorPosition(0, startTop + i);
61+
Console.WriteLine(new string(' ', Console.WindowWidth));
62+
}
63+
Console.ForegroundColor = ConsoleColor.White;
64+
Console.SetCursorPosition(0, startTop);
65+
Console.WriteLine($"Selected: {inputed}");
66+
Console.ResetColor();
4067
goto start;
4168
}
42-
else if (key == ConsoleKey.Backspace && inputed.Length > 0)
69+
else if (key == ConsoleKey.Escape) {
70+
ShouldBeTakingInput = false;
71+
for (int i = 0; i < MaxResults + 2; i++) {
72+
Console.SetCursorPosition(0, startTop + i);
73+
Console.WriteLine(new string(' ', Console.WindowWidth));
74+
}
75+
inputed = string.Empty;
76+
Console.ForegroundColor = ConsoleColor.White;
77+
Console.SetCursorPosition(0, startTop);
78+
Console.WriteLine($"Selected: None");
79+
Console.ResetColor();
80+
goto start;
81+
} else if (key == ConsoleKey.UpArrow) {
82+
autoSelected--;
83+
if (autoSelected < 0)
84+
autoSelected = 0;
85+
} else if (key == ConsoleKey.DownArrow) {
86+
autoSelected++;
87+
if (autoSelected >= autocomp.Length)
88+
autoSelected = autocomp.Length - 1;
89+
}
90+
else if (key == ConsoleKey.Backspace && inputed.Length > 0) {
4391
inputed = inputed.Substring(0, inputed.Length - 1);
44-
else
92+
autoSelected = 0;
93+
}
94+
else if (key == ConsoleKey.Tab && bestAutoComp != string.Empty) {
95+
inputed = bestAutoComp;
96+
autoSelected = 0;
97+
}
98+
else {
4599
inputed += info.KeyChar;
100+
autoSelected = 0;
101+
}
46102

47103
// clear last render
48-
Console.CursorTop = startTop;
49-
Console.CursorLeft = 0;
50-
for (int i = 0; i < lastNumbLines; i++) {
51-
Console.CursorLeft = 0;
104+
for (int i = 0; i < MaxResults + 2; i++) {
105+
Console.SetCursorPosition(0, startTop + i);
52106
Console.WriteLine(new string(' ', Console.WindowWidth));
53107
}
54108

109+
// get/set autocomplete
110+
autocomp = GetAutoComplete();
111+
if (autocomp.Length > 0)
112+
bestAutoComp = autocomp[Math.Min(autoSelected, autocomp.Length - 1)];
113+
else
114+
bestAutoComp = string.Empty;
115+
55116
// render
56-
Console.CursorTop = startTop;
57-
Console.CursorLeft = 0;
58-
Console.WriteLine(inputed);
117+
Console.SetCursorPosition(0, startTop);
118+
119+
Console.ForegroundColor = ConsoleColor.White;
120+
Console.Write(inputed);
121+
Console.ForegroundColor = ConsoleColor.Gray;
122+
Console.WriteLine(GetRest(bestAutoComp));
123+
Console.ResetColor();
59124

60-
lastNumbLines = 1;
125+
for (int i = 0; i < autocomp.Length; i++) {
126+
Console.SetCursorPosition(0, startTop + 1 + i);
127+
if (i != autoSelected)
128+
Console.ForegroundColor = ConsoleColor.DarkGray;
129+
Console.WriteLine(autocomp[i] + new string(' ', Console.WindowWidth - autocomp[i].Length));
130+
Console.ResetColor();
131+
}
61132
}
62133
}
63134

135+
private static string GetRest(string auto)
136+
{
137+
if (auto.Length < 1 || inputed.Length < 1)
138+
return string.Empty;
139+
140+
bool s = false;
141+
int inpIndex = 0;
142+
for (int j = 0; j < auto.Length; j++) {
143+
if (s == false && auto[j].ToLower() == inputed[inpIndex].ToLower()) {
144+
inpIndex++;
145+
s = true;
146+
}
147+
else if (s == true && auto[j].ToLower() != inputed[inpIndex].ToLower()) {
148+
break;
149+
}
150+
else if (s == true && auto[j].ToLower() == inputed[inpIndex].ToLower())
151+
inpIndex++;
152+
153+
if (inpIndex >= inputed.Length)
154+
break;
155+
}
156+
157+
if (inpIndex < auto.Length)
158+
return auto.Substring(inpIndex);
159+
else
160+
return string.Empty;
161+
}
162+
163+
private static string[] GetAutoComplete()
164+
{
165+
if (inputed.Length < 1)
166+
return new string[0];
167+
168+
List<(string text, float correct)> _res = new List<(string text, float correct)>();
169+
170+
for (int i = 0; i < autocomplete.Length; i++) {
171+
string auto = autocomplete[i];
172+
bool s = false;
173+
float correct = 0f;
174+
int inpIndex = 0;
175+
for (int j = 0; j < auto.Length; j++) {
176+
if (s == false && auto[j].ToLower() == inputed[inpIndex].ToLower()) {
177+
correct++;
178+
inpIndex++;
179+
s = true;
180+
}
181+
else if (s == true && auto[j].ToLower() != inputed[inpIndex].ToLower()) {
182+
break;
183+
}
184+
else if (s == true && auto[j].ToLower() == inputed[inpIndex].ToLower()) {
185+
correct++;
186+
inpIndex++;
187+
}
188+
else
189+
correct += SkipPenalty;
190+
191+
if (inpIndex >= inputed.Length)
192+
break;
193+
}
194+
195+
if (correct >= MinCorrect || correct >= inputed.Length)
196+
_res.Add((auto, correct));
197+
}
198+
199+
_res.Sort(((string text, float correct) a, (string text, float correct) b) =>
200+
{
201+
int compRes = a.correct.CompareTo(b.correct);
202+
if (compRes != 0)
203+
return compRes;
204+
else
205+
return a.text.ToLower().CompareTo(b.text.ToLower());
206+
});
207+
208+
_res.Reverse();
209+
210+
string[] res = new string[Math.Min(MaxResults, _res.Count)];
211+
212+
for (int i = 0; i < res.Length; i++)
213+
res[i] = _res[i].text;
214+
215+
return res;
216+
}
217+
64218
public static string TakeInput()
65219
{
66220
inputed = string.Empty;
67221
startTop = Console.CursorTop;
68-
lastNumbLines = 0;
69222
Util.SetConsoleForeground();
70223
ShouldBeTakingInput = true;
71224
while (ShouldBeTakingInput) Thread.Sleep(1);

BuildPlate_Editor.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@
164164
</ItemGroup>
165165
<ItemGroup>
166166
<Content Include="1051469223874011206.ico" />
167+
<Content Include="Data\Blocks.txt">
168+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
169+
</Content>
167170
<Content Include="Data\Textures\Notfound.png">
168171
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
169172
</Content>

0 commit comments

Comments
 (0)