ref: ca6d12ef8cf3573d11bc2c45cedd4f147af90648 netris/cmd/netris/gui_input.go -rw-r--r-- 11.4 KiB View raw
                                                                                
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
package main

import (
	"fmt"
	"log"
	"os"
	"runtime/pprof"
	"strings"

	"git.sr.ht/~tslocum/netris/pkg/event"
	"git.sr.ht/~tslocum/netris/pkg/game"
	"github.com/gdamore/tcell"
	"github.com/tslocum/tview"
)

type Keybinding struct {
	k tcell.Key
	r rune
	m tcell.ModMask

	a event.GameAction
}

var keybindings = []*Keybinding{
	{r: 'z', a: event.ActionRotateCCW},
	{r: 'Z', a: event.ActionRotateCCW},
	{r: 'x', a: event.ActionRotateCW},
	{r: 'X', a: event.ActionRotateCW},
	{k: tcell.KeyLeft, a: event.ActionMoveLeft},
	{r: 'h', a: event.ActionMoveLeft},
	{r: 'H', a: event.ActionMoveLeft},
	{k: tcell.KeyDown, a: event.ActionSoftDrop},
	{r: 'j', a: event.ActionSoftDrop},
	{r: 'J', a: event.ActionSoftDrop},
	{k: tcell.KeyUp, a: event.ActionHardDrop},
	{r: 'k', a: event.ActionHardDrop},
	{r: 'K', a: event.ActionHardDrop},
	{k: tcell.KeyRight, a: event.ActionMoveRight},
	{r: 'l', a: event.ActionMoveRight},
	{r: 'L', a: event.ActionMoveRight},
}

var draftKeybindings []*Keybinding

func scrollMessages(direction int) {
	var scroll int
	if showLogLines > 3 {
		scroll = (showLogLines - 2) * direction
	} else {
		scroll = showLogLines * direction
	}

	r, _ := recent.GetScrollOffset()
	r += scroll
	if r < 0 {
		r = 0
	}
	recent.ScrollTo(r, 0)

	draw <- event.DrawAll
}

