Skip to content

Commit

Permalink
Re-factor code style
Browse files Browse the repository at this point in the history
  • Loading branch information
ihsoft committed Apr 27, 2016
1 parent f83d524 commit 2af940e
Show file tree
Hide file tree
Showing 21 changed files with 6,153 additions and 6,689 deletions.
446 changes: 261 additions & 185 deletions Plugins/Source/KISAddonConfig.cs

Large diffs are not rendered by default.

338 changes: 155 additions & 183 deletions Plugins/Source/KISAddonCursor.cs
Original file line number Diff line number Diff line change
@@ -1,201 +1,173 @@
using KSPDev.LogUtils;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace KIS
{
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class KISAddonCursor : MonoBehaviour
{
private static bool cursorShow = false;
private static bool partDetectionActive = false;

private static Texture2D cursorTexture = null;
private static string cursorText;
private static List<string> cursorAdditionalTexts;
public static Part hoveredPart = null;
public static int partClickedFrame = -1;
private static OnMousePartAction delegateOnMousePartClick;
private static OnMousePartAction delegateOnMouseEnterPart;
private static OnMousePartAction delegateOnMouseHoverPart;
private static OnMousePartAction delegateOnMouseExitPart;
public delegate void OnMousePartAction(Part part);

// Cursor hint text settings.
private const int ActionIconSize = 24; // It's quare.
private const int HintFontSize = 10;
private const int HintTextLeftMargin = 4; // A gap between action icon and the text.
private static Color hintBackground = new Color(0.0f, 0.0f, 0.0f, 0.5f);
private static GUIStyle hintWindowStyle = new GUIStyle {
normal = {
background = CreateTextureFromColour(hintBackground),
textColor = Color.white
},
padding = new RectOffset(3, 3, 3, 3),
fontSize = HintFontSize
};

public static void StartPartDetection()
{
StartPartDetection(null, null, null, null);
}
namespace KIS {

public static void StartPartDetection(OnMousePartAction onMousePartClick, OnMousePartAction onMouseEnterPart, OnMousePartAction onMouseHoverPart, OnMousePartAction onMouseExitPart)
{
delegateOnMousePartClick = onMousePartClick;
delegateOnMouseEnterPart = onMouseEnterPart;
delegateOnMouseHoverPart = onMouseHoverPart;
delegateOnMouseExitPart = onMouseExitPart;
partDetectionActive = true;
}
[KSPAddon(KSPAddon.Startup.EveryScene, false)]
public class KISAddonCursor : MonoBehaviour {
private static bool cursorShow = false;
private static bool partDetectionActive = false;

private static Texture2D cursorTexture = null;
private static string cursorText;
private static List<string> cursorAdditionalTexts;
public static Part hoveredPart = null;
public static int partClickedFrame = -1;
private static OnMousePartAction delegateOnMousePartClick;
private static OnMousePartAction delegateOnMouseEnterPart;
private static OnMousePartAction delegateOnMouseHoverPart;
private static OnMousePartAction delegateOnMouseExitPart;
public delegate void OnMousePartAction(Part part);

public static void StopPartDetection()
{
partDetectionActive = false;
if (hoveredPart && delegateOnMouseExitPart != null)
{
delegateOnMouseExitPart(hoveredPart);
}
if (hoveredPart != null) {
KIS_Shared.SetHierarchySelection(hoveredPart, false /* isSelected */);
}
hoveredPart = null;
}
// Cursor hint text settings.
private const int ActionIconSize = 24;
// It's quare.
private const int HintFontSize = 10;
private const int HintTextLeftMargin = 4;
// A gap between action icon and the text.
private static Color hintBackground = new Color(0.0f, 0.0f, 0.0f, 0.5f);
private static GUIStyle hintWindowStyle = new GUIStyle {
normal = {
background = CreateTextureFromColour(hintBackground),
textColor = Color.white
},
padding = new RectOffset(3, 3, 3, 3),
fontSize = HintFontSize
};

public void Update() {
if (partDetectionActive)
{
Part part = Mouse.HoveredPart;
// OnMouseDown
if (Input.GetMouseButtonDown(0))
{
if (part)
{
partClickedFrame = Time.frameCount;
if (delegateOnMousePartClick != null) delegateOnMousePartClick(part);
}
}
// OnMouseOver
if (part)
{
if (delegateOnMouseHoverPart != null) delegateOnMouseHoverPart(part);
}

if (part)
{
// OnMouseEnter
if (part != hoveredPart)
{
if (hoveredPart)
{
if (delegateOnMouseExitPart != null) delegateOnMouseExitPart(hoveredPart);
}
if (delegateOnMouseEnterPart != null) delegateOnMouseEnterPart(part);
hoveredPart = part;
}
}
else
{
// OnMouseExit
if (part != hoveredPart)
{
if (delegateOnMouseExitPart != null) delegateOnMouseExitPart(hoveredPart);
hoveredPart = null;
}
}
}

if (HighLogic.LoadedSceneIsEditor && Input.GetMouseButtonDown(0))
{
if (InputLockManager.IsUnlocked(ControlTypes.EDITOR_PAD_PICK_PLACE))
{
Part part = Mouse.HoveredPart;
if (part)
{
if (delegateOnMousePartClick != null) delegateOnMousePartClick(part);
}
}
}
}
public static void StartPartDetection() {
StartPartDetection(null, null, null, null);
}

public static void CursorEnable(string texturePath, string text, string text2)
{
List<String> texts = new List<String>();
texts.Add(text2);
CursorEnable(texturePath, text, texts);
}
public static void StartPartDetection(OnMousePartAction onMousePartClick,
OnMousePartAction onMouseEnterPart,
OnMousePartAction onMouseHoverPart,
OnMousePartAction onMouseExitPart) {
delegateOnMousePartClick = onMousePartClick;
delegateOnMouseEnterPart = onMouseEnterPart;
delegateOnMouseHoverPart = onMouseHoverPart;
delegateOnMouseExitPart = onMouseExitPart;
partDetectionActive = true;
}

public static void CursorEnable(string texturePath, string text, List<string> additionalTexts = null)
{
cursorShow = true;
UnityEngine.Cursor.visible = false;
cursorTexture = GameDatabase.Instance.GetTexture(texturePath, false);
cursorText = text;
cursorAdditionalTexts = additionalTexts;
public static void StopPartDetection() {
partDetectionActive = false;
if (hoveredPart && delegateOnMouseExitPart != null) {
delegateOnMouseExitPart(hoveredPart);
}
if (hoveredPart != null) {
KIS_Shared.SetHierarchySelection(hoveredPart, false /* isSelected */);
}
hoveredPart = null;
}

public void Update() {
if (partDetectionActive) {
Part part = Mouse.HoveredPart;

if (part != hoveredPart) {
// OnMouseExit
if (hoveredPart && delegateOnMouseExitPart != null) {
delegateOnMouseExitPart(hoveredPart);
}

public static void CursorDefault()
{
cursorShow = false;
UnityEngine.Cursor.visible = true;
// OnMouseEnter
if (part && delegateOnMouseEnterPart != null) {
delegateOnMouseEnterPart(part);
}
hoveredPart = part;
}

public static void CursorDisable()
{
cursorShow = false;
UnityEngine.Cursor.visible = false;
if (hoveredPart) {
// OnMouseDown
if (Input.GetMouseButtonDown(0)) {
partClickedFrame = Time.frameCount; // Protection against false click trigger.
if (delegateOnMousePartClick != null) {
delegateOnMousePartClick(hoveredPart);
}
}

/// <summary>Makes a texture with the requested background color.</summary>
/// <remarks>
/// Borrowed from <see href="https://github.com/CYBUTEK/KerbalEngineer">KER Redux</see>
/// </remarks>
/// <param name="colour"></param>
/// <returns></returns>
private static Texture2D CreateTextureFromColour(Color colour)
{
var texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
texture.SetPixel(1, 1, colour);
texture.Apply();
return texture;
// OnMouseOver
if (delegateOnMouseHoverPart != null) {
delegateOnMouseHoverPart(hoveredPart);
}
}
}

private void OnGUI()
{
/*
if (draggedPart)
{
GUI.depth = 0;
GUI.DrawTexture(new Rect(Event.current.mousePosition.x - (draggedIconSize / 2), Event.current.mousePosition.y - (draggedIconSize / 2), draggedIconSize, draggedIconSize), icon.texture, ScaleMode.ScaleToFit);
*/

if (cursorShow)
{
// Display action icon.
GUI.DrawTexture(
new Rect(Event.current.mousePosition.x - ActionIconSize / 2,
Event.current.mousePosition.y - ActionIconSize / 2,
ActionIconSize, ActionIconSize),
cursorTexture, ScaleMode.ScaleToFit);

// Compile the whole hint text.
var allLines = new List<String>{cursorText};
if (cursorAdditionalTexts != null && cursorAdditionalTexts.Any()) {
allLines.Add(""); // A linefeed between status and hint text.
allLines.AddRange(cursorAdditionalTexts);
}
var hintText = String.Join("\n", allLines.ToArray());
// Calculate the label region.
Vector2 textSize = hintWindowStyle.CalcSize(new GUIContent(hintText));
var hintLabelRect = new Rect(
Event.current.mousePosition.x + ActionIconSize / 2 + HintTextLeftMargin,
Event.current.mousePosition.y - ActionIconSize / 2,
textSize.x, textSize.y);

GUI.Label(hintLabelRect, hintText, hintWindowStyle);
}
if (HighLogic.LoadedSceneIsEditor && Input.GetMouseButtonDown(0)) {
if (InputLockManager.IsUnlocked(ControlTypes.EDITOR_PAD_PICK_PLACE)) {
Part part = Mouse.HoveredPart;
if (part && delegateOnMousePartClick != null) {
delegateOnMousePartClick(part);
}
}
}
}

public static void CursorEnable(string texturePath, string text, string text2) {
List<String> texts = new List<String>();
texts.Add(text2);
CursorEnable(texturePath, text, texts);
}

public static void CursorEnable(string texturePath, string text,
List<string> additionalTexts = null) {
cursorShow = true;
Cursor.visible = false;
cursorTexture = GameDatabase.Instance.GetTexture(texturePath, false);
cursorText = text;
cursorAdditionalTexts = additionalTexts;
}

public static void CursorDefault() {
cursorShow = false;
Cursor.visible = true;
}

public static void CursorDisable() {
cursorShow = false;
Cursor.visible = false;
}

/// <summary>Makes a texture with the requested background color.</summary>
/// <remarks>
/// Borrowed from <see href="https://github.com/CYBUTEK/KerbalEngineer">KER Redux</see>
/// </remarks>
/// <param name="colour"></param>
/// <returns></returns>
private static Texture2D CreateTextureFromColour(Color colour) {
var texture = new Texture2D(1, 1, TextureFormat.ARGB32, false);
texture.SetPixel(1, 1, colour);
texture.Apply();
return texture;
}

private void OnGUI() {
if (cursorShow) {
// Display action icon.
GUI.DrawTexture(
new Rect(Event.current.mousePosition.x - ActionIconSize / 2,
Event.current.mousePosition.y - ActionIconSize / 2,
ActionIconSize, ActionIconSize),
cursorTexture, ScaleMode.ScaleToFit);

// Compile the whole hint text.
var allLines = new List<String>{ cursorText };
if (cursorAdditionalTexts != null && cursorAdditionalTexts.Any()) {
allLines.Add(""); // A linefeed between status and hint text.
allLines.AddRange(cursorAdditionalTexts);
}
var hintText = String.Join("\n", allLines.ToArray());
// Calculate the label region.
Vector2 textSize = hintWindowStyle.CalcSize(new GUIContent(hintText));
var hintLabelRect = new Rect(
Event.current.mousePosition.x + ActionIconSize / 2 + HintTextLeftMargin,
Event.current.mousePosition.y - ActionIconSize / 2,
textSize.x, textSize.y);

GUI.Label(hintLabelRect, hintText, hintWindowStyle);
}
}
}

} // namespace

0 comments on commit 2af940e

Please sign in to comment.