using UnityEngine;
using TNet;
using System.Collections;
public class UIGameKeyBinding : UIKeyBinding
{
static public UIGameKeyBinding rebind;
static KeyCode cancelKey = KeyCode.None;
/// <summary>
/// Optional caption showing the key.
/// </summary>
public UILabel caption;
/// <summary>
/// Optional name of the key binding, such as "Volley".
/// </summary>
[System.NonSerialized] public string visibleName;
public string regularBinding;
public string controllerBinding;
[System.NonSerialized] UISkillIcon mIcon;
[System.NonSerialized] bool mLoaded = false;
[System.NonSerialized] KeyCode mKey0 = KeyCode.None;
[System.NonSerialized] KeyCode mKey1 = KeyCode.None;
[System.NonSerialized] int mStartFrame = 0;
public delegate void OnChange();
public OnChange onChange;
/// <summary>
/// Key used for the binding, such as "SkillVolley"
/// </summary>
public string bindingName
{
set
{
regularBinding = value;
controllerBinding = value + " Controller";
}
}
/// <summary>
/// Active binding, letting you have different key binds depending on the control method.
/// </summary>
string activeBindingName
{
get
{
return (UICamera.currentScheme != UICamera.ControlScheme.Controller || string.IsNullOrEmpty(controllerBinding)) ?
regularBinding : controllerBinding;
}
}
/// <summary>
/// Key binding associated with this skill.
/// </summary>
public KeyCode savedKeyBinding
{
get
{
if (!mLoaded)
{
mLoaded = true;
UpdateKeyBindings();
}
return (UICamera.currentScheme != UICamera.ControlScheme.Controller) ? mKey0 : mKey1;
}
set
{
if (!mLoaded)
{
mLoaded = true;
UpdateKeyBindings();
}
if (value == KeyCode.None)
{
DataNode node = TNManager.playerDataNode.GetChild("Keybindings", false);
if (node != null) node.RemoveChild(activeBindingName);
}
else
{
DataNode node = TNManager.playerDataNode.GetChild("Keybindings", true);
node.SetChild(activeBindingName, value.ToString());
}
Cache();
MyPlayer.syncNeeded = true;
MyPlayer.saveNeeded = true;
UpdateSelf();
}
}
/// <summary>
/// Automatically load the key bindings.
/// </summary>
protected override void Start ()
{
base.Start();
mIcon = GetComponent<UISkillIcon>();
if (!mLoaded)
{
mLoaded = true;
UpdateKeyBindings();
}
}
/// <summary>
/// Update the skill's keybindings.
/// </summary>
public void UpdateKeyBindings ()
{
mKey0 = KeyCode.None;
mKey1 = KeyCode.None;
DataNode node = TNManager.playerDataNode.GetChild("Keybindings", false);
if (node == null)
{
node = TNManager.playerDataNode.GetChild("Keybindings", true);
node.AddMissingChild("SkillRepair", "R");
node.AddMissingChild("SkillNormalVolley", "V");
node.AddMissingChild("SkillAimedVolley", "V");
node.AddMissingChild("SkillRecklessVolley", "V");
node.AddMissingChild("SkillHaste", "H");
node.AddMissingChild("SkillChainShot", "C");
node.AddMissingChild("SkillInferno", "Q");
node.AddMissingChild("SkillPoison", "E");
node.AddMissingChild("SkillFog", "F");
node.AddMissingChild("SkillDeflect", "G");
node.AddMissingChild("SkillImprove", "B");
MyPlayer.saveNeeded = true;
}
// Auto-upgrade
if (node.GetChild("Map") == null)
{
node.AddMissingChild("Map", "M");
node.AddMissingChild("Inventory", "I");
node.AddMissingChild("Talents", "K");
node.AddMissingChild("Fast Travel", "T");
node.AddMissingChild("Players", "Tab");
MyPlayer.saveNeeded = true;
}
// Controller binds
if (node.GetChild("SkillRepair Controller") == null)
{
node.AddMissingChild("SkillRepair Controller", "JoystickButton0");
node.AddMissingChild("SkillNormalVolley Controller", "JoystickButton1");
node.AddMissingChild("SkillAimedVolley Controller", "JoystickButton1");
node.AddMissingChild("SkillRecklessVolley Controller", "JoystickButton1");
node.AddMissingChild("SkillInferno Controller", "JoystickButton2");
node.AddMissingChild("SkillPoison Controller", "JoystickButton3");
MyPlayer.saveNeeded = true;
}
// Restore the key bindings
if (node != null)
{
Cache();
UpdateSelf();
}
}
void Cache ()
{
DataNode node = TNManager.playerDataNode.GetChild("Keybindings", false);
if (node != null)
{
mKey0 = GetKeyCode(node, regularBinding);
mKey1 = !string.IsNullOrEmpty(controllerBinding) ? GetKeyCode(node, controllerBinding) : mKey0;
}
}
static KeyCode GetKeyCode (DataNode node, string bindingName)
{
node = node.GetChild(bindingName);
if (node
!= null && node
.value is string) {
try { return (KeyCode
)System.Enum.Parse(typeof(KeyCode
),
(string)node
.value); } catch (System.Exception) { }
}
return KeyCode.None;
}
protected override void OnEnable () { base.OnEnable(); UICamera.onSchemeChange += UpdateSelf; }
protected override void OnDisable () { base.OnDisable(); UICamera.onSchemeChange -= UpdateSelf; }
/// <summary>
/// Update the caption and the bound key code.
/// </summary>
void UpdateSelf ()
{
KeyCode saved = savedKeyBinding;
// Skill bar will have a skill selected when editing the layout
if (UISkillBar.isEditing && UICamera.currentScheme == UICamera.ControlScheme.Controller)
{
keyCode = KeyCode.None;
if (saved != KeyCode.None)
{
if (caption != null)
{
caption.text = NGUITools.KeyToCaption(saved);
caption
.color = new Color
(0
.35f, 0
.35f, 0
.35f
); caption.symbolStyle = NGUIText.SymbolStyle.Colored;
}
}
else if (caption != null) caption.text = "";
}
else if (saved != KeyCode.None)
{
keyCode = KeyCode.None;
StartCoroutine(DelayedKeyChange(saved));
if (caption != null)
{
caption.text = NGUITools.KeyToCaption(saved);
caption.color = Color.white;
caption.symbolStyle = NGUIText.SymbolStyle.Normal;
}
}
else
{
keyCode = KeyCode.None;
if (caption != null) caption.text = "";
}
}
/// <summary>
/// Coroutine is used so that the key binding doesn't trigger in the same frame.
/// </summary>
IEnumerator DelayedKeyChange (KeyCode key)
{
yield return null;
keyCode = key;
}
/// <summary>
/// Cancel the rebinding operation.
/// </summary>
void CancelRebind ()
{
rebind = null;
cancelKey = KeyCode.None;
}
/// <summary>
/// Update the highlight and the cooldown timer.
/// </summary>
protected override void Update ()
{
if (rebind == this && cancelKey != KeyCode.None)
{
if (UICamera.GetKeyUp(cancelKey))
{
Invoke("CancelRebind", 0.001f);
return;
}
}
if (rebind == this && mStartFrame != Time.frameCount && Input.anyKeyDown)
{
string s = visibleName ?? Localization.Get(regularBinding);
if (UICamera.GetKeyDown(KeyCode.Escape) ||
UICamera.GetKeyDown(KeyCode.Mouse1) ||
UICamera.GetKeyDown(KeyCode.JoystickButton4) ||
UICamera.GetKeyDown(KeyCode.JoystickButton5))
{
rebind = null;
savedKeyBinding = KeyCode.None;
if (onChange != null) onChange();
string text = Localization.Format("Key Binding Cleared", "[ffcc00]" + s + "[-]");
UIStatusBar.Show(text, 2f);
}
else if (!UICamera.GetKeyDown(KeyCode.Mouse0))
{
for (int i = 0, imax = NGUITools.keys.Length; i < imax; ++i)
{
KeyCode key = NGUITools.keys[i];
if (UICamera.GetKeyDown(key))
{
savedKeyBinding = key;
if (onChange != null) onChange();
string text = Localization.Format("Key Binding Set",
"[ffcc00]" + NGUITools.KeyToCaption(key) + "[-]",
"[ffcc00]" + s + "[-]");
UIStatusBar.Show(text, 2f);
cancelKey = key;
break;
}
}
}
}
else if (!UICamera.inputHasFocus && (UICamera.disableController || keyCode < KeyCode.JoystickButton0) && !UISkillBar.isEditing)
base.Update();
}
/// <summary>
/// Releasing the controller button should activate the skill.
/// </summary>
protected override void OnBindingPress (bool isPressed)
{
UIProTip.Show(33);
base.OnBindingPress(isPressed);
}
/// <summary>
/// Right-click rebind option.
/// </summary>
void OnClick ()
{
if (UICamera.currentTouchID == -2 && mIcon == null)
{
NGUITools.PlaySound(GameAudio.instance.tap);
ShowRebindOption();
}
}
/// <summary>
/// Show a simple "Rebind" popup list option.
/// </summary>
public void ShowRebindOption ()
{
UIPopupList popup = UIGameWindow.popupList;
if (popup != null)
{
popup.Clear();
popup.AddItem(Localization.Get("Key Binding"), "Rebind");
EventDelegate.Set(popup.onChange, OnPopupItem);
popup.Show();
}
}
/// <summary>
/// Change options on popup selection.
/// </summary>
void OnPopupItem ()
{
string data = UIPopupList.current.data as string;
if (data == "Rebind") Rebind();
UICamera.selectedObject = null;
}
/// <summary>
/// Start the rebinding process.
/// </summary>
public void Rebind ()
{
UIProTip.Hide(33);
rebind = this;
mStartFrame = Time.frameCount;
string s = visibleName ?? regularBinding;
UIStatusBar.Show(Localization.Format("Key Binding Info", "[ffcc00]" + s + "[-]"));
}
}