Skip to content

Commit

Permalink
Merge pull request #14 from Sigma88/Navigation
Browse files Browse the repository at this point in the history
Navigation
  • Loading branch information
Sigma88 authored Nov 18, 2017
2 parents 17b59dd + 749e321 commit 2054eb7
Show file tree
Hide file tree
Showing 25 changed files with 1,357 additions and 72 deletions.
3 changes: 3 additions & 0 deletions Navigation/Changelog.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**v0.1.0**

- First Beta Release
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"NAME": "<b><color=#9B59B6>Sigma Replacements: Navigation</color></b>",
"URL": "https://raw.githubusercontent.com/Sigma88/Sigma-Replacements/master/Navigation/GameData/Sigma/Replacements/Navigation/Plugins/SigmaReplacementsNavigation.version",
"DOWNLOAD": "http://forum.kerbalspaceprogram.com/index.php?/topic/167233-0",
"CHANGE_LOG_URL": "https://raw.githubusercontent.com/Sigma88/Sigma-Replacements/master/Navigation/Changelog.txt",
"GITHUB":
{
"USERNAME": "Sigma88",
"REPOSITORY": "Sigma-Replacements",
"ALLOW_PRE_RELEASE": false
},
"VERSION":
{
"MAJOR": 0,
"MINOR": 1,
"PATCH": 0,
"BUILD": 0
},
"KSP_VERSION":
{
"MAJOR": 1,
"MINOR": 3,
"PATCH": 1
}
}
8 changes: 8 additions & 0 deletions Navigation/License.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Sigma Replacements: Navigation by Sigma88 is released under the following license:


All Rights Reserved


For more info visit the KSP Forum Thread:
http://forum.kerbalspaceprogram.com/index.php?/topic/167233-0
11 changes: 11 additions & 0 deletions Navigation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Sigma Replacements: Navigation


**Customizeable Navigation Tools**


KSP Forum Thread: http://forum.kerbalspaceprogram.com/index.php?/topic/167233-0/

Download Latest Release: https://github.com/Sigma88/Sigma-Replacements/releases

Dev version: https://github.com/Sigma88/Sigma-Replacements/tree/Navigation
Binary file modified [Source]/Distribution/SigmaReplacementsHeads.dll
Binary file not shown.
Binary file not shown.
4 changes: 3 additions & 1 deletion [Source]/SigmaReplacements/Common/CrewMember.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Type = ProtoCrewMember.KerbalType;
using Roster = ProtoCrewMember.RosterStatus;


namespace SigmaReplacements
Expand All @@ -8,11 +9,12 @@ public class CrewMember : ProtoCrewMember
internal new string name = "";
internal int activity = 0;

public CrewMember(Type type, string name, Gender gender, string trait, bool veteran, bool isBadass, float courage, float stupidity, int experienceLevel , int activity = 0) : base(type, name)
public CrewMember(Type type, Roster rosterStatus, string name, Gender gender, string trait, bool veteran, bool isBadass, float courage, float stupidity, int experienceLevel, int activity = 0) : base(type, name)
{
Debug.Log("CrewMember", "new CrewMember from stats");

this.type = type;
this.rosterStatus = rosterStatus;
this.name = name;
this.gender = gender;
this.trait = trait;
Expand Down
258 changes: 238 additions & 20 deletions [Source]/SigmaReplacements/Common/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.UI;
using KSP.UI;
using IState = KSP.UI.UIStateImage.ImageState;
using BState = KSP.UI.UIStateToggleButton.ButtonState;
using Type = ProtoCrewMember.KerbalType;
using Roster = ProtoCrewMember.RosterStatus;


namespace SigmaReplacements
Expand All @@ -10,7 +16,7 @@ internal static class Extensions
{
internal static List<Info> Order(this List<Info> List)
{
Debug.Log(List?.GetType()?.Name + ".Order", "Total count = " + List?.Count);
Debug.Log("List<" + List?.FirstOrDefault()?.GetType()?.Name + ">.Order", "Total count = " + List?.Count);

List<Info> DataBase = new List<Info>();

Expand All @@ -19,7 +25,7 @@ internal static List<Info> Order(this List<Info> List)
DataBase.AddRange(List.Where(h => string.IsNullOrEmpty(h?.name) && !string.IsNullOrEmpty(h?.collection)));
DataBase.AddRange(List.Where(h => h != null && string.IsNullOrEmpty(h?.name) && string.IsNullOrEmpty(h?.collection)));

Debug.Log(List?.GetType()?.Name + ".Order", "Valid count = " + List?.Count);
Debug.Log("List<" + List?.FirstOrDefault()?.GetType()?.Name + ">.Order", "Valid count = " + DataBase?.Count);

return DataBase;
}
Expand All @@ -31,7 +37,8 @@ internal static void Load(this CrewMember[] array, ConfigNode node, int index)

array[index] = new CrewMember
(
stats?.rosterStatus ?? kerbal.type,
(int?)stats?.status > 3 ? 0 : (Type?)(int?)stats?.status ?? kerbal.type,
(int?)stats?.status > 3 ? (Roster)((int)stats.status - 4) : kerbal.rosterStatus,
!string.IsNullOrEmpty(stats?.name) ? stats.name : kerbal.name,
stats?.gender ?? kerbal.gender,
stats?.trait?.Length > 0 && !string.IsNullOrEmpty(stats.trait[0]) ? stats.trait[0] : kerbal.trait,
Expand All @@ -51,7 +58,7 @@ internal static void SetColor(this Material material, Color? color)
}
}

internal static void SetTexture(this Material material, Texture newTex)
internal static void SetTexture(this Material material, Texture newTex, bool flip = false)
{
if (material != null && newTex != null)
{
Expand All @@ -64,6 +71,13 @@ internal static void SetTexture(this Material material, Texture newTex)
}

material.mainTexture = newTex;

if (flip)
{
Vector2 scale = material.GetTextureScale("_MainTex");
scale.x *= -1;
material.SetTextureScale("_MainTex", scale);
}
}
}

