# ui/uidropdown (vanilla.js 0.4.0)

> https://vanillapunch.com/docs/vanilla.js/0.4.0/api/ui/uidropdown/

드롭다운. (콤보박스)

## UIDropdown extends WorldNode

드롭다운. (콤보박스)
- 닫힌 머리 단추에 현재 값이 보이고, 누르면 아래로 선택지 목록이 펼쳐진다.
- 사용:
    const dropdown = new UIDropdown({ width: 180, height: 30 });
    dropdown.setOptionList(["Dark", "Light", "System"]);
    dropdown.setChangedEvent((self) => { self.getSelectedText(); });
    panelNode.addChild(dropdown);

예제: 화질 고르기 드롭다운

```js
import { Engine, EngineConfiguration, UIScene, UIDropdown, Color, Vector2 } from "./build/vanilla.js";

class DropdownScene extends UIScene {
	create() {
		super.create();
		const dropdown = new UIDropdown({ width: 200, height: 32, fontSize: 14, highlightColor: new Color(0.3, 0.8, 1, 0.3) });
		dropdown.setOptionList(["Low", "Medium", "High", "Ultra"]);
		dropdown.setSelectedIndex(2);
		dropdown.setChangedEvent((self) => {
			const selectedText = self.getSelectedText();
			console.log(selectedText);
		});
		dropdown.setLocalPosition(Vector2.create(220, 80));
		const root = this.getRoot();
		root.addChild(dropdown);
		dropdown.setOpen(true);
	}
}

const engineConfiguration = new EngineConfiguration();
const engine = new Engine(engineConfiguration);
const scene = new DropdownScene();
scene.setLoadingMinDurationMs(0);
engine.run(scene);
```

- `constructor(options = {})` (constructor, since 0.3.0): 생성.
  - `options` object: { width = 180, height = 30, optionHeight = 28, fontSize = 12,
                            baseColor?, popupColor?, highlightColor?, textColor? }
- `setOptionList(optionList)` (method, since 0.3.0): 선택지 목록 설정. (펼침 목록 행을 다시 만든다)
  - `optionList` string[]: 선택지 글 목록. (복사해 쥔다, 선택 순서가 목록 밖이면 마지막으로 줄인다)
- `selectIndex(optionIndex)` (method, since 0.3.0): 선택 처리.
  - `optionIndex` number: 고를 선택지 순서. (목록 밖이면 아무것도 하지 않는다, 고르면 목록을 접고 바뀌었을 때만 changedEvent 를 부른다)
- `setOpen(isOpen)` (method, since 0.3.0): 펼침 / 접힘.
  - 펼칠 때 목록을 트리 뿌리로 옮겨 나중에 그려지는 형제 노드에 가려지지 않게 한다. (접으면 되돌린다)
  - `isOpen` boolean: 참이면 펼친다.
- `setChangedEvent(changedEvent)` (method, since 0.3.0): 선택이 바뀌었을 때 처리기 설정. (selectIndex 로 다른 것을 골랐을 때만, setSelectedIndex 로는 부르지 않는다)
  - `changedEvent` Function: (self)

  예제: 고른 선택지 받기

  ```js
  import { UIDropdown } from "./build/vanilla.js";
  
  const dropdown = new UIDropdown({ width: 160 });
  dropdown.setOptionList(["Low", "Medium", "High"]);
  dropdown.setChangedEvent((self) => {
  	const selectedText = self.getSelectedText();
  	console.log(selectedText);
  });
  dropdown.setSelectedIndex(1);
  dropdown.selectIndex(2);
  ```

- `setSelectedIndex(selectedIndex)` (method, since 0.3.0): 선택 순서 설정. (처리기를 부르지 않고 머리 글과 강조만 바꾼다)
  - `selectedIndex` number: 선택 순서. (0 ~ 선택지 수 - 1 로 자른다)
- `getSelectedIndex()` (method, since 0.3.0): 선택 순서 반환.
  - returns number: 선택 순서. (선택지가 없어도 0)
- `getSelectedText()` (method, since 0.3.0): 고른 선택지 글 반환.
  - returns string: 고른 선택지 글. (선택지가 없으면 빈 글)
- `isOpen()` (method, since 0.3.0): 펼쳐져 있는지 반환.
  - returns boolean: 펼쳐져 있으면 참.
- `getOptionList()` (method, since 0.4.0): 선택지 목록 반환.
  - returns string[]: 선택지 글 목록. (setOptionList 로 준 차례, 고쳐 쓰지 않는다)
- `getPopupNode()` (method, since 0.4.0): 펼침 목록 노드 반환. (펼친 동안은 트리 뿌리의 자식)
  - returns WorldNode: 펼침 목록 노드.
- `getOptionNodes()` (method, since 0.4.0): 선택지 행 노드 목록 반환. (선택지 차례, setOptionList 마다 다시 만든다)
  - returns WorldNode[]: 행 노드. (새 배열)

  예제: 선택지 행 노드를 읽어 둘째 행을 누른 것처럼 고르기

  ```js
  import { UIDropdown } from "./build/vanilla.js";
  
  const dropdown = new UIDropdown({ width: 160 });
  dropdown.setOptionList(["Low", "Medium", "High"]);
  const optionNodes = dropdown.getOptionNodes();
  console.log(optionNodes.length, optionNodes[1].getName(), dropdown.getOptionList()[1]);
  dropdown.selectIndex(1);
  console.log(dropdown.getSelectedText());
  ```

