-
Notifications
You must be signed in to change notification settings - Fork 12
/
user.go
183 lines (156 loc) · 4.67 KB
/
user.go
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package vimman
import (
"github.com/nsf/termbox-go"
"reflect"
"time"
)
type Class int
type User struct {
*Entity
}
func (u *User) ShouldCenterHorizontally() bool {
return false
}
func NewUser(s *Stage, x, y int) (u *User) {
cells := []*TermBoxCell{
{&termbox.Cell{'▒', termbox.ColorGreen, bgColor}, false, TileMapCellData{}},
}
tags := []Tag{{"Cursor"}}
entityOptions := EntityOptions{9999, tags, nil}
e := NewEntity(s, x, y, 1, 1, ' ', termbox.ColorBlue, termbox.ColorWhite, cells, false, entityOptions)
u = &User{
Entity: e,
}
return
}
func (u *User) handleNormalModeEvents(s *Stage, event termbox.Event) {
switch event.Ch {
case 'k':
nextY := u.GetPositionY() - 1
if !s.CheckCollision(u.GetPositionX(), nextY) {
u.SetPositionY(nextY)
}
case 'j':
nextY := u.GetPositionY() + 1
if !s.CheckCollision(u.GetPositionX(), nextY) {
u.SetPositionY(nextY)
}
case 'l':
nextX := u.GetPositionX() + 1
if !s.CheckCollision(nextX, u.GetPositionY()) {
u.SetPositionX(nextX)
}
case 'h':
nextX := u.GetPositionX() - 1
if !s.CheckCollision(nextX, u.GetPositionY()) {
u.SetPositionX(nextX)
}
case 'i':
if s.LevelInstance.VimMode != insertMode && !s.LevelInstance.InputBlocked {
s.LevelInstance.VimMode = insertMode
}
case 'x':
if ContainsTermboxKey(s.LevelInstance.BlockedKeys, termbox.KeyDelete) {
return
}
if s.LevelInstance.InputBlocked {
return
}
x := u.GetPositionX()
y := u.GetPositionY()
// keep the last element in place, insert an empty cell before the last character in the line
tileMap := s.LevelInstance.TileMap
lastElement := s.LevelInstance.TileMap[y][len(s.LevelInstance.TileMap[y])-1]
tileMap[y] = append(tileMap[y][:x], tileMap[y][x+1:len(tileMap[y])-1]...)
tileMap[y] = append(tileMap[y], EmptyTileMapCell(), lastElement)
case ':':
if s.LevelInstance.VimMode == normalMode {
s.LevelInstance.VimMode = colonMode
s.ColonLine.Content = ":"
}
}
}
func (u *User) handleInsertModeEvents(s *Stage, event termbox.Event) {
// return on empty event
if reflect.DeepEqual(event, termbox.Event{}) {
return
}
switch event.Key {
// switch to normal mode on esc key event
case termbox.KeyEsc:
s.LevelInstance.VimMode = normalMode
return
case termbox.KeyBackspace, termbox.KeyBackspace2:
if ContainsTermboxKey(s.LevelInstance.BlockedKeys, termbox.KeyBackspace) ||
ContainsTermboxKey(s.LevelInstance.BlockedKeys, termbox.KeyBackspace2) {
return
}
characterOptions := WordOptions{InitCallback: nil, Fg: typedCharacterFg, Bg: typedCharacterBg}
character := NewEmptyCharacter(s, u.GetPositionX()-1, u.GetPositionY(), characterOptions)
if !character.IsInsideOfCanvasBoundaries() {
return
}
s.AddTypedEntity(character)
u.SetPositionX(u.GetPositionX() - 1)
default:
// don't allow non-character events
if event.Ch == 0 {
return
}
// check if rune input is allowed
if len(s.LevelInstance.InputRunes) > 0 {
if !ContainsRune(s.LevelInstance.InputRunes, event.Ch) {
return
}
}
characterOptions := WordOptions{InitCallback: nil, Fg: typedCharacterFg, Bg: typedCharacterBg}
character := NewWord(s, u.GetPositionX(), u.GetPositionY(), string(event.Ch), characterOptions)
if !character.IsInsideOfCanvasBoundaries() {
return
}
if !s.LevelInstance.TextShiftingDisabled {
x := u.GetPositionX()
y := u.GetPositionY()
tileMap := s.LevelInstance.TileMap[y]
lastElement := tileMap[len(tileMap)-1]
tileMap = append(tileMap[:x], append([]*TermBoxCell{character.Cell}, tileMap[x:len(tileMap)-2]...)...)
tileMap = append(tileMap, lastElement)
}
// type a character and add as typed entity
s.AddTypedEntity(character)
u.SetPositionX(u.GetPositionX() + 1)
// at the end of the current line, move the cursor next line
if s.Canvas.IsInLastColumn(u.GetPositionX()) {
u.SetPosition(1, u.GetPositionY()+1)
}
if character.InitCallback != nil {
character.InitCallback()
}
if s.LevelInstance.TileData[event.Ch].InitCallback != nil {
s.LevelInstance.TileData[event.Ch].InitCallback(character.Entity)
}
}
}
func (u *User) handleColonModeEvents(s *Stage, event termbox.Event) {
if event.Key == termbox.KeyEnter {
s.LevelInstance.VimMode = normalMode
if len(s.LevelInstance.ColonLineCallbacks) > 0 {
if fn, ok := s.LevelInstance.ColonLineCallbacks[s.ColonLine.Content[1:]]; ok {
fn(s.Game)
}
}
}
if event.Ch != 0 {
s.ColonLine.Content = s.ColonLine.Content + string(event.Ch)
}
}
func (u *User) Update(s *Stage, event termbox.Event, delta time.Duration) {
switch s.LevelInstance.VimMode {
case colonMode:
u.handleColonModeEvents(s, event)
case normalMode:
u.handleNormalModeEvents(s, event)
case insertMode:
u.handleInsertModeEvents(s, event)
}
}