ui/uitogglebutton
토글 버튼. (원본 ui/uitogglebutton.js 의 UIToggleButton)
- 클릭할 때마다 켜짐 / 꺼짐이 바뀐다. 켜져 있는 동안은 눌림 틴트를 끝까지 건 채로 둔다.
- 원본은 UIButton 을 상속하고 자기 클릭 처리를 super.set_clicked_event 에 건다. 러스트 판은 UIButton 을 품고, 품은 버튼이 클릭을 알려 주면(뗌 처리의 반환값) 켜짐을 뒤집는다. 그래서 순서가 조금 다르다: 원본은 뗌 → [뒤집기, 토글 이벤트, 클릭됨 이벤트] → 클릭 이벤트, 러스트 판은 뗌 → 클릭 이벤트 → [뒤집기, 토글 이벤트, 클릭됨 이벤트].
- 토글 / 클릭됨 이벤트는 토글 버튼과 문맥을 받는다. 그 밖의 버튼 이벤트와 설정은 get_uibutton_mut 로 품은 버튼에 한다. 받은 이벤트는
Rc<RefCell>에 담는다. get_toggled_event 는 그 Rc 를 돌려주고, 부르는 쪽은 borrow_mut 로 빌려 부른다. get_clicked_event 는 원본(UIButton 에서 물려받은 getClickedEvent)처럼 토글 버튼 안쪽의 클릭 처리 함수를 돌려준다. 부르면 켜짐을 뒤집고 토글 이벤트, 클릭됨 이벤트를 부른다. (set_clicked_event 로 건 함수가 아니다) - set_is_on 의 틴트는 다음 tick 에 걸린다. (UIButton.set_interactable 과 같은 까닭)
Toggle button. (UIToggleButton of the original ui/uitogglebutton.js)
- Each click switches on / off. While on, the pressed tint stays fully applied.
- The original inherits UIButton and hooks its own click handling onto super.set_clicked_event. The Rust version embeds UIButton, and flips the on state when the embedded button reports a click (the return value of release handling). So the order differs slightly: the original is release → [flip, toggle event, clicked event] → click event, and the Rust version is release → click event → [flip, toggle event, clicked event].
- Toggle / clicked events take the toggle button and the context. Other button events and settings are made on the embedded button with get_uibutton_mut. Received events are stored in
Rc<RefCell>. get_toggled_event returns that Rc, and the caller borrows it with borrow_mut and calls it. get_clicked_event returns the toggle button's inner click handling function, like the original (getClickedEvent inherited from UIButton). Calling it flips the on state and calls the toggle event and the clicked event. (it is not the function set with set_clicked_event) - The tint for set_is_on is applied on the next tick. (for the same reason as UIButton.set_interactable)
UIToggleButtonEventtype
토글 버튼 이벤트. (토글 버튼과 문맥을 받는다)
Toggle button event. (takes the toggle button and the context)
pub type UIToggleButtonEvent = Box<dyn FnMut(&mut UIToggleButton, &mut ComponentContext)>UIToggleButtonstruct traits Default, Component
토글 버튼.
Toggle button.
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, Paint, TouchKind, UIToggleButton, Vector2};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let toggle_id = node_tree.create_child(root_id, "SoundToggle");
node_tree[toggle_id].set_content_size(Vector2::create(60.0, 60.0));
node_tree[toggle_id].add_component(Paint::create_with_color("#ffffff"));
let last_is_on = Rc::new(Cell::new(false));
let last_is_on_for_event = last_is_on.clone();
let toggle_button = node_tree.add_component(toggle_id, UIToggleButton::new()).expect("toggle");
toggle_button.set_toggled_event(Box::new(move |toggle_button: &mut UIToggleButton, _context: &mut ComponentContext| {
last_is_on_for_event.set(toggle_button.get_is_on());
}));
node_tree.dispatch_touch(toggle_id, &engine, TouchKind::Press, Vector2::zero());
node_tree.dispatch_touch(toggle_id, &engine, TouchKind::Release, Vector2::zero());
assert!(last_is_on.get());
node_tree.dispatch_touch(toggle_id, &engine, TouchKind::Press, Vector2::zero());
node_tree.dispatch_touch(toggle_id, &engine, TouchKind::Release, Vector2::zero());
assert!(!last_is_on.get());생성.
- 트리에 붙이려면 node_tree.add_component(node_id, UIToggleButton::new()) 를 쓴다.
Constructor.
- To attach to a tree, use node_tree.add_component(node_id, UIToggleButton::new()).
반환ReturnsUIToggleButton 꺼진 새 토글 버튼. (켜짐 틴트 검정 알파 0.3)New toggle button that is off. (on tint black alpha 0.3)
짝Counterpart: vanilla.js UIToggleButton.constructor
품은 버튼 반환.
Returns the embedded button.
반환Returns&UIButton 품은 UIButton.The embedded UIButton.
품은 버튼 반환. (변경용)
Returns the embedded button. (mutable)
반환Returns&mut UIButton 품은 UIButton. (토글, 클릭됨 밖의 이벤트와 틴트, 사용 가능 설정은 여기에 한다)The embedded UIButton. (events other than toggle and clicked, the tint, and the enabled setting go here)
컨텐트 노드 핸들.
Content node handle.
반환ReturnsOption<NodeId> 호스트 노드 밑에 만든 컨텐트 노드. 트리에 붙기 전이면 None.Content node created under the host node. None before being attached to a tree.
짝Counterpart: vanilla.js UIView.getContent
클릭 이벤트 설정. (원본처럼 품은 버튼이 아니라 토글 버튼이 따로 든다)
Sets the click event. (held separately by the toggle button, not the embedded button, as in the original)
- clicked_eventUIToggleButtonEvent
- 클릭으로 켜짐을 뒤집고 토글 이벤트를 부른 다음 부를 함수. (토글 버튼과 문맥을 받는다)Function called after a click flips the on state and calls the toggle event. (takes the toggle button and the context)
짝Counterpart: vanilla.js UIToggleButton.setClickedEvent
클릭 이벤트 반환. (원본처럼 안쪽의 클릭 처리 함수, 부르면 뒤집고 토글 이벤트와 클릭됨 이벤트를 부른다, 부를 때는 borrow_mut 로 빌린다)
Returns the click event. (the inner click handling function as in the original, calling it flips and calls the toggle and clicked events, borrow it with borrow_mut to call it)
반환ReturnsOption<Rc<RefCell<UIToggleButtonEvent>>> 안쪽의 클릭 처리 함수. (늘 Some, set_clicked_event 로 건 함수가 아니다)Inner click handling function. (always Some, not the function set with set_clicked_event)
짝Counterpart: vanilla.js UIButton.getClickedEvent
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, TouchKind, UIButton, UIToggleButton, Vector2};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let toggle_id = node_tree.create_child(root_id, "Toggle");
node_tree.add_component(toggle_id, UIToggleButton::new());
let relay_id = node_tree.create_child(root_id, "Relay");
node_tree[relay_id].set_content_size(Vector2::create(60.0, 60.0));
let relay_button = node_tree.add_component(relay_id, UIButton::new()).expect("relay");
relay_button.set_click_event(Box::new(move |_button: &mut UIButton, context: &mut ComponentContext| {
context.with_component_mut::<UIToggleButton, ()>(toggle_id, |toggle_button, toggle_context| {
if let Some(clicked_event) = toggle_button.get_clicked_event() {
(*clicked_event.borrow_mut())(toggle_button, toggle_context);
}
});
}));
node_tree.dispatch_touch(relay_id, &engine, TouchKind::Press, Vector2::zero());
node_tree.dispatch_touch(relay_id, &engine, TouchKind::Release, Vector2::zero());
assert!(node_tree[toggle_id].get_component::<UIToggleButton>().expect("toggle").get_is_on());켜짐 설정. (이벤트는 부르지 않는다. 틴트는 다음 tick 에 걸린다)
Sets the on state. (does not fire events. the tint is applied on the next tick)
- is_onbool
- 켜려면 true. (지금과 같으면 그대로)true to turn it on. (unchanged if the same as now)
짝Counterpart: vanilla.js UIToggleButton.setIsOn
use vanilla::{Color, Engine, EngineConfiguration, NodeTree, Paint, UIToggleButton, Vector2};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let toggle_id = node_tree.create_child(root_id, "Toggle");
node_tree[toggle_id].set_local_position(Vector2::create(100.0, 100.0));
node_tree[toggle_id].set_content_size(Vector2::create(60.0, 60.0));
node_tree[toggle_id].add_component(Paint::create_with_color(Color::white()));
let toggle_button = node_tree.add_component(toggle_id, UIToggleButton::new()).expect("toggle");
toggle_button.set_is_on(true);
assert!(toggle_button.get_is_on());
assert_eq!(node_tree[toggle_id].get_component::<Paint>().expect("paint").get_color(), Color::white());
node_tree.tick(&engine, 0.016);
let paint_color = node_tree[toggle_id].get_component::<Paint>().expect("paint").get_color();
assert!((paint_color.red - 0.7).abs() < 0.001);켜짐 여부.
Whether it is on.
반환Returnsbool 켜져 있으면 true. (기본 false)true if on. (default false)
짝Counterpart: vanilla.js UIToggleButton.getIsOn
켜짐 틴트 색 설정. (품은 버튼의 눌림 틴트 색도 같이 바꾼다)
Sets the on tint color. (also changes the embedded button's pressed tint color)
- colorimpl Into<Color>
- 켜져 있는 동안 끝까지 걸어 둘 틴트 색. (알파가 세기)Tint color kept fully applied while on. (alpha is the strength)
짝Counterpart: vanilla.js UIToggleButton.setOnTintColor
켜짐 틴트 색 반환.
Returns the on tint color.
반환ReturnsColor 켜짐 틴트 색.On tint color.
짝Counterpart: vanilla.js UIToggleButton.getOnTintColor
토글 이벤트 설정.
Sets the toggle event.
- toggled_eventUIToggleButtonEvent
- 클릭으로 켜짐이 바뀐 뒤 부를 함수. (토글 버튼과 문맥을 받는다, set_is_on 으로 바꿀 때는 불리지 않는다)Function called after a click changes the on state. (takes the toggle button and the context, not called when changed with set_is_on)
짝Counterpart: vanilla.js UIToggleButton.setToggledEvent
토글 이벤트 반환. (부를 때는 borrow_mut 로 빌린다)
Returns the toggle event. (borrow it with borrow_mut to call it)
반환ReturnsOption<Rc<RefCell<UIToggleButtonEvent>>> 건 토글 이벤트. 걸지 않았으면 None.The set toggle event. None if not set.
짝Counterpart: vanilla.js UIToggleButton.getToggledEvent