/* * Process mouse and keyboard events. */ static void process_event(SDL_Event* event){ if(event->type == SDL_MOUSEBUTTONDOWN){ SDL_CaptureMouse(SDL_TRUE); }else if(event->type == SDL_MOUSEBUTTONUP){ SDL_CaptureMouse(SDL_FALSE); }else if(event->type == SDL_MOUSEMOTION){ if(event->motion.state & SDL_BUTTON_LMASK){ int diff_y = -event->motion.yrel; int diff_x = event->motion.xrel; float aspect_ratio = (float)window_width/(float)window_height; camera_phi += (float)diff_y / (float)window_height * 90.0f; camera_phi = clamp(camera_phi, -90.0f, 90.0f); camera_theta += (float)-diff_x / (float)window_width * aspect_ratio * 90.0f; } }else if(event->type == SDL_MOUSEWHEEL ){ camera_offset += -event->wheel.y; camera_offset = max(camera_offset, 4.0f); }else if(event->type == SDL_KEYDOWN){ int key = event->key.keysym.sym; if(key == SDLK_ESCAPE){ quit(); } }else if(event->type == SDL_WINDOWEVENT){ if(event->window.event == SDL_WINDOWEVENT_RESIZED){ int new_width = event->window.data1; int new_height = event->window.data2; resize(new_width, new_height); } }else if(event->type == SDL_QUIT){ quit(); } }
/* * Process mouse and keyboard events. */ static void process_event (SDL_Event *event){ if(event->type == SDL_MOUSEBUTTONDOWN){ SDL_CaptureMouse(SDL_TRUE); }else if(event->type == SDL_MOUSEBUTTONUP){ SDL_CaptureMouse(SDL_FALSE); }else if(event->type == SDL_MOUSEMOTION){ int y = window_height -1 - event->motion.y; int x = event->motion.x; if(event->motion.state & SDL_BUTTON_LMASK){ int diff_y = -event->motion.yrel; int diff_x = event->motion.xrel; float aspect_ratio = (float)window_width/(float)window_height; camera_phi += (float)diff_y / (float)window_height * 90.0f; camera_phi = clamp(camera_phi, -90.0f, 90.0f); camera_theta += (float)-diff_x / (float)window_width * aspect_ratio * 90.0f; } }else if(event->type == SDL_MOUSEWHEEL ){ camera_offset += -event->wheel.y; camera_offset = max(camera_offset, 5.0f); }else if(event->type == SDL_KEYDOWN){ int key = event->key.keysym.sym; switch(key){ case SDLK_w: if(wireframe_flag == wireframe){ er_polygon_mode(ER_FRONT_AND_BACK, ER_FILL); wireframe_flag = solid; }else{ er_polygon_mode(ER_FRONT_AND_BACK, ER_LINE); wireframe_flag = wireframe; } break; case SDLK_l: if(light_enable == ER_TRUE){ current_surface_program = program_surface_color; light_enable = ER_FALSE; }else{ current_surface_program = program_surface_light; light_enable = ER_TRUE; } break; case SDLK_ESCAPE: quit(); } }else if(event->type == SDL_WINDOWEVENT){ if(event->window.event == SDL_WINDOWEVENT_RESIZED){ int new_width = event->window.data1; int new_height = event->window.data2; resize(new_width, new_height); } }else if(event->type == SDL_QUIT){ quit(); } }
void SDLApplication::ProcessMouseEvent (SDL_Event* event) { if (MouseEvent::callback) { switch (event->type) { case SDL_MOUSEMOTION: mouseEvent.type = MOUSE_MOVE; mouseEvent.x = event->motion.x; mouseEvent.y = event->motion.y; mouseEvent.movementX = event->motion.xrel; mouseEvent.movementY = event->motion.yrel; break; case SDL_MOUSEBUTTONDOWN: SDL_CaptureMouse (SDL_TRUE); mouseEvent.type = MOUSE_DOWN; mouseEvent.button = event->button.button - 1; mouseEvent.x = event->button.x; mouseEvent.y = event->button.y; break; case SDL_MOUSEBUTTONUP: SDL_CaptureMouse (SDL_FALSE); mouseEvent.type = MOUSE_UP; mouseEvent.button = event->button.button - 1; mouseEvent.x = event->button.x; mouseEvent.y = event->button.y; break; case SDL_MOUSEWHEEL: mouseEvent.type = MOUSE_WHEEL; mouseEvent.x = event->wheel.x; mouseEvent.y = event->wheel.y; break; } mouseEvent.windowID = event->button.windowID; MouseEvent::Dispatch (&mouseEvent); } }
void SDL_MouseQuit(void) { SDL_Cursor *cursor, *next; SDL_Mouse *mouse = SDL_GetMouse(); if (mouse->CaptureMouse) { SDL_CaptureMouse(SDL_FALSE); } SDL_SetRelativeMouseMode(SDL_FALSE); SDL_ShowCursor(1); cursor = mouse->cursors; while (cursor) { next = cursor->next; SDL_FreeCursor(cursor); cursor = next; } if (mouse->def_cursor && mouse->FreeCursor) { mouse->FreeCursor(mouse->def_cursor); } if (mouse->clickstate) { SDL_free(mouse->clickstate); } SDL_zerop(mouse); }
void SDL_MouseQuit(void) { SDL_Cursor *cursor, *next; SDL_Mouse *mouse = SDL_GetMouse(); if (mouse->CaptureMouse) { SDL_CaptureMouse(SDL_FALSE); } SDL_SetRelativeMouseMode(SDL_FALSE); SDL_ShowCursor(1); cursor = mouse->cursors; while (cursor) { next = cursor->next; SDL_FreeCursor(cursor); cursor = next; } if (mouse->def_cursor && mouse->FreeCursor) { mouse->FreeCursor(mouse->def_cursor); } if (mouse->clickstate) { SDL_free(mouse->clickstate); } SDL_zerop(mouse); SDL_DelHintCallback(SDL_HINT_MOUSE_NORMAL_SPEED_SCALE, SDL_MouseNormalSpeedScaleChanged, mouse); SDL_DelHintCallback(SDL_HINT_MOUSE_RELATIVE_SPEED_SCALE, SDL_MouseRelativeSpeedScaleChanged, mouse); }
void esGame_captureMouse(int on) { SDL_bool b = on ? SDL_TRUE : SDL_FALSE; SDL_SetRelativeMouseMode(b); SDL_CaptureMouse(b); if (b == SDL_FALSE) { SDL_WarpMouseInWindow(window, 0, 0); blockNextMouse = 1; } }
void sdl_window::release_capture() { if (iCapturingMouse) { iCapturingMouse = false; SDL_CaptureMouse(SDL_FALSE); #ifdef WIN32 ReleaseCapture(); #endif } }
void sdl_window::set_capture() { if (!iCapturingMouse) { iCapturingMouse = true; SDL_CaptureMouse(SDL_TRUE); #ifdef WIN32 SetCapture(static_cast<HWND>(native_handle())); #endif } }
static void ImGui_ImplSDL2_UpdateMousePosAndButtons() { ImGuiIO& io = ImGui::GetIO(); const ImVec2 mouse_pos_backup = io.MousePos; io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); // Set OS mouse position if requested (rarely used, only when ImGuiConfigFlags_NavEnableSetMousePos is enabled by user) // (When multi-viewports are enabled, all imgui positions are same as OS positions.) #if SDL_HAS_WARP_MOUSE_GLOBAL if (io.WantSetMousePos) SDL_WarpMouseGlobal((int)mouse_pos_backup.x, (int)mouse_pos_backup.y); #endif int mx, my; Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my); io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 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. io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0; io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0; g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false; #if SDL_HAS_CAPTURE_MOUSE SDL_Window* focused_window = SDL_GetKeyboardFocus(); if (g_Window == focused_window) { // SDL_GetMouseState() gives mouse position seemingly based on the last window entered/focused(?) // The creation of a new windows at runtime and SDL_CaptureMouse both seems to severely mess up with that, so we retrieve that position globally. int wx, wy; SDL_GetWindowPosition(focused_window, &wx, &wy); SDL_GetGlobalMouseState(&mx, &my); mx -= wx; my -= wy; io.MousePos = ImVec2((float)mx, (float)my); } // SDL_CaptureMouse() let the OS know e.g. that our imgui drag outside the SDL window boundaries shouldn't e.g. trigger the OS window resize cursor. // The function is only supported from SDL 2.0.4 (released Jan 2016) bool any_mouse_button_down = ImGui::IsAnyMouseDown(); SDL_CaptureMouse(any_mouse_button_down ? SDL_TRUE : SDL_FALSE); #else if (SDL_GetWindowFlags(g_Window) & SDL_WINDOW_INPUT_FOCUS) io.MousePos = ImVec2((float)mx, (float)my); #endif }
void ImGui_ImplSdlGL2_NewFrame(SDL_Window *window) { if (!g_FontTexture) ImGui_ImplSdlGL2_CreateDeviceObjects(); ImGuiIO& io = ImGui::GetIO(); // Setup display size (every frame to accommodate for window resizing) int w, h; int display_w, display_h; SDL_GetWindowSize(window, &w, &h); SDL_GL_GetDrawableSize(window, &display_w, &display_h); io.DisplaySize = ImVec2((float)w, (float)h); io.DisplayFramebufferScale = ImVec2(w > 0 ? ((float)display_w / w) : 0, h > 0 ? ((float)display_h / h) : 0); // Setup time step (we don't use SDL_GetTicks() because it is using millisecond resolution) static Uint64 frequency = SDL_GetPerformanceFrequency(); Uint64 current_time = SDL_GetPerformanceCounter(); io.DeltaTime = g_Time > 0 ? (float)((double)(current_time - g_Time) / frequency) : (float)(1.0f / 60.0f); g_Time = current_time; // Setup mouse inputs (we already got mouse wheel, keyboard keys & characters from our event handler) int mx, my; Uint32 mouse_buttons = SDL_GetMouseState(&mx, &my); io.MousePos = ImVec2(-FLT_MAX, -FLT_MAX); io.MouseDown[0] = g_MousePressed[0] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_LEFT)) != 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. io.MouseDown[1] = g_MousePressed[1] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_RIGHT)) != 0; io.MouseDown[2] = g_MousePressed[2] || (mouse_buttons & SDL_BUTTON(SDL_BUTTON_MIDDLE)) != 0; g_MousePressed[0] = g_MousePressed[1] = g_MousePressed[2] = false; // We need to use SDL_CaptureMouse() to easily retrieve mouse coordinates outside of the client area. This is only supported from SDL 2.0.4 (released Jan 2016) #if (SDL_MAJOR_VERSION >= 2) && (SDL_MINOR_VERSION >= 0) && (SDL_PATCHLEVEL >= 4) if ((SDL_GetWindowFlags(window) & (SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_MOUSE_CAPTURE)) != 0) io.MousePos = ImVec2((float)mx, (float)my); bool any_mouse_button_down = false; for (int n = 0; n < IM_ARRAYSIZE(io.MouseDown); n++) any_mouse_button_down |= io.MouseDown[n]; if (any_mouse_button_down && (SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_CAPTURE) == 0) SDL_CaptureMouse(SDL_TRUE); if (!any_mouse_button_down && (SDL_GetWindowFlags(window) & SDL_WINDOW_MOUSE_CAPTURE) != 0) SDL_CaptureMouse(SDL_FALSE); #else if ((SDL_GetWindowFlags(window) & SDL_WINDOW_INPUT_FOCUS) != 0) io.MousePos = ImVec2((float)mx, (float)my); #endif // Update OS/hardware mouse cursor if imgui isn't drawing a software cursor ImGuiMouseCursor cursor = ImGui::GetMouseCursor(); if (io.MouseDrawCursor || cursor == ImGuiMouseCursor_None) { SDL_ShowCursor(0); } else { SDL_SetCursor(g_MouseCursors[cursor] ? g_MouseCursors[cursor] : g_MouseCursors[ImGuiMouseCursor_Arrow]); SDL_ShowCursor(1); } // Start the frame. This call will update the io.WantCaptureMouse, io.WantCaptureKeyboard flag that you can use to dispatch inputs (or not) to your application. ImGui::NewFrame(); }