public MouseState(GameWindow window) { GlfwWindowPtr ptr = window.GetPointer(); Buttons = new Dictionary <MouseButton, bool>(AllButtons.Length); for (int i = 0; i < AllButtons.Length; i++) { MouseButton b = AllButtons[i]; Buttons[b] = Glfw.GetMouseButton(ptr, b); } double _x, _y; Glfw.GetCursorPos(ptr, out _x, out _y); X = (float)_x; Y = (float)_y; ClampedX = MathHelper.Clamp(X, 0, window.Width); ClampedY = MathHelper.Clamp(Y, 0, window.Height); // MouseState must rely on Input for scrolling // since the scroll wheel values can only be // accessed in a callback. ScrollX = Input.ScrollX; ScrollY = Input.ScrollY; }
protected override bool UpdatePlatform() { IsOpen = !Glfw.WindowShouldClose(_win); Glfw.PollEvents(); Glfw.GetCursorPos(_win, out double xPos, out double yPos); MousePosition = new Vector2((float)xPos, (float)yPos); return(true); }
public static Vector2 GetMousePosition() { //Glfw.GetCursorPos(Program.MainWindow.GlfwWindow, out double x, out double y); double x, y; Glfw.GetCursorPos(Engine.MainWindow.GlfwWindow, out x, out y); return(new Vector2(2 * (float)x / Engine.MainWindow.Width - 1, 2 * (Engine.MainWindow.Height - (float)y) / Engine.MainWindow.Height - 1)); }
private void calculateAngleAroundPlayer() { if (Glfw.GetMouseButton(window, Glfw.MouseButton.Button2)) { double xPos; double yPos; Glfw.GetCursorPos(window, out xPos, out yPos); float angleChange = (float)xPos * 0.004f; angleAroundPlayer -= angleChange; } }
private void calculatePitch() { if (Glfw.GetMouseButton(window, Glfw.MouseButton.Button1)) { double xPos; double yPos; Glfw.GetCursorPos(window, out xPos, out yPos); Console.WriteLine(xPos + " " + yPos); float pitchChange = (float)yPos * 0.01f; pitch -= pitchChange; } }
public void GetCursorDelta(ref double x, ref double y) { double xPos = 0; double yPos = 0; Glfw.GetCursorPos(window, ref xPos, ref yPos); x = xPos - cursorLastX; y = cursorLastY - yPos; cursorLastX = xPos; cursorLastY = yPos; }
// update the scene based on the time elapsed since last update private static void Update(double secondsElapsed) { //rotate by 1 degree float degreesPerSecond = 180.0f; _gDegreesRotated += (float)secondsElapsed * degreesPerSecond; //don't go over 360 degrees while (_gDegreesRotated > 360.0f) { _gDegreesRotated -= 360.0f; } //move position of camera based on WASD keys, and XZ keys for up and down float moveSpeed = 2.0f; //units per second if (Glfw.GetKey(_window, Key.S)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * -_gCamera.Forward); } else if (Glfw.GetKey(_window, Key.W)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * _gCamera.Forward); } if (Glfw.GetKey(_window, Key.A)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * -_gCamera.Right); } else if (Glfw.GetKey(_window, Key.D)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * _gCamera.Right); } if (Glfw.GetKey(_window, Key.Z)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * -new Vector3(0, 1, 0)); } else if (Glfw.GetKey(_window, Key.X)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * new Vector3(0, 1, 0)); } //rotate camera based on mouse movement float mouseSensitivity = 0.1f; double mouseX, mouseY; Glfw.GetCursorPos(_window, out mouseX, out mouseY); _gCamera.OffsetOrientation(mouseSensitivity * (float)mouseY, mouseSensitivity * (float)mouseX); Glfw.SetCursorPos(_window, 0, 0); //reset the mouse, so it doesn't go out of the window }
/// <inheritdoc /> public void Update() { // Update the mouse position. Glfw.GetCursorPos(_win, out double posX, out double posY); _mousePosition.X = (float)posX; _mousePosition.Y = (float)posY; // Reset mouse scroll. _mouseScrollThisFrame = _mouseScroll; _mouseScroll = _mouseScrollAccum; // Calculate mouse status. foreach (MouseKey key in _mouseKeys) { _mouseStatusShadow[key] = _mouseStatus[key]; int state = Glfw.GetMouseButton(_win, (int)key); _mouseStatus[key] = state >= 1; } // Calculate keyboard status. foreach (KeyCode key in _allKeys) { // Transfer from last frame. _keyStatusShadow[key] = _keyStatus[key]; int state = Glfw.GetKey(_win, (int)key); _keyStatus[key] = state >= 1; } // Update joysticks. foreach (KeyValuePair <int, GlfwJoystick> joystick in _loadedJoysticks) { joystick.Value.Update(); } // Check for fullscreen toggling key combo. if (IsKeyHeld("LeftAlt") && IsKeyDown("Enter")) { Engine.Host.WindowMode = Engine.Host.WindowMode == WindowMode.Fullscreen ? WindowMode.Windowed : WindowMode.Fullscreen; } // Check for closing combo. if (IsKeyDown("Escape")) { Engine.Quit(); } }
static void Update(float Dt) { Glfw.PollEvents(); Glfw.GetCursorPos(Window, out double CurX, out double CurY); Vector2 CurMousePos = new Vector2((float)CurX, (float)CurY); MouseDelta = CurMousePos - MousePos; MousePos = CurMousePos; if (DragDropPaths != null) { OnDragDrop?.Invoke(DragDropPaths); DragDropPaths = null; } UpdatePhysics(Dt); UpdateEntities(Dt); Game.Update(Dt); }
// update the scene based on the time elapsed since last update private static void Update(float secondsElapsed) { //rotate the first instance in `gInstances` float degreesPerSecond = 180.0f; _gDegreesRotated += secondsElapsed * degreesPerSecond; while (_gDegreesRotated > 360.0f) { _gDegreesRotated -= 360.0f; } _gInstances[0].Transform = Matrix.CreateFromAxisAngle(new Vector3(0, 1, 0), _gDegreesRotated); //move position of camera based on WASD keys, and XZ keys for up and down float moveSpeed = 4.0f; //units per second if (Glfw.GetKey(_window, Key.S)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * -_gCamera.Forward); } else if (Glfw.GetKey(_window, Key.W)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * _gCamera.Forward); } if (Glfw.GetKey(_window, Key.A)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * -_gCamera.Right); } else if (Glfw.GetKey(_window, Key.D)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * _gCamera.Right); } if (Glfw.GetKey(_window, Key.Z)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * -new Vector3(0, 1, 0)); } else if (Glfw.GetKey(_window, Key.X)) { _gCamera.OffsetPosition((float)secondsElapsed * moveSpeed * new Vector3(0, 1, 0)); } if (Glfw.GetKey(_window, Key.One)) { _gLight.Position = _gCamera.Position; } if (Glfw.GetKey(_window, Key.Two)) { _gLight.Intensities = new Vector3(1, 0, 0); // red } else if (Glfw.GetKey(_window, Key.Three)) { _gLight.Intensities = new Vector3(0, 1, 0); //green } else if (Glfw.GetKey(_window, Key.Four)) { _gLight.Intensities = new Vector3(1, 1, 1); //white } //rotate camera based on mouse movement float mouseSensitivity = 0.1f; double mouseX, mouseY; Glfw.GetCursorPos(_window, out mouseX, out mouseY); _gCamera.OffsetOrientation(mouseSensitivity * (float)mouseY, mouseSensitivity * (float)mouseX); Glfw.SetCursorPos(_window, 0, 0); //reset the mouse, so it doesn't go out of the window }
static void Main() { Init(); Glfw.Window window; var starCursors = new Glfw.Cursor[CURSOR_FRAME_COUNT]; var currentFrame = Glfw.Cursor.None; Gl.Initialize(); if (!Glfw.Init()) { Environment.Exit(1); } for (int i = 0; i < CURSOR_FRAME_COUNT; i++) { starCursors[i] = CreateCursorFrame(i / (float)CURSOR_FRAME_COUNT); if (!starCursors[i]) { Glfw.Terminate(); Environment.Exit(1); } } Glfw.CursorType[] shapes = { Glfw.CursorType.Arrow, Glfw.CursorType.Beam, Glfw.CursorType.Crosshair, Glfw.CursorType.Hand, Glfw.CursorType.ResizeX, Glfw.CursorType.ResizeY }; standardCursors = new Glfw.Cursor[6]; for (int i = 0; i < standardCursors.Length; i++) { standardCursors[i] = Glfw.CreateStandardCursor(shapes[i]); if (!standardCursors[i]) { Glfw.Terminate(); Environment.Exit(1); } } window = Glfw.CreateWindow(640, 480, "Cursor Test"); if (!window) { Glfw.Terminate(); Environment.Exit(1); } Glfw.MakeContextCurrent(window); Glfw.GetCursorPos(window, out cursorX, out cursorY); Log("Cursor position: {0} {1}", cursorX, cursorY); Glfw.SetCursorPosCallback(window, CursorPositionCallback); Glfw.SetKeyCallback(window, KeyCallback); while (!Glfw.WindowShouldClose(window)) { Gl.Clear(ClearBufferMask.ColorBufferBit); if (trackCursor) { int wnd_width, wnd_height, fb_width, fb_height; float scale; Glfw.GetWindowSize(window, out wnd_width, out wnd_height); Glfw.GetFramebufferSize(window, out fb_width, out fb_height); scale = (float)fb_width / (float)wnd_width; Gl.Viewport(0, 0, fb_width, fb_height); Gl.MatrixMode(MatrixMode.Projection); Gl.LoadIdentity(); Gl.Ortho(0f, fb_width, 0f, fb_height, 0f, 1f); Gl.Begin(PrimitiveType.Lines); Gl.Vertex2(0f, (float)(fb_height - cursorY * scale)); Gl.Vertex2((float)fb_width, (float)(fb_height - cursorY * scale)); Gl.Vertex2((float)cursorX * scale, 0f); Gl.Vertex2((float)cursorX * scale, (float)fb_height); Gl.End(); } Glfw.SwapBuffers(window); if (animateCursor) { var i = (int)(Glfw.GetTime() * 30.0) % CURSOR_FRAME_COUNT; if (currentFrame != starCursors[i]) { Glfw.SetCursor(window, starCursors[i]); currentFrame = starCursors[i]; } } else { currentFrame = Glfw.Cursor.None; } if (waitEvents) { if (animateCursor) { Glfw.WaitEventsTimeout(1.0 / 30.0); } else { Glfw.WaitEvents(); } } else { Glfw.PollEvents(); } } Glfw.DestroyWindow(window); for (int i = 0; i < CURSOR_FRAME_COUNT; i++) { Glfw.DestroyCursor(starCursors[i]); } for (int i = 0; i < standardCursors.Length; i++) { Glfw.DestroyCursor(standardCursors[i]); } Glfw.Terminate(); }