TranslationBus.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 "TranslationAsset.h"
  10. #include <AzCore/std/string/conversions.h>
  11. #include <AzCore/StringFunc/StringFunc.h>
  12. namespace GraphCanvas
  13. {
  14. namespace Translation
  15. {
  16. using Handle = size_t;
  17. }
  18. class TranslationKey
  19. {
  20. public:
  21. TranslationKey() = default;
  22. explicit TranslationKey(const AZStd::string& key)
  23. : m_key(key)
  24. {}
  25. TranslationKey(const TranslationKey& rhs)
  26. {
  27. m_key = rhs.m_key;
  28. }
  29. TranslationKey& operator = (const AZStd::string& key)
  30. {
  31. m_key = key;
  32. return *this;
  33. }
  34. bool operator == (const char* keyStr) const
  35. {
  36. return m_key.compare(keyStr) == 0;
  37. }
  38. bool operator == (const TranslationKey& rhs) const
  39. {
  40. return m_key.compare(rhs.m_key) == 0;
  41. }
  42. template <typename T>
  43. auto operator<< (T&& value) ->
  44. AZStd::enable_if_t<AZStd::is_void_v<AZStd::void_t<decltype(AZStd::to_string(value))>>, TranslationKey&>
  45. {
  46. AZStd::string valueString = AZStd::to_string(AZStd::forward<T>(value));
  47. if (!m_key.empty() && !valueString.empty())
  48. {
  49. m_key.append(".");
  50. }
  51. if (!valueString.empty())
  52. {
  53. m_key.append(valueString);
  54. }
  55. return *this;
  56. }
  57. TranslationKey& operator<< (const AZStd::string& value)
  58. {
  59. if (!m_key.empty() && !value.empty())
  60. {
  61. m_key.append(".");
  62. }
  63. if (!value.empty())
  64. {
  65. m_key.append(value);
  66. }
  67. return *this;
  68. }
  69. const AZStd::string operator + (const AZStd::string& value)
  70. {
  71. return m_key + value;
  72. }
  73. void clear() { m_key.clear(); }
  74. const AZStd::string& ToString() const { return m_key; }
  75. operator AZStd::string() { return m_key; }
  76. static AZStd::string Sanitize(const AZStd::string& text)
  77. {
  78. AZStd::string result = text;
  79. AZ::StringFunc::Replace(result, "*", "x");
  80. AZ::StringFunc::Replace(result, "(", "_");
  81. AZ::StringFunc::Replace(result, ")", "_");
  82. AZ::StringFunc::Replace(result, "{", "_");
  83. AZ::StringFunc::Replace(result, "}", "_");
  84. AZ::StringFunc::Replace(result, ":", "_");
  85. AZ::StringFunc::Replace(result, "<", "_");
  86. AZ::StringFunc::Replace(result, ",", "_");
  87. AZ::StringFunc::Replace(result, ">", " ");
  88. AZ::StringFunc::Replace(result, "/", "");
  89. AZ::StringFunc::Strip(result, " ");
  90. AZ::StringFunc::Path::Normalize(result);
  91. return result;
  92. }
  93. protected:
  94. AZStd::string m_key;
  95. };
  96. //! Requests to access the database
  97. class TranslationRequests : public AZ::EBusTraits
  98. {
  99. public:
  100. using MutexType = AZStd::recursive_mutex;
  101. //! Restores the database from all the assets
  102. virtual void Restore() {}
  103. //! Returns true if they database has the specified key
  104. virtual bool HasKey(const AZStd::string& /*key*/) { return false; }
  105. //! Returns the text value for a given key
  106. virtual bool Get(const AZStd::string& /*key*/, AZStd::string& /*value*/) { return false; }
  107. struct Details
  108. {
  109. AZStd::string m_name;
  110. AZStd::string m_tooltip;
  111. AZStd::string m_category;
  112. AZStd::string m_subtitle;
  113. bool m_valid = false;
  114. Details() = default;
  115. Details(const Details& rhs)
  116. {
  117. m_name = rhs.m_name;
  118. m_tooltip = rhs.m_tooltip;
  119. m_category = rhs.m_category;
  120. m_subtitle = rhs.m_subtitle;
  121. m_valid = rhs.m_valid;
  122. }
  123. Details(const char* name, const char* tooltip, const char* subtitle, const char* category)
  124. : m_name(name), m_tooltip(tooltip), m_subtitle(subtitle), m_category(category)
  125. {
  126. m_valid = !m_name.empty();
  127. }
  128. };
  129. //! Adds an entry into the database, returns true if there were any warnings while adding to the database
  130. virtual bool Add(const TranslationFormat& /*translationFormat*/) { return false; }
  131. //! Get the details associated with a given key (assumes they are within a "details" object)
  132. virtual Details GetDetails(const AZStd::string& /*key*/, const Details& /*fallbackDetails*/) { return Details(); }
  133. //! Generates the source JSON assets for all reflected elements
  134. virtual void GenerateSourceAssets() {}
  135. //! Stores the runtime database into a json file for debugging only
  136. virtual void DumpDatabase(const AZStd::string& /*filename*/) {}
  137. };
  138. using TranslationRequestBus = AZ::EBus<TranslationRequests>;
  139. }
  140. namespace AZStd
  141. {
  142. template <typename T>
  143. struct hash;
  144. // hashing support for STL containers
  145. template <>
  146. struct hash<GraphCanvas::TranslationKey>
  147. {
  148. using argument_type = GraphCanvas::TranslationKey;
  149. using result_type = AZStd::size_t;
  150. inline size_t operator()(const GraphCanvas::TranslationKey& value) const
  151. {
  152. size_t h = 0;
  153. hash_combine(h, value.ToString().c_str());
  154. return h;
  155. }
  156. };
  157. }