-
Notifications
You must be signed in to change notification settings - Fork 6
/
KerBalloonsCatagory.cs
88 lines (77 loc) · 3.18 KB
/
KerBalloonsCatagory.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
using KSP.UI.Screens;
using RUI.Icons.Selectable;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEngine;
namespace Kerballoons
{
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class KBFilter : BaseFilter
{
protected override string Manufacturer
{
get { return "KerBalloons"; }// part manufacturer in cfgs and agents files
set { }
}
protected override string categoryTitle
{
get { return "KerBalloons"; } // the category name
set { }
}
}
public abstract class BaseFilter : MonoBehaviour
{
private readonly List<AvailablePart> parts = new List<AvailablePart>();
internal string category = "Filter by function";
internal bool filter = true;
protected abstract string Manufacturer { get; set; }
protected abstract string categoryTitle { get; set; }
void Awake()
{
parts.Clear();
var count = PartLoader.LoadedPartsList.Count;
for (int i = 0; i < count; ++i)
{
var avPart = PartLoader.LoadedPartsList[i];
if (!avPart.partPrefab) continue;
if (avPart.manufacturer == Manufacturer)
{
parts.Add(avPart);
}
}
if (parts.Count > 0)
GameEvents.onGUIEditorToolbarReady.Add(SubCategories);
}
private bool EditorItemsFilter(AvailablePart avPart)
{
return parts.Contains(avPart);
}
private void SubCategories()
{
var icon = GetIcon(categoryTitle);
var filter = PartCategorizer.Instance.filters.Find(f => f.button.categorydisplayName == "#autoLOC_453547");//change for 1.3.1
PartCategorizer.AddCustomSubcategoryFilter(filter, categoryTitle, categoryTitle, icon, EditorItemsFilter);
}
private Icon GetIcon(string iconName)
{
var normIcon = new Texture2D(32, 32, TextureFormat.RGBA32, false);
var normIconFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), iconName + "_off.png"); // icon to be present in same folder as dll
WWW www = new WWW(normIconFile);
www.LoadImageIntoTexture(normIcon);
//normIcon = www.texture;
//normIcon.LoadRawTextureData(File.ReadAllBytes(normIconFile));
normIcon.Apply();
var selIcon = new Texture2D(32, 32, TextureFormat.RGBA32, false);
var selIconFile = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), iconName + "_on.png");// icon to be present in same folder as dll
www = new WWW(selIconFile);
www.LoadImageIntoTexture(selIcon);
//selIcon = www.texture;
//selIcon.LoadRawTextureData(File.ReadAllBytes(selIconFile));
selIcon.Apply();
www.Dispose();
var icon = new Icon(iconName + "Icon", normIcon, selIcon);
return icon;
}
}
}