-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpen.js
77 lines (67 loc) · 1.33 KB
/
pen.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {AbstractTool} from './abstractTool.js'
import {Text} from './text.js'
class PenLine extends Text {
constructor(row, col) {
super(row, col)
this.char="*"
this.put(row, col)
}
put(row, col) {
super.put(this.char, row, col)
}
}
export class PenTool extends AbstractTool {
static produces = PenLine
cancel() {
this.penline = null
this.finish();
}
finish() {
if( this.penline) {
this.paper.addObject(this.penline)
}
this.lenline = null
this.drawing = false
this.paper.buffer.clear()
this.paper.redraw()
}
keydown(e){
if( !this.drawing ) {
switch( e.key ) {
case ' ':
case 'Enter':
this.cursorDown(e);
break;
default:
super.keydown(e)
}
} else {
switch( e.key ) {
case 'Enter':
case ' ':
case 'Escape':
this.cursorUp()
break;
default:
super.keydown(e)
break;
}
}
}
cursorDown(e) {
this.penline = new PenLine(e.row, e.col)
this.drawing = true;
}
cursorUp() {
this.finish()
}
cursorMove(e) {
if(this.drawing) {
this.penline.put(e.row, e.col)
this.paper.redraw()
this.paper.buffer.clear()
this.penline.draw(this.paper.turtle)
}
super.cursorMove(e)
}
}