Skip to main content

ItemScroller

A class in Root.Township, the global namespace.

Client UI: a row of ScrollElements that slides along to show a window of a longer list, reusing a few element copies. Scrolling left moves to higher indices.

Constructors​

MemberWhat it is
ItemScroller()Unity makes components itself; use AddComponent or spawn a prefab instead of new.

Fields​

20 private and internal fields
MemberWhat it is
private BoolEvent canScrollLeftChangedEvent raised with CanScrollLeft whenever it may have changed.
private BoolEvent canScrollRightChangedEvent raised with CanScrollRight whenever it may have changed.
private int countHow many elements are visible at once.
private int finalTargetIndexThe index the scroller is heading to, possibly several steps away.
private int indexFirst visible index at the start of the current step.
private List<ScrollElement> instancesThe element instances, reused as a ring.
private float intervalSpacing between elements along the row.
private bool isScrollingLeftBacking field for IsScrollingLeft (scroll held down).
private bool isScrollingRightBacking field for IsScrollingRight (scroll held down).
private bool isShowingByDefaultWhether the elements show on Awake without waiting for ResetAndRefresh.
private int maxIndexNumber of items, or -1 for no limit.
private ScrollElement prefabThe ScrollElement prefab instantiated for each slot.
private Timeline.IEntry reshowTimer that shows the elements again 0.1 seconds after ResetAndRefresh.
private float rotationIntervalAngle in degrees between neighbouring elements, around the up axis.
private float rotationOffsetForward offset applied after the rotation, so elements can sit on an arc.
private AltaEvent scrollExecutedEvent raised when ScrollDirection accepts a step.
private float scrollProgressProgress through the current step, 0 to 1.
private float stepIntervalSeconds per scroll step.
private int stepSizeHow many items one ScrollDirection call moves.
private int targetIndexFirst visible index at the end of the current step.

Properties​

MemberWhat it is
bool CanScrollLeft { get; }Whether there are more items past the end of the visible window. Always true until a max index is set.
bool CanScrollRight { get; }Whether the window can move back towards index 0. Always true until a max index is set.
int Count { get; }How many elements are visible at once.
bool IsScrollingLeft { get; set; }Set while a left scroll is held, so it keeps stepping.
bool IsScrollingRight { get; set; }Set while a right scroll is held, so it keeps stepping.

Methods​

MemberWhat it is
void ResetAndRefresh(int maxIndex)Hides everything, goes back to index 0 with a new item count and reshows the elements after 0.1 seconds.
void ScrollDirection(int direction)Moves the target one step in a direction if it stays in range, and fires the scroll event.
void ScrollTo(int target)Sets the index to scroll to. The row then steps there over time.
void Show()Shows the first Count elements.
8 private and internal methods
MemberWhat it is
private void Awake()Creates count + 2 element instances, positions them, shows them if isShowingByDefault, and fires the can-scroll events.
private void OnEnable()Starts the next scroll step when the component is enabled with a scroll pending.
private void OnValidate()In play mode, adds or destroys instances to match a changed count.
private void PositionInstances()Places the visible instances along the row, with spacing, rotation and offset, based on scroll progress.
private int SafeMod(int value, int mod)Modulo that never returns a negative number, used to map item indices onto the instance ring.
private void StartScrollLeft()Begins one step towards higher indices: hides the element leaving and shows the one coming in.
private void StartScrollRight()Begins one step towards lower indices: hides the element leaving and shows the one coming in.
private void Update()Advances the step; at its end, starts the next step or disables the component once the target is reached.

Used in​

Members of other types that take, return or hold a ItemScroller: