Merge pull request #2214 from masa-masa-0314/fix/superflat-customize

🤖 Agent: Merged by hand by Ludovic's AI agent (orchestrator) after the contribution gate. rdbtCVS's change request is addressed; a re-review was asked, with no new objection since. Proven in wave 5's union run (cg20 union C 5900ab387, Mac mini); main merged in since, CI green at this head.
This commit is contained in:
Ludovic Marie
2026-10-27 12:28:29 +01:00
committed by GitHub
5 changed files with 1053 additions and 4 deletions
+16
View File
@@ -568,6 +568,22 @@ impl Game<'_> {
"generate_structures": options.structures,
"bonus_chest": options.bonus_chest(),
}),
Mode::CreateFlat {
name,
seed,
options,
flat,
..
} => json!({
"mode": "customize-flat",
"fields": { "name": name, "seed": seed },
"superflat": options.flat,
"biome": flat.settings().biome,
"layers": flat.settings().layers.iter().map(|layer| json!({
"block": layer.block,
"height": layer.height,
})).collect::<Vec<_>>(),
}),
Mode::Edit {
world,
name,
@@ -93,7 +93,12 @@ impl WorldRequest {
level.hardcore = o.hardcore;
level.difficulty = horizon_client::title::DIFFICULTIES[o.difficulty()].into();
level.allow_commands = Some(o.commands(creative));
horizon_client::save::write_world_gen_settings(&save_root, level.seed, o.world_gen())?;
horizon_client::save::write_world_gen_settings_with(
&save_root,
level.seed,
o.world_gen(),
o.flat_settings(),
)?;
// WorldCreationUiState.getGameRules, written with the level.
level.game_rules = o.rules.by_name();
}
+125
View File
@@ -14,10 +14,12 @@ use std::{
};
mod create_world;
mod flat;
#[cfg(test)]
mod tests;
pub mod world_management;
pub use create_world::{DIFFICULTIES, WorldOptions};
pub use flat::{FlatLayer, FlatSettings, State as FlatEditor};
/// SelectWorldScreen's `HeaderAndFooterLayout(8 + 9 + 8 + 20 + 4, 60)`.
const LIST_TOP: f32 = 49.0;
@@ -102,6 +104,16 @@ pub enum Mode {
return_to: Box<Mode>,
},
Multiplayer(crate::multiplayer::Screen),
/// CreateFlatWorldScreen over Create New World: the fields that screen
/// keeps and the superflat editor on top of them.
CreateFlat {
name: String,
seed: String,
creative: bool,
focus: u8,
options: WorldOptions,
flat: flat::State,
},
}
#[derive(Clone, Debug, PartialEq)]
pub enum Action {
@@ -129,6 +141,8 @@ pub struct Title {
/// Direct Connection's address, synchronized with `options.txt`'s `lastServer`.
pub last_server: String,
pub saves: PathBuf,
/// The prepared resource root: the flat preset list and block registry.
resources: PathBuf,
pub started: std::time::Instant,
search: String,
search_focused: bool,
@@ -234,6 +248,7 @@ impl Title {
mode,
last_server: String::new(),
saves,
resources: resources.to_owned(),
started: now,
search: String::new(),
search_focused: false,
@@ -320,6 +335,64 @@ impl Title {
self.return_to_list(None);
}
}
/// A press on the superflat screens: their list, share box or buttons.
fn flat_click(&mut self, x: f32, y: f32, size: (f32, f32), hit: Option<u8>) {
match hit {
Some(70) => {
if let Mode::CreateFlat { flat, .. } = &mut self.mode {
flat.remove_selected();
}
}
Some(71) => {
if let Mode::CreateFlat { flat, .. } = &mut self.mode {
flat.open_presets();
}
}
Some(72) => self.close_flat(true),
Some(73) => self.close_flat(false),
Some(74) => {
if let Mode::CreateFlat { flat, .. } = &mut self.mode {
flat.apply_share();
}
}
Some(75) => {
if let Mode::CreateFlat { flat, .. } = &mut self.mode {
flat.back_to_layers();
}
}
None => {
if let Mode::CreateFlat { flat, .. } = &mut self.mode {
flat::click(flat, (x, y), size);
}
}
_ => {}
}
}
/// Return to Create New World, keeping (`apply`) or dropping the layers.
fn close_flat(&mut self, apply: bool) {
let mode = std::mem::replace(&mut self.mode, Mode::Main);
if let Mode::CreateFlat {
name,
seed,
creative,
focus,
mut options,
flat,
} = mode
{
if apply {
options.flat = true;
options.flat_settings = Some(flat.settings);
}
self.mode = Mode::Create {
name,
seed,
creative,
focus,
options,
};
}
}
fn matches_search(&self, world: &WorldEntry) -> bool {
let filter = self.search.to_lowercase();
world.name.to_lowercase().contains(&filter) || world.folder.to_lowercase().contains(&filter)
@@ -370,6 +443,10 @@ impl Title {
if let Mode::Multiplayer(screen) = &mut self.mode {
return screen.drag(cursor.1, size);
}
if let Mode::CreateFlat { flat, .. } = &mut self.mode {
flat::drag(flat, cursor, size);
return;
}
if let Mode::Select { worlds, .. } = &self.mode {
let (top, bottom, _, content) = self.world_list(worlds, size);
let mut scroll = self.scroll.get();
@@ -385,6 +462,9 @@ impl Title {
if let Mode::Multiplayer(screen) = &mut self.mode {
screen.release();
}
if let Mode::CreateFlat { flat, .. } = &mut self.mode {
flat::release(flat);
}
self.scroll.get_mut().release();
}
/// Mouse wheel over the classic world and server lists.
@@ -396,6 +476,10 @@ impl Title {
screen.wheel(delta, cursor, size);
return;
}
if let Mode::CreateFlat { flat, .. } = &mut self.mode {
flat::wheel(flat, delta, cursor, size);
return;
}
let Mode::Select { worlds, .. } = &self.mode else {
return;
};
@@ -720,6 +804,7 @@ impl Title {
Mode::Create {
creative, options, ..
} => create_world::buttons(w, h, *creative, options),
Mode::CreateFlat { flat, .. } => flat::buttons(w, h, flat),
Mode::Edit { world, name, .. } => {
let x = (w / 2.0).floor() - 100.0;
let top = edit_top(h);
@@ -1042,6 +1127,7 @@ impl Title {
options,
..
} => self.draw_create(atlas, batch, cursor, name, seed, *focus, options),
Mode::CreateFlat { flat, .. } => flat::draw(self, atlas, batch, cursor, flat),
Mode::Edit { name, focused, .. } => {
crate::ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false);
centered(
@@ -1248,6 +1334,10 @@ impl Title {
.into_iter()
.find(|b| b.enabled && x >= b.x && x < b.x + b.w && y >= b.y && y < b.y + 20.0)
.map(|b| b.id);
if matches!(self.mode, Mode::CreateFlat { .. }) {
self.flat_click(x, y, (w, h), hit);
return (Action::None, hit.is_some());
}
let action = self.click_action(x, y, (w, h), hit);
// World list selection is silent; launching its selected entry sounds.
let sound = hit.is_some() || matches!(action, Action::Play { .. });
@@ -1416,6 +1506,27 @@ impl Title {
*self.rules.get_mut() = Some(RulesScreen::for_creation(&options.rules));
Action::None
}
(
Mode::Create {
name,
seed,
creative,
focus,
options,
},
Some(26),
) => {
let flat = flat::State::new(&self.resources, options.flat_settings.clone());
self.mode = Mode::CreateFlat {
name: name.clone(),
seed: seed.clone(),
creative: *creative,
focus: *focus,
options: options.clone(),
flat,
};
Action::None
}
(
Mode::Create {
creative, options, ..
@@ -1669,6 +1780,16 @@ impl Title {
self.return_to_list(Some(&folder));
}
Mode::Error { return_to, .. } => self.mode = *return_to.clone(),
Mode::CreateFlat { .. } => {
if matches!(&self.mode, Mode::CreateFlat { flat, .. } if flat.page == flat::Page::Presets)
{
if let Mode::CreateFlat { flat, .. } = &mut self.mode {
flat.back_to_layers();
}
} else {
self.close_flat(false);
}
}
Mode::Create { .. } => self.cancel_create(),
_ => self.mode = Mode::Main,
}
@@ -1697,6 +1818,7 @@ const SEARCH: crate::clipboard::FieldId = ("world.search", 0);
const EDIT_NAME: crate::clipboard::FieldId = ("world.edit-name", 0);
const WORLD_NAME: crate::clipboard::FieldId = ("world.name", 0);
const WORLD_SEED: crate::clipboard::FieldId = ("world.seed", 0);
const FLAT_SHARE: crate::clipboard::FieldId = ("world.flat-share", 0);
/// The focused field of the world screens and the server screens: the list's
/// search, a world name or seed, or Add Server, Direct Connection and Join a
/// Friend.
@@ -1720,6 +1842,9 @@ fn caret_field<'a>(
_ => None,
},
Mode::Multiplayer(screen) => screen.focused_text(),
Mode::CreateFlat { flat, .. } if flat.page == flat::Page::Presets && flat.share_focused => {
Some((FLAT_SHARE, &mut flat.share, 1230))
}
_ => None,
}
}
@@ -32,6 +32,8 @@ pub struct WorldOptions {
pub commands: Option<bool>,
/// The Superflat world preset instead of Default.
pub flat: bool,
/// CreateFlatWorldScreen's layers and biome, kept with the world type.
pub flat_settings: Option<super::flat::FlatSettings>,
pub structures: bool,
pub bonus_chest: bool,
/// `getGameRules`: the Game Rules screen's values, saved with the world.
@@ -45,6 +47,7 @@ impl Default for WorldOptions {
difficulty: 2,
commands: None,
flat: false,
flat_settings: None,
structures: true,
bonus_chest: false,
rules: crate::gamerules::GameRules::default(),
@@ -67,6 +70,10 @@ impl WorldOptions {
.unwrap_or(2),
commands: Some(commands),
flat: generation.flat,
flat_settings: generation
.flat
.then(|| super::flat::FlatSettings::from_world(dir))
.flatten(),
structures: generation.structures,
bonus_chest: generation.bonus_chest,
rules: crate::gamerules::GameRules::load(dir),
@@ -92,6 +99,12 @@ impl WorldOptions {
bonus_chest: self.bonus_chest(),
}
}
/// The custom layers, as `write_world_gen_settings_with` takes them.
pub fn flat_settings(&self) -> Option<horizon_world::nbt::Tag> {
self.flat_settings
.as_ref()
.map(super::flat::FlatSettings::to_tag)
}
/// 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 {
@@ -105,7 +118,13 @@ impl WorldOptions {
}
23 => self.difficulty = (self.difficulty + 1) % DIFFICULTIES.len(),
24 => self.commands = Some(!self.commands(*creative)),
25 => self.flat = !self.flat,
25 => {
self.flat = !self.flat;
if self.flat {
// setWorldType starts the generator from the flat preset.
self.flat_settings = Some(super::flat::FlatSettings::default());
}
}
27 => self.structures = !self.structures,
28 => self.bonus_chest = !self.bonus_chest,
_ => {}
@@ -247,13 +266,13 @@ pub(super) fn buttons(w: f32, h: f32, creative: bool, o: &WorldOptions) -> Vec<B
true,
25,
),
// Horizon has no preset editor (CreateFlatWorldScreen) yet.
// CreateFlatWorldScreen: only a flat generator has a preset editor.
b(
gx + 160.0,
gy,
150.0,
tr("selectWorld.customizeType"),
false,
o.flat,
26,
),
// SwitchGrid: 44-wide value-only buttons at the grid's right.
+884
View File
@@ -0,0 +1,884 @@
//! CreateFlatWorldScreen and PresetFlatWorldScreen: the Superflat layer list,
//! the preset chooser and the settings they hand back to Create New World.
use super::{Button, Title};
use crate::{
hud::{HudAtlas, HudBatch, rgba},
lang,
text::{Text, format_translation},
ui,
};
use horizon_world::nbt::Tag;
use serde_json::Value;
use std::{collections::BTreeSet, path::Path};
/// DimensionType.Y_SIZE: the most blocks a flat generator takes.
const Y_SIZE: i32 = 4064;
/// The list's row width (ObjectSelectionList.getRowWidth).
const ROW_WIDTH: f32 = 220.0;
/// The header row above the layers.
const HEADER: f32 = 14.0;
/// A layer or preset row.
const LAYER_ROW: f32 = 20.0;
const PRESET_ROW: f32 = 24.0;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FlatLayer {
/// A block id such as `minecraft:dirt`.
pub block: String,
/// How many blocks tall this layer is (0..=4064).
pub height: i32,
}
/// FlatLevelGeneratorSettings: the layers (bottom to top), the biome and the
/// decorations CreateFlatWorldScreen edits.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct FlatSettings {
pub biome: String,
pub features: bool,
pub lakes: bool,
pub layers: Vec<FlatLayer>,
pub structure_overrides: Vec<String>,
}
impl Default for FlatSettings {
fn default() -> Self {
// worldgen/world_preset/flat.json's overworld.
Self {
biome: "minecraft:plains".into(),
features: false,
lakes: false,
layers: vec![
FlatLayer {
block: "minecraft:bedrock".into(),
height: 1,
},
FlatLayer {
block: "minecraft:dirt".into(),
height: 2,
},
FlatLayer {
block: "minecraft:grass_block".into(),
height: 1,
},
],
structure_overrides: vec!["minecraft:strongholds".into(), "minecraft:villages".into()],
}
}
}
impl FlatSettings {
/// A saved world's flat generator settings, for Re-Create.
pub fn from_world(dir: &Path) -> Option<Self> {
let settings = crate::save::world_gen_settings(dir)?;
let generator = settings
.get("dimensions")?
.get("minecraft:overworld")?
.get("generator")?;
if crate::save::text(generator.get("type"))?.trim_start_matches("minecraft:") != "flat" {
return None;
}
Self::from_tag(generator.get("settings")?)
}
pub fn from_tag(tag: &Tag) -> Option<Self> {
let Some(Tag::List(list)) = tag.get("layers") else {
return None;
};
let layers: Vec<FlatLayer> = list
.iter()
.filter_map(|layer| {
Some(FlatLayer {
block: crate::save::text(layer.get("block"))?,
height: crate::save::number(layer.get("height"))?.clamp(0, Y_SIZE as i64)
as i32,
})
})
.collect();
// `layers: []` is a valid void flat world; a list whose entries all
// fail to parse is not.
if layers.is_empty() && !list.is_empty() {
return None;
}
let flag = |key: &str, default: bool| {
crate::save::number(tag.get(key)).map_or(default, |value| value != 0)
};
let structure_overrides = match tag.get("structure_overrides") {
Some(Tag::String(text)) => vec![text.to_string_lossy()],
Some(Tag::List(list)) => list
.iter()
.filter_map(|value| crate::save::text(Some(value)))
.collect(),
_ => Vec::new(),
};
Some(Self {
biome: crate::save::text(tag.get("biome")).unwrap_or_else(|| "minecraft:plains".into()),
features: flag("features", false),
lakes: flag("lakes", false),
layers,
structure_overrides,
})
}
/// FlatLevelGeneratorSettings.CODEC, as world_gen_settings stores it.
pub fn to_tag(&self) -> Tag {
let string = |text: &str| Tag::String(text.into());
Tag::compound([
("biome", string(&self.biome)),
("features", Tag::Byte(i8::from(self.features))),
("lakes", Tag::Byte(i8::from(self.lakes))),
(
"layers",
Tag::List(
self.layers
.iter()
.map(|layer| {
Tag::compound([
("block", string(&layer.block)),
("height", Tag::Int(layer.height)),
])
})
.collect(),
),
),
(
"structure_overrides",
Tag::List(
self.structure_overrides
.iter()
.map(|id| string(id))
.collect(),
),
),
])
}
/// `CreateFlatWorldScreen.save`: the text the preset screen shows.
pub fn to_share(&self) -> String {
let layers = self
.layers
.iter()
.map(|layer| {
if layer.height == 1 {
layer.block.clone()
} else {
format!("{}*{}", layer.height, layer.block)
}
})
.collect::<Vec<_>>()
.join(",");
format!("{layers};{}", self.biome)
}
/// `PresetFlatWorldScreen.fromString`: `layers;biome`, keeping the
/// decorations the screen was opened with. `None` for a malformed list,
/// as vanilla falls back to the preset's own settings.
pub fn parse(
text: &str,
blocks: &BTreeSet<String>,
biomes: &BTreeSet<String>,
base: &Self,
) -> Option<Self> {
let (layer_text, biome_text) = text.split_once(';').unwrap_or((text, ""));
let mut layers = Vec::new();
let mut total = 0;
for part in layer_text.split(',') {
let part = part.trim();
if part.is_empty() {
continue;
}
let (height, block) = match part.split_once('*') {
Some((height, block)) => (height.trim().parse::<i32>().ok()?, block.trim()),
None => (1, part),
};
let block = namespaced(block);
if !blocks.is_empty() && !blocks.contains(&block) {
return None;
}
let height = height.max(0).min(Y_SIZE - total);
if height == 0 {
continue;
}
total += height;
layers.push(FlatLayer { block, height });
}
if layers.is_empty() {
return None;
}
let mut biome = namespaced(biome_text.trim());
if biome == "minecraft:" || biome.is_empty() {
biome = "minecraft:plains".into();
}
if !biomes.is_empty() && !biomes.contains(&biome) {
biome = "minecraft:plains".into();
}
Some(Self {
biome,
layers,
..base.clone()
})
}
}
/// `Identifier.parse`: bare names get the default namespace.
fn namespaced(id: &str) -> String {
if id.is_empty() || id.contains(':') {
id.to_owned()
} else {
format!("minecraft:{id}")
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub(super) enum Page {
Layers,
Presets,
}
#[derive(Clone, Debug)]
pub struct State {
pub(super) page: Page,
pub(super) settings: FlatSettings,
/// The preset screen's share box (`createWorld.customize.presets.share`).
pub(super) share: String,
pub(super) share_focused: bool,
/// The selected layer, in the list's top-first order.
pub(super) selected: Option<usize>,
pub(super) scroll: ui::Scroll,
preset_selected: Option<usize>,
preset_scroll: ui::Scroll,
presets: Vec<Preset>,
blocks: BTreeSet<String>,
biomes: BTreeSet<String>,
}
#[derive(Clone, Debug)]
struct Preset {
id: String,
display: String,
settings: FlatSettings,
}
impl State {
/// The layers the editor holds, for the control socket's state.
pub fn settings(&self) -> &FlatSettings {
&self.settings
}
pub(super) fn new(root: &Path, current: Option<FlatSettings>) -> Self {
let settings = current.unwrap_or_default();
let (presets, blocks, biomes) = load(root);
Self {
page: Page::Layers,
share: settings.to_share(),
settings,
share_focused: false,
selected: None,
scroll: ui::Scroll::default(),
preset_selected: None,
preset_scroll: ui::Scroll::default(),
presets,
blocks,
biomes,
}
}
pub(super) fn open_presets(&mut self) {
self.page = Page::Presets;
self.preset_selected = None;
self.preset_scroll = ui::Scroll::default();
self.share = self.settings.to_share();
self.share_focused = false;
}
pub(super) fn back_to_layers(&mut self) {
self.page = Page::Layers;
self.share_focused = false;
}
pub(super) fn remove_selected(&mut self) {
let Some(row) = self.selected else {
return;
};
let index = self.settings.layers.len().wrapping_sub(1 + row);
if index < self.settings.layers.len() {
self.settings.layers.remove(index);
}
self.selected = None;
}
/// PresetFlatWorldScreen's Select: parse the share text into the settings
/// the Create New World screen keeps.
pub(super) fn apply_share(&mut self) {
// PresetFlatWorldScreen keeps the selected preset's decorations while
// the share text supplies its layers and biome.
let base = self
.preset_selected
.and_then(|index| self.presets.get(index))
.map(|preset| preset.settings.clone())
.unwrap_or_else(|| self.settings.clone());
// A malformed list falls back to FlatLevelGeneratorSettings.getDefault.
let settings =
FlatSettings::parse(&self.share, &self.blocks, &self.biomes, &base).unwrap_or_default();
self.settings = settings;
self.share = self.settings.to_share();
self.page = Page::Layers;
self.share_focused = false;
self.selected = None;
}
fn select_preset(&mut self, index: usize) {
let Some(preset) = self.presets.get(index) else {
return;
};
self.preset_selected = Some(index);
self.share = preset.settings.to_share();
self.share_focused = false;
}
fn selecting(&self) -> Option<usize> {
self.preset_selected
}
}
/// `<root>/data/<namespace>/worldgen/flat_level_generator_preset/<path>.json`,
/// in the order of the `visible` tag, plus the block and biome ids the share
/// text is checked against.
fn load(root: &Path) -> (Vec<Preset>, BTreeSet<String>, BTreeSet<String>) {
let mut presets = Vec::new();
let tag_path =
root.join("data/minecraft/tags/worldgen/flat_level_generator_preset/visible.json");
if let Ok(text) = std::fs::read_to_string(tag_path)
&& let Ok(Value::Object(tag)) = serde_json::from_str(&text)
&& let Some(Value::Array(ids)) = tag.get("values")
{
for id in ids.iter().filter_map(Value::as_str) {
let Some((namespace, path)) = id.split_once(':') else {
continue;
};
let path = root.join(format!(
"data/{namespace}/worldgen/flat_level_generator_preset/{path}.json"
));
let Ok(text) = std::fs::read_to_string(path) else {
continue;
};
let Ok(value) = serde_json::from_str::<Value>(&text) else {
continue;
};
let Some(settings) = preset_settings(&value) else {
continue;
};
presets.push(Preset {
id: id.to_owned(),
display: value["display"]
.as_str()
.unwrap_or("minecraft:grass_block")
.to_owned(),
settings,
});
}
}
let blocks = registry_ids(root, "minecraft:block");
let biomes = json_keys(&root.join("worldgen/biomes.json"));
(presets, blocks, biomes)
}
fn preset_settings(value: &Value) -> Option<FlatSettings> {
let settings = value.get("settings")?;
let layers = settings
.get("layers")?
.as_array()?
.iter()
.filter_map(|layer| {
Some(FlatLayer {
block: layer.get("block")?.as_str()?.to_owned(),
height: layer.get("height")?.as_i64()? as i32,
})
})
.collect::<Vec<_>>();
if layers.is_empty() {
return None;
}
let structure_overrides = match settings.get("structure_overrides") {
Some(Value::String(id)) => vec![id.clone()],
Some(Value::Array(ids)) => ids
.iter()
.filter_map(Value::as_str)
.map(str::to_owned)
.collect(),
_ => Vec::new(),
};
Some(FlatSettings {
biome: settings
.get("biome")
.and_then(Value::as_str)
.unwrap_or("minecraft:plains")
.to_owned(),
features: settings
.get("features")
.and_then(Value::as_bool)
.unwrap_or(false),
lakes: settings
.get("lakes")
.and_then(Value::as_bool)
.unwrap_or(false),
layers,
structure_overrides,
})
}
/// The ids of a registry in the prepared `registries.json`.
fn registry_ids(root: &Path, registry: &str) -> BTreeSet<String> {
let Ok(text) = std::fs::read_to_string(root.join("registries.json")) else {
return BTreeSet::new();
};
let Ok(value) = serde_json::from_str::<Value>(&text) else {
return BTreeSet::new();
};
value
.get(registry)
.and_then(|registry| registry.get("entries"))
.and_then(Value::as_object)
.map(|entries| entries.keys().cloned().collect())
.unwrap_or_default()
}
fn json_keys(path: &Path) -> BTreeSet<String> {
let Ok(text) = std::fs::read_to_string(path) else {
return BTreeSet::new();
};
let Ok(value) = serde_json::from_str::<Value>(&text) else {
return BTreeSet::new();
};
value
.as_object()
.map(|entries| entries.keys().cloned().collect())
.unwrap_or_default()
}
/// CreateFlatWorldScreen's list and the preset screen's list.
fn list_area(state: &State, size: (f32, f32)) -> (f32, f32, f32, f32) {
let (w, h) = (size.0.ceil(), size.1.ceil());
let (top, bottom) = match state.page {
Page::Layers => (43.0, h - 60.0),
Page::Presets => (80.0, h - 37.0),
};
let left = ((w - ROW_WIDTH) * 0.5).floor();
let rows = match state.page {
Page::Layers => HEADER + state.settings.layers.len() as f32 * LAYER_ROW + 4.0,
Page::Presets => state.presets.len() as f32 * PRESET_ROW + 4.0,
};
(top, bottom, left, rows)
}
pub(super) fn buttons(w: f32, h: f32, state: &State) -> Vec<Button> {
let b = |x: f32, y: f32, w: f32, key: &str, fallback: &str, enabled: bool, id: u8| Button {
x,
y,
w,
label: lang::get_or(key, fallback),
enabled,
id,
icon: None,
};
match state.page {
Page::Layers => {
let x = ((w - 308.0) * 0.5).trunc();
vec![
b(
x,
h - 52.0,
150.0,
"createWorld.customize.flat.removeLayer",
"Remove Layer",
state.selected.is_some(),
70,
),
b(
x + 158.0,
h - 52.0,
150.0,
"createWorld.customize.presets",
"Presets",
true,
71,
),
b(x, h - 28.0, 150.0, "gui.done", "Done", true, 72),
b(x + 158.0, h - 28.0, 150.0, "gui.cancel", "Cancel", true, 73),
]
}
Page::Presets => vec![
b(
w / 2.0 - 155.0,
h - 28.0,
150.0,
"createWorld.customize.presets.select",
"Select",
state.selecting().is_some() || state.share.len() > 1,
74,
),
b(
w / 2.0 + 5.0,
h - 28.0,
150.0,
"gui.cancel",
"Cancel",
true,
75,
),
],
}
}
/// The layer under the pointer (top-first), and whether the pointer is on the
/// list at all.
fn layer_row(state: &State, cursor: (f32, f32), size: (f32, f32)) -> Option<usize> {
let (top, bottom, _, _) = list_area(state, size);
if cursor.1 < top || cursor.1 >= bottom {
return None;
}
let y = cursor.1 - top - 2.0 + state.scroll.amount;
if y < HEADER {
return None;
}
let row = ((y - HEADER) / LAYER_ROW).floor() as usize;
(row < state.settings.layers.len()).then_some(row)
}
fn preset_row(state: &State, cursor: (f32, f32), size: (f32, f32)) -> Option<usize> {
let (top, bottom, _, _) = list_area(state, size);
if cursor.1 < top || cursor.1 >= bottom {
return None;
}
let row = ((cursor.1 - top - 2.0 + state.preset_scroll.amount) / PRESET_ROW).floor() as usize;
(row < state.presets.len()).then_some(row)
}
/// A press on the list, the share box or its scrollbar.
pub(super) fn click(state: &mut State, cursor: (f32, f32), size: (f32, f32)) {
let (top, bottom, left, content) = list_area(state, size);
let bar = left + ROW_WIDTH + 6.0;
match state.page {
Page::Layers => {
if state.scroll.press(cursor, bar, top, bottom, content) {
return;
}
state.selected = layer_row(state, cursor, size);
}
Page::Presets => {
let field = share_field(size);
state.share_focused = ui::hovered(cursor, field[0], field[1], field[2], field[3]);
if state.preset_scroll.press(cursor, bar, top, bottom, content) {
return;
}
if let Some(row) = preset_row(state, cursor, size) {
state.select_preset(row);
}
}
}
}
fn share_field(size: (f32, f32)) -> [f32; 4] {
[50.0, 40.0, (size.0.ceil() - 100.0).max(1.0), 20.0]
}
pub(super) fn wheel(state: &mut State, delta: f32, cursor: (f32, f32), size: (f32, f32)) {
let (top, bottom, _, content) = list_area(state, size);
if cursor.1 < top || cursor.1 >= bottom {
return;
}
match state.page {
Page::Layers => state.scroll.wheel(delta, LAYER_ROW, top, bottom, content),
Page::Presets => state
.preset_scroll
.wheel(delta, PRESET_ROW, top, bottom, content),
}
}
pub(super) fn drag(state: &mut State, cursor: (f32, f32), size: (f32, f32)) {
let (top, bottom, _, content) = list_area(state, size);
match state.page {
Page::Layers => state.scroll.drag(cursor.1, top, bottom, content),
Page::Presets => state.preset_scroll.drag(cursor.1, top, bottom, content),
}
}
pub(super) fn release(state: &mut State) {
state.scroll.release();
state.preset_scroll.release();
}
pub(super) fn draw(
title: &Title,
atlas: &HudAtlas,
batch: &mut HudBatch,
cursor: (f32, f32),
state: &State,
) {
let (w, h) = (batch.width.ceil(), batch.height.ceil());
ui::menu_background(batch, atlas, 0.0, 0.0, w, h, false);
match state.page {
Page::Layers => draw_layers(title, atlas, batch, cursor, state, (w, h)),
Page::Presets => draw_presets(title, atlas, batch, cursor, state, (w, h)),
}
}
fn draw_layers(
_title: &Title,
atlas: &HudAtlas,
batch: &mut HudBatch,
cursor: (f32, f32),
state: &State,
(w, h): (f32, f32),
) {
super::centered(
atlas,
batch,
&lang::get_or(
"createWorld.customize.flat.title",
"Superflat Customization",
),
8.0,
u32::MAX,
);
let (top, bottom, left, content) = list_area(state, (w, h));
ui::list_background(batch, atlas, top, bottom, false);
crate::ui::separators(batch, atlas, top, bottom, false);
let first = batch.vertices.len();
let right = left + ROW_WIDTH;
batch.text_styled(
atlas,
&Text::translate("createWorld.customize.flat.tile", &[]).underlined(),
left + 2.0,
top + 3.0,
u32::MAX,
true,
);
let height_header = Text::translate("createWorld.customize.flat.height", &[]).underlined();
let hw = HudBatch::text_width(atlas, &height_header.plain());
batch.text_styled(
atlas,
&height_header,
right - hw - 2.0,
top + 3.0,
u32::MAX,
true,
);
let offset = state.scroll.amount.trunc();
let selected = state.selected;
for (row, layer) in state.settings.layers.iter().rev().enumerate() {
let y = top + 2.0 + HEADER + row as f32 * LAYER_ROW - offset;
let target = [left - 2.0, y - 1.0, ROW_WIDTH + 4.0, LAYER_ROW];
let hovered = ui::hovered(cursor, target[0], target[1], target[2], target[3]);
if selected == Some(row) {
batch.fill(
atlas,
target[0],
target[1],
target[2],
target[3],
rgba(255, 255, 255, 70),
);
} else if hovered {
batch.fill(
atlas,
target[0],
target[1],
target[2],
target[3],
rgba(255, 255, 255, 25),
);
}
slot(
atlas,
batch,
layer_icon(atlas, &layer.block),
left + 2.0,
y + 1.0,
);
batch.text(
atlas,
&item_name(&layer.block),
left + 2.0 + 18.0 + 5.0,
y + 5.0,
u32::MAX,
true,
);
let (key, fallback) = if row == 0 {
("createWorld.customize.flat.layer.top", "Top - %s")
} else if row + 1 == state.settings.layers.len() {
("createWorld.customize.flat.layer.bottom", "Bottom - %s")
} else {
("createWorld.customize.flat.layer", "%s")
};
let label = format_translation(
&lang::get_or(key, fallback),
&[Text::literal(layer.height.to_string())],
)
.plain();
let text_width = HudBatch::text_width(atlas, &label);
batch.text(
atlas,
&label,
right - text_width - 2.0,
y + 5.0,
u32::MAX,
true,
);
}
batch.clip_since(first, 0.0, top, w, bottom);
ui::scrollbar(
batch,
atlas,
left + ROW_WIDTH + 6.0,
top,
bottom,
content,
state.scroll.amount,
);
}
fn draw_presets(
title: &Title,
atlas: &HudAtlas,
batch: &mut HudBatch,
cursor: (f32, f32),
state: &State,
(w, h): (f32, f32),
) {
super::centered(
atlas,
batch,
&lang::get_or("createWorld.customize.presets.title", "Select a Preset"),
8.0,
u32::MAX,
);
batch.text(
atlas,
&lang::get_or(
"createWorld.customize.presets.share",
"Want to share your preset with someone? Use the box below!",
),
51.0,
30.0,
rgba(160, 160, 160, 255),
true,
);
// The share box: EditBox at 50,40, width - 100 by 20.
let field = share_field((w, h));
title.caret.borrow_mut().draw(
batch,
atlas,
(super::FLAT_SHARE, &state.share, 1230),
field,
state.share_focused,
title.started,
);
batch.text(
atlas,
&lang::get_or(
"createWorld.customize.presets.list",
"Alternatively, here are some we made earlier!",
),
51.0,
68.0,
rgba(160, 160, 160, 255),
true,
);
let (top, bottom, left, content) = list_area(state, (w, h));
ui::list_background(batch, atlas, top, bottom, false);
crate::ui::separators(batch, atlas, top, bottom, false);
let first = batch.vertices.len();
let offset = state.preset_scroll.amount.trunc();
for (row, preset) in state.presets.iter().enumerate() {
let y = top + 2.0 + row as f32 * PRESET_ROW - offset;
let target = [left - 2.0, y - 1.0, ROW_WIDTH + 4.0, PRESET_ROW];
let hovered = ui::hovered(cursor, target[0], target[1], target[2], target[3]);
if state.preset_selected == Some(row) {
batch.fill(
atlas,
target[0],
target[1],
target[2],
target[3],
rgba(255, 255, 255, 70),
);
} else if hovered {
batch.fill(
atlas,
target[0],
target[1],
target[2],
target[3],
rgba(255, 255, 255, 25),
);
}
slot(
atlas,
batch,
layer_icon(atlas, &preset.display),
left + 2.0,
y + 2.0,
);
batch.text(
atlas,
&preset_name(&preset.id),
left + 2.0 + 18.0 + 5.0,
y + 7.0,
u32::MAX,
true,
);
}
batch.clip_since(first, 0.0, top, w, bottom);
ui::scrollbar(
batch,
atlas,
left + ROW_WIDTH + 6.0,
top,
bottom,
content,
state.preset_scroll.amount,
);
}
/// The 18x18 slot behind a layer's item icon (`container/slot`).
fn slot(atlas: &HudAtlas, batch: &mut HudBatch, icon: Option<String>, x: f32, y: f32) {
if let Some(rect) = atlas.sprites.get("gui/sprites/container/slot").copied() {
batch.rect_uv(x, y, 18.0, 18.0, rect, u32::MAX);
} else {
batch.fill(atlas, x, y, 18.0, 18.0, rgba(139, 139, 139, 255));
batch.fill(atlas, x + 1.0, y + 1.0, 16.0, 16.0, rgba(55, 55, 55, 255));
}
if let Some(icon) = icon {
batch.sprite_scaled(atlas, &icon, x + 1.0, y + 1.0, 16.0, 16.0, u32::MAX);
}
}
fn layer_icon(atlas: &HudAtlas, id: &str) -> Option<String> {
let name = id.strip_prefix("minecraft:").unwrap_or(id);
let candidates: Vec<String> = if name == "water" {
vec!["item/water_bucket".into()]
} else if name == "lava" {
vec!["item/lava_bucket".into()]
} else {
vec![
format!("item/{name}"),
format!("block/{name}"),
format!("block/{name}_side"),
format!("block/{name}_top"),
]
};
candidates
.into_iter()
.find(|sprite| atlas.sprites.contains_key(sprite))
}
fn item_name(id: &str) -> String {
let name = id.strip_prefix("minecraft:").unwrap_or(id);
if name == "water" {
return lang::get_or("item.minecraft.water_bucket", "Water Bucket");
}
if name == "lava" {
return lang::get_or("item.minecraft.lava_bucket", "Lava Bucket");
}
for key in [
format!("block.minecraft.{name}"),
format!("item.minecraft.{name}"),
] {
if lang::has(&key) {
return lang::get(&key);
}
}
pretty(name)
}
fn preset_name(id: &str) -> String {
let key = format!("flat_world_preset.{}", id.replace(':', "."));
lang::get_or(&key, &pretty(id.strip_prefix("minecraft:").unwrap_or(id)))
}
fn pretty(name: &str) -> String {
name.split('_')
.filter(|word| !word.is_empty())
.map(|word| {
let mut chars = word.chars();
match chars.next() {
Some(first) => first.to_uppercase().chain(chars).collect::<String>(),
None => String::new(),
}
})
.collect::<Vec<_>>()
.join(" ")
}