3
0

LyShineExamplesCppExample.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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 "LyShineExamplesCppExample.h"
  9. #include <AzCore/Component/Entity.h>
  10. #include <AzCore/RTTI/BehaviorContext.h>
  11. #include <LyShine/ISprite.h>
  12. #include <LyShine/UiSerializeHelpers.h>
  13. #include <LyShine/Bus/UiElementBus.h>
  14. #include <LyShine/Bus/UiTextBus.h>
  15. #include <LyShine/Bus/UiImageBus.h>
  16. #include <LyShine/Bus/UiButtonBus.h>
  17. #include <LyShine/Bus/UiCheckboxBus.h>
  18. #include <LyShine/Bus/UiSliderBus.h>
  19. #include <LyShine/Bus/UiTextInputBus.h>
  20. #include <LyShine/Bus/UiInteractableStatesBus.h>
  21. #include <LyShine/Bus/UiInitializationBus.h>
  22. #include <LyShine/UiComponentTypes.h>
  23. #include <LyShine/Bus/UiAnimationBus.h>
  24. #include <LyShine/Bus/UiNavigationBus.h>
  25. namespace LyShineExamples
  26. {
  27. ////////////////////////////////////////////////////////////////////////////////////////////////////
  28. LyShineExamplesCppExample::LyShineExamplesCppExample()
  29. : m_health(10)
  30. {
  31. LyShineExamplesCppExampleBus::Handler::BusConnect();
  32. }
  33. ////////////////////////////////////////////////////////////////////////////////////////////////////
  34. LyShineExamplesCppExample::~LyShineExamplesCppExample()
  35. {
  36. LyShineExamplesCppExampleBus::Handler::BusDisconnect();
  37. }
  38. ////////////////////////////////////////////////////////////////////////////////////////////////////
  39. void LyShineExamplesCppExample::CreateCanvas()
  40. {
  41. // Remove the existing example canvas if it exists
  42. DestroyCanvas();
  43. AZ::EntityId canvasEntityId = AZ::Interface<ILyShine>::Get()->CreateCanvas();
  44. if (!canvasEntityId.IsValid())
  45. {
  46. return;
  47. }
  48. m_canvasId = canvasEntityId;
  49. // Create an image to be the canvas background
  50. AZ::EntityId foregroundId = CreateBackground();
  51. // Create the canvas title
  52. CreateText("Title", false, foregroundId, UiTransform2dInterface::Anchors(0.5f, 0.1f, 0.5f, 0.1f), UiTransform2dInterface::Offsets(-200, 50, 200, -50),
  53. "Canvas created through C++", AZ::Color(0.f, 0.f, 0.f, 1.f), IDraw2d::HAlign::Center, IDraw2d::VAlign::Center,
  54. UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  55. // Add the elements examples, creating elements from scratch
  56. CreateElementsExample(foregroundId);
  57. // Add the behavior example, creating some light defining of custom behavior in C++
  58. CreateBehaviorExample(foregroundId);
  59. // Create a button to be able to destroy this canvas and keep navigating the UiFeatures examples
  60. m_destroyButton = CreateButton("DestroyButton", false, foregroundId, UiTransform2dInterface::Anchors(0.15f, 0.9f, 0.15f, 0.9f), UiTransform2dInterface::Offsets(-100, -25, 100, 25),
  61. "Destroy canvas", AZ::Color(0.604f, 0.780f, 0.839f, 1.f), AZ::Color(0.380f, 0.745f, 0.871f, 1.f), AZ::Color(0.055f, 0.675f, 0.886f, 1.f), AZ::Color(1.f, 1.f, 1.f, 1.f),
  62. UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  63. // Connect to the button notification bus so we receive click events from the destroy button
  64. UiButtonNotificationBus::MultiHandler::BusConnect(m_destroyButton);
  65. }
  66. ////////////////////////////////////////////////////////////////////////////////////////////////////
  67. void LyShineExamplesCppExample::DestroyCanvas()
  68. {
  69. if (m_canvasId.IsValid())
  70. {
  71. UiButtonNotificationBus::MultiHandler::BusDisconnect(m_damageButton);
  72. m_damageButton.SetInvalid();
  73. UiButtonNotificationBus::MultiHandler::BusDisconnect(m_healButton);
  74. m_healButton.SetInvalid();
  75. UiButtonNotificationBus::MultiHandler::BusDisconnect(m_destroyButton);
  76. m_destroyButton.SetInvalid();
  77. m_healthBar.SetInvalid();
  78. AZ::Interface<ILyShine>::Get()->ReleaseCanvas(m_canvasId, false);
  79. m_canvasId.SetInvalid();
  80. }
  81. }
  82. ////////////////////////////////////////////////////////////////////////////////////////////////////
  83. void LyShineExamplesCppExample::OnButtonClick()
  84. {
  85. // Get the id of what button just got clicked (it has to be one that we subscribed to)
  86. const AZ::EntityId* pButtonClickedId = UiButtonNotificationBus::GetCurrentBusId();
  87. // If the damage button got clicked
  88. if (*pButtonClickedId == m_damageButton)
  89. {
  90. UpdateHealth(-1);
  91. }
  92. // If the heal button got clicked
  93. else if (*pButtonClickedId == m_healButton)
  94. {
  95. UpdateHealth(1);
  96. }
  97. // If the destroy button got clicked
  98. else // *pButtonClickedId == m_destroyButton
  99. {
  100. DestroyCanvas();
  101. }
  102. }
  103. ////////////////////////////////////////////////////////////////////////////////////////////////////
  104. void LyShineExamplesCppExample::Reflect(AZ::ReflectContext* context)
  105. {
  106. AZ::BehaviorContext* behaviorContext = azrtti_cast<AZ::BehaviorContext*>(context);
  107. if (behaviorContext)
  108. {
  109. behaviorContext->EBus<LyShineExamplesCppExampleBus>("LyShineExamplesCppExampleBus")
  110. ->Event("CreateCanvas", &LyShineExamplesCppExampleBus::Events::CreateCanvas)
  111. ->Event("DestroyCanvas", &LyShineExamplesCppExampleBus::Events::DestroyCanvas);
  112. }
  113. }
  114. ////////////////////////////////////////////////////////////////////////////////////////////////////
  115. // PRIVATE MEMBER FUNCTIONS
  116. ////////////////////////////////////////////////////////////////////////////////////////////////////
  117. ////////////////////////////////////////////////////////////////////////////////////////////////////
  118. AZ::EntityId LyShineExamplesCppExample::CreateBackground()
  119. {
  120. // Get the canvas
  121. UiCanvasInterface* canvas = UiCanvasBus::FindFirstHandler(m_canvasId);
  122. // Create an empty element in the canvas
  123. AZ::Entity* background = canvas->CreateChildElement("Background");
  124. // Add a transform component and an image component to the background
  125. CreateComponent(background, LyShine::UiTransform2dComponentUuid);
  126. CreateComponent(background, LyShine::UiImageComponentUuid);
  127. // Add a button component to the background to prevent interactions with interactables on the canvases below this canvas
  128. CreateComponent(background, LyShine::UiButtonComponentUuid);
  129. // We want the background to stretch to the corners of the canvas
  130. // So we set the anchors to go all the way to the right and to the bottom
  131. AZ::EntityId backgroundId = background->GetId();
  132. UiTransform2dBus::Event(
  133. backgroundId, &UiTransform2dBus::Events::SetAnchors, UiTransform2dInterface::Anchors(0.0f, 0.0f, 1.0f, 1.0f), false, false);
  134. // Set the color of the background image to black
  135. UiImageBus::Event(backgroundId, &UiImageBus::Events::SetColor, AZ::Color(0.f, 0.f, 0.f, 1.f));
  136. // Set the background button's navigation to none
  137. UiNavigationBus::Event(backgroundId, &UiNavigationBus::Events::SetNavigationMode, UiNavigationInterface::NavigationMode::None);
  138. // Now let's create a child of the background
  139. AZ::Entity* foreground = canvas->CreateChildElement("Foreground");
  140. // Add a transform and an image to the foreground as well
  141. CreateComponent(foreground, LyShine::UiTransform2dComponentUuid);
  142. CreateComponent(foreground, LyShine::UiImageComponentUuid);
  143. // Stretch it to the corners of the background with the anchors, stretch it 90% of background to still a background outline
  144. AZ::EntityId foregroundId = foreground->GetId();
  145. UiTransform2dBus::Event(
  146. foregroundId, &UiTransform2dBus::Events::SetAnchors, UiTransform2dInterface::Anchors(0.1f, 0.1f, 0.9f, 0.9f), false, false);
  147. return foregroundId;
  148. }
  149. ////////////////////////////////////////////////////////////////////////////////////////////////////
  150. void LyShineExamplesCppExample::CreateElementsExample(AZ::EntityId foregroundId)
  151. {
  152. // Create the elements examples section title
  153. CreateText("ElementExamples", false, foregroundId, UiTransform2dInterface::Anchors(0.1f, 0.25f, 0.1f, 0.25f), UiTransform2dInterface::Offsets(),
  154. "Elements examples:", AZ::Color(0.f, 0.f, 0.f, 1.f), IDraw2d::HAlign::Left, IDraw2d::VAlign::Center,
  155. UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  156. // Create an example button
  157. CreateButton("ButtonExample", false, foregroundId, UiTransform2dInterface::Anchors(0.2f, 0.35f, 0.2f, 0.35f), UiTransform2dInterface::Offsets(-100, -25, 100, 25),
  158. "Button", AZ::Color(0.604f, 0.780f, 0.839f, 1.f), AZ::Color(0.380f, 0.745f, 0.871f, 1.f), AZ::Color(0.055f, 0.675f, 0.886f, 1.f), AZ::Color(0.f, 0.f, 0.f, 1.f),
  159. UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  160. // Create an example checkbox
  161. CreateCheckbox("CheckBoxExample", false, foregroundId, UiTransform2dInterface::Anchors(0.5f, 0.35f, 0.5f, 0.35f), UiTransform2dInterface::Offsets(-25, -25, 25, 25),
  162. "Checkbox", AZ::Color(1.f, 1.f, 1.f, 1.f), AZ::Color(0.718f, 0.733f, 0.741f, 1.f), AZ::Color(0.831f, 0.914f, 0.937f, 1.f), AZ::Color(0.2f, 1.f, 0.2f, 1.f), AZ::Color(0.f, 0.f, 0.f, 1.f),
  163. UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  164. // Create an example text input
  165. CreateTextInput("TextInputExample", false, foregroundId, UiTransform2dInterface::Anchors(0.8f, 0.35f, 0.8f, 0.35f), UiTransform2dInterface::Offsets(-100, -25, 100, 25),
  166. "", "Type here...", AZ::Color(1.f, 1.f, 1.f, 1.f), AZ::Color(0.616f, 0.792f, 0.851f, 1.0f), AZ::Color(0.616f, 0.792f, 0.851f, 1.0f), AZ::Color(0.f, 0.f, 0.f, 1.f), AZ::Color(0.43f, 0.43f, 0.43f, 1.f),
  167. UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  168. }
  169. ////////////////////////////////////////////////////////////////////////////////////////////////////
  170. void LyShineExamplesCppExample::CreateBehaviorExample(AZ::EntityId foregroundId)
  171. {
  172. // Create the behavior example section title
  173. CreateText("BehaviorExample", false, foregroundId, UiTransform2dInterface::Anchors(0.1f, 0.5f, 0.1f, 0.5f), UiTransform2dInterface::Offsets(),
  174. "Behavior example:", AZ::Color(0.f, 0.f, 0.f, 1.f), IDraw2d::HAlign::Left, IDraw2d::VAlign::Center, UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  175. // Here we set up a very simple health bar example, all piloted from C++
  176. // Create the health bar that we will use to display the health
  177. // We need a background to show how much of the health has been taken off
  178. AZ::EntityId healthBarBgId = CreateImage("HealthBarBackground", false, foregroundId, UiTransform2dInterface::Anchors(0.5f, 0.65f, 0.5f, 0.65f), UiTransform2dInterface::Offsets(-400, -50, 400, 50),
  179. "Textures/Basic/Button_Sliced_Normal.sprite", UiImageInterface::ImageType::Sliced, AZ::Color(0.2f, 0.2f, 0.2f, 1.f), UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  180. // And then the currently remaining health bar
  181. m_maxHealthBarOffsets = UiTransform2dInterface::Offsets(10, -40, 790, 40);
  182. m_healthBar = CreateImage("HealthBar", false, healthBarBgId, UiTransform2dInterface::Anchors(0.0f, 0.5f, 0.0f, 0.5f), m_maxHealthBarOffsets,
  183. "Textures/Basic/Button_Sliced_Normal.sprite", UiImageInterface::ImageType::Sliced, AZ::Color(0.7f, 0.f, 0.f, 1.f), UiTransformInterface::ScaleToDeviceMode::None);
  184. m_health = 10;
  185. // Create a damage button to decrease the health
  186. m_damageButton = CreateButton("DamageButton", false, foregroundId, UiTransform2dInterface::Anchors(0.35f, 0.8f, 0.35f, 0.8f), UiTransform2dInterface::Offsets(-75, -25, 75, 25),
  187. "Damage", AZ::Color(0.604f, 0.780f, 0.839f, 1.f), AZ::Color(0.380f, 0.745f, 0.871f, 1.f), AZ::Color(0.055f, 0.675f, 0.886f, 1.f), AZ::Color(0.f, 0.f, 0.f, 1.f),
  188. UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  189. UiButtonNotificationBus::MultiHandler::BusConnect(m_damageButton);
  190. // Create a heal button to increase the health
  191. m_healButton = CreateButton("HealButton", false, foregroundId, UiTransform2dInterface::Anchors(0.65f, 0.8f, 0.65f, 0.8f), UiTransform2dInterface::Offsets(-75, -25, 75, 25),
  192. "Heal", AZ::Color(0.604f, 0.780f, 0.839f, 1.f), AZ::Color(0.380f, 0.745f, 0.871f, 1.f), AZ::Color(0.055f, 0.675f, 0.886f, 1.f), AZ::Color(0.f, 0.f, 0.f, 1.f),
  193. UiTransformInterface::ScaleToDeviceMode::UniformScaleToFit);
  194. UiButtonNotificationBus::MultiHandler::BusConnect(m_healButton);
  195. }
  196. ////////////////////////////////////////////////////////////////////////////////////////////////////
  197. void LyShineExamplesCppExample::CreateComponent(AZ::Entity* entity, const AZ::Uuid& componentTypeId)
  198. {
  199. entity->Deactivate();
  200. entity->CreateComponent(componentTypeId);
  201. entity->Activate();
  202. }
  203. ////////////////////////////////////////////////////////////////////////////////////////////////////
  204. AZ::EntityId LyShineExamplesCppExample::CreateButton(const char* name, bool atRoot, AZ::EntityId parent,
  205. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,
  206. const char* text, AZ::Color baseColor, AZ::Color selectedColor, AZ::Color pressedColor, AZ::Color textColor,
  207. UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode)
  208. {
  209. // Create the button element
  210. AZ::Entity* button = nullptr;
  211. if (atRoot)
  212. {
  213. UiCanvasBus::EventResult(button, parent, &UiCanvasBus::Events::CreateChildElement, name);
  214. }
  215. else
  216. {
  217. UiElementBus::EventResult(button, parent, &UiElementBus::Events::CreateChildElement, name);
  218. }
  219. AZ::EntityId buttonId = button->GetId();
  220. // Set up the button element
  221. {
  222. CreateComponent(button, LyShine::UiTransform2dComponentUuid);
  223. CreateComponent(button, LyShine::UiImageComponentUuid);
  224. CreateComponent(button, LyShine::UiButtonComponentUuid);
  225. AZ_Assert(UiTransform2dBus::FindFirstHandler(buttonId), "Transform2d component missing");
  226. UiTransformBus::Event(buttonId, &UiTransformBus::Events::SetScaleToDeviceMode, scaleToDeviceMode);
  227. UiTransform2dBus::Event(buttonId, &UiTransform2dBus::Events::SetAnchors, anchors, false, false);
  228. UiTransform2dBus::Event(buttonId, &UiTransform2dBus::Events::SetOffsets, offsets);
  229. UiImageBus::Event(buttonId, &UiImageBus::Events::SetColor, baseColor);
  230. UiInteractableStatesBus::Event(
  231. buttonId,
  232. &UiInteractableStatesBus::Events::SetStateColor,
  233. UiInteractableStatesInterface::StateHover,
  234. buttonId,
  235. selectedColor);
  236. UiInteractableStatesBus::Event(
  237. buttonId,
  238. &UiInteractableStatesBus::Events::SetStateAlpha,
  239. UiInteractableStatesInterface::StateHover,
  240. buttonId,
  241. selectedColor.GetA());
  242. UiInteractableStatesBus::Event(
  243. buttonId,
  244. &UiInteractableStatesBus::Events::SetStateColor,
  245. UiInteractableStatesInterface::StatePressed,
  246. buttonId,
  247. pressedColor);
  248. UiInteractableStatesBus::Event(
  249. buttonId,
  250. &UiInteractableStatesBus::Events::SetStateAlpha,
  251. UiInteractableStatesInterface::StatePressed,
  252. buttonId,
  253. pressedColor.GetA());
  254. UiImageBus::Event(buttonId, &UiImageBus::Events::SetSpritePathname, "UI/Textures/Prefab/button_normal.sprite");
  255. UiImageBus::Event(buttonId, &UiImageBus::Events::SetImageType, UiImageInterface::ImageType::Sliced);
  256. UiInteractableStatesBus::Event(
  257. buttonId,
  258. &UiInteractableStatesBus::Events::SetStateSpritePathname,
  259. UiInteractableStatesInterface::StateDisabled,
  260. buttonId,
  261. "UI/Textures/Prefab/button_disabled.sprite");
  262. }
  263. AZ::Entity* textElem = nullptr;
  264. UiElementBus::EventResult(textElem, buttonId, &UiElementBus::Events::CreateChildElement, "ButtonText");
  265. AZ::EntityId textId = textElem->GetId();
  266. // Create and set up the text element (text displayed on the button)
  267. {
  268. CreateComponent(textElem, LyShine::UiTransform2dComponentUuid);
  269. CreateComponent(textElem, LyShine::UiTextComponentUuid);
  270. AZ_Assert(UiTransform2dBus::FindFirstHandler(textId), "Transform component missing");
  271. UiTransform2dBus::Event(
  272. textId, &UiTransform2dBus::Events::SetAnchors, UiTransform2dInterface::Anchors(0.5, 0.5, 0.5, 0.5), false, false);
  273. UiTransform2dBus::Event(textId, &UiTransform2dBus::Events::SetOffsets, UiTransform2dInterface::Offsets(0, 0, 0, 0));
  274. UiTextBus::Event(textId, &UiTextBus::Events::SetText, text);
  275. UiTextBus::Event(textId, &UiTextBus::Events::SetTextAlignment, IDraw2d::HAlign::Center, IDraw2d::VAlign::Center);
  276. UiTextBus::Event(textId, &UiTextBus::Events::SetColor, textColor);
  277. UiTextBus::Event(textId, &UiTextBus::Events::SetFontSize, 24.0f);
  278. }
  279. // Trigger all InGamePostActivate
  280. UiInitializationBus::Event(buttonId, &UiInitializationBus::Events::InGamePostActivate);
  281. UiInitializationBus::Event(textId, &UiInitializationBus::Events::InGamePostActivate);
  282. return buttonId;
  283. }
  284. ////////////////////////////////////////////////////////////////////////////////////////////////////
  285. AZ::EntityId LyShineExamplesCppExample::CreateCheckbox(const char* name, bool atRoot, AZ::EntityId parent,
  286. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets, [[maybe_unused]] const char* text,
  287. AZ::Color baseColor, AZ::Color selectedColor, [[maybe_unused]] AZ::Color pressedColor, AZ::Color checkColor, [[maybe_unused]] AZ::Color textColor,
  288. UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode)
  289. {
  290. // Create the checkbox element
  291. AZ::Entity* checkbox = nullptr;
  292. if (atRoot)
  293. {
  294. UiCanvasBus::EventResult(checkbox, parent, &UiCanvasBus::Events::CreateChildElement, name);
  295. }
  296. else
  297. {
  298. UiElementBus::EventResult(checkbox, parent, &UiElementBus::Events::CreateChildElement, name);
  299. }
  300. AZ::EntityId checkboxId = checkbox->GetId();
  301. // Set up the checkbox element
  302. {
  303. CreateComponent(checkbox, LyShine::UiTransform2dComponentUuid);
  304. CreateComponent(checkbox, LyShine::UiImageComponentUuid);
  305. CreateComponent(checkbox, LyShine::UiCheckboxComponentUuid);
  306. AZ_Assert(UiTransform2dBus::FindFirstHandler(checkboxId), "Transform2d component missing");
  307. UiTransformBus::Event(checkboxId, &UiTransformBus::Events::SetScaleToDeviceMode, scaleToDeviceMode);
  308. UiTransform2dBus::Event(checkboxId, &UiTransform2dBus::Events::SetAnchors, anchors, false, false);
  309. UiTransform2dBus::Event(checkboxId, &UiTransform2dBus::Events::SetOffsets, offsets);
  310. UiImageBus::Event(checkboxId, &UiImageBus::Events::SetColor, baseColor);
  311. UiImageBus::Event(checkboxId, &UiImageBus::Events::SetSpritePathname, "UI/Textures/Prefab/checkbox_box_normal.sprite");
  312. UiInteractableStatesBus::Event(
  313. checkboxId,
  314. &UiInteractableStatesBus::Events::SetStateColor,
  315. UiInteractableStatesInterface::StateHover,
  316. checkboxId,
  317. selectedColor);
  318. UiInteractableStatesBus::Event(
  319. checkboxId,
  320. &UiInteractableStatesBus::Events::SetStateAlpha,
  321. UiInteractableStatesInterface::StateHover,
  322. checkboxId,
  323. selectedColor.GetA());
  324. UiInteractableStatesBus::Event(
  325. checkboxId,
  326. &UiInteractableStatesBus::Events::SetStateSpritePathname,
  327. UiInteractableStatesInterface::StateHover,
  328. checkboxId,
  329. "UI/Textures/Prefab/checkbox_box_hover.sprite");
  330. UiInteractableStatesBus::Event(
  331. checkboxId,
  332. &UiInteractableStatesBus::Events::SetStateSpritePathname,
  333. UiInteractableStatesInterface::StateDisabled,
  334. checkboxId,
  335. "UI/Textures/Prefab/checkbox_box_disabled.sprite");
  336. }
  337. // Create the On element (the checkmark that will be displayed when the checkbox is "on")
  338. AZ::Entity* onElement;
  339. UiElementBus::EventResult(onElement, checkboxId, &UiElementBus::Events::CreateChildElement, "onElem");
  340. AZ::EntityId onId = onElement->GetId();
  341. // Set up the On element
  342. {
  343. CreateComponent(onElement, LyShine::UiTransform2dComponentUuid);
  344. CreateComponent(onElement, LyShine::UiImageComponentUuid);
  345. UiTransform2dBus::Event(
  346. onId, &UiTransform2dBus::Events::SetAnchors, UiTransform2dInterface::Anchors(0.5f, 0.5f, 0.5f, 0.5f), false, false);
  347. UiTransform2dBus::Event(onId, &UiTransform2dBus::Events::SetOffsets, offsets);
  348. UiImageBus::Event(onId, &UiImageBus::Events::SetSpritePathname, "UI/Textures/Prefab/checkbox_check.sprite");
  349. UiImageBus::Event(onId, &UiImageBus::Events::SetColor, checkColor);
  350. }
  351. // Link the on and off child entities to the parent checkbox entity.
  352. UiCheckboxBus::Event(checkboxId, &UiCheckboxBus::Events::SetCheckedEntity, onId);
  353. // Trigger all InGamePostActivate
  354. UiInitializationBus::Event(onId, &UiInitializationBus::Events::InGamePostActivate);
  355. UiInitializationBus::Event(checkboxId, &UiInitializationBus::Events::InGamePostActivate);
  356. return checkboxId;
  357. }
  358. ////////////////////////////////////////////////////////////////////////////////////////////////////
  359. AZ::EntityId LyShineExamplesCppExample::CreateText(const char* name, bool atRoot, AZ::EntityId parent,
  360. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,
  361. const char* text, AZ::Color textColor, IDraw2d::HAlign hAlign, IDraw2d::VAlign vAlign,
  362. UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode)
  363. {
  364. // Create the text element
  365. AZ::Entity* textElem = nullptr;
  366. if (atRoot)
  367. {
  368. UiCanvasBus::EventResult(textElem, parent, &UiCanvasBus::Events::CreateChildElement, name);
  369. }
  370. else
  371. {
  372. UiElementBus::EventResult(textElem, parent, &UiElementBus::Events::CreateChildElement, name);
  373. }
  374. AZ::EntityId textId = textElem->GetId();
  375. // Set up the text element
  376. {
  377. CreateComponent(textElem, LyShine::UiTransform2dComponentUuid);
  378. CreateComponent(textElem, LyShine::UiTextComponentUuid);
  379. AZ_Assert(UiTransform2dBus::FindFirstHandler(textId), "Transform component missing");
  380. UiTransformBus::Event(textId, &UiTransformBus::Events::SetScaleToDeviceMode, scaleToDeviceMode);
  381. UiTransform2dBus::Event(textId, &UiTransform2dBus::Events::SetAnchors, anchors, false, false);
  382. UiTransform2dBus::Event(textId, &UiTransform2dBus::Events::SetOffsets, offsets);
  383. UiTextBus::Event(textId, &UiTextBus::Events::SetText, text);
  384. UiTextBus::Event(textId, &UiTextBus::Events::SetTextAlignment, hAlign, vAlign);
  385. UiTextBus::Event(textId, &UiTextBus::Events::SetColor, textColor);
  386. }
  387. // Trigger all InGamePostActivate
  388. UiInitializationBus::Event(textId, &UiInitializationBus::Events::InGamePostActivate);
  389. return textId;
  390. }
  391. ////////////////////////////////////////////////////////////////////////////////////////////////////
  392. AZ::EntityId LyShineExamplesCppExample::CreateTextInput(const char* name, bool atRoot, AZ::EntityId parent,
  393. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,
  394. const char* text, const char* placeHolderText,
  395. AZ::Color baseColor, AZ::Color selectedColor, AZ::Color pressedColor,
  396. AZ::Color textColor, AZ::Color placeHolderColor,
  397. UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode)
  398. {
  399. // Create the text input element
  400. AZ::Entity* textInputElem = nullptr;
  401. if (atRoot)
  402. {
  403. UiCanvasBus::EventResult(textInputElem, parent, &UiCanvasBus::Events::CreateChildElement, name);
  404. }
  405. else
  406. {
  407. UiElementBus::EventResult(textInputElem, parent, &UiElementBus::Events::CreateChildElement, name);
  408. }
  409. AZ::EntityId textInputId = textInputElem->GetId();
  410. // Set up the text input element
  411. {
  412. CreateComponent(textInputElem, LyShine::UiTransform2dComponentUuid);
  413. CreateComponent(textInputElem, LyShine::UiImageComponentUuid);
  414. CreateComponent(textInputElem, LyShine::UiTextInputComponentUuid);
  415. AZ_Assert(UiTransform2dBus::FindFirstHandler(textInputId), "Transform2d component missing");
  416. UiTransformBus::Event(textInputId, &UiTransformBus::Events::SetScaleToDeviceMode, scaleToDeviceMode);
  417. UiTransform2dBus::Event(textInputId, &UiTransform2dBus::Events::SetAnchors, anchors, false, false);
  418. UiTransform2dBus::Event(textInputId, &UiTransform2dBus::Events::SetOffsets, offsets);
  419. UiImageBus::Event(textInputId, &UiImageBus::Events::SetColor, baseColor);
  420. UiInteractableStatesBus::Event(
  421. textInputId,
  422. &UiInteractableStatesBus::Events::SetStateColor,
  423. UiInteractableStatesInterface::StateHover,
  424. textInputId,
  425. selectedColor);
  426. UiInteractableStatesBus::Event(
  427. textInputId,
  428. &UiInteractableStatesBus::Events::SetStateAlpha,
  429. UiInteractableStatesInterface::StateHover,
  430. textInputId,
  431. selectedColor.GetA());
  432. UiInteractableStatesBus::Event(
  433. textInputId,
  434. &UiInteractableStatesBus::Events::SetStateColor,
  435. UiInteractableStatesInterface::StatePressed,
  436. textInputId,
  437. pressedColor);
  438. UiInteractableStatesBus::Event(
  439. textInputId,
  440. &UiInteractableStatesBus::Events::SetStateAlpha,
  441. UiInteractableStatesInterface::StatePressed,
  442. textInputId,
  443. pressedColor.GetA());
  444. UiImageBus::Event(textInputId, &UiImageBus::Events::SetSpritePathname, "UI/Textures/Prefab/textinput_normal.sprite");
  445. UiImageBus::Event(textInputId, &UiImageBus::Events::SetImageType, UiImageInterface::ImageType::Sliced);
  446. UiInteractableStatesBus::Event(
  447. textInputId,
  448. &UiInteractableStatesBus::Events::SetStateSpritePathname,
  449. UiInteractableStatesInterface::StateHover,
  450. textInputId,
  451. "UI/Textures/Prefab/textinput_hover.sprite");
  452. UiInteractableStatesBus::Event(
  453. textInputId,
  454. &UiInteractableStatesBus::Events::SetStateSpritePathname,
  455. UiInteractableStatesInterface::StateDisabled,
  456. textInputId,
  457. "UI/Textures/Prefab/textinput_disabled.sprite");
  458. }
  459. // Create the text element (what the user will type)
  460. AZ::EntityId textElemId = CreateText("Text", false, textInputId,
  461. UiTransform2dInterface::Anchors(0.0f, 0.0f, 1.0f, 1.0f),
  462. UiTransform2dInterface::Offsets(5.0f, 5.0f, -5.0f, -5.00f),
  463. text, textColor, IDraw2d::HAlign::Center, IDraw2d::VAlign::Center);
  464. // reduce the font size
  465. UiTextBus::Event(textElemId, &UiTextBus::Events::SetFontSize, 24.0f);
  466. // now link the textInputComponent to the child text entity
  467. UiTextInputBus::Event(textInputId, &UiTextInputBus::Events::SetTextEntity, textElemId);
  468. // Create the placeholder text element (what appears before any text is typed)
  469. AZ::EntityId placeHolderElemId = CreateText("PlaceholderText", false, textInputId,
  470. UiTransform2dInterface::Anchors(0.0f, 0.0f, 1.0f, 1.0f),
  471. UiTransform2dInterface::Offsets(5.0f, 5.0f, -5.0f, -5.00f),
  472. placeHolderText, placeHolderColor, IDraw2d::HAlign::Center, IDraw2d::VAlign::Center);
  473. // reduce the font size
  474. UiTextBus::Event(placeHolderElemId, &UiTextBus::Events::SetFontSize, 24.0f);
  475. // now link the textInputComponent to the child placeholder text entity
  476. UiTextInputBus::Event(textInputId, &UiTextInputBus::Events::SetPlaceHolderTextEntity, placeHolderElemId);
  477. // Trigger all InGamePostActivate
  478. UiInitializationBus::Event(textInputId, &UiInitializationBus::Events::InGamePostActivate);
  479. UiInitializationBus::Event(textElemId, &UiInitializationBus::Events::InGamePostActivate);
  480. UiInitializationBus::Event(placeHolderElemId, &UiInitializationBus::Events::InGamePostActivate);
  481. return textInputId;
  482. }
  483. ////////////////////////////////////////////////////////////////////////////////////////////////////
  484. AZ::EntityId LyShineExamplesCppExample::CreateImage(const char* name, bool atRoot, AZ::EntityId parent,
  485. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,
  486. AZStd::string spritePath, UiImageInterface::ImageType imageType, AZ::Color color,
  487. UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode)
  488. {
  489. // Create the image element
  490. AZ::Entity* image = nullptr;
  491. if (atRoot)
  492. {
  493. UiCanvasBus::EventResult(image, parent, &UiCanvasBus::Events::CreateChildElement, name);
  494. }
  495. else
  496. {
  497. UiElementBus::EventResult(image, parent, &UiElementBus::Events::CreateChildElement, name);
  498. }
  499. AZ::EntityId imageId = image->GetId();
  500. // Set up the image element
  501. {
  502. CreateComponent(image, LyShine::UiTransform2dComponentUuid);
  503. CreateComponent(image, LyShine::UiImageComponentUuid);
  504. AZ_Assert(UiTransform2dBus::FindFirstHandler(imageId), "Transform2d component missing");
  505. UiTransformBus::Event(imageId, &UiTransformBus::Events::SetScaleToDeviceMode, scaleToDeviceMode);
  506. UiTransform2dBus::Event(imageId, &UiTransform2dBus::Events::SetAnchors, anchors, false, false);
  507. UiTransform2dBus::Event(imageId, &UiTransform2dBus::Events::SetOffsets, offsets);
  508. UiImageBus::Event(imageId, &UiImageBus::Events::SetColor, color);
  509. UiImageBus::Event(imageId, &UiImageBus::Events::SetSpritePathname, spritePath);
  510. UiImageBus::Event(imageId, &UiImageBus::Events::SetImageType, imageType);
  511. }
  512. // Trigger all InGamePostActivate
  513. UiInitializationBus::Event(imageId, &UiInitializationBus::Events::InGamePostActivate);
  514. return imageId;
  515. }
  516. ////////////////////////////////////////////////////////////////////////////////////////////////////
  517. void LyShineExamplesCppExample::UpdateHealth(int change)
  518. {
  519. // Max health is 10, min health is 0
  520. m_health = AZStd::max(0, AZStd::min(10, m_health + change));
  521. // Update the health bar accordingly
  522. UiTransform2dInterface::Offsets newOffsets = m_maxHealthBarOffsets;
  523. float healthFraction = m_health / 10.f;
  524. newOffsets.m_right = m_maxHealthBarOffsets.m_left + (m_maxHealthBarOffsets.m_right - m_maxHealthBarOffsets.m_left) * healthFraction;
  525. UiTransform2dBus::Event(m_healthBar, &UiTransform2dBus::Events::SetOffsets, newOffsets);
  526. }
  527. }