Expand All @@ -85,36 +99,229 @@ internal static void SetNormal(this Material material, Texture newTex)
}
}

internal static Texture At(this List<Texture> right, Texture item, List<Texture> left)
internal static void SetEmissive(this Material material, Texture newTex, bool flip = false)
{
if (item != null && left.Contains(item) && right?.Count > left.IndexOf(item))
return right[left.IndexOf(item)];
return null;
if (material?.HasProperty("_Emissive") == true && newTex != null)
{
Texture oldTex = material.GetTexture("_Emissive");

if (oldTex != null)
{
newTex.anisoLevel = oldTex.anisoLevel;
newTex.wrapMode = oldTex.wrapMode;
}

material.SetTexture("_Emissive", newTex);

if (flip)
{
Vector2 scale = material.GetTextureScale("_Emissive");
scale.x *= -1;
material.SetTextureScale("_Emissive", scale);
}
}
}

internal static Color? At(this List<Color> right, Color? item, List<Color> left)
internal static void SetTintColor(this Material material, Color? color, Material stockMaterial = null)
{
if (item != null && left.Contains((Color)item) && right?.Count > left.IndexOf((Color)item))
return right[left.IndexOf((Color)item)];
return null;
if (material != null)
{
if (color != null)
material.SetColor("_TintColor", (Color)color);

else

if (stockMaterial?.HasProperty("_TintColor") == true)
material.SetColor("_TintColor", stockMaterial.GetColor("_TintColor"));
}
}

internal static Texture Pick(this List<Texture> list, ProtoCrewMember kerbal, bool useGameSeed)
internal static void SetMainTexture(this Material material, Texture newTex, Material stockMaterial = null)
{
if (list.Count == 1)
return list[0];
else if (list.Count > 1)
if (material != null)
{
if (newTex == null)
newTex = stockMaterial?.GetTexture("_MainTexture");

if (newTex == null) return;

Texture oldTex = material?.GetTexture("_MainTexture");

if (oldTex == newTex) return;

if (oldTex != null)
{
newTex.anisoLevel = oldTex.anisoLevel;
newTex.wrapMode = oldTex.wrapMode;
}

material.SetTexture("_MainTexture", newTex);
}
}

internal static void SetColor(this Image image, Color? color, Image stockImage = null)
{
if (image != null)
{
image.color = color ?? stockImage?.color ?? image.color;
}
}

internal static void SetTexture(this Image image, Texture newTex, Image stockImage = null, bool fit = false, bool resize = false, Vector2? resolution = null)
{
if (image?.sprite != null)
{
Vector2 scale = new Vector2(1, 1);
if (stockImage?.sprite?.rect != null)
{
scale.x = stockImage.sprite.rect.width;
scale.y = stockImage.sprite.rect.height;
}

image.sprite = image.sprite.SetTexture(newTex ?? stockImage?.sprite?.texture, fit);

if (resize)
{
scale.x /= image.sprite.rect.width / (resolution?.x ?? 1);
scale.y /= image.sprite.rect.height / (resolution?.y ?? 1);

if (stockImage?.rectTransform?.localScale != null)
image.rectTransform.localScale = stockImage.rectTransform.localScale;

image.rectTransform.localScale = new Vector3(image.rectTransform.localScale.x / scale.x, image.rectTransform.localScale.y / scale.y, image.rectTransform.localScale.z);
}
}
}

internal static void SetColor(this UIStateImage button, Color? color, UIStateImage stockButton)
{
Image image = button?.GetComponent<Image>();

if (image != null)
{
Image stockImage = stockButton?.GetComponent<Image>();
image.SetColor(color, stockImage);
}
}

