Skip to content

Commit

Permalink
Test decorater tile name as sprite first, then filename.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikke89 committed Aug 14, 2019
1 parent 2e0c0ca commit a5a8480
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 32 deletions.
43 changes: 13 additions & 30 deletions Source/Core/DecoratorTiledInstancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ void DecoratorTiledInstancer::RegisterTileProperty(const String& name, bool regi
ids.src = RegisterProperty(CreateString(32, "%s-src", name.c_str()), "").AddParser("string").GetId();
ids.x = RegisterProperty(CreateString(32, "%s-x", name.c_str()), "0px").AddParser("number_length_percent").GetId();
ids.y = RegisterProperty(CreateString(32, "%s-y", name.c_str()), "0px").AddParser("number_length_percent").GetId();
ids.width = RegisterProperty(CreateString(32, "%s-width", name.c_str()), "0px").AddParser("number_length_percent").GetId();
ids.height = RegisterProperty(CreateString(32, "%s-height", name.c_str()), "0px").AddParser("number_length_percent").GetId();
ids.width = RegisterProperty(CreateString(32, "%s-width", name.c_str()), "1.0").AddParser("number_length_percent").GetId();
ids.height = RegisterProperty(CreateString(32, "%s-height", name.c_str()), "1.0").AddParser("number_length_percent").GetId();
RegisterShorthand(CreateString(32, "%s-pos", name.c_str()), CreateString(64, "%s-x, %s-y", name.c_str(), name.c_str()), ShorthandType::FallThrough);
RegisterShorthand(CreateString(32, "%s-size", name.c_str()), CreateString(64, "%s-width, %s-height", name.c_str(), name.c_str()), ShorthandType::FallThrough);

Expand Down Expand Up @@ -107,47 +107,30 @@ bool DecoratorTiledInstancer::GetTileProperties(DecoratorTiled::Tile* tiles, Tex
if (texture_name.empty() || texture_name == auto_str)
continue;

// A tile is always either a sprite or a texture with position and dimensions specified.
bool src_is_sprite = false;

// We are required to set default values before instancing the tile, thus, all properties should always be dereferencable.
// If the debugger captures a zero-dereference, check that all properties for every tile is set and default values are set just before instancing.
const Property& width_property = *properties.GetProperty(ids.width);
float width = width_property.Get<float>();
if (width == 0.0f)
{
// A sprite always has its own dimensions, thus, we let zero width/height define that it is a sprite.
src_is_sprite = true;
}

DecoratorTiled::Tile& tile = tiles[i];
Texture& texture = textures[i];

if (src_is_sprite)
// A tile is always either a sprite or a texture with position and dimensions specified.
if (const Sprite * sprite = interface.GetSprite(texture_name))
{
if (const Sprite * sprite = interface.GetSprite(texture_name))
{
tile.position.x = sprite->rectangle.x;
tile.position.y = sprite->rectangle.y;
tile.size.x = sprite->rectangle.width;
tile.size.y = sprite->rectangle.height;
tile.position.x = sprite->rectangle.x;
tile.position.y = sprite->rectangle.y;
tile.size.x = sprite->rectangle.width;
tile.size.y = sprite->rectangle.height;

tile.position_absolute[0] = tile.position_absolute[1] = true;
tile.size_absolute[0] = tile.size_absolute[1] = true;
tile.position_absolute[0] = tile.position_absolute[1] = true;
tile.size_absolute[0] = tile.size_absolute[1] = true;

texture = sprite->sprite_sheet->texture;
}
else
{
Log::Message(Log::LT_WARNING, "The sprite '%s' given in decorator could not be found, declared at %s:%d", texture_name.c_str(), src_property->source.c_str(), src_property->source_line_number);
return false;
}
texture = sprite->sprite_sheet->texture;
}
else
{
LoadTexCoord(*properties.GetProperty(ids.x), tile.position.x, tile.position_absolute[0]);
LoadTexCoord(*properties.GetProperty(ids.y), tile.position.y, tile.position_absolute[1]);
LoadTexCoord(width_property, tile.size.x, tile.size_absolute[0]);
LoadTexCoord(*properties.GetProperty(ids.width), tile.size.x, tile.size_absolute[0]);
LoadTexCoord(*properties.GetProperty(ids.height), tile.size.y, tile.size_absolute[1]);

// Since the common use case is to specify the same texture for all tiles, we
Expand All @@ -163,7 +146,7 @@ bool DecoratorTiledInstancer::GetTileProperties(DecoratorTiled::Tile* tiles, Tex
}
else
{
Log::Message(Log::LT_WARNING, "Could not load texture '%s' given in decorator at %s:%d", texture_name.c_str(), src_property->source.c_str(), src_property->source_line_number);
Log::Message(Log::LT_WARNING, "Texture name '%s' is neither a valid sprite name nor a texture file. Specified in decorator at %s:%d.", texture_name.c_str(), src_property->source.c_str(), src_property->source_line_number);
return false;
}
}
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ A sprite sheet can be declared in RCSS as in the following example.
icon-help: 128px 152px 51px 39px;
}
```
The first property `src` provides the filename of the image for the sprite sheet. Every other property specifies a sprite as `<name>: <rectangle>`. A sprite's name applies globally to the style sheet, and must be unique. A rectangle is declared as `x y width height`, each of which must be in `px` units. Here, `x` and `y` refers to the position in the image with the origin placed at the top-left corner, and `width` and `height` extends the rectangle right and down.
The first property `src` provides the filename of the image for the sprite sheet. Every other property specifies a sprite as `<name>: <rectangle>`. A sprite's name applies globally to all included style sheets in a given document, and must be unique. A rectangle is declared as `x y width height`, each of which must be in `px` units. Here, `x` and `y` refers to the position in the image with the origin placed at the top-left corner, and `width` and `height` extends the rectangle right and down.

The sprite name can be used in decorators, such as:
```CSS
Expand Down Expand Up @@ -91,7 +91,7 @@ decorator: tiled-box(
decorator: image( invader.tga 5px 10px 30px 30px );
```

The `decorator` property follows the normal cascading rules, is non-inherited, and has the default value `none` which specifies no decorator on the element. Unlike in libRocket, decorators can now be set on the element's style, although we recommend declaring them in style sheets for performance reasons.
The `decorator` property follows the normal cascading rules, is non-inherited, and has the default value `none` which specifies no decorator on the element. The decorator looks for a sprite with the same name first. If none exists, then it treats it as a file name for an image. Decorators can now be set on the element's style, although we recommend declaring them in style sheets for performance reasons.

Furthermore, multiple decorators can be specified on any element by a comma-separated list of decorators.
```CSS
Expand Down

0 comments on commit a5a8480

Please sign in to comment.