zoom out further

This commit is contained in:
Crispy 2024-12-10 22:18:09 +01:00
parent 092a7b70ff
commit 9d54c17dcd
4 changed files with 33 additions and 33 deletions

View file

@ -21,17 +21,17 @@ use crate::{
solution::{Score, Solution},
text_input, texture_option_button,
theme::*,
userdata_dir, Scroll, Textures,
userdata_dir, Scroll, Textures, TILE_TEXTURE_SIZE,
};
const TILE_TEXTURE_SIZE: usize = 16;
const HEADER_HEIGHT: i32 = 40;
const FOOTER_HEIGHT: i32 = 95;
const SIDEBAR_WIDTH: i32 = 200 + 32 * 2 + 5 * 4;
const POPUP_WIDTH: i32 = 320;
const POPUP_HEIGHT: i32 = 165;
const MAX_ZOOM_IN: i32 = 3;
const MAX_ZOOM: f32 = 8.;
const MIN_ZOOM: f32 = 0.25;
const BOARD_MARGIN: PosInt = 3;
const MAX_SPEED_POWER: u8 = 16;
const SPEED_DIGITS: u8 = 5;
@ -44,7 +44,7 @@ pub struct Editor {
machine: Machine,
sim_state: SimState,
view_offset: Vector2,
zoom: i32,
zoom: f32,
output_as_text: bool,
input_as_text: bool,
active_tool: Tool,
@ -123,7 +123,7 @@ impl Editor {
machine: Machine::new_empty(level.inputs().to_owned(), 1),
sim_state: SimState::Editing,
view_offset: Vector2::zero(),
zoom: 1,
zoom: 1.,
active_tool: Tool::None,
output_as_text: level.output_is_text(),
input_as_text: level.input_is_text(),
@ -170,7 +170,7 @@ impl Editor {
}
fn pos_to_screen(&self, pos: Vector2) -> Vector2 {
pos * (TILE_TEXTURE_SIZE << self.zoom) as f32 + self.view_offset
pos * (TILE_TEXTURE_SIZE * self.zoom) as f32 + self.view_offset
}
fn start_sim(&mut self) {
@ -238,7 +238,7 @@ impl Editor {
}
pub fn center_view(&mut self, d: &RaylibHandle) {
let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32;
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32;
let tile_x = self.source_board.width() as f32 / 2. * tile_size;
let tile_y = self.source_board.height() as f32 / 2. * tile_size;
let screen_x = d.get_screen_width() as f32 / 2.;
@ -247,26 +247,26 @@ impl Editor {
self.view_offset.y = (screen_y - tile_y).floor();
}
fn change_zoom_level(&mut self, d: &RaylibHandle, delta: i32) {
let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32;
fn change_zoom_level(&mut self, d: &RaylibHandle, delta: f32) {
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32;
let mouse_pos = d.get_mouse_position();
let tile_pos_of_mouse = (mouse_pos - self.view_offset) / tile_size;
self.zoom += delta;
let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32;
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32;
self.view_offset = mouse_pos - tile_pos_of_mouse * tile_size;
self.view_offset.x = self.view_offset.x.floor();
self.view_offset.y = self.view_offset.y.floor();
}
fn zoom_in(&mut self, d: &RaylibHandle) {
if self.zoom < MAX_ZOOM_IN {
self.change_zoom_level(d, 1);
if self.zoom < MAX_ZOOM {
self.change_zoom_level(d, self.zoom);
}
}
fn zoom_out(&mut self, d: &RaylibHandle) {
if self.zoom > 0 {
self.change_zoom_level(d, -1);
if self.zoom > MIN_ZOOM {
self.change_zoom_level(d, self.zoom / -2.);
}
}
@ -292,7 +292,7 @@ impl Editor {
}
fn grow_board_and_update_view(&mut self, pos: &mut Pos) {
let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32;
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32;
let (x, y) = self.source_board.grow_to_include(*pos);
if x != 0 || y != 0 {
self.view_offset.x -= x as f32 * tile_size;
@ -319,7 +319,7 @@ impl Editor {
}
fn set_tile(&mut self, mut pos: Pos, tile: Tile) {
let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32;
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32;
pos.x -= BOARD_MARGIN;
pos.y -= BOARD_MARGIN;
self.grow_board_and_update_view(&mut pos);
@ -446,8 +446,8 @@ impl Editor {
pub fn draw(&mut self, d: &mut RaylibDrawHandle, textures: &Textures) {
d.clear_background(Color::new(64, 64, 64, 255));
if self.draw_overlay {
let tile_size = (TILE_TEXTURE_SIZE << self.zoom) as f32;
if self.draw_overlay && self.zoom >= 1. {
let tile_size = (TILE_TEXTURE_SIZE * self.zoom) as f32;
let grid_spill_x = (self.view_offset.x).rem(tile_size) - tile_size;
let grid_spill_y = (self.view_offset.y).rem(tile_size) - tile_size;
d.gui_grid(
@ -900,8 +900,7 @@ impl Editor {
let footer_top = (d.get_screen_height() - FOOTER_HEIGHT) as f32;
let mouse_pos = d.get_mouse_position();
let scroll_delta = d.get_mouse_wheel_move();
let tile_scale = (1 << self.zoom) as f32;
let tile_size = TILE_TEXTURE_SIZE << self.zoom;
let tile_size = TILE_TEXTURE_SIZE * self.zoom;
if self.sim_state == SimState::Editing {
if let Some(board) = &self.pasting_board {
if d.is_key_pressed(KeyboardKey::KEY_ESCAPE) {
@ -939,7 +938,7 @@ impl Editor {
textures.get("selection"),
tile_screen_pos,
0.,
tile_scale,
self.zoom,
Color::new(255, 180, 20, 255),
);
if d.is_key_pressed(KeyboardKey::KEY_LEFT) {
@ -992,7 +991,7 @@ impl Editor {
textures.get(&tex),
tile_screen_pos,
0.,
tile_scale,
self.zoom,
Color::new(255, 255, 255, 100),
);
}
@ -1090,13 +1089,13 @@ impl Editor {
let p_min = self.pos_to_screen(min.to_vec());
let p_max = self.pos_to_screen(max.to_vec());
let tex = textures.get("area_corner");
d.draw_texture_ex(tex, p_min, 0., tile_scale, Color::WHITE);
d.draw_texture_ex(tex, p_min, 0., self.zoom, Color::WHITE);
let one_xy = Vector2::new(tile_size as f32, tile_size as f32);
d.draw_texture_ex(tex, p_max + one_xy, 180., tile_scale, Color::WHITE);
d.draw_texture_ex(tex, p_max + one_xy, 180., self.zoom, Color::WHITE);
let top_right = Vector2::new(p_max.x + tile_size as f32, p_min.y);
d.draw_texture_ex(tex, top_right, 90., tile_scale, Color::WHITE);
d.draw_texture_ex(tex, top_right, 90., self.zoom, Color::WHITE);
let bot_left = Vector2::new(p_min.x, p_max.y + tile_size as f32);
d.draw_texture_ex(tex, bot_left, -90., tile_scale, Color::WHITE);
d.draw_texture_ex(tex, bot_left, -90., self.zoom, Color::WHITE);
}
}
}

View file

@ -21,6 +21,8 @@ use util::*;
const TITLE_TEXT: &str = concat!("Marble Machinations v", env!("CARGO_PKG_VERSION"));
pub const TILE_TEXTURE_SIZE: f32 = 16.0;
struct Game {
levels: Vec<Level>,
level_scroll: usize,

View file

@ -7,7 +7,7 @@ use board::Board;
use pos::*;
use tile::*;
use crate::{draw_usize_small, Textures};
use crate::{draw_usize_small, Textures, TILE_TEXTURE_SIZE};
#[derive(Debug)]
pub struct Machine {
@ -76,10 +76,9 @@ impl Machine {
d: &mut RaylibDrawHandle,
textures: &Textures,
offset: Vector2,
zoom: i32,
scale: f32,
) {
let tile_size = 16 << zoom;
let scale = (1 << zoom) as f32;
let tile_size = (TILE_TEXTURE_SIZE * scale) as i32;
for marble in &self.marbles {
let x = marble.x;
let y = marble.y;

View file

@ -1,3 +1,4 @@
use crate::TILE_TEXTURE_SIZE;
use crate::{draw_scaled_texture, Textures};
use super::tile::*;
@ -245,9 +246,8 @@ impl Board {
out
}
pub fn draw(&self, d: &mut RaylibDrawHandle, textures: &Textures, offset: Vector2, zoom: i32) {
let tile_size = 16 << zoom;
let scale = (1 << zoom) as f32;
pub fn draw(&self, d: &mut RaylibDrawHandle, textures: &Textures, offset: Vector2, scale: f32) {
let tile_size = (TILE_TEXTURE_SIZE * scale) as i32;
let start_x = (-offset.x as i32) / tile_size - 1;
let tile_width = d.get_screen_width() / tile_size + 2;