# ui/buttonmotion (vanilla.js 0.4.0)

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

단추 반응. (올리면 커지고, 누르는 동안 줄고, 떼면 통 튀고, 누를 수 없으면 흔들림, 창 안은 커지지 않음, 8.1 절)

## ButtonMotion extends Component

단추 반응. (올리면 커지고, 누르는 동안 줄고, 떼면 통 튀고, 누를 수 없으면 흔들림, 창 안은 커지지 않음, 8.1 절)

단추 노드에 UIButton 다음으로 붙이면, 프레임마다 그 단추의 상태를 읽어 노드를 가운데를 두고 키우고 줄인다.
색은 건드리지 않는다. (글 단추의 바탕과 글자 색은 화면의 코드가 정하고, 그림 단추는 UIButton 의 tint 가 맡음)
손가락이 주된 기기는 올림을 쓰지 않는다. (뗀 뒤에도 손가락 자리가 남아 올린 채로 보이지 않게)
반응이 없을 때는 노드의 자리와 크기를 건드리지 않아, 다른 곳이 같은 노드를 키우는 연출(나타날 때의 통 튐)과 겹치지 않는다.
노드가 커져도 커진 자리가 처음 자리를 다 덮으므로, 올림 판정이 경계에서 떨지 않는다.
꼴은 setStyle 로 단추마다 정하거나, 비워 두면 위 노드 가운데 setButtonMotionArea 로 꼴을 건 가장 가까운 것을 따르고, 없으면 보통이다. (창 안은 inset)
inset 꼴에서 올린 동안은 단추 노드의 맨 밑 자식으로 흰 테 판과 밝힌 바탕 판을 둔다. (바탕이 보이지 않는 단추는 밝히지 않음, 그 둘만은 이 부품이 색을 정함)

예제: 눌러지는 단추와 누를 수 없을 때 흔들기

