Newer
Older
Import / projects / Gameloft / bne_lib / code / GameSWF / GameSWFLocalization.cpp
/// local includes
#include "GameSWFLocalization.h"

/// library includes
#include <CasualCore.h>
#include <GameSWF/include/gameswf/swf/sprite.h>

GameSWFLocalization::GameSWFLocalization()
: m_localizationHandles()
{
  gameswf::registerNativeFunction("GetLanguage", &GameSWFLocalization::NativeGetLanguage);
  gameswf::registerNativeFunction("GetLocalizedString", &GameSWFLocalization::NativeGetLocalizedString);
  gameswf::registerNativeFunction("GetLocalizedFormattedString", &GameSWFLocalization::NativeGetLocalizedFormattedString);
  gameswf::registerNativeFunction("GetLocalizedTimeString", &GameSWFLocalization::NativeGetLocalizedTimeString);
  gameswf::registerNativeFunction("GetLocalizedNumberString", &GameSWFLocalization::NativeGetLocalizedNumberString);
  gameswf::registerNativeFunction("AddToLocalizationList", &GameSWFLocalization::NativeAddToLocalizationList);
  gameswf::registerNativeFunction("RemoveFromLocalizationList", &GameSWFLocalization::NativeRemoveFromLocalizationList);
}

void GameSWFLocalization::UpdateLanguage()
{
  int language_code = CGAME->GetLanguage();
  gameswf::ASValue new_language(language_code);

  uint32 size = m_localizationHandles.Size();
  for (uint32 i = 0; i < size; ++i)
  {
    gameswf::CharacterHandle ch = m_localizationHandles[i];
    if (ch.isValid())
    {
      ch.invokeMethod("onChangeLanguage", &new_language, 1);
    }
  }
}

// Called from Actionscript, localised string fetched, passed back to Actionscript
void GameSWFLocalization::NativeGetLocalizedString(const gameswf::FunctionCall& fn)
{
  if (fn.nargs > 0)
  {
    const char* arg = fn.arg(0).toCStr();
    gameswf::String str = CGAME->GetStringPack()->Translate(arg).GetString();
    fn.result->setString(str);
  }
  else
  {
    RKLOGt("localization", "Invalid argument count for NativeGetLocalizedString; requires at least one argument");
  }
}

// Called from Actionscript, localised string fetched, passed back to Actionscript
void GameSWFLocalization::NativeGetLocalizedFormattedString(const gameswf::FunctionCall& fn)
{
  if (fn.nargs > 0)
  {
    const char* arg = fn.arg(0).toCStr();
    int argsLen = fn.nargs;
    RKArgumentList& lst = getRKArgumentList();
    for (int i = 1; i < argsLen; ++i)
    {
      int pListIndex = i - 1; // fn.args index is 1 to size (element 0 is the string). pList args is 0-based.

      if (fn.arg(i).isNull() || fn.arg(i).isUndefined())
      {
        RKLOGt_ERROR("ui", "Passing an undefined or null parameter into NativeGetLocalizedFormattedString()! Unable to get correct value.");
      }
      else if (fn.arg(i).isNumber())
      {
        float valFloat = fn.arg(i).toFloat();
        lst.argument[pListIndex].value = *(reinterpret_cast<RKAVal*>(&valFloat));
        lst.argument[pListIndex].typeId = RKSA_typeID(valFloat);
      }
      else if (fn.arg(i).isString())
      {
        const char* valStr = fn.arg(i).toString().c_str();
        lst.argument[pListIndex].value = *(reinterpret_cast<RKAVal*>(&valStr));
        lst.argument[pListIndex].typeId = RKSA_typeID(valStr);
      }
    }
    lst.nbArguments = argsLen - 1; // not including the base string (fn.arg(0))

    gameswf::String str = CGAME->GetStringPack()->Format(arg, lst).GetString();
    fn.result->setString(str);
  }
  else
  {
    RKLOGt("localization", "Invalid argument count for NativeGetLocalizedFormattedString; requires at least one argument");
  }
}