internal static void SetTexture(this UIStateImage button, Texture newTex, UIStateImage stockButton = null)
{
for (int i = 0; i < button?.states?.Length; i++)
{
IState state = button?.states[i];

if (state != null)
{
Texture stockTexture = stockButton?.states?.Length > i ? stockButton.states[i]?.sprite?.texture : null;
state.sprite = state.sprite.SetTexture(newTex ?? stockTexture);
}
}
}

internal static void SetColor(this UIStateToggleButton toggle, Color? color, UIStateToggleButton stockButton = null)
{
Image image = toggle?.GetComponent<Image>();

if (image != null && color != null)
{
Image stockImage = stockButton?.GetComponent<Image>();
image.SetColor(color, stockImage);
}
}

internal static void SetTexture(this UIStateToggleButton toggle, Texture newTex, UIStateToggleButton stockToggle = null)
{
if (toggle != null)
{
BState[] states = { toggle.stateTrue, toggle.stateFalse };
BState[] stockStates = { stockToggle?.stateTrue, stockToggle?.stateFalse };
for (int i = 0; i < states?.Length; i++)
{
BState stock = stockStates[i];
states[i].disabled = states[i].disabled.SetTexture(newTex ?? stock?.disabled?.texture);
states[i].normal = states[i].normal.SetTexture(newTex ?? stock?.normal?.texture);
states[i].highlight = states[i].highlight.SetTexture(newTex ?? stock?.highlight?.texture);
states[i].pressed = states[i].pressed.SetTexture(newTex ?? stock?.pressed?.texture);
}
}
}

internal static Sprite SetTexture(this Sprite sprite, Texture newTex, bool fit = false)
{
if (sprite == null || newTex == null) return sprite;
if (sprite.texture == newTex) return sprite;

Texture oldTex = sprite?.texture;

if (oldTex != null)
{
newTex.anisoLevel = oldTex.anisoLevel;
newTex.wrapMode = oldTex.wrapMode;
}

Rect rect = sprite.rect;
Vector2 pivot = sprite.pivot;

if (fit)
{
rect.width = newTex.width;
rect.height = newTex.height;
pivot = new Vector2(newTex.width * 0.5f, newTex.height * 0.5f);
}

return Sprite.Create((Texture2D)newTex, rect, pivot);
}

internal static Color? At(this List<Color?> list, Color? element, List<Color?> reference, ProtoCrewMember kerbal, bool useGameSeed, string name = "")
{
if (reference.Contains(element) && list?.Count > reference.IndexOf(element))
return list[reference.IndexOf(element)];
else
return list.Pick(kerbal, useGameSeed, name);
}

internal static Texture At(this List<Texture> list, Texture element, List<Texture> reference, ProtoCrewMember kerbal, bool useGameSeed, string name = "")
{
if (reference.Contains(element) && list?.Count > reference.IndexOf(element))
return list[reference.IndexOf(element)];
else
return list.Pick(kerbal, useGameSeed, name);
}

internal static Vector2? At(this List<Vector2?> list, Texture element, List<Texture> reference, ProtoCrewMember kerbal, bool useGameSeed, string name = "")
{
if (reference.Contains(element) && list?.Count > reference.IndexOf(element))
return list[reference.IndexOf(element)];
else
return list.Pick(kerbal, useGameSeed, name);
}

internal static Texture Pick(this List<Texture> list, ProtoCrewMember kerbal, bool useGameSeed, string name = "")
{
if (list?.Count > 1 && kerbal != null)
return list[kerbal.Hash(useGameSeed) % list.Count];
else if (list?.Count > 0)
return list[name.Hash(useGameSeed) % list.Count];
else
return null;
}

internal static Color? Pick(this List<Color> list, ProtoCrewMember kerbal, bool useGameSeed)
internal static Color? Pick(this List<Color?> list, ProtoCrewMember kerbal, bool useGameSeed, string name = "")
{
if (list.Count == 1)
return list[0];
else if (list.Count > 1)
if (list?.Count > 1 && kerbal != null)
return list[kerbal.Hash(useGameSeed) % list.Count];
else if (list?.Count > 0)
return list[name.Hash(useGameSeed) % list.Count];
else
return null;
}

internal static Vector2? Pick(this List<Vector2?> list, ProtoCrewMember kerbal, bool useGameSeed, string name = "")
{
if (list?.Count > 1 && kerbal != null)
return list[kerbal.Hash(useGameSeed) % list.Count];
else if (list?.Count > 0)
return list[name.Hash(useGameSeed) % list.Count];
else
return null;
}
Expand All @@ -133,5 +340,16 @@ internal static int Hash(this ProtoCrewMember kerbal, bool useGameSeed)

return h;
}

internal static int Hash(this string name, bool useGameSeed)
{
name = name ?? "";

int h = Math.Abs(name.GetHashCode());

if (useGameSeed && HighLogic.CurrentGame != null) h += Math.Abs(HighLogic.CurrentGame.Seed);

return h;
}
}
}
Loading

0 comments on commit 2054eb7

Please sign in to comment.