Skip to content

Commit

Permalink
Control color value input fields
Browse files Browse the repository at this point in the history
  • Loading branch information
DMagic1 committed Feb 22, 2017
1 parent a64b407 commit 552b8d2
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions SCANsat.Unity/HSVPicker/UI/ColorInput.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using UnityEngine;
using UnityEngine.UI;
using System;
using SCANsat.Unity.HSVPicker.Enum;

namespace SCANsat.Unity.HSVPicker.UI
{
/// <summary>
/// Displays one of the color values of aColorPicker
/// </summary>
[RequireComponent(typeof(InputField))]
public class ColorInput : MonoBehaviour
{
public ColorPicker hsvpicker;

/// <summary>
/// Which value this slider can edit.
/// </summary>
public ColorValues type;

private InputField inputField;

private void Awake()
{
inputField = GetComponent<InputField>();

inputField.onValueChanged.AddListener(InputChanged);
}

private void OnDestroy()
{
inputField.onValueChanged.RemoveListener(InputChanged);
}

private void InputChanged(string input)
{
if (string.IsNullOrEmpty(input))
return;

float original = 0;

switch(type)
{
case ColorValues.R:
original = hsvpicker.R;
break;
case ColorValues.G:
original = hsvpicker.G;
break;
case ColorValues.B:
original = hsvpicker.B;
break;
}

float f = original;

if (!float.TryParse(input, out f))
return;

if (f < 0)
return;
else if (input.StartsWith("1."))
f = 1;
else if (f >= 1 && f <= 255)
{
f = Mathf.RoundToInt(f);

f /= 255;
}
else if (f > 255)
return;

hsvpicker.AssignColor(type, f);
}
}
}

0 comments on commit 552b8d2

Please sign in to comment.