3
3
4
4
import pyglet
5
5
6
- from pyglet .event import EventDispatcher
7
6
from pyglet .text import caret , document , layout
8
7
from pyglet .window import key
9
8
12
11
from cocos .layer .util_layers import ColorLayer
13
12
from cocos .interpreter import Interpreter
14
13
14
+ LINE_HEIGHT = 20
15
15
16
- class InterpreterLayer (Layer , EventDispatcher ):
16
+
17
+ class InterpreterLayer (Layer ):
17
18
name = "py"
18
19
19
20
prompt = ">>> " #: first line prompt
@@ -104,6 +105,8 @@ def write(self, text):
104
105
'color' : self .cfg ['code.color' ],
105
106
})
106
107
self .layout .end_update ()
108
+ lines = len (text .split ())
109
+ self ._scroll (lines )
107
110
108
111
#################
109
112
# Layer API
@@ -129,6 +132,9 @@ def write_prompt(self, first_line=True):
129
132
self .prompt_end = len (self .document .text )
130
133
self .caret .position = len (self .document .text )
131
134
135
+ def _scroll (self , amount ):
136
+ self .layout .view_y -= amount * LINE_HEIGHT
137
+
132
138
#################
133
139
# Layer Events
134
140
#################
@@ -162,7 +168,7 @@ def on_exit(self):
162
168
#################
163
169
def on_text (self , text ):
164
170
if text == '\r ' :
165
- self .dispatch_event ( ' on_command' , self .get_command ())
171
+ self .on_command ( self .get_command ())
166
172
else :
167
173
self .caret .on_text (text )
168
174
@@ -178,9 +184,9 @@ def on_text_motion(self, motion):
178
184
elif motion == key .MOTION_DELETE :
179
185
self .caret .on_text_motion (motion )
180
186
elif motion == key .MOTION_UP :
181
- self .interpreter .dispatch_event ( ' on_history_prev' , self .get_command ())
187
+ self .interpreter .on_history_prev ( self .get_command ())
182
188
elif motion == key .MOTION_DOWN :
183
- self .interpreter .dispatch_event ( ' on_history_next' , self .get_command ())
189
+ self .interpreter .on_history_next ( self .get_command ())
184
190
elif motion == key .MOTION_BEGINNING_OF_LINE :
185
191
self .caret .position = self .prompt_start
186
192
elif motion == key .MOTION_END_OF_LINE :
@@ -193,7 +199,7 @@ def on_text_motion_select(self, motion):
193
199
# Mouse Events
194
200
#################
195
201
def on_mouse_scroll (self , x , y , dx , dy ):
196
- self .layout . view_y += dy * 16
202
+ self ._scroll ( dy )
197
203
198
204
def on_mouse_press (self , x , y , button , modifiers ):
199
205
self .caret .on_mouse_press (x , y , button , modifiers )
@@ -225,11 +231,11 @@ def on_key_press(self, symbol, modifiers):
225
231
self .document .delete_text (self .prompt_end , len (self .document .text ))
226
232
self .caret .position = self .prompt_end
227
233
elif symbol == key .TAB :
228
- self .dispatch_event ( ' on_completion' , self .get_command ())
234
+ self .on_completion ( self .get_command ())
229
235
elif symbol == key .PAGEUP :
230
- self .layout . view_y += 16
236
+ self ._scroll ( - 1 )
231
237
elif symbol == key .PAGEDOWN :
232
- self .layout . view_y -= 16
238
+ self ._scroll ( 1 )
233
239
else :
234
240
return pyglet .event .EVENT_UNHANDLED
235
241
return pyglet .event .EVENT_HANDLED
@@ -238,8 +244,11 @@ def on_key_press(self, symbol, modifiers):
238
244
# Command Events
239
245
#################
240
246
def on_command (self , command ):
247
+ # clear autocompletion results
248
+ self .document .delete_text (self .caret .position , len (self .document .text ))
241
249
self .write ('\n ' )
242
- self .interpreter .dispatch_event ('on_command' , command )
250
+ # execute command
251
+ self .interpreter .on_command (command )
243
252
244
253
def on_command_done (self ):
245
254
self .write_prompt ()
@@ -250,9 +259,9 @@ def on_completion(self, command):
250
259
# clear the command line
251
260
self .document .delete_text (self .prompt_end , len (self .document .text ))
252
261
# request completion
253
- self .interpreter .dispatch_event ( ' on_completion' , command )
262
+ self .interpreter .on_completion ( command )
254
263
255
- def on_completed (self , completed , output ):
264
+ def on_completion_done (self , completed , output ):
256
265
# write out the result of completion
257
266
self .write (completed )
258
267
self .caret .position = len (self .document .text )
@@ -263,16 +272,10 @@ def on_completed(self, completed, output):
263
272
# so write out all possibilities
264
273
self .write (output )
265
274
275
+ def on_history_result (self , command ):
276
+ self .set_command (command )
277
+
266
278
def on_get_command (self ):
267
279
command = self .get_command ()
268
280
self .write (command + '\n ' )
269
281
270
- def on_set_command (self , command ):
271
- self .set_command (command )
272
-
273
- InterpreterLayer .register_event_type ('on_command' )
274
- InterpreterLayer .register_event_type ('on_command_done' )
275
- InterpreterLayer .register_event_type ('on_completion' )
276
- InterpreterLayer .register_event_type ('on_completed' )
277
- InterpreterLayer .register_event_type ('on_get_command' )
278
- InterpreterLayer .register_event_type ('on_set_command' )
0 commit comments