```js
import { Engine, EngineConfiguration, UIScene, UIButton, UILabel, Paint, WorldNode, Color, Pivot, Vector2, attachButtonMotion, shakeButton } from "./build/vanilla.js";

class ButtonMotionScene extends UIScene {
	create() {
		super.create();
		const buttonNode = new WorldNode();
		buttonNode.setName("BuyButton");
		buttonNode.setPivot(Pivot.topLeft.clone());
		buttonNode.setAnchor(Pivot.topLeft.clone());
		buttonNode.setContentSize(Vector2.create(160, 48));
		buttonNode.setLocalPosition(Vector2.create(240, 156));
		const buttonPaint = buttonNode.addComponent(Paint);
		buttonPaint.setColor(new Color(0.36, 0.42, 1, 1));
		buttonPaint.setRoundSize(10);
		const button = buttonNode.addComponent(UIButton);
		const buttonLabel = buttonNode.addComponent(UILabel);
		buttonLabel.setText("Buy");
		buttonLabel.setFontSize(18);
		attachButtonMotion(buttonNode);
		let coinCount = 40;
		button.setClickedEvent(() => {
			if (coinCount < 100) {
				shakeButton(buttonNode);
				return;
			}
			coinCount -= 100;
		});
		const root = this.getRoot();
		root.addChild(buttonNode);
		shakeButton(buttonNode);
	}
}

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

- `constructor()` (constructor, since 0.4.0): 생성.
- `setStyle(style)` (method, since 0.4.0): 이 단추의 꼴 정하기. (ButtonMotionStyle, 빈 글이면 위 노드에 건 꼴을 따름)
  - `style` string: 꼴. (ButtonMotionStyle 의 값, 빈 글이면 위 노드에 건 꼴)
- `readMotionStyle(node)` (method, since 0.4.0): 이 단추가 따를 꼴. (정한 것, 없으면 위 노드에 건 가장 가까운 꼴, 그것도 없으면 보통)
  - `node` WorldNode: 단추 노드. (부모부터 거슬러 오르며 setButtonMotionArea 로 건 꼴을 찾는다)
  - returns string: 따를 꼴. (ButtonMotionStyle 의 값)
- `attach(node)` (method, since 0.4.0): 붙음. (같은 노드의 단추를 찾아 둠)
  - `node` WorldNode: 붙을 노드. (그 노드에 UIButton 이 없으면 반응하지 않는다)
- `shake()` (method, since 0.4.0): 누를 수 없을 때 흔들기. (누른 쪽에서 부른다)
- `tick(timeDelta)` (method, since 0.4.0): 갱신. (단추 상태에 따라 크기를 맞추고, 노드의 가운데를 두고 놓음)
  - `timeDelta` number: 지난 프레임부터의 시간. (초)
- `mutePressTint(isMuted)` (method, since 0.4.0): 단추의 눌림 색을 투명으로 바꾸거나 되돌리기. (글로시 껍데기가 눌림을 그리는 동안 투명, 껍데기를 끄면 정한 색)
  - `isMuted` boolean: 참이면 눌림 색의 알파를 0 으로, 거짓이면 처음 읽어 둔 눌림 색으로 되돌린다.
- `readScrollPressed(node, buttonState)` (method, since 0.4.0): 스크롤 칸 안에서 누르고 있는지. (엔진은 스크롤 칸 안 단추에 누르는 동안 눌림을 주지 않으므로, 누름이 이 단추 위에서 시작했고 손이 아직 단추 위이며 스크롤 칸이 밀리지 않았으면 눌린 것으로 그림, 밀리기 시작하면 풂)
  - `node` WorldNode: 단추 노드.
  - `buttonState` string: 단추 상태. (ButtonState 의 값, disabled 면 거짓)
  - returns boolean: 눌린 것으로 그릴 때 참. (스크롤 칸이나 엔진(System.vanillaEngine)이 없으면 거짓)
- `readScrollView(node)` (method, since 0.4.0): 단추를 담은 가장 가까운 스크롤 칸. (처음 한 번 위 노드를 거슬러 찾아 둠, 없으면 null)
  - `node` WorldNode: 단추 노드.
  - returns UIScrollView | null: 가장 가까운 조상의 스크롤 칸. (없으면 null)
- `placeLitParts(node, isLit, isPressed)` (method, since 0.4.0): 밝힌 판 놓기. (흰 테 판은 단추 크기, 밝힌 바탕 판은 테 굵기만큼 안쪽, 둘 다 단추의 맨 밑 자식이라 글과 그림 밑)
  - `node` WorldNode: 단추 노드.
  - `isLit` boolean: 참이면 밝힌다. (단추 바탕 Paint 의 알파가 0 이면 밝히지 않는다)
  - `isPressed` boolean: 누르는 중인지. (밝히는 함수에만 넘긴다)
- `createLitParts(node)` (method, since 0.4.0): 밝힌 판 만들기. (단추 노드의 맨 밑 자식 둘, 단추의 눌림 색은 받지 않음)
  - `node` WorldNode: 단추 노드. (맨 밑 자식 둘을 더한다)
  - returns object: { ringNode, ringPaint, fillNode, fillPaint, fillColor }
- `isInputOver(node)` (method, since 0.4.0): 손(마우스나 손가락)이 노드 위인지.
  - `node` WorldNode: 볼 노드.
  - returns boolean: 입력 위치가 노드 안이면 참. (엔진(System.vanillaEngine)이 없으면 거짓)
- `placeAround(node, scale, shakeOffset)` (method, since 0.4.0): 노드를 가운데를 두고 그 크기로 놓기. (노드는 왼쪽 위가 기준이라 커진 만큼 자리를 옮김)
    - 그 사이 화면이 노드를 다시 놓았으면(자리가 이 부품이 놓은 것과 다르면) 그 자리를 새 기준으로 삼는다.
  - `node` WorldNode: 단추 노드.
  - `scale` number: 배율. (1 이 보통 크기)
  - `shakeOffset` number: 가로로 밀 거리. (픽셀)
- `setLitHandler(litHandler)` (method, since 0.4.0): 밝힘을 맡는 함수 설정. (단추에 얹은 꾸밈이 제 모양으로 밝힐 때, null 이면 이 부품이 흰 테 판과 밝힌 바탕 판을 둔다)
  - `litHandler` function(WorldNode, boolean, boolean): void | null: (단추 노드, 밝히는지, 누르는 중인지)
- `getLitHandler()` (method, since 0.4.0): 밝힘을 맡는 함수 반환.
  - returns function(WorldNode, boolean, boolean): void | null: 밝힘을 맡는 함수. (없으면 null)

## Functions and constants

- `attachButtonMotion(node)` (method, since 0.4.0): 단추 노드에 반응 붙이기. (UIButton 이 이미 붙은 노드, ButtonMotion 을 돌려줌)
  - `node` WorldNode: UIButton 이 붙은 단추 노드.
  - returns ButtonMotion: 붙인 반응 부품.
- `setButtonMotionArea(node, style)` (method, since 0.4.0): 노드 밑의 단추가 따를 꼴 걸기. (창이 제 판에 inset 을 걸면 그 안의 단추는 커지지 않음, 단추가 setStyle 로 정한 꼴이 먼저)
  - `node` WorldNode: 꼴을 걸 노드. (그 아래 모든 단추가 따른다)
  - `style` string: ButtonMotionStyle

  예제: 판 안의 단추를 inset 꼴로

  ```js
  import { WorldNode, UIButton, ButtonMotionStyle, attachButtonMotion, setButtonMotionArea } from "./build/vanilla.js";
  
  const panelNode = new WorldNode();
  setButtonMotionArea(panelNode, ButtonMotionStyle.inset);
  const buttonNode = new WorldNode();
  buttonNode.addComponent(UIButton);
  panelNode.addChild(buttonNode);
  const buttonMotion = attachButtonMotion(buttonNode);
  const areaStyle = buttonMotion.readMotionStyle(buttonNode);
  console.log(areaStyle);
  buttonMotion.setStyle(ButtonMotionStyle.normal);
  const ownStyle = buttonMotion.readMotionStyle(buttonNode);
  console.log(ownStyle);
  ```

- `shakeButton(node)` (method, since 0.4.0): 누를 수 없는 단추를 흔들기. (반응이 붙은 단추만, 그 밖은 아무 일도 없음)
  - `node` WorldNode: ButtonMotion 이 붙은 단추 노드.

## ButtonMotionStyle (enum)

단추 반응의 꼴.
  - normal: 올리면 커지고, 누르는 동안 줄고, 떼면 통 튄다.
  - inset: 창 안의 단추. 1 배를 넘게 커지지 않아 창 안쪽 여백을 넘지 않는다. 올리면 바탕이 밝아지고 안쪽에 흰 테가 서며, 누르면 조금 줄었다 돌아온다.

- `normal` = `normal`: 올리면 커지고, 누르는 동안 줄고, 떼면 통 튄다.
- `inset` = `inset`: 창 안의 단추. (커지지 않고, 올리면 바탕이 밝아지고 안쪽에 흰 테가 서며, 누르면 조금 줄었다 돌아온다)
