# ui/uihitarea (vanilla-engine 0.4.0)

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

Hit area widening. (UIHitArea of vanilla.js ui/uihitarea.js)
- Leaves the visible button as is and places a transparent hit area as the sibling right behind the button, widening its width and height to at least the minimum size. (easier to press with a finger)
- The node drawn later receives the press, so the button receives presses over the button and the hit area receives only the widened margin around it.
- Pressing the hit area fires the button's click notifications (Clicked, Click) as is. If the button is turned off or not pressable, the hit area follows.
- It follows the button's position, size, and pivot every frame, so it stays aligned even when the screen moves the button.
- When a clipping node is set, the widened margin is clipped so it does not go outside that node's rectangle. (the button's own area stays as is, so areas outside the window or scroll area are not pressed)
- Usage: UIHitArea::attach(node_tree, button_id, minimum_size) (call after the button is attached to its parent)

## UIHitArea (struct) (Default, Component)

Hit area widening. (a transparent hit area attached to the sibling node right behind the button that follows the button)

Example: Enlarge the press area of a small close button

```rust
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, Paint, Pivot, TouchKind, UIButton, UIHitArea, Vector2};

let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let button_id = node_tree.create_child(root_id, "CloseButton");
node_tree[button_id].set_pivot(Pivot::TOP_LEFT);
node_tree[button_id].set_local_position(Vector2::create(300.0, 20.0));
node_tree[button_id].set_content_size(Vector2::create(24.0, 24.0));
node_tree[button_id].add_component(Paint::create_with_color("#d94d4d"));
let click_count = Rc::new(Cell::new(0));
let click_count_for_event = click_count.clone();
let button = node_tree.add_component(button_id, UIButton::new()).expect("button");
button.set_click_event(Box::new(move |_button: &mut UIButton, _context: &mut ComponentContext| {
	click_count_for_event.set(click_count_for_event.get() + 1);
}));

let hit_id = UIHitArea::attach(&mut node_tree, button_id, 48.0).expect("hit area");
node_tree.tick(&engine, 0.016);
assert_eq!(node_tree[hit_id].get_local_position(), Vector2::create(288.0, 8.0));
assert_eq!(node_tree[hit_id].get_content_size(), Vector2::create(48.0, 48.0));
let hit_area = node_tree[hit_id].get_component::<UIHitArea>().expect("hit area");
assert!(hit_area.get_target_button(&node_tree).is_some());

let beside_button = Vector2::create(295.0, 15.0);
node_tree.dispatch_touch(hit_id, &engine, TouchKind::Press, beside_button);
node_tree.dispatch_touch(hit_id, &engine, TouchKind::Release, beside_button);
assert_eq!(click_count.get(), 1);
```

- `pub fn new() -> UIHitArea` (constructor, since 0.4.0): Constructor.
  - returns UIHitArea: Hit area with no target and a minimum width and height of 0. (use attach to put it on a button)
- `pub fn attach(node_tree: &mut NodeTree, button_id: NodeId, minimum_size: f64) -> Option<NodeId>` (static, since 0.4.0): Attaches a widened hit area to a button. (inserts it as the sibling right behind the button and returns the hit area node, None if there is no parent or no UIButton)
  - The hit area node is inserted right before the button in the parent's child list, so it is drawn before the button. The pivot is the top left.
  - A UIButton is also attached to the hit area node, and its click calls the button's fire_click_events.
  - `node_tree` &mut NodeTree: Node tree that holds the button.
  - `button_id` NodeId: Button node to widen. (must have a UIButton and be attached to a parent)
  - `minimum_size` f64: Minimum width and height of the hit area. (same units as the button's content_size)
  - returns Option<NodeId>: The created hit area node. (named the button's name followed by "HitArea") None if the button has no parent or no UIButton, and no node is created.

  Example: Not attached if it is not a button, inserted right before it if it is

  ```rust
  use vanilla::{NodeTree, UIButton, UIHitArea};
  
  let mut node_tree = NodeTree::new();
  let root_id = node_tree.get_root_id();
  let plain_id = node_tree.create_child(root_id, "Plain");
  assert!(UIHitArea::attach(&mut node_tree, plain_id, 48.0).is_none());
  
  let button_id = node_tree.create_child(root_id, "Ok");
  node_tree.add_component(button_id, UIButton::new());
  let hit_id = UIHitArea::attach(&mut node_tree, button_id, 48.0).expect("hit area");
  assert_eq!(node_tree.get_child_ids(root_id), &[plain_id, hit_id, button_id]);
  assert_eq!(node_tree[hit_id].get_name(), "OkHitArea");
  ```

- `pub fn set_target(&mut self, target_id: NodeId)` (method, since 0.4.0): Sets the node of the button to widen.
  - `target_id` NodeId: Button node to widen. (a node with a UIButton, tick follows its position and size)
- `pub fn get_target(&self) -> Option<NodeId>` (method, since 0.4.0): Returns the node of the button to widen.
  - returns Option<NodeId>: Button node to widen. None if not set.
- `pub fn get_target_button<'a>(&self, node_tree: &'a NodeTree) -> Option<&'a UIButton>` (method, since 0.4.0): Button to widen. (original getTargetButton, the original holds the button component while the Rust version takes it out of the target node, so it takes the tree)
  - `node_tree` &'a NodeTree: Node tree to find the target node in.
  - returns Option<&'a UIButton>: UIButton of the target node. None if no target is set, the target node does not exist, or it has no UIButton.
- `pub fn set_minimum_size(&mut self, minimum_size: f64)` (method, since 0.4.0): Sets the minimum width and height of the press area.
  - `minimum_size` f64: Minimum width and height. (same units as the button's content_size, if the button is larger on a side, that side uses the button size)
- `pub fn get_minimum_size(&self) -> f64` (method, since 0.4.0): Returns the minimum width and height of the press area.
  - returns f64: Minimum width and height. (default 0)
- `pub fn set_clip_node(&mut self, clip_id: Option<NodeId>)` (method, since 0.4.0): Sets the node that clips the widened margin. (None for no clipping)
  - `clip_id` Option<NodeId>: Node whose content_size rectangle the widened margin must not go outside. (a window or scroll area, None for no clipping)
- `pub fn get_clip_node(&self) -> Option<NodeId>` (method, since 0.4.0): Returns the node that clips the widened portion.
  - returns Option<NodeId>: Clipping node. None if not set.
