UiStateActionManager.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "UiStateActionManager.h"
  9. #include <LyShine/Bus/UiVisualBus.h>
  10. ////////////////////////////////////////////////////////////////////////////////////////////////////
  11. UiStateActionManager::UiStateActionManager()
  12. {
  13. }
  14. ////////////////////////////////////////////////////////////////////////////////////////////////////
  15. UiStateActionManager::~UiStateActionManager()
  16. {
  17. ClearStates();
  18. }
  19. ////////////////////////////////////////////////////////////////////////////////////////////////////
  20. void UiStateActionManager::SetStateColor(int state, AZ::EntityId target, const AZ::Color& color)
  21. {
  22. UiInteractableStateColor* stateColor = GetStateAction<UiInteractableStateColor>(state, target);
  23. if (stateColor)
  24. {
  25. stateColor->SetColor(color);
  26. }
  27. else
  28. {
  29. StateActions* stateActions = GetStateActions(state);
  30. if (stateActions)
  31. {
  32. stateActions->push_back(new UiInteractableStateColor(target, color));
  33. }
  34. }
  35. }
  36. ////////////////////////////////////////////////////////////////////////////////////////////////////
  37. AZ::Color UiStateActionManager::GetStateColor(int state, AZ::EntityId target)
  38. {
  39. AZ::Color color(0.0f, 0.0f, 0.0f, 1.0f);
  40. UiInteractableStateColor* stateColor = GetStateAction<UiInteractableStateColor>(state, target);
  41. if (stateColor)
  42. {
  43. color = stateColor->GetColor();
  44. }
  45. else
  46. {
  47. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING,
  48. "GetStateColor: Couldn't find color action for state/target combination");
  49. }
  50. return color;
  51. }
  52. ////////////////////////////////////////////////////////////////////////////////////////////////////
  53. bool UiStateActionManager::HasStateColor(int state, AZ::EntityId target)
  54. {
  55. UiInteractableStateColor* stateColor = GetStateAction<UiInteractableStateColor>(state, target);
  56. return stateColor ? true : false;
  57. }
  58. ////////////////////////////////////////////////////////////////////////////////////////////////////
  59. void UiStateActionManager::SetStateAlpha(int state, AZ::EntityId target, float alpha)
  60. {
  61. UiInteractableStateAlpha* stateAlpha = GetStateAction<UiInteractableStateAlpha>(state, target);
  62. if (stateAlpha)
  63. {
  64. stateAlpha->SetAlpha(alpha);
  65. }
  66. else
  67. {
  68. StateActions* stateActions = GetStateActions(state);
  69. if (stateActions)
  70. {
  71. stateActions->push_back(new UiInteractableStateAlpha(target, alpha));
  72. }
  73. }
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////////////////////////
  76. float UiStateActionManager::GetStateAlpha(int state, AZ::EntityId target)
  77. {
  78. float alpha = 1.0f;
  79. UiInteractableStateAlpha* stateAlpha = GetStateAction<UiInteractableStateAlpha>(state, target);
  80. if (stateAlpha)
  81. {
  82. alpha = stateAlpha->GetAlpha();
  83. }
  84. else
  85. {
  86. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING,
  87. "GetStateAlpha: Couldn't find alpha action for state/target combination");
  88. }
  89. return alpha;
  90. }
  91. ////////////////////////////////////////////////////////////////////////////////////////////////////
  92. bool UiStateActionManager::HasStateAlpha(int state, AZ::EntityId target)
  93. {
  94. UiInteractableStateAlpha* stateAlpha = GetStateAction<UiInteractableStateAlpha>(state, target);
  95. return stateAlpha ? true : false;
  96. }
  97. ////////////////////////////////////////////////////////////////////////////////////////////////////
  98. void UiStateActionManager::SetStateSprite(int state, AZ::EntityId target, ISprite* sprite)
  99. {
  100. UiInteractableStateSprite* stateSprite = GetStateAction<UiInteractableStateSprite>(state, target);
  101. if (stateSprite)
  102. {
  103. stateSprite->SetSprite(sprite);
  104. }
  105. else
  106. {
  107. StateActions* stateActions = GetStateActions(state);
  108. if (stateActions)
  109. {
  110. stateActions->push_back(new UiInteractableStateSprite(target, sprite));
  111. }
  112. }
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////
  115. ISprite* UiStateActionManager::GetStateSprite(int state, AZ::EntityId target)
  116. {
  117. ISprite* sprite = nullptr;
  118. UiInteractableStateSprite* stateSprite = GetStateAction<UiInteractableStateSprite>(state, target);
  119. if (stateSprite)
  120. {
  121. sprite = stateSprite->GetSprite();
  122. }
  123. else
  124. {
  125. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING,
  126. "GetStateSprite: Couldn't find sprite action for state/target combination");
  127. }
  128. return sprite;
  129. }
  130. ////////////////////////////////////////////////////////////////////////////////////////////////////
  131. void UiStateActionManager::SetStateSpritePathname(int state, AZ::EntityId target, const AZStd::string& spritePath)
  132. {
  133. UiInteractableStateSprite* stateSprite = GetStateAction<UiInteractableStateSprite>(state, target);
  134. if (stateSprite)
  135. {
  136. stateSprite->SetSpritePathname(spritePath);
  137. }
  138. else
  139. {
  140. StateActions* stateActions = GetStateActions(state);
  141. if (stateActions)
  142. {
  143. stateActions->push_back(new UiInteractableStateSprite(target, spritePath));
  144. }
  145. }
  146. }
  147. ////////////////////////////////////////////////////////////////////////////////////////////////////
  148. AZStd::string UiStateActionManager::GetStateSpritePathname(int state, AZ::EntityId target)
  149. {
  150. AZStd::string spritePath;
  151. UiInteractableStateSprite* stateSprite = GetStateAction<UiInteractableStateSprite>(state, target);
  152. if (stateSprite)
  153. {
  154. spritePath = stateSprite->GetSpritePathname();
  155. }
  156. else
  157. {
  158. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING,
  159. "GetStateSpritePathname: Couldn't find sprite action for state/target combination");
  160. }
  161. return spritePath;
  162. }
  163. ////////////////////////////////////////////////////////////////////////////////////////////////////
  164. bool UiStateActionManager::HasStateSprite(int state, AZ::EntityId target)
  165. {
  166. UiInteractableStateSprite* stateSprite = GetStateAction<UiInteractableStateSprite>(state, target);
  167. return stateSprite ? true : false;
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////////////////////////
  170. void UiStateActionManager::SetStateFont(
  171. int state,
  172. AZ::EntityId target,
  173. const AZStd::string& fontPathname,
  174. unsigned int fontEffectIndex)
  175. {
  176. UiInteractableStateFont* stateFont = GetStateAction<UiInteractableStateFont>(state, target);
  177. if (stateFont)
  178. {
  179. stateFont->SetFontPathname(fontPathname);
  180. stateFont->SetFontEffectIndex(fontEffectIndex);
  181. }
  182. else
  183. {
  184. StateActions* stateActions = GetStateActions(state);
  185. if (stateActions)
  186. {
  187. stateActions->push_back(new UiInteractableStateFont(target, fontPathname, fontEffectIndex));
  188. }
  189. }
  190. }
  191. ////////////////////////////////////////////////////////////////////////////////////////////////////
  192. AZStd::string UiStateActionManager::GetStateFontPathname(int state, AZ::EntityId target)
  193. {
  194. AZStd::string fontPath;
  195. UiInteractableStateFont* stateFont = GetStateAction<UiInteractableStateFont>(state, target);
  196. if (stateFont)
  197. {
  198. fontPath = stateFont->GetFontPathname();
  199. }
  200. else
  201. {
  202. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING,
  203. "GetStateFontPathname: Couldn't find font action for state/target combination");
  204. }
  205. return fontPath;
  206. }
  207. ////////////////////////////////////////////////////////////////////////////////////////////////////
  208. unsigned int UiStateActionManager::GetStateFontEffectIndex(int state, AZ::EntityId target)
  209. {
  210. unsigned int fontEffectIndex = 0;
  211. UiInteractableStateFont* stateFont = GetStateAction<UiInteractableStateFont>(state, target);
  212. if (stateFont)
  213. {
  214. fontEffectIndex = stateFont->GetFontEffectIndex();
  215. }
  216. else
  217. {
  218. CryWarning(VALIDATOR_MODULE_SYSTEM, VALIDATOR_WARNING,
  219. "GetStateFontEffectIndex: Couldn't find font action for state/target combination");
  220. }
  221. return fontEffectIndex;
  222. }
  223. ////////////////////////////////////////////////////////////////////////////////////////////////////
  224. bool UiStateActionManager::HasStateFont(int state, AZ::EntityId target)
  225. {
  226. UiInteractableStateFont* stateFont = GetStateAction<UiInteractableStateFont>(state, target);
  227. return stateFont ? true : false;
  228. }
  229. ////////////////////////////////////////////////////////////////////////////////////////////////////
  230. void UiStateActionManager::Init(AZ::EntityId entityId)
  231. {
  232. m_entityId = entityId;
  233. InitStateActions();
  234. }
  235. ////////////////////////////////////////////////////////////////////////////////////////////////////
  236. void UiStateActionManager::Activate()
  237. {
  238. UiInteractableStatesBus::Handler::BusConnect(m_entityId);
  239. }
  240. ////////////////////////////////////////////////////////////////////////////////////////////////////
  241. void UiStateActionManager::Deactivate()
  242. {
  243. UiInteractableStatesBus::Handler::BusDisconnect();
  244. }
  245. ////////////////////////////////////////////////////////////////////////////////////////////////////
  246. void UiStateActionManager::AddState(StateActions* stateActions)
  247. {
  248. m_states.push_back(stateActions);
  249. }
  250. ////////////////////////////////////////////////////////////////////////////////////////////////////
  251. void UiStateActionManager::ResetAllOverrides()
  252. {
  253. AZStd::vector<AZ::EntityId> entityIdList = GetTargetEntitiesInAllStates();
  254. for (auto targetEntityId : entityIdList)
  255. {
  256. UiVisualBus::Event(targetEntityId, &UiVisualBus::Events::ResetOverrides);
  257. }
  258. }
  259. ////////////////////////////////////////////////////////////////////////////////////////////////////
  260. void UiStateActionManager::ApplyStateActions(int state)
  261. {
  262. // Get the actions for the state (if any) and apply them
  263. StateActions* stateActions = m_states[state];
  264. if (stateActions)
  265. {
  266. for (UiInteractableStateAction* stateAction : (*stateActions))
  267. {
  268. stateAction->ApplyState();
  269. }
  270. }
  271. }
  272. ////////////////////////////////////////////////////////////////////////////////////////////////////
  273. void UiStateActionManager::InitInteractableEntityForStateActions(StateActions& stateActions)
  274. {
  275. for (auto stateAction : stateActions)
  276. {
  277. stateAction->SetInteractableEntity(m_entityId);
  278. }
  279. }
  280. ////////////////////////////////////////////////////////////////////////////////////////////////////
  281. void UiStateActionManager::ClearStates()
  282. {
  283. for (StateActions* stateActions : m_states)
  284. {
  285. if (stateActions)
  286. {
  287. for (UiInteractableStateAction* stateAction : * stateActions)
  288. {
  289. delete stateAction;
  290. }
  291. }
  292. }
  293. m_states.clear();
  294. }
  295. ////////////////////////////////////////////////////////////////////////////////////////////////////
  296. // PUBLIC STATIC MEMBER FUNCTIONS
  297. ////////////////////////////////////////////////////////////////////////////////////////////////////
  298. ////////////////////////////////////////////////////////////////////////////////////////////////////
  299. // PROTECTED MEMBER FUNCTIONS
  300. ////////////////////////////////////////////////////////////////////////////////////////////////////
  301. ////////////////////////////////////////////////////////////////////////////////////////////////////
  302. void UiStateActionManager::InitStateActions()
  303. {
  304. for (StateActions* stateActions : m_states)
  305. {
  306. if (stateActions)
  307. {
  308. for (UiInteractableStateAction* stateAction : * stateActions)
  309. {
  310. stateAction->Init(m_entityId);
  311. }
  312. }
  313. }
  314. }
  315. ////////////////////////////////////////////////////////////////////////////////////////////////////
  316. AZStd::vector<AZ::EntityId> UiStateActionManager::GetTargetEntitiesInAllStates()
  317. {
  318. AZStd::vector<AZ::EntityId> result;
  319. for (StateActions* stateActions : m_states)
  320. {
  321. if (stateActions)
  322. {
  323. for (auto stateAction : (*stateActions))
  324. {
  325. AZ::EntityId targetEntity = stateAction->GetTargetEntity();
  326. // if this state action has a target entity and it is not already in the list then add it
  327. if (targetEntity.IsValid() && AZStd::find(result.begin(), result.end(), targetEntity) == result.end())
  328. {
  329. result.push_back(targetEntity);
  330. }
  331. }
  332. }
  333. }
  334. return result;
  335. }
  336. ////////////////////////////////////////////////////////////////////////////////////////////////////
  337. UiStateActionManager::StateActions* UiStateActionManager::GetStateActions(int state)
  338. {
  339. if (state >= 0 && state < m_states.size())
  340. {
  341. return m_states[state];
  342. }
  343. return nullptr;
  344. }