vanilladocs
vanillapunch.com

저작 도구와 두 엔진이 같이 쓰는 파일

원본 vanilla.js 와 함께 오는 저작 도구가 만드는 파일을 러스트 판에서 불러 쓰는 방법을 다룹니다. 러스트 판에는 따로 편집기가 없고, vanilla.js 의 편집기와 명령줄 도구가 저장하는 파일을 원본과 같은 꼴로 읽습니다. UI Editor 의 .uiasset.json, Timeline Editor 의 .timeline.json, Particle Editor 의 .vfx.json, Visual Editor 의 .scene.json 과 .prefab.json 차례로 보고, 끝에 스프라이트 아틀라스, 언어 파일, 글꼴 목록, 저장 값처럼 두 엔진이 같이 쓰는 다른 파일을 정리합니다. 도구 목록과 여는 곳은 "도구" 장에 있습니다.

도구와 파일

도구 데모 만드는 파일 러스트 판에서 읽는 곳
UI Editor 열기 .uiasset.json (처음 이름 ui.uiasset.json) UIDocument::from_json_text
Timeline Editor 열기 .timeline.json (처음 이름 titlecard.timeline.json) Timeline::load_from_text, load_from_url, create_from_json_asset
Particle Editor 열기 .vfx.json (처음 이름 effect.vfx.json) ParticleSystem::apply_description
Visual Editor 열기 scenes/*.scene.json, prefabs/*.prefab.json UIDocument::from_json_text
  • 편집기는 vanilla.js 저장소의 tools/<도구 이름>/<도구 이름>.html 이고, 엔진까지 한 파일에 담은 빌드라서 서버 없이 파일로 열어도 동작합니다.
  • UI Editor, Timeline Editor, Particle Editor 는 저장하면 브라우저 내려받기로 파일을 받습니다. Visual Editor 는 크롬 계열 브라우저에서 폴더 열기로 연 폴더에 바로 쓰고, 그 밖의 브라우저에서는 내려받기로 받습니다.
  • 받은 파일은 게임의 애셋 폴더(데스크탑은 EngineConfiguration.asset_directory_paths, 웹은 asset_base_url 기준)에 두고 아래처럼 불러옵니다.

UI Editor 문서 불러오기

.uiasset.json 은 노드 트리를 그대로 적은 UI 문서입니다. 판 번호와 뿌리 노드 하나로 되어 있고, 노드마다 종류, 이름, 켜짐, 위치, 배율, 회전, 투명도, 피벗, 앵커, 내용 크기, 터치 받기, 컴포넌트, 자식을 적습니다.

UI Editor 에 UI 쇼케이스의 컴포넌트 문서를 연 화면: 왼쪽 컴포넌트 목록과 하이어라키, 가운데 문서, 오른쪽 인스펙터

{
	"version": 1,
	"root": {
		"type": "WorldNode",
		"name": "Root",
		"position": [0, 0],
		"contentSize": [960, 640],
		"components": [],
		"children": [
			{
				"type": "WorldNode",
				"name": "StartButton",
				"position": [40, 300],
				"contentSize": [240, 64],
				"components": [
					{ "type": "Paint", "color": [0.25, 0.5, 0.9, 1], "roundSize": 12 },
					{ "type": "UIButton" },
					{ "type": "UILabel", "text": "Start", "fontSize": 24 }
				],
				"children": []
			}
		]
	}
}

원본의 UIDocument 는 정적 클래스와 모듈 전역 표입니다. 러스트 판은 종류 표를 든 UIDocument 값을 만들어 씁니다. UIDocument::new() 는 엔진 컴포넌트를 등록해 둡니다. from_json_text(node_tree, json_text) 는 문서 글을 노드 트리에 지어 어느 부모에도 붙지 않은 뿌리의 NodeId 를 돌려줍니다. JSON 이 아니거나 root 객체가 없으면 Err 입니다. 씬에 보이려면 add_child 로 씬의 루트에 붙이고, 이름으로 노드를 찾아 동작을 붙입니다. UIDocument::find_node_by_path(node_tree, root_id, path_text) 는 문서 뿌리부터 이름을 / 로 이은 경로(뿌리 이름은 빼고 / 로 시작)로 노드를 찾고, 없으면 None 입니다.

use vanilla::{NodeTree, Paint, UIButton, UIDocument, UILabel};

let json_text = r#"{
	"version": 1,
	"root": {
		"type": "WorldNode",
		"name": "Root",
		"contentSize": [960, 640],
		"components": [],
		"children": [
			{
				"type": "WorldNode",
				"name": "StartButton",
				"position": [40, 300],
				"contentSize": [240, 64],
				"components": [
					{ "type": "Paint", "color": [0.25, 0.5, 0.9, 1], "roundSize": 12 },
					{ "type": "UIButton" },
					{ "type": "UILabel", "text": "Start", "fontSize": 24 }
				],
				"children": []
			}
		]
	}
}"#;

let mut node_tree = NodeTree::new();
let mut document = UIDocument::new();
let document_root_id = document.from_json_text(&mut node_tree, json_text).unwrap();
let root_id = node_tree.get_root_id();
node_tree.add_child(root_id, document_root_id);

let start_button_id = UIDocument::find_node_by_path(&node_tree, document_root_id, "/StartButton").unwrap();
let start_button_node = &node_tree[start_button_id];
assert!(start_button_node.get_component::<Paint>().is_some());
assert!(start_button_node.get_component::<UIButton>().is_some());
assert_eq!(start_button_node.get_component::<UILabel>().unwrap().get_text(), "Start");
assert!(UIDocument::find_node_by_path(&node_tree, document_root_id, "/Missing").is_none());
  • 스크롤 뷰처럼 content 노드를 만드는 위젯은 붙을 때 안쪽 content 노드를 스스로 만듭니다. 그래서 문서에는 위젯만 적고, content 안에 넣은 자식(목록 항목 등)은 contentChildren 으로 따로 적었다가 되살릴 때 content 아래에 붙입니다. content 노드의 이름은 content 이므로 경로에 넣어 찾습니다. (예: /Panel/ItemList/content/Item0)
  • 다른 노드를 가리키는 값(노드 참조)은 문서에 $nodeRef 와 뿌리 기준 경로로 적혀 있고, 트리를 다 지은 뒤 이어 붙입니다.
  • 읽을 때 노드에 이미 있는 컴포넌트와 자식(노드 종류나 위젯이 붙을 때 지은 것)은 차례와 종류가 맞으면 그것에 문서 값을 넣고 다시 짓지 않습니다. 원본처럼 겹쳐 붙지 않습니다.
  • 에디터가 얹는 frameSize 같은 모르는 키는 건너뜁니다.

그림 경로 해석

문서에는 그림 원본이 아니라 경로(imagePath)만 적힙니다. Sprite, UIImageView, ShadowImage 가 이 칸을 씁니다. 그림을 잇는 길은 둘입니다.

  • 애셋 폴더에서 읽기: from_json_text 앞에 load_images(engine, json_text) 를 부르면 문서의 imagePath 마다 ImageAsset 을 요청해 문서 안에 들고, 새로 요청한 읽기 손잡이 목록을 돌려줍니다. 씬이 load 에서 부르면 손잡이를 track_load_handle 로 맡겨 initialize 를 기다리게 할 수 있습니다. 데스크탑은 그 자리에서 읽히고, 웹처럼 나중에 읽히면 아직 읽히지 않은 그림은 문서가 맡아 두었다가 bind_pending_images(node_tree) 가 붙입니다. 남은 수가 0 이 될 때까지 씬의 tick 에서 부릅니다.
  • 해석기: set_image_resolver(resolver) 로 경로 → Option<Rc<Image>> 함수를 걸면 애셋 창고 대신 그것으로 찾습니다. 바로 돌려줘야 하므로 그림은 먼저 받아 둡니다.

찾지 못한 그림은 경고를 찍고 버리고, 그 컴포넌트는 원본처럼 그림 없이 남습니다.

use std::rc::Rc;
use vanilla::{Image, NodeTree, Sprite, UIDocument};

let star_image = Image::create_from_pixels(1, 1, vec![255, 255, 255, 255]);
let resolved_image = star_image.clone();
let mut document = UIDocument::new();
document.set_image_resolver(move |image_path: &str| {
	if image_path == "images/star.png" {
		Some(resolved_image.clone())
	}
	else {
		None
	}
});

let json_text = r#"{"version":1,"root":{"name":"Star","components":[{"type":"Sprite","imagePath":"images/star.png"}]}}"#;
let mut node_tree = NodeTree::new();
let star_id = document.from_json_text(&mut node_tree, json_text).unwrap();
let sprite = node_tree[star_id].get_component::<Sprite>().unwrap();
assert!(sprite.get_image().is_some_and(|image| Rc::ptr_eq(image, &star_image)));

컴포넌트와 노드 종류 등록

UIDocument::new() 가 처음부터 아는 컴포넌트는 원본 표의 Paint, Sprite, Text, RichText, Mask, Outline, SoftShade, ShadowText, UIProgressRing, UILabel, UIImageView, UIButton, UIToggleButton, UIProgressView, UISlider, UIScrollView 와 러스트 판이 더한 ShadowImage 입니다. (get_component_type_names()) 노드 종류는 없습니다. UI Editor 는 엔진이 내보내는 모든 컴포넌트와 WorldNode 파생 노드를 문서 꼴에 등록해 씁니다. 그래서 편집기에서 쓴 종류가 게임 쪽 표에 없으면, 그 컴포넌트는 읽을 때 경고를 찍고 빠지고 그 노드는 그냥 노드로 되살아납니다.

러스트 판은 register_editor_types() 한 줄로 편집기와 같은 종류(컴포넌트 35, 노드 종류 12)를 등록합니다. 편집기가 저장한 문서를 읽는 게임은 이것을 한 번 부릅니다. 종류마다 등록할 수도 있습니다.

  • 원본 노드 종류(WorldNode 를 이어받은 클래스, registerNodeType)는 러스트 판에서 노드에 붙는 컴포넌트입니다. register_node_type(type_name, create_component) 로 등록하면 원본 문서와 같은 꼴로 쓰고 읽습니다. 그 컴포넌트가 붙은 노드의 type 이 그 이름이고, 컴포넌트 목록에는 적지 않습니다. (UIInputField, UIScrollBar, UIWindow 같은 것)
  • 컴포넌트는 register_component_type(type_name, create_component) 로 등록합니다. 쓰기, 읽기 함수가 따로 있는 종류는 register_component_type_with_handler, content 노드를 만드는 위젯은 register_widget_type 으로 등록합니다.
  • 전용 저장 규칙이 없는 컴포넌트는 짝이 맞는 게터와 세터로 읽고 쓰는 값으로 저장되고 되살아납니다. (save_by_reflection, load_by_reflection) 원본은 실행 중에 객체를 훑고, 러스트 판은 #[vanilla::reflect] 매크로가 컴파일 때 지은 목록을 씁니다. 엔진 컴포넌트는 모두 붙어 있고, 게임 컴포넌트를 문서로 다루려면 매크로를 붙입니다. ("반사 매크로" 장)
use vanilla::{NodeTree, UIDocument, UIInputField};

let mut document = UIDocument::new();
assert!(!document.get_component_type_names().contains(&"UISnapScrollView"));
document.register_editor_types();
let component_type_names = document.get_component_type_names();
assert!(component_type_names.contains(&"UISnapScrollView"));
assert!(component_type_names.contains(&"ParticleSystem"));

let json_text = r#"{"version":1,"root":{"type":"UIInputField","name":"NameField","contentSize":[320,48]}}"#;
let mut node_tree = NodeTree::new();
let field_id = document.from_json_text(&mut node_tree, json_text).unwrap();
assert!(node_tree[field_id].get_component::<UIInputField>().is_some());

게임에서 문서 저장하기

to_json_text(node_tree, root_id, is_pretty) 는 노드 트리를 같은 꼴의 글로 바꿉니다. 키 차례와 들여쓰기(탭)는 원본 JSON.stringify 와 같고, 수는 f64 를 가장 짧은 십진으로 적습니다. 등록한 종류는 등록한 이름으로 적고, 등록하지 않은 컴포넌트는 원본처럼 게터와 세터로 적되 #[vanilla::reflect] 가 없으면 적을 수 없어 경고를 찍고 뺍니다. 원본처럼 번들러가 클래스 이름을 바꿀 걱정은 없습니다. &mut self 게터가 있는 컴포넌트도 적습니다. (그래서 트리를 &mut NodeTree 로 받습니다)

Timeline Editor 문서 재생하기

.timeline.json 은 무대 노드 목록, 트랙(대상 이름, 속성, 키), 마커로 이루어진 문서입니다. 맨 위에 name, duration(초), frameRate, loop, stage(width, height, backgroundColor, nodes), tracks, markers 가 있습니다.

Timeline Editor 에 컷신 문서를 연 화면: 레터박스와 대화 상자가 걸린 무대, 아래 트랙과 키, 오른쪽 인스펙터

use std::cell::RefCell;
use std::rc::Rc;
use vanilla::{NodeTree, Timeline, TimelineMarker};

let timeline_text = r##"{
	"name": "Logo",
	"duration": 1,
	"frameRate": 30,
	"loop": false,
	"stage": { "width": 960, "height": 540, "backgroundColor": "#101114ff", "nodes": [
		{ "name": "Emblem", "type": "paint", "width": 120, "height": 120, "color": "#ffcc33ff" }
	] },
	"tracks": [
		{ "target": "Emblem", "property": "x", "keys": [{ "time": 0, "value": 0 }, { "time": 1, "value": 480 }] }
	],
	"markers": [{ "time": 0.5, "name": "half" }]
}"##;

let mut timeline = Timeline::load_from_text(timeline_text).unwrap();
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let node_table = timeline.build_stage(&mut node_tree, root_id, None);

let event_list = Rc::new(RefCell::new(Vec::new()));
let marker_event_list = event_list.clone();
timeline.set_marker_handler(Some(Box::new(move |marker_name: &str, _marker: &TimelineMarker, _timeline: &mut Timeline, _node_tree: &mut NodeTree| {
	marker_event_list.borrow_mut().push(format!("marker {}", marker_name));
})));
let complete_event_list = event_list.clone();
timeline.set_complete_handler(Some(Box::new(move |_timeline: &mut Timeline, _node_tree: &mut NodeTree| {
	complete_event_list.borrow_mut().push(String::from("complete"));
})));

timeline.play();
timeline.tick(&mut node_tree, 0.6);
timeline.tick(&mut node_tree, 0.6);
assert_eq!(*event_list.borrow(), vec![String::from("marker half"), String::from("complete")]);
assert_eq!(node_tree[node_table["Emblem"]].get_local_position().x, 480.0);
assert!(!timeline.is_playing());
  • 읽기: Timeline::load_from_text(text)(JSON 글에서, 실패하면 Err), Timeline::create_from_json_asset(&mut json_asset)(읽은 JsonAsset 에서, 서술을 복제해 씀, 안 읽혔으면 None), Timeline::load_from_url(engine, url)(원본 loadFromUrl, 엔진 작업 안에서 await 하면 Result<Timeline, String> 이 나오는 퓨처). 씬은 load 에서 JsonAsset 을 요청해 track_asset 으로 맡기고, initialize 에서 create_from_json_asset 으로 만드는 꼴이 가장 쉽습니다. AssetLoader 로 다른 애셋과 함께 받아도 됩니다.
  • 무대: build_stage(node_tree, parent_id, image_resolver) 는 문서의 무대 노드를 만들어 parent_id 아래에 붙이고, 이름 → NodeId 표(HashMap)를 돌려줍니다. 이미 지은 노드 트리에 트랙을 걸려면 bind(node_tree, root_id) 로 트랙의 대상 이름과 같은 이름의 노드를 찾아 붙입니다. 대상은 TimelineTarget 으로, 노드는 Node(NodeId), 노드가 아닌 것(화면 효과 같은)은 External 이고, set_target(name, target) 이나 set_target_resolver(resolver) 로 겁니다.
  • 그림: 편집기에서 가져온 그림은 문서의 image 칸에 data: 주소로 들어가고, 런타임이 스스로 풉니다. (데스크탑은 그 자리에서, 웹은 브라우저가 풀면 겁니다) 이름으로 적힌 그림은 image_resolver 가 돌려줘야 합니다. 해석기는 Some(&|image_name: &str| image_table.get(image_name).cloned()) 처럼 넘깁니다.
  • 재생: play(), pause(), stop(node_tree)(처음으로), seek(node_tree, time)(이벤트를 내지 않음)과, 매 프레임 tick(node_tree, time_delta) 를 부릅니다. 노드를 움직이는 함수는 NodeTree 를 같이 받습니다. set_loop, set_speed 로 반복과 빠르기를 바꿉니다.
  • 알림: set_marker_handler, set_event_handler, set_complete_handler 는 원본처럼 마지막에 타임라인 자신을 받고, 그 뒤에 노드 트리를 같이 받습니다. 완료는 반복이 아닐 때 끝에 닿으면 불립니다. 핸들러는 씬을 빌릴 수 없으므로 나눌 값은 Rc<RefCell> 에 담습니다.
  • 소리 노드가 있는 문서는 원본 Timeline.setAudioManager 대신, 무대를 만든 뒤 load_sounds(node_tree, engine) 를 한 번 불러 소리마다 오디오 애셋을 읽습니다.

트랙, 이징, 키의 자세한 규칙은 "트윈, 애니메이션, 타임라인" 장에서 다룹니다.

Particle Editor 애셋 쓰기

.vfx.json 은 파티클 시스템 서술의 목록입니다. 시스템마다 이름과 ParticleSystem::apply_description 이 받는 서술 칸을 함께 적습니다. 서술 칸은 원본과 같은 이름(looping, emissionRate, shape, lifetime, renderShape, blendMode 같은)이고, 모양과 합성은 원본의 글로 적힌 것을 읽어 열거형으로 바꿉니다. 모르는 이름의 모양과 합성은 무시합니다.

Particle Editor 처음 화면: Fire 프리셋이 도는 뷰, 왼쪽 프리셋 목록, 오른쪽 방출과 모양 설정

{
	"systems": [
		{ "name": "Fire", "looping": true, "duration": 1, "emissionRate": 60, "shape": "cone", "lifetime": [0.6, 1.2], "speed": [80, 180], "renderShape": "circle", "blendMode": "lighter" }
	]
}

엔진에서는 시스템마다 노드를 하나 만들어 ParticleSystem 을 붙이고 서술을 넣습니다. ParticleSystem 은 처음부터 재생 중입니다. 게임에서는 파일을 JsonAsset 으로 받아 get_data() 의 systems 를 아래처럼 돕니다. 편집기는 systems 가 없는 옛 꼴(서술 하나만 있는 객체)도 읽으므로, 그런 파일이면 객체 하나를 서술로 씁니다.

use vanilla::{NodeTree, ParticleSystem, Vector2};

let vfx_asset_text = r#"{
	"systems": [
		{
			"name": "Sparks",
			"looping": true,
			"duration": 1,
			"emissionRate": 80,
			"shape": "circle",
			"shapeRadius": 10,
			"lifetime": [0.4, 0.8],
			"speed": [120, 260],
			"size": [3, 6],
			"gravity": [0, 300],
			"startColorA": [1, 0.9, 0.4, 1],
			"startColorB": [1, 0.5, 0.1, 1],
			"colorOverLifetime": [{ "time": 0, "color": [1, 1, 1, 1] }, { "time": 1, "color": [1, 0.3, 0.05, 0] }],
			"renderShape": "streak",
			"blendMode": "lighter"
		}
	]
}"#;

let vfx_asset: serde_json::Value = serde_json::from_str(vfx_asset_text).unwrap();
let description_list = match vfx_asset.get("systems").and_then(|systems| systems.as_array()) {
	Some(system_array) => {
		system_array.clone()
	}
	None => {
		vec![vfx_asset.clone()]
	}
};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
for description in description_list.iter() {
	let system_name = description["name"].as_str().unwrap_or("ParticleSystem");
	let system_id = node_tree.create_child(root_id, system_name);
	let system_node = &mut node_tree[system_id];
	system_node.set_local_position(Vector2::create(640.0, 360.0));
	let particle_system = system_node.add_component(ParticleSystem::new());
	particle_system.apply_description(description);
	particle_system.simulate(0.5);
	assert!(particle_system.get_particle_count() > 0);
}
assert!(node_tree.find_child_by_name(root_id, "Sparks").is_some());

편집기에서 파티클 그림을 가져왔으면 그 그림이 서술의 imageDataUrl 칸에 data: 주소로 들어갑니다. apply_description 은 원본처럼 이 칸을 읽지 않으므로, ImageAsset::load(engine, data_url) 로 그림을 읽고(애셋 경로 자리에 data: 주소도 됩니다) 다 읽힌 뒤 set_image(image_asset.get_image()) 로 줍니다.

use vanilla::{Engine, EngineConfiguration, ImageAsset, ParticleSystem};

let engine = Engine::new(EngineConfiguration::new());
let description = serde_json::json!({
	"name": "Dust",
	"renderShape": "image",
	"imageDataUrl": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
});

let mut particle_system = ParticleSystem::new();
particle_system.apply_description(&description);
let mut image_asset = ImageAsset::new();
if let Some(image_data_url) = description["imageDataUrl"].as_str() {
	image_asset.load(&engine, image_data_url);
}
particle_system.set_image(image_asset.get_image());
assert!(image_asset.get_image().is_some());

데스크탑은 data: 주소를 그 자리에서 풉니다. 웹은 나중에 읽히므로 get_image() 가 Some 이 될 때(또는 읽기 손잡이가 끝날 때) 겁니다. 서술 칸의 뜻은 "파티클" 장에서 다룹니다.

Visual Editor 씬 불러오기

Visual Editor 는 씬을 UI 문서와 같은 꼴로 저장합니다. 처음 저장 경로는 scenes/untitled.scene.json 이고, 편집기만 쓰는 틀 크기(frameSize)를 같은 JSON 에 함께 둡니다. UIDocument 는 모르는 키를 건너뛰므로 게임은 from_json_text 로 그대로 읽습니다. 씬 뿌리는 이름이 sceneRoot 이고 피벗이 왼쪽 위인 노드입니다. Visual Editor 의 Sprite 는 그림의 프로젝트 기준 경로를 imagePath 에 적으므로, 애셋 폴더를 같은 프로젝트 폴더로 두면 load_images 가 그 경로로 읽습니다.

Visual Editor 에 UI 쇼케이스의 컴포넌트 문서를 씬으로 연 화면: 왼쪽 하이어라키, 가운데 씬 뷰와 이동 핸들, 오른쪽 인스펙터

아래 예제는 저장소의 examples/assets/scenedocument.scene.json(Visual Editor 저장 꼴로 만든 씬)을 읽습니다. 씬 파일은 load 에서 TextAsset 으로 요청해 맡기고, initialize 에서 그림을 요청한 뒤 트리를 짓고, tick 에서 늦게 읽힌 그림을 붙입니다. 단추 누름은 set_clicked_event 의 함수가 씬을 빌릴 수 없으므로 Rc<Cell> 로 알리고 tick 에서 받습니다. 이 씬 파일의 그림 몇 장은 저장소에 없어 경고를 찍고 그림 없이 남습니다.

use std::cell::Cell;
use std::path::PathBuf;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, Graphic, NodeId, Scene, SceneBase, TextAsset, UIButton, UIDocument, UILabel};

struct VisualScene {
	scene_base: SceneBase,
	scene_asset: TextAsset,
	document: UIDocument,
	label_id: Option<NodeId>,
	is_button_clicked: Rc<Cell<bool>>,
}

impl Scene for VisualScene {
	fn get_scene_base(&self) -> &SceneBase {
		&self.scene_base
	}

	fn get_scene_base_mut(&mut self) -> &mut SceneBase {
		&mut self.scene_base
	}

	fn load(&mut self, engine: &Engine) {
		self.scene_asset.load(engine, "scenedocument.scene.json");
		self.scene_base.track_asset(&self.scene_asset);
	}

	fn initialize(&mut self, engine: &Engine) {
		let scene_text = match self.scene_asset.get_text() {
			Some(scene_text) => {
				scene_text.to_string()
			}
			None => {
				return;
			}
		};
		self.document.register_editor_types();
		self.document.load_images(engine, &scene_text);
		let root_id = self.scene_base.get_root_id();
		let node_tree = self.scene_base.get_node_tree_mut();
		let scene_root_id = match self.document.from_json_text(node_tree, &scene_text) {
			Ok(scene_root_id) => {
				scene_root_id
			}
			Err(error) => {
				eprintln!("{}", error);
				return;
			}
		};
		node_tree.add_child(root_id, scene_root_id);

		self.label_id = UIDocument::find_node_by_path(node_tree, scene_root_id, "/Panel/label");
		if let Some(button_id) = UIDocument::find_node_by_path(node_tree, scene_root_id, "/Panel/button") {
			if let Some(button) = node_tree[button_id].get_component_mut::<UIButton>() {
				let is_button_clicked = self.is_button_clicked.clone();
				button.set_clicked_event(Box::new(move |_button: &mut UIButton, _context: &mut ComponentContext| {
					is_button_clicked.set(true);
				}));
			}
		}
	}

	fn tick(&mut self, time_delta: f64) {
		self.scene_base.tick(time_delta);
		let node_tree = self.scene_base.get_node_tree_mut();
		self.document.bind_pending_images(node_tree);
		if self.is_button_clicked.replace(false) {
			if let Some(label_id) = self.label_id {
				if let Some(label) = node_tree[label_id].get_component_mut::<UILabel>() {
					label.set_text("Clicked");
				}
			}
		}
	}

	fn pre_draw(&mut self, graphic: &mut Graphic) {
		self.scene_base.draw_scene_background(graphic);
	}
}

fn main() {
	let mut engine_configuration = EngineConfiguration::new();
	engine_configuration.asset_directory_paths = vec![PathBuf::from("examples/assets")];
	let engine = Engine::new(engine_configuration);
	let visual_scene = VisualScene {
		scene_base: SceneBase::new(),
		scene_asset: TextAsset::new(),
		document: UIDocument::new(),
		label_id: None,
		is_button_clicked: Rc::new(Cell::new(false)),
	};
	if let Err(error) = engine.run(Box::new(visual_scene)) {
		eprintln!("{}", error);
	}
}

고른 노드를 프리팹으로 저장하면 prefabs/<이름>.prefab.json 에 그 노드 하나를 뿌리로 하는 UI 문서가 생깁니다. 같은 프리팹을 여러 번 쓰려면 글을 한 번 받아 두고 from_json_text 를 쓸 때마다 부릅니다. 부를 때마다 새 노드 트리가 나옵니다.

use vanilla::{NodeTree, Paint, UIDocument, Vector2};

let prefab_json_text = r#"{"version":1,"root":{"type":"WorldNode","name":"Coin","contentSize":[80,80],"components":[{"type":"Paint","color":[1,0.8,0.2,1],"roundSize":40}],"children":[]}}"#;

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let mut document = UIDocument::new();
for index in 0..3 {
	let coin_id = document.from_json_text(&mut node_tree, prefab_json_text).unwrap();
	node_tree[coin_id].set_local_position(Vector2::create(200.0 + index as f64 * 120.0, 120.0));
	node_tree.add_child(root_id, coin_id);
}
let coin_ids = node_tree.get_child_ids(root_id).to_vec();
assert_eq!(coin_ids.len(), 3);
assert!(node_tree[coin_ids[2]].get_component::<Paint>().is_some());
assert_eq!(node_tree[coin_ids[2]].get_local_position().x, 440.0);

VisualAsset 은 Visual Editor 가 저장한 씬과 프리팹(.scene.json, .prefab.json, UIDocument 꼴)을 읽어 그 문서를 든 Visual 을 담습니다. VisualAsset::load(engine, path) 는 기다릴 수 있는 읽기 손잡이를 돌려주고, 받기에 실패하거나 문서 꼴(JSON 객체와 root)이 틀리면 읽히지 않은 것으로 남습니다. 받은 바이트는 load_from_bytes, 글은 VisualAsset::create_visual_from_text 로도 됩니다. Visual::create_node(&mut document, &mut node_tree) 가 문서의 노드 트리를 새로 지어 뿌리의 로컬 위치를 location 으로 두고 그 뿌리를 돌려줍니다. (문서가 없으면 Ok(None), 문서 꼴이 틀리면 Err, 부르는 쪽이 원하는 부모에 붙입니다) 원본 VisualAsset, Visual.createNode 와 같습니다.

두 엔진이 같이 쓰는 다른 파일

파일 만드는 곳 러스트 판에서 읽는 곳
스프라이트 아틀라스 JSON 과 쪽 그림 vanilla.js 의 tools/atlas.cjs SpriteAtlas::load, load_data
언어 목록 languages.json 과 언어 파일 손으로 쓰거나 번역 도구 languagepack ("언어" 장)
글꼴 색인 fonts.json 과 묶음 목록 손으로 쓰거나 글꼴 조각 도구 uifont ("언어" 장)
저장 값 (window.localStorage) 게임 LocalStorage, PersistedStore ("저장" 장)
빌드 판 version.json 배포 빌드 BuildInfo ("개발자 도구" 장)
소리 파일 .webm (Opus) vanilla.js 의 tools/webm.cjs AudioAsset, AssetLoader ("소리" 장)
  • 웹의 저장 값은 키와 값의 꼴, 숫자 글, PersistedStore 의 {"version":..,"state":..} 가 원본과 같아서, 같은 출처의 원본 게임 저장을 러스트 판이 그대로 읽고 씁니다.
  • 데스크탑의 소리 해석은 크롬과 같은 해석기 묶음이라 webm.cjs 가 만든 Opus 파일을 그대로 틉니다.

스프라이트 아틀라스

SpriteAtlas 는 원본과 같은 두 꼴의 JSON 을 읽습니다.

  1. tools/atlas.cjs 가 만든 것. { images: { "이름.png": { x, y, width, height, page } }, meta: { imageFileName, pages: [{ imageFileName }] } } 이고, page 가 없으면 0 쪽, meta.pages 가 없으면 meta.imageFileName 한 장입니다.
  2. 쪽 경로와 표. { pagePaths: ["경로"], frames: { "이름": [쪽, x, y, 가로, 세로] } }

그림 이름은 확장자를 뗀 이름으로도 찾습니다. ("coin.png" 는 "coin" 으로도 찾습니다) 쪽 경로에 빗금(/)이 없으면 JSON 이 있는 폴더를 기준으로, 있으면 적힌 그대로 읽습니다. load(engine, json_path) 는 원본처럼 await 할 수 있는 SpriteAtlasLoadHandle 을 돌려줍니다. 데스크탑은 그 자리에서 다 읽히고, 웹은 JSON 과 쪽 그림이 차례로 오므로 is_loaded() 가 참이 될 때까지 tick 에서 update(engine) 를 부르거나 손잡이를 await 합니다.

use std::path::PathBuf;
use vanilla::{Engine, EngineConfiguration, Sprite, SpriteAtlas};

let mut engine_configuration = EngineConfiguration::new();
engine_configuration.asset_directory_paths = vec![PathBuf::from("examples/assets")];
let engine = Engine::new(engine_configuration);

let mut sprite_atlas = SpriteAtlas::new();
sprite_atlas.load(&engine, "atlas/icons.json");
assert!(sprite_atlas.is_loaded());
assert!(sprite_atlas.has_frame("coin"));
assert!(sprite_atlas.has_frame("coin.png"));

let mut sprite = Sprite::new();
assert!(sprite_atlas.apply_frame(&mut sprite, "star"));

자세한 쓰임은 "애셋" 장에서 다룹니다.

명령줄 도구

러스트 판 저장소의 명령줄 도구는 API 문서 자료를 뽑는 vanilla-engine-apidoc 하나입니다. 쓰는 법의 전체는 tools/TOOLS.md 에 있습니다.

cargo run -p vanilla-engine-apidoc -- 0.4.0
cargo run -p vanilla-engine-apidoc -- 0.4.0 --check
cargo test --doc -p vanilla-engine-apidoc
  • 첫 줄은 엔진 소스의 문서 주석에서 docs/api/<판>.json 을 씁니다. 자료 꼴은 vanilla.js 의 apidoc.cjs 와 같습니다.
  • --check 는 자료의 꼴과 이 가이드(ko 와 en 의 파일 이름과 코드 칸이 같은지)를 봅니다.
  • cargo test --doc -p vanilla-engine-apidoc 는 이 가이드의 rust 코드 칸을 doctest 로 컴파일하고 돌립니다.

vanilla.js 의 명령줄 도구는 Node.js 로 실행하고, 결과물 가운데 러스트 판이 쓰는 것은 위 표의 둘입니다. atlas.cjs 의 아틀라스는 SpriteAtlas 가, webm.cjs 의 .webm 은 AudioAsset 이 읽습니다. project.cjs(esbuild 로 묶기), mangle.cjs(묶은 자바스크립트의 이름 줄이기)는 자바스크립트 빌드용이라 러스트 판에는 쓸 곳이 없습니다. 러스트 판의 데스크탑 빌드는 cargo build, 웹 빌드는 cargo build --target wasm32-unknown-unknown 입니다. ("프로젝트 시작" 장)

주의할 점

  • UI 문서의 그림은 경로만 적힙니다. from_json_text 앞에 load_images 를 부르거나 set_image_resolver 를 겁니다. 웹처럼 그림이 늦게 오면 bind_pending_images 를 tick 에서 부릅니다.
  • 편집기에서 쓴 컴포넌트와 노드 종류가 게임 쪽 UIDocument 표에 없으면 경고를 찍고 빠지거나 그냥 노드가 됩니다. 편집기 문서를 읽는 게임은 register_editor_types() 를 부릅니다.
  • 게임 컴포넌트를 문서로 쓰고 읽으려면 #[vanilla::reflect] 를 붙이고 등록합니다.
  • Timeline 은 매 프레임 tick 을 불러야 움직입니다. 소리가 있는 문서는 무대를 만든 뒤 load_sounds 를 부릅니다.
  • .vfx.json 의 imageDataUrl 은 apply_description 이 읽지 않습니다. 그림은 직접 읽어 set_image 로 줍니다.
  • Visual::create_node 는 넘긴 UIDocument 의 표로 짓습니다. 편집기가 저장한 문서면 그 문서에 register_editor_types() 를 먼저 부릅니다.

관련 타입

UIDocument, UIDocumentSaveContext, UIDocumentLoadContext, NodeTree, TextAsset, JsonAsset, ImageAsset, Image, Timeline, TimelineTarget, TimelineMarker, ParticleSystem, WorldNode, UIButton, UILabel, UIInputField, UIScrollBar, UISnapScrollView, Visual, VisualAsset, SpriteAtlas, SpriteAtlasLoadHandle, AudioAsset, AssetLoader

Authoring Tools and Files Shared by Both Engines

This chapter covers how the Rust version loads files made by the authoring tools that come with the original vanilla.js. The Rust version has no editors of its own. It reads the files saved by the vanilla.js editors and command-line tools in the same format as the original. It walks through the UI Editor's .uiasset.json, the Timeline Editor's .timeline.json, the Particle Editor's .vfx.json, and the Visual Editor's .scene.json and .prefab.json, and ends with other files both engines share, such as sprite atlases, language files, font lists, and saved values. The tool list and where to open each tool are in the "Tools" chapter.

Tools and files

Tool Demo File it makes Where the Rust version reads it
UI Editor Open .uiasset.json (default name ui.uiasset.json) UIDocument::from_json_text
Timeline Editor Open .timeline.json (default name titlecard.timeline.json) Timeline::load_from_text, load_from_url, create_from_json_asset
Particle Editor Open .vfx.json (default name effect.vfx.json) ParticleSystem::apply_description
Visual Editor Open scenes/*.scene.json, prefabs/*.prefab.json UIDocument::from_json_text
  • Each editor is tools/<tool name>/<tool name>.html in the vanilla.js repository. It is a build that bundles the engine into one file, so it works when opened as a file without a server.
  • The UI Editor, Timeline Editor, and Particle Editor save through a browser download. The Visual Editor writes straight into a folder opened with the folder picker in Chromium-based browsers, and uses a download in other browsers.
  • Put the saved files in your game's asset folder (relative to EngineConfiguration.asset_directory_paths on the desktop and asset_base_url on the web) and load them as shown below.

Loading UI Editor documents

A .uiasset.json is a UI document that writes out a node tree as is. It consists of a version number and one root node, and each node lists its type, name, active state, position, scale, rotation, opacity, pivot, anchor, content size, touch handling, components, and children.

UI Editor with the UI Showcase components document open: component list and hierarchy on the left, the document in the middle, the inspector on the right

{
	"version": 1,
	"root": {
		"type": "WorldNode",
		"name": "Root",
		"position": [0, 0],
		"contentSize": [960, 640],
		"components": [],
		"children": [
			{
				"type": "WorldNode",
				"name": "StartButton",
				"position": [40, 300],
				"contentSize": [240, 64],
				"components": [
					{ "type": "Paint", "color": [0.25, 0.5, 0.9, 1], "roundSize": 12 },
					{ "type": "UIButton" },
					{ "type": "UILabel", "text": "Start", "fontSize": 24 }
				],
				"children": []
			}
		]
	}
}

The original UIDocument is a static class with module-global tables. The Rust version creates a UIDocument value that holds the type tables. UIDocument::new() registers the engine components. from_json_text(node_tree, json_text) builds the document text into the node tree and returns the NodeId of a root that is not attached to any parent. It returns Err if the text is not JSON or has no root object. To show it, attach it to the scene's root with add_child, then find nodes by name and attach behavior. UIDocument::find_node_by_path(node_tree, root_id, path_text) finds a node by a path of names joined with / from the document root (leaving out the root's name and starting with /), and returns None if not found.

use vanilla::{NodeTree, Paint, UIButton, UIDocument, UILabel};

let json_text = r#"{
	"version": 1,
	"root": {
		"type": "WorldNode",
		"name": "Root",
		"contentSize": [960, 640],
		"components": [],
		"children": [
			{
				"type": "WorldNode",
				"name": "StartButton",
				"position": [40, 300],
				"contentSize": [240, 64],
				"components": [
					{ "type": "Paint", "color": [0.25, 0.5, 0.9, 1], "roundSize": 12 },
					{ "type": "UIButton" },
					{ "type": "UILabel", "text": "Start", "fontSize": 24 }
				],
				"children": []
			}
		]
	}
}"#;

let mut node_tree = NodeTree::new();
let mut document = UIDocument::new();
let document_root_id = document.from_json_text(&mut node_tree, json_text).unwrap();
let root_id = node_tree.get_root_id();
node_tree.add_child(root_id, document_root_id);

let start_button_id = UIDocument::find_node_by_path(&node_tree, document_root_id, "/StartButton").unwrap();
let start_button_node = &node_tree[start_button_id];
assert!(start_button_node.get_component::<Paint>().is_some());
assert!(start_button_node.get_component::<UIButton>().is_some());
assert_eq!(start_button_node.get_component::<UILabel>().unwrap().get_text(), "Start");
assert!(UIDocument::find_node_by_path(&node_tree, document_root_id, "/Missing").is_none());
  • Widgets that create a content node, such as scroll views, create their inner content node themselves when attached. So the document lists only the widget, writes the children placed inside the content (list items and so on) separately as contentChildren, and attaches them under the content when restoring. The content node is named content, so include it in paths. (For example /Panel/ItemList/content/Item0)
  • Values that point to other nodes (node references) are written in the document as $nodeRef with a path from the root, and are connected after the whole tree is built.
  • On read, components and children that a node already has (built when a node type or widget was attached) receive the document values if their order and type match, and are not built again. Unlike the original, nothing is attached twice.
  • Unknown keys such as the frameSize the editor adds are skipped.

Resolving image paths

Documents contain only image paths (imagePath), not the images themselves. Sprite, UIImageView, and ShadowImage use this field. There are two ways to connect images.

  • Reading from the asset folder: calling load_images(engine, json_text) before from_json_text requests an ImageAsset for every imagePath in the document, keeps them in the document, and returns the newly requested load handles. If a scene calls it in load, it can hand the handles to track_load_handle so that initialize waits for them. On the desktop they load on the spot. When they arrive later, as on the web, the document holds on to images that have not loaded yet and bind_pending_images(node_tree) attaches them. Call it from the scene's tick until the remaining count reaches 0.
  • A resolver: registering a path to Option<Rc<Image>> function with set_image_resolver(resolver) makes lookups use it instead of the asset store. It must return right away, so load the images first.

Images that cannot be found print a warning and are dropped, and the component stays without an image, as in the original.

use std::rc::Rc;
use vanilla::{Image, NodeTree, Sprite, UIDocument};

let star_image = Image::create_from_pixels(1, 1, vec![255, 255, 255, 255]);
let resolved_image = star_image.clone();
let mut document = UIDocument::new();
document.set_image_resolver(move |image_path: &str| {
	if image_path == "images/star.png" {
		Some(resolved_image.clone())
	}
	else {
		None
	}
});

let json_text = r#"{"version":1,"root":{"name":"Star","components":[{"type":"Sprite","imagePath":"images/star.png"}]}}"#;
let mut node_tree = NodeTree::new();
let star_id = document.from_json_text(&mut node_tree, json_text).unwrap();
let sprite = node_tree[star_id].get_component::<Sprite>().unwrap();
assert!(sprite.get_image().is_some_and(|image| Rc::ptr_eq(image, &star_image)));

Registering component and node types

The components UIDocument::new() knows from the start are those in the original table, Paint, Sprite, Text, RichText, Mask, Outline, SoftShade, ShadowText, UIProgressRing, UILabel, UIImageView, UIButton, UIToggleButton, UIProgressView, UISlider, UIScrollView, plus ShadowImage, which the Rust version adds (get_component_type_names()). There are no node types. The UI Editor registers every component and WorldNode-derived node the engine exports into the document format. So if a type used in the editor is missing from the game's table, that component prints a warning and is dropped on read, and that node comes back as a plain node.

The Rust version registers the same types as the editor (35 components, 12 node types) with one call to register_editor_types(). Games that read documents saved by the editors call it once. You can also register types one by one.

  • The original node types (classes deriving from WorldNode, registerNodeType) are components attached to a node in the Rust version. Registered with register_node_type(type_name, create_component), they are written and read in the same form as original documents: the type of a node with that component is that name, and the component is not listed among the components. (UIInputField, UIScrollBar, UIWindow, and so on)
  • Register components with register_component_type(type_name, create_component). Types with their own write and read functions use register_component_type_with_handler, and widgets that create a content node use register_widget_type.
  • Components without dedicated save rules are saved and restored through values read and written by matching getters and setters (save_by_reflection, load_by_reflection). The original walks objects at run time, and the Rust version uses a list the #[vanilla::reflect] macro builds at compile time. Every engine component has it, and game components need the macro to be handled in documents. (See the "Reflection Macro" chapter.)
use vanilla::{NodeTree, UIDocument, UIInputField};

let mut document = UIDocument::new();
assert!(!document.get_component_type_names().contains(&"UISnapScrollView"));
document.register_editor_types();
let component_type_names = document.get_component_type_names();
assert!(component_type_names.contains(&"UISnapScrollView"));
assert!(component_type_names.contains(&"ParticleSystem"));

let json_text = r#"{"version":1,"root":{"type":"UIInputField","name":"NameField","contentSize":[320,48]}}"#;
let mut node_tree = NodeTree::new();
let field_id = document.from_json_text(&mut node_tree, json_text).unwrap();
assert!(node_tree[field_id].get_component::<UIInputField>().is_some());

Saving documents from the game

to_json_text(node_tree, root_id, is_pretty) turns a node tree into text of the same form. Key order and indentation (tabs) match the original JSON.stringify, and numbers are written as the shortest decimal of an f64. Registered types are written under their registered names. Unregistered components are written through getters and setters as in the original, but without #[vanilla::reflect] they cannot be written, so a warning is printed and they are left out. There is no need to worry about a bundler renaming classes as in the original. It also writes components that have &mut self getters (which is why it takes the tree as &mut NodeTree).

Playing Timeline Editor documents

A .timeline.json is a document made of a list of stage nodes, tracks (target name, property, keys), and markers. At the top level it has name, duration (seconds), frameRate, loop, stage (width, height, backgroundColor, nodes), tracks, and markers.

Timeline Editor with a cutscene document open: the stage with letterbox and dialogue box, tracks and keys below, the inspector on the right

use std::cell::RefCell;
use std::rc::Rc;
use vanilla::{NodeTree, Timeline, TimelineMarker};

let timeline_text = r##"{
	"name": "Logo",
	"duration": 1,
	"frameRate": 30,
	"loop": false,
	"stage": { "width": 960, "height": 540, "backgroundColor": "#101114ff", "nodes": [
		{ "name": "Emblem", "type": "paint", "width": 120, "height": 120, "color": "#ffcc33ff" }
	] },
	"tracks": [
		{ "target": "Emblem", "property": "x", "keys": [{ "time": 0, "value": 0 }, { "time": 1, "value": 480 }] }
	],
	"markers": [{ "time": 0.5, "name": "half" }]
}"##;

let mut timeline = Timeline::load_from_text(timeline_text).unwrap();
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let node_table = timeline.build_stage(&mut node_tree, root_id, None);

let event_list = Rc::new(RefCell::new(Vec::new()));
let marker_event_list = event_list.clone();
timeline.set_marker_handler(Some(Box::new(move |marker_name: &str, _marker: &TimelineMarker, _timeline: &mut Timeline, _node_tree: &mut NodeTree| {
	marker_event_list.borrow_mut().push(format!("marker {}", marker_name));
})));
let complete_event_list = event_list.clone();
timeline.set_complete_handler(Some(Box::new(move |_timeline: &mut Timeline, _node_tree: &mut NodeTree| {
	complete_event_list.borrow_mut().push(String::from("complete"));
})));

timeline.play();
timeline.tick(&mut node_tree, 0.6);
timeline.tick(&mut node_tree, 0.6);
assert_eq!(*event_list.borrow(), vec![String::from("marker half"), String::from("complete")]);
assert_eq!(node_tree[node_table["Emblem"]].get_local_position().x, 480.0);
assert!(!timeline.is_playing());
  • Reading: Timeline::load_from_text(text) (from JSON text, Err on failure), Timeline::create_from_json_asset(&mut json_asset) (from a loaded JsonAsset, clones the description, None if not loaded yet), and Timeline::load_from_url(engine, url) (the original loadFromUrl, a future that yields Result<Timeline, String> when awaited inside an engine task). The easiest pattern is for the scene to request a JsonAsset in load, hand it to track_asset, and build the timeline with create_from_json_asset in initialize. Loading it together with other assets through AssetLoader also works.
  • Stage: build_stage(node_tree, parent_id, image_resolver) creates the document's stage nodes, attaches them under parent_id, and returns a name to NodeId table (HashMap). To hook tracks onto a node tree you already built, bind(node_tree, root_id) finds and attaches the nodes whose names match the tracks' target names. Targets are TimelineTarget: nodes are Node(NodeId), and things that are not nodes (such as screen effects) are External, set with set_target(name, target) or set_target_resolver(resolver).
  • Images: images imported in the editor go into the document's image field as data: URLs, and the runtime decodes them itself (on the spot on the desktop, and when the browser finishes decoding on the web). Images referenced by name must be returned by image_resolver. Pass the resolver as in Some(&|image_name: &str| image_table.get(image_name).cloned()).
  • Playback: play(), pause(), stop(node_tree) (back to the start), seek(node_tree, time) (fires no events), and tick(node_tree, time_delta) every frame. Functions that move nodes also take the NodeTree. Change looping and speed with set_loop and set_speed.
  • Notifications: set_marker_handler, set_event_handler, and set_complete_handler receive the timeline itself last, as in the original, followed by the node tree. Completion fires when the end is reached without looping. Handlers cannot borrow the scene, so put shared values in Rc<RefCell>.
  • For documents with sound nodes, instead of the original Timeline.setAudioManager, call load_sounds(node_tree, engine) once after building the stage to load an audio asset for each sound.

The detailed rules for tracks, easing, and keys are covered in the "Tweens, Animation, and Timelines" chapter.

Using Particle Editor assets

A .vfx.json is a list of particle system descriptions. Each system lists its name together with the description fields that ParticleSystem::apply_description accepts. The fields use the same names as the original (looping, emissionRate, shape, lifetime, renderShape, blendMode, and so on), and shapes and blend modes written as the original's strings are read into enums. Unknown shape and blend mode names are ignored.

Particle Editor at start: the view running the Fire preset, the preset list on the left, emission and shape settings on the right

{
	"systems": [
		{ "name": "Fire", "looping": true, "duration": 1, "emissionRate": 60, "shape": "cone", "lifetime": [0.6, 1.2], "speed": [80, 180], "renderShape": "circle", "blendMode": "lighter" }
	]
}

In the engine, create one node per system, attach a ParticleSystem, and apply the description. A ParticleSystem is playing from the start. In a game, load the file as a JsonAsset and loop over systems from get_data() as below. The editor also reads the old form without systems (an object with just one description), so for such files use the single object as the description.

use vanilla::{NodeTree, ParticleSystem, Vector2};

let vfx_asset_text = r#"{
	"systems": [
		{
			"name": "Sparks",
			"looping": true,
			"duration": 1,
			"emissionRate": 80,
			"shape": "circle",
			"shapeRadius": 10,
			"lifetime": [0.4, 0.8],
			"speed": [120, 260],
			"size": [3, 6],
			"gravity": [0, 300],
			"startColorA": [1, 0.9, 0.4, 1],
			"startColorB": [1, 0.5, 0.1, 1],
			"colorOverLifetime": [{ "time": 0, "color": [1, 1, 1, 1] }, { "time": 1, "color": [1, 0.3, 0.05, 0] }],
			"renderShape": "streak",
			"blendMode": "lighter"
		}
	]
}"#;

let vfx_asset: serde_json::Value = serde_json::from_str(vfx_asset_text).unwrap();
let description_list = match vfx_asset.get("systems").and_then(|systems| systems.as_array()) {
	Some(system_array) => {
		system_array.clone()
	}
	None => {
		vec![vfx_asset.clone()]
	}
};

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
for description in description_list.iter() {
	let system_name = description["name"].as_str().unwrap_or("ParticleSystem");
	let system_id = node_tree.create_child(root_id, system_name);
	let system_node = &mut node_tree[system_id];
	system_node.set_local_position(Vector2::create(640.0, 360.0));
	let particle_system = system_node.add_component(ParticleSystem::new());
	particle_system.apply_description(description);
	particle_system.simulate(0.5);
	assert!(particle_system.get_particle_count() > 0);
}
assert!(node_tree.find_child_by_name(root_id, "Sparks").is_some());

If a particle image was imported in the editor, it goes into the description's imageDataUrl field as a data: URL. Like the original, apply_description does not read this field, so load the image with ImageAsset::load(engine, data_url) (a data: URL works in place of an asset path) and, once it has loaded, pass it with set_image(image_asset.get_image()).

use vanilla::{Engine, EngineConfiguration, ImageAsset, ParticleSystem};

let engine = Engine::new(EngineConfiguration::new());
let description = serde_json::json!({
	"name": "Dust",
	"renderShape": "image",
	"imageDataUrl": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==",
});

let mut particle_system = ParticleSystem::new();
particle_system.apply_description(&description);
let mut image_asset = ImageAsset::new();
if let Some(image_data_url) = description["imageDataUrl"].as_str() {
	image_asset.load(&engine, image_data_url);
}
particle_system.set_image(image_asset.get_image());
assert!(image_asset.get_image().is_some());

The desktop decodes data: URLs on the spot. On the web they load later, so set the image once get_image() returns Some (or the load handle finishes). The meaning of the description fields is covered in the "Particles" chapter.

Loading Visual Editor scenes

The Visual Editor saves scenes in the same form as UI documents. The default save path is scenes/untitled.scene.json, and the editor-only frame size (frameSize) sits in the same JSON. UIDocument skips unknown keys, so the game reads the file as is with from_json_text. The scene root is a node named sceneRoot with its pivot at the top left. The Visual Editor's Sprite writes the image's project-relative path into imagePath, so if the asset folder is the same project folder, load_images reads the images by those paths.

Visual Editor with the UI Showcase components document open as a scene: hierarchy on the left, the scene view with move handles in the middle, the inspector on the right

The example below reads examples/assets/scenedocument.scene.json from the repository (a scene made in the Visual Editor save format). The scene requests the scene file as a TextAsset in load and tracks it, requests the images and builds the tree in initialize, and attaches late images in tick. The function given to set_clicked_event cannot borrow the scene, so the button press is signaled through an Rc<Cell> and picked up in tick. Some images in this scene file are not in the repository, so they print a warning and stay without an image.

use std::cell::Cell;
use std::path::PathBuf;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, Graphic, NodeId, Scene, SceneBase, TextAsset, UIButton, UIDocument, UILabel};

struct VisualScene {
	scene_base: SceneBase,
	scene_asset: TextAsset,
	document: UIDocument,
	label_id: Option<NodeId>,
	is_button_clicked: Rc<Cell<bool>>,
}

impl Scene for VisualScene {
	fn get_scene_base(&self) -> &SceneBase {
		&self.scene_base
	}

	fn get_scene_base_mut(&mut self) -> &mut SceneBase {
		&mut self.scene_base
	}

	fn load(&mut self, engine: &Engine) {
		self.scene_asset.load(engine, "scenedocument.scene.json");
		self.scene_base.track_asset(&self.scene_asset);
	}

	fn initialize(&mut self, engine: &Engine) {
		let scene_text = match self.scene_asset.get_text() {
			Some(scene_text) => {
				scene_text.to_string()
			}
			None => {
				return;
			}
		};
		self.document.register_editor_types();
		self.document.load_images(engine, &scene_text);
		let root_id = self.scene_base.get_root_id();
		let node_tree = self.scene_base.get_node_tree_mut();
		let scene_root_id = match self.document.from_json_text(node_tree, &scene_text) {
			Ok(scene_root_id) => {
				scene_root_id
			}
			Err(error) => {
				eprintln!("{}", error);
				return;
			}
		};
		node_tree.add_child(root_id, scene_root_id);

		self.label_id = UIDocument::find_node_by_path(node_tree, scene_root_id, "/Panel/label");
		if let Some(button_id) = UIDocument::find_node_by_path(node_tree, scene_root_id, "/Panel/button") {
			if let Some(button) = node_tree[button_id].get_component_mut::<UIButton>() {
				let is_button_clicked = self.is_button_clicked.clone();
				button.set_clicked_event(Box::new(move |_button: &mut UIButton, _context: &mut ComponentContext| {
					is_button_clicked.set(true);
				}));
			}
		}
	}

	fn tick(&mut self, time_delta: f64) {
		self.scene_base.tick(time_delta);
		let node_tree = self.scene_base.get_node_tree_mut();
		self.document.bind_pending_images(node_tree);
		if self.is_button_clicked.replace(false) {
			if let Some(label_id) = self.label_id {
				if let Some(label) = node_tree[label_id].get_component_mut::<UILabel>() {
					label.set_text("Clicked");
				}
			}
		}
	}

	fn pre_draw(&mut self, graphic: &mut Graphic) {
		self.scene_base.draw_scene_background(graphic);
	}
}

fn main() {
	let mut engine_configuration = EngineConfiguration::new();
	engine_configuration.asset_directory_paths = vec![PathBuf::from("examples/assets")];
	let engine = Engine::new(engine_configuration);
	let visual_scene = VisualScene {
		scene_base: SceneBase::new(),
		scene_asset: TextAsset::new(),
		document: UIDocument::new(),
		label_id: None,
		is_button_clicked: Rc::new(Cell::new(false)),
	};
	if let Err(error) = engine.run(Box::new(visual_scene)) {
		eprintln!("{}", error);
	}
}

Saving a selected node as a prefab creates a UI document at prefabs/<name>.prefab.json with that one node as its root. To use the same prefab many times, load the text once and call from_json_text each time you need one. Every call produces a new node tree.

use vanilla::{NodeTree, Paint, UIDocument, Vector2};

let prefab_json_text = r#"{"version":1,"root":{"type":"WorldNode","name":"Coin","contentSize":[80,80],"components":[{"type":"Paint","color":[1,0.8,0.2,1],"roundSize":40}],"children":[]}}"#;

let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let mut document = UIDocument::new();
for index in 0..3 {
	let coin_id = document.from_json_text(&mut node_tree, prefab_json_text).unwrap();
	node_tree[coin_id].set_local_position(Vector2::create(200.0 + index as f64 * 120.0, 120.0));
	node_tree.add_child(root_id, coin_id);
}
let coin_ids = node_tree.get_child_ids(root_id).to_vec();
assert_eq!(coin_ids.len(), 3);
assert!(node_tree[coin_ids[2]].get_component::<Paint>().is_some());
assert_eq!(node_tree[coin_ids[2]].get_local_position().x, 440.0);

VisualAsset reads scenes and prefabs saved by the Visual Editor (.scene.json, .prefab.json, in UIDocument form) and holds a Visual that carries the document. VisualAsset::load(engine, path) returns a load handle you can await; if the download fails or the document form (a JSON object with root) is wrong, it stays not loaded. Downloaded bytes also work with load_from_bytes, and text with VisualAsset::create_visual_from_text. Visual::create_node(&mut document, &mut node_tree) builds a new node tree from the document, sets the root's local position to location, and returns that root. (Ok(None) without a document, Err for a wrong document form; the caller attaches it to the parent it wants) This is the same as the original VisualAsset and Visual.createNode.

Other files shared by both engines

File Made by Where the Rust version reads it
Sprite atlas JSON and page images tools/atlas.cjs in vanilla.js SpriteAtlas::load, load_data
Language list languages.json and language files By hand or a translation tool languagepack ("Languages" chapter)
Font index fonts.json and group lists By hand or a font slicing tool uifont ("Languages" chapter)
Saved values (window.localStorage) The game LocalStorage, PersistedStore ("Storage" chapter)
Build version version.json The release build BuildInfo ("Developer Tools" chapter)
Sound files .webm (Opus) tools/webm.cjs in vanilla.js AudioAsset, AssetLoader ("Sound" chapter)
  • Saved values on the web use the same key and value form, number text, and PersistedStore {"version":..,"state":..} as the original, so the Rust version reads and writes saves of an original game on the same origin as is.
  • Sound decoding on the desktop uses the same set of decoders as Chrome, so it plays the Opus files made by webm.cjs as is.

Sprite atlases

SpriteAtlas reads JSON in the same two forms as the original.

  1. The form made by tools/atlas.cjs. { images: { "name.png": { x, y, width, height, page } }, meta: { imageFileName, pages: [{ imageFileName }] } }. Without page the image is on page 0, and without meta.pages there is one page, meta.imageFileName.
  2. Page paths and a table. { pagePaths: ["path"], frames: { "name": [page, x, y, width, height] } }

Image names can also be looked up without the extension ("coin.png" is also found as "coin"). A page path without a slash (/) is read relative to the folder of the JSON, and a path with one is read as written. load(engine, json_path) returns a SpriteAtlasLoadHandle that can be awaited, as in the original. The desktop loads everything on the spot. On the web the JSON and the page images arrive one after another, so call update(engine) in tick until is_loaded() is true, or await the handle.

use std::path::PathBuf;
use vanilla::{Engine, EngineConfiguration, Sprite, SpriteAtlas};

let mut engine_configuration = EngineConfiguration::new();
engine_configuration.asset_directory_paths = vec![PathBuf::from("examples/assets")];
let engine = Engine::new(engine_configuration);

let mut sprite_atlas = SpriteAtlas::new();
sprite_atlas.load(&engine, "atlas/icons.json");
assert!(sprite_atlas.is_loaded());
assert!(sprite_atlas.has_frame("coin"));
assert!(sprite_atlas.has_frame("coin.png"));

let mut sprite = Sprite::new();
assert!(sprite_atlas.apply_frame(&mut sprite, "star"));

Detailed usage is covered in the "Assets" chapter.

Command-line tools

The only command-line tool in the Rust repository is vanilla-engine-apidoc, which extracts the API documentation data. Full usage is in tools/TOOLS.md.

cargo run -p vanilla-engine-apidoc -- 0.4.0
cargo run -p vanilla-engine-apidoc -- 0.4.0 --check
cargo test --doc -p vanilla-engine-apidoc
  • The first line writes docs/api/<version>.json from the doc comments in the engine source. The data format is the same as vanilla.js's apidoc.cjs.
  • --check checks the data format and this guide (whether the ko and en file names and code blocks match).
  • cargo test --doc -p vanilla-engine-apidoc compiles and runs this guide's rust code blocks as doctests.

The vanilla.js command-line tools run on Node.js, and of their outputs the Rust version uses the two in the table above. SpriteAtlas reads the atlases from atlas.cjs, and AudioAsset reads the .webm files from webm.cjs. project.cjs (bundling with esbuild) and mangle.cjs (shortening names in bundled JavaScript) are for JavaScript builds and have no use in the Rust version. Desktop builds of the Rust version use cargo build, and web builds use cargo build --target wasm32-unknown-unknown. (See the "Project Setup" chapter.)

Things to watch for

  • UI documents contain only image paths. Call load_images or set set_image_resolver before from_json_text. When images arrive late, as on the web, call bind_pending_images from tick.
  • If component and node types used in an editor are missing from the game's UIDocument table, they print a warning and are dropped or become plain nodes. Games that read editor documents call register_editor_types().
  • To write and read game components in documents, add #[vanilla::reflect] and register them.
  • A Timeline moves only if you call tick every frame. For documents with sound, call load_sounds after building the stage.
  • apply_description does not read imageDataUrl in a .vfx.json. Load the image yourself and pass it with set_image.
  • Visual::create_node builds with the tables of the UIDocument you pass. For documents saved by the editor, call register_editor_types() on that document first.

UIDocument, UIDocumentSaveContext, UIDocumentLoadContext, NodeTree, TextAsset, JsonAsset, ImageAsset, Image, Timeline, TimelineTarget, TimelineMarker, ParticleSystem, WorldNode, UIButton, UILabel, UIInputField, UIScrollBar, UISnapScrollView, Visual, VisualAsset, SpriteAtlas, SpriteAtlasLoadHandle, AudioAsset, AssetLoader