Skip to content

Commit 3822b44

Browse files
committed
OSK: simplify coord conv, for older SDL too #435
This change greatly simplify mouse cooridnate conversion needed for correct positioning on the OSK, also it eliminates the need for SDL 2.0.18+ which was a problem in my last commit, as the build environment uses older SDL2 on Mac and Windows builds. Double win :D (Side note: though the build environment should be updated ...)
1 parent 0a87e9f commit 3822b44

File tree

1 file changed

+4
-54
lines changed

1 file changed

+4
-54
lines changed

xemu/emutools_osk.c

Lines changed: 4 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -119,60 +119,10 @@ static void redraw_kbd ( void )
119119

120120
static int get_key_at_mouse ( int x, int y )
121121
{
122-
int mouse_x, mouse_y;
123-
SDL_RenderLogicalToWindow(sdl_ren, x, y, &mouse_x, &mouse_y);
124-
int output_w, output_h;
125-
SDL_GetRendererOutputSize(sdl_ren, &output_w, &output_h);
126-
int logic_w = 0, logic_h = 0;
127-
SDL_RenderGetLogicalSize(sdl_ren, &logic_w, &logic_h);
128-
// If no logical size was set, fallback to output size
129-
if (!logic_w || !logic_h) {
130-
logic_w = output_w;
131-
logic_h = output_h;
132-
}
133-
// Compute the SDL letterbox/pillarbox viewport
134-
SDL_Rect viewport;
135-
const float window_aspect = (float)output_w / output_h;
136-
const float logic_aspect = (float)logic_w / logic_h;
137-
if (window_aspect > logic_aspect) {
138-
// Letterboxing
139-
viewport.h = output_h;
140-
viewport.w = (int)(output_h * logic_aspect);
141-
viewport.x = (output_w - viewport.w) / 2;
142-
viewport.y = 0;
143-
} else {
144-
// Pillarboxing
145-
viewport.w = output_w;
146-
viewport.h = (int)(output_w / logic_aspect);
147-
viewport.x = 0;
148-
viewport.y = (output_h - viewport.h) / 2;
149-
}
150-
// Only map coordinates within the viewport area
151-
// OSK is rendered inside the same area as the main logical rendering
152-
int rel_x = mouse_x - viewport.x;
153-
int rel_y = mouse_y - viewport.y;
154-
// Clamp to viewport in case mouse is in black bars
155-
if (rel_x < 0) rel_x = 0;
156-
if (rel_y < 0) rel_y = 0;
157-
if (rel_x > viewport.w) rel_x = viewport.w;
158-
if (rel_y > viewport.h) rel_y = viewport.h;
159-
// Finally map the real window-space mouse position to the OSK texture
160-
// (assuming OSK is rendered full-window)
161-
x = size_x * rel_x / viewport.w;
162-
y = size_y * rel_y / viewport.h;
163-
#if 0
164-
int win_x, win_y;
165-
SDL_RenderLogicalToWindow(sdl_ren, x, y, &win_x, &win_y);
166-
int out_w, out_h;
167-
SDL_GetRendererOutputSize(sdl_ren, &out_w, &out_h);
168-
x = size_x * win_x / out_w;
169-
y = size_y * win_y / out_h;
170-
#endif
171-
#if 0
172-
char debug[100];
173-
sprintf(debug, "Texture: %04d %04d", x, y);
174-
draw_text(debug, strlen(debug), 0, 80, true);
175-
#endif
122+
// Convert coordinates
123+
x = size_x * x / sdl_default_win_x_size;
124+
y = size_y * y / sdl_default_win_y_size;
125+
// Search for matching button - if any
176126
for (int i = 0; i < all_keys; i++)
177127
if (x > keys[i].x1 && x < keys[i].x2 && y > keys[i].y1 && y < keys[i].y2)
178128
return i;

0 commit comments

Comments
 (0)