Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix viewport #9

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,37 @@ impl Editor {
)));
}

/// Returns the aspect ratios for width & height.
fn aspect_ratio(canvas: &egui::Rect) -> (f32, f32) {
if canvas.width() > canvas.height() {
(canvas.width() / canvas.height(), 1.0)
} else {
(1.0, canvas.height() / canvas.width())
}
}

pub fn set_cam(&mut self) {
let canvas = self
.canvas
.expect("expect define_egui() to be called before");
let map = &self.gen.map;
let display_factor = self.get_display_factor(map);
let x_view = display_factor * map.width as f32;
let y_view = display_factor * map.height as f32;
let y_shift = screen_height() - y_view;
let map_rect = Rect::new(0.0, 0.0, map.width as f32, map.height as f32);
let (scale_w, scale_h) = Self::aspect_ratio(&canvas);
let size = map.width.max(map.height);
let x_view = scale_w * size as f32;
let y_view = scale_h * size as f32;
let map_rect = Rect::new(0.0, 0.0, x_view, y_view);
let mut cam = Camera2D::from_display_rect(map_rect);

// so i guess this is (x, y, width, height) not two positions?
cam.viewport = Some((0, y_shift as i32, x_view as i32, y_view as i32));
// OpenGL viewports start at the bottom
// (https://www.saschawillems.de/images/2019-03-29-vulkan-flipping-the-viewport/viewports_gl_vk.png)
let y_shift = screen_height() - canvas.max.y;
// use the remaining canvas as viewport
cam.viewport = Some((
canvas.min.x as i32,
y_shift as i32,
canvas.width() as i32,
canvas.height() as i32,
));

cam.target -= self.offset;
cam.zoom *= self.zoom;
Expand Down
Loading