Merge pull request #483 from ludovic111/fix/world-options-game-rules
World Options: Edit Game Rules (InWorldGameRulesScreen)
This commit is contained in:
@@ -48,6 +48,7 @@ mod frames;
|
||||
mod friends;
|
||||
mod functions;
|
||||
mod game_mode;
|
||||
mod game_rules_screen;
|
||||
mod gamemode_switcher;
|
||||
mod ghasts;
|
||||
mod golems;
|
||||
|
||||
@@ -428,6 +428,9 @@ impl Game<'_> {
|
||||
Screen::Chat => self.chat_type(text),
|
||||
Screen::CommandBlock => self.command_block_type(text),
|
||||
Screen::Options => self.ui.options.type_text(&self.res.atlas, text),
|
||||
// The port, or the game rules' search and number boxes.
|
||||
Screen::WorldOptions if text == "\\b" => self.world_options_text(None),
|
||||
Screen::WorldOptions => self.world_options_text(Some(text)),
|
||||
Screen::Inventory if self.recipe_search_focused() => {
|
||||
self.recipe_text(Some(text), false)
|
||||
}
|
||||
@@ -437,7 +440,7 @@ impl Game<'_> {
|
||||
}
|
||||
_ => {
|
||||
return Err(
|
||||
"type needs the chat screen (key t tap), the language search or the creative search".into(),
|
||||
"type needs the chat screen (key t tap), the language search, the creative search or World Options".into(),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,593 @@
|
||||
//! InWorldGameRulesScreen (AbstractGameRulesScreen), opened from World
|
||||
//! Options: the rules by category under a search box, a switch or a number
|
||||
//! box each, and Done sending the changed ones (ServerboundSetGameRulePacket).
|
||||
//! Leaving with changes asks first (ConfirmScreen).
|
||||
use horizon_client::{
|
||||
font::Font,
|
||||
gamerules::{self, GameRules, RULES, Rule, Value},
|
||||
hud::{HudAtlas, HudBatch},
|
||||
lang,
|
||||
text::{Style, Text, named_color},
|
||||
ui::{self, EditBox},
|
||||
};
|
||||
|
||||
/// HeaderAndFooterLayout(12 + 9 + 15, 33): the title over the search box.
|
||||
const HEADER: f32 = 36.0;
|
||||
const FOOTER: f32 = 33.0;
|
||||
/// RuleList: 24 pixel entries in AbstractSelectionList's 220 pixel rows.
|
||||
const ROW: f32 = 24.0;
|
||||
const ROW_WIDTH: f32 = 220.0;
|
||||
const SEARCH_WIDTH: f32 = 200.0;
|
||||
/// EditBox text colours (`hud::rgba` order, ABGR): IntegerRuleEntry's valid
|
||||
/// (-2039584) and invalid (-65536).
|
||||
const VALID: u32 = 0xFFE0_E0E0;
|
||||
const INVALID: u32 = 0xFF00_00FF;
|
||||
|
||||
#[derive(Clone, Copy)]
|
||||
enum Row {
|
||||
Category(&'static str),
|
||||
Rule(&'static Rule),
|
||||
}
|
||||
|
||||
/// What a click or Escape asks of World Options.
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(super) enum Exit {
|
||||
/// Done: the rules that changed.
|
||||
Apply(Vec<(&'static str, Value)>),
|
||||
/// Cancel, or discarding the changes.
|
||||
Discard,
|
||||
}
|
||||
|
||||
pub(super) struct RulesScreen {
|
||||
search: EditBox,
|
||||
values: GameRules,
|
||||
initial: GameRules,
|
||||
/// IntegerRuleEntry's boxes, by rule.
|
||||
numbers: Vec<(&'static str, EditBox)>,
|
||||
scroll: f32,
|
||||
/// ConfirmScreen "Game Rule Changes" in its place.
|
||||
confirm: bool,
|
||||
}
|
||||
|
||||
fn name_key(r: &Rule) -> String {
|
||||
format!("gamerule.minecraft.{}", r.name)
|
||||
}
|
||||
fn category_key(category: &str) -> String {
|
||||
format!("gamerule.category.minecraft.{category}")
|
||||
}
|
||||
/// GameRule.deserialize of an integer rule: Brigadier's readInt (digits,
|
||||
/// '.' and '-' only, nothing after), then the rule's range.
|
||||
fn parse(r: &Rule, s: &str) -> Option<i32> {
|
||||
let number = !s.is_empty()
|
||||
&& s.chars()
|
||||
.all(|c| c.is_ascii_digit() || c == '-' || c == '.');
|
||||
number
|
||||
.then(|| s.parse::<i32>().ok())
|
||||
.flatten()
|
||||
.filter(|v| (r.min..=r.max).contains(v))
|
||||
}
|
||||
fn widest(font: &Font, lines: &[Text]) -> f32 {
|
||||
lines.iter().map(|l| font.width_text(l)).fold(0.0, f32::max)
|
||||
}
|
||||
fn lines(font: &Font, t: &Text, width: f32) -> Vec<Text> {
|
||||
font.split_lines(t, width)
|
||||
.into_iter()
|
||||
.map(|(line, _)| line)
|
||||
.collect()
|
||||
}
|
||||
fn row_left(w: f32) -> f32 {
|
||||
(w / 2.0).trunc() - ROW_WIDTH / 2.0
|
||||
}
|
||||
/// A rule's switch or box: 44 by 20, 45 from the entry content's right.
|
||||
fn control_x(w: f32) -> f32 {
|
||||
row_left(w) + 2.0 + (ROW_WIDTH - 4.0) - 45.0
|
||||
}
|
||||
/// The header's LinearLayout (title, 4, search box) centred in its 36 pixels.
|
||||
fn search_at(w: f32) -> (f32, f32) {
|
||||
(((w - SEARCH_WIDTH) / 2.0).trunc(), 4.0 + 9.0 + 4.0)
|
||||
}
|
||||
/// The footer's Done and Cancel, 150 wide and 8 apart.
|
||||
fn footer(w: f32, h: f32) -> [(f32, f32); 2] {
|
||||
let x = ((w - 308.0) / 2.0).trunc();
|
||||
[(x, h - FOOTER + 6.0), (x + 158.0, h - FOOTER + 6.0)]
|
||||
}
|
||||
fn yellow() -> u32 {
|
||||
named_color("yellow").unwrap_or(0xFFFF55)
|
||||
}
|
||||
|
||||
/// ConfirmScreen: the title, the message (at most `width - 50` wide), and
|
||||
/// Yes / No 16 below, 4 apart, all centred.
|
||||
struct Confirm {
|
||||
lines: Vec<(Text, f32, f32)>,
|
||||
buttons: [(f32, f32); 2],
|
||||
}
|
||||
impl Confirm {
|
||||
fn new(font: &Font, (w, h): (f32, f32)) -> Self {
|
||||
let title = Text::translate("editGamerule.inGame.discardChanges.title", &[]);
|
||||
let message = Text::translate("editGamerule.inGame.discardChanges.message", &[]);
|
||||
let message: Vec<Text> = lines(font, &message, w - 50.0)
|
||||
.into_iter()
|
||||
.take(15)
|
||||
.collect();
|
||||
let (tw, mw) = (font.width_text(&title), widest(font, &message));
|
||||
let mh = 9.0 * message.len() as f32;
|
||||
let col = tw.max(mw).max(304.0);
|
||||
let total = 9.0 + 8.0 + mh + 8.0 + 36.0;
|
||||
let (lx, ly) = (((w - col) / 2.0).trunc(), ((h - total) / 2.0).trunc());
|
||||
let cell = |cw: f32| lx + ((col - cw) / 2.0).trunc();
|
||||
let mid = cell(mw) + (mw / 2.0).trunc();
|
||||
let mut out = vec![(title, cell(tw), ly)];
|
||||
for (i, line) in message.into_iter().enumerate() {
|
||||
let x = mid - (font.width_text(&line) / 2.0).trunc();
|
||||
out.push((line, x, ly + 17.0 + 9.0 * i as f32));
|
||||
}
|
||||
let (bx, by) = (cell(304.0), ly + 9.0 + 8.0 + mh + 8.0 + 16.0);
|
||||
Self {
|
||||
lines: out,
|
||||
buttons: [(bx, by), (bx + 154.0, by)],
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RulesScreen {
|
||||
pub(super) fn new(rules: &GameRules) -> Self {
|
||||
// setInitialFocus: the search box.
|
||||
let search = EditBox::new(0.0, 0.0, SEARCH_WIDTH, 15.0);
|
||||
let numbers = RULES
|
||||
.iter()
|
||||
.filter(|r| matches!(r.default, Value::Int(_)))
|
||||
.map(|r| {
|
||||
let mut b = EditBox::new(0.0, 0.0, 44.0, 20.0);
|
||||
b.focused = false;
|
||||
b.set_value(&rules.int(r.name).to_string());
|
||||
b.text_color = VALID;
|
||||
(r.name, b)
|
||||
})
|
||||
.collect();
|
||||
Self {
|
||||
search,
|
||||
values: rules.clone(),
|
||||
initial: rules.clone(),
|
||||
numbers,
|
||||
scroll: 0.0,
|
||||
confirm: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// RuleList.populateChildren: the rules matching the search (id, name,
|
||||
/// category or description), by category, each sorted by description id.
|
||||
fn rows(&self) -> Vec<Row> {
|
||||
let filter = self.search.value.to_lowercase();
|
||||
let has = |s: &str| s.to_lowercase().contains(&filter);
|
||||
let mut categories: Vec<(&'static str, Vec<&'static Rule>)> = Vec::new();
|
||||
for r in RULES {
|
||||
let key = name_key(r);
|
||||
let description = format!("{key}.description");
|
||||
let matches = has(r.name)
|
||||
|| has(&lang::get(&key))
|
||||
|| has(&lang::get(&category_key(r.category)))
|
||||
|| (lang::has(&description) && has(&lang::get(&description)));
|
||||
if !matches {
|
||||
continue;
|
||||
}
|
||||
match categories.iter_mut().find(|(c, _)| *c == r.category) {
|
||||
Some((_, rules)) => rules.push(r),
|
||||
None => categories.push((r.category, vec![r])),
|
||||
}
|
||||
}
|
||||
// By the categories' ids, then the rules' description ids.
|
||||
categories.sort_by_key(|(c, _)| *c);
|
||||
categories
|
||||
.into_iter()
|
||||
.flat_map(|(c, mut rules)| {
|
||||
rules.sort_by_key(|r| name_key(r));
|
||||
std::iter::once(Row::Category(c)).chain(rules.into_iter().map(Row::Rule))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
fn max_scroll(rows: usize, h: f32) -> f32 {
|
||||
(ROW * rows as f32 + 4.0 - (h - HEADER - FOOTER)).max(0.0)
|
||||
}
|
||||
/// Entry `i`'s top: 2 under the list's top, less the scroll.
|
||||
fn row_top(&self, i: usize) -> f32 {
|
||||
HEADER + 2.0 - self.scroll.trunc() + ROW * i as f32
|
||||
}
|
||||
/// getEntryAtPosition: the row under the pointer, inside the list.
|
||||
fn hovered(&self, rows: usize, (x, y): (f32, f32), (w, h): (f32, f32)) -> Option<usize> {
|
||||
let left = row_left(w);
|
||||
let inside = y >= HEADER && y < h - FOOTER && x >= left && x < left + ROW_WIDTH;
|
||||
let i = ((y - self.row_top(0)) / ROW).floor();
|
||||
(inside && i >= 0.0 && (i as usize) < rows).then_some(i as usize)
|
||||
}
|
||||
fn number(&mut self, name: &str) -> Option<&mut EditBox> {
|
||||
self.numbers
|
||||
.iter_mut()
|
||||
.find(|(n, _)| *n == name)
|
||||
.map(|(_, b)| b)
|
||||
}
|
||||
/// An invalid number disables Done (markInvalid).
|
||||
fn invalid(&self) -> bool {
|
||||
self.numbers
|
||||
.iter()
|
||||
.any(|(name, b)| gamerules::rule(name).is_none_or(|r| parse(r, &b.value).is_none()))
|
||||
}
|
||||
fn changes(&self) -> Vec<(&'static str, Value)> {
|
||||
RULES
|
||||
.iter()
|
||||
.filter_map(|r| {
|
||||
let value = self.values.get(r.name)?;
|
||||
(Some(value) != self.initial.get(r.name)).then_some((r.name, value))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
/// InWorldGameRulesScreen.onClose: pending changes ask first.
|
||||
fn close(&mut self) -> Option<Exit> {
|
||||
if self.changes().is_empty() {
|
||||
Some(Exit::Discard)
|
||||
} else {
|
||||
self.confirm = true;
|
||||
None
|
||||
}
|
||||
}
|
||||
/// Escape: ConfirmScreen answers no; the rules close.
|
||||
pub(super) fn escape(&mut self) -> Option<Exit> {
|
||||
if self.confirm {
|
||||
self.confirm = false;
|
||||
None
|
||||
} else {
|
||||
self.close()
|
||||
}
|
||||
}
|
||||
/// The mouse wheel (AbstractSelectionList: half an entry a step).
|
||||
pub(super) fn wheel(&mut self, delta: f32, h: f32) {
|
||||
if !self.confirm {
|
||||
let max = Self::max_scroll(self.rows().len(), h);
|
||||
self.scroll = (self.scroll - delta * ROW / 2.0).clamp(0.0, max);
|
||||
}
|
||||
}
|
||||
fn focused(&mut self) -> Option<(Option<&'static str>, &mut EditBox)> {
|
||||
if self.search.focused {
|
||||
return Some((None, &mut self.search));
|
||||
}
|
||||
self.numbers
|
||||
.iter_mut()
|
||||
.find(|(_, b)| b.focused)
|
||||
.map(|(n, b)| (Some(*n), b))
|
||||
}
|
||||
/// The focused box changed: the search filters again from the top; a
|
||||
/// number sets its rule when valid (IntegerRuleEntry's responder).
|
||||
fn edited(&mut self, rule: Option<&'static str>) {
|
||||
let Some(name) = rule else {
|
||||
self.scroll = 0.0;
|
||||
return;
|
||||
};
|
||||
let Some(r) = gamerules::rule(name) else {
|
||||
return;
|
||||
};
|
||||
let value = self.number(name).map(|b| parse(r, &b.value));
|
||||
if let Some(Some(v)) = value {
|
||||
self.values.set(name, Value::Int(v));
|
||||
}
|
||||
if let Some(b) = self.number(name) {
|
||||
b.text_color = if matches!(value, Some(Some(_))) {
|
||||
VALID
|
||||
} else {
|
||||
INVALID
|
||||
};
|
||||
}
|
||||
}
|
||||
/// Run an edit on the focused box, then its responder.
|
||||
pub(super) fn edit(&mut self, f: impl FnOnce(&mut EditBox)) {
|
||||
let Some((rule, b)) = self.focused() else {
|
||||
return;
|
||||
};
|
||||
let before = b.value.clone();
|
||||
f(b);
|
||||
if b.value != before {
|
||||
self.edited(rule);
|
||||
}
|
||||
}
|
||||
pub(super) fn typing(&self) -> bool {
|
||||
!self.confirm && (self.search.focused || self.numbers.iter().any(|(_, b)| b.focused))
|
||||
}
|
||||
|
||||
/// A left click; Some when the screen closes.
|
||||
pub(super) fn click(
|
||||
&mut self,
|
||||
atlas: &HudAtlas,
|
||||
cursor: (f32, f32),
|
||||
size: (f32, f32),
|
||||
) -> Option<Exit> {
|
||||
let (w, h) = size;
|
||||
let on = |x: f32, y: f32, bw: f32, bh: f32| ui::hovered(cursor, x, y, bw, bh);
|
||||
if self.confirm {
|
||||
let c = Confirm::new(&atlas.font, size);
|
||||
let [(yx, yy), (nx, ny)] = c.buttons;
|
||||
if on(yx, yy, 150.0, 20.0) {
|
||||
return Some(Exit::Discard);
|
||||
}
|
||||
if on(nx, ny, 150.0, 20.0) {
|
||||
self.confirm = false;
|
||||
}
|
||||
return None;
|
||||
}
|
||||
let [(dx, dy), (cx, cy)] = footer(w, h);
|
||||
if on(dx, dy, 150.0, 20.0) && !self.invalid() {
|
||||
return Some(Exit::Apply(self.changes()));
|
||||
}
|
||||
if on(cx, cy, 150.0, 20.0) {
|
||||
return self.close();
|
||||
}
|
||||
// A click moves the focus to the widget under it.
|
||||
let (sx, sy) = search_at(w);
|
||||
if on(sx, sy, SEARCH_WIDTH, 15.0) {
|
||||
self.unfocus();
|
||||
self.search.focused = true;
|
||||
self.search.reset_blink();
|
||||
self.search.click(atlas, cursor.0, false, false);
|
||||
return None;
|
||||
}
|
||||
let rows = self.rows();
|
||||
let i = self.hovered(rows.len(), cursor, size)?;
|
||||
let Row::Rule(r) = rows[i] else {
|
||||
return None;
|
||||
};
|
||||
if !on(control_x(w), self.row_top(i) + 2.0, 44.0, 20.0) {
|
||||
return None;
|
||||
}
|
||||
self.unfocus();
|
||||
match r.default {
|
||||
Value::Bool(_) => {
|
||||
let on = self.values.bool(r.name);
|
||||
self.values.set(r.name, Value::Bool(!on));
|
||||
}
|
||||
Value::Int(_) => {
|
||||
if let Some(b) = self.number(r.name) {
|
||||
b.focused = true;
|
||||
b.reset_blink();
|
||||
b.click(atlas, cursor.0, false, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
fn unfocus(&mut self) {
|
||||
self.search.focused = false;
|
||||
for (_, b) in &mut self.numbers {
|
||||
b.focused = false;
|
||||
}
|
||||
}
|
||||
/// Whether a click lands on a button (for its sound).
|
||||
pub(super) fn clicks_button(
|
||||
&self,
|
||||
atlas: &HudAtlas,
|
||||
cursor: (f32, f32),
|
||||
size: (f32, f32),
|
||||
) -> bool {
|
||||
let (w, h) = size;
|
||||
let on = |(x, y): (f32, f32)| ui::hovered(cursor, x, y, 150.0, 20.0);
|
||||
if self.confirm {
|
||||
return Confirm::new(&atlas.font, size).buttons.into_iter().any(on);
|
||||
}
|
||||
let [done, cancel] = footer(w, h);
|
||||
if (on(done) && !self.invalid()) || on(cancel) {
|
||||
return true;
|
||||
}
|
||||
let rows = self.rows();
|
||||
self.hovered(rows.len(), cursor, size).is_some_and(|i| {
|
||||
matches!(rows[i], Row::Rule(r) if matches!(r.default, Value::Bool(_)))
|
||||
&& ui::hovered(cursor, control_x(w), self.row_top(i) + 2.0, 44.0, 20.0)
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn draw(&mut self, batch: &mut HudBatch, atlas: &HudAtlas, cursor: (f32, f32)) {
|
||||
let (w, h) = (batch.width, batch.height);
|
||||
ui::menu_background(batch, atlas, 0.0, 0.0, w, h, true);
|
||||
if self.confirm {
|
||||
let c = Confirm::new(&atlas.font, (w, h));
|
||||
for (line, x, y) in &c.lines {
|
||||
batch.text_styled(atlas, line, *x, *y, u32::MAX, true);
|
||||
}
|
||||
for ((bx, by), key) in c.buttons.into_iter().zip(["gui.yes", "gui.no"]) {
|
||||
let hover = ui::hovered(cursor, bx, by, 150.0, 20.0);
|
||||
let label = Text::translate(key, &[]);
|
||||
ui::button(batch, atlas, bx, by, 150.0, 20.0, &label, true, hover);
|
||||
}
|
||||
return;
|
||||
}
|
||||
let rows = self.rows();
|
||||
self.scroll = self.scroll.clamp(0.0, Self::max_scroll(rows.len(), h));
|
||||
// RuleList: the in-world list background, its entries clipped to it,
|
||||
// then the separators and scrollbar.
|
||||
ui::list_background(batch, atlas, HEADER, h - FOOTER, true);
|
||||
let first = batch.vertices.len();
|
||||
let hovered = self.hovered(rows.len(), cursor, (w, h));
|
||||
let (left, x) = (row_left(w), control_x(w));
|
||||
for (i, row) in rows.iter().enumerate() {
|
||||
let top = self.row_top(i);
|
||||
if top + ROW < HEADER || top > h - FOOTER {
|
||||
continue;
|
||||
}
|
||||
let (cx, cy) = (left + 2.0, top + 2.0);
|
||||
match *row {
|
||||
Row::Category(c) => {
|
||||
// A FocusableTextWidget padded by 4, centred in the entry.
|
||||
let label = Text::translate(&category_key(c), &[])
|
||||
.bold()
|
||||
.underlined()
|
||||
.with_color(yellow());
|
||||
let lw = atlas.font.width_text(&label);
|
||||
let mid = cx + (ROW_WIDTH - 4.0) / 2.0;
|
||||
let lx = mid - (lw / 2.0).trunc();
|
||||
batch.text_styled(atlas, &label, lx, cy + 10.0 - 8.0 + 4.0, u32::MAX, true);
|
||||
}
|
||||
Row::Rule(r) => {
|
||||
let name = lines(&atlas.font, &Text::translate(&name_key(r), &[]), 170.0);
|
||||
match name.as_slice() {
|
||||
[one] => {
|
||||
batch.text_styled(atlas, one, cx, cy + 5.0, u32::MAX, true);
|
||||
}
|
||||
[one, two, ..] => {
|
||||
batch.text_styled(atlas, one, cx, cy, u32::MAX, true);
|
||||
batch.text_styled(atlas, two, cx, cy + 10.0, u32::MAX, true);
|
||||
}
|
||||
[] => {}
|
||||
}
|
||||
let hover = hovered == Some(i) && ui::hovered(cursor, x, cy, 44.0, 20.0);
|
||||
match r.default {
|
||||
Value::Bool(_) => {
|
||||
let key = if self.values.bool(r.name) {
|
||||
"options.on"
|
||||
} else {
|
||||
"options.off"
|
||||
};
|
||||
let label = Text::translate(key, &[]);
|
||||
ui::button(batch, atlas, x, cy, 44.0, 20.0, &label, true, hover);
|
||||
}
|
||||
Value::Int(_) => {
|
||||
if let Some(b) = self.number(r.name) {
|
||||
b.x = x;
|
||||
b.y = cy;
|
||||
b.draw(batch, atlas, None);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
batch.clip_since(first, 0.0, HEADER, w, h - FOOTER);
|
||||
ui::separators(batch, atlas, HEADER, h - FOOTER, true);
|
||||
let content = ROW * rows.len() as f32 + 4.0;
|
||||
let bar_x = left + ROW_WIDTH + 6.0 + 2.0;
|
||||
ui::scrollbar(
|
||||
batch,
|
||||
atlas,
|
||||
bar_x,
|
||||
HEADER,
|
||||
h - FOOTER,
|
||||
content,
|
||||
self.scroll.trunc(),
|
||||
);
|
||||
// The header: the title centred over the search box.
|
||||
let title = Text::translate("editGamerule.title", &[]);
|
||||
let tw = atlas.font.width_text(&title);
|
||||
let (sx, sy) = search_at(w);
|
||||
let lw = tw.max(SEARCH_WIDTH);
|
||||
let lx = ((w - lw) / 2.0).trunc();
|
||||
batch.text_styled(
|
||||
atlas,
|
||||
&title,
|
||||
lx + ((lw - tw) / 2.0).trunc(),
|
||||
4.0,
|
||||
u32::MAX,
|
||||
true,
|
||||
);
|
||||
self.search.x = sx;
|
||||
self.search.y = sy;
|
||||
self.search.draw(batch, atlas, None);
|
||||
if self.search.value.is_empty() && !self.search.focused {
|
||||
// EditBox.SEARCH_HINT_STYLE: gray and italic.
|
||||
let hint = Text::translate("gui.game_rule.search", &[]).with_style(&Style {
|
||||
color: named_color("gray"),
|
||||
italic: Some(true),
|
||||
..Style::default()
|
||||
});
|
||||
let (hx, hy) = (self.search.text_x(), self.search.text_y());
|
||||
batch.text_styled(atlas, &hint, hx, hy, u32::MAX, true);
|
||||
}
|
||||
let invalid = self.invalid();
|
||||
for ((bx, by), (key, active)) in footer(w, h)
|
||||
.into_iter()
|
||||
.zip([("gui.done", !invalid), ("gui.cancel", true)])
|
||||
{
|
||||
let hover = active && ui::hovered(cursor, bx, by, 150.0, 20.0);
|
||||
let label = Text::translate(key, &[]);
|
||||
ui::button(batch, atlas, bx, by, 150.0, 20.0, &label, active, hover);
|
||||
}
|
||||
// The hovered rule's tooltip: its id, description and default.
|
||||
if let Some(Row::Rule(r)) = hovered.map(|i| rows[i]) {
|
||||
let mut tip = vec![Text::literal(r.name).with_color(yellow())];
|
||||
let description = format!("{}.description", name_key(r));
|
||||
if lang::has(&description) {
|
||||
tip.extend(lines(
|
||||
&atlas.font,
|
||||
&Text::translate(&description, &[]),
|
||||
150.0,
|
||||
));
|
||||
}
|
||||
let gray = named_color("gray").unwrap_or(0xAAAAAA);
|
||||
tip.push(
|
||||
Text::translate(
|
||||
"editGamerule.default",
|
||||
&[Text::literal(r.default.to_string())],
|
||||
)
|
||||
.with_color(gray),
|
||||
);
|
||||
ui::tooltip(batch, atlas, &tip, cursor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn box_colours_are_vanillas() {
|
||||
assert_eq!(VALID, horizon_client::hud::rgba(224, 224, 224, 255));
|
||||
assert_eq!(INVALID, horizon_client::hud::rgba(255, 0, 0, 255));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn numbers_parse_like_brigadier_within_the_rule() {
|
||||
let r = gamerules::rule("max_snow_accumulation_height").unwrap();
|
||||
assert_eq!(parse(r, "8"), Some(8));
|
||||
assert_eq!(parse(r, "9"), None);
|
||||
assert_eq!(parse(r, "-1"), None);
|
||||
for s in ["", "+3", " 3", "3 ", "3.0", "x"] {
|
||||
assert_eq!(parse(r, s), None, "{s:?}");
|
||||
}
|
||||
let r = gamerules::rule("fire_spread_radius_around_player").unwrap();
|
||||
assert_eq!(parse(r, "-1"), Some(-1));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn rows_group_categories_and_rules_by_description_id() {
|
||||
let s = RulesScreen::new(&GameRules::default());
|
||||
let rows = s.rows();
|
||||
assert_eq!(rows.len(), RULES.len() + 7);
|
||||
assert!(matches!(rows[0], Row::Category("chat")));
|
||||
let categories: Vec<_> = rows
|
||||
.iter()
|
||||
.filter_map(|r| match r {
|
||||
Row::Category(c) => Some(*c),
|
||||
Row::Rule(_) => None,
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
categories,
|
||||
[
|
||||
"chat", "drops", "misc", "mobs", "player", "spawning", "updates"
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn edits_change_rules_and_bad_numbers_hold_done() {
|
||||
let mut s = RulesScreen::new(&GameRules::default());
|
||||
assert!(s.changes().is_empty());
|
||||
s.unfocus();
|
||||
s.number("random_tick_speed").unwrap().focused = true;
|
||||
s.edit(|b| b.set_value("10"));
|
||||
assert_eq!(s.changes(), vec![("random_tick_speed", Value::Int(10))]);
|
||||
s.edit(|b| b.set_value("ten"));
|
||||
assert!(s.invalid());
|
||||
// The last valid value stays, as the responder left it.
|
||||
assert_eq!(s.changes(), vec![("random_tick_speed", Value::Int(10))]);
|
||||
// Leaving with changes asks; No returns, Yes discards.
|
||||
assert_eq!(s.escape(), None);
|
||||
assert!(s.confirm);
|
||||
assert_eq!(s.escape(), None);
|
||||
assert!(!s.confirm);
|
||||
}
|
||||
}
|
||||
@@ -4,7 +4,11 @@
|
||||
//! difficulty, LAN on/off with its port, command access for guests and
|
||||
//! forcing the game mode. Its difficulty lock asks first (PopupScreen), and
|
||||
//! Restrictions opens RestrictionsScreen.
|
||||
use super::{Game, Screen, lan::LanSettings};
|
||||
use super::{
|
||||
Game, Screen,
|
||||
game_rules_screen::{Exit, RulesScreen},
|
||||
lan::LanSettings,
|
||||
};
|
||||
use horizon_client::{
|
||||
font::Font,
|
||||
hud::{HudBatch, rgba},
|
||||
@@ -33,14 +37,18 @@ pub(super) struct WorldOptions {
|
||||
/// ScrollableLayout's scroll amount.
|
||||
scroll: f32,
|
||||
view: View,
|
||||
/// InWorldGameRulesScreen, while it is open.
|
||||
rules: Option<RulesScreen>,
|
||||
}
|
||||
/// The screen on show: the options, the lock's question over them, or the
|
||||
/// RestrictionsScreen they opened (both return to the same options).
|
||||
/// RestrictionsScreen or InWorldGameRulesScreen they opened (all return to
|
||||
/// the same options).
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
enum View {
|
||||
Options,
|
||||
LockQuestion,
|
||||
Restrictions,
|
||||
GameRules,
|
||||
}
|
||||
#[derive(Clone, PartialEq)]
|
||||
struct Snapshot {
|
||||
@@ -83,7 +91,6 @@ enum Button {
|
||||
Difficulty,
|
||||
/// DifficultyButtons.lockButton (LockIconButton).
|
||||
Lock,
|
||||
/// InWorldGameRulesScreen is not in Horizon yet.
|
||||
GameRules,
|
||||
Restrictions,
|
||||
Lan,
|
||||
@@ -229,7 +236,9 @@ impl WorldOptions {
|
||||
// DifficultyButtons.updateDifficultyButtonsState: the level's
|
||||
// lock or hardcore, not the wanted lock.
|
||||
Button::Difficulty | Button::Lock => !self.hardcore && !self.difficulty_locked,
|
||||
Button::GameRules => false,
|
||||
// updateButton from the player's permission: the Allow Commands
|
||||
// switch leaves it (updatePermissionDependentButtons(.., false)).
|
||||
Button::GameRules => !self.hardcore && self.initial.cheats,
|
||||
Button::Restrictions => true,
|
||||
Button::CommandAccess => self.lan && self.cheats,
|
||||
Button::Force => self.lan && !(self.guest_commands && self.cheats) && !self.hardcore,
|
||||
@@ -421,6 +430,7 @@ impl Game<'_> {
|
||||
locked: self.level.difficulty_locked || self.level.hardcore,
|
||||
scroll: 0.0,
|
||||
view: View::Options,
|
||||
rules: None,
|
||||
initial: Snapshot {
|
||||
default_mode: 0,
|
||||
personal_mode: 0,
|
||||
@@ -444,14 +454,40 @@ impl Game<'_> {
|
||||
}
|
||||
|
||||
/// Escape: the question and RestrictionsScreen return to the options
|
||||
/// (their onClose), which close.
|
||||
/// (their onClose), which close; the game rules ask first.
|
||||
pub(super) fn world_options_escape(&mut self) {
|
||||
match self.world_options.as_mut() {
|
||||
Some(o) if o.view == View::GameRules => {
|
||||
if let Some(exit) = o.rules.as_mut().and_then(RulesScreen::escape) {
|
||||
self.close_game_rules(exit);
|
||||
}
|
||||
}
|
||||
Some(o) if o.view != View::Options => o.view = View::Options,
|
||||
_ => self.close_world_options(),
|
||||
}
|
||||
}
|
||||
|
||||
/// InWorldGameRulesScreen's exit, back to the options. Done sends the
|
||||
/// changed rules (ServerboundSetGameRulePacket), which the server's
|
||||
/// handleSetGameRule sets for a gamemaster and tells its operators.
|
||||
fn close_game_rules(&mut self, exit: Exit) {
|
||||
if let Some(o) = self.world_options.as_mut() {
|
||||
o.view = View::Options;
|
||||
o.rules = None;
|
||||
}
|
||||
let Exit::Apply(changes) = exit else {
|
||||
return;
|
||||
};
|
||||
if !self.allow_commands() || changes.is_empty() {
|
||||
return;
|
||||
}
|
||||
for (name, value) in changes {
|
||||
self.ui.rules.set(name, value);
|
||||
self.system_message(Text::tr("commands.gamerule.set", &[&name, &value]));
|
||||
}
|
||||
self.sync_rules();
|
||||
}
|
||||
|
||||
pub(super) fn draw_world_options(&mut self, batch: &mut HudBatch) {
|
||||
let Some(mut o) = self.world_options.take() else {
|
||||
return;
|
||||
@@ -464,6 +500,11 @@ impl Game<'_> {
|
||||
self.draw_lock_question(&o, batch);
|
||||
}
|
||||
View::Restrictions => self.draw_restrictions(batch),
|
||||
View::GameRules => {
|
||||
if let Some(rules) = o.rules.as_mut() {
|
||||
rules.draw(batch, &self.res.atlas, self.cursor);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.world_options = Some(o);
|
||||
}
|
||||
@@ -639,12 +680,16 @@ impl Game<'_> {
|
||||
/// The mouse wheel over the screen (ScrollableLayout, 10 pixels a step).
|
||||
pub(super) fn world_options_scroll(&mut self, delta: f32) {
|
||||
let h = self.gui_size().1;
|
||||
if let Some(o) = self
|
||||
.world_options
|
||||
.as_mut()
|
||||
.filter(|o| o.view == View::Options)
|
||||
{
|
||||
o.scroll = (o.scroll - delta * 10.0).clamp(0.0, WorldOptions::max_scroll(h));
|
||||
match self.world_options.as_mut() {
|
||||
Some(o) if o.view == View::Options => {
|
||||
o.scroll = (o.scroll - delta * 10.0).clamp(0.0, WorldOptions::max_scroll(h));
|
||||
}
|
||||
Some(o) => {
|
||||
if let Some(rules) = o.rules.as_mut() {
|
||||
rules.wheel(delta, h);
|
||||
}
|
||||
}
|
||||
None => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -655,6 +700,20 @@ impl Game<'_> {
|
||||
let (w, h) = self.gui_size();
|
||||
match o.view {
|
||||
View::Options => {}
|
||||
View::GameRules => {
|
||||
let atlas = &self.res.atlas;
|
||||
let exit = o.rules.as_mut().and_then(|rules| {
|
||||
if rules.clicks_button(atlas, self.cursor, (w, h)) {
|
||||
self.audio.play_button_click();
|
||||
}
|
||||
rules.click(atlas, self.cursor, (w, h))
|
||||
});
|
||||
self.world_options = Some(o);
|
||||
if let Some(exit) = exit {
|
||||
self.close_game_rules(exit);
|
||||
}
|
||||
return;
|
||||
}
|
||||
View::LockQuestion => {
|
||||
let p = Popup::lock_question(&self.res.atlas.font, o.difficulty, (w, h));
|
||||
let hit = p
|
||||
@@ -725,6 +784,10 @@ impl Game<'_> {
|
||||
Some(Button::Difficulty) => o.difficulty = (o.difficulty + 1) % 4,
|
||||
Some(Button::Lock) => o.locked = !o.locked,
|
||||
Some(Button::Restrictions) => o.view = View::Restrictions,
|
||||
Some(Button::GameRules) => {
|
||||
o.rules = Some(RulesScreen::new(&self.ui.rules));
|
||||
o.view = View::GameRules;
|
||||
}
|
||||
Some(Button::Lan) => {
|
||||
o.lan = !o.lan;
|
||||
o.port.focused = false;
|
||||
@@ -739,7 +802,7 @@ impl Game<'_> {
|
||||
// A new lock asks first.
|
||||
Some(Button::Apply) if o.locked != o.initial.locked => o.view = View::LockQuestion,
|
||||
Some(Button::Apply) => return self.apply_and_close_world_options(o),
|
||||
Some(Button::GameRules) | None => {}
|
||||
None => {}
|
||||
}
|
||||
self.world_options = Some(o);
|
||||
}
|
||||
@@ -764,6 +827,21 @@ impl Game<'_> {
|
||||
let Some(o) = self.world_options.as_mut() else {
|
||||
return;
|
||||
};
|
||||
if let Some(rules) = o.rules.as_mut().filter(|r| r.typing()) {
|
||||
match shortcut {
|
||||
Some(shortcut) => rules.edit(|b| {
|
||||
b.shortcut(shortcut, &mut self.clipboard);
|
||||
}),
|
||||
None => {
|
||||
let mut handled = false;
|
||||
rules.edit(|b| handled = b.key(key, ctrl, false));
|
||||
if !handled && !ctrl {
|
||||
self.world_options_text(event.text.as_deref());
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
if !o.port.focused || o.view != View::Options {
|
||||
return;
|
||||
}
|
||||
@@ -778,6 +856,25 @@ impl Game<'_> {
|
||||
/// Digits typed into the port field (the keyboard, or a controller's
|
||||
/// on-screen keyboard), and its backspace.
|
||||
pub(super) fn world_options_text(&mut self, text: Option<&str>) {
|
||||
if let Some(rules) = self
|
||||
.world_options
|
||||
.as_mut()
|
||||
.and_then(|o| o.rules.as_mut())
|
||||
.filter(|r| r.typing())
|
||||
{
|
||||
match text {
|
||||
Some(text) => {
|
||||
let typed: String = text.chars().filter(|c| !c.is_control()).collect();
|
||||
if !typed.is_empty() {
|
||||
rules.edit(|b| b.insert_text(&typed));
|
||||
}
|
||||
}
|
||||
None => rules.edit(|b| {
|
||||
b.key(winit::keyboard::KeyCode::Backspace, false, false);
|
||||
}),
|
||||
}
|
||||
return;
|
||||
}
|
||||
let Some(o) = self.world_options.as_mut().filter(|o| o.port.focused) else {
|
||||
return;
|
||||
};
|
||||
@@ -853,6 +950,7 @@ mod tests {
|
||||
locked: hardcore,
|
||||
scroll: 0.0,
|
||||
view: View::Options,
|
||||
rules: None,
|
||||
initial: Snapshot {
|
||||
default_mode: 0,
|
||||
personal_mode: 0,
|
||||
@@ -901,8 +999,15 @@ mod tests {
|
||||
assert!(o.active(Button::DefaultMode) && o.active(Button::Difficulty));
|
||||
assert!(!o.active(Button::CommandAccess) && !o.active(Button::Force));
|
||||
assert!(!o.active(Button::Apply));
|
||||
let o = options(false, false);
|
||||
assert!(o.active(Button::GameRules));
|
||||
let mut o = options(false, false);
|
||||
assert!(!o.active(Button::DefaultMode) && !o.active(Button::PersonalMode));
|
||||
// Game Rules follows the commands the player has, not the switch.
|
||||
assert!(!o.active(Button::GameRules));
|
||||
o.cheats = true;
|
||||
assert!(!o.active(Button::GameRules));
|
||||
let o = options(true, true);
|
||||
assert!(!o.active(Button::GameRules));
|
||||
let o = options(false, true);
|
||||
assert!(!o.active(Button::Cheats) && !o.active(Button::Difficulty));
|
||||
assert!(!o.active(Button::Force));
|
||||
|
||||
Reference in New Issue
Block a user