vanilladocs
vanillapunch.com
API / ui/uilistview

ui/uilistview

재활용 리스트 뷰.

Recycling list view.

UIListViewextends Component

짝Counterpart: vanilla-engine UIListView

재활용 리스트 뷰.

  • 템플릿 노드 하나와 항목 수만 받아, 화면에 보이는 범위만 실제 노드로 만들고 벗어난 노드는 회수해 다시 쓴다. 수백 ~ 수천 건도 노드 수는 화면 분량에 머문다.
  • UIScrollView 와 같은 노드에 붙는다. (require 로 자동 부착) 항목 내용은 setBindItemEvent 로 코드에서 채운다. (데이터 주도)
  • 끝 근처에 닿으면 setReachEndEvent 알림을 한 번 보낸다. (무한 스크롤 / 더 불러오기) setItemCount 로 항목 수가 늘어나면 알림이 다시 무장된다.
  • 템플릿 대신 항목 노드를 만드는 함수(setCreateItemEvent)를 줄 수도 있다. 항목 크기를 정하지 않았으면 처음 만든 항목의 크기를 읽는다.
  • 사용:
const listView = scrollNode.addComponent(UIListView);
listView.setTemplateNode(templateNode);
listView.setBindItemEvent((itemNode, itemIndex) => { ... });
listView.setItemCount(500);
listView.setReachEndEvent(() => { ... });

Recycling list view.

  • Takes just one template node and an item count, creates real nodes only for the visible range, and reclaims and reuses nodes that go out of range. Even with hundreds ~ thousands of items, the node count stays at one screen's worth.
  • Attaches to the same node as UIScrollView. (auto-attached via require) Item content is filled in code with setBindItemEvent. (data-driven)
  • When it nears the end, it sends a setReachEndEvent notification once. (infinite scroll / load more) When the item count grows via setItemCount, the notification is re-armed.
  • Instead of a template, you can give a function that creates item nodes (setCreateItemEvent). If the item size is not set, it reads the size of the first created item.
  • Usage:
const listView = scrollNode.addComponent(UIListView);
listView.setTemplateNode(templateNode);
listView.setBindItemEvent((itemNode, itemIndex) => { ... });
listView.setItemCount(500);
listView.setReachEndEvent(() => { ... });
예제Example500 줄 목록을 템플릿 하나로 보이기Show a 500 row list with a single template
import { Engine, EngineConfiguration, UIScene, UIListView, UILabel, WorldNode, Paint, Color, Pivot, Vector2 } from "./build/vanilla.js";

