Skip to main content

TouchScreenMenuBase

A class in Root.Township, the global namespace.

Base for an in-world touch screen that the local player's fingertips press: maps child buttons to screen rectangles, presses and releases them, and reports swipes. Client side; call Setup with true for the local player.

Constructors​

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

Fields​

MemberWhat it is
protected Vector2 dimensionsSize of the screen area.
protected readonly Dictionary<KeyCode, ILabeledTouchScreenButton> instantiatedSuggestionKeysExtra labelled keys by key code, also mapped as buttons.
22 private and internal fields
MemberWhat it is
private ITouchScreenButton activeButtonPressWith multi-touch off, the one button now pressed.
private bool alwaysShowButtonsIf true, the buttons stay visible; else they only show while a finger is close.
private Rect baseRectScreen area that covers every mapped button, in local space.
private readonly List<ITouchScreenButton> buttonPressCacheReused list of buttons to press this frame.
private GameObject buttonsParentObject holding the buttons; turned on and off to show them.
private Transform canvasTransformTransform used to measure finger distance; set to the buttons parent.
private bool isEnabledSet each frame when a finger is close, to show the buttons.
private bool isMultiTouchWhether several buttons can be pressed at once.
private bool isSetupLocalSet once Setup has run for the local player with fingertips.
private static Logger loggerNLog logger for this class.
private float minDistanceToRenderSquaredSquared distance a fingertip must be within for touches to count and buttons to show.
private PositionEvent onScreenReleasedEffectsEffects played at a button's position when it is released.
private PositionEvent onScreenTouchEffectsEffects played at the fingertip when the screen is touched.
private bool setupOnStartSerialized option. Not read in this class.
private HashSet<ITouchScreenButton> toReleaseButtons to check for release this frame.
private HashSet<ITouchScreenButton> touchedButtons pressed this frame, so they aren't released.
private float touchHapticHaptic strength sent to the hand on a touch.
private TouchScreenMenuBase.TouchInput[] touchInputsTouch trackers for the right and left fingertips.
private float touchReleaseThresholdDistance from the screen a finger must pull back to end a touch.
private float touchThresholdDistance from the screen within which a finger starts a touch.
private bool triggerEffectsOnButtonTouchOnlyIf true, touch and release effects only play when a button is hit.
private Coroutine updateTouchRunning touch update coroutine, or null.

Properties​

MemberWhat it is
virtual List<ITouchScreenButton> InstantiatedButtons { get; set; }Buttons on the screen. Filled from the buttons parent if empty when mapping.
protected Dictionary<ITouchScreenButton, Rect> ButtonMap { get; }Each button's rectangle in the screen's local space.
protected Transform ButtonsParent { get; }Parent of the buttons; shown only while a finger is close unless always shown.
protected virtual bool IsScreenFree { get; }Whether touches are processed. True by default.
protected virtual Vector2 Offset { get; set; }Offset for subclasses.
protected float TouchReleaseThreshold { get; }Distance from the screen at which a touch ends.
protected float TouchThreshold { get; }Distance from the screen at which a touch starts.

Events​

MemberWhat it is
event Action<Vector2> SwipeExecutedRaised with the swipe vector when a touch that hit no button ends.

Methods​

MemberWhat it is
void MapButtons()Rebuilds the button rectangles and the screen bounds from the button positions and sizes.
void Setup(bool isLocal)For the local player, maps buttons and starts tracking both fingertips; hides the buttons unless always shown.
protected void DisableButtons()Hides the buttons.
protected void EnableButtons()Shows the buttons if a finger is close.
protected virtual void OnDrawGizmos()Draws the screen area and touch depth.
protected virtual void PostTouchCalculation()Hook run after each frame's presses and releases.
protected virtual void PreTouchCalculation()Hook run before each frame's presses; clears the touched and release lists.
protected virtual void SetupForLocal()Hook run at the end of local setup.
protected virtual void UpdateTouchWithButtons(TouchScreenMenuBase.TouchInput touch)Hook run for each finger after its press is processed.
10 private and internal methods
MemberWhat it is
private bool IsButtonValid(KeyValuePair<ITouchScreenButton, Rect> buttonPair, TouchScreenMenuBase.TouchInput touch)True if the button is enabled, active, allowed under the multi-touch rule and contains the touch.
private bool IsFirstPressInBounds(TouchScreenMenuBase.TouchInput touch)True if the touch just started and is inside the screen's button area.
private void MapKey(Matrix4x4 matrix, ITouchScreenButton button, ref Vector2 min, ref Vector2 max)Works out a button's rectangle on the screen and grows the overall bounds to fit it.
private void OnDisable()Stops the touch update coroutine.
private void OnEnable()Restarts the touch update coroutine if the screen was set up for the local player.
private void ProcessButtonPress(TouchScreenMenuBase.TouchInput touch)On a new touch inside the screen, presses every button under the finger and plays the haptic and touch effects.
private void UpdateButtonPresses()Updates each free hand's finger touch, presses buttons, raises swipes and notes if a finger is close.
private void UpdateButtonReleases()Releases pressed buttons no finger is still touching and plays the release effects.
private IEnumerator UpdateTouch() (iterator)Coroutine run at the end of each frame: handles presses and releases and shows or hides the buttons.
private bool ValidateMultiTouch(ITouchScreenButton button)With multi-touch off, only the button already being pressed counts.

Used in​

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