aeroson

Untitled

Jun 20th, 2014
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using System;
  4.  
  5. using Manager;
  6.  
  7. public class InputManager : ManagerBase<InputManager> {
  8.  
  9.  
  10. Dictionary<string,Expression> translatedBindings=new Dictionary<string,Expression>();
  11. Dictionary<string,KeyCode> keyCodeReverseToString=new Dictionary<string,KeyCode>();
  12.  
  13.  
  14. public TextAsset bindingsFile;
  15.  
  16. // to make instance work inside editor, after script recompile
  17. void OnValidate() {
  18. Start ();
  19. }
  20.  
  21. void Awake() {
  22. Start ();
  23. }
  24.  
  25. void Start() {
  26. for(int x=0; x<9999; x++) {
  27. KeyCode key=(KeyCode)x;
  28. if(!keyCodeReverseToString.ContainsKey(key.ToString()))
  29. keyCodeReverseToString.Add(key.ToString(),key);
  30. }
  31. if(bindingsFile) {
  32. foreach(string line in bindingsFile.text.Split('\n')) {
  33. Bind(line);
  34. }
  35. } else {
  36. Debug.LogWarning("No key bindings file");
  37. }
  38. }
  39.  
  40. /*
  41. Dictionary<Expression, bool> lastIsDown=new Dictionary<Expression, bool>();
  42. void LateUpdate() {
  43. foreach (var binding in translatedBindings) {
  44. bool val=binding.Value.Evaluate().bool_;
  45. if(lastIsDown.ContainsKey(binding.Value)) {
  46. if(lastIsDown[binding.Value]!=val) {
  47. Hooks.KeyPressed(binding.Key, val);
  48. lastIsDown[binding.Value]=val;
  49. }
  50. } else {
  51. lastIsDown[binding.Value]=val;
  52. Hooks.KeyPressed(binding.Key, val);
  53. }
  54. }
  55. }
  56. */
  57.  
  58.  
  59.  
  60.  
  61. /*
  62. Camera/Adjust/Enable = LeftControl
  63. Camera/Adjust/Horizontal Value = Mouse X * 10
  64. Camera/Adjust/Vertical Value = Mouse Y * 10
  65. Camera/Adjust/Distance Value = Mouse ScrollWheel * 20
  66.  
  67. Movement/Speed Adjust/Enable = LeftShift
  68. Movement/Speed Adjust/Value = Mouse ScrollWheel
  69.  
  70. Movement/Jump = Space
  71. Movement/Horizontal Value = Horizontal * 50
  72. Movement/Vertical Value = Vertical * 50
  73. Movement/Attack = Mouse0
  74.  
  75. Movement/Aim/Enable = ~Mouse1
  76. Movement/Aim/Horizontal Value = Mouse X * 50
  77. Movement/Aim/Vertical Value = Mouse Y * 50
  78. */
  79.  
  80.  
  81. public static Value Get(string name) {
  82. if(instance.translatedBindings.ContainsKey(name)) {
  83. //Debug.Log(instance.translatedBindings[name].ToString());// + " = "+instance.translatedBindings[name].Evaluate());
  84. return instance.translatedBindings[name].Evaluate();
  85. } else {
  86. Debug.Log(name+" is not binded");
  87. return new Value();
  88. }
  89. }
  90.  
  91. /*public static T Get<T>(string name) {
  92. Value v=GetValue(name);
  93. if(typeof(T)==typeof(bool)) return v.bool_;
  94. if(typeof(T)==typeof(float)) return v.float_;
  95. if(typeof(T)==typeof(Vector2)) return v.vector2_;
  96. if(typeof(T)==typeof(Vector3)) return v.vector3_;
  97. return null;
  98. }*/
  99.  
  100.  
  101. // value to return, can conduct different operations based on type
  102. // define implicit (not exactly correct) values of other types
  103. public class Value {
  104. public Value() {
  105. }
  106. public Value Set(bool v) {
  107. bool_=v;
  108. float_=bool_?1f:-1f;
  109. vector2_=bool_?Vector2.one:Vector2.zero;
  110. vector3_=bool_?Vector3.one:Vector3.zero;
  111. type=typeof(bool);
  112. return this;
  113. }
  114. public Value Set(float v) {
  115. float_=v;
  116. bool_=float_>0f;
  117. vector2_.x=float_; vector2_.y=float_;
  118. vector3_.x=float_; vector3_.y=float_; vector3_.z=float_;
  119. type=typeof(float);
  120. return this;
  121. }
  122. public Value Set(Vector2 v) {
  123. vector2_=v;
  124. vector3_.x=vector2_.x; vector3_.y=vector2_.y; vector3_.z=0;
  125. float_=vector2_.sqrMagnitude;
  126. bool_=float_>0f;
  127. type=typeof(Vector2);
  128. return this;
  129. }
  130. public Value Set(Vector3 v) {
  131. vector3_=v;
  132. vector2_.x=vector3_.x; vector2_.y=vector3_.y;
  133. float_=vector3_.sqrMagnitude;
  134. bool_=float_>0f;
  135. type=typeof(Vector3);
  136. return this;
  137. }
  138. public override string ToString() {
  139. return "type="+type.ToString()+" b="+bool_+" f="+float_+" v2="+vector2_+" v3="+vector3_;
  140. //return " ("+type.ToString()+" "+float_+") ";
  141. }
  142.  
  143. public static explicit operator bool(Value me) {
  144. return me.bool_;
  145. }
  146. public static explicit operator float(Value me) {
  147. return me.float_;
  148. }
  149. public static explicit operator Vector2(Value me) {
  150. return me.vector2_;
  151. }
  152. public static explicit operator Vector3(Value me) {
  153. return me.vector3_;
  154. }
  155.  
  156.  
  157. public bool GetBool() {
  158. return (bool)this;
  159. }
  160. public float GetFloat() {
  161. return (float)this;
  162. }
  163. public Vector2 GetVector3() {
  164. return (Vector2)this;
  165. }
  166. public Vector3 GetVector2() {
  167. return (Vector3)this;
  168. }
  169.  
  170. public Type type=typeof(object);
  171. public bool bool_=false;
  172. public float float_=0f;
  173. public Vector2 vector2_=Vector2.zero;
  174. public Vector3 vector3_=Vector3.zero;
  175. }
  176.  
  177.  
  178.  
  179. // funky key binding language parser: string > class hiearchy
  180. abstract class Node {
  181. public abstract Value Evaluate();
  182. }
  183. abstract class Expression : Node {
  184. }
  185. class KeyLiteral : Expression {
  186. public KeyCode key;
  187. public override string ToString() {
  188. return key.ToString();
  189. }
  190. public override Value Evaluate() {
  191. return new Value().Set(Input.GetKey(key));
  192. }
  193. }
  194. class AxisLiteral : Expression {
  195. public string axis;
  196. public override string ToString() {
  197. return axis;
  198. }
  199. public override Value Evaluate() {
  200. if(axis=="MousePosition") return new Value().Set(Input.mousePosition);
  201. return new Value().Set(Input.GetAxisRaw(axis));
  202. }
  203. }
  204. class FloatLiteral : Expression {
  205. public float number;
  206. public override string ToString() {
  207. return number.ToString();
  208. }
  209. public override Value Evaluate() {
  210. return new Value().Set(number);
  211. }
  212. }
  213. class UnaryExpression : Expression {
  214. public char operation;
  215. public Expression expression;
  216. public bool toogle;
  217. public bool lastIsDown;
  218. public override string ToString() {
  219. return ""+operation+expression;
  220. }
  221. public override Value Evaluate() {
  222. Value value=expression.Evaluate();
  223.  
  224. bool currentIsDown=false;
  225. if(value.bool_) currentIsDown=true;
  226.  
  227. if(operation=='!') {
  228. value.Set(!currentIsDown);
  229. } else if(operation=='~') {
  230. if(currentIsDown==true && lastIsDown==false) toogle=!toogle;
  231. value.Set(toogle);
  232. } else if(operation=='+') {
  233. if(lastIsDown==false && currentIsDown==true) {
  234. value.Set(true);
  235. } else {
  236. value.Set(false);
  237. }
  238. } else if(operation=='-') {
  239. if(lastIsDown==true && currentIsDown==false) {
  240. value.Set(true);
  241. } else {
  242. value.Set(false);
  243. }
  244. }
  245.  
  246. lastIsDown=currentIsDown;
  247. return value;
  248. }
  249. public static string[] operators={"~","!","+","-"};
  250. }
  251. class BinaryExpression : Expression {
  252. public char operation;
  253. public Expression expressionLeft, expressionRight;
  254. public override string ToString() {
  255. return expressionLeft+" "+operation+" "+expressionRight;
  256. }
  257. public override Value Evaluate() {
  258. Value leftValue=expressionLeft.Evaluate();
  259. Value rightValue=expressionRight.Evaluate();
  260. if(operation=='&') {
  261. leftValue.Set( leftValue.bool_ && rightValue.bool_ );
  262. } else if(operation=='|') {
  263. leftValue.Set( leftValue.bool_ || rightValue.bool_ );
  264. } else if(operation=='*') {
  265. if(leftValue.type==typeof(float) && leftValue.type==rightValue.type) {
  266. leftValue.Set( leftValue.float_*rightValue.float_ );
  267. } else {
  268. leftValue.Set( Vector3.Scale(leftValue.vector3_,rightValue.vector3_) );
  269. }
  270. }
  271. return leftValue;
  272. }
  273. public static string[] operators={"&","|","*"};
  274. }
  275.  
  276.  
  277. int Find(string[] strings, string lookFor) {
  278. int ret=0;
  279. foreach(string s in strings) {
  280. if(s==lookFor) return ret;
  281. ret++;
  282. }
  283. return -1;
  284. }
  285.  
  286.  
  287. Expression ParseExpression(string input) {
  288. input=input.Trim();
  289. int index=-1;
  290. int len=1;
  291.  
  292. foreach(string op in BinaryExpression.operators) {
  293. int i=input.IndexOf(op);
  294. if(
  295. (index==-1 || i<index) &&
  296. i!=-1
  297. ) {
  298. index=i;
  299. len=op.Length;
  300. }
  301. }
  302.  
  303.  
  304. if(index>=0) {
  305.  
  306. BinaryExpression ret=new BinaryExpression();
  307. ret.operation=input[index];
  308. ret.expressionLeft=ParseExpression(input.Substring(0,index-1));
  309. ret.expressionRight=ParseExpression(input.Substring(index+len));
  310. return ret;
  311.  
  312. } else {
  313.  
  314. if(Find(UnaryExpression.operators,input[0].ToString())!=-1) {
  315. UnaryExpression ret=new UnaryExpression();
  316. ret.operation=input[0];
  317. ret.expression=ParseExpression(input.Substring(1));
  318. return ret;
  319.  
  320. } else {
  321.  
  322. float number;
  323. if(float.TryParse(input, out number)) {
  324.  
  325. FloatLiteral ret=new FloatLiteral();
  326. ret.number=number;
  327. return ret;
  328.  
  329. } else {
  330.  
  331. if(keyCodeReverseToString.ContainsKey(input)) {
  332.  
  333. KeyLiteral ret=new KeyLiteral();
  334. ret.key=keyCodeReverseToString[input];
  335. return ret;
  336.  
  337. } else {
  338.  
  339. if(instance.translatedBindings.ContainsKey(input)) {
  340.  
  341. return instance.translatedBindings[input];
  342.  
  343. } else {
  344.  
  345. AxisLiteral ret=new AxisLiteral();
  346. ret.axis=input;
  347. return ret;
  348.  
  349. }
  350. }
  351. }
  352. }
  353. }
  354. }
  355.  
  356. public bool Bind(string line) {
  357. string[] tmp;
  358. tmp=line.Split(new string[]{"//"},StringSplitOptions.None);
  359. tmp=tmp[0].Split('=');
  360. if(tmp.Length!=2) return false;
  361. translatedBindings[tmp[0].Trim()]=ParseExpression(tmp[1]);
  362. return true;
  363. }
  364.  
  365. }
Advertisement
Add Comment
Please, Sign In to add comment