func makeEvent(tev tcell.Event) Event { switch tev := tev.(type) { case *tcell.EventInterrupt: return Event{Type: EventInterrupt} case *tcell.EventResize: w, h := tev.Size() return Event{Type: EventResize, Width: w, Height: h} case *tcell.EventKey: k := tev.Key() ch := rune(0) if k == tcell.KeyRune { ch = tev.Rune() if ch == ' ' { k = tcell.Key(' ') } } mod := tev.Modifiers() return Event{ Type: EventKey, Key: Key(k), Ch: ch, Mod: Modifier(mod), } default: return Event{Type: EventNone} } }
// HandleEvent handles events. In particular, it handles certain key events // to move the cursor or pan the view. func (a *CellView) HandleEvent(e tcell.Event) bool { if a.model == nil { return false } switch e := e.(type) { case *tcell.EventKey: switch e.Key() { case tcell.KeyUp, tcell.KeyCtrlP: a.keyUp() return true case tcell.KeyDown, tcell.KeyCtrlN: a.keyDown() return true case tcell.KeyRight, tcell.KeyCtrlF: a.keyRight() return true case tcell.KeyLeft, tcell.KeyCtrlB: a.keyLeft() return true case tcell.KeyPgDn: a.keyPgDn() return true case tcell.KeyPgUp: a.keyPgUp() return true case tcell.KeyEnd: a.keyEnd() return true case tcell.KeyHome: a.keyHome() return true } } return false }
func (a *mainWindow) HandleEvent(ev tcell.Event) bool { switch ev := ev.(type) { case *tcell.EventKey: switch ev.Key() { case tcell.KeyCtrlL: app.Refresh() return true case tcell.KeyRune: switch ev.Rune() { case 'Q', 'q': app.Quit() return true case 'S', 's': a.model.hide = false a.updateKeys() return true case 'H', 'h': a.model.hide = true a.updateKeys() return true case 'E', 'e': a.model.enab = true a.updateKeys() return true case 'D', 'd': a.model.enab = false a.updateKeys() return true } } } return a.Panel.HandleEvent(ev) }
func (m *boxL) HandleEvent(ev tcell.Event) bool { switch ev := ev.(type) { case *tcell.EventKey: if ev.Key() == tcell.KeyEscape { app.Quit() return true } } return m.BoxLayout.HandleEvent(ev) }
func (a *AuthPanel) HandleEvent(ev tcell.Event) bool { switch ev := ev.(type) { case *tcell.EventKey: switch ev.Key() { case tcell.KeyEsc: a.App().Quit() return true case tcell.KeyTab, tcell.KeyEnter: if a.passactive { a.App().SetUserPassword(string(a.username), string(a.password)) a.App().ShowMain() } else { a.passactive = true } case tcell.KeyBacktab: if a.passactive { a.passactive = false } case tcell.KeyCtrlU, tcell.KeyCtrlW: if a.passactive { a.password = a.password[:0] } else { a.username = a.username[:0] } case tcell.KeyBackspace, tcell.KeyBackspace2: if a.passactive { if len(a.password) > 0 { a.password = a.password[:len(a.password)-1] } } else { if len(a.username) > 0 { a.username = a.username[:len(a.username)-1] } } case tcell.KeyRune: r := ev.Rune() if a.passactive { if len(a.password) < 256 { a.password = append(a.password, r) } } else { if len(a.username) < 256 { a.username = append(a.username, r) } } default: return false } return true } return a.Panel.HandleEvent(ev) }
func (p *LogPanel) HandleEvent(ev tcell.Event) bool { info := p.info app := p.app switch ev := ev.(type) { case *tcell.EventKey: switch ev.Key() { case tcell.KeyEsc: app.ShowMain() return true case tcell.KeyF1: app.ShowHelp() return true case tcell.KeyRune: switch ev.Rune() { case 'Q', 'q': app.ShowMain() return true case 'H', 'h': app.ShowHelp() return true case 'I', 'i': if info != nil { app.ShowInfo(info.Name) return true } case 'R', 'r': if info != nil { app.RestartService(info.Name) return true } case 'E', 'e': if info != nil && !info.Enabled { app.EnableService(info.Name) return true } case 'D', 'd': if info != nil && info.Enabled { app.DisableService(info.Name) return true } case 'C', 'c': if info != nil && info.Failed { app.ClearService(info.Name) return true } } } } return p.Panel.HandleEvent(ev) }
func (i *InfoPanel) HandleEvent(ev tcell.Event) bool { info := i.info switch ev := ev.(type) { case *tcell.EventKey: switch ev.Key() { case tcell.KeyEsc: i.App().ShowMain() return true case tcell.KeyF1: i.App().ShowHelp() return true case tcell.KeyRune: switch ev.Rune() { case 'Q', 'q': i.App().ShowMain() return true case 'H', 'h': i.App().ShowHelp() return true case 'L', 'l': if info != nil { i.App().ShowLog(info.Name) return true } case 'R', 'r': if info != nil { i.App().RestartService(info.Name) return true } case 'E', 'e': if info != nil && !info.Enabled { i.App().EnableService(info.Name) return true } case 'D', 'd': if info != nil && info.Enabled { i.App().DisableService(info.Name) return true } case 'C', 'c': if info != nil && info.Failed { i.App().ClearService(info.Name) return true } } } } return i.Panel.HandleEvent(ev) }
func (h *HelpPanel) HandleEvent(ev tcell.Event) bool { switch ev := ev.(type) { case *tcell.EventKey: switch ev.Key() { case tcell.KeyEsc: h.App().ShowMain() return true case tcell.KeyRune: switch ev.Rune() { case 'Q', 'q': h.app.ShowMain() return true } } } return h.Panel.HandleEvent(ev) }
func (g *Game) HandleEvent(ev tcell.Event) bool { switch ev := ev.(type) { case *tcell.EventResize: g.lview.Resize(0, 1, -1, -1) g.sview.Resize(0, 0, -1, 1) g.level.HandleEvent(ev) case *tcell.EventKey: if ev.Key() == tcell.KeyEscape { g.Quit() return true } if !g.started { if ev.Key() == tcell.KeyEnter { if g.gameover { g.lives = 5 } g.level.Reset() g.level.Start() g.started = true g.gameover = false } // eat all keys until level starts return true } case *EventPlayerDeath: g.lives-- g.started = false if g.lives == 0 { g.gameover = true g.level.HandleEvent(&EventGameOver{}) } return true case *EventLevelComplete: g.lives++ // bonus life (for now) g.started = false return true } if !g.level.HandleEvent(ev) { return true } return true }
func (o *ship) HandleEvent(ev tcell.Event) bool { if o.dead { return false } switch ev := ev.(type) { case *EventSpriteAccelerate: if ev.s != o.ship { return false } vx, _ := o.ship.Velocity() if vx >= 1.0 { o.ship.SetFrame("RIGHT") } else if vx <= -1.0 { o.ship.SetFrame("LEFT") } else { o.ship.SetFrame("FWD") } case *EventSpriteMove: // We don't let ship leave the map x, y := o.ship.Position() ox, oy := x, y vx, vy := o.ship.Velocity() w, h := o.level.Size() if x < 0 { x = 0 if vx < 0 { vx = 0 } } else if x >= w { x = w - 1 if vx > 0 { vx = 0 } } if y < 0 { y = 0 if vy < 0 { vy = 0 } } else if y >= h { y = h - 1 if vy > 0 { vy = 0 } } if ox != x || oy != y { o.ship.SetPosition(x, y) o.ship.SetVelocity(vx, vy) } if y == 0 { o.dead = true o.level.HandleEvent(&EventLevelComplete{}) } o.adjustView() case *EventGravity: now := ev.When() if !o.lastgrav.IsZero() { vx, vy := o.ship.Velocity() frac := float64(now.Sub(o.lastgrav)) frac /= float64(time.Second) vy += ev.Accel() * frac o.ship.SetVelocity(vx, vy) } o.lastgrav = now case *EventCollision: switch ev.Collider().Layer() { case LayerTerrain, LayerHazard, LayerShot: o.destroy() case LayerPad: // if we're on the pad, and not too // fast, then stay on the pad. // TODO: probably the max velocity (4.0) // should be tunable. vx, vy := o.ship.Velocity() x, y := o.ship.Position() if vx == 0 && vy > 0 && vy < 4.0 { y-- vy = 0 o.ship.SetPosition(x, y) o.ship.SetVelocity(vx, vy) o.launched = false } else { o.destroy() } } case *EventTimesUp: o.destroy() case *tcell.EventKey: switch ev.Key() { case tcell.KeyLeft: o.thrustLeft() return true case tcell.KeyRight: o.thrustRight() return true case tcell.KeyUp: o.thrustUp() return true case tcell.KeyDown: o.thrustDown() return true case tcell.KeyRune: switch ev.Rune() { case ' ': o.shoot() return true case 'j', 'J': o.thrustLeft() return true case 'k', 'K': o.thrustRight() return true case 'i', 'I': o.thrustUp() return true case 'm', 'M': o.thrustDown() return true } } case *tcell.EventResize: x, y := o.ship.Position() o.level.Center(x, y) o.adjustView() } return false }
func (l *Level) HandleEvent(ev tcell.Event) bool { switch ev := ev.(type) { case *tcell.EventKey: switch ev.Key() { case tcell.KeyCtrlR: // secret reset button l.Reset() } case *tcell.EventMouse: offx, offy, _, _ := l.view.GetVisible() px1, py1, px2, py2 := l.view.GetPhysical() mx, my := ev.Position() if mx < px1 || mx > px2 || my < py1 || my > py2 { // outside our view return false } if ev.Buttons()&tcell.Button1 != 0 { l.stopped = true l.view.Center(offx+mx-px1, offy+my-py1) } else if ev.Buttons()&tcell.Button2 != 0 { l.Reset() } else if ev.Buttons()&tcell.Button3 != 0 { l.Reset() } return true case *EventPlayerDeath: l.game.HandleEvent(ev) l.ShowPress() return true case *EventLevelComplete: l.win = true l.game.HandleEvent(ev) l.ShowComplete() l.ShowPress() return true case *EventGameOver: x1, y1, x2, y2 := l.view.GetVisible() sprite := GetSprite("GameOver") sprite.SetLayer(LayerDialog) sprite.SetFrame("F0") sprite.SetPosition(x1+(x2-x1)/2, y1+(y2-y1)/2) l.manager.AddSprite(sprite) case *EventTimesUp: bw := GetSprite("Blastwave") bw.Resize(l.width, l.height) bw.ScheduleFrame("0", time.Now().Add(2*time.Second)) l.AddSprite(bw) dur := time.Duration(0) for i := 0; i < 100; i++ { x, y := l.randomViewXY() sprite := GetSprite("Explosion") sprite.ScheduleFrame("0", time.Now().Add(dur)) sprite.SetPosition(x, y) l.AddSprite(sprite) dur += time.Millisecond * 5 for j := 0; j < 4; j++ { x, y = l.randomViewXY() sprite = GetSprite("SmallExplosion") sprite.SetPosition(x, y) sprite.ScheduleFrame("0", time.Now().Add(dur)) l.AddSprite(sprite) dur += time.Millisecond * 50 } } } if l.stopped { return false } return l.manager.HandleEvent(ev) }
func (m *MainPanel) HandleEvent(ev tcell.Event) bool { switch ev := ev.(type) { case *tcell.EventKey: switch ev.Key() { case tcell.KeyEsc: m.unselect() return true case tcell.KeyF1: m.App().ShowHelp() return true case tcell.KeyEnter: if m.selected != nil { m.App().ShowInfo(m.selected.Name) return true } case tcell.KeyRune: switch ev.Rune() { case 'Q', 'q': m.App().Quit() return true case 'H', 'h': m.App().ShowHelp() return true case 'I', 'i': if m.selected != nil { m.App().ShowInfo(m.selected.Name) return true } case 'L', 'l': if m.selected != nil { m.App().ShowLog(m.selected.Name) return true } else { m.App().ShowLog("") return true } case 'E', 'e': if m.selected != nil && !m.selected.Enabled { m.App().EnableService(m.selected.Name) return true } case 'D', 'd': if m.selected != nil && m.selected.Enabled { m.App().DisableService(m.selected.Name) return true } case 'C', 'c': if m.selected != nil && m.selected.Failed { m.App().ClearService(m.selected.Name) return true } case 'R', 'r': if m.selected != nil { m.App().RestartService(m.selected.Name) return true } } } } return m.Panel.HandleEvent(ev) }