Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
Support for categories added
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Dec 23, 2014
1 parent d71bc67 commit 1a25a3f
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 9 deletions.
92 changes: 92 additions & 0 deletions FilterExtension/Category.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace FilterExtensions
{
class Category : IEquatable<Category>
{
internal string categoryTitle;
internal string iconName;
internal Color colour;

public Category(ConfigNode node)
{
categoryTitle = node.GetValue("title");
iconName = node.GetValue("icon");
convertToColor(node.GetValue("colour"));
}

internal void initialise()
{
if (categoryTitle == null)
return;

PartCategorizer.Icon icon = Core.getIcon(iconName);
PartCategorizer.AddCustomFilter(categoryTitle, icon, colour);
}

private void convertToColor(string hex_ARGB)
{
hex_ARGB = hex_ARGB.Replace("#", "");

if (hex_ARGB.Length == 8)
{
Color c = new Color();
c.a = (int)byte.Parse(hex_ARGB.Substring(0, 2), System.Globalization.NumberStyles.HexNumber) / 255;
c.r = (int)byte.Parse(hex_ARGB.Substring(2, 2), System.Globalization.NumberStyles.HexNumber) / 255;
c.g = (int)byte.Parse(hex_ARGB.Substring(4, 2), System.Globalization.NumberStyles.HexNumber) / 255;
c.b = (int)byte.Parse(hex_ARGB.Substring(6, 2), System.Globalization.NumberStyles.HexNumber) / 255;
colour = c;
}
else if (hex_ARGB.Length == 6)
{
Color c = new Color();
c.a = 1;
c.r = (int)byte.Parse(hex_ARGB.Substring(0, 2), System.Globalization.NumberStyles.HexNumber) / 255;
c.g = (int)byte.Parse(hex_ARGB.Substring(2, 2), System.Globalization.NumberStyles.HexNumber) / 255;
c.b = (int)byte.Parse(hex_ARGB.Substring(3, 2), System.Globalization.NumberStyles.HexNumber) / 255;
colour = c;
}
}

public bool Equals(Category other)
{
if (other == null)
return false;

if (this.categoryTitle == other.categoryTitle)
return true;
else
return false;
}

public override bool Equals(System.Object obj)
{
if (obj == null)
return false;

Category CObj = obj as Category;
if (CObj == null)
return false;
else
return Equals(CObj);
}

public override int GetHashCode()
{
return categoryTitle.GetHashCode();
}

public static bool operator ==(Category c1, Category c2)
{
return c1.Equals(c2);
}

public static bool operator !=(Category c1, Category c2)
{
return !c1.Equals(c2);
}
}
}
40 changes: 32 additions & 8 deletions FilterExtension/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,21 @@ namespace FilterExtensions
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class Core : MonoBehaviour
{
List<Category> Categories = new List<Category>();
List<subCategory> subCategories = new List<subCategory>();
internal static Dictionary<string, GameDatabase.TextureInfo> texDict = new Dictionary<string, GameDatabase.TextureInfo>();

void Awake()
{
GameEvents.onGUIEditorToolbarReady.Add(SubCategories);

foreach (ConfigNode node in GameDatabase.Instance.GetConfigNodes("CATEGORY"))
{
Category C = new Category(node);
if (!Categories.Contains(C))
Categories.Add(C);
}

foreach (ConfigNode node in GameDatabase.Instance.GetConfigNodes("SUBCATEGORY"))
{
subCategory sC = new subCategory(node);
Expand All @@ -28,6 +37,11 @@ private void SubCategories()
{
loadIcons();

foreach (Category c in Categories)
{
PartCategorizer.AddCustomFilter(c.categoryTitle, getIcon(c.iconName), c.colour);
}

foreach (PartCategorizer.Category c in PartCategorizer.Instance.filters)
{
checkIcons(c);
Expand Down Expand Up @@ -137,18 +151,15 @@ private void checkIcons(PartCategorizer.Category category)
private void loadIcons()
{
List<GameDatabase.TextureInfo> texList = GameDatabase.Instance.GetAllTexturesInFolderType("filterIcon");
// use a dictionary for looking up _selected textures. Else the list has to be iterated over for every texture (operation reduced to O(n))
texDict = texList.ToDictionary(k => k.name);
foreach (GameDatabase.TextureInfo t in texList)
{
bool simple = false;
Texture2D selectedTex = null;
foreach (GameDatabase.TextureInfo t2 in texList)
{
if (t.name + "_selected" == t2.name)
{
selectedTex = t2.texture;
}
}
if (selectedTex == null)
if (texDict.ContainsKey(t.name + "_selected"))
selectedTex = texDict[t.name + "_selected"].texture;
else
{
selectedTex = t.texture;
simple = true;
Expand All @@ -159,5 +170,18 @@ private void loadIcons()
PartCategorizer.Instance.iconDictionary.Add(icon.name, icon);
}
}

internal static PartCategorizer.Icon getIcon(string name)
{
PartCategorizer.Icon icon = PartCategorizer.Instance.GetIcon(name);
if (icon.name == PartCategorizer.Instance.fallbackIcon.name)
{
if (PartCategorizer.Instance.iconDictionary.ContainsKey(name))
return PartCategorizer.Instance.iconDictionary[name];
else
return icon;
}
return icon;
}
}
}
1 change: 1 addition & 0 deletions FilterExtension/PartFilters.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Category.cs" />
<Compile Include="Check.cs" />
<Compile Include="Core.cs" />
<Compile Include="Categoriser\PartType.cs" />
Expand Down
2 changes: 1 addition & 1 deletion FilterExtension/subCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ internal void initialise()
Debug.Log("[Filter Extensions] " + this.subCategoryTitle + " missing icon reference");
return;
}
PartCategorizer.Icon icon = PartCategorizer.Instance.GetIcon(iconName);
PartCategorizer.Icon icon = Core.getIcon(iconName);

if (filter)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
CATEGORY
{
title = TestCategory
icon = R&D_node_icon_experimentalmotors
colour = #FF0000FF
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
SUBCATEGORY
{
category = TestCategory
title = TestTitle
icon = R&D_node_icon_experimentalmotors
FILTER
{
CHECK
{
type = title
value = Cargo Bay
pass = true
}
}
}
Binary file modified GameData/Filter Extensions/FilterExtensions.dll
Binary file not shown.

0 comments on commit 1a25a3f

Please sign in to comment.