class ListScene extends UIScene {
	create() {
		super.create();
		const listNode = new WorldNode();
		listNode.setPivot(Pivot.topLeft.clone());
		listNode.setAnchor(Pivot.topLeft.clone());
		listNode.setContentSize(Vector2.create(320, 240));
		listNode.setLocalPosition(Vector2.create(160, 60));

		const templateNode = new WorldNode();
		templateNode.setPivot(Pivot.topLeft.clone());
		templateNode.setAnchor(Pivot.topLeft.clone());
		templateNode.setContentSize(Vector2.create(320, 36));
		const rowPaint = templateNode.addComponent(Paint);
		rowPaint.setColor(new Color(1, 1, 1, 0.08));
		const rowLabel = templateNode.addComponent(UILabel);
		rowLabel.setFontSize(16);
		listNode.addChild(templateNode);

		const listView = listNode.addComponent(UIListView);
		listView.setTemplateNode(templateNode);
		listView.setSpacing(4);
		listView.setBindItemEvent((itemNode, itemIndex) => {
			const itemLabel = itemNode.getComponent(UILabel);
			itemLabel.setText("Row " + itemIndex);
		});
		listView.setItemCount(500);
		listView.setReachEndEvent(() => {
			const itemCount = listView.getItemCount();
			listView.setItemCount(itemCount + 100);
		});
		const root = this.getRoot();
		root.addChild(listNode);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new ListScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
생성자constructorconstructor()#0.3.0 부터since 0.3.0소스source

생성.

Constructor.

짝Counterpart: vanilla-engine UIListView.new

메서드methodrequire()#0.3.0 부터since 0.3.0소스source

의존 컴포넌트.

Dependent components.

반환ReturnsFunction[] 같은 노드에 함께 붙어야 하는 컴포넌트. (UIScrollView, 없으면 addComponent 가 먼저 붙인다)Components that must be attached to the same node. (UIScrollView, addComponent attaches it first if missing)

메서드methodsetTemplateNode(templateNode)#0.3.0 부터since 0.3.0소스source

템플릿 설정.

  • 노드를 직렬화해 두고 필요할 때 복제한다. 원본은 감춘다. 항목 크기는 템플릿의 contentSize 진행 축 값에서 읽는다.
  • 엔진 기본 종류만 등록한 새 UIDocument 로 복제한다. 게임 컴포넌트가 든 템플릿은 setTemplateNodeWithDocument 를 쓴다.
  • 항목은 템플릿의 그림을 그대로 쓴다. (문서가 템플릿을 적을 때 imagePath 를 적은 컴포넌트의 그림을 기억한다, UIDocument.serializeNode)

Sets the template.

  • Serializes the node and clones it when needed. The original is hidden. The item size is read from the template's contentSize value along the scroll axis.
  • Clones with a new UIDocument that has only the engine's default types. For a template with game components, use setTemplateNodeWithDocument.
  • Items use the template's images as they are. (the document remembers the image of each component that writes an imagePath when it writes the template, UIDocument.serializeNode)
templateNodeimport("../core/node/worldnode.js").WorldNode
항목의 본이 될 노드. (UIDocument 로 직렬화해 쥐고 이 노드는 끈다, 항목 크기를 정하지 않았으면 이 노드의 contentSize 진행 축 값을 쓴다, 앞서 준 만들기 함수는 지운다)Node used as the item template. (Serialized with UIDocument and this node is turned off, if the item size is not set the contentSize along the main axis of this node is used, a previously given create function is cleared)

짝Counterpart: vanilla-engine UIListView.set_template_node

메서드methodsetTemplateNodeWithDocument(templateNode, uiDocument)#0.4.0 부터since 0.4.0소스source

템플릿 설정. (게임 컴포넌트를 등록한 UIDocument 로 직렬화하고 복제한다)

Sets the template. (serializes and clones with a UIDocument that has the game components registered)

templateNodeimport("../core/node/worldnode.js").WorldNode
항목의 본이 될 노드. (setTemplateNode 와 같다)The node that items are based on. (same as setTemplateNode)
uiDocumentUIDocument
템플릿을 쓰고 읽을 문서. (템플릿의 컴포넌트 종류를 등록해 둔 것, 항목을 만들 때마다 이 문서로 읽는다)The document that writes and reads the template. (one with the template's component types registered, items are read with this document every time one is made)

짝Counterpart: vanilla-engine UIListView.set_template_node_with_document

예제Example게임 컴포넌트가 든 템플릿으로 목록 짓기Build a list from a template with a game component
import { UIDocument, UIListView, WorldNode, Component, Vector2 } from "./build/vanilla.js";

class Badge extends Component {
}

const uiDocument = new UIDocument();
uiDocument.registerComponentType("Badge", Badge);
const listNode = new WorldNode();
listNode.setContentSize(Vector2.create(200, 300));
const listView = listNode.addComponent(UIListView);
const templateNode = new WorldNode();
templateNode.setContentSize(Vector2.create(200, 40));
templateNode.addComponent(Badge);
listView.setTemplateNodeWithDocument(templateNode, uiDocument);
console.log(templateNode.isActive(), listView.getItemSize());
메서드methodsetCreateItemEvent(createItemEvent)#0.4.0 부터since 0.4.0소스source

항목 노드 만들기 함수 설정. (템플릿 대신, () => WorldNode. 만든 노드는 content 노드의 자식으로 붙인다)

Sets the item node creation function. (instead of a template, () => WorldNode. Created nodes are attached as children of the content node)

createItemEventFunction | null
항목 노드를 만들어 돌려줄 함수. (() => WorldNode, 함수를 주면 앞서 준 템플릿을 버리고, null 이면 만들기 함수를 지운다)Function that creates and returns an item node. (() => WorldNode, giving a function drops a previously given template, null clears the create function)

짝Counterpart: vanilla-engine UIListView.set_create_item_event

예제Example만들기 함수로 보이는 줄만 노드 만들기Create nodes only for visible rows with a create function
import { UIListView, UIScrollView, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";

const listNode = new WorldNode();
listNode.setContentSize(Vector2.create(200, 100));
const listView = listNode.addComponent(UIListView);
listView.setCreateItemEvent(() => {
	const itemNode = new WorldNode();
	itemNode.setPivot(Pivot.topLeft.clone());
	itemNode.setAnchor(Pivot.topLeft.clone());
	itemNode.setContentSize(Vector2.create(200, 20));
	return itemNode;
});
listView.setItemCount(100);
listView.tick(0);
console.log(listView.getItemSize(), listView.getActiveItemCount());
const scrollView = listNode.getComponent(UIScrollView);
scrollView.setScrollOffset(Vector2.create(0, -500));
listView.tick(0);
console.log(listView.getItemNode(0), listView.getItemNode(25) !== null);
메서드methodsetItemCount(itemCount)#0.3.0 부터since 0.3.0소스source

항목 수 설정. (스크롤 영역 갱신 + 끝 알림 재무장)

Sets the item count. (updates the scroll area + re-arms the end notification)

itemCountnumber
항목 수. (0 보다 작으면 0, 늘어나면 끝 알림을 다시 무장하고, 스크롤 영역을 다시 재고 보이는 항목을 다시 바인딩한다)Item count. (Below 0 becomes 0, an increase re-arms the end notification, the scroll area is measured again and the visible items are bound again)

짝Counterpart: vanilla-engine UIListView.set_item_count

메서드methodtick(timeDelta)#0.3.0 부터since 0.3.0소스source

갱신. (보이는 범위 계산 → 회수 / 생성)

Update. (computes the visible range → reclaim / create)

timeDeltanumber
지난 프레임부터의 시간. (초, 쓰지 않는다)Time since the previous frame. (Seconds, unused)
메서드methodrefresh()#0.3.0 부터since 0.3.0소스source

보이는 항목 전부 다시 바인딩. (데이터 내용이 바뀌었을 때)

Rebinds all visible items. (when the data content changes)

짝Counterpart: vanilla-engine UIListView.refresh

메서드methodgetItemNode(itemIndex)#0.3.0 부터since 0.3.0소스source

특정 항목의 실제 노드 반환. (화면 밖이면 null)

Returns the real node of a given item. (null if off-screen)

itemIndexnumber
항목 순서. (0 부터)Item index. (From 0)

반환Returnsimport("../core/node/worldnode.js").WorldNode | null 그 항목을 보이는 노드. (보이는 범위와 여유 항목 밖이라 노드가 없으면 null, 노드는 범위를 벗어나면 다른 항목에 다시 쓰인다)Node showing that item. (null when it is outside the visible range and buffer so no node exists, nodes are reused for other items once they leave the range)

짝Counterpart: vanilla-engine UIListView.get_item_node

메서드methodsetBindItemEvent(bindItemEvent)#0.3.0 부터since 0.3.0소스source

항목 바인딩 함수 설정.

Sets the item bind function.

bindItemEventFunction
항목 노드에 내용을 채울 함수. ((itemNode, itemIndex) => void, 노드를 그 항목에 새로 꺼내 쓸 때와 refresh, setItemCount 때 부른다)Function that fills an item node. ((itemNode, itemIndex) => void, called when a node is newly taken for that item, and on refresh and setItemCount)

짝Counterpart: vanilla-engine UIListView.set_bind_item_event

메서드methodsetReachEndEvent(reachEndEvent)#0.3.0 부터since 0.3.0소스source

끝 알림 함수 설정. (무한 스크롤, 더 불러오기)

Sets the end notification function. (Infinite scroll, load more)

reachEndEventFunction
남은 길이가 setReachEndThreshold 안에 들면 한 번 부를 함수. (() => void, setItemCount 로 항목이 늘면 다시 부를 수 있다)Function called once when the remaining length falls within setReachEndThreshold. (() => void, can be called again after setItemCount adds items)

짝Counterpart: vanilla-engine UIListView.set_reach_end_event

예제Example끝에 닿으면 다음 20 개 붙이기Append the next 20 when reaching the end
import { UIListView, UIScrollView, WorldNode, Pivot, Vector2 } from "./build/vanilla.js";

const listNode = new WorldNode();
listNode.setContentSize(Vector2.create(200, 100));
const listView = listNode.addComponent(UIListView);
listView.setItemSize(20);
listView.setCreateItemEvent(() => {
	const itemNode = new WorldNode();
	itemNode.setPivot(Pivot.topLeft.clone());
	itemNode.setAnchor(Pivot.topLeft.clone());
	itemNode.setContentSize(Vector2.create(200, 20));
	return itemNode;
});
let reachEndCount = 0;
listView.setReachEndEvent(() => {
	reachEndCount += 1;
	const itemCount = listView.getItemCount();
	listView.setItemCount(itemCount + 20);
});
listView.setReachEndThreshold(40);
listView.setItemCount(20);
listView.tick(0);
console.log(reachEndCount, listView.getItemCount());
const scrollView = listNode.getComponent(UIScrollView);
scrollView.setScrollOffset(Vector2.create(0, -280));
listView.tick(0);
console.log(reachEndCount, listView.getItemCount());
메서드methodsetReachEndThreshold(reachEndThreshold)#0.3.0 부터since 0.3.0소스source

끝 알림 거리 설정.

Sets the end notification distance.

reachEndThresholdnumber
끝에서 이 길이 안에 들면 알린다. (픽셀, 기본 120)Notifies when within this length of the end. (Pixels, default 120)

짝Counterpart: vanilla-engine UIListView.set_reach_end_threshold

메서드methodsetItemSize(itemSize)#0.3.0 부터since 0.3.0소스source

항목 크기 설정.

Sets the item size.

itemSizenumber
진행 축 방향 항목 크기. (픽셀, 0 이하면 템플릿이나 처음 만든 항목의 크기를 읽는다, 스크롤 영역을 다시 잰다)Item size along the main axis. (Pixels, 0 or less reads the size of the template or of the first created item, the scroll area is measured again)

짝Counterpart: vanilla-engine UIListView.set_item_size

메서드methodsetSpacing(spacing)#0.3.0 부터since 0.3.0소스source

항목 간격 설정.

Sets the item spacing.

spacingnumber
항목 사이 간격. (픽셀, 기본 0, 스크롤 영역을 다시 잰다)Spacing between items. (Pixels, default 0, the scroll area is measured again)

짝Counterpart: vanilla-engine UIListView.set_spacing

메서드methodsetHorizontal(isHorizontal)#0.3.0 부터since 0.3.0소스source

진행 방향 설정.

Sets the layout direction.

isHorizontalboolean
참이면 항목을 가로로 늘어놓는다. (기본 세로, 바뀌면 스크롤 영역을 다시 재고 보이는 항목을 새 축에 다시 놓는다, 템플릿이나 항목 크기를 읽기 전에 정한다, 스크롤 뷰의 축은 따로 정한다)True lays items out horizontally. (Default vertical, a change remeasures the scroll area and places the visible items again on the new axis, set it before the template or item size is read, the scroll view axis is set separately)

짝Counterpart: vanilla-engine UIListView.set_horizontal

메서드methodsetBufferItemCount(bufferItemCount)#0.3.0 부터since 0.3.0소스source

화면 밖 여유 항목 수 설정.

Sets the number of off screen buffer items.

bufferItemCountnumber
보이는 범위 앞뒤로 더 만들어 둘 항목 수. (0 보다 작으면 0, 기본 1)Number of extra items to keep before and after the visible range. (Below 0 becomes 0, default 1)

짝Counterpart: vanilla-engine UIListView.set_buffer_item_count

메서드methodgetItemCount()#0.3.0 부터since 0.3.0소스source

항목 수 반환.

Returns the item count.

반환Returnsnumber setItemCount 로 정한 항목 수.Item count set with setItemCount.

짝Counterpart: vanilla-engine UIListView.get_item_count

메서드methodgetItemSize()#0.3.0 부터since 0.3.0소스source

항목 크기 반환.

Returns the item size.

반환Returnsnumber 진행 축 방향 항목 크기. (픽셀, 아직 모르면 0)Item size along the main axis. (Pixels, 0 if not known yet)

짝Counterpart: vanilla-engine UIListView.get_item_size

메서드methodgetSpacing()#0.3.0 부터since 0.3.0소스source

항목 간격 반환.

Returns the item spacing.

반환Returnsnumber 항목 사이 간격. (픽셀)Spacing between items. (Pixels)

짝Counterpart: vanilla-engine UIListView.get_spacing

메서드methodisHorizontal()#0.4.0 부터since 0.4.0소스source

가로 진행 여부 반환.

Returns whether the layout is horizontal.

반환Returnsboolean 항목을 가로로 늘어놓으면 참. (기본 거짓)True if items are laid out horizontally. (Default false)

짝Counterpart: vanilla-engine UIListView.is_horizontal

메서드methodgetActiveItemCount()#0.3.0 부터since 0.3.0소스source

보이는 항목 노드 수 반환.

Returns the number of visible item nodes.

반환Returnsnumber 지금 항목을 맡은 노드 수. (보이는 범위와 여유 항목, 회수해 둔 노드는 세지 않는다)Number of nodes currently assigned to items. (Visible range and buffer items, recycled nodes are not counted)

짝Counterpart: vanilla-engine UIListView.get_active_item_count