Esempio n. 1
0
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}
	}
}
Esempio n. 2
0
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)
}
Esempio n. 3
0
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)
}
Esempio n. 4
0
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)
}
Esempio n. 5
0
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)
}
Esempio n. 6
0
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)
}
Esempio n. 7
0
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
}
Esempio n. 8
0
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)
}