Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using System;
- using Manager;
- public class InputManager : ManagerBase<InputManager> {
- Dictionary<string,Expression> translatedBindings=new Dictionary<string,Expression>();
- Dictionary<string,KeyCode> keyCodeReverseToString=new Dictionary<string,KeyCode>();
- public TextAsset bindingsFile;
- // to make instance work inside editor, after script recompile
- void OnValidate() {
- Start ();
- }
- void Awake() {
- Start ();
- }
- void Start() {
- for(int x=0; x<9999; x++) {
- KeyCode key=(KeyCode)x;
- if(!keyCodeReverseToString.ContainsKey(key.ToString()))
- keyCodeReverseToString.Add(key.ToString(),key);
- }
- if(bindingsFile) {
- foreach(string line in bindingsFile.text.Split('\n')) {
- Bind(line);
- }
- } else {
- Debug.LogWarning("No key bindings file");
- }
- }
- /*
- Dictionary<Expression, bool> lastIsDown=new Dictionary<Expression, bool>();
- void LateUpdate() {
- foreach (var binding in translatedBindings) {
- bool val=binding.Value.Evaluate().bool_;
- if(lastIsDown.ContainsKey(binding.Value)) {
- if(lastIsDown[binding.Value]!=val) {
- Hooks.KeyPressed(binding.Key, val);
- lastIsDown[binding.Value]=val;
- }
- } else {
- lastIsDown[binding.Value]=val;
- Hooks.KeyPressed(binding.Key, val);
- }
- }
- }
- */
- /*
- Camera/Adjust/Enable = LeftControl
- Camera/Adjust/Horizontal Value = Mouse X * 10
- Camera/Adjust/Vertical Value = Mouse Y * 10
- Camera/Adjust/Distance Value = Mouse ScrollWheel * 20
- Movement/Speed Adjust/Enable = LeftShift
- Movement/Speed Adjust/Value = Mouse ScrollWheel
- Movement/Jump = Space
- Movement/Horizontal Value = Horizontal * 50
- Movement/Vertical Value = Vertical * 50
- Movement/Attack = Mouse0
- Movement/Aim/Enable = ~Mouse1
- Movement/Aim/Horizontal Value = Mouse X * 50
- Movement/Aim/Vertical Value = Mouse Y * 50
- */
- public static Value Get(string name) {
- if(instance.translatedBindings.ContainsKey(name)) {
- //Debug.Log(instance.translatedBindings[name].ToString());// + " = "+instance.translatedBindings[name].Evaluate());
- return instance.translatedBindings[name].Evaluate();
- } else {
- Debug.Log(name+" is not binded");
- return new Value();
- }
- }
- /*public static T Get<T>(string name) {
- Value v=GetValue(name);
- if(typeof(T)==typeof(bool)) return v.bool_;
- if(typeof(T)==typeof(float)) return v.float_;
- if(typeof(T)==typeof(Vector2)) return v.vector2_;
- if(typeof(T)==typeof(Vector3)) return v.vector3_;
- return null;
- }*/
- // value to return, can conduct different operations based on type
- // define implicit (not exactly correct) values of other types
- public class Value {
- public Value() {
- }
- public Value Set(bool v) {
- bool_=v;
- float_=bool_?1f:-1f;
- vector2_=bool_?Vector2.one:Vector2.zero;
- vector3_=bool_?Vector3.one:Vector3.zero;
- type=typeof(bool);
- return this;
- }
- public Value Set(float v) {
- float_=v;
- bool_=float_>0f;
- vector2_.x=float_; vector2_.y=float_;
- vector3_.x=float_; vector3_.y=float_; vector3_.z=float_;
- type=typeof(float);
- return this;
- }
- public Value Set(Vector2 v) {
- vector2_=v;
- vector3_.x=vector2_.x; vector3_.y=vector2_.y; vector3_.z=0;
- float_=vector2_.sqrMagnitude;
- bool_=float_>0f;
- type=typeof(Vector2);
- return this;
- }
- public Value Set(Vector3 v) {
- vector3_=v;
- vector2_.x=vector3_.x; vector2_.y=vector3_.y;
- float_=vector3_.sqrMagnitude;
- bool_=float_>0f;
- type=typeof(Vector3);
- return this;
- }
- public override string ToString() {
- return "type="+type.ToString()+" b="+bool_+" f="+float_+" v2="+vector2_+" v3="+vector3_;
- //return " ("+type.ToString()+" "+float_+") ";
- }
- public static explicit operator bool(Value me) {
- return me.bool_;
- }
- public static explicit operator float(Value me) {
- return me.float_;
- }
- public static explicit operator Vector2(Value me) {
- return me.vector2_;
- }
- public static explicit operator Vector3(Value me) {
- return me.vector3_;
- }
- public bool GetBool() {
- return (bool)this;
- }
- public float GetFloat() {
- return (float)this;
- }
- public Vector2 GetVector3() {
- return (Vector2)this;
- }
- public Vector3 GetVector2() {
- return (Vector3)this;
- }
- public Type type=typeof(object);
- public bool bool_=false;
- public float float_=0f;
- public Vector2 vector2_=Vector2.zero;
- public Vector3 vector3_=Vector3.zero;
- }
- // funky key binding language parser: string > class hiearchy
- abstract class Node {
- public abstract Value Evaluate();
- }
- abstract class Expression : Node {
- }
- class KeyLiteral : Expression {
- public KeyCode key;
- public override string ToString() {
- return key.ToString();
- }
- public override Value Evaluate() {
- return new Value().Set(Input.GetKey(key));
- }
- }
- class AxisLiteral : Expression {
- public string axis;
- public override string ToString() {
- return axis;
- }
- public override Value Evaluate() {
- if(axis=="MousePosition") return new Value().Set(Input.mousePosition);
- return new Value().Set(Input.GetAxisRaw(axis));
- }
- }
- class FloatLiteral : Expression {
- public float number;
- public override string ToString() {
- return number.ToString();
- }
- public override Value Evaluate() {
- return new Value().Set(number);
- }
- }
- class UnaryExpression : Expression {
- public char operation;
- public Expression expression;
- public bool toogle;
- public bool lastIsDown;
- public override string ToString() {
- return ""+operation+expression;
- }
- public override Value Evaluate() {
- Value value=expression.Evaluate();
- bool currentIsDown=false;
- if(value.bool_) currentIsDown=true;
- if(operation=='!') {
- value.Set(!currentIsDown);
- } else if(operation=='~') {
- if(currentIsDown==true && lastIsDown==false) toogle=!toogle;
- value.Set(toogle);
- } else if(operation=='+') {
- if(lastIsDown==false && currentIsDown==true) {
- value.Set(true);
- } else {
- value.Set(false);
- }
- } else if(operation=='-') {
- if(lastIsDown==true && currentIsDown==false) {
- value.Set(true);
- } else {
- value.Set(false);
- }
- }
- lastIsDown=currentIsDown;
- return value;
- }
- public static string[] operators={"~","!","+","-"};
- }
- class BinaryExpression : Expression {
- public char operation;
- public Expression expressionLeft, expressionRight;
- public override string ToString() {
- return expressionLeft+" "+operation+" "+expressionRight;
- }
- public override Value Evaluate() {
- Value leftValue=expressionLeft.Evaluate();
- Value rightValue=expressionRight.Evaluate();
- if(operation=='&') {
- leftValue.Set( leftValue.bool_ && rightValue.bool_ );
- } else if(operation=='|') {
- leftValue.Set( leftValue.bool_ || rightValue.bool_ );
- } else if(operation=='*') {
- if(leftValue.type==typeof(float) && leftValue.type==rightValue.type) {
- leftValue.Set( leftValue.float_*rightValue.float_ );
- } else {
- leftValue.Set( Vector3.Scale(leftValue.vector3_,rightValue.vector3_) );
- }
- }
- return leftValue;
- }
- public static string[] operators={"&","|","*"};
- }
- int Find(string[] strings, string lookFor) {
- int ret=0;
- foreach(string s in strings) {
- if(s==lookFor) return ret;
- ret++;
- }
- return -1;
- }
- Expression ParseExpression(string input) {
- input=input.Trim();
- int index=-1;
- int len=1;
- foreach(string op in BinaryExpression.operators) {
- int i=input.IndexOf(op);
- if(
- (index==-1 || i<index) &&
- i!=-1
- ) {
- index=i;
- len=op.Length;
- }
- }
- if(index>=0) {
- BinaryExpression ret=new BinaryExpression();
- ret.operation=input[index];
- ret.expressionLeft=ParseExpression(input.Substring(0,index-1));
- ret.expressionRight=ParseExpression(input.Substring(index+len));
- return ret;
- } else {
- if(Find(UnaryExpression.operators,input[0].ToString())!=-1) {
- UnaryExpression ret=new UnaryExpression();
- ret.operation=input[0];
- ret.expression=ParseExpression(input.Substring(1));
- return ret;
- } else {
- float number;
- if(float.TryParse(input, out number)) {
- FloatLiteral ret=new FloatLiteral();
- ret.number=number;
- return ret;
- } else {
- if(keyCodeReverseToString.ContainsKey(input)) {
- KeyLiteral ret=new KeyLiteral();
- ret.key=keyCodeReverseToString[input];
- return ret;
- } else {
- if(instance.translatedBindings.ContainsKey(input)) {
- return instance.translatedBindings[input];
- } else {
- AxisLiteral ret=new AxisLiteral();
- ret.axis=input;
- return ret;
- }
- }
- }
- }
- }
- }
- public bool Bind(string line) {
- string[] tmp;
- tmp=line.Split(new string[]{"//"},StringSplitOptions.None);
- tmp=tmp[0].Split('=');
- if(tmp.Length!=2) return false;
- translatedBindings[tmp[0].Trim()]=ParseExpression(tmp[1]);
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment