You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
o3de/Gems/GraphCanvas/Code/Source/Translation/TranslationBus.h

194 lines
5.4 KiB
C++

/*
* Copyright (c) Contributors to the Open 3D Engine Project.
* For complete copyright and license terms please see the LICENSE at the root of this distribution.
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*
*/
#pragma once
#include "TranslationAsset.h"
#include <AzCore/std/string/conversions.h>
#include <AzCore/StringFunc/StringFunc.h>
namespace GraphCanvas
{
namespace Translation
{
using Handle = size_t;
}
class TranslationKey
{
public:
TranslationKey() = default;
explicit TranslationKey(const AZStd::string& key)
: m_key(key)
{}
TranslationKey(const TranslationKey& rhs)
{
m_key = rhs.m_key;
}
TranslationKey& operator = (const AZStd::string& key)
{
m_key = key;
return *this;
}
bool operator == (const TranslationKey& rhs) const
{
return m_key.compare(rhs.m_key) == 0;
}
template <typename T>
auto operator << (T value) -> AZStd::enable_if_t<AZStd::is_same_v<std::void_t<decltype(AZStd::to_string(value))>, void>, TranslationKey&>
{
if (!m_key.empty() && !AZStd::to_string(value).empty())
{
m_key.append(".");
}
if (!AZStd::to_string(value).empty())
{
m_key.append(AZStd::to_string(value));
}
return *this;
}
TranslationKey& operator << (const AZStd::string& value)
{
if (!m_key.empty() && !value.empty())
{
m_key.append(".");
}
if (!value.empty())
{
m_key.append(value.c_str());
}
return *this;
}
const AZStd::string operator + (const AZStd::string& value)
{
return m_key + value;
}
void clear() { m_key.clear(); }
const AZStd::string& ToString() const { return m_key; }
operator AZStd::string() { return m_key; }
static AZStd::string Sanitize(const AZStd::string& text)
{
AZStd::string result = text;
AZ::StringFunc::Replace(result, "*", "x");
AZ::StringFunc::Replace(result, "(", "_");
AZ::StringFunc::Replace(result, ")", "_");
AZ::StringFunc::Replace(result, "{", "_");
AZ::StringFunc::Replace(result, "}", "_");
AZ::StringFunc::Replace(result, ":", "_");
AZ::StringFunc::Replace(result, "<", "_");
AZ::StringFunc::Replace(result, ",", "_");
AZ::StringFunc::Replace(result, ">", " ");
AZ::StringFunc::Replace(result, "/", "");
AZ::StringFunc::Strip(result, " ");
AZ::StringFunc::Path::Normalize(result);
return result;
}
protected:
AZStd::string m_key;
};
//! Requests to access the database
class TranslationRequests : public AZ::EBusTraits
{
public:
using MutexType = AZStd::recursive_mutex;
//! Restores the database from all the assets
virtual void Restore() {}
//! Returns true if they database has the specified key
virtual bool HasKey(const AZStd::string& /*key*/) { return false; }
//! Returns the text value for a given key
virtual bool Get(const AZStd::string& /*key*/, AZStd::string& /*value*/) { return false; }
struct Details
{
AZStd::string m_name;
AZStd::string m_tooltip;
AZStd::string m_category;
AZStd::string m_subtitle;
bool m_valid = false;
Details() = default;
Details(const Details& rhs)
{
m_name = rhs.m_name;
m_tooltip = rhs.m_tooltip;
m_category = rhs.m_category;
m_subtitle = rhs.m_subtitle;
m_valid = rhs.m_valid;
}
Details(const char* name, const char* tooltip, const char* subtitle, const char* category)
: m_name(name), m_tooltip(tooltip), m_subtitle(subtitle), m_category(category)
{
m_valid = !m_name.empty();
}
};
//! Adds an entry into the database, returns true if there were any warnings while adding to the database
virtual bool Add(const TranslationFormat& /*translationFormat*/) { return false; }
//! Get the details associated with a given key (assumes they are within a "details" object)
virtual Details GetDetails(const AZStd::string& /*key*/, const Details& /*fallbackDetails*/) { return Details(); }
//! Generates the source JSON assets for all reflected elements
virtual void GenerateSourceAssets() {}
//! Stores the runtime database into a json file for debugging only
virtual void DumpDatabase(const AZStd::string& /*filename*/) {}
};
using TranslationRequestBus = AZ::EBus<TranslationRequests>;
}
namespace AZStd
{
template <typename T>
struct hash;
// hashing support for STL containers
template <>
struct hash<GraphCanvas::TranslationKey>
{
using argument_type = GraphCanvas::TranslationKey;
using result_type = AZStd::size_t;
inline size_t operator()(const GraphCanvas::TranslationKey& value) const
{
size_t h = 0;
hash_combine(h, value.ToString().c_str());
return h;
}
};
}