Part of #41: the comments of `benilla-ui` (the Lua UI engine, its FrameXML loader and its widgets) are rewritten to the rule in `docs/METHOD.md`, which is what the code does and the reference fact behind it, in a line. 43,140 comment lines become 15,161. - **The mechanical strip.** The citations that were only a private record number go first, and each edit is shown to be a pure removal. - **Wrong facts corrected.** The rewrite corrects the comments that stated wrong facts. Among them: - `0x64a480` is the case-sensitive compare (a byte `repz cmpsb`), and `0x64a4c0` the case-insensitive one. - A frame's id sits at `+0xb0`, not `+0xb4`. `EnableKeyboard`/`IsKeyboardEnabled` are `0x776ec0`/`0x776f90`. - A region's unpinned edge has no fallback to its owner's rect: the owner supplies only the scale. - `$parent` expands to the nearest named ancestor, or "Top" when none is named (`0x76c5b0`), not simply to the parent. - A StatusBar fills from its internal normalised fraction, not from `GetValue()`. - The addresses of `StartDuel` and `StartDuelUnit` were swapped. - Many "untraced" notes now give the traced reference fact: the drag threshold, the per-grip sizing edges, the empty-`autoFocus` skip, and `SetTexCoordModifiesRect`'s default. - Stock FrameXML line citations are checked against the stock 1.12 files and corrected. `ref-` file names become the stock file names. - **Deviation labels.** "Deviation:" now marks only a choice with a reason. A surface that exists only for the retired Era addon target says so plainly: it is not 1.12. - **Verification.** Every file strips to identical code, and `cargo fmt --check` is clean. Nine second readers compared every file old against new, restored the facts the rewrite had dropped, and checked each new citation against the code, the stock files, vmangos and the reference. Behaviour differences the rewrite surfaced are filed as their own issues (#131–#142), and more are being validated. The comments here state what the code does. String literals are untouched; they are #92. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
202 lines
6.5 KiB
Rust
202 lines
6.5 KiB
Rust
//! The `ItemTextFrame` Lua surface (letters, books, plaques) over the read session the app owns:
|
|
//! getters over the pushed [`ItemTextState`], and the close and page-turn intents. The app fires
|
|
//! `ITEM_TEXT_BEGIN` then `ITEM_TEXT_READY` (`ItemTextFrame.lua:11`, `:38`), and `ITEM_TEXT_CLOSED`
|
|
//! after `CloseItemText()`, the frame's OnHide (`ItemTextFrame.xml:349`).
|
|
|
|
use mlua::{Lua, Value};
|
|
|
|
use super::Model;
|
|
|
|
/// The open read session as Lua sees it, pushed whole by the app.
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub struct ItemTextState {
|
|
/// `ItemTextGetItem()`: the item's name, the window title.
|
|
pub item: String,
|
|
/// `ItemTextGetCreator()`: `ITEM_FIELD_CREATOR`'s name, `None` for an authorless text.
|
|
pub creator: Option<String>,
|
|
/// `ItemTextGetText()`: the current page's text.
|
|
pub text: String,
|
|
/// The 1-based page (`ItemTextGetPage()`); letters are always page 1.
|
|
pub page: u32,
|
|
/// Whether a next page exists (`ItemTextHasNextPage()`); always `false` for letters.
|
|
pub has_next: bool,
|
|
/// `ItemTextGetMaterial()`, e.g. "Stone"; `None` reads nil, which the frame shows as
|
|
/// parchment (`ItemTextFrame.lua:19-21`).
|
|
pub material: Option<String>,
|
|
}
|
|
|
|
impl super::UiScript {
|
|
/// Push the open read session, or clear it with `None`.
|
|
pub fn set_item_text(&mut self, state: Option<ItemTextState>) {
|
|
self.model_mut().item_text = state;
|
|
}
|
|
|
|
/// Whether `CloseItemText()` was called since the last drain (and clear the flag).
|
|
pub fn take_item_text_close(&mut self) -> bool {
|
|
std::mem::take(&mut self.model_mut().item_text_close)
|
|
}
|
|
|
|
/// Drain the `ItemTextPrevPage()`/`ItemTextNextPage()` turns (`-1`/`+1`, in click order).
|
|
pub fn take_item_text_page_turns(&mut self) -> Vec<i32> {
|
|
std::mem::take(&mut self.model_mut().item_text_page_turns)
|
|
}
|
|
}
|
|
|
|
/// Register the item-text globals.
|
|
pub(super) fn install(lua: &Lua) -> mlua::Result<()> {
|
|
let g = lua.globals();
|
|
|
|
g.set(
|
|
"ItemTextGetItem",
|
|
lua.create_function(|lua, ()| {
|
|
let model = lua.app_data_ref::<Model>().expect("model app_data");
|
|
Ok(model
|
|
.item_text
|
|
.as_ref()
|
|
.map(|s| s.item.clone())
|
|
.unwrap_or_default())
|
|
})?,
|
|
)?;
|
|
|
|
g.set(
|
|
"ItemTextGetCreator",
|
|
lua.create_function(|lua, ()| {
|
|
let model = lua.app_data_ref::<Model>().expect("model app_data");
|
|
Ok(
|
|
match model.item_text.as_ref().and_then(|s| s.creator.clone()) {
|
|
Some(c) => Value::String(lua.create_string(&c)?),
|
|
None => Value::Nil,
|
|
},
|
|
)
|
|
})?,
|
|
)?;
|
|
|
|
g.set(
|
|
"ItemTextGetText",
|
|
lua.create_function(|lua, ()| {
|
|
let model = lua.app_data_ref::<Model>().expect("model app_data");
|
|
Ok(model
|
|
.item_text
|
|
.as_ref()
|
|
.map(|s| s.text.clone())
|
|
.unwrap_or_default())
|
|
})?,
|
|
)?;
|
|
|
|
g.set(
|
|
"ItemTextGetPage",
|
|
lua.create_function(|lua, ()| {
|
|
let model = lua.app_data_ref::<Model>().expect("model app_data");
|
|
Ok(model.item_text.as_ref().map_or(1, |s| i64::from(s.page)))
|
|
})?,
|
|
)?;
|
|
|
|
g.set(
|
|
"ItemTextHasNextPage",
|
|
lua.create_function(|lua, ()| {
|
|
let model = lua.app_data_ref::<Model>().expect("model app_data");
|
|
Ok(match model.item_text.as_ref().is_some_and(|s| s.has_next) {
|
|
true => Value::Integer(1),
|
|
false => Value::Nil,
|
|
})
|
|
})?,
|
|
)?;
|
|
|
|
g.set(
|
|
"ItemTextGetMaterial",
|
|
lua.create_function(|lua, ()| {
|
|
let model = lua.app_data_ref::<Model>().expect("model app_data");
|
|
Ok(
|
|
match model.item_text.as_ref().and_then(|s| s.material.clone()) {
|
|
Some(m) => Value::String(lua.create_string(&m)?),
|
|
None => Value::Nil,
|
|
},
|
|
)
|
|
})?,
|
|
)?;
|
|
|
|
g.set(
|
|
"CloseItemText",
|
|
lua.create_function(|lua, ()| {
|
|
lua.app_data_mut::<Model>()
|
|
.expect("model app_data")
|
|
.item_text_close = true;
|
|
Ok(())
|
|
})?,
|
|
)?;
|
|
|
|
for (name, delta) in [("ItemTextPrevPage", -1i32), ("ItemTextNextPage", 1)] {
|
|
g.set(
|
|
name,
|
|
lua.create_function(move |lua, ()| {
|
|
lua.app_data_mut::<Model>()
|
|
.expect("model app_data")
|
|
.item_text_page_turns
|
|
.push(delta);
|
|
Ok(())
|
|
})?,
|
|
)?;
|
|
}
|
|
|
|
Ok(())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use crate::script::UiScript;
|
|
|
|
fn state() -> ItemTextState {
|
|
ItemTextState {
|
|
item: "Plain Letter".into(),
|
|
creator: Some("One".into()),
|
|
text: "asd".into(),
|
|
page: 1,
|
|
has_next: false,
|
|
material: None,
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn getters_mirror_the_pushed_state() {
|
|
let mut s = UiScript::new().unwrap();
|
|
// No session: empty defaults; the frame reads these only while shown.
|
|
assert_eq!(s.eval::<String>("return ItemTextGetItem()").unwrap(), "");
|
|
assert!(s
|
|
.eval::<bool>("return ItemTextGetCreator() == nil")
|
|
.unwrap());
|
|
|
|
s.set_item_text(Some(state()));
|
|
assert_eq!(
|
|
s.eval::<String>("return ItemTextGetItem()").unwrap(),
|
|
"Plain Letter"
|
|
);
|
|
assert_eq!(
|
|
s.eval::<String>("return ItemTextGetCreator()").unwrap(),
|
|
"One"
|
|
);
|
|
assert_eq!(s.eval::<String>("return ItemTextGetText()").unwrap(), "asd");
|
|
assert_eq!(s.eval::<i64>("return ItemTextGetPage()").unwrap(), 1);
|
|
assert!(s
|
|
.eval::<bool>("return ItemTextHasNextPage() == nil")
|
|
.unwrap());
|
|
assert!(s
|
|
.eval::<bool>("return ItemTextGetMaterial() == nil")
|
|
.unwrap());
|
|
}
|
|
|
|
#[test]
|
|
fn close_and_page_intents_queue_and_drain() {
|
|
let mut s = UiScript::new().unwrap();
|
|
s.set_item_text(Some(state()));
|
|
assert!(!s.take_item_text_close());
|
|
s.run("CloseItemText()").unwrap();
|
|
assert!(s.take_item_text_close());
|
|
assert!(!s.take_item_text_close(), "drained");
|
|
|
|
s.run("ItemTextNextPage() ItemTextPrevPage()").unwrap();
|
|
assert_eq!(s.take_item_text_page_turns(), vec![1, -1]);
|
|
assert!(s.take_item_text_page_turns().is_empty(), "drained");
|
|
}
|
|
}
|