Theme

alex/beta-minecraft-rust

Minecraft Beta 1.7.3 Rewrite

BRANCH main
main
SNAPSHOT Draw terrain GPU-driven from packed per-section buffers with GPU occlusion culling, add a render distance slider (2-128) with a quad/disk shape, cut chunk bookkeeping at high render distances, and fix a light underflow at y=0
DOWNLOAD
Download file

root / crates / client / src / gui / layout.rs

use crate::interaction::Screen;
use crate::menu::contains;
use common::inventory::ContainerKind;

pub fn container_rect(size: [f32; 2], kind: ContainerKind) -> [f32; 4] {
    let height = match kind {
        ContainerKind::Chest { slots } => (slots / 9 * 18 + 114) as f32,
        _ => 166.0,
    };
    [
        ((size[0] - 176.0) / 2.0).floor(),
        ((size[1] - height) / 2.0).floor(),
        176.0,
        height,
    ]
}

pub fn slot_position(kind: ContainerKind, index: usize) -> [f32; 2] {
    let base = kind.inventory_start();
    if index >= base {
        let index = index - base;
        let y = match kind {
            ContainerKind::Chest { slots } => (slots / 9 * 18 + 32) as f32,
            _ => 84.0,
        };
        return [
            8.0 + (index % 9) as f32 * 18.0,
            y + if index < 27 {
                (index / 9) as f32 * 18.0
            } else {
                58.0
            },
        ];
    }
    match kind {
        ContainerKind::Inventory => match index {
            0 => [144.0, 36.0],
            1..=4 => [
                88.0 + ((index - 1) % 2) as f32 * 18.0,
                26.0 + ((index - 1) / 2) as f32 * 18.0,
            ],
            _ => [8.0, 8.0 + (index - 5) as f32 * 18.0],
        },
        ContainerKind::Workbench if index == 0 => [124.0, 35.0],
        ContainerKind::Workbench => [
            30.0 + ((index - 1) % 3) as f32 * 18.0,
            17.0 + ((index - 1) / 3) as f32 * 18.0,
        ],
        ContainerKind::Chest { .. } => [
            8.0 + (index % 9) as f32 * 18.0,
            18.0 + (index / 9) as f32 * 18.0,
        ],
        ContainerKind::Furnace => [[56.0, 17.0], [56.0, 53.0], [116.0, 35.0]][index],
        ContainerKind::Dispenser => [
            62.0 + (index % 3) as f32 * 18.0,
            17.0 + (index / 3) as f32 * 18.0,
        ],
    }
}

pub fn slot_at(size: [f32; 2], kind: ContainerKind, cursor: [f32; 2]) -> Option<i16> {
    let rect = container_rect(size, kind);
    let slot = (0..kind.slot_count()).find(|&index| {
        let [x, y] = slot_position(kind, index);
        contains([rect[0] + x - 1.0, rect[1] + y - 1.0, 18.0, 18.0], cursor)
    });
    slot.map(|index| index as i16)
        .or((!contains(rect, cursor)).then_some(-999))
}

pub fn button_rect(size: [f32; 2], screen: Screen, index: usize) -> [f32; 4] {
    let offset = if screen == Screen::Death {
        72.0 + index as f32 * 24.0
    } else {
        8.0 + index as f32 * 96.0
    };
    [size[0] / 2.0 - 100.0, size[1] / 4.0 + offset, 200.0, 20.0]
}

pub fn button_at(size: [f32; 2], screen: Screen, cursor: [f32; 2]) -> Option<usize> {
    (0..2).find(|&index| contains(button_rect(size, screen, index), cursor))
}