Skip to content

Commit ffb3cbe

Browse files
committed
Fix arbitrary scaling
1 parent 3f59f8a commit ffb3cbe

File tree

2 files changed

+36
-14
lines changed

2 files changed

+36
-14
lines changed

src/rust/bitbox02-sys/wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
#include <ui/screen_stack.h>
5151
#include <ui/ugui/ugui.h>
5252
#include <util.h>
53-
#include <workflow/orientation_screen.h>
5453

5554
#if defined(TESTING)
5655
#include <fake_memory.h>
@@ -60,6 +59,7 @@
6059
#include <ui/event_handler.h>
6160
#include <usb/usb_packet.h>
6261
#include <usb/usb_processing.h>
62+
#include <workflow/orientation_screen.h>
6363
#endif
6464

6565
#if !defined(TESTING)

test/simulator-ng/src/main.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@ impl App {
287287
let bg_id = canvas
288288
.create_image(
289289
ImageSource::try_from(&bg_orig).unwrap(),
290-
ImageFlags::empty(),
290+
ImageFlags::NEAREST,
291291
)
292292
.unwrap();
293293
let screen_id = canvas
294294
.create_image(
295295
ImageSource::try_from(&*SCREEN_FB.lock().unwrap()).unwrap(),
296-
ImageFlags::empty(),
296+
ImageFlags::NEAREST,
297297
)
298298
.unwrap();
299299

@@ -337,12 +337,12 @@ impl ApplicationHandler<UserEvent> for App {
337337
let window_size = window.inner_size();
338338
canvas.set_size(window_size.width, window_size.height, dpi_factor);
339339

340+
let margin = (MARGIN as f32 * dpi_factor) as u32;
341+
340342
let bitbox_x = MARGIN as f32 * dpi_factor;
341343
let bitbox_y = MARGIN as f32 * dpi_factor;
342-
let bitbox_width =
343-
(SCREEN_WIDTH + PADDING_LEFT + PADDING_RIGHT) as f32 * dpi_factor;
344-
let bitbox_height =
345-
(SCREEN_HEIGHT + 2 * PADDING_TOP_BOTTOM) as f32 * dpi_factor;
344+
let bitbox_width = (window_size.width - 2 * margin) as f32;
345+
let bitbox_height = (window_size.height - 2 * margin) as f32;
346346

347347
let mut bitbox_path = Path::new();
348348
bitbox_path.rect(bitbox_x, bitbox_y, bitbox_width, bitbox_height);
@@ -359,11 +359,19 @@ impl ApplicationHandler<UserEvent> for App {
359359
1f32,
360360
),
361361
);
362-
363-
let screen_x = (MARGIN + PADDING_LEFT) as f32 * dpi_factor;
364-
let screen_y = (MARGIN + PADDING_TOP_BOTTOM) as f32 * dpi_factor;
365-
let screen_width = SCREEN_WIDTH as f32 * dpi_factor;
366-
let screen_height = SCREEN_HEIGHT as f32 * dpi_factor;
362+
let bitbox_width_scale_factor = (window_size.width - 2 * margin) as f32
363+
/ (PADDING_LEFT + SCREEN_WIDTH + PADDING_RIGHT) as f32;
364+
365+
let bitbox_height_scale_factor = (window_size.height - 2 * margin) as f32
366+
/ (2 * PADDING_TOP_BOTTOM + SCREEN_HEIGHT) as f32;
367+
368+
let screen_x =
369+
(margin as f32 + PADDING_LEFT as f32 * bitbox_width_scale_factor) as f32;
370+
let screen_y = (margin as f32
371+
+ PADDING_TOP_BOTTOM as f32 * bitbox_height_scale_factor)
372+
as f32;
373+
let screen_width = SCREEN_WIDTH as f32 * bitbox_width_scale_factor;
374+
let screen_height = SCREEN_HEIGHT as f32 * bitbox_height_scale_factor;
367375
let mut screen_path = Path::new();
368376
screen_path.rect(screen_x, screen_y, screen_width, screen_height);
369377
canvas.fill_path(
@@ -395,11 +403,25 @@ impl ApplicationHandler<UserEvent> for App {
395403
}
396404
}
397405
WindowEvent::CursorMoved { position, .. } => {
398-
debug!("{position:?}");
406+
//debug!("{position:?}");
399407
let Some(window) = &mut self.window else {
400408
return;
401409
};
402-
let (x, y): (i32, i32) = position.to_logical::<f64>(window.scale_factor()).into();
410+
//let (x, y): (i32, i32) = position.to_logical::<f64>(window.scale_factor()).into();
411+
let (x, y) = (position.x, position.y);
412+
let window_size = window.inner_size();
413+
let dpi_factor = window.scale_factor() as f32;
414+
//let margin = (MARGIN as f32 * dpi_factor) as u32;
415+
let width_scale_factor = (window_size.width) as f32
416+
/ (2 * MARGIN + PADDING_LEFT + SCREEN_WIDTH + PADDING_RIGHT) as f32;
417+
418+
let height_scale_factor = (window_size.height) as f32
419+
/ (2 * MARGIN + 2 * PADDING_TOP_BOTTOM + SCREEN_HEIGHT) as f32;
420+
421+
let (x, y) = (
422+
(x as f32 / width_scale_factor) as i32,
423+
(y as f32 / height_scale_factor) as i32,
424+
);
403425
let xrel = x - self.mouse_last_x;
404426
let yrel = y - self.mouse_last_y;
405427
// Ignore if mouse didn't move long enough

0 commit comments

Comments
 (0)