LyShineExamplesCppExample.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. #pragma once
  9. #include <AzCore/Slice/SliceAsset.h>
  10. #include <LyShine/IDraw2d.h>
  11. #include <LyShine/Bus/UiCanvasBus.h>
  12. #include <LyShine/Bus/UiTransform2dBus.h>
  13. #include <LyShine/Bus/UiImageBus.h>
  14. #include <LyShine/Bus/UiButtonBus.h>
  15. #include <LyShineExamples/LyShineExamplesCppExampleBus.h>
  16. namespace LyShineExamples
  17. {
  18. ////////////////////////////////////////////////////////////////////////////////////////////////////
  19. //! Class for demonstrating how to programmatically create a canvas from scratch in C++
  20. //! The created canvas shows a few examples of interactable elements (a button, a checkbox, and a
  21. //! textInput) as well as a very simple example of custom behavior (a small health system with a
  22. //! health bar that can be damaged / healed through two buttons).
  23. class LyShineExamplesCppExample
  24. : protected LyShineExamplesCppExampleBus::Handler
  25. , public UiButtonNotificationBus::MultiHandler
  26. {
  27. public: // member functions
  28. LyShineExamplesCppExample();
  29. ~LyShineExamplesCppExample();
  30. // LyShineExamplesCppExampleBus
  31. void CreateCanvas() override;
  32. void DestroyCanvas() override;
  33. // ~LyShineExamplesCppExampleBus
  34. // UiButtonNotificationBus
  35. void OnButtonClick() override;
  36. // ~UiButtonNotificationBus
  37. static void Reflect(AZ::ReflectContext* context);
  38. private: // member functions
  39. AZ_DISABLE_COPY_MOVE(LyShineExamplesCppExample);
  40. //! Create the background image
  41. AZ::EntityId CreateBackground();
  42. //! Create elements from the ground up
  43. void CreateElementsExample(AZ::EntityId foregroundId);
  44. //! Create elements with programmatic behavior
  45. void CreateBehaviorExample(AZ::EntityId foregroundId);
  46. //! Creates a component
  47. void CreateComponent(AZ::Entity* entity, const AZ::Uuid& componentTypeId);
  48. //! Creates a button element
  49. AZ::EntityId CreateButton(const char* name, bool atRoot, AZ::EntityId parent,
  50. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,
  51. const char* text, AZ::Color baseColor, AZ::Color selectedColor, AZ::Color pressedColor,
  52. AZ::Color textColor, UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode);
  53. //! Creates a checkbox element
  54. AZ::EntityId CreateCheckbox(const char* name, bool atRoot, AZ::EntityId parent,
  55. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,
  56. const char* text, AZ::Color baseColor, AZ::Color selectedColor, AZ::Color pressedColor,
  57. AZ::Color checkColor, AZ::Color textColor,
  58. UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode = UiTransformInterface::ScaleToDeviceMode::None);
  59. //! Creates a text element
  60. AZ::EntityId CreateText(const char* name, bool atRoot, AZ::EntityId parent,
  61. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,
  62. const char* text, AZ::Color textColor, IDraw2d::HAlign hAlign, IDraw2d::VAlign vAlign,
  63. UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode = UiTransformInterface::ScaleToDeviceMode::None);
  64. //! Creates a text input element
  65. AZ::EntityId CreateTextInput(const char* name, bool atRoot, AZ::EntityId parent,
  66. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,
  67. const char* text, const char* placeHolderText,
  68. AZ::Color baseColor, AZ::Color selectedColor, AZ::Color pressedColor,
  69. AZ::Color textColor, AZ::Color placeHolderColor,
  70. UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode = UiTransformInterface::ScaleToDeviceMode::None);
  71. //! Creates an image element
  72. AZ::EntityId CreateImage(const char* name, bool atRoot, AZ::EntityId parent,
  73. UiTransform2dInterface::Anchors anchors, UiTransform2dInterface::Offsets offsets,
  74. AZStd::string spritePath, UiImageInterface::ImageType imageType, AZ::Color color,
  75. UiTransformInterface::ScaleToDeviceMode scaleToDeviceMode = UiTransformInterface::ScaleToDeviceMode::None);
  76. //! Change the health by change amount and update the health bar
  77. void UpdateHealth(int change);
  78. private: // data
  79. AZ::EntityId m_canvasId;
  80. AZ::EntityId m_damageButton;
  81. AZ::EntityId m_healButton;
  82. AZ::EntityId m_destroyButton;
  83. AZ::EntityId m_healthBar;
  84. UiTransform2dInterface::Offsets m_maxHealthBarOffsets;
  85. int m_health;
  86. };
  87. }