Пример #1
0
func (c *ChatUI) handleKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
	if (key == glfw.KeyEscape || key == glfw.KeyEnter) && action == glfw.Release {
		if key == glfw.KeyEnter && len(c.inputLine) != 0 {
			Client.network.Write(&protocol.ChatMessage{string(c.inputLine)})
		}
		// Return control back to the default
		c.enteringText = false
		c.inputLine = c.inputLine[:0]
		lockMouse = true
		w.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
		w.SetCharCallback(nil)
		return
	}
	if key == glfw.KeyBackspace && action != glfw.Release {
		if len(c.inputLine) > 0 {
			c.inputLine = c.inputLine[:len(c.inputLine)-1]
		}
	}
}
Пример #2
0
func onKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
	// Debug override
	if key == glfw.KeyGraveAccent && action == glfw.Release {
		con.focus()
		return
	}

	if currentScreen != nil {
		ui.HandleKey(w, key, scancode, action, mods)
		return
	}
	if Client.chat.enteringText {
		Client.chat.handleKey(w, key, scancode, action, mods)
		return
	}

	if k, ok := keyStateMap[key]; action != glfw.Repeat && ok {
		Client.KeyState[k] = action == glfw.Press
	}
	switch key {
	case glfw.KeyEscape:
		if action == glfw.Release {
			setScreen(newGameMenu())
		}
	case glfw.KeyF1:
		if action == glfw.Release {
			if Client.scene.IsVisible() {
				Client.scene.Hide()
				Client.hotbarScene.Hide()
			} else {
				Client.scene.Show()
				Client.hotbarScene.Show()
			}
		}
	case glfw.KeyF3:
		if action == glfw.Release {
			Client.toggleDebug()
		}
	case glfw.KeyF5:
		if action == glfw.Release {
			Client.cycleCamera()
		}
	case glfw.KeyTab:
		if action == glfw.Press {
			Client.playerList.set(true)
		} else if action == glfw.Release {
			Client.playerList.set(false)
		}
	case glfw.KeyE:
		if action == glfw.Release {
			wasPlayer := Client.activeInventory == Client.playerInventory
			closeInventory()
			if wasPlayer {
				return
			}
			openInventory(Client.playerInventory)
		}
	case glfw.KeyT:
		state := w.GetKey(glfw.KeyF3)
		if action == glfw.Release && state == glfw.Press {
			reloadResources()
			return
		}
		fallthrough
	case glfw.KeySlash:
		if action != glfw.Release {
			return
		}
		for i := range Client.KeyState {
			Client.KeyState[i] = false
		}
		Client.chat.enteringText = true
		if key == glfw.KeySlash {
			Client.chat.inputLine = append(Client.chat.inputLine, '/')
		}
		lockMouse = false
		w.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
		w.SetCharCallback(Client.chat.handleChar)
	}
}
Пример #3
0
func onKey(w *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
	// Debug override
	if key == glfw.KeyGraveAccent && action == glfw.Release {
		con.focus()
		return
	}

	if currentScreen != nil {
		ui.HandleKey(w, key, scancode, action, mods)
		return
	}
	if Client.chat.enteringText {
		Client.chat.handleKey(w, key, scancode, action, mods)
		return
	}

	// For creative flying
	if Client.GameMode.Fly() && keyStateMap[key] == KeyJump && action == glfw.Press {
		now := time.Now()
		if now.Sub(lastJumpPress) < 500*time.Millisecond {
			Client.isFlying = !Client.isFlying
		}
		lastJumpPress = now
	}

	if k, ok := keyStateMap[key]; action != glfw.Repeat && ok {
		Client.KeyState[k] = action == glfw.Press
	}
	if key >= glfw.Key0 && key <= glfw.Key9 && action == glfw.Press {
		slot := int((8 + (key - glfw.Key0)) % 9)
		Client.currentHotbarSlot = slot
		Client.network.Write(&protocol.HeldItemChange{Slot: int16(Client.currentHotbarSlot)})
	}
	switch key {
	case glfw.KeyEscape:
		if action == glfw.Release {
			setScreen(newGameMenu())
		}
	case glfw.KeyF1:
		if action == glfw.Release {
			if Client.scene.IsVisible() {
				Client.scene.Hide()
				Client.hotbarScene.Hide()
			} else {
				Client.scene.Show()
				Client.hotbarScene.Show()
			}
		}
	case glfw.KeyF3:
		if action == glfw.Release {
			Client.toggleDebug()
		}
	case glfw.KeyF5:
		if action == glfw.Release {
			Client.cycleCamera()
		}
	case glfw.KeyTab:
		if action == glfw.Press {
			Client.playerList.set(true)
		} else if action == glfw.Release {
			Client.playerList.set(false)
		}
	case glfw.KeyE:
		if action == glfw.Release {
			wasPlayer := Client.activeInventory == Client.playerInventory
			closeInventory()
			if wasPlayer {
				return
			}
			openInventory(Client.playerInventory)
		}
	case glfw.KeyT:
		state := w.GetKey(glfw.KeyF3)
		if action == glfw.Release && state == glfw.Press {
			reloadResources()
			return
		}
		fallthrough
	case glfw.KeySlash:
		if action != glfw.Release {
			return
		}
		for i := range Client.KeyState {
			Client.KeyState[i] = false
		}
		Client.chat.enteringText = true
		if key == glfw.KeySlash {
			Client.chat.inputLine = append(Client.chat.inputLine, '/')
		}
		lockMouse = false
		w.SetInputMode(glfw.CursorMode, glfw.CursorNormal)
		w.SetCharCallback(Client.chat.handleChar)
	}
}