void Camera::updateCameraRotation() { double xPos, yPos; glfwGetCursorPos(m_window, &xPos, &yPos); // get current mouse position // if mouse still in window center, no need to recalculate view matrix if (xPos == m_windowWidth / 2 && yPos == m_windowHeight / 2) { return; } centerMouse(); m_horizontalAngle += float(m_windowWidth / 2 - xPos) * m_mouseSpeed; m_verticalAngle += float(m_windowHeight / 2 - yPos) * m_mouseSpeed; glm::vec3 oldLookIn = getLookAt() - getCenter(); glm::vec3 lookInHorizontal = glm::vec3(sin(m_horizontalAngle), oldLookIn.y, cos(m_horizontalAngle)); glm::vec3 lookInVertical = glm::vec3(oldLookIn.x * cos(m_verticalAngle), sin(m_verticalAngle), oldLookIn.z * cos(m_verticalAngle)); glm::vec3 newLookIn = glm::normalize(lookInHorizontal + lookInVertical); setLookAt(getCenter() + newLookIn); float rightHorizontalAngle = m_horizontalAngle - PI / 2.0f; setRight(glm::vec3(sin(rightHorizontalAngle), getRight().y, cos(rightHorizontalAngle))); setUp(glm::normalize(glm::cross(getRight(), newLookIn))); }
void setDragFunc(funcDrag drag) { gDrag = drag; if (gDrag) { centerMouse(&gLastX, &gLastY); } }
Camera::Camera(GLFWwindow *window) { m_window = window; getWindowDimensions(); centerMouse(); initVars(); setCenter(glm::vec3(0, 0, 5)); setLookAt(glm::vec3(0, 0, 4)); setUp(glm::vec3(0, 1, 0)); setRight(glm::vec3(1, 0, 0)); updateViewMatrix(); }
void InputManager::onPassiveMotion(int x, int y){ if(x != last_mx || y != last_my){ float dx = (float)(x - last_mx)/windowWidth; float dy = (float)(last_my - y)/windowHeight; last_my = y, last_mx = x; auto pair = event_assocs_.find(ENTITY_ROTATE_BY); if(pair != event_assocs_.end()){ for(auto entity: pair->second) evt_manager_->queueEvent(new EntityXYEvent(ENTITY_ROTATE_BY, entity, dx, dy)); } centerMouse(); } }
void Input::setMouseMode(Mouse::Mode mode) { if(m_mouseMode == mode) { return; } m_mouseMode = mode; if(backend && m_useRawMouseInput) { if(!backend->setMouseMode(mode)) { m_useRawMouseInput = false; } } if(m_mouseMode == Mouse::Absolute) { centerMouse(); } }
void Input::update(float time) { backend->update(); bool keyJustPressed = false; iKeyId = -1; int modifier = 0; for(int i = 0; i < Keyboard::KeyCount; i++) { if(isKeyPressed(i)) { switch(i) { case Keyboard::Key_LeftShift: case Keyboard::Key_RightShift: case Keyboard::Key_LeftCtrl: case Keyboard::Key_RightCtrl: case Keyboard::Key_LeftAlt: case Keyboard::Key_RightAlt: modifier = i; break; } if(keysStates[i] < 2) { keysStates[i]++; } if(!keyJustPressed) { if(keysStates[i] == 1) { iKeyId = i; keyJustPressed = true; } else { iKeyId = i; } } } else { if(keysStates[i] > 0) { keysStates[i]--; } } } if(modifier != 0 && iKeyId != modifier) { iKeyId |= (modifier << 16); } if(iKeyId >= 0) { //keys priority switch(iKeyId) { case Keyboard::Key_LeftShift: case Keyboard::Key_RightShift: case Keyboard::Key_LeftCtrl: case Keyboard::Key_RightCtrl: case Keyboard::Key_LeftAlt: case Keyboard::Key_RightAlt: { bool bFound = false; for(int i = 0; i < Keyboard::KeyCount; i++) { if(bFound) { break; } switch(i & 0xFFFF) { case Keyboard::Key_LeftShift: case Keyboard::Key_RightShift: case Keyboard::Key_LeftCtrl: case Keyboard::Key_RightCtrl: case Keyboard::Key_LeftAlt: case Keyboard::Key_RightAlt: continue; default: { if(keysStates[i]) { bFound = true; iKeyId &= ~0xFFFF; iKeyId |= i; } break; } } } } } } const PlatformInstant now = g_platformTime.frameStart(); for(int buttonId = Mouse::ButtonBase; buttonId < Mouse::ButtonMax; buttonId++) { int i = buttonId - Mouse::ButtonBase; int iNumClick; int iNumUnClick; backend->getMouseButtonClickCount(buttonId, iNumClick, iNumUnClick); iOldNumClick[i] += iNumClick + iNumUnClick; if(!bMouseButton[i] && iOldNumClick[i] == iNumUnClick) { iOldNumClick[i] = 0; } bOldMouseButton[i] = bMouseButton[i]; if(bMouseButton[i]) { if(iOldNumClick[i]) { bMouseButton[i] = false; } } else { if(iOldNumClick[i]) { bMouseButton[i] = true; } } if(iOldNumClick[i]) { iOldNumClick[i]--; } if(iMouseTimeSet[i] > 1 || (iMouseTimeSet[i] == 1 && now - iMouseTime[i][0] > PlatformDurationMs(300))) { iMouseTime[i][0] = 0; iMouseTime[i][1] = 0; iMouseTimeSet[i] = 0; } if(getMouseButtonNowPressed(buttonId)) { switch(iMouseTimeSet[i]) { case 0: iMouseTime[i][0] = now; iMouseTimeSet[i]++; break; case 1: iMouseTime[i][1] = now; iMouseTimeSet[i]++; break; } } } // Get the new coordinates int absX, absY; mouseInWindow = backend->getAbsoluteMouseCoords(absX, absY); Vec2s newMousePosition(absX, absY); Vec2i wndSize = mainApp->getWindow()->getSize(); if(absX >= 0 && absX < wndSize.x && absY >= 0 && absY < wndSize.y) { // Use the absolute mouse position reported by the backend, as is if(m_mouseMode == Mouse::Absolute) { iMouseA = newMousePosition; } else { iMouseA = wndSize / s32(2); } } else { mouseInWindow = false; } int relX, relY; backend->getRelativeMouseCoords(relX, relY, iWheelDir); if(m_mouseMode == Mouse::Relative) { if(m_useRawMouseInput) { m_mouseMovement = Vec2f(relX * 2, relY * 2); } else { m_mouseMovement = Vec2f(newMousePosition - m_lastMousePosition); if(newMousePosition != m_lastMousePosition) { centerMouse(); } else { m_lastMousePosition = newMousePosition; } } // Use the sensitivity config value to adjust relative mouse mouvements m_mouseMovement *= m_mouseSensitivity; if(m_mouseAcceleration > 0 && time > 0.0f) { Vec2f speed = m_mouseMovement / (time * 0.14f); Vec2f sign(speed.x < 0 ? -1.f : 1.f, speed.y < 0 ? -1.f : 1.f); float exponent = 1.f + m_mouseAcceleration * 0.05f; speed.x = (std::pow(speed.x * sign.x + 1.f, exponent) - 1.f) * sign.x; speed.y = (std::pow(speed.y * sign.y + 1.f, exponent) - 1.f) * sign.y; m_mouseMovement = speed * (time * 0.14f); } if(m_invertMouseY) { m_mouseMovement.y *= -1.f; } if(!mouseInWindow) { LogWarning << "Cursor escaped the window while in relative input mode"; centerMouse(); } } else { m_mouseMovement = Vec2f_ZERO; if(!m_useRawMouseInput) { m_lastMousePosition = newMousePosition; } } }
/** * The central routing function for for all actions. Takes in a vector of * actions and the # of actions taken thus far. It then passes the current * action to be taken through tests to see which function to route to. * This function requires all parameters to for execute.cpp's delayAndMemCheck * function. */ void takeAction(const std::vector<nlohmann::json>& actions, int& actions_taken, std::string window_name, int childPID, float &elapsed, float& next_checkpoint, float seconds_to_run, int& rss_memory, int allowed_rss_memory, int& memory_kill, int& time_kill, std::ostream &logfile){ //We get the window data at every step in case it has changed size. //if we make it past this check, we'll assume an action has been taken. if(!windowExists(window_name)){ return; } nlohmann::json action = actions[actions_taken]; // std::vector<std::string> vec = stringOrArrayOfStrings(action, "action"); // if (vec.size() == 0){ // std::cout << "ERROR: poorly formatted action (no 'action' type specified)" << std::endl; // action_name = "INVALID: NAME MISSING"; // } // else{ // std::string action_name = vec[0]; // } std::string action_name = action.value("action", "ACTION_NOT_SPECIFIED"); std::cout <<"Taking action "<<actions_taken+1<<" of "<<actions.size() <<": "<< action_name<< std::endl; float delay_time = 0; bool success = false; //DELAY if(action_name == "delay"){ float time_in_secs = action.value("seconds", 0); delay_time = delay(time_in_secs); success = true; } //SCREENSHOT else if(action_name == "screenshot"){ std::string screenshot_name = action["name"]; success = screenshot(window_name, screenshot_name); } //TYPE else if(action_name == "type"){ float delay_in_secs = action["delay_in_seconds"]; int presses = action["presses"]; std::string string_to_type = action["string"]; success = type(string_to_type, delay_in_secs, presses, window_name,childPID,elapsed, next_checkpoint, seconds_to_run, rss_memory, allowed_rss_memory, memory_kill, time_kill, logfile); } //KEY else if(action_name == "key"){ std::string key_to_type = action["key_combination"]; success = key(key_to_type, window_name); } //CLICK AND DRAG else if(action_name == "click and drag"){ success = clickAndDragAbsolute(window_name,action); } else if(action_name == "click and drag delta"){ success = clickAndDragDelta(window_name,action); } //CLICK else if(action_name == "click"){ std::string mouse_button = action["mouse_button"]; if(mouse_button == "left"){ success = click(window_name, 1); } else if(mouse_button == "middle"){ success = click(window_name, 2); } else if(mouse_button == "right"){ success = click(window_name, 3); } else{ success = click(window_name, 1); } } //MOUSE MOVE else if(action_name == "move mouse"){ //TODO: implement later if deemed prudent. bool no_clamp = false; int moved_x = action["end_x"]; int moved_y = action["end_y"]; int height, width, x_start, x_end, y_start, y_end; bool populated = populateWindowData(window_name, height, width, x_start, x_end, y_start, y_end); if(populated){ moved_x += x_start; moved_y += y_start; success = mouse_move(window_name, moved_x, moved_y, x_start, x_end, y_start, y_end, no_clamp); } } //CENTER else if(action_name == "center"){ success = centerMouse(window_name); } //ORIGIN else if(action_name == "origin"){ success = moveMouseToOrigin(window_name); } else if(action_name == "gif"){ std::string gif_name = action["name"]; float duration_in_seconds = action["seconds"]; int fps = action["frames_per_second"]; bool save_pngs = action["preserve_individual_frames"]; success = make_gif(window_name, gif_name, duration_in_seconds, fps, save_pngs, childPID, elapsed, next_checkpoint, seconds_to_run, rss_memory, allowed_rss_memory, memory_kill, time_kill, logfile); } //BAD COMMAND else{ std::cout << "ERROR: ill formatted action: " << actions[actions_taken] << std::endl; } if(success){ std::cout << "The action was successful" << std::endl; actions_taken++; }else{ std::cout << "The action was unsuccessful" << std::endl; } delay_and_mem_check(delay_time, childPID, elapsed, next_checkpoint, seconds_to_run, rss_memory, allowed_rss_memory, memory_kill, time_kill,logfile); }
void Input::update() { backend->update(); bool keyJustPressed = false; iKeyId = -1; int modifier = 0; for(int i = 0; i < Keyboard::KeyCount; i++) { if(isKeyPressed(i)) { switch(i) { case Keyboard::Key_LeftShift: case Keyboard::Key_RightShift: case Keyboard::Key_LeftCtrl: case Keyboard::Key_RightCtrl: case Keyboard::Key_LeftAlt: case Keyboard::Key_RightAlt: modifier = i; break; } if(keysStates[i] < 2) { keysStates[i]++; } if(!keyJustPressed) { if(keysStates[i] == 1) { iKeyId = i; keyJustPressed = true; } else { iKeyId = i; } } } else { if(keysStates[i] > 0) { keysStates[i]--; } } } if(modifier != 0 && iKeyId != modifier) { iKeyId |= (modifier << 16); } if(iKeyId >= 0) { //keys priority switch(iKeyId) { case Keyboard::Key_LeftShift: case Keyboard::Key_RightShift: case Keyboard::Key_LeftCtrl: case Keyboard::Key_RightCtrl: case Keyboard::Key_LeftAlt: case Keyboard::Key_RightAlt: { bool bFound=false; for(int i = 0; i < Keyboard::KeyCount; i++) { if(bFound) { break; } switch(i & 0xFFFF) { case Keyboard::Key_LeftShift: case Keyboard::Key_RightShift: case Keyboard::Key_LeftCtrl: case Keyboard::Key_RightCtrl: case Keyboard::Key_LeftAlt: case Keyboard::Key_RightAlt: continue; default: { if(keysStates[i]) { bFound=true; iKeyId&=~0xFFFF; iKeyId|=i; } } break; } } } } } const s64 now = platform::getTimeMs(); for(int buttonId = Mouse::ButtonBase; buttonId < Mouse::ButtonMax; buttonId++) { int i = buttonId - Mouse::ButtonBase; int iNumClick; int iNumUnClick; backend->getMouseButtonClickCount(buttonId, iNumClick, iNumUnClick); iOldNumClick[i] += iNumClick + iNumUnClick; if(!bMouseButton[i] && iOldNumClick[i] == iNumUnClick) { iOldNumClick[i] = 0; } bOldMouseButton[i] = bMouseButton[i]; if(bMouseButton[i]) { if(iOldNumClick[i]) { bMouseButton[i] = false; } } else { if(iOldNumClick[i]) { bMouseButton[i] = true; } } if(iOldNumClick[i]) iOldNumClick[i]--; int iDTime; backend->isMouseButtonPressed(buttonId, iDTime); if(iDTime) { iMouseTime[i] = iDTime; iMouseTimeSet[i] = 2; } else { if(iMouseTimeSet[i] > 0 && (now - iMouseTime[i]) > 300) { iMouseTime[i] = 0; iMouseTimeSet[i] = 0; } if(getMouseButtonNowPressed(buttonId)) { switch(iMouseTimeSet[i]) { case 0: iMouseTime[i] = now; iMouseTimeSet[i]++; break; case 1: iMouseTime[i] = now - iMouseTime[i]; iMouseTimeSet[i]++; break; } } } } // Get the new coordinates int absX, absY; mouseInWindow = backend->getAbsoluteMouseCoords(absX, absY); Vec2s newMousePosition(absX, absY); Vec2i wndSize = mainApp->getWindow()->getSize(); if(absX >= 0 && absX < wndSize.x && absY >= 0 && absY < wndSize.y) { // Use the absolute mouse position reported by the backend, as is if(m_mouseMode == Mouse::Absolute) { iMouseA = newMousePosition; } else { iMouseA = wndSize / s32(2); } } else { mouseInWindow = false; } int relX, relY; backend->getRelativeMouseCoords(relX, relY, iWheelDir); if(m_mouseMode == Mouse::Relative) { if(m_useRawMouseInput) { iMouseR = Vec2s(relX * 2, relY * 2); } else { iMouseR = newMousePosition - m_lastMousePosition; m_lastMousePosition = newMousePosition; if(iMouseR != Vec2s_ZERO) { centerMouse(); } } // Use the sensitivity config value to adjust relative mouse mouvements float fSensMax = 1.f / 6.f; float fSensMin = 2.f; float fSens = ( ( fSensMax - fSensMin ) * ( (float)iSensibility ) / 10.f ) + fSensMin; fSens = std::pow(0.7f, fSens) * 2.f; iMouseR *= fSens; if(!mouseInWindow) { LogWarning << "Cursor escaped the window while in relative input mode"; centerMouse(); } } else { iMouseR = Vec2s_ZERO; m_lastMousePosition = newMousePosition; } }
void InputManager::windowSizeChanged(int width, int height){ windowWidth = width; windowHeight = height; centerMouse(); }