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

Commit

Permalink
Added filterCreator,
Browse files Browse the repository at this point in the history
  • Loading branch information
Crzyrndm committed Jan 4, 2015
1 parent 7c48314 commit 903dcdb
Show file tree
Hide file tree
Showing 11 changed files with 99 additions and 150 deletions.
188 changes: 56 additions & 132 deletions FilterCreator/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,18 @@ namespace FilterCreator
[KSPAddon(KSPAddon.Startup.EditorAny, false)]
public class Core : MonoBehaviour
{
// mod folder for each part by internal name
internal static Dictionary<string, string> partFolderDict = new Dictionary<string, string>();
// Dictionary of icons created on entering the main menu
internal static Dictionary<string, PartCategorizer.Icon> iconDict = new Dictionary<string, PartCategorizer.Icon>();
// GUI Rectangle
Rect windowRect = new Rect(400, 100, 0, 0);

Vector2 categoryScroll = new Vector2(0, 0);
Vector2 subCategoryScroll = new Vector2(0, 0);
Vector2 filterScroll = new Vector2(0, 0);
Vector2 checkScroll = new Vector2(0, 0);
Vector2 partsScroll = new Vector2(0, 0);

PartCategorizer.Category activeCategory;
PartCategorizer.Category activeSubCategory;
ConfigNode activeFilter;
ConfigNode activeFilter = new ConfigNode();
ConfigNode activeCheck;

List<ConfigNode> subCategoryNodes = new List<ConfigNode>();
Expand All @@ -32,101 +29,7 @@ public void Awake()
{
Debug.Log("[Filter Creator] Version 1.0");

assignModsToParts();
loadIcons();
}

private void assignModsToParts()
{
// Build list of mod folder names and Dict associating parts with mods
List<string> modNames = new List<string>();
foreach (AvailablePart p in PartLoader.Instance.parts)
{
// don't want dummy parts
if (p.category == PartCategories.none)
continue;

if (string.IsNullOrEmpty(p.partUrl))
RepairAvailablePartUrl(p);

// if the url is still borked, can't assign a mod to it
if (string.IsNullOrEmpty(p.partUrl))
continue;

string name = p.partUrl.Split(new char[] { '/', '\\' })[0]; // mod folder name (\\ is escaping the \, read as '\')

// if we haven't seen any from this mod before
if (!modNames.Contains(name))
modNames.Add(name);

// associate the mod to the part
if (!partFolderDict.ContainsKey(p.name))
partFolderDict.Add(p.name, name);
else
Log(p.name + " duplicated part key in part-mod dictionary");
}
}

private void loadIcons()
{
List<GameDatabase.TextureInfo> texList = GameDatabase.Instance.databaseTexture.Where(t => t.texture != null
&& t.texture.height <= 40 && t.texture.width <= 40
&& t.texture.width >= 25 && t.texture.height >= 25
).ToList();

Dictionary<string, GameDatabase.TextureInfo> texDict = new Dictionary<string, GameDatabase.TextureInfo>();
// using a dictionary for looking up _selected textures. Else the list has to be iterated over for every texture
foreach (GameDatabase.TextureInfo t in texList)
{
if (!texDict.ContainsKey(t.name))
texDict.Add(t.name, t);
}

foreach (GameDatabase.TextureInfo t in texList)
{
Texture2D selectedTex = null;

if (texDict.ContainsKey(t.name + "_selected"))
selectedTex = texDict[t.name + "_selected"].texture;
else
selectedTex = t.texture;

string[] name = t.name.Split(new char[] { '/', '\\' });
PartCategorizer.Icon icon = new PartCategorizer.Icon(name[name.Length - 1], t.texture, selectedTex, false);

if (!iconDict.ContainsKey(icon.name))
iconDict.Add(icon.name, icon);
}
}

internal static PartCategorizer.Icon getIcon(string name)
{
if (iconDict.ContainsKey(name))
{
return iconDict[name];
}
else if (PartCategorizer.Instance.iconDictionary.ContainsKey(name))
{
return PartCategorizer.Instance.iconDictionary[name];
}
else if (name.StartsWith("stock_"))
{
PartCategorizer.Category fbf = PartCategorizer.Instance.filters.Find(c => c.button.categoryName == "Filter by Function");
name = name.Substring(6);
return fbf.subcategories.FirstOrDefault(sC => sC.button.categoryName == name).button.icon;
}
return null;
}

// credit to EvilReeperx for this lifesaving function
private void RepairAvailablePartUrl(AvailablePart ap)
{
var url = GameDatabase.Instance.GetConfigs("PART").FirstOrDefault(u => u.name.Replace('_', '.') == ap.name);

if (url == null)
return;

ap.partUrl = url.url;
subCategoryNodes = GameDatabase.Instance.GetConfigNodes("SUBCATEGORY").ToList();
}
#endregion

Expand All @@ -139,13 +42,14 @@ public void OnGUI()

if (AppLauncherEditor.bDisplayEditor)
windowRect = GUILayout.Window(579164, windowRect, drawWindow, "");

subCategoryNodes = GameDatabase.Instance.GetConfigNodes("SUBCATEGORY").ToList();
}

