-
Notifications
You must be signed in to change notification settings - Fork 3
/
PartSearch.cs
302 lines (245 loc) · 10.5 KB
/
PartSearch.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
using System;
using System.Collections.Generic;
using System.Linq;
using KSP.IO;
using UnityEngine;
namespace PartSearch
{
static class I
{
private static GameObject gameObject;
public static T AddI<T>(string name) where T : Component
{
if (gameObject == null)
{
gameObject = new GameObject(name, typeof(T));
GameObject.DontDestroyOnLoad(gameObject);
return gameObject.GetComponent<T>();
}
else
{
return gameObject.GetComponent<T>() ?? gameObject.AddComponent<T>();
}
}
}
public class PartSearch : KSP.Testing.UnitTest
{
public PartSearch() : base()
{
I.AddI<PSBehaviour>("PSImmortal");
}
}
[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class PSBehaviour : MonoBehaviour
{
/*
* Things to note:
*
* The editor decides what to display based on PartLoader.LoadedPartList
*
* Calling EditorPartList.Instance.Refresh() manually updates the editorfrom the above source
*
* EditorPartList.Instance.categorySelected determines which category the user currently has up
*/
private const String WND_POSITION = "PartSearchWndPos";
private Dictionary<AvailablePart, PartCategories> master = null;
private TextWithListener text;
private GameScenes gameScene;
private PartCategories panel;
private bool categorize = true;
//private static Texture2D back;
private Rect wndRect = new Rect(400, 40, 300, 65);
private string key = "space";
private string keyMod = "left ctrl";
private ApplicationLauncherButton tbButton;
private Boolean showGUI = false;
private bool setFocus = true;
private PluginConfiguration config;
private EditorPartListFilter filter;
public void Start()
{
if (text == null) // create a searchText box with listener, see below
{
text = new TextWithListener(1, "") {
textChangedListener = (id, s, oldlength, newlength) =>
{
EditorPartList.Instance.SelectTab(EditorPartList.Instance.categorySelected); // Selects the first page of the current category
EditorPartList.Instance.Refresh();
}
};
}
gameScene = HighLogic.LoadedScene;
config = PluginConfiguration.CreateForType<PartSearch>();
config.load();
wndRect = config.GetValue(WND_POSITION, new Rect(400, 40, 300, 65));
key = config.GetValue("PartSearchKey", "space");
keyMod = config.GetValue("PartSearchKeyMod", "left ctrl");
filter = new EditorPartListFilter("PartSearchFilter", p => Search(p, text.GetText()));
GameEvents.onGUIApplicationLauncherReady.Add(OnGUIAppLauncherReady);
GameEvents.onGameSceneLoadRequested.Add(OnGameSceneLoadRequested);
//byte[] bit = Properties.Resources.eraser_small; // get button image from resources
//back = new Texture2D(25, 25); // make new texture
//back.LoadImage(bit); // load image into texture
}
public void Awake() {
RenderingManager.AddToPostDrawQueue(0, OnDraw);
}
public void Update()
{
if (master == null && PartLoader.LoadedPartsList != null && HighLogic.LoadedSceneIsEditor) // master part list not initialized
{
master = new Dictionary<AvailablePart, PartCategories>(); // create master part list
PartLoader.LoadedPartsList.ForEach(x => master.Add(x, x.category)); // populate master part list, the value is the category of the part
}
}
void OnGUIAppLauncherReady() {
if (ApplicationLauncher.Ready && tbButton == null) {
tbButton = ApplicationLauncher.Instance.
AddModApplication(OnToggleOn, OnToggleOff, null, null, null, null,
ApplicationLauncher.AppScenes.VAB | ApplicationLauncher.AppScenes.SPH,
GameDatabase.Instance.GetTexture("PartSearch/Textures/tbbutton", false));
}
}
void OnGameSceneLoadRequested(GameScenes scene) {
if (tbButton != null) {
Destroy();
}
}
private void OnToggleOn() {
DisableCategories(categorize);
EditorPartList.Instance.ExcludeFilters.AddFilter(filter);
showGUI = true;
setFocus = true;
}
private void OnToggleOff() {
DisableCategories(false);
text.SetText("");
text.Fire();
showGUI = false;
EditorPartList.Instance.ExcludeFilters.RemoveFilter(filter);
}
public void OnDraw()
{
if (Input.GetKey(keyMod) && Input.GetKeyDown(key)) {
if(showGUI) setFocus=true;
else tbButton.SetTrue();
}
if (gameScene != HighLogic.LoadedScene && text != null && HighLogic.LoadedSceneIsEditor) { // scene changed and there's searchText in the box and the new scene is the editor {
text.SetText("");
}
if (showGUI && !EditorLogic.editorLocked && HighLogic.LoadedSceneIsEditor && EditorLogic.fetch)
{
GUI.skin = EditorLogic.fetch.shipBrowserSkin;// AssetBase.GetGUISkin("OrbitMapSkin");
if (EditorPartList.Instance.categorySelected != (int) panel) // user clicked new panel
{
if (text != null) {
text.Fire();
}
panel = (PartCategories)EditorPartList.Instance.categorySelected;
}
wndRect = GUILayout.Window("Hello".GetHashCode(), wndRect, id => {
GUILayout.BeginHorizontal();
text.Draw(); // draw our textbox, see below
if (setFocus) {
GUI.FocusControl("PartSearchTextbox");
setFocus = false;
}
if (GUILayout.Button(new GUIContent("C", "Clear search"))) {
text.SetText("");
}
bool test = categorize;
categorize = GUILayout.Toggle(categorize,new GUIContent("∞", "Search all categories"), GUI.skin.button);
GUILayout.EndHorizontal();
if (categorize != test) {// if category button changes, fire the listener
DisableCategories(categorize);
text.Fire();
}
GUI.DragWindow();
}, "Search");
GUI.skin = HighLogic.Skin;
}
gameScene = HighLogic.LoadedScene; // refresh _scene to current scene
}
public void OnDestroy() {
Destroy();
}
private void Destroy() {
DisableCategories(false);
config.SetValue(WND_POSITION, wndRect);
config.SetValue("PartSearchKey", key);
config.SetValue("PartSearchKeyMod", keyMod);
config.save();
if (tbButton != null) {
ApplicationLauncher.Instance.RemoveModApplication(tbButton);
tbButton = null;
}
GameEvents.onGUIApplicationLauncherReady.Remove(OnGUIAppLauncherReady);
GameEvents.onGameSceneLoadRequested.Remove(OnGameSceneLoadRequested);
}
private void DisableCategories(bool disable) {
if (disable) {
EditorPartList.Instance.HideTabs();
PartLoader.LoadedPartsList.ForEach(
x => x.category = (PartCategories)EditorPartList.Instance.categorySelected);
} else {
EditorPartList.Instance.ShowTabs();
PartLoader.LoadedPartsList.ForEach(
x => x.category = master[x]);
}
EditorPartList.Instance.SelectTab(EditorPartList.Instance.categorySelected); // Selects the first page of the current category
EditorPartList.Instance.Refresh();
}
private bool Search(AvailablePart part, string searchText) {
string[] words = searchText.Split(" ".ToCharArray());
bool ret = false;
foreach (string word in words) {
ret |= part.title.ToLower().Contains(word.ToLower()) ||
part.partPrefab.GetType().FullName.Contains(word.ToLower()) ||
part.moduleInfo.ToLower().Contains(word.ToLower()) ||
part.description.ToLower().Contains(word.ToLower());
}
return ret;
}
}
public class TextWithListener {
public delegate void TextChanged(int id, string text, int oldlength, int newlength); // searchText change delegate definition
public TextChanged textChangedListener = null; // instance's delegate
private string lastText = "";
private string currentText = "";
private Rect pos;
private int _id = 0;
public TextWithListener(int id, string startText)
{
currentText = startText;
_id = id;
}
public void Draw()
{
if (currentText != lastText) // searchText changed
{
if (textChangedListener != null)
textChangedListener(_id, currentText, lastText.Length, currentText.Length); // fire the listener
lastText = currentText;
}
pos = new Rect(EditorPanels.Instance.partsPanelWidth + 10, Screen.height - EditorPanels.Instance.partsPanelWidth / 3 - 8, 100, 25); // calculate position and store it for getPosition()
GUI.SetNextControlName("PartSearchTextbox");
currentText = GUILayout.TextField(currentText, GUI.skin.textField, GUILayout.MinWidth(200));
}
public void Fire() // manually fire the listener by calling this function
{
if (textChangedListener != null)
textChangedListener(_id, currentText, lastText.Length, currentText.Length);
}
public void SetText(string text)
{
currentText = text;
}
public String GetText() {
return currentText;
}
public Rect GetPosition()
{
return pos;
}
}
}