# ui/uitogglebutton (vanilla-engine 0.4.0)

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

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)

## UIToggleButtonEvent (type)

Toggle button event. (takes the toggle button and the context)

```rust
pub type UIToggleButtonEvent = Box<dyn FnMut(&mut UIToggleButton, &mut ComponentContext)>
```

## UIToggleButton (struct) (Default, Component)

Toggle button.

Example: A button that turns on and off with each press

```rust
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());
```

- `pub fn new() -> UIToggleButton` (constructor, since 0.3.2): Constructor.
  - To attach to a tree, use node_tree.add_component(node_id, UIToggleButton::new()).
  - returns UIToggleButton: New toggle button that is off. (on tint black alpha 0.3)
- `pub fn get_uibutton(&self) -> &UIButton` (method, since 0.3.2): Returns the embedded button.
  - returns &UIButton: The embedded UIButton.
- `pub fn get_uibutton_mut(&mut self) -> &mut UIButton` (method, since 0.3.2): Returns the embedded button. (mutable)
  - returns &mut UIButton: The embedded UIButton. (events other than toggle and clicked, the tint, and the enabled setting go here)
- `pub fn get_content(&self) -> Option<NodeId>` (method, since 0.3.2): Content node handle.
  - returns Option<NodeId>: Content node created under the host node. None before being attached to a tree.
- `pub fn set_clicked_event(&mut self, clicked_event: UIToggleButtonEvent)` (method, since 0.3.2): Sets the click event. (held separately by the toggle button, not the embedded button, as in the original)
  - `clicked_event` UIToggleButtonEvent: Function called after a click flips the on state and calls the toggle event. (takes the toggle button and the context)
- `pub fn get_clicked_event(&self) -> Option<Rc<RefCell<UIToggleButtonEvent>>>` (method, since 0.4.0): 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)
  - returns Option<Rc<RefCell<UIToggleButtonEvent>>>: Inner click handling function. (always Some, not the function set with set_clicked_event)

  Example: Toggle through another button's click

  ```rust
  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());
  ```

- `pub fn set_is_on(&mut self, is_on: bool)` (method, since 0.3.2): Sets the on state. (does not fire events. the tint is applied on the next tick)
  - `is_on` bool: true to turn it on. (unchanged if the same as now)

  Example: Turn it on from a saved value

  ```rust
  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);
  ```

- `pub fn get_is_on(&self) -> bool` (method, since 0.3.2): Whether it is on.
  - returns bool: true if on. (default false)
- `pub fn set_on_tint_color(&mut self, color: impl Into<Color>)` (method, since 0.3.2): Sets the on tint color. (also changes the embedded button's pressed tint color)
  - `color` impl Into<Color>: Tint color kept fully applied while on. (alpha is the strength)
- `pub fn get_on_tint_color(&self) -> Color` (method, since 0.3.2): Returns the on tint color.
  - returns Color: On tint color.
- `pub fn set_toggled_event(&mut self, toggled_event: UIToggleButtonEvent)` (method, since 0.3.2): Sets the toggle event.
  - `toggled_event` UIToggleButtonEvent: Function called after a click changes the on state. (takes the toggle button and the context, not called when changed with set_is_on)
- `pub fn get_toggled_event(&self) -> Option<Rc<RefCell<UIToggleButtonEvent>>>` (method, since 0.4.0): Returns the toggle event. (borrow it with borrow_mut to call it)
  - returns Option<Rc<RefCell<UIToggleButtonEvent>>>: The set toggle event. None if not set.