private void drawWindow(int id)
{
ConfigNode subCategory = new ConfigNode();
ConfigNode subCategory_selected = new ConfigNode();

HighLogic.Skin.button.wordWrap = true;

GUILayout.BeginHorizontal();
// Categories column
Expand All @@ -164,55 +68,75 @@ private void drawWindow(int id)
GUILayout.EndVertical();
GUILayout.EndScrollView();
// subCategories column
subCategoryScroll = GUILayout.BeginScrollView(subCategoryScroll, GUILayout.Height((float)(Screen.height * 0.7)), GUILayout.Width(220));
subCategoryScroll = GUILayout.BeginScrollView(subCategoryScroll, GUILayout.Height((float)(Screen.height * 0.7)), GUILayout.Width(230));
GUILayout.BeginVertical();
foreach (PartCategorizer.Category sC in activeCategory.subcategories)
{
if (GUILayout.Toggle(activeSubCategory == sC, sC.button.categoryName, HighLogic.Skin.button, GUILayout.Width(200)))
subCategory = subCategoryNodes.FirstOrDefault(n => n.GetValue("title") == sC.button.categoryName);
if (subCategory != null)
{
activeSubCategory = sC;
subCategory = subCategoryNodes.FirstOrDefault(n => n.GetValue("title") == sC.button.categoryName);
if (GUILayout.Toggle(activeSubCategory == sC, "title: " + sC.button.categoryName + "\r\ncategory: "
+ subCategory.GetValue("category").Split(',').FirstOrDefault(s => s.Trim() == activeCategory.button.categoryName)
+ "\r\noldTitle: " + subCategory.GetValue("oldTitle")
+ "\r\nicon: " + subCategory.GetValue("icon"), HighLogic.Skin.button, GUILayout.Width(200)))
{
activeSubCategory = sC;
subCategory_selected = subCategoryNodes.FirstOrDefault(n => n.GetValue("title") == sC.button.categoryName);
}
}
}
GUILayout.EndVertical();
GUILayout.EndScrollView();

GUILayout.BeginVertical();

if (GUILayout.Button("Add New Filter"))
// Filters column
filterScroll = GUILayout.BeginScrollView(filterScroll, GUILayout.Height((float)(Screen.height * 0.7)), GUILayout.Width(220));
if (subCategory_selected != null && subCategory_selected.GetNodes("FILTER") != null)
{

GUILayout.BeginVertical();
foreach (ConfigNode fil in subCategory_selected.GetNodes("FILTER"))
{
if (GUILayout.Toggle(activeFilter == fil, "invert: " + fil.GetValue("invert"), HighLogic.Skin.button, GUILayout.Width(200)))
{
activeFilter = fil;
}
}
GUILayout.EndVertical();
}
GUILayout.EndScrollView();

filterScroll = GUILayout.BeginScrollView(filterScroll, GUILayout.Height((float)(Screen.height * 0.7)), GUILayout.Width(220));
foreach (ConfigNode node in subCategory.GetNodes("FILTER"))
// Checks column
checkScroll = GUILayout.BeginScrollView(checkScroll, GUILayout.Height((float)(Screen.height * 0.7)), GUILayout.Width(220));
if (activeFilter != null && activeFilter.GetNodes("CHECK") != null)
{
if (GUILayout.Toggle(activeFilter == node, node.GetValue("name"), HighLogic.Skin.button, GUILayout.Width(200)))
GUILayout.BeginVertical();
foreach (ConfigNode check in activeFilter.GetNodes("CHECK"))
{
activeFilter = node;
if (GUILayout.Toggle(activeCheck == check, "type: " + check.GetValue("type") + "\r\nvalue: " + check.GetValue("value") + "\r\ninvert: " + check.GetValue("invert"), HighLogic.Skin.button, GUILayout.Width(200)))
{
activeCheck = check;
}
}
GUILayout.EndVertical();
}
GUILayout.EndScrollView();
GUILayout.EndVertical();

//GUILayout.BeginVertical();

//if (GUILayout.Button("Add New Check"))
//{

//}

//checkScroll = GUILayout.BeginScrollView(checkScroll, GUILayout.Height((float)(Screen.height * 0.7)), GUILayout.Width(220));
//foreach (ConfigNode node in activeFilter.GetNodes("CHECK"))
//{
// if (GUILayout.Toggle(activeCheck == node, node.GetValue("type"), HighLogic.Skin.button, GUILayout.Width(200)))
// {
// activeFilter = node;
// }
//}
//GUILayout.EndScrollView();
//GUILayout.EndVertical();
// Parts column
if (subCategory_selected != null)
{
FilterExtensions.customSubCategory sC = new FilterExtensions.customSubCategory(subCategory_selected, "");
partsScroll = GUILayout.BeginScrollView(partsScroll, GUILayout.Height((float)(Screen.height * 0.7)), GUILayout.Width(220));

GUILayout.BeginVertical();
foreach (AvailablePart ap in PartLoader.Instance.parts)
{
if (sC.checkFilters(ap))
{
GUILayout.Label(ap.title, GUILayout.Width(200));
}
}
GUILayout.EndVertical();
GUILayout.EndScrollView();
}
GUILayout.EndHorizontal();

GUI.DragWindow();
Expand Down
11 changes: 10 additions & 1 deletion FilterCreator/FilterCreator.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>none</DebugType>
<Optimize>true</Optimize>
<OutputPath>..\..\..\Desktop\Kerbal Space Program Dev\GameData\000_FilterExtensions\</OutputPath>
<OutputPath>..\GameData\000_FilterExtensions\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
Expand All @@ -35,6 +35,10 @@
<HintPath>..\..\..\Desktop\Kerbal Space Program Dev\KSP_Data\Managed\Assembly-CSharp.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="FilterExtensions, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\GameData\000_FilterExtensions\FilterExtensions.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -52,6 +56,11 @@
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<PropertyGroup>
<PostBuildEvent>set SOURCE="D:\Users\Joshua\GitHub\Filter-Extensions\GameData"
set DESTINATION="D:\Users\Joshua\Desktop\Kerbal Space Program Dev\GameData"
xcopy %25SOURCE%25 %25DESTINATION%25 /e /c /r /i /u /y</PostBuildEvent>
</PropertyGroup>
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
Expand Down
6 changes: 3 additions & 3 deletions FilterExtension.sln
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ Microsoft Visual Studio Solution File, Format Version 12.00
VisualStudioVersion = 12.0.30501.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PartFilters", "FilterExtension\PartFilters.csproj", "{8633A568-654E-409D-A7B2-95839F2EBD3D}"
ProjectSection(ProjectDependencies) = postProject
{7249D770-00A9-491F-86C1-385E36150388} = {7249D770-00A9-491F-86C1-385E36150388}
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FilterCreator", "FilterCreator\FilterCreator.csproj", "{7249D770-00A9-491F-86C1-385E36150388}"
ProjectSection(ProjectDependencies) = postProject
{8633A568-654E-409D-A7B2-95839F2EBD3D} = {8633A568-654E-409D-A7B2-95839F2EBD3D}
EndProjectSection
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Expand Down
2 changes: 1 addition & 1 deletion FilterExtension/Check.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace FilterExtensions
{
using Categoriser;

internal class Check
public class Check
{
internal string type = ""; // type of check to perform (module, title/name, resource,...)
internal string value = "";
Expand Down
23 changes: 17 additions & 6 deletions FilterExtension/Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,29 @@ namespace FilterExtensions
[KSPAddon(KSPAddon.Startup.MainMenu, true)]
public class Core : MonoBehaviour
{
private static Core instance;

// storing categories/subCategories loaded at Main Menu for creation when entering SPH/VAB
internal static List<customCategory> Categories = new List<customCategory>();
internal static List<customSubCategory> subCategories = new List<customSubCategory>();
internal List<customCategory> Categories = new List<customCategory>();
internal List<customSubCategory> subCategories = new List<customSubCategory>();

// mod folder for each part by internal name
internal static Dictionary<string, string> partFolderDict = new Dictionary<string, string>();
public static Dictionary<string, string> partFolderDict = new Dictionary<string, string>();

// Dictionary of icons created on entering the main menu
internal static Dictionary<string, PartCategorizer.Icon> iconDict = new Dictionary<string, PartCategorizer.Icon>();
public static Dictionary<string, PartCategorizer.Icon> iconDict = new Dictionary<string, PartCategorizer.Icon>();

public static Core Instance
{
get
{
return instance;
}
}

void Awake()
{
instance = this;
Log("Version 1.11");

// Add event for when the Editor GUI becomes active. This is never removed because we need it to fire every time
Expand Down Expand Up @@ -231,7 +242,7 @@ private void checkIcons(PartCategorizer.Category category)
}
}

private void loadIcons()
private static void loadIcons()
{
List<GameDatabase.TextureInfo> texList = GameDatabase.Instance.databaseTexture.Where(t => t.texture != null
&& t.texture.height <= 40 && t.texture.width <= 40
Expand Down Expand Up @@ -263,7 +274,7 @@ private void loadIcons()
}
}

internal static PartCategorizer.Icon getIcon(string name)
public static PartCategorizer.Icon getIcon(string name)
{
if (iconDict.ContainsKey(name))
{
Expand Down
2 changes: 1 addition & 1 deletion FilterExtension/Filter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace FilterExtensions
{
class Filter
public class Filter
{
internal List<Check> checks = new List<Check>(); // checks are processed in serial (a && b), inversion gives (!a || !b) logic
internal bool invert = false;
Expand Down
4 changes: 2 additions & 2 deletions FilterExtension/customCategory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace FilterExtensions
{
using Categoriser;

class customCategory
public class customCategory
{
internal string categoryTitle;
internal string iconName;
Expand Down Expand Up @@ -73,7 +73,7 @@ private void generateSubCategories()
nodeSub.AddValue("icon", "stock_" + s);
nodeSub.AddNode(nodeFilter);

Core.subCategories.Add(new customSubCategory(nodeSub, categoryTitle));
Core.Instance.subCategories.Add(new customSubCategory(nodeSub, categoryTitle));
}
}

Expand Down
Loading

0 comments on commit 903dcdb

Please sign in to comment.