Exemplo n.º 1
0
void PushButton::setupUI(const VariantMap &ui)
{
  Widget::setupUI( ui );

  __D_REF(_d,PushButton)

  Variant tmp;
  tmp = ui.get( "bgtype" );
  if( tmp.isValid() )
  {
    BackgroundStyleHelper helper;
    setBackgroundStyle( helper.findType( tmp.toString() ) );
  }

  setIsPushButton( (bool)ui.get( "pushbutton" ) );
  _d.text.offset = ui.get( "textOffset" );
  _d.text.offset = ui.get( "text.offset", _d.text.offset );
  setEnabled( (bool)ui.get( "enabled", true ) );

  Variant vFont = ui.get( "font" );
  if( vFont.isValid() ) { setFont( Font::create( vFont.toString() ) ); }

  VariantList vlist = ui.get( "normal" ).toList();
  if( !vlist.empty() ) setPicture( vlist.get( 0 ).toString(), vlist.get( 1 ).toInt(), stNormal );

  vlist = ui.get( "hovered" ).toList();
  if( !vlist.empty() ) setPicture( vlist.get( 0 ).toString(), vlist.get( 1 ).toInt(), stHovered  );

  vlist = ui.get( "pressed" ).toList();
  if( !vlist.empty() ) setPicture( vlist.get( 0 ).toString(), vlist.get( 1 ).toInt(), stPressed );

  vlist = ui.get( "disabled" ).toList();
  if( !vlist.empty() ) setPicture( vlist.get( 0 ).toString(), vlist.get( 1 ).toInt(), stDisabled );
}
Exemplo n.º 2
0
void Stock::load( const VariantList& stream )
{
  if( stream.empty())
    return;

  _type = stream.get( 0 ).toInt();
  if( _type >= good::goodCount.toInt() )
  {
    Logger::warning( "GoodStock: wrong type of good %d", _type );
    _type = good::none.toInt();
  }

  _capacity = (int)stream.get( 1 );
  _qty = math::clamp( (int)stream.get( 2 ), 0, _capacity );
}
Exemplo n.º 3
0
std::string Json::serialize(const Variant &data, bool &success, const std::string& tab)
{
  std::string str;
  success = true;

  if( data.isNull() ) // invalid or null?
  {
    return "null";
  }

  switch( data.type() )
  {
    case Variant::List:
    case Variant::NStringArray: // variant is a list?
    {
      const VariantList rlist = data.toList();

      if( rlist.empty() )
      {
        str = "[]";
      }
      else
      {
        StringArray values;
        std::string serializedValue;
        serializedValue.reserve( 512 );
        for( auto& item : rlist )
        {
          serializedValue = serialize( item, "" );
          if( serializedValue.empty() )
          {
              success = false;
              break;
          }

          values.push_back( serializedValue );
        }

        str = "[ " + join( values, ", " ) + " ]";
      }
    }
    break;

    case Variant::Map: // variant is a map?
    {
      VariantMap vmap = data.toMap();

      if( vmap.empty() )
      {
        str = "{}";
      }
      else
      {
        str = "{ \n";
        StringArray pairs;
        std::string serializedValue;
        serializedValue.reserve( 512 );
        for( auto& item : vmap )
        {
          serializedValue = serialize( item.second, tab + "  ");
          if( serializedValue.empty())
          {
            pairs.push_back( tab + sanitizeString( item.first ) + std::string( " : \"nonSerializableValue\"" ) );
            continue;
          }
          pairs.push_back( tab + sanitizeString( item.first ) + " : " + serializedValue );
        }
        str += join(pairs, ",\n");
        std::string rtab( tab );
        rtab.resize( std::max<int>( 0, tab.size() - 2 ) );
        str += std::string( "\n" ) + rtab + "}";
      }
    }
    break;

    case Variant::String:
    case Variant::NByteArray: // a string or a byte array?
    {
      str = sanitizeString( data.toString() );
    }
    break;

    case Variant::Double:
    case Variant::Float: // double?
    {
      // TODO: cheap hack - almost locale independent double formatting
      str = utils::format( 0xff, "\"%f\"", data.toDouble() );
      str = utils::replace(str, ",", ".");
      if( str.find(".") == std::string::npos && str.find("e") == std::string::npos )
      {
         str += ".0";
      }
    }
    break;

    case Variant::NTilePos:
    {
      const TilePos& pos = data.toTilePos();
      str = utils::format( 0xff, "[ %d, %d ]", pos.i(), pos.j() );
    }
    break;

    case Variant::NSize:
    {
      const Size& size = data.toSize();
      str = utils::format( 0xff, "[ %d, %d ]", size.width(), size.height() );
    }
    break;

    case Variant::NPoint:
    {
      const Point& pos = data.toPoint();
      str = utils::format( 0xff, "[ %d, %d ]", pos.x(), pos.y() );
    }
    break;

    case Variant::NPointF:
    {
      PointF pos = data.toPointF();
      // TODO: cheap hack - almost locale independent double formatting
      std::string posX = utils::replace(utils::format( 0xff, "%f", pos.x()), ",", ".");
      std::string posY = utils::replace(utils::format( 0xff, "%f", pos.y()), ",", ".");
      str = fmt::format( "[ {0}, {1} ]", posX, posY );
    }
    break;

    case Variant::Bool: // boolean value?
    {
      str = data.toBool() ? "true" : "false";
    }
    break;

    case Variant::ULongLong: // large unsigned number?
    {
      str = utils::format( 0xff, "%u", data.toULongLong() );
    }
    break;

    case Variant::Int: // simple int?
    {
      str = utils::format( 0xff, "%d", data.toInt() );
    }
    break;

    case Variant::UInt:
    {
      str = utils::format( 0xff, "%d", data.toInt() );
    }
    break;

    default:
      if ( data.canConvert( Variant::LongLong ) ) // any signed number?
      {
        str = utils::i2str( data.toLongLong() );
      }
      else if (data.canConvert( Variant::Long ))
      {
        str = utils::i2str( data.toLongLong() );
      }
      else if (data.canConvert( Variant::String ) ) // can value be converted to string?
      {
        // this will catch Date, DateTime, Url, ...
        str = sanitizeString( data.toString() );
      }
      else
      {
        success = false;
      }
    break;
  }

  return success ? str : std::string();
}