// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Collections.Generic; namespace Microsoft.MixedReality.Toolkit.UI { /// /// Base class for defining state model logic to use in conjunction with the State class /// Allows for retrieving current state mode and comparing states /// public abstract class BaseStateModel { protected State currentState; protected List stateList; protected State[] allStates; /// /// Import the list of states into this state model /// /// list of state objects to import public void ImportStates(List states) { stateList = states; for (int i = 0; i < stateList.Count; i++) { State state = allStates[stateList[i].Index]; state.ActiveIndex = i; } } /// /// Set the value of the state with given index to on (1) /// /// index of state to access public virtual void SetStateOn(int index) { if (allStates.Length > index && index > 0) { State state = allStates[index]; state.Value = 1; SetStateListValue(state.ActiveIndex, 1); } } /// /// Set the value of the state with given index to off (0) /// /// index of state to access public virtual void SetStateOff(int index) { if (allStates.Length > index && index > 0) { State state = allStates[index]; state.Value = 0; SetStateListValue(state.ActiveIndex, 0); } } /// /// Set value of state with given index to the provided value /// /// index of state to access /// value to set for state public virtual void SetStateValue(int index, int value) { if (allStates.Length > index && index > 0) { State state = allStates[index]; state.Value = value; SetStateListValue(state.ActiveIndex, value); } } protected virtual void SetStateListValue(int index, int value) { if (index < stateList.Count && index > -1) { State state = stateList[index]; state.Value = value; } } /// /// Get the value of the state with the given index /// /// index of state to access /// value of the state public int GetStateValue(int index) { if (allStates.Length > index && index > 0) { State state = allStates[index]; return state.Value; } return 0; } /// /// Get the State object with the given index /// /// index of state to access /// State Object at given index public State GetState(int index) { if (allStates.Length > index && index >= 0) { State state = allStates[index]; return state; } return new State(); } public BaseStateModel() { } public BaseStateModel(State defaultState) { currentState = defaultState; } /// /// Set the current state to the provided State object /// /// State object to set public virtual void SetCurrentState(State state) { currentState = state; } /// /// Return the current State object /// /// Return the current State object public virtual State CurrentState() { return currentState; } /// /// Compare all state values, set appropriate current State and return that current State /// /// Current State after comparing State values public abstract State CompareStates(); /// /// Get list of available States for this State Model /// /// Array of available State objects public abstract State[] GetStates(); protected int GetBit() { int bit = 0; int bitCount = 0; for (int i = 0; i < stateList.Count; i++) { if (i == 0) { bit += 1; } else { bit += bit; } if (stateList[i].Value > 0) { bitCount += bit; } } return bitCount; } } }