// Render functions called here don't need to be queued (Draw is called when nil is returned)
func handleKeypress(ev *tcell.EventKey) *tcell.EventKey {
	k := ev.Key()
	r := ev.Rune()

	if capturingKeybind {
		capturingKeybind = false
		if k == tcell.KeyEscape {
			draftKeybindings = nil

			app.SetRoot(gameSettingsContainerGrid, true)
			updateGameSettings()

			return nil
		}

		for i, bind := range draftKeybindings {
			if (bind.k != 0 && bind.k != k) || (bind.r != 0 && bind.r != r) || (bind.m != 0 && bind.m != ev.Modifiers()) {
				continue
			}

			draftKeybindings = append(draftKeybindings[:i], draftKeybindings[i+1:]...)
			break
		}

		var action event.GameAction
		switch gameSettingsSelectedButton {
		case 1:
			action = event.ActionRotateCCW
		case 2:
			action = event.ActionRotateCW
		case 3:
			action = event.ActionMoveLeft
		case 4:
			action = event.ActionMoveRight
		case 5:
			action = event.ActionSoftDrop
		case 6:
			action = event.ActionHardDrop
		default:
			log.Fatal("setting keybind for unknown action")
		}

		draftKeybindings = append(draftKeybindings, &Keybinding{k: k, r: r, m: ev.Modifiers(), a: action})

		app.SetRoot(gameSettingsContainerGrid, true)
		updateGameSettings()
		return nil
	} else if titleVisible {
		if titleScreen > 1 {
			switch k {
			case tcell.KeyEscape:
				if titleScreen == 5 {
					titleScreen = 4
					gameListSelected = 0
					titleSelectedButton = 0
					app.SetRoot(gameListContainerGrid, true)
					renderGameList()
					updateTitle()
					return nil
				} else if titleScreen == 4 {
					titleScreen = 0
				} else {
					titleScreen = 1
				}
				titleSelectedButton = 0

				app.SetRoot(titleContainerGrid, true)
				updateTitle()
				return nil
			}

			if titleScreen == 3 {
				switch k {
				case tcell.KeyTab:
					gameSettingsSelectedButton++
					if gameSettingsSelectedButton > 8 {
						gameSettingsSelectedButton = 8
					}

					updateGameSettings()
					return nil
				case tcell.KeyBacktab:
					gameSettingsSelectedButton--
					if gameSettingsSelectedButton < 0 {
						gameSettingsSelectedButton = 0
					}

					updateGameSettings()
					return nil
				case tcell.KeyEnter:
					if gameSettingsSelectedButton == 0 {
						// TODO Cache until saved
						drawGhostPieceUnsaved = !drawGhostPieceUnsaved
						updateGameSettings()
						return nil
					} else if gameSettingsSelectedButton == 7 || gameSettingsSelectedButton == 8 {
						if gameSettingsSelectedButton == 8 {
							drawGhostPiece = drawGhostPieceUnsaved

							keybindings = make([]*Keybinding, len(draftKeybindings))
							copy(keybindings, draftKeybindings)
						}
						draftKeybindings = nil

						titleScreen = 1
						titleSelectedButton = 0

						app.SetRoot(titleContainerGrid, true)
						updateTitle()
						return nil
					}

					modal := tview.NewModal().SetText("Press desired key(s) to set keybinding or press Escape to cancel.").ClearButtons()
					app.SetRoot(modal, true)

					capturingKeybind = true
					return nil
				}
			} else if titleScreen == 4 {
				switch k {
				case tcell.KeyUp:
					if titleSelectedButton == 0 {
						if gameListSelected > 0 {
							gameListSelected--
						}
						renderGameList()
					}
					return nil
				case tcell.KeyBacktab:
					previousTitleButton()
					updateTitle()
					renderGameList()
					return nil
				case tcell.KeyDown:
					if titleSelectedButton == 0 {
						if gameListSelected < len(gameList)-1 {
							gameListSelected++
						}
						renderGameList()
					}
					return nil
				case tcell.KeyTab:
					nextTitleButton()
					updateTitle()
					renderGameList()
					return nil
				case tcell.KeyEnter:
					if titleSelectedButton == 0 {
						if gameListSelected >= 0 && gameListSelected < len(gameList) {
							joinGame <- gameList[gameListSelected].ID
						}
					} else if titleSelectedButton == 1 {
						titleScreen = 5
						titleSelectedButton = 0

						resetNewGameInputs()
						app.SetRoot(newGameContainerGrid, true).SetFocus(nil)
						updateTitle()
					} else if titleSelectedButton == 2 {
						titleScreen = 5
						titleSelectedButton = 0

						modal := tview.NewModal().SetText("Joining another server by IP via GUI is not yet implemented.\nPlease re-launch netris with the --connect argument instead.\n\nPress Escape to return.").ClearButtons()
						app.SetRoot(modal, true)
					} else if titleSelectedButton == 3 {
						titleScreen = 0
						titleSelectedButton = 0

						app.SetRoot(titleContainerGrid, true)
						updateTitle()
					}

					return nil
				default:
					if titleSelectedButton == 0 {
						switch r {
						case 'j', 'J':
							if gameListSelected < len(gameList)-1 {
								gameListSelected++
							}
							renderGameList()
							return nil
						case 'k', 'K':
							if gameListSelected > 0 {
								gameListSelected--
							}
							renderGameList()
							return nil
						case 'r', 'R':
							refreshGameList()
							return nil
						}
					}
				}
			} else if titleScreen == 5 {
				switch k {
				case tcell.KeyBacktab:
					previousTitleButton()
					updateTitle()
					return nil
				case tcell.KeyTab:
					nextTitleButton()
					updateTitle()
					return nil
				case tcell.KeyEnter:
					if titleSelectedButton == 3 {
						titleScreen = 4
						gameListSelected = 0
						titleSelectedButton = 0
						app.SetRoot(gameListContainerGrid, true)
						renderGameList()
						updateTitle()
					} else if titleSelectedButton == 4 {
						joinGame <- event.GameIDNewCustom
					}

					return nil
				}
			}

			return ev
		}

		switch k {
		case tcell.KeyEnter:
			if titleScreen == 1 {
				switch titleSelectedButton {
				case 0:
					resetPlayerSettingsForm()

					titleScreen = 2
					titleSelectedButton = 0

					app.SetRoot(playerSettingsContainerGrid, true).SetFocus(playerSettingsForm)
					return nil
				case 1:
					titleScreen = 3
					titleSelectedButton = 0
					gameSettingsSelectedButton = 0

					drawGhostPieceUnsaved = drawGhostPiece

					draftKeybindings = make([]*Keybinding, len(keybindings))
					copy(draftKeybindings, keybindings)

					app.SetRoot(gameSettingsContainerGrid, true)
					updateGameSettings()
					return nil
				case 2:
					titleScreen = 0
					titleSelectedButton = 0

					updateTitle()
					return nil
				}
			} else {
				if joinedGame {
					switch titleSelectedButton {
					case 0:
						setTitleVisible(false)
						return nil
					case 1:
						titleScreen = 1
						titleSelectedButton = 0

						updateTitle()
						return nil
					case 2:
						done <- true
						return nil
					}
				} else {
					switch titleSelectedButton {
					case 0:
						titleScreen = 4
						titleSelectedButton = 0
						gameListSelected = 0

						refreshGameList()
						renderGameList()

						app.SetRoot(gameListContainerGrid, true).SetFocus(nil)
						updateTitle()
						return nil
					case 1:
						joinGame <- event.GameIDNewLocal
						return nil
					case 2:
						titleScreen = 1
						titleSelectedButton = 0

						updateTitle()
						return nil
					}
				}
			}
			return nil
		case tcell.KeyUp, tcell.KeyBacktab:
			previousTitleButton()
			updateTitle()
			return nil
		case tcell.KeyDown, tcell.KeyTab:
			nextTitleButton()
			updateTitle()
			return nil
		case tcell.KeyEscape:
			if titleScreen == 1 {
				titleScreen = 0
				titleSelectedButton = 0
				updateTitle()
			} else if joinedGame {
				setTitleVisible(false)
			} else {
				done <- true
			}
			return nil
		default:
			switch r {
			case 'k', 'K':
				previousTitleButton()
				updateTitle()
				return nil
			case 'j', 'J':
				nextTitleButton()
				updateTitle()
				return nil
			}
		}

		return ev
	}

	if inputActive {
		switch k {
		case tcell.KeyEnter:
			defer setInputStatus(false)

			msg := inputView.GetText()
			if strings.TrimSpace(msg) == "" {
				return nil
			}

			msgl := strings.ToLower(msg)
			switch {
			case strings.HasPrefix(msgl, "/nick"):
				if activeGame != nil && len(msg) > 6 {
					var oldnick string
					activeGame.Lock()
					if p, ok := activeGame.Players[activeGame.LocalPlayer]; ok {
						oldnick = p.Name
						p.Name = game.Nickname(msg[6:])
					} else {
						return nil
					}
					activeGame.ProcessActionL(event.ActionNick)
					if p, ok := activeGame.Players[activeGame.LocalPlayer]; ok {
						p.Name = oldnick
					}
					activeGame.Unlock()
				}
			case strings.HasPrefix(msgl, "/ping"):
				if activeGame != nil {
					activeGame.ProcessAction(event.ActionPing)
				}
			case strings.HasPrefix(msgl, "/stats"):
				if activeGame != nil {
					activeGame.ProcessAction(event.ActionStats)
				}
			case strings.HasPrefix(msgl, "/version"):
				v := game.Version
				if v == "" {
					v = "unknown"
				}

				logMessage(fmt.Sprintf("netris version %s", v))
			case strings.HasPrefix(msgl, "/cpu"):
				if profileCPU == nil {
					if len(msg) < 5 {
						logMessage("Profile name must be specified")
					} else {
						profileName := strings.TrimSpace(msg[5:])

						var err error
						profileCPU, err = os.Create(profileName)
						if err != nil {
							log.Fatal(err)
						}

						err = pprof.StartCPUProfile(profileCPU)
						if err != nil {
							log.Fatal(err)
						}

						logMessage(fmt.Sprintf("Started profiling CPU usage as %s", profileName))
					}
				} else {
					pprof.StopCPUProfile()
					profileCPU.Close()
					profileCPU = nil

					logMessage("Stopped profiling CPU usage")
				}
			default:
				if activeGame != nil {
					activeGame.Event <- &event.MessageEvent{Message: msg}
				} else {
					logMessage("Message not sent - not currently connected to any game")
				}
			}

			return nil
		case tcell.KeyPgUp:
			scrollMessages(-1)
			return nil
		case tcell.KeyPgDn:
			scrollMessages(1)
			return nil
		case tcell.KeyEscape:
			setInputStatus(false)
			return nil
		}

		return ev
	}

	switch k {
	case tcell.KeyEnter:
		setInputStatus(!inputActive)
		return nil
	case tcell.KeyTab:
		setShowDetails(!showDetails)
		return nil
	case tcell.KeyPgUp:
		scrollMessages(-1)
		return nil
	case tcell.KeyPgDn:
		scrollMessages(1)
		return nil
	case tcell.KeyEscape:
		setTitleVisible(true)
		return nil
	}

	for _, bind := range keybindings {
		if (bind.k != 0 && bind.k != k) || (bind.r != 0 && bind.r != r) || (bind.m != 0 && bind.m != ev.Modifiers()) {
			continue
		} else if activeGame == nil {
			break
		}

		activeGame.ProcessAction(bind.a)
		return nil
	}

	return ev
}