Files
horizon-airdef/crates/horizon-client/src/title/create_world.rs
T
Ludovic Marie dc863a66a4 Create New World: vanilla's Game, World and More tabs
CreateWorldScreen as in 26.3: a MenuTabBar (tab sprites, header
separators, underline) over the transparent tab header, each tab's
GridLayout aligned at (0.5, 1/6) between it and a 33-pixel footer with
Create New World and Cancel.

Game: the world name box with its save-folder tooltip, Game Mode
(Survival, Hardcore, Creative), Difficulty and Allow Commands, locked
in Hardcore. World: World Type (Default, Superflat), Customize, the seed
box with its hint, and the Generate Structures / Bonus Chest switches.
More: Game Rules, Experiments and Data Packs. Tooltips follow vanilla's.

The choices reach the new world: hardcore, difficulty and allowCommands
in level.dat, and world_gen_settings with the flat preset, structures and
bonus chest written before streaming starts. Re-Create reads them back.

Fixes #281

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 10:24:52 +02:00

499 lines
16 KiB
Rust

//! CreateWorldScreen: the Game, World and More tabs under a MenuTabBar, with
//! Create New World and Cancel in a 33-pixel footer.
use super::{Button, Title};
use crate::{
hud::{HudAtlas, HudBatch},
text::Text,
ui,
};
/// MenuTabBar.HEIGHT: the header above the tab area.
const TAB_BAR: f32 = 24.0;
/// HeaderAndFooterLayout's default footer height.
const FOOTER: f32 = 33.0;
const TABS: [&str; 3] = [
"createWorld.tab.game.title",
"createWorld.tab.world.title",
"createWorld.tab.more.title",
];
/// Difficulty ids, in the cycle button's order.
pub const DIFFICULTIES: [&str; 4] = ["peaceful", "easy", "normal", "hard"];
/// WorldCreationUiState beyond the name and seed.
#[derive(Clone, Debug, PartialEq)]
pub struct WorldOptions {
/// The open tab: Game, World or More.
pub tab: usize,
/// Survival locked to Hard, one life (`SelectedGameMode.HARDCORE`).
pub hardcore: bool,
/// Index in `DIFFICULTIES`.
pub difficulty: usize,
/// Allow Cheats as toggled; unset follows the game mode.
pub commands: Option<bool>,
/// The Superflat world preset instead of Default.
pub flat: bool,
pub structures: bool,
pub bonus_chest: bool,
}
impl Default for WorldOptions {
fn default() -> Self {
Self {
tab: 0,
hardcore: false,
difficulty: 2,
commands: None,
flat: false,
structures: true,
bonus_chest: false,
}
}
}
impl WorldOptions {
/// Re-Create: the saved world's choices.
pub(super) fn of_world(
level: &crate::save::Level,
commands: bool,
dir: &std::path::Path,
) -> Self {
let generation = crate::save::WorldGen::read(dir).unwrap_or_default();
Self {
hardcore: level.hardcore,
difficulty: DIFFICULTIES
.iter()
.position(|d| *d == level.difficulty)
.unwrap_or(2),
commands: Some(commands),
flat: generation.flat,
structures: generation.structures,
bonus_chest: generation.bonus_chest,
..Self::default()
}
}
/// `getDifficulty`: Hardcore is always Hard.
pub fn difficulty(&self) -> usize {
if self.hardcore { 3 } else { self.difficulty }
}
/// `isAllowCommands`: never in Hardcore, else on for Creative until toggled.
pub fn commands(&self, creative: bool) -> bool {
!self.hardcore && self.commands.unwrap_or(creative)
}
/// `isBonusChest`: never in Hardcore.
pub fn bonus_chest(&self) -> bool {
!self.hardcore && self.bonus_chest
}
pub fn world_gen(&self) -> crate::save::WorldGen {
crate::save::WorldGen {
flat: self.flat,
structures: self.structures,
bonus_chest: self.bonus_chest(),
}
}
/// A press on one of the open tab's cycle buttons or switches.
pub(super) fn press(&mut self, id: u8, creative: &mut bool) {
match id {
// Survival, Hardcore, Creative.
20 => {
(*creative, self.hardcore) = match (*creative, self.hardcore) {
(false, false) => (false, true),
(false, true) => (true, false),
_ => (false, false),
}
}
23 => self.difficulty = (self.difficulty + 1) % DIFFICULTIES.len(),
24 => self.commands = Some(!self.commands(*creative)),
25 => self.flat = !self.flat,
27 => self.structures = !self.structures,
28 => self.bonus_chest = !self.bonus_chest,
_ => {}
}
}
}
/// `Mth.roundToward`: up to a multiple of `step`.
fn round_toward(value: f32, step: f32) -> f32 {
(value / step).ceil() * step
}
/// MenuTabBar.arrangeElements: the first tab's x and every tab's width.
fn tabs(w: f32) -> (f32, f32) {
let width = w.min(400.0) - 28.0;
(
round_toward(((w - width) / 2.0).trunc(), 2.0),
round_toward((width / TABS.len() as f32).trunc(), 2.0),
)
}
/// The tab under the pointer.
pub(super) fn tab_at(w: f32, (x, y): (f32, f32)) -> Option<usize> {
let (x0, width) = tabs(w);
((0.0..TAB_BAR).contains(&y) && x >= x0)
.then(|| ((x - x0) / width) as usize)
.filter(|i| *i < TABS.len())
}
/// GridLayoutTab.doLayout: the open tab's grid, aligned at (0.5, 1/6) in the
/// area between the tab bar and the footer.
fn grid(w: f32, h: f32, tab: usize) -> (f32, f32) {
// Game: the labelled name box (9 + 4 + 20) and three buttons, 8 apart.
// World: a button row, the labelled seed box and the SwitchGrid
// (4 + 20 + 4 + 20 under its empty spacer row). More: three buttons.
let (gw, gh) = match tab {
0 => (210.0, 117.0),
1 => (310.0, 117.0),
_ => (210.0, 76.0),
};
let area = h - TAB_BAR - FOOTER;
(
((w - gw) * 0.5).trunc(),
TAB_BAR + ((area - gh) * 0.166_666_67).trunc(),
)
}
/// The text box under the pointer: 0 the name, 1 the seed.
pub(super) fn field_at(w: f32, h: f32, tab: usize, (x, y): (f32, f32)) -> Option<u8> {
let (gx, gy) = grid(w, h, tab);
match tab {
0 => ui::hovered((x, y), gx + 1.0, gy + 13.0, 208.0, 20.0).then_some(0),
1 => ui::hovered((x, y), gx, gy + 41.0, 308.0, 20.0).then_some(1),
_ => None,
}
}
fn tr(key: &str) -> String {
Text::translate(key, &[]).plain()
}
/// `CycleButton`'s "Label: Value" (`options.generic_value`).
fn labelled(key: &str, value: String) -> String {
Text::translate(
"options.generic_value",
&[Text::translate(key, &[]), Text::literal(value)],
)
.plain()
}
fn on_off(on: bool) -> String {
tr(if on { "options.on" } else { "options.off" })
}
fn game_mode(creative: bool, hardcore: bool) -> &'static str {
match (creative, hardcore) {
(true, _) => "selectWorld.gameMode.creative",
(false, true) => "selectWorld.gameMode.hardcore",
(false, false) => "selectWorld.gameMode.survival",
}
}
/// The open tab's buttons and the footer's.
pub(super) fn buttons(w: f32, h: f32, creative: bool, o: &WorldOptions) -> Vec<Button> {
let b = |x: f32, y: f32, w: f32, label: String, enabled: bool, id: u8| Button {
x,
y,
w,
label,
enabled,
id,
icon: None,
};
let (gx, gy) = grid(w, h, o.tab);
let mut buttons = match o.tab {
0 => vec![
b(
gx,
gy + 41.0,
210.0,
labelled("selectWorld.gameMode", tr(game_mode(creative, o.hardcore))),
true,
20,
),
b(
gx,
gy + 69.0,
210.0,
labelled(
"options.difficulty",
tr(&format!(
"options.difficulty.{}",
DIFFICULTIES[o.difficulty()]
)),
),
!o.hardcore,
23,
),
b(
gx,
gy + 97.0,
210.0,
labelled("selectWorld.allowCommands", on_off(o.commands(creative))),
!o.hardcore,
24,
),
],
1 => vec![
b(
gx,
gy,
150.0,
labelled(
"selectWorld.mapType",
tr(if o.flat {
"generator.minecraft.flat"
} else {
"generator.minecraft.normal"
}),
),
true,
25,
),
// Horizon has no preset editor (CreateFlatWorldScreen) yet.
b(
gx + 160.0,
gy,
150.0,
tr("selectWorld.customizeType"),
false,
26,
),
// SwitchGrid: 44-wide value-only buttons at the grid's right.
b(gx + 266.0, gy + 73.0, 44.0, on_off(o.structures), true, 27),
b(
gx + 266.0,
gy + 97.0,
44.0,
on_off(o.bonus_chest()),
!o.hardcore,
28,
),
],
// Game rules, experiments and data packs have no screens yet.
_ => [
"selectWorld.gameRules",
"selectWorld.experiments",
"selectWorld.dataPacks",
]
.iter()
.enumerate()
.map(|(i, key)| {
b(
gx,
gy + 28.0 * i as f32,
210.0,
tr(key),
false,
29 + i as u8,
)
})
.collect(),
};
// The footer: two 150-wide buttons 8 apart, centred in its 33 pixels.
let x = ((w - 308.0) * 0.5).trunc();
buttons.push(b(
x,
h - 27.0,
150.0,
crate::lang::get_or("selectWorld.create", "Create New World"),
true,
21,
));
buttons.push(b(
x + 158.0,
h - 27.0,
150.0,
crate::lang::get_or("gui.cancel", "Cancel"),
true,
22,
));
buttons
}
impl Title {
/// Everything but the buttons, which `draw` adds after.
#[allow(clippy::too_many_arguments)]
pub(super) fn draw_create(
&self,
atlas: &HudAtlas,
batch: &mut HudBatch,
cursor: (f32, f32),
name: &str,
seed: &str,
focus: u8,
o: &WorldOptions,
) {
let (w, h) = (batch.width.ceil(), batch.height.ceil());
// extractMenuBackground: the tab header strip, the menu background under it.
ui::tiled(
batch,
atlas,
"gui/tab_header_background",
0.0,
0.0,
w,
TAB_BAR,
16.0,
u32::MAX,
);
ui::menu_background(batch, atlas, 0.0, TAB_BAR, w, h - TAB_BAR, false);
// MenuTabBar: header separators either side of the tabs.
let (x0, tw) = tabs(w);
let right = x0 + tw * TABS.len() as f32;
for (x, width) in [(0.0, x0), (right, w - right)] {
ui::tiled(
batch,
atlas,
"gui/header_separator",
x,
TAB_BAR - 2.0,
width,
2.0,
32.0,
u32::MAX,
);
}
for (i, key) in TABS.iter().enumerate() {
let x = x0 + tw * i as f32;
let selected = o.tab == i;
let label = Text::translate(key, &[]);
crate::widgets::record(&label.plain(), [x, 0.0, tw, TAB_BAR], true);
let sprite = match (selected, ui::hovered(cursor, x, 0.0, tw, TAB_BAR)) {
(true, true) => "gui/sprites/widget/tab_selected_highlighted",
(true, false) => "gui/sprites/widget/tab_selected",
(false, true) => "gui/sprites/widget/tab_highlighted",
(false, false) => "gui/sprites/widget/tab",
};
ui::nine_slice(batch, atlas, sprite, x, 0.0, tw, TAB_BAR, u32::MAX);
let lw = atlas.font.width_text(&label).floor();
if selected {
ui::menu_background(batch, atlas, x + 2.0, 2.0, tw - 4.0, TAB_BAR - 2.0, false);
// renderFocusUnderline.
let uw = lw.min(tw - 4.0);
batch.fill(
atlas,
x + ((tw - uw) / 2.0).floor(),
TAB_BAR - 2.0,
uw,
1.0,
u32::MAX,
);
}
// The label sits 3 pixels lower on unselected tabs.
let top = if selected { 0.0 } else { 3.0 };
batch.text_styled(
atlas,
&label,
x + (tw / 2.0).floor() - (lw / 2.0).floor(),
((top + TAB_BAR - 9.0) / 2.0).floor() + 1.0,
u32::MAX,
true,
);
}
let (gx, gy) = grid(w, h, o.tab);
let label = |batch: &mut HudBatch, key: &str, x: f32, y: f32| {
batch.text_styled(atlas, &Text::translate(key, &[]), x, y, u32::MAX, true);
};
match o.tab {
0 => {
// CommonLayouts.labeledElement, centred in the 210 column.
label(batch, "selectWorld.enterName", gx + 1.0, gy);
self.create_field(
atlas,
batch,
name,
[gx + 1.0, gy + 13.0, 208.0],
focus == 0,
None,
);
}
1 => {
label(batch, "selectWorld.enterSeed", gx, gy + 28.0);
self.create_field(
atlas,
batch,
seed,
[gx, gy + 41.0, 308.0],
focus == 1,
Some("selectWorld.seedInfo"),
);
// SwitchGrid labels, centred on their switch rows.
label(batch, "selectWorld.mapFeatures", gx, gy + 78.0);
label(batch, "selectWorld.bonusItems", gx, gy + 102.0);
}
_ => {}
}
// The footer separator, drawn over the screen.
ui::tiled(
batch,
atlas,
"gui/footer_separator",
0.0,
h - FOOTER - 2.0,
w,
2.0,
32.0,
u32::MAX,
);
}
/// An EditBox with its cursor at the end; `hint` shows while it is empty
/// and unfocused.
fn create_field(
&self,
atlas: &HudAtlas,
batch: &mut HudBatch,
value: &str,
[x, y, width]: [f32; 3],
focused: bool,
hint: Option<&str>,
) {
let mut field = ui::EditBox::new(x, y, width, 20.0);
field.value = value.to_owned();
field.cursor = ui::char_len(value);
field.highlight = field.cursor;
field.focused = focused;
field.focused_since(self.started);
field.draw(batch, atlas, None);
if let Some(hint) = hint
&& value.is_empty()
&& !focused
{
batch.text_styled(
atlas,
&Text::translate(hint, &[]).with_color(crate::text::DARK_GRAY),
field.text_x(),
field.text_y(),
u32::MAX,
true,
);
}
}
/// The hovered widget's tooltip, wrapped to Tooltip's 170 pixels.
pub(super) fn create_tooltip(
&self,
atlas: &HudAtlas,
batch: &mut HudBatch,
cursor: (f32, f32),
name: &str,
creative: bool,
o: &WorldOptions,
) {
let (w, h) = (batch.width.ceil(), batch.height.ceil());
let (gx, gy) = grid(w, h, o.tab);
let over = |x: f32, y: f32, width: f32| ui::hovered(cursor, x, y, width, 20.0);
let text = match o.tab {
0 if over(gx + 1.0, gy + 13.0, 208.0) => Some(Text::translate(
"selectWorld.targetFolder",
&[Text::literal(self.target_folder(name)).italic()],
)),
0 if over(gx, gy + 41.0, 210.0) => Some(Text::translate(
&format!("{}.info", game_mode(creative, o.hardcore)),
&[],
)),
0 if over(gx, gy + 69.0, 210.0) => Some(Text::translate(
&format!("options.difficulty.{}.info", DIFFICULTIES[o.difficulty()]),
&[],
)),
0 if over(gx, gy + 97.0, 210.0) => {
Some(Text::translate("selectWorld.allowCommands.info", &[]))
}
1 if over(gx + 266.0, gy + 73.0, 44.0) => {
Some(Text::translate("selectWorld.mapFeatures.info", &[]))
}
_ => None,
};
if let Some(text) = text {
ui::tooltip(batch, atlas, &atlas.font.split(&text, 170.0), cursor);
}
}
}