// Called from Actionscript, localised string fetched, passed back to Actionscript
void GameSWFLocalization::NativeGetLocalizedTimeString(const gameswf::FunctionCall& fn)
{
  if (fn.nargs > 0)
  {
    const float& time = fn.arg(0).toFloat();
    gameswf::String str = CGAME->GetStringPack()->FormatTime(time).GetString();
    fn.result->setString(str);
  }
  else
  {
    RKLOGt("localization", "Invalid argument count for NativeGetLocalizedTimeString; requires at least one argument");
  }
}

void GameSWFLocalization::NativeGetLocalizedNumberString(const gameswf::FunctionCall& fn)
{
  if (fn.nargs > 0)
  {
    const int& val = fn.arg(0).toInt();
    gameswf::String str = CGAME->GetStringPack()->FormatNumber(val).GetString();
    fn.result->setString(str);
  }
  else
  {
    RKLOGt("localization", "Invalid argument count for NativeGetLocalizedNumberString; requires at least one argument");
  }
}

void GameSWFLocalization::NativeGetLanguage(const gameswf::FunctionCall& fn)
{
  fn.result->setInt(CGAME->GetLanguage());
}

void
GameSWFLocalization::NativeAddToLocalizationList(const gameswf::FunctionCall& fn)
{
  //
  // THIS CODE IS OBSOLETE KEEP THIS FUNCTION STUB HERE FOR ACTIONSCRIPT REFERENCES
  //
#if 0
  if (fn.nargs > 0)
  {
    gameswf::ASValue value = fn.arg(0);
    if (!value.isNull())
    {
      GameSWFLocalization& localization = GameSWFLocalization::GetInstance();
      gameswf::SpriteInstance* instance = gameswf::castTo<gameswf::SpriteInstance>(value.toObject());
      localization.m_localizationHandles.AppendUnique(instance->getHandle());
    }
  }
  else
  {
    RKLOGt("localization", "Invalid argument count for NativeAddToLocalizationList; requires at least one argument");
  }
#endif
}

void GameSWFLocalization::NativeRemoveFromLocalizationList(const gameswf::FunctionCall& fn)
{
  //
  // THIS CODE IS OBSOLETE KEEP THIS FUNCTION STUB HERE FOR ACTIONSCRIPT REFERENCES
  //
#if 0
  if (fn.nargs > 0)
  {
    gameswf::ASValue value = fn.arg(0);
    if (!value.isNull())
    {
      gameswf::SpriteInstance* instance = gameswf::castTo<gameswf::SpriteInstance>(value.toObject());
      GameSWFLocalization& localization = GameSWFLocalization::GetInstance();
      localization.m_localizationHandles.EraseAll(instance->getHandle());
    }
  }
  else
  {
    RKLOGt("localization", "Invalid argument count for NativeRemoveFromLocalizationList; requires at least one argument");
  }
#endif
}

void GameSWFLocalization::RemoveLocalizedElements(const gameswf::FlashFX* a_pFx)
{
  //
  // THIS CODE IS OBSOLETE KEEP THIS FUNCTION STUB HERE FOR ACTIONSCRIPT REFERENCES
  //
#if 0
  if (a_pFx != nullptr)
  {
    RecursiveRemoveLocalizedElements(a_pFx->getRootHandle());
  }
#endif
}

void GameSWFLocalization::RecursiveRemoveLocalizedElements(gameswf::CharacterHandle a_hChar)
{
  //
  // THIS CODE IS OBSOLETE KEEP THIS FUNCTION STUB HERE FOR ACTIONSCRIPT REFERENCES
  //
#if 0
  gameswf::array<gameswf::CharacterHandle> pChildren;
  a_hChar.getChildren(pChildren);

  int iSize = pChildren.size();
  for (int i = 0; i < iSize; ++i)
  {
    RecursiveRemoveLocalizedElements(pChildren[i]);
  }

  gameswf::String sObjectType = a_hChar.getLocalVariable("className").toString();
  if (a_hChar.isValid() && sObjectType == "Text")
  {
    m_localizationHandles.EraseAll(a_hChar);
  }
#endif
}