ui/uislider
슬라이더. (원본 ui/uislider.js 의 UISlider)
- 끌거나 눌러서 값을 고른다. 트랙 + 채움 + 손잡이(원)를 직접 그린다. 값은 범위로 자르기만 한다. (원본에도 눈금 맞추기는 없다)
- 손잡이 노드(set_thumb_node)를 정하면 원을 그리지 않고 그 노드를 손잡이 자리로 옮긴다.
- 원본과 다른 것:
- 원본은 draw 안에서 손잡이 노드를 옮긴다. 러스트 판은 그리는 동안 트리를 바꿀 수 없어 tick 에서 옮긴다. (손잡이 노드는 호스트의 자식으로 둔다)
- 입력 좌표는 원본처럼 호스트 왼쪽 위 모서리 기준이되, 노드 행렬의 역으로 바꿔 크기와 회전까지 따른다. (원본은 모서리 위치만 뺀다)
- 값 바뀜 이벤트는 슬라이더와 문맥을 받는다. 씬 코드에서 부르는 fire_value_changed 는 다음 tick 에 이벤트를 부른다. 받은 이벤트는
Rc<RefCell>에 담는다. get_value_changed_event 는 그 Rc 를 돌려주고, 부르는 쪽은 borrow_mut 로 빌려 부른다. - attach_to_tree 에서 호스트 노드를 interactable 로 켠다. node_tree.add_component(node_id, UISlider::new()) 로 붙인다.
Slider. (UISlider of the original ui/uislider.js)
- Picks a value by dragging or pressing. Draws the track + fill + thumb (circle) directly. The value is only clamped to the range. (the original has no tick snapping either)
- When a thumb node (set_thumb_node) is set, it does not draw the circle and moves that node to the thumb position.
- Differences from the original:
- The original moves the thumb node inside draw. The Rust version cannot change the tree while drawing, so it moves it in tick. (the thumb node is kept as a child of the host)
- Input coordinates are relative to the host's top-left corner as in the original, but converted with the inverse of the node matrix so they follow scale and rotation too. (the original only subtracts the corner position)
- The value changed event takes the slider and the context. fire_value_changed called from scene code fires the event on the next tick. Received events are stored in
Rc<RefCell>. get_value_changed_event returns that Rc, and the caller borrows it with borrow_mut and calls it. - attach_to_tree turns the host node interactable. Attach with node_tree.add_component(node_id, UISlider::new()).
UISliderEventtype
슬라이더 이벤트. (슬라이더와 문맥을 받는다)
Slider event. (takes the slider and the context)
pub type UISliderEvent = Box<dyn FnMut(&mut UISlider, &mut ComponentContext)>UISliderstruct traits Default, Component
슬라이더.
Slider.
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, Pivot, TouchKind, UISlider, Vector2};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let slider_id = node_tree.create_child(root_id, "VolumeSlider");
node_tree[slider_id].set_pivot(Pivot::TOP_LEFT);
node_tree[slider_id].set_local_position(Vector2::create(100.0, 50.0));
node_tree[slider_id].set_content_size(Vector2::create(200.0, 20.0));
let volume = Rc::new(Cell::new(0.0));
let volume_for_event = volume.clone();
let slider = node_tree.add_component(slider_id, UISlider::new()).expect("slider");
slider.set_range(0.0, 100.0);
slider.set_track_thickness(6.0);
slider.set_thumb_radius(10.0);
slider.set_value_changed_event(Box::new(move |slider: &mut UISlider, _context: &mut ComponentContext| {
volume_for_event.set(slider.get_value());
}));
node_tree.dispatch_touch(slider_id, &engine, TouchKind::Press, Vector2::create(150.0, 60.0));
assert_eq!(volume.get(), 25.0);
node_tree.dispatch_touch(slider_id, &engine, TouchKind::Moved, Vector2::create(500.0, 60.0));
assert_eq!(volume.get(), 100.0);
node_tree.dispatch_touch(slider_id, &engine, TouchKind::Release, Vector2::create(500.0, 60.0));
assert!(!node_tree[slider_id].get_component::<UISlider>().expect("slider").is_dragging());생성. (값 0, 범위 0 ~ 1, 트랙 회색, 채움 파랑, 손잡이 흰 원 반지름 16)
- 트리에 붙이려면 node_tree.add_component(node_id, UISlider::new()) 를 쓴다.
Constructor. (value 0, range 0 ~ 1, gray track, blue fill, white circle thumb radius 16)
- To attach to a tree, use node_tree.add_component(node_id, UISlider::new()).
반환ReturnsUISlider 가로 방향의 새 슬라이더. (트랙 두께 0, 둥근 모서리 0, 누를 수 있음)New horizontal slider. (track thickness 0, rounded corners 0, pressable)
짝Counterpart: vanilla.js UISlider.constructor
품은 컨트롤 반환.
Returns the embedded control.
반환Returns&UIControl 품은 UIControl.The embedded UIControl.
품은 컨트롤 반환. (변경용)
Returns the embedded control. (mutable)
반환Returns&mut UIControl 품은 UIControl.The embedded UIControl.
컨텐트 노드 핸들.
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
0 ~ 1 비율 반환.
Returns the 0 ~ 1 ratio.
반환Returnsf64 값이 범위에서 차지하는 비율. (0 ~ 1, 최대값이 최소값보다 크지 않으면 0)Ratio of the value within the range. (0 ~ 1, 0 if the maximum is not greater than the minimum)
짝Counterpart: vanilla.js UISlider.getRatio
값 설정. (범위로 자른다. 이벤트는 부르지 않는다)
Sets the value. (clamped to the range. does not fire the event)
- valuef64
- 새 값. (최소값보다 작으면 최소값, 최대값보다 크면 최대값)New value. (the minimum if less than the minimum, the maximum if greater than the maximum)
짝Counterpart: vanilla.js UISlider.setValue
값 반환.
Returns the value.
반환Returnsf64 지금 값. (최소값 ~ 최대값)Current value. (minimum ~ maximum)
짝Counterpart: vanilla.js UISlider.getValue
범위 설정. (지금 값을 새 범위로 다시 자른다)
Sets the range. (reclamps the current value to the new range)
- min_valuef64
- 최소값.Minimum value.
- max_valuef64
- 최대값. (최소값보다 커야 한다, 같거나 작으면 비율은 늘 0)Maximum value. (must be greater than the minimum, the ratio is always 0 if equal or less)
짝Counterpart: vanilla.js UISlider.setRange
use vanilla::UISlider;
let mut slider = UISlider::new();
slider.set_range(10.0, 20.0);
assert_eq!(slider.get_value(), 10.0);
slider.set_value(17.5);
assert_eq!(slider.get_ratio(), 0.75);
slider.set_value(30.0);
assert_eq!(slider.get_value(), 20.0);
slider.set_range(0.0, 5.0);
assert_eq!(slider.get_value(), 5.0);최소값 반환.
Returns the minimum value.
반환Returnsf64 최소값. (기본 0)Minimum value. (default 0)
짝Counterpart: vanilla.js UISlider.getMinValue
최대값 반환.
Returns the maximum value.
반환Returnsf64 최대값. (기본 1)Maximum value. (default 1)
짝Counterpart: vanilla.js UISlider.getMaxValue
값 바뀜 이벤트를 억지로 부르기. (씬 코드에서 set_value 한 뒤. 다음 tick 에 부른다)
Forces the value changed event to fire. (after set_value from scene code. fired on the next tick)
짝Counterpart: vanilla.js UISlider.fireValueChanged
use std::cell::Cell;
use std::rc::Rc;
use vanilla::{ComponentContext, Engine, EngineConfiguration, NodeTree, UISlider};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let slider_id = node_tree.create_child(root_id, "Slider");
let changed_count = Rc::new(Cell::new(0));
let changed_count_for_event = changed_count.clone();
let slider = node_tree.add_component(slider_id, UISlider::new()).expect("slider");
slider.set_value_changed_event(Box::new(move |_slider: &mut UISlider, _context: &mut ComponentContext| {
changed_count_for_event.set(changed_count_for_event.get() + 1);
}));
slider.set_value(0.5);
slider.fire_value_changed();
assert_eq!(changed_count.get(), 0);
node_tree.tick(&engine, 0.016);
assert_eq!(changed_count.get(), 1);트랙 색 설정.
Sets the track color.
- colorimpl Into<Color>
- 트랙 색. (기본 회색 0.7)Track color. (default gray 0.7)
짝Counterpart: vanilla.js UISlider.setTrackColor
트랙 색 반환.
Returns the track color.
반환ReturnsColor 트랙 색.Track color.
짝Counterpart: vanilla.js UISlider.getTrackColor
채움 색 설정.
Sets the fill color.
- colorimpl Into<Color>
- 최소값 쪽 끝부터 지금 값까지 채우는 색. (기본 파랑)Color that fills from the minimum end up to the current value. (default blue)
짝Counterpart: vanilla.js UISlider.setFillColor
채움 색 반환.
Returns the fill color.
반환ReturnsColor 채움 색.Fill color.
짝Counterpart: vanilla.js UISlider.getFillColor
손잡이 색 설정.
Sets the thumb color.
- colorimpl Into<Color>
- 손잡이 원의 색. (기본 흰색, 손잡이 노드를 정하면 원을 그리지 않는다)Color of the thumb circle. (default white, the circle is not drawn when a thumb node is set)
짝Counterpart: vanilla.js UISlider.setThumbColor
손잡이 색 반환.
Returns the thumb color.
반환ReturnsColor 손잡이 색.Thumb color.
짝Counterpart: vanilla.js UISlider.getThumbColor
손잡이 반지름 설정.
Sets the thumb radius.
- thumb_radiusf64
- 손잡이 원의 반지름. (호스트 content_size 와 같은 단위, 기본 16)Radius of the thumb circle. (same units as the host content_size, default 16)
짝Counterpart: vanilla.js UISlider.setThumbRadius
손잡이 반지름 반환.
Returns the thumb radius.
반환Returnsf64 손잡이 원의 반지름.Radius of the thumb circle.
짝Counterpart: vanilla.js UISlider.getThumbRadius
트랙 두께 설정. (0 이면 호스트 영역 전체, 아니면 가운데에 그 두께로)
Sets the track thickness. (0 for the whole host area, otherwise that thickness in the middle)
- track_thicknessf64
- 트랙 두께. (호스트 content_size 와 같은 단위, 기본 0)Track thickness. (same units as the host content_size, default 0)
짝Counterpart: vanilla.js UISlider.setTrackThickness
트랙 두께 반환.
Returns the track thickness.
반환Returnsf64 트랙 두께. (0 이면 호스트 영역 전체)Track thickness. (0 for the whole host area)
짝Counterpart: vanilla.js UISlider.getTrackThickness
손잡이 노드 설정. (호스트의 자식. 정하면 원 대신 이 노드를 손잡이 자리로 옮긴다)
- tick 에서 손잡이 노드의 로컬 위치를 (손잡이 중심 - 노드 크기 절반) 에 둔다. (피봇이 왼쪽 위면 가운데가 손잡이 자리에 온다)
Sets the thumb node. (a child of the host. when set, moves this node to the thumb position instead of the circle)
- In tick, places the thumb node's local position at (thumb center - half the node size). (with a top-left pivot, its center lands on the thumb position)
- thumb_node_idOption<NodeId>
- 손잡이로 쓸 노드. (None 이면 원을 그린다)Node to use as the thumb. (None draws the circle)
짝Counterpart: vanilla.js UISlider.setThumbNode
use vanilla::{Engine, EngineConfiguration, NodeTree, Pivot, UISlider, Vector2};
let engine = Engine::new(EngineConfiguration::new());
let mut node_tree = NodeTree::new();
let root_id = node_tree.get_root_id();
let slider_id = node_tree.create_child(root_id, "Slider");
node_tree[slider_id].set_pivot(Pivot::TOP_LEFT);
node_tree[slider_id].set_content_size(Vector2::create(200.0, 20.0));
node_tree.add_component(slider_id, UISlider::new());
let thumb_id = node_tree.create_child(slider_id, "Thumb");
node_tree[thumb_id].set_pivot(Pivot::TOP_LEFT);
node_tree[thumb_id].set_content_size(Vector2::create(10.0, 10.0));
let slider = node_tree[slider_id].get_component_mut::<UISlider>().expect("slider");
slider.set_thumb_node(Some(thumb_id));
slider.set_value(0.5);
node_tree.tick(&engine, 0.016);
assert_eq!(node_tree[thumb_id].get_local_position(), Vector2::create(95.0, 5.0));손잡이 노드 반환.
Returns the thumb node.
반환ReturnsOption<NodeId> 손잡이 노드. 정하지 않았으면 None.Thumb node. None if not set.
짝Counterpart: vanilla.js UISlider.getThumbNode
둥근 모서리 반지름 설정.
Sets the rounded corner radius.
- corner_radiusf64
- 트랙과 채움 사각형의 둥근 모서리 반지름. (0 이면 각진 사각형, 기본 0)Rounded corner radius of the track and fill rectangles. (0 for square corners, default 0)
짝Counterpart: vanilla.js UISlider.setCornerRadius
둥근 모서리 반지름 반환.
Returns the rounded corner radius.
반환Returnsf64 둥근 모서리 반지름.Rounded corner radius.
짝Counterpart: vanilla.js UISlider.getCornerRadius
방향 설정.
Sets the direction.
- directionProgressDirection
- 방향. (Horizontal 은 왼쪽이 최소값, Vertical 은 아래가 최소값, 기본 Horizontal)Direction. (Horizontal has the minimum on the left, Vertical has the minimum at the bottom, default Horizontal)
짝Counterpart: vanilla.js UISlider.setDirection
방향 반환.
Returns the direction.
반환ReturnsProgressDirection 지금 방향.Current direction.
짝Counterpart: vanilla.js UISlider.getDirection
값 바뀜 이벤트 설정.
Sets the value changed event.
- value_changed_eventUISliderEvent
- 누르거나 끌어 값이 바뀔 때 부를 함수. (슬라이더와 문맥을 받는다, 같은 값이면 불리지 않고 set_value 로 바꿀 때도 불리지 않는다)Function called when the value changes by pressing or dragging. (takes the slider and the context, not called for the same value nor when changed with set_value)
짝Counterpart: vanilla.js UISlider.setValueChangedEvent
값 바뀜 이벤트 반환. (부를 때는 borrow_mut 로 빌린다)
Returns the value changed event. (borrow it with borrow_mut to call it)
반환ReturnsOption<Rc<RefCell<UISliderEvent>>> 건 값 바뀜 이벤트. 걸지 않았으면 None.The set value changed event. None if not set.
짝Counterpart: vanilla.js UISlider.getValueChangedEvent
사용 가능 설정. (끄면 누름을 받지 않는다)
Sets whether it is enabled. (when off, presses are not taken)
- is_interactablebool
- 누름을 받으려면 true. (기본 true, 끄더라도 이미 끄는 중인 손가락은 끝까지 따라간다)true to take presses. (default true, even when turned off, a finger already dragging is followed to the end)
짝Counterpart: vanilla.js UISlider.setInteractable
사용 가능 여부.
Whether it is enabled.
반환Returnsbool 누름을 받으면 true.true if it takes presses.
짝Counterpart: vanilla.js UISlider.getInteractable
끄는 중인지.
Whether it is being dragged.
반환Returnsbool 누름부터 뗌이나 취소까지 true.true from press until release or cancel.
짝Counterpart: vanilla.js UISlider.isDragging