forked from allista/AT_Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimplePartFilter.cs
91 lines (84 loc) · 3.39 KB
/
SimplePartFilter.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
// SimplePartCategorizer.cs
//
// Author:
// Allis Tauri <[email protected]>
//
// Copyright (c) 2017 Allis Tauri
using System;
using System.Linq;
using UnityEngine;
using RUI.Icons.Selectable;
using KSP.UI;
using KSP.UI.Screens;
using System.Collections.Generic;
namespace AT_Utils
{
/// <summary>
/// This is the base class for easy creaton of custom part filters.
/// Its main purpouse is to allow creation of dynamic filters responsive
/// to in-game part modifications.
///
/// The code is adapted from the RealChute mod by Christophe Savard (stupid_chris):
/// https://github.com/StupidChris/RealChute/blob/master/RealChute/RCFilterManager.cs
/// Many thanks to Chris for figuring this out so fast!
/// </summary>
public abstract class SimplePartFilter : MonoBehaviour
{
protected List<Type> MODULES;
protected string CATEGORY = "Filter by Function";
protected string SUBCATEGORY = "";
protected string FOLDER = "";
protected string ICON = "";
void Awake()
{
GameEvents.onGUIEditorToolbarReady.Add(add_filter);
}
protected abstract bool filter(AvailablePart part);
static void set_modules_icon(List<Type> modules, Icon icon)
{
if(modules != null && modules.Count > 0)
{
PartCategorizer.Instance.filters
.Find(f => f.button.categoryName == "Filter by Module")
.subcategories.FindAll(s =>
{
var cat_name = string.Join("", s.button.categoryName.Split());
return modules.Any(m => m.Name == cat_name);
})
.ForEach(c => c.button.SetIcon(icon));
}
}
static Icon load_icon(string icon_name, string folder)
{
if(PartCategorizer.Instance.iconLoader.iconDictionary.ContainsKey(icon_name))
return PartCategorizer.Instance.iconLoader.GetIcon(icon_name);
var icon_path = folder+"/"+icon_name;
var icon = GameDatabase.Instance.GetTexture(icon_path, false);
var icon_s = GameDatabase.Instance.GetTexture(icon_path+"_selected", false) ?? icon;
var selectable_icon = new Icon(icon_name, icon, icon_s, icon == icon_s);
PartCategorizer.Instance.iconLoader.icons.Add(selectable_icon);
PartCategorizer.Instance.iconLoader.iconDictionary.Add(icon_name, selectable_icon);
return selectable_icon;
}
protected virtual void add_filter()
{
if(string.IsNullOrEmpty(ICON) ||
string.IsNullOrEmpty(CATEGORY) ||
string.IsNullOrEmpty(SUBCATEGORY))
return;
//load the icon
var icon = load_icon(ICON, FOLDER);
//get category
var category = PartCategorizer.Instance.filters
.Find(f => f.button.categoryName == CATEGORY);
//add custom function filter
PartCategorizer.AddCustomSubcategoryFilter(category, SUBCATEGORY, icon, filter);
//Apparently needed to make sure the icon actually shows at first
var button = category.button.activeButton;
button.Value = false;
button.Value = true;
//set icon(s) for all the modules
set_modules_icon(MODULES, icon);
}
}
}