forked from CosignCosine/CS-NetPicker3
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Improved QCommon support
- Loading branch information
Showing
5 changed files
with
26 additions
and
180 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,180 +1,19 @@ | ||
using ColossalFramework; | ||
using ColossalFramework.Globalization; | ||
using ColossalFramework.UI; | ||
using Picker.Localization; | ||
using System.Reflection; | ||
using QCommonLib; | ||
using UnityEngine; | ||
|
||
// Based on Move It's code | ||
namespace Picker | ||
{ | ||
public class OptionsKeymappingMain : OptionsKeymapping | ||
public class OptionsKeymapping : QKeybinding | ||
{ | ||
private void Awake() | ||
{ | ||
AddKeymapping(Localize.keybind_ToggleTool, toggleTool); | ||
} | ||
} | ||
|
||
public class OptionsKeymapping : UICustomControl | ||
{ | ||
protected static readonly string kKeyBindingTemplate = "KeyBindingTemplate"; | ||
|
||
protected SavedInputKey m_EditingBinding; | ||
|
||
protected string m_EditingBindingCategory; | ||
|
||
public static readonly SavedInputKey toggleTool = new SavedInputKey("toggleTool", Picker.settingsFileName, SavedInputKey.Encode(KeyCode.T, false, false, false), true); | ||
|
||
protected int count = 0; | ||
|
||
protected void AddKeymapping(string label, SavedInputKey savedInputKey) | ||
{ | ||
UIPanel uIPanel = component.AttachUIComponent(UITemplateManager.GetAsGameObject(kKeyBindingTemplate)) as UIPanel; | ||
if (count++ % 2 == 1) uIPanel.backgroundSprite = null; | ||
|
||
UILabel uILabel = uIPanel.Find<UILabel>("Name"); | ||
UIButton uIButton = uIPanel.Find<UIButton>("Binding"); | ||
uIButton.eventKeyDown += new KeyPressHandler(this.OnBindingKeyDown); | ||
uIButton.eventMouseDown += new MouseEventHandler(this.OnBindingMouseDown); | ||
|
||
uILabel.text = label; | ||
uIButton.text = savedInputKey.ToLocalizedString("KEYNAME"); | ||
uIButton.objectUserData = savedInputKey; | ||
} | ||
|
||
protected bool IsModifierKey(KeyCode code) | ||
protected override void Awake() | ||
{ | ||
return code == KeyCode.LeftControl || code == KeyCode.RightControl || code == KeyCode.LeftShift || code == KeyCode.RightShift || code == KeyCode.LeftAlt || code == KeyCode.RightAlt; | ||
} | ||
|
||
protected bool IsControlDown() | ||
{ | ||
return Input.GetKey(KeyCode.LeftControl) || Input.GetKey(KeyCode.RightControl); | ||
} | ||
|
||
protected bool IsShiftDown() | ||
{ | ||
return Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift); | ||
} | ||
PressAnyKeyStr = Localize.key_pressAnyKey; | ||
|
||
protected bool IsAltDown() | ||
{ | ||
return Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt); | ||
} | ||
|
||
protected bool IsUnbindableMouseButton(UIMouseButton code) | ||
{ | ||
return code == UIMouseButton.Left || code == UIMouseButton.Right; | ||
} | ||
|
||
protected KeyCode ButtonToKeycode(UIMouseButton button) | ||
{ | ||
if (button == UIMouseButton.Left) | ||
{ | ||
return KeyCode.Mouse0; | ||
} | ||
if (button == UIMouseButton.Right) | ||
{ | ||
return KeyCode.Mouse1; | ||
} | ||
if (button == UIMouseButton.Middle) | ||
{ | ||
return KeyCode.Mouse2; | ||
} | ||
if (button == UIMouseButton.Special0) | ||
{ | ||
return KeyCode.Mouse3; | ||
} | ||
if (button == UIMouseButton.Special1) | ||
{ | ||
return KeyCode.Mouse4; | ||
} | ||
if (button == UIMouseButton.Special2) | ||
{ | ||
return KeyCode.Mouse5; | ||
} | ||
if (button == UIMouseButton.Special3) | ||
{ | ||
return KeyCode.Mouse6; | ||
} | ||
return KeyCode.None; | ||
} | ||
|
||
protected void OnBindingKeyDown(UIComponent comp, UIKeyEventParameter p) | ||
{ | ||
if (this.m_EditingBinding != null && !this.IsModifierKey(p.keycode)) | ||
{ | ||
p.Use(); | ||
UIView.PopModal(); | ||
KeyCode keycode = p.keycode; | ||
InputKey inputKey = (p.keycode == KeyCode.Escape) ? this.m_EditingBinding.value : SavedInputKey.Encode(keycode, p.control, p.shift, p.alt); | ||
if (p.keycode == KeyCode.Backspace) | ||
{ | ||
inputKey = SavedInputKey.Empty; | ||
} | ||
this.m_EditingBinding.value = inputKey; | ||
UITextComponent uITextComponent = p.source as UITextComponent; | ||
uITextComponent.text = this.m_EditingBinding.ToLocalizedString("KEYNAME"); | ||
this.m_EditingBinding = null; | ||
this.m_EditingBindingCategory = string.Empty; | ||
} | ||
} | ||
|
||
protected void OnBindingMouseDown(UIComponent comp, UIMouseEventParameter p) | ||
{ | ||
if (this.m_EditingBinding == null) | ||
{ | ||
p.Use(); | ||
this.m_EditingBinding = (SavedInputKey)p.source.objectUserData; | ||
this.m_EditingBindingCategory = p.source.stringUserData; | ||
UIButton uIButton = p.source as UIButton; | ||
uIButton.buttonsMask = (UIMouseButton.Left | UIMouseButton.Right | UIMouseButton.Middle | UIMouseButton.Special0 | UIMouseButton.Special1 | UIMouseButton.Special2 | UIMouseButton.Special3); | ||
uIButton.text = "Press any key"; | ||
p.source.Focus(); | ||
UIView.PushModal(p.source); | ||
} | ||
else if (!this.IsUnbindableMouseButton(p.buttons)) | ||
{ | ||
p.Use(); | ||
UIView.PopModal(); | ||
InputKey inputKey = SavedInputKey.Encode(this.ButtonToKeycode(p.buttons), this.IsControlDown(), this.IsShiftDown(), this.IsAltDown()); | ||
|
||
this.m_EditingBinding.value = inputKey; | ||
UIButton uIButton2 = p.source as UIButton; | ||
uIButton2.text = this.m_EditingBinding.ToLocalizedString("KEYNAME"); | ||
uIButton2.buttonsMask = UIMouseButton.Left; | ||
this.m_EditingBinding = null; | ||
this.m_EditingBindingCategory = string.Empty; | ||
} | ||
} | ||
|
||
protected InputKey GetDefaultEntry(string entryName) | ||
{ | ||
FieldInfo field = typeof(DefaultSettings).GetField(entryName, BindingFlags.Static | BindingFlags.Public); | ||
if (field == null) | ||
{ | ||
return 0; | ||
} | ||
object value = field.GetValue(null); | ||
if (value is InputKey) | ||
{ | ||
return (InputKey)value; | ||
} | ||
return 0; | ||
} | ||
|
||
protected void RefreshKeyMapping() | ||
{ | ||
foreach (UIComponent current in component.GetComponentsInChildren<UIComponent>()) | ||
{ | ||
UITextComponent uITextComponent = current.Find<UITextComponent>("Binding"); | ||
SavedInputKey savedInputKey = (SavedInputKey)uITextComponent.objectUserData; | ||
if (this.m_EditingBinding != savedInputKey) | ||
{ | ||
uITextComponent.text = savedInputKey.ToLocalizedString("KEYNAME"); | ||
} | ||
} | ||
AddKeymapping(Localize.keybind_ToggleTool, toggleTool); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters