|
@@ -28,11 +28,10 @@
|
|
|
#ifndef ROCKETCORESTYLESHEET_H
|
|
#ifndef ROCKETCORESTYLESHEET_H
|
|
|
#define ROCKETCORESTYLESHEET_H
|
|
#define ROCKETCORESTYLESHEET_H
|
|
|
|
|
|
|
|
-#include "Dictionary.h"
|
|
|
|
|
#include "ReferenceCountable.h"
|
|
#include "ReferenceCountable.h"
|
|
|
-#include <set>
|
|
|
|
|
#include "PropertyDictionary.h"
|
|
#include "PropertyDictionary.h"
|
|
|
-#include "Texture.h"
|
|
|
|
|
|
|
+#include "Spritesheet.h"
|
|
|
|
|
+#include <set>
|
|
|
|
|
|
|
|
namespace Rocket {
|
|
namespace Rocket {
|
|
|
namespace Core {
|
|
namespace Core {
|
|
@@ -41,6 +40,8 @@ class Element;
|
|
|
class ElementDefinition;
|
|
class ElementDefinition;
|
|
|
class StyleSheetNode;
|
|
class StyleSheetNode;
|
|
|
class Decorator;
|
|
class Decorator;
|
|
|
|
|
+class SpritesheetList;
|
|
|
|
|
+struct Sprite;
|
|
|
struct Spritesheet;
|
|
struct Spritesheet;
|
|
|
|
|
|
|
|
struct KeyframeBlock {
|
|
struct KeyframeBlock {
|
|
@@ -51,132 +52,13 @@ struct Keyframes {
|
|
|
std::vector<PropertyId> property_ids;
|
|
std::vector<PropertyId> property_ids;
|
|
|
std::vector<KeyframeBlock> blocks;
|
|
std::vector<KeyframeBlock> blocks;
|
|
|
};
|
|
};
|
|
|
-typedef UnorderedMap<String, Keyframes> KeyframesMap;
|
|
|
|
|
|
|
+using KeyframesMap = UnorderedMap<String, Keyframes>;
|
|
|
|
|
|
|
|
struct DecoratorSpecification {
|
|
struct DecoratorSpecification {
|
|
|
String decorator_type;
|
|
String decorator_type;
|
|
|
PropertyDictionary properties;
|
|
PropertyDictionary properties;
|
|
|
std::shared_ptr<Decorator> decorator;
|
|
std::shared_ptr<Decorator> decorator;
|
|
|
};
|
|
};
|
|
|
-
|
|
|
|
|
-struct Sprite {
|
|
|
|
|
- Rectangle rectangle;
|
|
|
|
|
- const Spritesheet* sprite_sheet;
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-struct Spritesheet {
|
|
|
|
|
- String name;
|
|
|
|
|
- String image_source;
|
|
|
|
|
- String definition_source;
|
|
|
|
|
- int definition_line_number;
|
|
|
|
|
- Texture texture;
|
|
|
|
|
- StringList sprite_names;
|
|
|
|
|
-
|
|
|
|
|
- Spritesheet(const String& name, const String& image_source, const String& definition_source, int definition_line_number, const Texture& texture)
|
|
|
|
|
- : name(name), image_source(image_source), definition_source(definition_source), definition_line_number(definition_line_number), texture(texture) {}
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
-using SpriteMap = UnorderedMap<String, Sprite>;
|
|
|
|
|
-using SpritesheetMap = UnorderedMap<String, std::shared_ptr<Spritesheet>>;
|
|
|
|
|
-using SpriteDefinitionList = std::vector<std::pair<String, Rectangle>>;
|
|
|
|
|
-
|
|
|
|
|
-class SpritesheetList {
|
|
|
|
|
-public:
|
|
|
|
|
-
|
|
|
|
|
- bool AddSpriteSheet(const String& name, const String& image_source, const String& definition_source, int definition_line_number, const SpriteDefinitionList& sprite_definitions)
|
|
|
|
|
- {
|
|
|
|
|
- // Load the texture
|
|
|
|
|
- Texture texture;
|
|
|
|
|
- if (!texture.Load(image_source, definition_source))
|
|
|
|
|
- {
|
|
|
|
|
- Log::Message(Log::LT_WARNING, "Could not load image '%s' specified in spritesheet '%s' at %s:%d", image_source.c_str(), name.c_str(), definition_source.c_str(), definition_line_number);
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- auto result = spritesheet_map.emplace(name, std::make_shared<Spritesheet>(name, image_source, definition_source, definition_line_number, texture));
|
|
|
|
|
- if (!result.second)
|
|
|
|
|
- {
|
|
|
|
|
- Log::Message(Log::LT_WARNING, "Spritesheet '%s' has the same name as another spritesheet, ignored. See %s:%d", name.c_str(), definition_source.c_str(), definition_line_number);
|
|
|
|
|
- return false;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- Spritesheet& spritesheet = *result.first->second;
|
|
|
|
|
- StringList& sprite_names = spritesheet.sprite_names;
|
|
|
|
|
-
|
|
|
|
|
- // Insert all the sprites with names not already defined in the global sprite list.
|
|
|
|
|
- int num_removed_sprite_names = 0;
|
|
|
|
|
- for (auto& sprite_definition : sprite_definitions)
|
|
|
|
|
- {
|
|
|
|
|
- const String& sprite_name = sprite_definition.first;
|
|
|
|
|
- const Rectangle& sprite_rectangle = sprite_definition.second;
|
|
|
|
|
- auto result = sprite_map.emplace(sprite_name, Sprite{ sprite_rectangle, &spritesheet });
|
|
|
|
|
- if (result.second)
|
|
|
|
|
- {
|
|
|
|
|
- sprite_names.push_back(sprite_name);
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- Log::Message(Log::LT_WARNING, "Sprite '%s' has the same name as an existing sprite, skipped. See %s:%d", sprite_name.c_str(), definition_source.c_str(), definition_line_number);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- return true;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Note: The pointer is invalidated whenever another sprite is added. Do not store it around.
|
|
|
|
|
- const Sprite* GetSprite(const String& name) const
|
|
|
|
|
- {
|
|
|
|
|
- auto it = sprite_map.find(name);
|
|
|
|
|
- if (it != sprite_map.end())
|
|
|
|
|
- return &it->second;
|
|
|
|
|
- return nullptr;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- // Merge other into this
|
|
|
|
|
- void Merge(const SpritesheetList& other)
|
|
|
|
|
- {
|
|
|
|
|
- for (auto& pair : other.spritesheet_map)
|
|
|
|
|
- {
|
|
|
|
|
- auto sheet_result = spritesheet_map.emplace(pair);
|
|
|
|
|
- bool sheet_inserted = sheet_result.second;
|
|
|
|
|
- if (sheet_inserted)
|
|
|
|
|
- {
|
|
|
|
|
- const String& sheet_name = sheet_result.first->first;
|
|
|
|
|
- Spritesheet& sheet = *sheet_result.first->second;
|
|
|
|
|
-
|
|
|
|
|
- // The spritesheet was succesfully added, now try to add each sprite to the global list.
|
|
|
|
|
- // Each sprite name must be unique, if we fail, we must also remove the sprite from the spritesheet.
|
|
|
|
|
- for (auto it = sheet.sprite_names.begin(); it != sheet.sprite_names.end(); )
|
|
|
|
|
- {
|
|
|
|
|
- const String& sprite_name = *it;
|
|
|
|
|
- auto it_sprite = other.sprite_map.find(sprite_name);
|
|
|
|
|
- bool success = false;
|
|
|
|
|
- if(it_sprite != other.sprite_map.end())
|
|
|
|
|
- {
|
|
|
|
|
- auto sprite_result = sprite_map.emplace(*it, it_sprite->second);
|
|
|
|
|
- success = sprite_result.second;
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- if (success)
|
|
|
|
|
- {
|
|
|
|
|
- ++it;
|
|
|
|
|
- }
|
|
|
|
|
- else
|
|
|
|
|
- {
|
|
|
|
|
- Log::Message(Log::LT_WARNING, "Duplicate sprite name '%s' found while merging style sheets, defined in %s:%d.", sprite_name.c_str(), sheet.definition_source.c_str(), sheet.definition_line_number);
|
|
|
|
|
- it = sheet.sprite_names.erase(it);
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
-private:
|
|
|
|
|
- SpritesheetMap spritesheet_map;
|
|
|
|
|
- SpriteMap sprite_map;
|
|
|
|
|
-};
|
|
|
|
|
-
|
|
|
|
|
using DecoratorSpecificationMap = UnorderedMap<String, DecoratorSpecification>;
|
|
using DecoratorSpecificationMap = UnorderedMap<String, DecoratorSpecification>;
|
|
|
|
|
|
|
|
|
|
|
|
@@ -211,10 +93,10 @@ public:
|
|
|
/// Returns the Decorator of the given name, or null if it does not exist.
|
|
/// Returns the Decorator of the given name, or null if it does not exist.
|
|
|
std::shared_ptr<Decorator> GetDecorator(const String& name) const;
|
|
std::shared_ptr<Decorator> GetDecorator(const String& name) const;
|
|
|
|
|
|
|
|
- /// Parses the decorator property from a string and returns a list of instanced decorators
|
|
|
|
|
|
|
+ /// Parses the decorator property from a string and returns a list of instanced decorators.
|
|
|
DecoratorList InstanceDecoratorsFromString(const String& decorator_string_value, const String& source_file, int source_line_number) const;
|
|
DecoratorList InstanceDecoratorsFromString(const String& decorator_string_value, const String& source_file, int source_line_number) const;
|
|
|
|
|
|
|
|
- /// Get sprite located in any spritesheet within this stylesheet
|
|
|
|
|
|
|
+ /// Get sprite located in any spritesheet within this stylesheet.
|
|
|
const Sprite* GetSprite(const String& name) const;
|
|
const Sprite* GetSprite(const String& name) const;
|
|
|
|
|
|
|
|
/// Returns the compiled element definition for a given element hierarchy. A reference count will be added for the
|
|
/// Returns the compiled element definition for a given element hierarchy. A reference count will be added for the
|
|
@@ -242,6 +124,7 @@ private:
|
|
|
// Name of every @decorator mapped to their specification
|
|
// Name of every @decorator mapped to their specification
|
|
|
DecoratorSpecificationMap decorator_map;
|
|
DecoratorSpecificationMap decorator_map;
|
|
|
|
|
|
|
|
|
|
+ // Name of every @spritesheet and underlying sprites mapped to their values
|
|
|
SpritesheetList spritesheet_list;
|
|
SpritesheetList spritesheet_list;
|
|
|
|
|
|
|
|
// Map of only nodes with actual style information.
|
|
// Map of only nodes with actual style information.
|