#ifndef TOS_UI_THEME_H #define TOS_UI_THEME_H #include "../stdlib/Types.h" #include "../memory/RingMemory.h" #include "../utils/EndianUtils.h" #include "../utils/StringUtils.h" #include "../stdlib/HashMap.h" #include "../font/Font.h" #include "../system/FileUtils.cpp" #include "attribute/UIAttribute.h" #include "UIElementType.h" #define UI_THEME_VERSION 1 // @question Currently there is some data duplication in here and in the UIElement. // Not sure if this is how we want this to be or if we want to change this in the future // Modified for every scene // WARNING: Make sure the order of this struct and UITheme is the same for the first elements // This allows us to cast between both struct UIThemeStyle { // A theme may have N named styles // The hashmap contains the offset where the respective style can be found // @performance Switch to perfect hash map HashMap hash_map; byte* data; // It feels weird that this is here, especially considering we could have multiple fonts Font* font; }; inline UIAttributeGroup* theme_style_group(UIThemeStyle* theme, const char* group_name) { HashEntryInt64* entry = (HashEntryInt64 *) hashmap_get_entry(&theme->hash_map, group_name); if (!entry) { ASSERT_SIMPLE(false); return NULL; } return (UIAttributeGroup *) entry->value; } inline UIAttributeGroup* theme_style_group(UIThemeStyle* theme, const char* group_name, int32 group_id) { HashEntryInt64* entry = (HashEntryInt64 *) hashmap_get_entry(&theme->hash_map, group_name, group_id); if (!entry) { ASSERT_SIMPLE(false); return NULL; } return (UIAttributeGroup *) entry->value; } static inline int compare_by_attribute_id(const void* a, const void* b) { UIAttribute* attr_a = (UIAttribute *) a; UIAttribute* attr_b = (UIAttribute *) b; return attr_a->attribute_id - attr_b->attribute_id; } // File layout - text // version // #group_name // attributes ... // attributes ... // attributes ... // #group_name // attributes ... // attributes ... // attributes ... // WARNING: theme needs to have memory already reserved and assigned to data void theme_from_file_txt( UIThemeStyle* theme, const char* path, RingMemory* ring ) { FileBody file; file_read(path, &file, ring); ASSERT_SIMPLE(file.size); char* pos = (char *) file.content; // move past the version string pos += 8; // Use version for different handling int32 version = strtol(pos, &pos, 10); ++pos; // We have to find how many groups are defined in the theme file. // Therefore we have to do an initial iteration int32 temp_group_count = 0; while (*pos != '\0') { // Skip all white spaces str_skip_empty(&pos); // Is group name if (*pos == '#' || *pos == '.') { ++temp_group_count; } // Go to the next line str_move_past(&pos, '\n'); } // @performance This is probably horrible since we are not using a perfect hashing function (1 hash -> 1 index) // I wouldn't be surprised if we have a 50% hash overlap (2 hashes -> 1 index) hashmap_create(&theme->hash_map, temp_group_count, sizeof(HashEntryInt64), theme->data); int64 data_offset = hashmap_size(&theme->hash_map); UIAttributeGroup* temp_group = NULL; pos = (char *) file.content; // move past version string str_move_past(&pos, '\n'); bool block_open = false; char block_name[28]; char attribute_name[32]; bool last_token_newline = false; while (*pos != '\0') { str_skip_whitespace(&pos); if (*pos == '\n') { ++pos; // 2 new lines => closing block if (last_token_newline) { block_open = false; last_token_newline = false; } else { last_token_newline = true; } continue; } last_token_newline = false; if (!block_open) { str_copy_move_until(&pos, block_name, " \r\n"); // All blocks need to start with #. In the past this wasn't the case and may not be in the future. This is why we keep this if here. if (*block_name == '#' || *block_name == '.') { // Named style block_open = true; if (temp_group) { // Before we insert a new group we have to sort the attributes // since this makes searching them later on more efficient. qsort(temp_group->attributes, temp_group->attribute_size, sizeof(UIAttribute), compare_by_attribute_id); } // Insert new group hashmap_insert(&theme->hash_map, block_name, data_offset); temp_group = (UIAttributeGroup *) (theme->data + data_offset); temp_group->attribute_size = 0; temp_group->attributes = (UIAttribute *) (theme->data + data_offset + sizeof(UIAttributeGroup)); data_offset += sizeof(UIAttributeGroup); } continue; } str_copy_move_until(&pos, attribute_name, " :\n"); // Skip any white spaces or other delimeter str_skip_list(&pos, " \t:", sizeof(" \t:") - 1); ASSERT_SIMPLE((*pos != '\0' && *pos != '\n')); // Parse attribute value UIAttribute attribute = {}; ui_attribute_parse_value(&attribute, attribute_name, pos); // Again, currently this if check is redundant but it wasn't in the past and we may need it again in the future. if (block_name[0] == '#' || block_name[0] == '.') { // Named block memcpy( temp_group->attributes + temp_group->attribute_size, &attribute, sizeof(attribute) ); data_offset += sizeof(attribute); ++temp_group->attribute_size; } str_move_to(&pos, '\n'); } // We still need to sort the last group qsort(temp_group->attributes, temp_group->attribute_size, sizeof(UIAttribute), compare_by_attribute_id); } static inline void ui_theme_parse_group(HashEntryInt64* entry, byte* data, const byte** pos) { // @performance Are we sure the data is nicely aligned? // Probably depends on the from_txt function and the start of theme->data // Change offset to pointer entry->value = (uintptr_t) data + entry->value; UIAttributeGroup* group = (UIAttributeGroup *) entry->value; group->attribute_size = SWAP_ENDIAN_LITTLE(*((int32 *) *pos)); *pos += sizeof(group->attribute_size); for (uint32 j = 0; j < group->attribute_size; ++j) { group->attributes[j].attribute_id = (UIAttributeType) SWAP_ENDIAN_LITTLE(*((uint16 *) *pos)); *pos += sizeof(group->attributes[j].attribute_id); group->attributes[j].datatype = *((UIAttributeDataType *) *pos); *pos += sizeof(group->attributes[j].datatype); if (group->attributes[j].datatype == UI_ATTRIBUTE_DATA_TYPE_INT) { group->attributes[j].value_int = SWAP_ENDIAN_LITTLE(*((int32 *) *pos)); *pos += sizeof(group->attributes[j].value_int); } else if (group->attributes[j].datatype == UI_ATTRIBUTE_DATA_TYPE_INT) { group->attributes[j].value_float = SWAP_ENDIAN_LITTLE(*((f32 *) *pos)); *pos += sizeof(group->attributes[j].value_float); } else if (group->attributes[j].datatype == UI_ATTRIBUTE_DATA_TYPE_STR) { memcpy(group->attributes[j].value_str, *pos, sizeof(group->attributes[j].value_str)); *pos += sizeof(group->attributes[j].value_str); } else if (group->attributes[j].datatype == UI_ATTRIBUTE_DATA_TYPE_V4_F32) { group->attributes[j].value_v4_f32.x = SWAP_ENDIAN_LITTLE(*((f32 *) *pos)); *pos += sizeof(group->attributes[j].value_v4_f32.x); group->attributes[j].value_v4_f32.y = SWAP_ENDIAN_LITTLE(*((f32 *) *pos)); *pos += sizeof(group->attributes[j].value_v4_f32.y); group->attributes[j].value_v4_f32.z = SWAP_ENDIAN_LITTLE(*((f32 *) *pos)); *pos += sizeof(group->attributes[j].value_v4_f32.z); group->attributes[j].value_v4_f32.w = SWAP_ENDIAN_LITTLE(*((f32 *) *pos)); *pos += sizeof(group->attributes[j].value_v4_f32.w); } } } // Memory layout (data) - This is not the layout of the file itself, just how we represent it in memory // Hashmap // UIAttributeGroup - This is where the pointers point to (or what the offset represents) // size // Attributes ... // Attributes ... // Attributes ... // UIAttributeGroup // size // Attributes ... // Attributes ... // Attributes ... // The size of theme->data should be the file size. // Yes, this means we have a little too much data but not by a lot int32 theme_from_data( const byte* data, UIThemeStyle* theme ) { const byte* pos = data; const byte* max_pos = data; int32 version = *((int32 *) pos); pos += sizeof(version); // Prepare hashmap (incl. reserve memory) by initializing it the same way we originally did // Of course we still need to populate the data using hashmap_load() // The value is a int64 (because this is the value of the chunk buffer size but the hashmap only allows int32) hashmap_create(&theme->hash_map, (int32) SWAP_ENDIAN_LITTLE(*((uint64 *) pos)), sizeof(HashEntryInt64), theme->data); const byte* start = theme->hash_map.buf.memory; pos += hashmap_load(&theme->hash_map, pos); // theme data // Layout: first load the size of the group, then load the individual attributes for (int32 i = 0; i < theme->hash_map.buf.count; ++i) { if (!theme->hash_map.table[i]) { continue; } HashEntryInt64* entry = (HashEntryInt64 *) theme->hash_map.table[i]; // This way we now could access the data directly without the silly theme->data + entry->value calc. pos = start + entry->value; ui_theme_parse_group(entry, theme->data, &pos); if (pos > max_pos) { max_pos = pos; } // load all the next elements while (entry->next) { pos = start + entry->value; ui_theme_parse_group(entry, theme->data, &pos); if (pos > max_pos) { max_pos = pos; } entry = entry->next; } } return (int32) (max_pos - data); } // Calculates the maximum theme size // Not every group has all the attributes (most likely only a small subset) // However, an accurate calculation is probably too slow and not needed most of the time inline int64 theme_data_size(const UIThemeStyle* theme) { return hashmap_size(&theme->hash_map) + theme->hash_map.buf.count * UI_ATTRIBUTE_TYPE_SIZE * sizeof(UIAttribute); } // @todo Why do even need **pos, shouldn't it just be *pos static inline void ui_theme_serialize_group(HashEntryInt64* entry, byte* data, byte** pos, const byte* start) { // @performance Are we sure the data is nicely aligned? // Probably depends on the from_txt function and the start of theme->data UIAttributeGroup* group = (UIAttributeGroup *) (data + entry->value); *((int32 *) *pos) = SWAP_ENDIAN_LITTLE(group->attribute_size); *pos += sizeof(group->attribute_size); for (uint32 j = 0; j < group->attribute_size; ++j) { *((uint16 *) *pos) = SWAP_ENDIAN_LITTLE(group->attributes[j].attribute_id); *pos += sizeof(group->attributes[j].attribute_id); *((byte *) *pos) = group->attributes[j].datatype; *pos += sizeof(group->attributes[j].datatype); if (group->attributes[j].datatype == UI_ATTRIBUTE_DATA_TYPE_INT) { *((int32 *) *pos) = SWAP_ENDIAN_LITTLE(group->attributes[j].value_int); *pos += sizeof(group->attributes[j].value_int); } else if (group->attributes[j].datatype == UI_ATTRIBUTE_DATA_TYPE_INT) { *((f32 *) *pos) = SWAP_ENDIAN_LITTLE(group->attributes[j].value_float); *pos += sizeof(group->attributes[j].value_float); } else if (group->attributes[j].datatype == UI_ATTRIBUTE_DATA_TYPE_STR) { memcpy(*pos, group->attributes[j].value_str, sizeof(group->attributes[j].value_str)); *pos += sizeof(group->attributes[j].value_str); } else if (group->attributes[j].datatype == UI_ATTRIBUTE_DATA_TYPE_V4_F32) { *((f32 *) *pos) = SWAP_ENDIAN_LITTLE(group->attributes[j].value_v4_f32.x); *pos += sizeof(group->attributes[j].value_v4_f32.x); *((f32 *) *pos) = SWAP_ENDIAN_LITTLE(group->attributes[j].value_v4_f32.y); *pos += sizeof(group->attributes[j].value_v4_f32.y); *((f32 *) *pos) = SWAP_ENDIAN_LITTLE(group->attributes[j].value_v4_f32.z); *pos += sizeof(group->attributes[j].value_v4_f32.z); *((f32 *) *pos) = SWAP_ENDIAN_LITTLE(group->attributes[j].value_v4_f32.w); *pos += sizeof(group->attributes[j].value_v4_f32.w); } } } // File layout - binary // version // hashmap size // Hashmap (includes offsets to the individual groups) // #group_name (this line doesn't really exist in file, it's more like the pointer offset in the hashmap) // size // attributes ... // attributes ... // attributes ... // #group_name // size // attributes ... // attributes ... // attributes ... int32 theme_to_data( const UIThemeStyle* theme, byte* data ) { byte* pos = data; byte* max_pos = data; // version *((int32 *) pos) = SWAP_ENDIAN_LITTLE(UI_THEME_VERSION); pos += sizeof(int32); // hashmap byte* start = pos; pos += hashmap_dump(&theme->hash_map, pos); // theme data // Layout: first save the size of the group, then save the individual attributes for (uint32 i = 0; i < theme->hash_map.buf.count; ++i) { if (!theme->hash_map.table[i]) { continue; } HashEntryInt64* entry = (HashEntryInt64 *) theme->hash_map.table[i]; pos = start + entry->value; ui_theme_serialize_group(entry, theme->data, &pos, start); if (pos > max_pos) { max_pos = pos; } // save all the next elements while (entry->next) { pos = start + entry->value; ui_theme_serialize_group(entry, theme->data, &pos, start); if (pos > max_pos) { max_pos = pos; } entry = entry->next; } } return (int32) (max_pos - data); } #endif