156
技術社區[雲棲]
HierarchicalStateMachine(HandlerStateMachine)分析
HierarchicalStateMachine源代碼參照:
https://blog.chinaunix.net/space.php?uid=17151843&do=blog&id=142162
3. HierarchicalStateMachine是一個機器,這個機器是含有狀態的機器,
機器的每個狀態都可以有父狀態,如果當前狀態需要父狀態作處理, 那麼讓該狀態的processMessage返回false
4. 當前狀態和它的父狀態們放在mStateStack數組中, active是用來記錄StateInfo包含的state是否是active,在後麵計算當前狀態和目標狀態的共同父狀態 過程中使用。
所有狀態和狀態的StateInfo的鍵值對存放在HashMap mStateInfo中。
/** The map of all of the states in the state machine */
private HashMap<HierarchicalState, StateInfo> mStateInfo =
new HashMap<HierarchicalState, StateInfo>();
/** Stack used to manage the current hierarchy of states */
private StateInfo mStateStack[
] ;
5. 轉換狀態:performTransitions()
a. 尋找當前狀態和目標狀態的共同父狀態,從目標狀態開始循環查詢父狀態的active是否是true,如果是active,說明是當前狀態和目標狀態的父狀態,並依次放入數組mTempStateStack中。
b. 從當前狀態到共同父狀態(不包括本身)依次調用exit,並將active設置為false,將mStateStackTopIndex設置成mStateStack數組的個數。
c. 反序將mTempStateStack中的元素拷貝到mStateStack中,並重新設置mStateStackTopIndex,並返回第一個需要調用enter的state的
index startingIndex
d. 從共同父狀態到目標狀態依次調用enter
e. 移動ArrayLiist mDeferredMessages中的deffered message, 從尾部開始將message加入到MessageQueue中的第一個,這樣,最先加入mDeferredMessages中的Message第一個得到執行。並且deffered message比普通Message先執行。
最後更新:2017-04-02 06:52:01