mirror of
				https://github.com/ciphervance/supercell-wx.git
				synced 2025-10-31 16:10:06 +00:00 
			
		
		
		
	Register font subcategories with text settings
This commit is contained in:
		
							parent
							
								
									ad1646d725
								
							
						
					
					
						commit
						d82fb666f9
					
				
					 4 changed files with 95 additions and 5 deletions
				
			
		|  | @ -2,6 +2,8 @@ | ||||||
| #include <scwx/qt/util/json.hpp> | #include <scwx/qt/util/json.hpp> | ||||||
| #include <scwx/util/logger.hpp> | #include <scwx/util/logger.hpp> | ||||||
| 
 | 
 | ||||||
|  | #include <algorithm> | ||||||
|  | 
 | ||||||
| namespace scwx | namespace scwx | ||||||
| { | { | ||||||
| namespace qt | namespace qt | ||||||
|  | @ -21,6 +23,8 @@ public: | ||||||
| 
 | 
 | ||||||
|    const std::string name_; |    const std::string name_; | ||||||
| 
 | 
 | ||||||
|  |    std::vector<std::pair<std::string, std::vector<SettingsCategory*>>> | ||||||
|  |                                       subcategoryArrays_; | ||||||
|    std::vector<SettingsVariableBase*> variables_; |    std::vector<SettingsVariableBase*> variables_; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
|  | @ -41,6 +45,16 @@ std::string SettingsCategory::name() const | ||||||
| 
 | 
 | ||||||
| void SettingsCategory::SetDefaults() | void SettingsCategory::SetDefaults() | ||||||
| { | { | ||||||
|  |    // Set subcategory array defaults
 | ||||||
|  |    for (auto& subcategoryArray : p->subcategoryArrays_) | ||||||
|  |    { | ||||||
|  |       for (auto& subcategory : subcategoryArray.second) | ||||||
|  |       { | ||||||
|  |          subcategory->SetDefaults(); | ||||||
|  |       } | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|  |    // Set variable defaults
 | ||||||
|    for (auto& variable : p->variables_) |    for (auto& variable : p->variables_) | ||||||
|    { |    { | ||||||
|       variable->SetValueToDefault(); |       variable->SetValueToDefault(); | ||||||
|  | @ -57,6 +71,47 @@ bool SettingsCategory::ReadJson(const boost::json::object& json) | ||||||
|    { |    { | ||||||
|       const boost::json::object& object = value->as_object(); |       const boost::json::object& object = value->as_object(); | ||||||
| 
 | 
 | ||||||
|  |       // Read subcategory arrays
 | ||||||
|  |       for (auto& subcategoryArray : p->subcategoryArrays_) | ||||||
|  |       { | ||||||
|  |          const boost::json::value* arrayValue = | ||||||
|  |             object.if_contains(subcategoryArray.first); | ||||||
|  | 
 | ||||||
|  |          if (arrayValue != nullptr && arrayValue->is_object()) | ||||||
|  |          { | ||||||
|  |             const boost::json::object& arrayObject = arrayValue->as_object(); | ||||||
|  | 
 | ||||||
|  |             for (auto& subcategory : subcategoryArray.second) | ||||||
|  |             { | ||||||
|  |                validated &= subcategory->ReadJson(arrayObject); | ||||||
|  |             } | ||||||
|  |          } | ||||||
|  |          else | ||||||
|  |          { | ||||||
|  |             if (arrayValue == nullptr) | ||||||
|  |             { | ||||||
|  |                logger_->debug( | ||||||
|  |                   "Subcategory array key {} is not present, resetting to " | ||||||
|  |                   "defaults", | ||||||
|  |                   subcategoryArray.first); | ||||||
|  |             } | ||||||
|  |             else if (!arrayValue->is_object()) | ||||||
|  |             { | ||||||
|  |                logger_->warn( | ||||||
|  |                   "Invalid json for subcategory array key {}, resetting to " | ||||||
|  |                   "defaults", | ||||||
|  |                   p->name_); | ||||||
|  |             } | ||||||
|  | 
 | ||||||
|  |             for (auto& subcategory : subcategoryArray.second) | ||||||
|  |             { | ||||||
|  |                subcategory->SetDefaults(); | ||||||
|  |             } | ||||||
|  |             validated = false; | ||||||
|  |          } | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       // Read variables
 | ||||||
|       for (auto& variable : p->variables_) |       for (auto& variable : p->variables_) | ||||||
|       { |       { | ||||||
|          validated &= variable->ReadValue(object); |          validated &= variable->ReadValue(object); | ||||||
|  | @ -66,7 +121,7 @@ bool SettingsCategory::ReadJson(const boost::json::object& json) | ||||||
|    { |    { | ||||||
|       if (value == nullptr) |       if (value == nullptr) | ||||||
|       { |       { | ||||||
|          logger_->warn("Key {} is not present, resetting to defaults", |          logger_->debug("Key {} is not present, resetting to defaults", | ||||||
|                         p->name_); |                         p->name_); | ||||||
|       } |       } | ||||||
|       else if (!value->is_object()) |       else if (!value->is_object()) | ||||||
|  | @ -86,6 +141,20 @@ void SettingsCategory::WriteJson(boost::json::object& json) const | ||||||
| { | { | ||||||
|    boost::json::object object; |    boost::json::object object; | ||||||
| 
 | 
 | ||||||
|  |    // Write subcategory arrays
 | ||||||
|  |    for (auto& subcategoryArray : p->subcategoryArrays_) | ||||||
|  |    { | ||||||
|  |       boost::json::object arrayObject; | ||||||
|  | 
 | ||||||
|  |       for (auto& subcategory : subcategoryArray.second) | ||||||
|  |       { | ||||||
|  |          subcategory->WriteJson(arrayObject); | ||||||
|  |       } | ||||||
|  | 
 | ||||||
|  |       object.insert_or_assign(subcategoryArray.first, arrayObject); | ||||||
|  |    } | ||||||
|  | 
 | ||||||
|  |    // Write variables
 | ||||||
|    for (auto& variable : p->variables_) |    for (auto& variable : p->variables_) | ||||||
|    { |    { | ||||||
|       variable->WriteValue(object); |       variable->WriteValue(object); | ||||||
|  | @ -94,6 +163,18 @@ void SettingsCategory::WriteJson(boost::json::object& json) const | ||||||
|    json.insert_or_assign(p->name_, object); |    json.insert_or_assign(p->name_, object); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void SettingsCategory::RegisterSubcategoryArray( | ||||||
|  |    const std::string& name, std::vector<SettingsCategory>& subcategories) | ||||||
|  | { | ||||||
|  |    auto& newSubcategories = p->subcategoryArrays_.emplace_back( | ||||||
|  |       name, std::vector<SettingsCategory*> {}); | ||||||
|  | 
 | ||||||
|  |    std::transform(subcategories.begin(), | ||||||
|  |                   subcategories.end(), | ||||||
|  |                   std::back_inserter(newSubcategories.second), | ||||||
|  |                   [](SettingsCategory& subcategory) { return &subcategory; }); | ||||||
|  | } | ||||||
|  | 
 | ||||||
| void SettingsCategory::RegisterVariables( | void SettingsCategory::RegisterVariables( | ||||||
|    std::initializer_list<SettingsVariableBase*> variables) |    std::initializer_list<SettingsVariableBase*> variables) | ||||||
| { | { | ||||||
|  |  | ||||||
|  | @ -50,7 +50,8 @@ public: | ||||||
|     */ |     */ | ||||||
|    virtual void WriteJson(boost::json::object& json) const; |    virtual void WriteJson(boost::json::object& json) const; | ||||||
| 
 | 
 | ||||||
| protected: |    void RegisterSubcategoryArray(const std::string&             name, | ||||||
|  |                                  std::vector<SettingsCategory>& subcategories); | ||||||
|    void |    void | ||||||
|    RegisterVariables(std::initializer_list<SettingsVariableBase*> variables); |    RegisterVariables(std::initializer_list<SettingsVariableBase*> variables); | ||||||
|    void RegisterVariables(std::vector<SettingsVariableBase*> variables); |    void RegisterVariables(std::vector<SettingsVariableBase*> variables); | ||||||
|  |  | ||||||
|  | @ -82,6 +82,7 @@ public: | ||||||
|    TextSettings* self_; |    TextSettings* self_; | ||||||
| 
 | 
 | ||||||
|    std::unordered_map<types::FontCategory, FontData> fontData_ {}; |    std::unordered_map<types::FontCategory, FontData> fontData_ {}; | ||||||
|  |    std::vector<SettingsCategory>                     fontSettings_ {}; | ||||||
| 
 | 
 | ||||||
|    SettingsVariable<std::int64_t> hoverTextWrap_ {"hover_text_wrap"}; |    SettingsVariable<std::int64_t> hoverTextWrap_ {"hover_text_wrap"}; | ||||||
|    SettingsVariable<std::string>  tooltipMethod_ {"tooltip_method"}; |    SettingsVariable<std::string>  tooltipMethod_ {"tooltip_method"}; | ||||||
|  | @ -120,8 +121,15 @@ void TextSettings::Impl::InitializeFontVariables() | ||||||
|       font.fontPointSize_.SetMinimum(6.0); |       font.fontPointSize_.SetMinimum(6.0); | ||||||
|       font.fontPointSize_.SetMaximum(72.0); |       font.fontPointSize_.SetMaximum(72.0); | ||||||
| 
 | 
 | ||||||
|       // TODO: Variable registration
 |       // Variable registration
 | ||||||
|  |       auto& settings = fontSettings_.emplace_back( | ||||||
|  |          SettingsCategory {types::GetFontCategoryName(fontCategory)}); | ||||||
|  | 
 | ||||||
|  |       settings.RegisterVariables( | ||||||
|  |          {&font.fontFamily_, &font.fontStyle_, &font.fontPointSize_}); | ||||||
|    } |    } | ||||||
|  | 
 | ||||||
|  |    self_->RegisterSubcategoryArray("fonts", fontSettings_); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| SettingsVariable<std::string>& | SettingsVariable<std::string>& | ||||||
|  |  | ||||||
|  | @ -1 +1 @@ | ||||||
| Subproject commit 33caca188b1007c643db75afa560fdfe348c0ee5 | Subproject commit 1685e4048ef4a9f34bc11ecbb8db4905dd0a2e19 | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Dan Paulat
						Dan Paulat