Skip to main content

flatland_client_ui/
layout.rs

1//! Normalized HUD layout regions (0.0–1.0 fractions of the viewport).
2
3use crate::hud::HudViewMode;
4
5#[derive(Debug, Clone, Copy, PartialEq)]
6pub struct NormRect {
7    pub x: f32,
8    pub y: f32,
9    pub w: f32,
10    pub h: f32,
11}
12
13impl NormRect {
14    pub const fn new(x: f32, y: f32, w: f32, h: f32) -> Self {
15        Self { x, y, w, h }
16    }
17
18    pub fn pixel_rect(self, width: f32, height: f32) -> (f32, f32, f32, f32) {
19        (
20            self.x * width,
21            self.y * height,
22            self.w * width,
23            self.h * height,
24        )
25    }
26}
27
28#[derive(Debug, Clone, Copy, PartialEq)]
29pub struct HudLayout {
30    pub world: NormRect,
31    pub combat: Option<NormRect>,
32    pub status: Option<NormRect>,
33    pub sidebar: Option<NormRect>,
34    pub log: Option<NormRect>,
35}
36
37pub fn hud_layout(combat_visible: bool, mode: HudViewMode) -> HudLayout {
38    match (mode, combat_visible) {
39        (HudViewMode::Normal, true) => HudLayout {
40            world: NormRect::new(0.0, 0.0, 1.0, 0.52),
41            combat: Some(NormRect::new(0.0, 0.52, 1.0, 0.10)),
42            status: Some(NormRect::new(0.0, 0.62, 1.0, 0.04)),
43            sidebar: Some(NormRect::new(0.0, 0.66, 1.0, 0.27)),
44            log: Some(NormRect::new(0.0, 0.93, 1.0, 0.07)),
45        },
46        (HudViewMode::Normal, false) => HudLayout {
47            world: NormRect::new(0.0, 0.0, 1.0, 0.56),
48            combat: None,
49            status: Some(NormRect::new(0.0, 0.56, 1.0, 0.04)),
50            sidebar: Some(NormRect::new(0.0, 0.60, 1.0, 0.33)),
51            log: Some(NormRect::new(0.0, 0.93, 1.0, 0.07)),
52        },
53        (HudViewMode::Compact, true) => HudLayout {
54            world: NormRect::new(0.0, 0.0, 1.0, 0.70),
55            combat: Some(NormRect::new(0.0, 0.70, 1.0, 0.08)),
56            status: Some(NormRect::new(0.0, 0.78, 1.0, 0.04)),
57            sidebar: None,
58            log: Some(NormRect::new(0.0, 0.82, 1.0, 0.04)),
59        },
60        (HudViewMode::Compact, false) => HudLayout {
61            world: NormRect::new(0.0, 0.0, 1.0, 0.88),
62            combat: None,
63            status: Some(NormRect::new(0.0, 0.88, 1.0, 0.04)),
64            sidebar: None,
65            log: Some(NormRect::new(0.0, 0.92, 1.0, 0.04)),
66        },
67        (HudViewMode::Map, true) => HudLayout {
68            world: NormRect::new(0.0, 0.0, 1.0, 0.84),
69            combat: Some(NormRect::new(0.0, 0.84, 1.0, 0.10)),
70            status: Some(NormRect::new(0.0, 0.94, 1.0, 0.02)),
71            sidebar: None,
72            log: None,
73        },
74        (HudViewMode::Map, false) => HudLayout {
75            world: NormRect::new(0.0, 0.0, 1.0, 0.96),
76            combat: None,
77            status: Some(NormRect::new(0.0, 0.96, 1.0, 0.02)),
78            sidebar: None,
79            log: None,
80        },
81    }
82}