resource/visualasset
비주얼 애셋. (원본 resource/visualasset.js 의 VisualAsset, 비주얼 에디터가 저장한 문서 파일을 읽어 Visual 을 만든다)
- 파일은 비주얼 에디터(vanilla.js tools/visualeditor)가 저장하는 UIDocument JSON 이다. (씬 *.scene.json, 프리팹 *.prefab.json, { version, root, frameSize? })
- load 가 엔진의 애셋 받기로 바이트를 부탁한다. 바이트가 오면 처음 볼 때 JSON 객체이고 root 객체가 있는지 보고, 맞으면 읽힌 것이고 그 글을 든 Visual 을 담는다. 받기에 실패하거나 문서 꼴이 아니면 읽히지 않은 것이다. (원본은 load 가 거부된다)
- 노드 트리는 Visual::create_node 가 짓는다. (부를 때마다 새 트리)
- 원본과 다른 점:
- 원본 load(경로) 는 Promise 를 돌려주고, 러스트 판 load(엔진, 경로) 는 await 할 수 있는 읽기 손잡이를 돌려준다. (다른 애셋과 같다)
- 원본 정적 createVisualFromText(글) 는 실패하면 예외를 던지고, 러스트 판 create_visual_from_text 는 Err 다.
- 시험과 묶음 파일을 위해 load_from_bytes 가 있다. (TextAsset 과 같다)
Visual asset. (VisualAsset of the original resource/visualasset.js, reads a document file saved by the visual editor and makes a Visual)
- The file is the UIDocument JSON saved by the visual editor (vanilla.js tools/visualeditor). (Scene *.scene.json, prefab *.prefab.json, { version, root, frameSize? })
- load asks the engine's asset fetching for the bytes. When the bytes arrive, the first look checks that they are a JSON object with a root object; if so it counts as loaded and stores a Visual holding that text. A failed fetch or a malformed document stays unloaded. (The original rejects load)
- Visual::create_node builds the node tree. (A new tree on every call)
- Differences from the original:
- The original load(path) returns a Promise, the Rust load(engine, path) returns a load handle that can be awaited. (Same as other assets)
- The original static createVisualFromText(text) throws on failure, the Rust create_visual_from_text gives Err.
- load_from_bytes exists for tests and bundle files. (Same as TextAsset)
VisualAssetstruct traits Default, Asset
비주얼 애셋.
- 비주얼 에디터 문서 파일을 읽어 그 글을 든 Visual 을 담는다. 문서 꼴(JSON 객체, root 객체)이 아니면 읽히지 않은 것이다.
Visual asset.
- Reads a visual editor document file and stores a Visual holding that text. A file that is not a document (a JSON object with a root object) stays unloaded.
use vanilla::{Asset, AssetType, NodeTree, UIDocument, VisualAsset};
let document_text = r#"{"version":1,"root":{"type":"WorldNode","name":"Panel","contentSize":[200,120],"components":[],"children":[]},"frameSize":[960,540]}"#;
let mut visual_asset = VisualAsset::new();
assert!(!visual_asset.is_loaded());
visual_asset.load_from_bytes("panel.prefab.json", document_text.as_bytes().to_vec());
assert!(visual_asset.is_loaded());
assert_eq!(visual_asset.get_asset_base().get_asset_type(), AssetType::Visual);
let visual = visual_asset.get_visual().expect("visual");
let mut document = UIDocument::new();
let mut node_tree = NodeTree::new();
let panel_id = visual.create_node(&mut document, &mut node_tree).expect("document").expect("root");
assert_eq!(node_tree[panel_id].get_name(), "Panel");생성. (애셋 타입 visual)
Creates one. (asset type visual)
반환ReturnsVisualAsset 요청 전의 빈 비주얼 애셋. (읽히지 않았고 담긴 비주얼이 없다)An empty visual asset before any request. (Not loaded, no stored visual)
짝Counterpart: vanilla.js VisualAsset.constructor
문서 글로 Visual 만들기.
Make a Visual from document text.
- document_text&str
- 비주얼 에디터가 저장한 UIDocument JSON 글.UIDocument JSON text saved by the visual editor.
반환ReturnsResult<Visual, String> 그 글을 든 Visual. JSON 객체가 아니거나 root 객체가 없으면 Err.A Visual holding that text. Err when it is not a JSON object or has no root object.
짝Counterpart: vanilla.js VisualAsset.createVisualFromText
use vanilla::{NodeTree, UIDocument, VisualAsset};
let document_text = r#"{"version":1,"root":{"type":"WorldNode","name":"Icon","components":[],"children":[]}}"#;
let visual = VisualAsset::create_visual_from_text(document_text).expect("visual");
assert_eq!(visual.get_document_text(), Some(document_text));
let mut document = UIDocument::new();
let mut node_tree = NodeTree::new();
let icon_id = visual.create_node(&mut document, &mut node_tree).expect("document").expect("root");
assert_eq!(node_tree[icon_id].get_name(), "Icon");
assert!(VisualAsset::create_visual_from_text("not json").is_err());
assert!(VisualAsset::create_visual_from_text("[1, 2]").is_err());
assert!(VisualAsset::create_visual_from_text(r#"{"version":1,"root":[1, 2]}"#).is_err());읽기 요청. (원본 load, await 할 수 있는 읽기 손잡이를 돌려준다)
Request a load. (Original load, returns a load handle that can be awaited)
- engine&Engine
- 애셋을 받을 엔진.Engine to load the asset with.
- asset_path&str
- 비주얼 에디터가 저장한 문서 파일 경로.Path of a document file saved by the visual editor.
반환ReturnsAssetLoadHandle 읽기 손잡이. (바이트가 오면 끝난다, 문서 꼴은 is_loaded 와 get_visual 이 본다)Load handle. (Finishes when the bytes arrive, is_loaded and get_visual check the document shape)
짝Counterpart: vanilla.js VisualAsset.load
use vanilla::{Asset, Engine, EngineConfiguration, VisualAsset};
let engine = Engine::new(EngineConfiguration::new());
let mut visual_asset = VisualAsset::new();
visual_asset.load(&engine, r#"data:application/json,{"version":1,"root":{"type":"WorldNode","name":"Icon","components":[],"children":[]}}"#);
assert!(visual_asset.is_loaded());
assert!(visual_asset.get_visual().is_some());
let mut broken_asset = VisualAsset::new();
broken_asset.load(&engine, r#"data:application/json,{"version":1}"#);
assert!(!broken_asset.is_loaded());
assert!(broken_asset.get_visual().is_none());이미 가진 바이트로 읽기.
Load from bytes already in hand.
- asset_path&str
- 적어 둘 경로.Path to record.
- bytesVec<u8>
- 문서 파일 바이트.Document file bytes.
내리기. (담은 Visual 도 비운다)
Unload. (Also clears the stored Visual)
짝Counterpart: vanilla.js VisualAsset.unload
비주얼 설정.
Sets the visual.
- visualOption<Visual>
- 담을 비주얼. (None 이면 비운다, 읽힌 상태는 바꾸지 않는다)Visual to store. (None clears it, does not change the loaded state)
짝Counterpart: vanilla.js VisualAsset.setVisual
비주얼 반환.
Returns the visual.
반환ReturnsOption<&Visual> 담긴 비주얼. (읽은 문서를 든 것이거나 set_visual 로 넣은 것) 아직 읽히지 않았거나 문서 꼴이 아니고 넣은 것도 없으면 None.Stored visual. (The one holding the read document, or the one set with set_visual) None when not loaded yet, or not a document and nothing was set.
짝Counterpart: vanilla.js VisualAsset.getVisual
use vanilla::{Asset, Vector2, Visual, VisualAsset};
let mut visual_asset = VisualAsset::new();
assert!(visual_asset.get_visual().is_none());
let mut visual = Visual::new();
visual.set_location(Vector2::create(10.0, 20.0));
visual_asset.set_visual(Some(visual));
assert_eq!(visual_asset.get_visual().map(|visual| visual.get_location()), Some(Vector2::create(10.0, 20.0)));
assert!(!visual_asset.is_loaded());
visual_asset.load_from_bytes("coin.prefab.json", br#"{"version":1,"root":{"name":"Coin"}}"#.to_vec());
assert!(visual_asset.get_visual().is_some_and(|visual| visual.get_document_text().is_some()));
visual_asset.set_visual(None);
assert!(visual_asset.get_visual().is_none());
assert!(visual_asset.is_loaded());
visual_asset.unload();
assert!(!visual_asset.is_loaded());
assert!(visual_asset.get_visual().is_none());