Esempio n. 1
0
void Map::LoadFromJSON( const rapidjson::Value& object )
{
    // Destroy all Units.
    // TODO: Don't do this.
    DestroyAllUnits();

    // Get the Units tag.
    const rapidjson::Value& unitsArray = object[ "units" ];
    assertion( unitsArray.IsArray(), "Could not load game state from JSON because no \"units\" list was found!" );

    Scenario* scenario = GetScenario();

    for( auto it = unitsArray.Begin(); it != unitsArray.End(); ++it )
    {
        const rapidjson::Value& object = ( *it );
        assertion( object.IsObject(), "Could not load Unit from JSON because the JSON provided was not an object!" );

        // Get all properties.
        HashString unitTypeName = GetJSONStringValue( object, "unitType", "" );
        int ownerIndex = GetJSONIntValue( object, "owner", -1 );
        int tileX = GetJSONIntValue( object, "x", -1 );
        int tileY = GetJSONIntValue( object, "y", -1 );

        assertion( GetTile( tileX, tileY ).IsValid(), "Loaded invalid tile position (%d,%d) from JSON!", tileX, tileY );

        // Get references.
        UnitType* unitType = scenario->UnitTypes.FindByName( unitTypeName );
        assertion( unitType, "Could not load invalid UnitType (\"%s\") from JSON!", unitTypeName.GetCString() );

        Faction* faction = GetFactionByIndex( ownerIndex );
        assertion( faction, "Could not load Unit with invalid Faction index (%d) from JSON!", ownerIndex );

        // Spawn the Unit.
        Unit* unit = CreateUnit( unitType, faction, tileX, tileY );

        // Load each Unit from the array.
        unit->LoadFromJSON( *it );
    }
}
Esempio n. 2
0
//---------------------------------------
Widget::VerticalAlignment Widget::GetVerticalAlignmentByName( const HashString& name )
{
	VerticalAlignment result = VERTICAL_ALIGN_TOP;

	if( name == VERTICAL_ALIGN_TOP_NAME )
	{
		result = VERTICAL_ALIGN_TOP;
	}
	else if( name == VERTICAL_ALIGN_CENTER_NAME )
	{
		result = VERTICAL_ALIGN_CENTER;
	}
	else if( name == VERTICAL_ALIGN_BOTTOM_NAME )
	{
		result = VERTICAL_ALIGN_BOTTOM;
	}
	else
	{
		WarnFail( "VerticalAlignment setting \"%s\" is invalid!", name.GetCString() );
	}

	return result;
}
Esempio n. 3
0
//---------------------------------------
Widget::HorizontalAlignment Widget::GetHorizontalAlignmentByName( const HashString& name )
{
	HorizontalAlignment result = HORIZONTAL_ALIGN_LEFT;

	if( name == HORIZONTAL_ALIGN_LEFT_NAME )
	{
		result = HORIZONTAL_ALIGN_LEFT;
	}
	else if( name == HORIZONTAL_ALIGN_CENTER_NAME )
	{
		result = HORIZONTAL_ALIGN_CENTER;
	}
	else if( name == HORIZONTAL_ALIGN_RIGHT_NAME )
	{
		result = HORIZONTAL_ALIGN_RIGHT;
	}
	else
	{
		WarnFail( "HorizontalAlignment setting \"%s\" is invalid!", name.GetCString() );
	}

	return result;
}
Esempio n. 4
0
//---------------------------------------
void Widget::RemoveChildByName( const HashString& name )
{
	auto it = mChildren.find( name );

	if( it != mChildren.end() )
	{
		Widget* child = it->second;

		// Remove the child with the specified name.
		mChildren.erase( it );

		// Resort all children by their draw order.
		InvalidateDrawOrder();
	}
	else
	{
		// If the child wasn't found, post a warning.
		WarnFail( "Could not remove child \"%s\" from Widget \"%s\" because no child with that name was found!", name.GetCString(), mName.GetCString() );
	}
}
Esempio n. 5
0
//---------------------------------------
void Widget::AddChild( Widget* child )
{
	assertion( child, "Cannot add null child to Widget \"%s\"!", GetName().GetCString() );
	assertion( child->GetManager() == mManager, "Cannot add child \"%s\" to Widget \"%s\" because it was created by a different WidgetManager!", child->mName.GetCString(), mName.GetCString() );

	HashString parentName = ( child->mParent ? child->mParent->mName : "" );

	assertion( child->mParent == nullptr, "Cannot add child \"%s\" to Widget \"%s\" because it is already a child of Widget \"%s\"!", child->mName.GetCString(), mName.GetCString(), parentName.GetCString() );
	assertion( !HasChildWithName( child->mName ), "Could not add child \"%s\" to Widget \"%s\" because a child of the same name already exists!", child->mName.GetCString(), mName.GetCString() );

	//DebugPrintf( "Adding child \"%s\" to Widget \"%s\"...", child->GetName().GetCString(), GetName().GetCString() );

	// If a child with the specified name does not already exist, add the new child.
	mChildren[ child->mName ] = child;
	child->mParent = this;

	// Resort all children by their draw order.
	InvalidateDrawOrder();
}