@@ -25,8 +25,8 @@ public class ConsoleGUI : MonoBehaviour {
25
25
private void Start ( ) {
26
26
consoleRect = new Rect ( 0 , 0 , Screen . width , Mathf . Min ( 300 , Screen . height ) ) ;
27
27
consoleLog = ConsoleLog . Instance ;
28
- consoleCommandsRepository = ConsoleCommandsRepository . Instance ;
29
- }
28
+ consoleCommandsRepository = ConsoleCommandsRepository . Instance ;
29
+ }
30
30
31
31
private void OnEnable ( ) {
32
32
focus = true ;
@@ -40,23 +40,23 @@ public void OnGUI() {
40
40
GUILayout . Window ( WINDOW_ID , consoleRect , RenderWindow , "Console" ) ;
41
41
}
42
42
private void RenderWindow ( int id ) {
43
- if ( fixPositionNextFrame )
44
- {
45
- MoveCursorToPos ( input . Length ) ;
46
- fixPositionNextFrame = false ;
47
- }
43
+ if ( fixPositionNextFrame )
44
+ {
45
+ MoveCursorToPos ( input . Length ) ;
46
+ fixPositionNextFrame = false ;
47
+ }
48
48
HandleSubmit ( ) ;
49
49
HandleEscape ( ) ;
50
- HandleTab ( ) ;
51
- HandleUp ( ) ;
52
- HandleDown ( ) ;
50
+ HandleTab ( ) ;
51
+ HandleUp ( ) ;
52
+ HandleDown ( ) ;
53
53
scrollPosition = GUILayout . BeginScrollView ( new Vector2 ( 0 , scrollPosition ) , false , true ) . y ;
54
- if ( consoleLog . fresh )
55
- {
56
- scrollPosition = consoleLog . scrollLength ;
57
- consoleLog . fresh = false ;
58
- }
59
- GUILayout . Label ( consoleLog . log ) ;
54
+ if ( consoleLog . fresh )
55
+ {
56
+ scrollPosition = consoleLog . scrollLength ;
57
+ consoleLog . fresh = false ;
58
+ }
59
+ GUILayout . Label ( consoleLog . log ) ;
60
60
GUILayout . EndScrollView ( ) ;
61
61
GUI . SetNextControlName ( "input" ) ;
62
62
input = GUILayout . TextField ( input ) ;
@@ -65,95 +65,95 @@ private void RenderWindow(int id) {
65
65
focus = false ;
66
66
}
67
67
}
68
-
69
-
70
- private string LargestSubString ( string in1 , string in2 ) // takes two strings and returns the largest matching substring.
71
- {
72
- string output = "" ;
73
- int smallestLen = Mathf . Min ( in1 . Length , in2 . Length ) ;
74
- for ( int i = 0 ; i < smallestLen ; i ++ ) {
75
- if ( in1 [ i ] == in2 [ i ] ) output += in1 [ i ] ;
76
- else return output ;
77
- }
78
- return output ;
79
- }
80
-
81
- private void MoveCursorToPos ( int position )
82
- {
83
- TextEditor editor = ( TextEditor ) GUIUtility . GetStateObject ( typeof ( TextEditor ) , GUIUtility . keyboardControl ) ;
68
+
69
+
70
+ private string LargestSubString ( string in1 , string in2 ) // takes two strings and returns the largest matching substring.
71
+ {
72
+ string output = "" ;
73
+ int smallestLen = Mathf . Min ( in1 . Length , in2 . Length ) ;
74
+ for ( int i = 0 ; i < smallestLen ; i ++ ) {
75
+ if ( in1 [ i ] == in2 [ i ] ) output += in1 [ i ] ;
76
+ else return output ;
77
+ }
78
+ return output ;
79
+ }
80
+
81
+ private void MoveCursorToPos ( int position )
82
+ {
83
+ TextEditor editor = ( TextEditor ) GUIUtility . GetStateObject ( typeof ( TextEditor ) , GUIUtility . keyboardControl ) ;
84
84
#if UNITY_5_2 || UNITY_5_3 || UNITY_5_3_OR_NEWER
85
- editor . selectIndex = position ;
86
- editor . cursorIndex = position ;
85
+ editor . selectIndex = position ;
86
+ editor . cursorIndex = position ;
87
87
#else
88
-
89
- editor . selectPos = position ;
90
- editor . pos = position ;
88
+
89
+ editor . selectPos = position ;
90
+ editor . pos = position ;
91
91
#endif
92
- return ;
93
- }
94
-
95
- private void HandleTab ( )
96
- {
97
- if ( KeyDown ( "tab" ) ) {
98
- if ( input != "" ) { // don't do anything if the input field is still blank.
99
- List < string > search = consoleCommandsRepository . SearchCommands ( input ) ;
100
- if ( search . Count == 0 ) { // nothing found
101
- consoleLog . Log ( "No command start with \" " + input + "\" ." ) ;
102
- input = "" ; // clear input
103
- return ;
104
- } else if ( search . Count == 1 ) {
105
- input = search [ 0 ] + " " ; // only found one command - type it in for the guy
106
- MoveCursorToPos ( input . Length ) ;
107
- } else {
108
- consoleLog . Log ( "Commands starting with \" " + input + "\" :" ) ;
109
- string largestMatch = search [ 0 ] ; // keep track of the largest substring that matches all searches
110
- foreach ( string command in search )
111
- {
112
- consoleLog . Log ( command ) ;
113
- largestMatch = LargestSubString ( largestMatch , command ) ;
114
- }
115
- input = largestMatch ;
116
- MoveCursorToPos ( input . Length ) ;
117
- }
118
- }
119
- }
120
- }
121
-
122
- private void HandleUp ( )
123
- {
124
- if ( KeyDown ( "up" ) ) {
125
- consoleHistoryPosition += 1 ;
126
- if ( consoleHistoryPosition > consoleHistoryCommands . Count - 1 ) consoleHistoryPosition = consoleHistoryCommands . Count - 1 ;
127
- input = consoleHistoryCommands [ consoleHistoryPosition ] ;
128
- fixPositionNextFrame = true ;
129
- //MoveCursorToPos(input.Length);
130
- }
131
- }
132
-
133
- private void HandleDown ( )
134
- {
135
- if ( KeyDown ( "down" ) ) {
136
- consoleHistoryPosition -= 1 ;
137
- if ( consoleHistoryPosition < 0 ) {
138
- consoleHistoryPosition = - 1 ;
139
- input = "" ;
140
- }
141
- else
142
- input = consoleHistoryCommands [ consoleHistoryPosition ] ;
143
- MoveCursorToPos ( input . Length ) ;
144
- }
145
- }
92
+ return ;
93
+ }
94
+
95
+ private void HandleTab ( )
96
+ {
97
+ if ( KeyDown ( "tab" ) ) {
98
+ if ( input != "" ) { // don't do anything if the input field is still blank.
99
+ List < string > search = consoleCommandsRepository . SearchCommands ( input ) ;
100
+ if ( search . Count == 0 ) { // nothing found
101
+ consoleLog . Log ( "No command start with \" " + input + "\" ." ) ;
102
+ input = "" ; // clear input
103
+ return ;
104
+ } else if ( search . Count == 1 ) {
105
+ input = search [ 0 ] + " " ; // only found one command - type it in for the guy
106
+ MoveCursorToPos ( input . Length ) ;
107
+ } else {
108
+ consoleLog . Log ( "Commands starting with \" " + input + "\" :" ) ;
109
+ string largestMatch = search [ 0 ] ; // keep track of the largest substring that matches all searches
110
+ foreach ( string command in search )
111
+ {
112
+ consoleLog . Log ( command ) ;
113
+ largestMatch = LargestSubString ( largestMatch , command ) ;
114
+ }
115
+ input = largestMatch ;
116
+ MoveCursorToPos ( input . Length ) ;
117
+ }
118
+ }
119
+ }
120
+ }
121
+
122
+ private void HandleUp ( )
123
+ {
124
+ if ( KeyDown ( "up" ) ) {
125
+ consoleHistoryPosition += 1 ;
126
+ if ( consoleHistoryPosition > consoleHistoryCommands . Count - 1 ) consoleHistoryPosition = consoleHistoryCommands . Count - 1 ;
127
+ input = consoleHistoryCommands [ consoleHistoryPosition ] ;
128
+ fixPositionNextFrame = true ;
129
+ //MoveCursorToPos(input.Length);
130
+ }
131
+ }
132
+
133
+ private void HandleDown ( )
134
+ {
135
+ if ( KeyDown ( "down" ) ) {
136
+ consoleHistoryPosition -= 1 ;
137
+ if ( consoleHistoryPosition < 0 ) {
138
+ consoleHistoryPosition = - 1 ;
139
+ input = "" ;
140
+ }
141
+ else
142
+ input = consoleHistoryCommands [ consoleHistoryPosition ] ;
143
+ MoveCursorToPos ( input . Length ) ;
144
+ }
145
+ }
146
146
147
147
private void HandleSubmit ( ) {
148
148
if ( KeyDown ( "[enter]" ) || KeyDown ( "return" ) ) {
149
- consoleHistoryPosition = - 1 ; // up arrow or down arrow will set it to 0, which is the last command typed.
150
- if ( submitAction != null ) {
149
+ consoleHistoryPosition = - 1 ; // up arrow or down arrow will set it to 0, which is the last command typed.
150
+ if ( submitAction != null ) {
151
151
submitAction . Activate ( ) ;
152
- consoleHistoryCommands . Insert ( 0 , input ) ;
153
- if ( consoleHistoryCommands . Count > maxConsoleHistorySize )
154
- consoleHistoryCommands . RemoveAt ( consoleHistoryCommands . Count - 1 ) ;
152
+ consoleHistoryCommands . Insert ( 0 , input ) ;
153
+ if ( consoleHistoryCommands . Count > maxConsoleHistorySize )
154
+ consoleHistoryCommands . RemoveAt ( consoleHistoryCommands . Count - 1 ) ;
155
155
}
156
- input = "" ;
156
+ input = "" ;
157
157
}
158
158
}
159
159
@@ -164,10 +164,10 @@ private void HandleEscape() {
164
164
}
165
165
}
166
166
167
- private void Update ( ) {
168
- if ( input == "`" )
169
- input = "" ;
170
- }
167
+ private void Update ( ) {
168
+ if ( input == "`" )
169
+ input = "" ;
170
+ }
171
171
172
172
private bool KeyDown ( string key ) {
173
173
return Event . current . Equals ( Event . KeyboardEvent ( key ) ) ;
0 commit comments