void Camera::Update(GLFWwindow *window, float delta) { changed_ = false; if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) { double xpos, ypos; glfwGetCursorPos(window, &xpos, &ypos); glfwSetCursorPos(window, width_ / 2, height_ / 2); yaw_ -= (static_cast<float>(xpos) - static_cast<float>(width_) / 2.0f) * sensivity_; pitch_ -= (static_cast<float>(ypos) - static_cast<float>(height_) / 2.0f) * sensivity_; pitch_ = clamp(pitch_, radians(-89.9f), radians(89.9f)); changed_ = true; } front_ = float3(cos(yaw_)*cos(pitch_), sin(yaw_)*cos(pitch_), sin(pitch_)); float3 right = float3(sin(yaw_), -cos(yaw_), 0.0f); up_ = cross(right, front_); if (glfwGetKey(window, GLFW_KEY_W)) { velocity_ += front_ * speed_ * delta; changed_ = true; } if (glfwGetKey(window, GLFW_KEY_S)) { velocity_ -= front_ * speed_ * delta; changed_ = true; } if (glfwGetKey(window, GLFW_KEY_A)) { velocity_ -= right * speed_ * delta; changed_ = true; } if (glfwGetKey(window, GLFW_KEY_D)) { velocity_ += right * speed_ * delta; changed_ = true; } position_ += velocity_; velocity_ *= 0.5f; }
void InputManager::Update() { _prevLeftMouseButton = _leftMouseButton; _leftMouseButton = glfwGetMouseButton(_window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS; glfwGetCursorPos(_window, &_mousePos[0], &_mousePos[1]); bool insideWindow = _mousePos[0] > 0 && _mousePos[0] < _windowSize[0] && _mousePos[1] > 0 && _mousePos[1] < _windowSize[1]; if (_leftMouseButton && (insideWindow || _cursorLocked)) { glfwSetCursorPos(_window, _windowSize[0] / 2, _windowSize[1] / 2); if (!_cursorLocked) { glfwGetCursorPos(_window, &_mousePos[0], &_mousePos[1]); _cursorLocked = true; } glfwSetInputMode(_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); } else { _cursorLocked = false; glfwSetInputMode(_window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); } _prevDownKey = _downKey; _downKey = glfwGetKey(_window, GLFW_KEY_DOWN) == GLFW_PRESS; _prevUpKey = _upKey; _upKey = glfwGetKey(_window, GLFW_KEY_UP) == GLFW_PRESS; _prevLeftKey = _leftKey; _leftKey = glfwGetKey(_window, GLFW_KEY_LEFT) == GLFW_PRESS; _prevRightKey = _rightKey; _rightKey = glfwGetKey(_window, GLFW_KEY_RIGHT) == GLFW_PRESS; _prevWKey = _wKey; _wKey = glfwGetKey(_window, GLFW_KEY_W) == GLFW_PRESS; _prevAKey = _aKey; _aKey = glfwGetKey(_window, GLFW_KEY_A) == GLFW_PRESS; _prevSKey = _sKey; _sKey = glfwGetKey(_window, GLFW_KEY_S) == GLFW_PRESS; _prevDKey = _dKey; _dKey = glfwGetKey(_window, GLFW_KEY_D) == GLFW_PRESS; _prevShiftKey = _shiftKey; _shiftKey = glfwGetKey(_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS; _prevCtrlKey = _ctrlKey; _ctrlKey = glfwGetKey(_window, GLFW_KEY_LEFT_CONTROL) == GLFW_PRESS; _prevSpaceKey = _spaceKey; _spaceKey = glfwGetKey(_window, GLFW_KEY_SPACE) == GLFW_PRESS; }
void ImGui_ImplGlfw_NewFrame() { if (!g_FontTexture) ImGui_ImplGlfw_CreateDeviceObjects(); ImGuiIO& io = ImGui::GetIO(); // Setup display size (every frame to accommodate for window resizing) int w, h; int display_w, display_h; glfwGetWindowSize(g_Window, &w, &h); glfwGetFramebufferSize(g_Window, &display_w, &display_h); io.DisplaySize = ImVec2((float)w, (float)h); io.DisplayFramebufferScale = ImVec2((float)display_w / w, (float)display_h / h); // Setup time step double current_time = glfwGetTime(); io.DeltaTime = g_Time > 0.0 ? (float)(current_time - g_Time) : (float)(1.0f/60.0f); g_Time = current_time; // Setup inputs // (we already got mouse wheel, keyboard keys & characters from glfw callbacks polled in glfwPollEvents()) if (glfwGetWindowAttrib(g_Window, GLFW_FOCUSED)) { double mouse_x, mouse_y; glfwGetCursorPos(g_Window, &mouse_x, &mouse_y); io.MousePos = ImVec2((float)mouse_x, (float)mouse_y); // Mouse position in screen coordinates (set to -1,-1 if no mouse / on another screen, etc.) } else { io.MousePos = ImVec2(-1,-1); } for (int i = 0; i < 3; i++) { io.MouseDown[i] = g_MousePressed[i] || glfwGetMouseButton(g_Window, i) != 0; // If a mouse press event came, always pass it as "mouse held this frame", so we don't miss click-release events that are shorter than 1 frame. g_MousePressed[i] = false; } io.MouseWheel = g_MouseWheel; g_MouseWheel = 0.0f; // Hide OS mouse cursor if ImGui is drawing it glfwSetInputMode(g_Window, GLFW_CURSOR, io.MouseDrawCursor ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL); // Start the frame ImGui::NewFrame(); }
void mousePosEventHandle(int iPosX,int iPosY) { int iButtonState = glfwGetMouseButton(GLFW_MOUSE_BUTTON_LEFT); //to test move if (iButtonState == GLFW_PRESS) { if (iPosX!=(int)s_pMainWindow->m_mousePoint.x||iPosY!=(int)s_pMainWindow->m_mousePoint.y) { //it movies s_pMainWindow->m_pTouch->SetTouchInfo(0, (float)(iPosX- s_pMainWindow->m_rcViewPort.origin.x) / s_pMainWindow->m_fScreenScaleFactor, (float)(iPosY - s_pMainWindow->m_rcViewPort.origin.y) / s_pMainWindow->m_fScreenScaleFactor); s_pMainWindow->m_pDelegate->touchesMoved(s_pMainWindow->m_pSet, NULL); //update new mouse pos s_pMainWindow->m_mousePoint.x = iPosX; s_pMainWindow->m_mousePoint.y = iPosY; } } }
void Turret::update(float a_DT) { // std::cout << m_turretEnd.GetTranslation().x << " ---------- " << m_turretEnd.GetTranslation().y << std::endl; glfwGetCursorPos(m_mainGame->getWindow(), &m_mPosX, &m_mPosY); //get the normalised difference of the object (from centre) and the mouse pos //takes into account the scale of the matrix Vec2 diff = Vec2( Vec2(m_mPosX,m_mPosY) - Vec2(m_node->getFinalTransform().GetTranslation().x + (m_texture->getWidth() * m_node->getFinalTransform().GetScale().x / 2) + m_pivot.x, m_node->getFinalTransform().GetTranslation().y + (m_texture->getHeight() * m_node->getFinalTransform().GetScale().x / 2) + m_pivot.y)).GetNormalised(); //get the radian float rot = Vec2(0,1).GetAngleBetween(diff); //get the parent tanks rotation and offset the turrets rotation float allRot = m_node->getFinalTransform().GetUpRot(); float thisRot = m_node->getNodeTransform().GetUpRot(); float rotOffset = allRot - thisRot; //set rotation to point at mouse and offset the parents rotation m_node->manipMat3()->SetRotation(rot - rotOffset); //m_turretEnd = m_node->getFinalTransform(); //int button; if(glfwGetMouseButton(m_mainGame->getWindow(), GLFW_MOUSE_BUTTON_1)) { //get the current running time of the game float totalTime = glfwGetTime(); //get a random number between 0~1 float r = (float)((double) rand() / RAND_MAX); //fire gun at a steady rate offset by the random number if(totalTime - m_lastFired >= m_fireRate + r) { m_lastFired = totalTime; SpawnBullet(); } // SpawnBullet(); } }
InputState Input::GetMouseButton( InputMouse button ) { InputState state = INPUT_UP; if ( window_ != nullptr ) { if ( glfwGetMouseButton( window_, button ) == GLFW_PRESS ) { if ( mouse_map_[button] == INPUT_UP ) { mouse_map_[button] = state = INPUT_PRESS; } if ( mouse_map_[button] == INPUT_PRESS ) { mouse_map_[button] = state = INPUT_DOWN; } if ( mouse_map_[button] == INPUT_DOWN ) { /*mouse_map_[button] =*/ state = INPUT_DOWN; } if ( mouse_map_[button] == INPUT_RELEASE ) { mouse_map_[button] = state = INPUT_PRESS; } } else { if ( mouse_map_[button] == INPUT_UP ) { /*mouse_map_[button] =*/ state = INPUT_UP; } if ( mouse_map_[button] == INPUT_PRESS ) { mouse_map_[button] = state = INPUT_RELEASE; } if ( mouse_map_[button] == INPUT_DOWN ) { mouse_map_[button] = state = INPUT_RELEASE; } if ( mouse_map_[button] == INPUT_RELEASE ) { mouse_map_[button] = state = INPUT_UP; } } } return state; }
void upDate2DPhysics(float delta) { BoxClass* box1 = (BoxClass*)physicsScene->actors[0]; static glm::vec2 cm_to_anchor = glm::vec2(); static bool grabbed = false; if ( glfwGetMouseButton(window, 0) == GLFW_PRESS ) { if (!grabbed) { if (box1->isPointOver(GetWorldMouse())) // added this check { cm_to_anchor = GetWorldMouse() - box1->position; float sin_theta = sinf(-box1->rotation2D); float cos_theta = cosf(-box1->rotation2D); cm_to_anchor = glm::vec2(cos_theta * cm_to_anchor.x - sin_theta * cm_to_anchor.y, sin_theta * cm_to_anchor.x + cos_theta * cm_to_anchor.y); grabbed = true; } } else //added the else { //compute the anchor point float sin_theta = sinf(box1->rotation2D); float cos_theta = cosf(box1->rotation2D); glm::vec2 rot_local_pos = glm::vec2(cos_theta * cm_to_anchor.x - sin_theta * cm_to_anchor.y, sin_theta * cm_to_anchor.x + cos_theta * cm_to_anchor.y); glm::vec2 anchor = box1->position + rot_local_pos; //add force at anchor point towards the mouse box1->applyForceAtPoint(GetWorldMouse() - anchor, anchor); Gizmos::add2DLine(anchor, GetWorldMouse(), glm::vec4(0, 1, 1, 1)); } } else { grabbed = false; } physicsScene->upDate(); physicsScene->upDateGizmos(); onUpdateRocket(delta); }
bool Input::step() { INIT_ASSERT(Input); int count; for (unsigned i = 0; i < NUM_OF_KEYS; ++i) { keyState[i] = keyPress[i] > keyRelease[i]; if (glfwGetKey(Window::instance().window, i) == GLFW_PRESS) keyPress[i] = glfwGetTime(); else keyRelease[i] = glfwGetTime(); } for (unsigned i = 0; i < NUM_OF_MOUSE_BTNS; ++i) { mouseState[i] = mousePress[i] > mouseRelease[i]; if (glfwGetMouseButton(Window::instance().window, i) == GLFW_PRESS) mousePress[i] = glfwGetTime(); else mouseRelease[i] = glfwGetTime(); } for (unsigned i = 0; i < activeJoysticks; ++i) { for (unsigned j = 0; j < NUM_OF_JOY_BTNS; ++j) { joyState[i][j] = joyPress[i][j] > joyRelease[i][j]; if (glfwGetJoystickButtons(i, &count)[j] == GLFW_PRESS) joyPress[i][j] = glfwGetTime(); else joyRelease[i][j] = glfwGetTime(); } } double x, y; glfwGetCursorPos(Window::instance().window, &x, &y); mouseX = x; mouseY = y; for (unsigned i = 0; i < activeJoysticks; ++i) for (unsigned j = 0; j < NUM_OF_JOY_AXES; ++j) joyAxes[i][j] = glfwGetJoystickAxes(i, &count)[j]; return false; }
void mainmenu(){ //主菜单 gui::Form MainForm; int leftp = windowwidth / 2 - 200; int midp = windowwidth / 2; int rightp = windowwidth / 2 + 200; int upp = 280; glfwSetInputMode(MainWindow, GLFW_CURSOR, GLFW_CURSOR_NORMAL); glClearColor(0.0, 0.0, 0.0, 1.0); glDisable(GL_CULL_FACE); bool f = false; MainForm.Init(); TextRenderer::setFontColor(1.0, 1.0, 1.0, 1.0); gui::button* startbtn = MainForm.createbutton("开始游戏"); gui::button* optionsbtn = MainForm.createbutton(">> 选项..."); gui::button* quitbtn = MainForm.createbutton("退出"); do{ leftp = windowwidth / 2 - 200; midp = windowwidth / 2; rightp = windowwidth / 2 + 200; startbtn->resize(leftp, rightp, upp, upp + 32); optionsbtn->resize(leftp, midp - 3, upp + 38, upp + 72); quitbtn->resize(midp + 3, rightp, upp + 38, upp + 72); mb = glfwGetMouseButton(MainWindow, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS ? 1 : 0; glfwGetCursorPos(MainWindow, &mx, &my); MainForm.mousedata((int)mx, (int)my, mw, mb); MainForm.update(); if (startbtn->clicked) worldmenu(); if (gamebegin) f = true; if (optionsbtn->clicked) options(); if (quitbtn->clicked) exit(0); MainForm.render(); glBindTexture(GL_TEXTURE_2D, guiImage[4]); glBegin(GL_QUADS); glTexCoord2f(0.0f, 1.0f), glVertex2i(midp - 256, 20); glTexCoord2f(0.0f, 0.5f), glVertex2i(midp - 256, 276); glTexCoord2f(1.0f, 0.5f), glVertex2i(midp + 256, 276); glTexCoord2f(1.0f, 1.0f), glVertex2i(midp + 256, 20); glEnd(); glfwSwapBuffers(MainWindow); glfwPollEvents(); if (glfwWindowShouldClose(MainWindow)) exit(0); } while (!f); MainForm.cleanup(); }
void GLPlayer::step(void) { double firstTime = glfwGetTime(); if (glfwGetMouseButton(OpenGLWrapper::window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) { updateMousePos(); } camera->calculateMatrix(&config, xpos, ypos, deltaTime); xpos = config.width / 2.0f; ypos = config.height / 2.0f; glm::mat4 ModelMatrix = glm::mat4(1.0); glm::mat4 MVP = camera->projectionMatrix * camera->viewMatrix * ModelMatrix; GLint model = glGetUniformLocation(OpenGLWrapper::programObject, "mvp"); glUniformMatrix4fv(model, 1, GL_FALSE, glm::value_ptr(MVP)); GLint loc = glGetUniformLocation(OpenGLWrapper::programObject, "baseColor"); glUniform4f(loc, 0.75f, 0.64f, 0.04f, 1.0f); GLint pos = glGetUniformLocation(OpenGLWrapper::programObject, "vDir"); glUniform3f(pos, camera->direction.x, camera->direction.y, camera->direction.z); pos = glGetUniformLocation(OpenGLWrapper::programObject, "angle"); glUniform1f(pos, angle); angle += 0.1f; glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glViewport(0, 0, config.width, config.height); glClear(GL_COLOR_BUFFER_BIT); glUseProgram(OpenGLWrapper::programObject); meshHandler->render(); double lastTime = glfwGetTime(); deltaTime = float(lastTime - firstTime); deltaTime = (deltaTime == 0) ? 0.0015 : deltaTime; sprintf(title, "%s - fps[%.2f]", config.title, (float)(1 / deltaTime)); glfwSetWindowTitle(OpenGLWrapper::window, title); }
static void fpsCameraViewMatrix(GLFWwindow *window, float *view) { // initial camera config static float position[] = { 0.0f, 0.3f, 1.5f }; static float rotation[] = { 0.0f, 0.0f }; // mouse look static double lastMouse[] = { 0.0, 0.0 }; double mouse[2]; glfwGetCursorPos(window, &mouse[0], &mouse[1]); if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) { rotation[0] += (float)(mouse[1] - lastMouse[1]) * -0.2f; rotation[1] += (float)(mouse[0] - lastMouse[0]) * -0.2f; } lastMouse[0] = mouse[0]; lastMouse[1] = mouse[1]; float rotationY[16], rotationX[16], rotationYX[16]; rotationMatrix(rotationX, rotation[0], 1.0f, 0.0f, 0.0f); rotationMatrix(rotationY, rotation[1], 0.0f, 1.0f, 0.0f); multiplyMatrices(rotationYX, rotationY, rotationX); // keyboard movement (WSADEQ) float speed = (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) ? 0.1f : 0.01f; float movement[3] = {0}; if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) movement[2] -= speed; if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) movement[2] += speed; if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) movement[0] -= speed; if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) movement[0] += speed; if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) movement[1] -= speed; if (glfwGetKey(window, GLFW_KEY_Q) == GLFW_PRESS) movement[1] += speed; float worldMovement[3]; transformPosition(worldMovement, rotationYX, movement); position[0] += worldMovement[0]; position[1] += worldMovement[1]; position[2] += worldMovement[2]; // construct view matrix float inverseRotation[16], inverseTranslation[16]; transposeMatrix(inverseRotation, rotationYX); translationMatrix(inverseTranslation, -position[0], -position[1], -position[2]); multiplyMatrices(view, inverseRotation, inverseTranslation); // = inverse(translation(position) * rotationYX); }
void mousemove(GLFWwindow *wnd, double x, double y) { mx = ((float)x / (screen_width/2)) - 1.0F; my = 1.0F-((float)y / (screen_height/2)); mix = (int)round(x); miy = (int)round(y); if (glfwGetMouseButton(window,GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) switch (MCursor) { case 2: { wd_y1 = my; break; } case 3: wd_x1 = mx; } if ((mx > (wd_x1-(3*px)))&&(mx < (wd_x1+(3*px)))&&(my > (wd_y1+(3*py)))) MCursor = 3; else if ((my < (wd_y1+(3*py)))&&(my > (wd_y1-(3*py)))) MCursor = 2; else MCursor = 1; Redisplay=1; }
void InputHandler::Flush() { for (int i = 32; i < GLFW_KEY_LAST + 1; ++i) key[i].state = -1; if (freezeCursor) ResetCursorPos(); for (int i = 0; i < 3; ++i) { if (glfwGetMouseButton(Screen.window, i) != GLFW_PRESS) clickBuffer[i] = false; else clickBuffer[i] = true; } scrollX = scrollY = 0; }
void State::Gameplay::handleMouseMovement(GLFWwindow* window, double x, double y) { static State::Gameplay& thisState = *States::get().gameplay; static glm::dvec2 cursorPosLast; static glm::dvec2 cursorPosNow; static glm::vec2 cursorMovement; static bool pressedLast = false; static double centerX = Game::get().getWindow().getWidth() / 2.0; static double centerY = Game::get().getWindow().getHeight() / 2.0; static double dx; static double dy; if(thisState.isMode2D()) { cursorPosNow = glm::dvec2(x, y); if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) { if(pressedLast) { cursorMovement = cursorPosNow - cursorPosLast; cursorMovement.y = -cursorMovement.y; cursorMovement.x *= 0.0025f * (1.0f / thisState._mapController.getZoom()); cursorMovement.y *= (0.01f / 3.0f) * (1.0f / thisState._mapController.getZoom()); cursorMovement *= (4.0f / 3.0f); thisState._mapController.move(cursorMovement); } pressedLast = true; } else { pressedLast = false; } cursorPosLast = cursorPosNow; } else { dx = x - centerX; dy = y - centerY; thisState.getCamera().updateMouse(static_cast<int>(dx), static_cast<int>(dy)); glfwSetCursorPos(window, centerX, centerY); } }
void UpdateState() { std::swap(this->PreviousMouseState, this->CurrentMouseState); double x, y; glfwGetCursorPos(this->GlfwWindow, &x, &y); this->CurrentMouseState.MousePosition = Vector2f(x, y); this->CurrentMouseState.ScrollWheelDelta = GetScrollWheelValue(); for (int i = 0; i < MouseState::ButtonCount; i++) { this->CurrentMouseState.SetValue(i, glfwGetMouseButton(this->GlfwWindow, i) == GLFW_PRESS); } std::swap(this->PreviousKeyboardState, this->CurrentKeyboardState); for (int i = 0; i < KeyboardState::KeyCount; i++) { this->CurrentKeyboardState.SetValue(i, glfwGetKey(this->GlfwWindow, KeyCodeToGLFWKey(static_cast<KeyCode>(i))) == GLFW_PRESS); } }
void PlayerControl::update() { pickRayDirection(); rayPlaneIntersection(); if(glfwGetMouseButton(m_window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS) { m_mouseDown = true; } else { if(m_mouseDown) { //event on current position m_event = true; } m_mouseDown = false; } }
void Application::Update() { m_pTimer->Update(); m_pSceneManager->Update(m_pTimer->GetDelta()); m_pMoveTarget = m_pSceneManager->GetMoveTarget().get(); GLUtil::CheckError(); int state = glfwGetMouseButton(m_pWindow, GLFW_MOUSE_BUTTON_RIGHT); static double x,y; double _x = x, _y = y; glfwGetCursorPos(m_pWindow, &x, &y); if (state == GLFW_PRESS){ m_pMoveTarget->Rotate((x-_x)/100.0f, (y-_y)/100.0f); } else{ m_pInputManager->OnMousePos(x, y); } }
// This runs once every physics timestep. void update() { //Get the current mouse position double currentMouseX, currentMouseY; glfwGetCursorPos(window, ¤tMouseX, ¤tMouseY); //Check if the mouse button is being pressed if (glfwGetMouseButton(window, 0) == GLFW_PRESS) { //Get the difference in mouse position from last frame float deltaMouseX = (float)(currentMouseX - prevMouseX); float deltaMouseY = (float)(currentMouseY - prevMouseY); static float rotationSpeed = 0.01f; glm::mat4 yaw; glm::mat4 pitch; //Rotate the selected shape by an angle equal to the mouse movement if (deltaMouseX != 0.0f) yaw = glm::rotate(glm::mat4(1.0f), deltaMouseX * rotationSpeed, glm::vec3(0.0f, 1.0f, 0.0f)); if (deltaMouseY != 0.0f) pitch = glm::rotate(glm::mat4(1.0f), deltaMouseY * -rotationSpeed, glm::vec3(1.0f, 0.0f, 0.0f)); right->direction = glm::vec3(pitch * yaw * glm::vec4(glm::normalize(right->direction), 0.0f)); //Perform the graham schmidt process to generate two more mutually orthogonal vectors which, //with the right vector, will form a basis for 3D space GrahamSchmidt(right->direction, up->direction, forward->direction); right->direction *= 0.2f; up->direction *= 0.2f; forward->direction *= 0.2f; } //Update previous positions prevMouseX = currentMouseX; prevMouseY = currentMouseY; }
void mouse_callback(GLFWwindow* window) { int i = 0; if (glfwGetMouseButton(window, i) == GLFW_PRESS) { std::map<int, std::function<void()>> activeMap = iH.getActiveInputMap()->getMap(); iH.getActiveInputMap()->setGLFWwindow(window); for (std::map<int, std::function<void()>>::iterator it = activeMap.begin(); it != activeMap.end(); it++){ if (it->first == i) activeMap.at(i)(); if (it == activeMap.end()) std::cout << "Key is not mapped to an action" << std::endl; } } else{ cam.updateCursor(window); } }
void GameController2D::Update() { if (IsKeyPressed('W')) model->setCommands(GameModel2D::COMMANDS::MOVE_UP); if (IsKeyPressed('A')) model->setCommands(GameModel2D::COMMANDS::MOVE_LEFT); if (IsKeyPressed('S')) model->setCommands(GameModel2D::COMMANDS::MOVE_DOWN); if (IsKeyPressed('D')) model->setCommands(GameModel2D::COMMANDS::MOVE_RIGHT); if (IsKeyPressed(VK_SPACE)) model->setCommands(GameModel2D::COMMANDS::UNLOCK); if (IsKeyPressed(VK_RETURN)) model->setCommands(GameModel2D::COMMANDS::ENTER); if (IsKeyPressed(VK_ESCAPE)) model->setCommands(GameModel2D::COMMANDS::ESCAPE); //Weapon changing //if (IsKeyPressed('Q')) // model->setCommands(GameModel2D::COMMANDS::PREVWEAP); //if (IsKeyPressed('E')) // model->setCommands(GameModel2D::COMMANDS::NEXTWEAP); if (IsKeyPressed('1')) model->setCommands(GameModel2D::COMMANDS::WEAPON1); if (IsKeyPressed('2')) model->setCommands(GameModel2D::COMMANDS::WEAPON2); if (IsKeyPressed('3')) model->setCommands(GameModel2D::COMMANDS::WEAPON3); if (glfwGetMouseButton(view->getWindow(), 0)) model->setCommands(GameModel2D::COMMANDS::SHOOT); if (IsKeyPressed('R')) model->setCommands(GameModel2D::COMMANDS::RELOAD); if (IsKeyPressed('X')) model->setCommands(GameModel2D::COMMANDS::CHECK); if (IsKeyPressed('E')) model->setCommands(GameModel2D::COMMANDS::INTERACT); Controller::Update(); }
void FlyCamera::HandleMouseInput(double dt) { if (glfwGetMouseButton(m_window, GLFW_MOUSE_BUTTON_3) == GLFW_PRESS) { if (m_bViewButtonClicked == false) { int width, height; glfwGetFramebufferSize(m_window, &width, &height); m_dCursorX = width / 2.0; m_dCursorY = height / 2.0; glfwSetCursorPos(m_window, m_dCursorX, m_dCursorY); m_bViewButtonClicked = true; } else { double mouseX, mouseY; glfwGetCursorPos(m_window, &mouseX, &mouseY); double xOffset = mouseX - m_dCursorX; double yOffset = mouseY - m_dCursorY; m_dCursorX = mouseX; m_dCursorY = mouseY; CalculateRotation(dt, xOffset, yOffset); } } else { m_bViewButtonClicked = false; } }
// // カラーバッファを入れ替えてイベントを取り出す // void Window::swapBuffers() { // カラーバッファを入れ替える glfwSwapBuffers(window); // OpenGL のエラーをチェックする ggError("SwapBuffers"); // イベントを取り出す glfwPollEvents(); // マウスの位置を調べる double x, y; glfwGetCursorPos(window, &x, &y); // 左ボタンドラッグ if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1)) { // トラックボール回転 tb.motion(static_cast<GLfloat>(x), static_cast<GLfloat>(y)); } }
int input_mouse_getkey_platform(GLFWwindow* wnd_hdl, enum input_mouse_key mkey) { int button = 0; switch (mkey) { case INPUT_MOUSEKEY_LEFT: button = GLFW_MOUSE_BUTTON_LEFT; break; case INPUT_MOUSEKEY_RIGHT: button = GLFW_MOUSE_BUTTON_RIGHT; break; case INPUT_MOUSEKEY_MIDDLE: button = GLFW_MOUSE_BUTTON_MIDDLE; break; case INPUT_MOUSEKEY_PGUP: button = GLFW_MOUSE_BUTTON_4; break; case INPUT_MOUSEKEY_PGDOWN: button = GLFW_MOUSE_BUTTON_5; break; } return glfwGetMouseButton(wnd_hdl, button) == GLFW_PRESS; }
void handle_input() { glfwPollEvents(); const int left_mouse_button_state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1); if (left_mouse_button_state == GLFW_PRESS) { double x_pos_old = x_pos; double y_pos_old = y_pos; glfwGetCursorPos(window, &x_pos, &y_pos); if (!reset_press && x_pos_old == x_pos && y_pos_old == y_pos) return; reset_press = false; if (isDragging == false) { isDragging = true; engine->OnTouchPress(x_pos, y_pos); } else { engine->OnTouchDrag(x_pos, y_pos); } } else { isDragging = false; reset_press = true; } }
bool Application::GetMouseUpdate() { glfwGetCursorPos(m_window, &mouse_current_x, &mouse_current_y); // Calculate the difference in positions mouse_diff_x = mouse_current_x - mouse_last_x; mouse_diff_y = mouse_current_y - mouse_last_y; //Calculate the yaw and pitch camera_yaw = (float) mouse_diff_x * 0.0174555555555556f;// * 3.142f / 180.0f; camera_pitch = mouse_diff_y * 0.0174555555555556f;// 3.142f / 180.0f ); //Store the current position as the last position mouse_last_x = mouse_current_x; mouse_last_y = mouse_current_y; //Get the mouse button status if(glfwGetMouseButton(m_window, GLFW_MOUSE_BUTTON_LEFT) == GLFW_PRESS) { scene->UpdateAttackStatus(scene->CA_ATTACK); } return false; }
void Camera::Input(GLFWwindow *window, float speed, float deltaTime) { glfwSetScrollCallback(window, scrollFunc); /*if (glfwGetKey(window,GLFW_KEY_LEFT) == GLFW_PRESS) { worldTransform[3].xyz += -1 * worldTransform[0].xyz * speed * deltaTime; } if (glfwGetKey(window, GLFW_KEY_RIGHT) == GLFW_PRESS) { worldTransform[3].xyz += worldTransform[0].xyz * speed * deltaTime; } if (glfwGetKey(window, GLFW_KEY_UP) == GLFW_PRESS) { worldTransform[3].xyz += worldTransform[1].xyz * speed * deltaTime; } if (glfwGetKey(window, GLFW_KEY_DOWN) == GLFW_PRESS) { worldTransform[3].xyz -= worldTransform[1].xyz *speed * deltaTime; }*/ if ( Y_SCROLL == -1) { worldTransform[3].xyz += worldTransform[2].xyz * speed * deltaTime * 100; Y_SCROLL = 0; } if ( Y_SCROLL == 1) { worldTransform[3].xyz -= worldTransform[2].xyz * speed * deltaTime * 100; Y_SCROLL = 0; } glfwGetCursorPos(window, ¤tXCursor, ¤tYCursor); deltaXCursor = currentXCursor - lastXCursor; deltaYCursor = currentYCursor - lastYCursor; lastXCursor = currentXCursor; lastYCursor = currentYCursor; // scrollbar click if (glfwGetMouseButton(window, 2)) { if (deltaXCursor > 0) { worldTransform[3] -= worldTransform[0] * speed * deltaXCursor * deltaTime; } if (deltaXCursor < 0) { worldTransform[3] += worldTransform[0] * speed * -deltaXCursor * deltaTime; } if (deltaYCursor < 0) { worldTransform[3] -= worldTransform[1] * speed * -deltaYCursor * deltaTime; } if (deltaYCursor > 0) { worldTransform[3] += worldTransform[1] * speed * deltaYCursor * deltaTime; } } // right click if (glfwGetMouseButton(window, 1)) { if (deltaXCursor > 0) { //worldTransform = rotate((double)deltaXCursor, vec3(0, 1, 0)); //worldTransform = rotate(worldTransform, 10, vec3(0, 1, 0)); //worldTransform = rotateX(glm::vec3(worldTransform[3].xyz), (const double) deltaXCursor); } if (deltaXCursor < 0) { //worldTransform[0] += worldTransform[0] * speed * -deltaXCursor * deltaTime; } if (deltaYCursor < 0) { //worldTransform[2] -= worldTransform[1] * speed * -deltaYCursor * deltaTime; } if (deltaYCursor > 0) { //worldTransform[2] += worldTransform[1] * speed * deltaYCursor * deltaTime; } } }
void FlyCamera::update(float dt) { GLFWwindow* curr_window = glfwGetCurrentContext(); if (glfwGetKey(glfwGetCurrentContext(), GLFW_KEY_W) == GLFW_PRESS) { m_world[3] -= m_world[2] * m_speed * dt; //forwards } if (glfwGetKey(glfwGetCurrentContext(), GLFW_KEY_S) == GLFW_PRESS) { m_world[3] += m_world[2] * m_speed * dt; //backwards } if (glfwGetKey(glfwGetCurrentContext(), GLFW_KEY_A) == GLFW_PRESS) { m_world[3] -= m_world[0] * m_speed * dt; //left } if (glfwGetKey(glfwGetCurrentContext(), GLFW_KEY_D) == GLFW_PRESS) { m_world[3] += m_world[0] * m_speed * dt; //right } if (glfwGetKey(glfwGetCurrentContext(), GLFW_KEY_Q) == GLFW_PRESS) { m_world[3] += m_world[1] * m_speed * dt; //up } if (glfwGetKey(glfwGetCurrentContext(), GLFW_KEY_E) == GLFW_PRESS) { m_world[3] -= m_world[1] * m_speed * dt; //down } m_world[3][3] = 1; double x_delta, y_delta; if (glfwGetMouseButton(curr_window, 1)) { glfwGetCursorPos(curr_window, &x_delta, &y_delta); glfwSetCursorPos(curr_window, 1280.f / 2.f, 720.f / 2.f); x_delta -= (1280.f / 2.f); y_delta -= (720.f / 2.f); x_delta /= (1280.f / 2.f); y_delta /= (720.f / 2.f); x_delta *= -0.5f; y_delta *= -0.5f; vec3 camera_right = (vec3)m_world[0]; mat4 yaw = glm::rotate((float)x_delta, vec3(0, 1, 0)); mat4 pitch = glm::rotate((float)y_delta, camera_right); mat4 rot = yaw * pitch; m_world[0] = rot * m_world[0]; m_world[1] = rot * m_world[1]; m_world[2] = rot * m_world[2]; } m_view = glm::inverse(m_world); UpdateViewProj(); }
bool GLFWInput::isMouseButtonHold(MouseButton btn) { return glfwGetMouseButton(m_Window, GLFWMouseButtonMap[static_cast<int>(btn)])==GLFW_PRESS; }
bool Screen::MouseButtonPressed(int button) const { return glfwGetMouseButton(button) == GLFW_PRESS; }
bool input_mouse_down(MouseCode mouse) { int glfwmouse = _mousecode_to_glfw(mouse); return glfwGetMouseButton(game_window, glfwmouse) == GLFW_PRESS; }