# ui/autolayout/layoutvariable (vanilla-engine 0.4.0)

> https://vanillapunch.com/docs/vanilla-engine/0.4.0/api/ui/autolayout/layoutvariable/

레이아웃 솔버 미지수. (원본 ui/autolayout/layoutvariable.js 의 LayoutVariable)
- 사용자 코드가 식에 쓰는 변수다. 솔버가 풀고 나서 update_variables 를 부르면 set_value 로 결과가 들어온다.
- 원본과 다른 것:
  - 원본은 객체 하나를 여러 곳(노드, 식, 솔버)이 같이 가리킨다. 러스트 판은 Rc 를 품은 값싼 Clone 핸들이라
    복제한 핸들끼리 같은 이름과 값을 본다. 같음과 해시는 원본의 객체 동일성처럼 핸들이 가리키는 변수로 가린다.
  - 원본에 없는 get_id 로 변수마다 붙는 고유 번호를 읽는다.
  - 이름과 값은 RefCell, Cell 에 두어 &self 로 바꾼다.

## LayoutVariableEvent (type)

값이 바뀐 변수를 받는 함수. (원본 setValueChangedEvent 의 함수)

```rust
pub type LayoutVariableEvent = Rc<dyn Fn(&LayoutVariable)>
```

## LayoutVariable (struct) (Clone, PartialEq, Eq, Hash, fmt::Debug)

레이아웃 솔버 미지수. (복제해도 같은 변수를 가리킨다)

예제: 솔버가 푼 값을 읽기, 복제한 핸들도 같은 값을 본다

```rust
use vanilla::{LayoutExpression, LayoutSolver, LayoutVariable};

let solver = LayoutSolver::new();
let left = LayoutVariable::new("left");
let width = LayoutVariable::new("width");
let right = LayoutVariable::new("right");
solver.add_constraint(&LayoutExpression::from_variable(&left).equal_to(16)).unwrap();
solver.add_constraint(&LayoutExpression::from_variable(&width).equal_to(200)).unwrap();
solver.add_constraint(&LayoutExpression::from_variable(&right).equal_to(LayoutExpression::from_variable(&left).add(&width))).unwrap();
solver.update_variables();
assert!((right.get_value() - 216.0).abs() < 1e-9);

let shared = right.clone();
assert_eq!(shared, right);
assert_eq!(shared.get_id(), right.get_id());
assert_eq!(shared.get_value(), right.get_value());
```

- `pub fn new(name: &str) -> LayoutVariable` (constructor, since 0.4.0): 생성. (값은 0)
  - `name` &str: 이름. (보이기용, 이름이 같아도 따로 만든 변수는 다르다)
  - returns LayoutVariable: 새 고유 번호를 붙인 새 변수 핸들.
- `pub fn get_id(&self) -> u64` (method, since 0.4.0): 고유 번호 반환.
  - returns u64: 고유 번호. (1 부터 만든 차례로 하나씩 늘어난다, 복제한 핸들은 같은 번호)
- `pub fn set_name(&self, name: &str)` (method, since 0.4.0): 이름 설정.
  - `name` &str: 새 이름. (복제한 핸들 모두에 보인다)
- `pub fn get_name(&self) -> String` (method, since 0.4.0): 이름 반환.
  - returns String: 이름의 사본.
- `pub fn set_value(&self, value: f64)` (method, since 0.4.0): 값 설정. (솔버가 부르며 바깥에서 직접 부르는 것은 권하지 않는다)
  - `value` f64: 넣을 값.
- `pub fn set_value_changed_event(&self, value_changed_event: Option<LayoutVariableEvent>)` (method, since 0.4.0): 값이 바뀔 때 부를 함수 설정. (원본 setValueChangedEvent, 솔버가 푼 값이 달라질 때만 부른다, None 이면 부르지 않는다)
  - 변수를 가진 쪽이 바뀐 것을 알아챈다. (원본 UINode 는 자리와 크기 변수에 걸어 하위 트리 그리기 경계를 다시 셈하게 한다. 러스트 판 UINode 는 노드 트랜스폼에 풀어 넣으므로 트랜스폼 세터가 알린다)
  - `value_changed_event` Option<LayoutVariableEvent>: 바뀐 변수를 받는 함수. None 이면 지운다.

  예제: 값이 바뀔 때 알기

  ```rust
  use std::cell::Cell;
  use std::rc::Rc;
  use vanilla::LayoutVariable;
  
  let width = LayoutVariable::new("width");
  let changed_count = Rc::new(Cell::new(0));
  let counter = changed_count.clone();
  width.set_value_changed_event(Some(Rc::new(move |variable: &LayoutVariable| {
  	assert_eq!(variable.get_value(), 120.0);
  	counter.set(counter.get() + 1);
  })));
  width.set_value(120.0);
  width.set_value(120.0);
  assert_eq!(changed_count.get(), 1);
  assert!(width.get_value_changed_event().is_some());
  ```

- `pub fn get_value_changed_event(&self) -> Option<LayoutVariableEvent>` (method, since 0.4.0): 값이 바뀔 때 부를 함수 반환. (원본 getValueChangedEvent)
  - returns Option<LayoutVariableEvent>: 건 함수. 없으면 None.
- `pub fn get_value(&self) -> f64` (method, since 0.4.0): 현재 값 반환. (update_variables 뒤에 맞다)
  - returns f64: 지금 값. (처음은 0)
