~comcloudway/little_town

5181da62dd51e4b599909514efbd7cf2cc20350d — Jakob Meier 1 year, 8 months ago 6be2537
Made mobile experience better,
by replacing the mouse events
3 files changed, 81 insertions(+), 71 deletions(-)

M src/screens/build.rs
M src/screens/inventory.rs
M src/screens/welcome.rs
M src/screens/build.rs => src/screens/build.rs +66 -24
@@ 43,8 43,6 @@ const TEXTURE_DEPTH: f32 = 100.0;
pub struct BuildScreen {
    grid: HashMap<Pos3, (Block, Direction)>,
    cam: Camera,
    mouse_left_down: bool,
    mouse_right_down: bool,
    show_inv: bool,
    inv: Inventory
}


@@ 53,13 51,11 @@ impl BuildScreen {
        let mut this = Self {
            grid: HashMap::new(),
            cam: Camera::new(),
            mouse_left_down: false,
            mouse_right_down: false,
            show_inv: false,
            inv: Inventory::new()
        };

        this.grid.insert(Pos3::new(0, 0, 0), (Block::Dirt, Direction::North));
        this.grid.insert(Pos3::new(0, 0, 0), (Block::default(), Direction::North));

        this
    }


@@ 191,6 187,53 @@ impl GameComponent for BuildScreen {
                        });
                }
        }

        // TODO: Draw selected item (top right)
        {
            let smallest = if screen_width() < screen_height() {
                screen_width()
            } else {
                screen_height()
            };
            // use 70% of screen width for slots
            let width = smallest * 0.08;
            // only use 90% of slot width for the actual item
            // ensures enough space around item
            let slot_render_dim = width * 0.9;

            let tx = screen_width() - width;
            let ty = width - slot_render_dim;

            // draw background
            draw_texture_ex(
                assets.ui.panel_brown.0,
                tx,
                ty,
                WHITE,
                DrawTextureParams {
                    dest_size: Some(Vec2::new(slot_render_dim, slot_render_dim)),
                    ..Default::default()
                }
            );

            if let Some(block) = &self.inv.selected {
                // draw block
                draw_texture_ex(
                    block.get_texture(&assets).get_dir(&self.inv.direction),
                    tx + slot_render_dim / 6.0,
                    ty + width / 2.0
                        - (slot_render_dim * 2.0/3.0 * TEXTURE_HEIGHT / TEXTURE_WIDTH) / 2.0
                        - TEXTURE_DEPTH * 0.08 * 0.9,
                    WHITE,
                    DrawTextureParams {
                        dest_size: Some(Vec2::new(
                            slot_render_dim * 2.0/3.0,
                            slot_render_dim * 2.0/3.0 * TEXTURE_HEIGHT / TEXTURE_WIDTH)),
                        ..Default::default()
                    }
                );
            }
        }
    }
    fn ev_loop(&mut self) -> GameEvent {



@@ 206,17 249,8 @@ impl GameComponent for BuildScreen {
            }
        }


        // mouse input
        if is_mouse_button_down(MouseButton::Left) {
            self.mouse_left_down = true;
        }
        if is_mouse_button_down(MouseButton::Right) {
            self.mouse_right_down = true;
        }

        if (self.mouse_left_down && !is_mouse_button_down(MouseButton::Left))
            || (self.mouse_right_down && !is_mouse_button_down(MouseButton::Right)) {
        if is_mouse_button_pressed(MouseButton::Left)
            || is_mouse_button_pressed(MouseButton::Right) {

                // determine which block / side was clicked
                let (mx, my) = mouse_position();


@@ 254,10 288,7 @@ impl GameComponent for BuildScreen {
                    // for a given pixel
                    let mut pos = Pos3::new(pos.x, pos.y, pos.z);

                    if self.mouse_right_down && !is_mouse_button_down(MouseButton::Right) {
                        // released right click
                        self.mouse_right_down = false;

                    if is_mouse_button_pressed(MouseButton::Right) {
                        // if grid has only one block
                        // the block can not be removed
                        // because the palyer would not be able to place


@@ 271,10 302,7 @@ impl GameComponent for BuildScreen {
                        }
                    }

                    if self.mouse_left_down && !is_mouse_button_down(MouseButton::Left) {
                        // released left click
                        self.mouse_left_down = false;

                    if is_mouse_button_pressed(MouseButton::Left) {
                        // place block
                        let (x,y) = get_screen_coords(&pos, self.cam.scale, self.cam.center);
                        let face = Face::from_xy(mx-x, my-y, self.cam.scale);


@@ 324,6 352,20 @@ impl GameComponent for BuildScreen {
            self.cam.center.x += 1.0 * (1.0/self.cam.scale);
        }

        // change block direction
        if is_key_pressed(KeyCode::N) {
            self.inv.direction = Direction::North;
        }
        if is_key_pressed(KeyCode::E) {
            self.inv.direction = Direction::East;
        }
        if is_key_pressed(KeyCode::S) {
            self.inv.direction = Direction::South;
        }
        if is_key_pressed(KeyCode::W) {
            self.inv.direction = Direction::West;
        }

        GameEvent::None
    }
}

M src/screens/inventory.rs => src/screens/inventory.rs +6 -11
@@ 32,8 32,6 @@ pub struct Inventory {
    /// and on how many slots to draw per page
    /// see SLOTS_PER_PAGE for more
    page: usize,
    /// true if left mouse button is being held
    is_mouse_down: bool
}
impl Inventory {
    pub fn new() -> Self {


@@ 49,10 47,9 @@ impl Inventory {
        Self {
            infinite_items,
            contents: cont,
            selected: Some(Block::Dirt),
            selected: None,
            direction: Direction::North,
            page: 0,
            is_mouse_down: false
        }
    }
    /// adds a block to the players inventory


@@ 211,7 208,7 @@ impl GameComponent for Inventory {
        }

        let page_count = self.contents.len().div_ceil(SLOTS_PER_PAGE);
        if is_key_down(KeyCode::Left) {
        if is_key_pressed(KeyCode::Left) {
            if self.page == 0 {
                if page_count > 0 {
                    self.page = page_count - 1;


@@ 220,7 217,9 @@ impl GameComponent for Inventory {
                self.page -= 1;
            }
        }
        if is_key_down(KeyCode::Right) {


        if is_key_pressed(KeyCode::Right) {
            if self.page +1 >= page_count {
                self.page = 0;
            } else {


@@ 228,11 227,7 @@ impl GameComponent for Inventory {
            }
        }

        if is_mouse_button_down(MouseButton::Left) {
            self.is_mouse_down = true;
        } else if self.is_mouse_down {
            // mouse button released
            self.is_mouse_down = false;
        if is_mouse_button_pressed(MouseButton::Left) {
            let (mx,my) = mouse_position();
            // virtual render cycle
            let mid_x = screen_width()/2.0;

M src/screens/welcome.rs => src/screens/welcome.rs +9 -36
@@ 10,23 10,11 @@ use crate::draw::{
};

/// The welcome screen
pub struct WelcomeScreen {
    /// true when mouse is above the build button
    /// and the left button is held
    /// used for press-on-release-mechanism
    button_build_down: bool,
    /// true when mouse is above the exit button
    /// and the left button is held
    /// used for press-on-release-mechanism
    button_exit_down: bool
}
pub struct WelcomeScreen;
impl WelcomeScreen {
    /// create a new WelcomeScreen instance
    pub fn new() -> Self {
        Self {
            button_build_down: false,
            button_exit_down: false
        }
        Self
    }
}
impl GameComponent for WelcomeScreen {


@@ 37,23 25,17 @@ impl GameComponent for WelcomeScreen {
            Some(assets.font), 100, WHITE);

        draw_wide_button(
            "Build", self.button_build_down,
            "Build", false,
            screen_width()/2.0, screen_height()/2.0 + 60.0,
            &assets);

        draw_wide_button(
            "Quit", self.button_exit_down,
            "Quit", false,
            screen_width()/2.0, screen_height()/2.0 + 130.0,
            &assets);
    }
    fn ev_loop(&mut self) -> GameEvent {
        let old_btn_build = self.button_build_down;
        let old_btn_exit = self.button_exit_down;

        self.button_exit_down = false;
        self.button_build_down = false;

        if is_mouse_button_down(MouseButton::Left) {
        if is_mouse_button_pressed(MouseButton::Left) {
            let (x,y) = mouse_position();
            let sw_2 = screen_width() / 2.0;
            let sh_2 = screen_height() / 2.0;


@@ 64,26 46,17 @@ impl GameComponent for WelcomeScreen {
                // might be pressing a button
                if y >= sh_2 + 60.0 && y <= sh_2 + 60.0 + btn_height {
                    // pressing first button
                    self.button_build_down = true;
                    // launch build mode
                    return GameEvent::ChangeScreen(crate::Screen::Build(super::build::BuildScreen::new()));
                }
                if y >= sh_2 + 130.0 && y <= sh_2 + 130.0 + btn_height {
                    // pressing second button
                    self.button_exit_down = true;
                    // close game
                    return GameEvent::Quit;
                }
            }
        }

        if !self.button_build_down && self.button_build_down != old_btn_build {
            // user released key
            // TODO: launch build mode
            return GameEvent::ChangeScreen(crate::Screen::Build(super::build::BuildScreen::new()));
        }
        if !self.button_exit_down && self.button_exit_down != old_btn_exit {
            // user released key
            // exit game
            return GameEvent::Quit;
        }

        GameEvent::None
    }
}