AtomFontSystemComponent.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #define USE_NULLFONT
  9. #include "AtomFontSystemComponent.h"
  10. #include <Atom/RHI/RHIUtils.h>
  11. #include <AzCore/Serialization/SerializeContext.h>
  12. #include <AzCore/Serialization/EditContext.h>
  13. #include <AzCore/Serialization/EditContextConstants.inl>
  14. #include <AzFramework/Components/ConsoleBus.h>
  15. #include <ISystem.h>
  16. #include <AtomLyIntegration/AtomFont/AtomNullFont.h>
  17. #include <AtomLyIntegration/AtomFont/AtomFont.h>
  18. namespace AZ
  19. {
  20. namespace Render
  21. {
  22. void AtomFontSystemComponent::Reflect(AZ::ReflectContext* context)
  23. {
  24. if (AZ::SerializeContext* serialize = azrtti_cast<AZ::SerializeContext*>(context))
  25. {
  26. serialize->Class<AtomFontSystemComponent, AZ::Component>()
  27. ->Version(0)
  28. ;
  29. if (AZ::EditContext* ec = serialize->GetEditContext())
  30. {
  31. ec->Class<AtomFontSystemComponent>("Font", "Manages lifetime of the font subsystem")
  32. ->ClassElement(Edit::ClassElements::EditorData, "")
  33. ->Attribute(Edit::Attributes::AutoExpand, true)
  34. ;
  35. }
  36. }
  37. }
  38. void AtomFontSystemComponent::GetProvidedServices(AZ::ComponentDescriptor::DependencyArrayType& provided)
  39. {
  40. provided.push_back(AZ_CRC_CE("AtomFontService"));
  41. }
  42. void AtomFontSystemComponent::GetIncompatibleServices(AZ::ComponentDescriptor::DependencyArrayType& incompatible)
  43. {
  44. incompatible.push_back(AZ_CRC_CE("AtomFontService"));
  45. }
  46. void AtomFontSystemComponent::GetRequiredServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& required)
  47. {
  48. }
  49. void AtomFontSystemComponent::GetDependentServices([[maybe_unused]] AZ::ComponentDescriptor::DependencyArrayType& dependent)
  50. {
  51. }
  52. void AtomFontSystemComponent::Activate()
  53. {
  54. CrySystemEventBus::Handler::BusConnect();
  55. }
  56. void AtomFontSystemComponent::Deactivate()
  57. {
  58. CrySystemEventBus::Handler::BusDisconnect();
  59. }
  60. void LoadFont(ICryFont& cryFont, const AZStd::string& fontName)
  61. {
  62. IFFont* font = cryFont.NewFont(fontName.c_str());
  63. AZ_Assert(font, "Could not instantiate font: %s", fontName.c_str());
  64. const AZStd::string fontPath = "Fonts/" + fontName + ".font";
  65. if (!font->Load(fontPath.c_str()))
  66. {
  67. AZ_Error("AtomFont", false, "Could not load font: %s", fontPath.c_str());
  68. }
  69. }
  70. void AtomFontSystemComponent::OnCrySystemInitialized(ISystem& system, const SSystemInitParams&)
  71. {
  72. #if !defined(AZ_MONOLITHIC_BUILD)
  73. // When module is linked dynamically, we must set our gEnv pointer.
  74. // When module is linked statically, we'll share the application's gEnv pointer.
  75. gEnv = system.GetGlobalEnvironment();
  76. #endif
  77. if (RHI::IsNullRHI())
  78. {
  79. gEnv->pCryFont = new AtomNullFont();
  80. }
  81. else
  82. {
  83. gEnv->pCryFont = new AtomFont(&system);
  84. }
  85. if (gEnv->pCryFont)
  86. {
  87. LoadFont(*gEnv->pCryFont, "default");
  88. LoadFont(*gEnv->pCryFont, "default-ui");
  89. }
  90. }
  91. void AtomFontSystemComponent::OnCrySystemShutdown([[maybe_unused]] ISystem& system)
  92. {
  93. #if !defined(AZ_MONOLITHIC_BUILD)
  94. gEnv = nullptr;
  95. #endif
  96. }
  97. } // namespace Render
  98. } // namespace AZ