Beispiel #1
0
func onFocus(w *glfw.Window, focused bool) {
	if !focused {
		for i := range Client.KeyState {
			Client.KeyState[i] = false
		}
	} else if lockMouse {
		w.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
	}
}
Beispiel #2
0
func NewEventHandler(w *glfw.Window) (e *EventHandler) {
	e = &EventHandler{
		Events: make(chan Event, 100),
		window: w,
	}
	// See http://www.glfw.org/docs/latest/input.html#input_keyboard
	w.SetInputMode(glfw.StickyKeysMode, 1)
	w.SetCursorPosCallback(e.onMouseMove)
	w.SetKeyCallback(e.onKey)
	w.SetMouseButtonCallback(e.onMouseButton)
	return
}
Beispiel #3
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]
		}
	}
}
Beispiel #4
0
func onMouseClick(w *glfw.Window, button glfw.MouseButton, action glfw.Action, mod glfw.ModifierKey) {
	if currentScreen != nil {
		if button != glfw.MouseButtonLeft || action == glfw.Repeat {
			return
		}
		width, height := w.GetSize()
		xpos, ypos := w.GetCursorPos()
		fw, fh := w.GetFramebufferSize()
		currentScreen.click(action == glfw.Press, xpos*(float64(fw)/float64(width)), ypos*(float64(fh)/float64(height)), fw, fh)
		return
	}
	if !Client.chat.enteringText && lockMouse && action != glfw.Repeat {
		Client.MouseAction(button, action == glfw.Press)
	}
	if button == glfw.MouseButtonLeft && action == glfw.Press && !Client.chat.enteringText {
		lockMouse = true
		w.SetInputMode(glfw.CursorMode, glfw.CursorDisabled)
	}
}
Beispiel #5
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)
	}
}
Beispiel #6
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)
	}
}