From 470ac384e27aa47b3a86ac9fd689d2f80408569b Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Tue, 26 Jan 2016 00:05:40 +0000 Subject: [PATCH 01/13] refactor window themes and use JSON for format --- openrct2.vcxproj | 3 +- openrct2.vcxproj.filters | 3 + src/interface/Theme.cpp | 474 +++++++++++++++++++++++++++++++++++++++ src/rct2.h | 1 + 4 files changed, 480 insertions(+), 1 deletion(-) create mode 100644 src/interface/Theme.cpp diff --git a/openrct2.vcxproj b/openrct2.vcxproj index 330d4242fe..5ea5b3b28f 100644 --- a/openrct2.vcxproj +++ b/openrct2.vcxproj @@ -51,6 +51,7 @@ + @@ -380,4 +381,4 @@ - + \ No newline at end of file diff --git a/openrct2.vcxproj.filters b/openrct2.vcxproj.filters index 5fc8dbe7c9..9e8e13016e 100644 --- a/openrct2.vcxproj.filters +++ b/openrct2.vcxproj.filters @@ -582,6 +582,9 @@ Source\Windows + + Source\Interface + diff --git a/src/interface/Theme.cpp b/src/interface/Theme.cpp new file mode 100644 index 0000000000..911a703644 --- /dev/null +++ b/src/interface/Theme.cpp @@ -0,0 +1,474 @@ +#include + +extern "C" +{ + #include "../common.h" + #include "window.h" +} + +#include "../core/FileStream.hpp" +#include "../core/Math.hpp" +#include "../core/Memory.hpp" +#include "../core/String.hpp" +#include "../core/Util.hpp" + +struct WindowThemeDesc; + +// Don't try to load theme files that exceed 64 MiB +constexpr uint64 MAX_THEME_SIZE = 64 * 1024 * 1024; + +enum { + UITHEME_FLAG_PREDEFINED = 1 << 0, + UITHEME_FLAG_USE_LIGHTS_RIDE = 1 << 1, + UITHEME_FLAG_USE_LIGHTS_PARK = 1 << 2, + UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT = 1 << 3, +}; + +/** + * Represents a window theming style such as the colour scheme. + */ +struct WindowTheme +{ + colour_t Colours[6]; +}; + +/** + * Represents the style for a particular type of window. + */ +struct UIThemeEntry +{ + rct_windowclass WindowClass; + WindowTheme Theme; + + + json_t * ToJson() const; + static UIThemeEntry * FromJson(const WindowThemeDesc * wtDesc, const json_t * json); +}; + +/** + * Represents a user interface theme. Contains window colour schemes and appearance features. + */ +struct UITheme +{ + utf8 * Name; + UIThemeEntry * Entries; + uint8 Flags; + + + void SetEntry(const UIThemeEntry * entry); + void RemoveEntry(rct_windowclass windowClass); + json_t * ToJson() const; + bool WriteToFile(const utf8 * path) const; + static UITheme * Create(const utf8 * name); + static void Free(UITheme * theme); + static UITheme * FromJson(const json_t * json); + static UITheme * FromFile(const utf8 * path); +}; + +/** + * Represents the theme descriptor for a specific window type including the default colour scheme. + */ +struct WindowThemeDesc +{ + rct_windowclass WindowClass; + const utf8 * WindowClassSZ; + rct_string_id WindowName; + uint8 NumColours; + WindowTheme DefaultTheme; +}; + +#pragma region Window Theme Descriptors + +#define COLOURS_1(c0) 1, { { (c0), 0, 0, 0, 0, 0 } } +#define COLOURS_2(c0, c1) 2, { { (c0), (c1), 0, 0, 0, 0 } } +#define COLOURS_3(c0, c1, c2) 3, { { (c0), (c1), (c2), 0, 0, 0 } } +#define COLOURS_4(c0, c1, c2, c3) 4, { { (c0), (c1), (c2), (c3), 0, 0 } } +#define COLOURS_5(c0, c1, c2, c3, c4) 5, { { (c0), (c1), (c2), (c3), (c4), 0 } } +#define COLOURS_6(c0, c1, c2, c3, c4, c5) 6, { { (c0), (c1), (c2), (c3), (c4), (c5) } } + +#define THEME_DEF_END { 0xFF, { 0, 0, 0, 0, 0, 0 } } + +#define TWINDOW(window_class, window_name, window_string_id, theme) { window_class, window_name, window_string_id, theme } + +#define THEME_WC(wc) wc, #wc + +WindowThemeDesc WindowThemeDescriptors[] = +{ + // WindowClass, WindowClassSZ WindowName NumColours, DefaultTheme + { THEME_WC(WC_TOP_TOOLBAR), 5245, COLOURS_4(COLOUR_LIGHT_BLUE, COLOUR_DARK_GREEN, COLOUR_DARK_BROWN, COLOUR_GREY ) }, + { THEME_WC(WC_BOTTOM_TOOLBAR), 5246, COLOURS_4(TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN), COLOUR_BLACK, COLOUR_BRIGHT_GREEN ) }, + { THEME_WC(WC_RIDE), 5203, COLOURS_3(COLOUR_GREY, COLOUR_BORDEAUX_RED, COLOUR_GREY ) }, + { THEME_WC(WC_RIDE_CONSTRUCTION), 5199, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_RIDE_LIST), 5204, COLOURS_3(COLOUR_GREY, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, + { THEME_WC(WC_SAVE_PROMPT), 5223, COLOURS_1(TRANSLUCENT(COLOUR_BORDEAUX_RED) ) }, + { THEME_WC(WC_CONSTRUCT_RIDE), 5201, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, + { THEME_WC(WC_DEMOLISH_RIDE_PROMPT), 5224, COLOURS_1(TRANSLUCENT(COLOUR_BORDEAUX_RED) ) }, + { THEME_WC(WC_SCENERY), 5197, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_GREEN, COLOUR_DARK_GREEN ) }, + { THEME_WC(WC_OPTIONS), 5219, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_FOOTPATH), 5198, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_LAND), 5193, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_WATER), 5194, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_PEEP), 5205, COLOURS_3(COLOUR_GREY, COLOUR_OLIVE_GREEN, COLOUR_OLIVE_GREEN ) }, + { THEME_WC(WC_GUEST_LIST), 5206, COLOURS_3(COLOUR_GREY, COLOUR_OLIVE_GREEN, COLOUR_OLIVE_GREEN ) }, + { THEME_WC(WC_STAFF_LIST), 5208, COLOURS_3(COLOUR_GREY, COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE ) }, + { THEME_WC(WC_FIRE_PROMPT), 5225, COLOURS_1(TRANSLUCENT(COLOUR_BORDEAUX_RED) ) }, + { THEME_WC(WC_PARK_INFORMATION), 5253, COLOURS_3(COLOUR_GREY, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, + { THEME_WC(WC_FINANCES), 5187, COLOURS_3(COLOUR_GREY, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, + { THEME_WC(WC_TITLE_MENU), 5249, COLOURS_3(TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN) ) }, + { THEME_WC(WC_TITLE_EXIT), 5250, COLOURS_3(TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN) ) }, + { THEME_WC(WC_RECENT_NEWS), 5192, COLOURS_3(COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK ) }, + { THEME_WC(WC_SCENARIO_SELECT), 5252, COLOURS_3(COLOUR_GREY, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, + { THEME_WC(WC_TRACK_DESIGN_LIST), 5202, COLOURS_3(COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, + { THEME_WC(WC_TRACK_DESIGN_PLACE), 5200, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_NEW_CAMPAIGN), 5188, COLOURS_3(COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, + { THEME_WC(WC_KEYBOARD_SHORTCUT_LIST), 5220, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_CHANGE_KEYBOARD_SHORTCUT), 5221, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_MAP), 5190, COLOURS_2(COLOUR_DARK_GREEN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_BANNER), 5209, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_EDITOR_OBJECT_SELECTION), 5210, COLOURS_3(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY ) }, + { THEME_WC(WC_EDITOR_INVENTION_LIST), 5211, COLOURS_3(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY ) }, + { THEME_WC(WC_EDITOR_SCENARIO_OPTIONS), 5212, COLOURS_3(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY ) }, + { THEME_WC(WC_EDTIOR_OBJECTIVE_OPTIONS), 5213, COLOURS_3(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY ) }, + { THEME_WC(WC_MANAGE_TRACK_DESIGN), 5215, COLOURS_3(COLOUR_GREY, COLOUR_GREY, COLOUR_GREY ) }, + { THEME_WC(WC_TRACK_DELETE_PROMPT), 5226, COLOURS_3(COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, + { THEME_WC(WC_INSTALL_TRACK), 5216, COLOURS_3(COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, + { THEME_WC(WC_CLEAR_SCENERY), 5195, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_CHEATS), 5217, COLOURS_3(COLOUR_GREY, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, + { THEME_WC(WC_RESEARCH), 5189, COLOURS_3(COLOUR_GREY, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, + { THEME_WC(WC_VIEWPORT), 5191, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_MAPGEN), 5214, COLOURS_3(COLOUR_DARK_GREEN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, + { THEME_WC(WC_LOADSAVE), 5222, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_LOADSAVE_OVERWRITE_PROMPT), 5227, COLOURS_1(TRANSLUCENT(COLOUR_BORDEAUX_RED) ) }, + { THEME_WC(WC_TITLE_OPTIONS), 5251, COLOURS_3(TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN) ) }, + { THEME_WC(WC_LAND_RIGHTS), 5196, COLOURS_3(COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, + { THEME_WC(WC_THEMES), 5218, COLOURS_3(COLOUR_GREY, COLOUR_DARK_GREEN, COLOUR_DARK_GREEN ) }, + { THEME_WC(WC_STAFF), 5207, COLOURS_3(COLOUR_GREY, COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE ) }, + { THEME_WC(WC_EDITOR_TRACK_BOTTOM_TOOLBAR), 5247, COLOURS_3(TRANSLUCENT(COLOUR_LIGHT_BLUE), TRANSLUCENT(COLOUR_LIGHT_BLUE), TRANSLUCENT(COLOUR_LIGHT_BLUE) ) }, + { THEME_WC(WC_EDITOR_SCENARIO_BOTTOM_TOOLBAR), 5248, COLOURS_3(TRANSLUCENT(COLOUR_LIGHT_BROWN), TRANSLUCENT(COLOUR_LIGHT_BROWN), TRANSLUCENT(COLOUR_MOSS_GREEN) ) }, + { THEME_WC(WC_TITLE_EDITOR), 5433, COLOURS_3(COLOUR_GREY, COLOUR_OLIVE_GREEN, COLOUR_OLIVE_GREEN ) }, +}; + +#pragma endregion + +#pragma region Pre-defined Themes + +#define COLOURS_RCT1(c0, c1, c2, c3, c4, c5) { { (c0), (c1), (c2), (c3), (c4), (c5) } } + +UIThemeEntry PredefinedThemeRCT1_Entries[] = +{ + { WC_TOP_TOOLBAR, COLOURS_RCT1(COLOUR_GREY, COLOUR_GREY, COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_BOTTOM_TOOLBAR, COLOURS_RCT1(TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), COLOUR_BLACK, COLOUR_YELLOW, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_RIDE, COLOURS_RCT1(COLOUR_BORDEAUX_RED, COLOUR_GREY, COLOUR_SATURATED_GREEN, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_RIDE_LIST, COLOURS_RCT1(COLOUR_BORDEAUX_RED, COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_CONSTRUCT_RIDE, COLOURS_RCT1(COLOUR_BORDEAUX_RED, COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_PEEP, COLOURS_RCT1(COLOUR_LIGHT_BROWN, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_GUEST_LIST, COLOURS_RCT1(COLOUR_LIGHT_BROWN, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_STAFF_LIST, COLOURS_RCT1(COLOUR_DARK_GREEN, COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_FINANCES, COLOURS_RCT1(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_TITLE_MENU, COLOURS_RCT1(TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_TITLE_EXIT, COLOURS_RCT1(TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_NEW_CAMPAIGN, COLOURS_RCT1(COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_TITLE_OPTIONS, COLOURS_RCT1(TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + { WC_STAFF, COLOURS_RCT1(COLOUR_DARK_GREEN, COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, + THEME_DEF_END +}; + +UIThemeEntry PredefinedThemeRCT2_Entries[] = +{ + THEME_DEF_END +}; + +const UITheme PredefinedThemeRCT1 = { "RCT1", PredefinedThemeRCT1_Entries, UITHEME_FLAG_PREDEFINED }; +const UITheme PredefinedThemeRCT2 = { "RCT2", PredefinedThemeRCT2_Entries, UITHEME_FLAG_PREDEFINED }; + +const UITheme * PredefinedThemes[] = { + &PredefinedThemeRCT1, + &PredefinedThemeRCT2, + nullptr +}; + +#pragma endregion + +static const WindowThemeDesc * GetWindowThemeDescriptor(rct_windowclass windowClass) +{ + for (size_t i = 0; i < Util::CountOf(WindowThemeDescriptors); i++) + { + const WindowThemeDesc * desc = &WindowThemeDescriptors[i]; + if (desc->WindowClass == windowClass) + { + return desc; + } + } + return nullptr; +} + +static const WindowThemeDesc * GetWindowThemeDescriptor(const utf8 * windowClassSZ) +{ + for (size_t i = 0; i < Util::CountOf(WindowThemeDescriptors); i++) + { + const WindowThemeDesc * desc = &WindowThemeDescriptors[i]; + if (String::Equals(desc->WindowClassSZ, windowClassSZ)) + { + return desc; + } + } + return nullptr; +} + +static void ThrowThemeLoadException() +{ + throw Exception("Invalid JSON UI theme entry."); +} + +#pragma region UIThemeEntry + +json_t * UIThemeEntry::ToJson() const +{ + const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(WindowClass); + + json_t * jsonColours = json_array(); + for (uint8 i = 0; i < wtDesc->NumColours; i++) { + colour_t colour = Theme.Colours[i]; + json_array_append_new(jsonColours, json_integer(colour)); + } + + json_t * jsonEntry = json_object(); + json_object_set_new(jsonEntry, "colours", jsonColours); + + return jsonEntry; +} + +UIThemeEntry * UIThemeEntry::FromJson(const WindowThemeDesc * wtDesc, const json_t * json) +{ + json_t * jsonColours = json_object_get(json, "colours"); + if (jsonColours == nullptr) + { + ThrowThemeLoadException(); + } + + uint8 numColours = (uint8)json_array_size(jsonColours); + numColours = Math::Min(numColours, wtDesc->NumColours); + + UIThemeEntry * result = Memory::Allocate(); + result->WindowClass = wtDesc->WindowClass; + result->Theme = wtDesc->DefaultTheme; + + for (uint8 i = 0; i < numColours; i++) + { + result->Theme.Colours[i] = (colour_t)json_integer_value(json_array_get(jsonColours, i)); + } + + return result; +} + +UITheme * UITheme::Create(const utf8 * name) +{ + UITheme * theme = Memory::Allocate(); + theme->Name = String::Duplicate(name); + theme->Entries = Memory::AllocateArray(1); + theme->Entries[0].WindowClass = 255; + theme->Flags = 0; + return theme; +} + +#pragma endregion + +#pragma region UITheme + +void UITheme::Free(UITheme * theme) +{ + assert(!(theme->Flags & UITHEME_FLAG_PREDEFINED)); + + Memory::Free(theme->Name); + Memory::Free(theme->Entries); +} + +void UITheme::SetEntry(const UIThemeEntry * newEntry) +{ + // Try to replace existing entry + size_t numEntries = 0; + for (UIThemeEntry * entry = Entries; entry->WindowClass != 0xFF; entry++) + { + if (entry->WindowClass == newEntry->WindowClass) + { + *entry = *newEntry; + return; + } + + numEntries++; + } + + // Increase size of entry list + Entries = Memory::ReallocateArray(Entries, numEntries + 2); + Entries[numEntries] = *newEntry; + Entries[numEntries + 1].WindowClass = 255; +} + +void UITheme::RemoveEntry(rct_windowclass windowClass) +{ + // Remove existing entry + bool found = false; + for (UIThemeEntry * entry = Entries; entry->WindowClass != 0xFF; entry++) + { + if (entry->WindowClass == windowClass) + { + for (UIThemeEntry * entry2 = entry; entry2->WindowClass != 0xFF; entry2++) + { + *entry2 = *(entry2 + 1); + } + found = true; + break; + } + } + if (!found) return; + + // Reduce size of entry list + size_t numEntries = 0; + for (UIThemeEntry * entry = Entries; entry->WindowClass != 0xFF; entry++) + { + numEntries++; + } + Entries = Memory::ReallocateArray(Entries, numEntries); +} + +json_t * UITheme::ToJson() const +{ + // Create entries + json_t * jsonEntries = json_object(); + for (UIThemeEntry * entry = Entries; entry->WindowClass != 0xFF; entry++) + { + const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(entry->WindowClass); + json_object_set_new(jsonEntries, wtDesc->WindowClassSZ, entry->ToJson()); + } + + // Create theme object + json_t * jsonTheme = json_object(); + json_object_set_new(jsonTheme, "name", json_string(Name)); + json_object_set_new(jsonTheme, "entries", jsonEntries); + + json_object_set_new(jsonTheme, "useLightsRide", json_boolean(Flags & UITHEME_FLAG_USE_LIGHTS_RIDE)); + json_object_set_new(jsonTheme, "useLightsPark", json_boolean(Flags & UITHEME_FLAG_USE_LIGHTS_PARK)); + json_object_set_new(jsonTheme, + "useAltScenarioSelectFont", + json_boolean(Flags & UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT)); + + return jsonTheme; +} + +bool UITheme::WriteToFile(const utf8 * path) const +{ + const char * jsonOutput; + + // Serialise theme object as JSON + json_t * jsonTheme = ToJson(); + jsonOutput = json_dumps(jsonTheme, JSON_INDENT(4)); + json_decref(jsonTheme); + + try + { + auto fs = FileStream(path, FILE_MODE_WRITE); + size_t jsonOutputSize = String::SizeOf(jsonOutput); + fs.Write(jsonOutput, jsonOutputSize); + + return true; + } + catch (Exception ex) + { + log_error("Unable to save %s: %s", path, ex.GetMessage()); + return false; + } +} + +UITheme * UITheme::FromJson(const json_t * json) +{ + const char * themeName = json_string_value(json_object_get(json, "name")); + if (themeName == nullptr) + { + ThrowThemeLoadException(); + } + + json_t * jsonEntries = json_object_get(json, "entries"); + size_t numEntries = json_object_size(jsonEntries); + if (numEntries == 0) + { + ThrowThemeLoadException(); + } + + UITheme * result = nullptr; + try + { + result = UITheme::Create(themeName); + + if (json_is_true(json_object_get(json, "useLightsRide"))) + { + result->Flags |= UITHEME_FLAG_USE_LIGHTS_RIDE; + } + if (json_is_true(json_object_get(json, "useLightsPark"))) + { + result->Flags |= UITHEME_FLAG_USE_LIGHTS_PARK; + } + if (json_is_true(json_object_get(json, "useAltScenarioSelectFont"))) + { + result->Flags |= UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT; + } + + const char * jkey; + json_t * jvalue; + size_t i = 0; + json_object_foreach(jsonEntries, jkey, jvalue) + { + const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(jkey); + if (wtDesc == nullptr) continue; + + UIThemeEntry * entry = UIThemeEntry::FromJson(wtDesc, jvalue); + result->SetEntry(entry); + Memory::Free(entry); + } + + return result; + } + catch (Exception ex) + { + UITheme::Free(result); + throw ex; + } +} + +UITheme * UITheme::FromFile(const utf8 * path) +{ + json_t * json = nullptr; + UITheme * result = nullptr; + try + { + auto fs = FileStream(path, FILE_MODE_OPEN); + + size_t fileLength = (size_t)fs.GetLength(); + if (fileLength > MAX_THEME_SIZE) + { + throw IOException("Language file too large."); + } + + utf8 * fileData = Memory::Allocate(fileLength + 1); + fs.Read(fileData, fileLength); + fileData[fileLength] = '\0'; + + json_error_t jsonLoadError; + json = json_loads(fileData, 0, &jsonLoadError); + Memory::Free(fileData); + + if (json != nullptr) + { + result = UITheme::FromJson(json); + } + } + catch (Exception ex) + { + result = nullptr; + } + + json_decref(json); + return result; +} + +#pragma endregion + diff --git a/src/rct2.h b/src/rct2.h index e3a3af6300..7c84aaeab0 100644 --- a/src/rct2.h +++ b/src/rct2.h @@ -59,6 +59,7 @@ typedef wchar_t utf16; typedef utf16* utf16string; typedef uint32 codepoint_t; +typedef uint8 colour_t; #define rol8(x, shift) (((uint8)(x) << (shift)) | ((uint8)(x) >> (8 - (shift)))) #define ror8(x, shift) (((uint8)(x) >> (shift)) | ((uint8)(x) << (8 - (shift)))) From d67d6a483136887ac9c59e7a303703d9c3a15091 Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Mon, 4 Jan 2016 17:24:12 +0000 Subject: [PATCH 02/13] add more core classes --- openrct2.vcxproj | 3 ++ openrct2.vcxproj.filters | 3 ++ src/audio/mixer.cpp | 1 + src/core/Diagnostics.hpp | 22 ++++++++++++ src/core/Guard.hpp | 22 ++++++++++++ src/core/List.hpp | 76 ++++++++++++++++++++++++++++++++++++++++ src/core/Memory.hpp | 44 +++++++++++++++-------- 7 files changed, 157 insertions(+), 14 deletions(-) create mode 100644 src/core/Diagnostics.hpp create mode 100644 src/core/Guard.hpp create mode 100644 src/core/List.hpp diff --git a/openrct2.vcxproj b/openrct2.vcxproj index 5ea5b3b28f..2269f23310 100644 --- a/openrct2.vcxproj +++ b/openrct2.vcxproj @@ -200,10 +200,13 @@ + + + diff --git a/openrct2.vcxproj.filters b/openrct2.vcxproj.filters index 9e8e13016e..acc6194d01 100644 --- a/openrct2.vcxproj.filters +++ b/openrct2.vcxproj.filters @@ -887,5 +887,8 @@ Source\Core + + + \ No newline at end of file diff --git a/src/audio/mixer.cpp b/src/audio/mixer.cpp index 7f23802a26..44cefadf38 100644 --- a/src/audio/mixer.cpp +++ b/src/audio/mixer.cpp @@ -19,6 +19,7 @@ *****************************************************************************/ extern "C" { + #include "../addresses.h" #include "../config.h" #include "../platform/platform.h" #include "../localisation/localisation.h" diff --git a/src/core/Diagnostics.hpp b/src/core/Diagnostics.hpp new file mode 100644 index 0000000000..5019b8c0d8 --- /dev/null +++ b/src/core/Diagnostics.hpp @@ -0,0 +1,22 @@ +#pragma once + +#if _WIN32 +#include +#endif + +/** + * Utility methods for asserting and logging. + */ +namespace Debug +{ + void Break() + { +#if DEBUG +#if _WIN32 + if (IsDebuggerPresent()) { + DebugBreak(); + } +#endif +#endif + } +} diff --git a/src/core/Guard.hpp b/src/core/Guard.hpp new file mode 100644 index 0000000000..1f8cff8033 --- /dev/null +++ b/src/core/Guard.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +#include "Diagnostics.hpp" + +/** + * Utility methods for asserting function parameters. + */ +namespace Guard +{ + template + void ArgumentInRange(T argument, T min, T max, const char *message = nullptr) + { + Debug::Break(); + + fputs(message, stderr); + fputc('\n', stderr); + assert(argument < min || argument > max); + } +}; diff --git a/src/core/List.hpp b/src/core/List.hpp new file mode 100644 index 0000000000..0d8385238e --- /dev/null +++ b/src/core/List.hpp @@ -0,0 +1,76 @@ +#pragma once + +#include + +#include "../common.h" +#include "Guard.hpp" +#include "Memory.hpp" + +/** + * A container that stores elements in contiguous memory. Automatically reallocates memory when required. Equivalent to + * std::vector. + */ +template +class List : public std::vector +{ +public: + size_t GetCapacity() const { return this->capacity(); } + size_t GetCount() const { return this->size(); } + const T * GetItems() const { return this->data(); } + + List() : std::vector() { } + + List(size_t capacity) : std::vector(capacity) { } + + List(const T * items, size_t count) : std::vector(items, items + count) { } + + void EnsureCapacity(size_t capacity) + { + this->reserve(capacity); + } + + void ShrinkToLength() + { + this->shrink_to_fit(); + } + + void Clear() + { + this->clear(); + } + + void Add(T item) + { + this->push_back(item); + } + + void Insert(T item, size_t index) + { + Guard::ArgumentInRange(index, (size_t)0, this->size()); + this->insert() + } + + bool Remove(T item) + { + for (size_t i = 0; i < this->size(); i++) + { + if (_items[i] == item) + { + RemoveAt(i); + return true; + } + } + return false; + } + + void RemoveAt(size_t index) + { + Guard::ArgumentInRange(index, (size_t)0, this->size() - 1); + this->erase(this->begin() + index); + } + + const T * ToArray() const + { + return Memory::DuplicateArray(this->data(), this->size()); + } +}; diff --git a/src/core/Memory.hpp b/src/core/Memory.hpp index fcab3ac554..50d9dfbf6a 100644 --- a/src/core/Memory.hpp +++ b/src/core/Memory.hpp @@ -67,13 +67,6 @@ namespace Memory return (T*)memcpy((void*)dst, (const void*)src, size); } - template - T * CopyArray(T *dst, const T * src, size_t count) - { - if (count == 0) return (T*)dst; - return (T*)memcpy((void*)dst, (const void*)src, count * sizeof(T)); - } - template T * Duplicate(const T * src, size_t size) { @@ -81,16 +74,39 @@ namespace Memory return Copy(result, src, size); } - template - T * DuplicateArray(const T * src, size_t count) - { - T *result = AllocateArray(count); - return CopyArray(result, src, count); - } - template T * Set(T * dst, uint8 value, size_t size) { return (T*)memset((void*)dst, (int)value, size); } + + template + T * CopyArray(T * dst, const T * src, size_t count) + { + // Use a loop so that copy constructors are called + // compiler should optimise to memcpy if possible + T * result = dst; + for (; count > 0; count--) + { + *dst++ = *src++; + } + return result; + } + + template + T * DuplicateArray(const T * src, size_t count) + { + T * result = AllocateArray(count); + return CopyArray(result, src, count); + } + + template + void FreeArray(T * ptr, size_t count) + { + for (size_t i = 0; i < count; i++) + { + ptr[i]::~T(); + } + Free(ptr); + } } From b1b97410902366cc8a3cbe930144c1fdc53c59c7 Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Thu, 28 Jan 2016 22:21:36 +0000 Subject: [PATCH 03/13] add JSON helpers --- openrct2.vcxproj | 2 ++ openrct2.vcxproj.filters | 2 ++ src/core/Json.cpp | 45 ++++++++++++++++++++++++++++++++++++++++ src/core/Json.hpp | 29 ++++++++++++++++++++++++++ 4 files changed, 78 insertions(+) create mode 100644 src/core/Json.cpp create mode 100644 src/core/Json.hpp diff --git a/openrct2.vcxproj b/openrct2.vcxproj index 2269f23310..16dfe52ca6 100644 --- a/openrct2.vcxproj +++ b/openrct2.vcxproj @@ -30,6 +30,7 @@ + @@ -206,6 +207,7 @@ + diff --git a/openrct2.vcxproj.filters b/openrct2.vcxproj.filters index acc6194d01..df0b6eb8d6 100644 --- a/openrct2.vcxproj.filters +++ b/openrct2.vcxproj.filters @@ -585,6 +585,7 @@ Source\Interface + @@ -890,5 +891,6 @@ + \ No newline at end of file diff --git a/src/core/Json.cpp b/src/core/Json.cpp new file mode 100644 index 0000000000..0c89336d73 --- /dev/null +++ b/src/core/Json.cpp @@ -0,0 +1,45 @@ +#include "FileStream.hpp" +#include "Json.hpp" +#include "Memory.hpp" +#include "String.hpp" + +namespace Json +{ + json_t * ReadFromFile(const utf8 * path, size_t maxSize) + { + json_t * json = nullptr; + auto fs = FileStream(path, FILE_MODE_OPEN); + + size_t fileLength = (size_t)fs.GetLength(); + if (fileLength > maxSize) + { + throw IOException("Language file too large."); + } + + utf8 * fileData = Memory::Allocate(fileLength + 1); + fs.Read(fileData, fileLength); + fileData[fileLength] = '\0'; + + json_error_t jsonLoadError; + json = json_loads(fileData, 0, &jsonLoadError); + Memory::Free(fileData); + + if (json == nullptr) + { + throw JsonException(&jsonLoadError); + } + + return json; + } + + void WriteToFile(const utf8 * path, const json_t * json, size_t flags) + { + // Serialise JSON + const char * jsonOutput = json_dumps(json, flags); + + // Write to file + auto fs = FileStream(path, FILE_MODE_WRITE); + size_t jsonOutputSize = String::SizeOf(jsonOutput); + fs.Write(jsonOutput, jsonOutputSize); + } +} diff --git a/src/core/Json.hpp b/src/core/Json.hpp new file mode 100644 index 0000000000..9a9aeb5d6b --- /dev/null +++ b/src/core/Json.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include + +#include "../common.h" +#include "Exception.hpp" + +namespace Json +{ + // Don't try to load JSON files that exceed 64 MiB + constexpr uint64 MAX_JSON_SIZE = 64 * 1024 * 1024; + + json_t * ReadFromFile(const utf8 * path, size_t maxSize = MAX_JSON_SIZE); + void WriteToFile(const utf8 * path, const json_t * json, size_t flags = 0); +} + +class JsonException : public Exception +{ +private: + json_error_t _jsonError; + +public: + JsonException(const char * message) : Exception(message) { } + + JsonException(const json_error_t * jsonError) : JsonException(jsonError->text) + { + _jsonError = *jsonError; + } +}; From de17eb7279f76fe6615c8e13e8113132ab725da2 Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Thu, 28 Jan 2016 22:22:02 +0000 Subject: [PATCH 04/13] improve Theme to use List and Json --- src/interface/Theme.cpp | 180 ++++++++++++++++++---------------------- 1 file changed, 81 insertions(+), 99 deletions(-) diff --git a/src/interface/Theme.cpp b/src/interface/Theme.cpp index 911a703644..a94ac6c9a6 100644 --- a/src/interface/Theme.cpp +++ b/src/interface/Theme.cpp @@ -6,7 +6,8 @@ extern "C" #include "window.h" } -#include "../core/FileStream.hpp" +#include "../core/Json.hpp" +#include "../core/List.hpp" #include "../core/Math.hpp" #include "../core/Memory.hpp" #include "../core/String.hpp" @@ -41,28 +42,32 @@ struct UIThemeEntry WindowTheme Theme; - json_t * ToJson() const; - static UIThemeEntry * FromJson(const WindowThemeDesc * wtDesc, const json_t * json); + json_t * ToJson() const; + static UIThemeEntry FromJson(const WindowThemeDesc * wtDesc, const json_t * json); }; /** * Represents a user interface theme. Contains window colour schemes and appearance features. */ -struct UITheme +class UITheme { - utf8 * Name; - UIThemeEntry * Entries; - uint8 Flags; +public: + utf8 * Name; + List Entries; + uint8 Flags; + UITheme(const utf8 * name); + UITheme(const UITheme & name); + ~UITheme(); + + void SetEntry(const UIThemeEntry * entry); + void RemoveEntry(rct_windowclass windowClass); + json_t * ToJson() const; + bool WriteToFile(const utf8 * path) const; - void SetEntry(const UIThemeEntry * entry); - void RemoveEntry(rct_windowclass windowClass); - json_t * ToJson() const; - bool WriteToFile(const utf8 * path) const; - static UITheme * Create(const utf8 * name); - static void Free(UITheme * theme); static UITheme * FromJson(const json_t * json); static UITheme * FromFile(const utf8 * path); + static UITheme CreatePredefined(const utf8 * name, const UIThemeEntry * entries, uint8 flags); }; /** @@ -178,8 +183,13 @@ UIThemeEntry PredefinedThemeRCT2_Entries[] = THEME_DEF_END }; -const UITheme PredefinedThemeRCT1 = { "RCT1", PredefinedThemeRCT1_Entries, UITHEME_FLAG_PREDEFINED }; -const UITheme PredefinedThemeRCT2 = { "RCT2", PredefinedThemeRCT2_Entries, UITHEME_FLAG_PREDEFINED }; +const UITheme PredefinedThemeRCT1 = UITheme::CreatePredefined( + "RCT1", PredefinedThemeRCT1_Entries, UITHEME_FLAG_USE_LIGHTS_RIDE | + UITHEME_FLAG_USE_LIGHTS_PARK | + UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT); + +const UITheme PredefinedThemeRCT2 = UITheme::CreatePredefined( + "RCT2", PredefinedThemeRCT2_Entries, 0); const UITheme * PredefinedThemes[] = { &PredefinedThemeRCT1, @@ -238,7 +248,7 @@ json_t * UIThemeEntry::ToJson() const return jsonEntry; } -UIThemeEntry * UIThemeEntry::FromJson(const WindowThemeDesc * wtDesc, const json_t * json) +UIThemeEntry UIThemeEntry::FromJson(const WindowThemeDesc * wtDesc, const json_t * json) { json_t * jsonColours = json_object_get(json, "colours"); if (jsonColours == nullptr) @@ -249,96 +259,78 @@ UIThemeEntry * UIThemeEntry::FromJson(const WindowThemeDesc * wtDesc, const json uint8 numColours = (uint8)json_array_size(jsonColours); numColours = Math::Min(numColours, wtDesc->NumColours); - UIThemeEntry * result = Memory::Allocate(); - result->WindowClass = wtDesc->WindowClass; - result->Theme = wtDesc->DefaultTheme; + UIThemeEntry result; + result.WindowClass = wtDesc->WindowClass; + result.Theme = wtDesc->DefaultTheme; for (uint8 i = 0; i < numColours; i++) { - result->Theme.Colours[i] = (colour_t)json_integer_value(json_array_get(jsonColours, i)); + result.Theme.Colours[i] = (colour_t)json_integer_value(json_array_get(jsonColours, i)); } return result; } -UITheme * UITheme::Create(const utf8 * name) -{ - UITheme * theme = Memory::Allocate(); - theme->Name = String::Duplicate(name); - theme->Entries = Memory::AllocateArray(1); - theme->Entries[0].WindowClass = 255; - theme->Flags = 0; - return theme; -} - #pragma endregion #pragma region UITheme -void UITheme::Free(UITheme * theme) +UITheme::UITheme(const utf8 * name) { - assert(!(theme->Flags & UITHEME_FLAG_PREDEFINED)); + Name = String::Duplicate(name); + Flags = 0; +} - Memory::Free(theme->Name); - Memory::Free(theme->Entries); +UITheme::UITheme(const UITheme & copy) +{ + Name = String::Duplicate(copy.Name); + Flags = copy.Flags; + Entries = copy.Entries; +} + +UITheme::~UITheme() +{ + Memory::Free(Name); } void UITheme::SetEntry(const UIThemeEntry * newEntry) { // Try to replace existing entry - size_t numEntries = 0; - for (UIThemeEntry * entry = Entries; entry->WindowClass != 0xFF; entry++) + for (size_t i = 0; i < Entries.GetCount(); i++) { + UIThemeEntry * entry = &Entries[i]; if (entry->WindowClass == newEntry->WindowClass) { *entry = *newEntry; return; } - - numEntries++; } - // Increase size of entry list - Entries = Memory::ReallocateArray(Entries, numEntries + 2); - Entries[numEntries] = *newEntry; - Entries[numEntries + 1].WindowClass = 255; + Entries.Add(*newEntry); } void UITheme::RemoveEntry(rct_windowclass windowClass) { // Remove existing entry - bool found = false; - for (UIThemeEntry * entry = Entries; entry->WindowClass != 0xFF; entry++) + for (size_t i = 0; i < Entries.GetCount(); i++) { + UIThemeEntry * entry = &Entries[i]; if (entry->WindowClass == windowClass) { - for (UIThemeEntry * entry2 = entry; entry2->WindowClass != 0xFF; entry2++) - { - *entry2 = *(entry2 + 1); - } - found = true; + Entries.RemoveAt(i); break; } } - if (!found) return; - - // Reduce size of entry list - size_t numEntries = 0; - for (UIThemeEntry * entry = Entries; entry->WindowClass != 0xFF; entry++) - { - numEntries++; - } - Entries = Memory::ReallocateArray(Entries, numEntries); } json_t * UITheme::ToJson() const { // Create entries json_t * jsonEntries = json_object(); - for (UIThemeEntry * entry = Entries; entry->WindowClass != 0xFF; entry++) + for (const UIThemeEntry & entry : Entries) { - const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(entry->WindowClass); - json_object_set_new(jsonEntries, wtDesc->WindowClassSZ, entry->ToJson()); + const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(entry.WindowClass); + json_object_set_new(jsonEntries, wtDesc->WindowClassSZ, entry.ToJson()); } // Create theme object @@ -357,26 +349,21 @@ json_t * UITheme::ToJson() const bool UITheme::WriteToFile(const utf8 * path) const { - const char * jsonOutput; - - // Serialise theme object as JSON json_t * jsonTheme = ToJson(); - jsonOutput = json_dumps(jsonTheme, JSON_INDENT(4)); - json_decref(jsonTheme); - + bool result; try { - auto fs = FileStream(path, FILE_MODE_WRITE); - size_t jsonOutputSize = String::SizeOf(jsonOutput); - fs.Write(jsonOutput, jsonOutputSize); - - return true; + Json::WriteToFile(path, jsonTheme, JSON_INDENT(4)); + result = true; } catch (Exception ex) { log_error("Unable to save %s: %s", path, ex.GetMessage()); - return false; + result = false; } + + json_decref(jsonTheme); + return result; } UITheme * UITheme::FromJson(const json_t * json) @@ -397,7 +384,7 @@ UITheme * UITheme::FromJson(const json_t * json) UITheme * result = nullptr; try { - result = UITheme::Create(themeName); + result = new UITheme(themeName); if (json_is_true(json_object_get(json, "useLightsRide"))) { @@ -420,16 +407,15 @@ UITheme * UITheme::FromJson(const json_t * json) const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(jkey); if (wtDesc == nullptr) continue; - UIThemeEntry * entry = UIThemeEntry::FromJson(wtDesc, jvalue); - result->SetEntry(entry); - Memory::Free(entry); + UIThemeEntry entry = UIThemeEntry::FromJson(wtDesc, jvalue); + result->SetEntry(&entry); } return result; } catch (Exception ex) { - UITheme::Free(result); + delete result; throw ex; } } @@ -440,26 +426,8 @@ UITheme * UITheme::FromFile(const utf8 * path) UITheme * result = nullptr; try { - auto fs = FileStream(path, FILE_MODE_OPEN); - - size_t fileLength = (size_t)fs.GetLength(); - if (fileLength > MAX_THEME_SIZE) - { - throw IOException("Language file too large."); - } - - utf8 * fileData = Memory::Allocate(fileLength + 1); - fs.Read(fileData, fileLength); - fileData[fileLength] = '\0'; - - json_error_t jsonLoadError; - json = json_loads(fileData, 0, &jsonLoadError); - Memory::Free(fileData); - - if (json != nullptr) - { - result = UITheme::FromJson(json); - } + json = Json::ReadFromFile(path); + result = UITheme::FromJson(json); } catch (Exception ex) { @@ -470,5 +438,19 @@ UITheme * UITheme::FromFile(const utf8 * path) return result; } -#pragma endregion +UITheme UITheme::CreatePredefined(const utf8 * name, const UIThemeEntry * entries, uint8 flags) +{ + auto theme = UITheme(name); + theme.Flags = flags | UITHEME_FLAG_PREDEFINED; + size_t numEntries = 0; + for (const UIThemeEntry * entry = entries; entry->WindowClass != 255; entry++) + { + numEntries++; + } + + theme.Entries = List(entries, numEntries); + return theme; +} + +#pragma endregion From 0eb57e5fb55c5b63f1150b02a9cd9af2d25809ed Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Fri, 29 Jan 2016 00:06:02 +0000 Subject: [PATCH 05/13] start work on theme manager --- src/core/Path.cpp | 24 ++++++++ src/core/Path.hpp | 3 +- src/interface/Theme.cpp | 123 +++++++++++++++++++++++++++++++++------- 3 files changed, 127 insertions(+), 23 deletions(-) diff --git a/src/core/Path.cpp b/src/core/Path.cpp index 016dac36ef..67c60e59ec 100644 --- a/src/core/Path.cpp +++ b/src/core/Path.cpp @@ -5,6 +5,7 @@ extern "C" #include "../util/util.h" } +#include "Math.hpp" #include "Memory.hpp" #include "Path.hpp" #include "String.hpp" @@ -17,6 +18,29 @@ namespace Path return safe_strcat_path(buffer, src, bufferSize); } + utf8 * GetFileNameWithoutExtension(utf8 * buffer, size_t bufferSize, const utf8 * path) + { + const utf8 * lastDot = nullptr; + const utf8 * ch = path; + for (; ch != '\0'; ch++) + { + if (*ch == '.') + { + lastDot = ch; + } + } + + if (lastDot == nullptr) + { + return String::Set(buffer, bufferSize, path); + } + + size_t truncatedLength = Math::Min(bufferSize - 1, lastDot - path); + Memory::Copy(buffer, path, truncatedLength); + buffer[truncatedLength] = '\0'; + return buffer; + } + utf8 * GetAbsolute(utf8 *buffer, size_t bufferSize, const utf8 * relativePath) { #if __WINDOWS__ diff --git a/src/core/Path.hpp b/src/core/Path.hpp index cdc1bab230..5d35ee6d65 100644 --- a/src/core/Path.hpp +++ b/src/core/Path.hpp @@ -8,5 +8,6 @@ extern "C" namespace Path { utf8 * Append(utf8 * buffer, size_t bufferSize, const utf8 * src); - utf8 * GetAbsolute(utf8 *buffer, size_t bufferSize, const utf8 * relativePath); + utf8 * GetFileNameWithoutExtension(utf8 * buffer, size_t bufferSize, const utf8 * path); + utf8 * GetAbsolute(utf8 * buffer, size_t bufferSize, const utf8 * relativePath); } diff --git a/src/interface/Theme.cpp b/src/interface/Theme.cpp index a94ac6c9a6..52f52b9265 100644 --- a/src/interface/Theme.cpp +++ b/src/interface/Theme.cpp @@ -10,6 +10,7 @@ extern "C" #include "../core/List.hpp" #include "../core/Math.hpp" #include "../core/Memory.hpp" +#include "../core/Path.hpp" #include "../core/String.hpp" #include "../core/Util.hpp" @@ -36,14 +37,14 @@ struct WindowTheme /** * Represents the style for a particular type of window. */ -struct UIThemeEntry +struct UIThemeWindowEntry { rct_windowclass WindowClass; WindowTheme Theme; - json_t * ToJson() const; - static UIThemeEntry FromJson(const WindowThemeDesc * wtDesc, const json_t * json); + json_t * ToJson() const; + static UIThemeWindowEntry FromJson(const WindowThemeDesc * wtDesc, const json_t * json); }; /** @@ -52,22 +53,24 @@ struct UIThemeEntry class UITheme { public: - utf8 * Name; - List Entries; - uint8 Flags; + utf8 * Name; + List Entries; + uint8 Flags; UITheme(const utf8 * name); UITheme(const UITheme & name); ~UITheme(); - void SetEntry(const UIThemeEntry * entry); - void RemoveEntry(rct_windowclass windowClass); + const UIThemeWindowEntry * GetEntry(rct_windowclass windowClass) const; + void SetEntry(const UIThemeWindowEntry * entry); + void RemoveEntry(rct_windowclass windowClass); + json_t * ToJson() const; bool WriteToFile(const utf8 * path) const; static UITheme * FromJson(const json_t * json); static UITheme * FromFile(const utf8 * path); - static UITheme CreatePredefined(const utf8 * name, const UIThemeEntry * entries, uint8 flags); + static UITheme CreatePredefined(const utf8 * name, const UIThemeWindowEntry * entries, uint8 flags); }; /** @@ -159,7 +162,7 @@ WindowThemeDesc WindowThemeDescriptors[] = #define COLOURS_RCT1(c0, c1, c2, c3, c4, c5) { { (c0), (c1), (c2), (c3), (c4), (c5) } } -UIThemeEntry PredefinedThemeRCT1_Entries[] = +UIThemeWindowEntry PredefinedThemeRCT1_Entries[] = { { WC_TOP_TOOLBAR, COLOURS_RCT1(COLOUR_GREY, COLOUR_GREY, COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK) }, { WC_BOTTOM_TOOLBAR, COLOURS_RCT1(TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), COLOUR_BLACK, COLOUR_YELLOW, COLOUR_BLACK, COLOUR_BLACK) }, @@ -178,7 +181,7 @@ UIThemeEntry PredefinedThemeRCT1_Entries[] = THEME_DEF_END }; -UIThemeEntry PredefinedThemeRCT2_Entries[] = +UIThemeWindowEntry PredefinedThemeRCT2_Entries[] = { THEME_DEF_END }; @@ -232,7 +235,7 @@ static void ThrowThemeLoadException() #pragma region UIThemeEntry -json_t * UIThemeEntry::ToJson() const +json_t * UIThemeWindowEntry::ToJson() const { const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(WindowClass); @@ -248,7 +251,7 @@ json_t * UIThemeEntry::ToJson() const return jsonEntry; } -UIThemeEntry UIThemeEntry::FromJson(const WindowThemeDesc * wtDesc, const json_t * json) +UIThemeWindowEntry UIThemeWindowEntry::FromJson(const WindowThemeDesc * wtDesc, const json_t * json) { json_t * jsonColours = json_object_get(json, "colours"); if (jsonColours == nullptr) @@ -259,7 +262,7 @@ UIThemeEntry UIThemeEntry::FromJson(const WindowThemeDesc * wtDesc, const json_t uint8 numColours = (uint8)json_array_size(jsonColours); numColours = Math::Min(numColours, wtDesc->NumColours); - UIThemeEntry result; + UIThemeWindowEntry result; result.WindowClass = wtDesc->WindowClass; result.Theme = wtDesc->DefaultTheme; @@ -293,12 +296,25 @@ UITheme::~UITheme() Memory::Free(Name); } -void UITheme::SetEntry(const UIThemeEntry * newEntry) +const UIThemeWindowEntry * UITheme::GetEntry(rct_windowclass windowClass) const +{ + for (size_t i = 0; i < Entries.GetCount(); i++) + { + const UIThemeWindowEntry * entry = &Entries[i]; + if (entry->WindowClass == windowClass) + { + return entry; + } + } + return nullptr; +} + +void UITheme::SetEntry(const UIThemeWindowEntry * newEntry) { // Try to replace existing entry for (size_t i = 0; i < Entries.GetCount(); i++) { - UIThemeEntry * entry = &Entries[i]; + UIThemeWindowEntry * entry = &Entries[i]; if (entry->WindowClass == newEntry->WindowClass) { *entry = *newEntry; @@ -314,7 +330,7 @@ void UITheme::RemoveEntry(rct_windowclass windowClass) // Remove existing entry for (size_t i = 0; i < Entries.GetCount(); i++) { - UIThemeEntry * entry = &Entries[i]; + UIThemeWindowEntry * entry = &Entries[i]; if (entry->WindowClass == windowClass) { Entries.RemoveAt(i); @@ -327,7 +343,7 @@ json_t * UITheme::ToJson() const { // Create entries json_t * jsonEntries = json_object(); - for (const UIThemeEntry & entry : Entries) + for (const UIThemeWindowEntry & entry : Entries) { const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(entry.WindowClass); json_object_set_new(jsonEntries, wtDesc->WindowClassSZ, entry.ToJson()); @@ -407,7 +423,7 @@ UITheme * UITheme::FromJson(const json_t * json) const WindowThemeDesc * wtDesc = GetWindowThemeDescriptor(jkey); if (wtDesc == nullptr) continue; - UIThemeEntry entry = UIThemeEntry::FromJson(wtDesc, jvalue); + UIThemeWindowEntry entry = UIThemeWindowEntry::FromJson(wtDesc, jvalue); result->SetEntry(&entry); } @@ -438,19 +454,82 @@ UITheme * UITheme::FromFile(const utf8 * path) return result; } -UITheme UITheme::CreatePredefined(const utf8 * name, const UIThemeEntry * entries, uint8 flags) +UITheme UITheme::CreatePredefined(const utf8 * name, const UIThemeWindowEntry * entries, uint8 flags) { auto theme = UITheme(name); theme.Flags = flags | UITHEME_FLAG_PREDEFINED; size_t numEntries = 0; - for (const UIThemeEntry * entry = entries; entry->WindowClass != 255; entry++) + for (const UIThemeWindowEntry * entry = entries; entry->WindowClass != 255; entry++) { numEntries++; } - theme.Entries = List(entries, numEntries); + theme.Entries = List(entries, numEntries); return theme; } #pragma endregion + +namespace ThemeManager +{ + const utf8 * CurrentThemePath; + UITheme * CurrentTheme; + + struct AvailableTheme + { + utf8 Path[MAX_PATH]; + utf8 Name[64]; + }; + + void GetAvailableThemes(List * outThemes) + { + utf8 themesPattern[MAX_PATH]; + platform_get_user_directory(themesPattern, "themes"); + Path::Append(themesPattern, sizeof(themesPattern), "*.json"); + + utf8 path[MAX_PATH]; + int handle = platform_enumerate_files_begin(themesPattern); + while (platform_enumerate_directories_next(handle, path)) + { + AvailableTheme theme; + String::Set(theme.Path, sizeof(theme.Path), path); + Path::GetFileNameWithoutExtension(theme.Path, sizeof(theme.Path), path); + outThemes->Add(theme); + } + } +} + +extern "C" +{ + void colour_scheme_update(rct_window * window) + { + colour_scheme_update_by_class(window, window->classification); + } + + void colour_scheme_update_by_class(rct_window * window, rct_windowclass classification) + { + const WindowTheme * windowTheme; + const UIThemeWindowEntry * entry = ThemeManager::CurrentTheme->GetEntry(classification); + if (entry != nullptr) + { + windowTheme = &entry->Theme; + } + else + { + const WindowThemeDesc * desc = GetWindowThemeDescriptor(classification); + windowTheme = &desc->DefaultTheme; + } + + bool transparent = false; + for (int i = 0; i < 6; i++) { + window->colours[i] = windowTheme->Colours[i]; + if (windowTheme->Colours[i] & COLOUR_FLAG_TRANSLUCENT) { + transparent = true; + } + } + // Some windows need to be transparent even if the colours aren't. + // There doesn't seem to be any side-effects for all windows being transparent + window->flags |= WF_TRANSPARENT; + } +} From caf9bd9939f0df582235954c6db54006cfe985ef Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Fri, 29 Jan 2016 19:39:31 +0000 Subject: [PATCH 06/13] get game working with new theme manager Theme editor currently unavailable. --- src/config.c | 391 -------------------------- src/config.h | 21 -- src/core/Guard.hpp | 34 ++- src/core/List.hpp | 12 + src/core/Path.cpp | 9 + src/core/Path.hpp | 1 + src/core/String.cpp | 2 +- src/core/String.hpp | 2 + src/interface/Theme.cpp | 119 +++++++- src/interface/themes.c | 4 + src/interface/themes.h | 35 ++- src/localisation/localisation.c | 6 +- src/openrct2.c | 4 +- src/windows/editor_object_selection.c | 5 +- src/windows/options.c | 31 +- src/windows/park.c | 2 +- src/windows/ride.c | 4 +- src/windows/ride_construction.c | 1 + src/windows/ride_list.c | 2 +- src/windows/server_list.c | 1 + src/windows/themes.c | 8 + src/windows/title_scenarioselect.c | 6 +- 22 files changed, 233 insertions(+), 467 deletions(-) diff --git a/src/config.c b/src/config.c index e0982e42ae..2eb1f44c9a 100644 --- a/src/config.c +++ b/src/config.c @@ -299,7 +299,6 @@ sound_configuration gConfigSound; twitch_configuration gConfigTwitch; network_configuration gConfigNetwork; notification_configuration gConfigNotifications; -themes_configuration gConfigThemes; title_sequences_configuration gConfigTitleSequences; static bool config_open(const utf8string path); @@ -1044,396 +1043,6 @@ bool config_shortcut_keys_save() #pragma endregion - -#pragma region Themes - -typedef struct { - size_t offset; - const_utf8string property_name; - uint8 type; - value_union default_value; - config_enum_definition *enum_definitions; -} theme_property_definition; - -typedef struct { - size_t offset; - const_utf8string section_name; - theme_property_definition *property_definitions; - int property_definitions_count; -} theme_section_definition; - - -theme_property_definition _themeWindowDefinitions[] = { - { offsetof(theme_window, colours[0]), "colour_0", CONFIG_VALUE_TYPE_UINT8, 0, NULL }, - { offsetof(theme_window, colours[1]), "colour_1", CONFIG_VALUE_TYPE_UINT8, 0, NULL }, - { offsetof(theme_window, colours[2]), "colour_2", CONFIG_VALUE_TYPE_UINT8, 0, NULL }, - { offsetof(theme_window, colours[3]), "colour_3", CONFIG_VALUE_TYPE_UINT8, 0, NULL }, - { offsetof(theme_window, colours[4]), "colour_4", CONFIG_VALUE_TYPE_UINT8, 0, NULL }, - { offsetof(theme_window, colours[5]), "colour_5", CONFIG_VALUE_TYPE_UINT8, 0, NULL }, -}; - -theme_property_definition _themeFeaturesDefinitions[] = { - { offsetof(theme_features, rct1_ride_lights), "rct1_ride_lights", CONFIG_VALUE_TYPE_BOOLEAN, false, NULL }, - { offsetof(theme_features, rct1_park_lights), "rct1_park_lights", CONFIG_VALUE_TYPE_BOOLEAN, false, NULL }, - { offsetof(theme_features, rct1_scenario_font), "rct1_scenario_font", CONFIG_VALUE_TYPE_BOOLEAN, false, NULL }, -}; - - -theme_section_definition _themeSectionDefinitions[] = { - // Special definition for theme windows - { 0, "", _themeWindowDefinitions, countof(_themeWindowDefinitions) }, - { offsetof(theme_preset, features), "features", _themeFeaturesDefinitions, countof(_themeFeaturesDefinitions) }, -}; - -static bool themes_open(const_utf8string path); -static bool themes_save(const_utf8string path, int preset); -static void themes_read_properties(theme_preset *theme, theme_section_definition **currentSection, utf8string line); -static void themes_set_property(theme_preset *theme, const theme_section_definition *section, const theme_property_definition *property, utf8string value, int valueSize); -static theme_section_definition* themes_get_section_def(utf8string name, int size); -static theme_property_definition *themes_get_property_def(theme_section_definition *section, utf8string name, int size); - -void themes_set_default() -{ - utf8 path[MAX_PATH]; - - platform_get_user_directory(path, "themes"); - platform_ensure_directory_exists(path); - - gConfigThemes.num_presets = 2; - gConfigThemes.presets = malloc(sizeof(theme_preset) * gConfigThemes.num_presets); - - // Set RCT2 theme - safe_strcpy(gConfigThemes.presets[0].name, language_get_string(2741), THEME_PRESET_NAME_SIZE); - gConfigThemes.presets[0].windows = malloc(sizeof(theme_window) * gNumThemeWindows); - - // Define the defaults for RCT2 here - gConfigThemes.presets[0].features.rct1_ride_lights = false; - gConfigThemes.presets[0].features.rct1_park_lights = false; - gConfigThemes.presets[0].features.rct1_scenario_font = false; - - - for (int i = 0; i < (int)gNumThemeWindows; i++) { - gConfigThemes.presets[0].windows[i] = gThemeWindowDefinitions[i].window; - } - - // Set RCT1 theme - safe_strcpy(gConfigThemes.presets[1].name, language_get_string(2740), THEME_PRESET_NAME_SIZE); - gConfigThemes.presets[1].windows = malloc(sizeof(theme_window) * gNumThemeWindows); - - // Define the defaults for RCT1 here - gConfigThemes.presets[1].features.rct1_ride_lights = true; - gConfigThemes.presets[1].features.rct1_park_lights = true; - gConfigThemes.presets[1].features.rct1_scenario_font = true; - - - for (int i = 0; i < (int)gNumThemeWindows; i++) { - uint8 changed_colour = 0xFF; - for (int k = 0; gThemeWindowsRCT1[k].classification != 0xFF; k++) { - if (gThemeWindowsRCT1[k].classification == gThemeWindowDefinitions[i].classification) { - changed_colour = (uint8)k; - break; - } - } - gConfigThemes.presets[1].windows[i] = (changed_colour != 0xFF ? gThemeWindowsRCT1[changed_colour].window : gThemeWindowDefinitions[i].window); - } - } - -void themes_load_presets() -{ - utf8 path[MAX_PATH]; - file_info file; - int fileEnumHandle, i; - - platform_get_user_directory(path, "themes"); - strcat(path, "*.ini"); - fileEnumHandle = platform_enumerate_files_begin(path); - while (platform_enumerate_files_next(fileEnumHandle, &file)) { - platform_get_user_directory(path, "themes"); - strcat(path, file.path); - themes_open(path); - } - platform_enumerate_files_end(fileEnumHandle); - - if (strcmp(gConfigInterface.current_theme_preset, "*RCT2") == 0) { - theme_change_preset(0); - } - else if (strcmp(gConfigInterface.current_theme_preset, "*RCT1") == 0) { - theme_change_preset(1); - } - else { - for (i = 2; i < gConfigThemes.num_presets; i++) { - if (strcmp(gConfigInterface.current_theme_preset, gConfigThemes.presets[i].name) == 0) { - theme_change_preset(i); - break; - } - } - if (i == gConfigThemes.num_presets) { - theme_change_preset(0); - } - } -} - -bool themes_save_preset(int preset) -{ - utf8 path[MAX_PATH]; - - platform_get_user_directory(path, "themes"); - strcat(path, gConfigThemes.presets[preset].name); - strcat(path, ".ini"); - if (themes_save(path, preset)) { - return true; - } - - return false; -} - -bool themes_open(const_utf8string path) -{ - SDL_RWops *file; - utf8string lineBuffer; - size_t lineBufferCapacity; - size_t lineLength; - int c, preset; - theme_section_definition *currentSection; - - file = SDL_RWFromFile(path, "rb"); - if (file == NULL) - return false; - - // Check if the colour scheme is already loaded - // No nead to read the first two presets as they're hardcoded in - for (preset = 2; preset < gConfigThemes.num_presets; preset++) { - if (strcmp(path, gConfigThemes.presets[preset].name) == 0) { - break; - } - } - // Otherwise allocate one - if (preset == gConfigThemes.num_presets) { - gConfigThemes.num_presets++; - gConfigThemes.presets = realloc(gConfigThemes.presets, sizeof(theme_preset) * gConfigThemes.num_presets); - safe_strcpy(gConfigThemes.presets[preset].name, path_get_filename(path), THEME_PRESET_NAME_SIZE); - path_remove_extension(gConfigThemes.presets[preset].name); - gConfigThemes.presets[preset].windows = malloc(sizeof(theme_window) * gNumThemeWindows); - gConfigThemes.presets[preset].features.rct1_ride_lights = false; - gConfigThemes.presets[preset].features.rct1_park_lights = false; - gConfigThemes.presets[preset].features.rct1_scenario_font = false; - for (int i = 0; i < (int)gNumThemeWindows; i++) { - gConfigThemes.presets[preset].windows[i] = gThemeWindowDefinitions[i].window; - } - } - - currentSection = NULL; - lineBufferCapacity = 64; - lineBuffer = malloc(lineBufferCapacity); - lineLength = 0; - - // Skim UTF-8 byte order mark - SDL_RWread(file, lineBuffer, 3, 1); - if (!(lineBuffer[0] == (utf8)0xEF && lineBuffer[1] == (utf8)0xBB && lineBuffer[2] == (utf8)0xBF)) - SDL_RWseek(file, 0, SEEK_SET); - - while ((c = rwopsreadc(file)) != EOF) { - if (c == '\n' || c == '\r') { - lineBuffer[lineLength++] = 0; - themes_read_properties(&gConfigThemes.presets[preset], ¤tSection, (utf8string)lineBuffer); - lineLength = 0; - } - else { - lineBuffer[lineLength++] = c; - } - - if (lineLength >= lineBufferCapacity) { - lineBufferCapacity *= 2; - lineBuffer = realloc(lineBuffer, lineBufferCapacity); - } - } - - if (lineLength > 0) { - lineBuffer[lineLength++] = 0; - themes_read_properties(&gConfigThemes.presets[preset], ¤tSection, lineBuffer); - } - - free(lineBuffer); - SDL_RWclose(file); - return true; -} - -static bool themes_save(const_utf8string path, int preset) -{ - SDL_RWops *file; - int i, j; - value_union *value; - - file = SDL_RWFromFile(path, "wb"); - if (file == NULL) { - log_error("Unable to write to theme file."); - return false; - } - - // Skip the window definition, we'll do that after - for (i = 1; i < countof(_themeSectionDefinitions); i++) { - theme_section_definition *section = &_themeSectionDefinitions[i]; - - rwopswritec(file, '['); - rwopswritestr(file, section->section_name); - rwopswritec(file, ']'); - rwopswritenewline(file); - - for (j = 0; j < section->property_definitions_count; j++) { - theme_property_definition *property = §ion->property_definitions[j]; - - rwopswritestr(file, property->property_name); - rwopswritestr(file, " = "); - - value = (value_union*)((size_t)&gConfigThemes.presets[preset] + (size_t)section->offset + (size_t)property->offset); - - if (property->enum_definitions != NULL) - config_write_enum(file, property->type, value, property->enum_definitions); - else - config_save_property_value(file, property->type, value); - rwopswritenewline(file); - } - rwopswritenewline(file); - } - - for (i = 0; i < (int)gNumThemeWindows; i++) { - theme_section_definition *section = &_themeSectionDefinitions[0]; - - rwopswritec(file, '['); - rwopswritestr(file, gThemeWindowDefinitions[i].section_name); - rwopswritec(file, ']'); - rwopswritenewline(file); - - for (j = 0; j < section->property_definitions_count; j++) { - theme_property_definition *property = §ion->property_definitions[j]; - - rwopswritestr(file, property->property_name); - rwopswritestr(file, " = "); - - value = (value_union*)((size_t)gConfigThemes.presets[preset].windows + (size_t)(sizeof(theme_window) * i) + (size_t)property->offset); - - if (property->enum_definitions != NULL) - config_write_enum(file, property->type, value, property->enum_definitions); - else - config_save_property_value(file, property->type, value); - rwopswritenewline(file); - } - } - - SDL_RWclose(file); - return true; -} - - -static void themes_read_properties(theme_preset *theme, theme_section_definition **currentSection, utf8string line) -{ - utf8 *ch = (utf8*)line; - utf8_skip_whitespace(&ch); - - if (*ch == '[') { - const utf8 *sectionName; - int sectionNameSize; - if (config_get_section(ch, §ionName, §ionNameSize)) - *currentSection = themes_get_section_def((utf8string)sectionName, sectionNameSize); - } else { - if (*currentSection != NULL) { - utf8 *propertyName, *value; - int propertyNameSize, valueSize; - if (config_get_property_name_value(ch, &propertyName, &propertyNameSize, &value, &valueSize)) { - theme_property_definition *property; - property = themes_get_property_def(*currentSection, propertyName, propertyNameSize); - if (property != NULL) - themes_set_property(theme, *currentSection, property, value, valueSize); - } - } - } -} -static void themes_set_property(theme_preset *theme, const theme_section_definition *section, const theme_property_definition *property, utf8string value, int valueSize) -{ - value_union *destValue = (value_union*)((size_t)theme + (size_t)section->offset + (size_t)property->offset); - - // Get getting the address from the windows pointer instead - if (section == &_themeSectionDefinitions[0]) - destValue = (value_union*)((size_t)theme->windows + (size_t)section->offset + (size_t)property->offset); - - if (property->enum_definitions != NULL) - if (config_read_enum(destValue, _configValueTypeSize[property->type], value, valueSize, property->enum_definitions)) - return; - - switch (property->type) { - case CONFIG_VALUE_TYPE_BOOLEAN: - if (_strnicmp(value, "false", valueSize) == 0) destValue->value_boolean = false; - else if (_strnicmp(value, "true", valueSize) == 0) destValue->value_boolean = true; - else destValue->value_boolean = strtol(value, NULL, 0) != 0; - break; - case CONFIG_VALUE_TYPE_UINT8: - destValue->value_uint8 = (uint8)strtol(value, NULL, 0); - break; - case CONFIG_VALUE_TYPE_UINT16: - destValue->value_uint16 = (uint16)strtol(value, NULL, 0); - break; - case CONFIG_VALUE_TYPE_UINT32: - destValue->value_uint32 = (uint32)strtol(value, NULL, 0); - break; - case CONFIG_VALUE_TYPE_SINT8: - destValue->value_sint8 = (sint8)strtol(value, NULL, 0); - break; - case CONFIG_VALUE_TYPE_SINT16: - destValue->value_sint16 = (sint16)strtol(value, NULL, 0); - break; - case CONFIG_VALUE_TYPE_SINT32: - destValue->value_sint32 = (sint32)strtol(value, NULL, 0); - break; - case CONFIG_VALUE_TYPE_FLOAT: - destValue->value_float = strtof(value, NULL); - break; - case CONFIG_VALUE_TYPE_DOUBLE: - destValue->value_double = strtod(value, NULL); - break; - case CONFIG_VALUE_TYPE_STRING: - SafeFree(destValue->value_string); - destValue->value_string = malloc(valueSize + 1); - memcpy(destValue->value_string, value, valueSize); - destValue->value_string[valueSize] = 0; - break; - } -} -static theme_section_definition* themes_get_section_def(utf8string name, int size) -{ - int i; - - // Skip the special definition - for (i = 1; i < countof(_themeSectionDefinitions); i++) { - const_utf8string sectionName = _themeSectionDefinitions[i].section_name; - if (sectionName[size] == 0 && _strnicmp(sectionName, name, size) == 0) - return &_themeSectionDefinitions[i]; - } - // Check for window definitions - for (i = 0; i < (int)gNumThemeWindows; i++) { - const_utf8string sectionName = gThemeWindowDefinitions[i].section_name; - if (sectionName[size] == 0 && _strnicmp(sectionName, name, size) == 0) { - _themeSectionDefinitions[0].offset = (size_t)(sizeof(theme_window) * i); - return &_themeSectionDefinitions[0]; - } - } - - return NULL; - } -static theme_property_definition *themes_get_property_def(theme_section_definition *section, utf8string name, int size) -{ - int i; - - for (i = 0; i < section->property_definitions_count; i++) { - const_utf8string propertyName = section->property_definitions[i].property_name; - if (propertyName[size] == 0 && _strnicmp(propertyName, name, size) == 0) - return §ion->property_definitions[i]; - } - - return NULL; -} - -#pragma endregion - #pragma region Title Sequences static void title_sequence_open(const char *path, const char *customName); diff --git a/src/config.h b/src/config.h index b94fcb2b22..60f733f26c 100644 --- a/src/config.h +++ b/src/config.h @@ -267,22 +267,6 @@ typedef struct { uint8 rct1_scenario_font; } theme_features; -#define THEME_PRESET_NAME_SIZE 256 - -typedef struct theme_preset { - char name[THEME_PRESET_NAME_SIZE]; - theme_window *windows; - - // Add structures for any other settings here - theme_features features; - -} theme_preset; - -typedef struct { - theme_preset *presets; - uint16 num_presets; -} themes_configuration; - #define TITLE_SEQUENCE_MAX_SAVE_LENGTH 51 typedef struct { @@ -327,7 +311,6 @@ extern sound_configuration gConfigSound; extern twitch_configuration gConfigTwitch; extern network_configuration gConfigNetwork; extern notification_configuration gConfigNotifications; -extern themes_configuration gConfigThemes; extern title_sequences_configuration gConfigTitleSequences; extern uint16 gShortcutKeys[SHORTCUT_COUNT]; @@ -346,10 +329,6 @@ bool config_shortcut_keys_save(); bool config_find_or_browse_install_directory(); -void themes_set_default(); -void themes_load_presets(); -bool themes_save_preset(int preset); - void title_sequences_set_default(); void title_sequences_load_presets(); void title_sequence_save_preset_script(int preset); diff --git a/src/core/Guard.hpp b/src/core/Guard.hpp index 1f8cff8033..8556ce215a 100644 --- a/src/core/Guard.hpp +++ b/src/core/Guard.hpp @@ -3,6 +3,7 @@ #include #include +#include "Console.hpp" #include "Diagnostics.hpp" /** @@ -10,13 +11,30 @@ */ namespace Guard { - template - void ArgumentInRange(T argument, T min, T max, const char *message = nullptr) - { - Debug::Break(); + void Assert(bool expression, const char * message = nullptr) + { + if (expression) return; - fputs(message, stderr); - fputc('\n', stderr); - assert(argument < min || argument > max); - } + if (message != nullptr) + { + Console::Error::WriteLine(message); + } + +#if DEBUG + Debug::Break(); +#endif + assert(false); + } + + template + void ArgumentNotNull(T * argument, const char * message = nullptr) + { + Assert(argument != nullptr, message); + } + + template + void ArgumentInRange(T argument, T min, T max, const char * message = nullptr) + { + Assert(argument >= min && argument <= max, message); + } }; diff --git a/src/core/List.hpp b/src/core/List.hpp index 0d8385238e..2c750f4833 100644 --- a/src/core/List.hpp +++ b/src/core/List.hpp @@ -73,4 +73,16 @@ public: { return Memory::DuplicateArray(this->data(), this->size()); } + + const_reference operator[](size_t index) const + { + Guard::ArgumentInRange(index, (size_t)0, this->size() - 1); + return std::vector::operator[](index); + } + + reference operator[](size_t index) + { + Guard::ArgumentInRange(index, (size_t)0, this->size() - 1); + return std::vector::operator[](index); + } }; diff --git a/src/core/Path.cpp b/src/core/Path.cpp index 67c60e59ec..90b8e0f031 100644 --- a/src/core/Path.cpp +++ b/src/core/Path.cpp @@ -73,4 +73,13 @@ namespace Path } #endif } + + bool Equals(const utf8 * a, const utf8 * b) + { + bool ignoreCase = false; +#if __WINDOWS__ + ignoreCase = true; +#endif + return String::Equals(a, b, ignoreCase); + } } diff --git a/src/core/Path.hpp b/src/core/Path.hpp index 5d35ee6d65..1186f86e51 100644 --- a/src/core/Path.hpp +++ b/src/core/Path.hpp @@ -10,4 +10,5 @@ namespace Path utf8 * Append(utf8 * buffer, size_t bufferSize, const utf8 * src); utf8 * GetFileNameWithoutExtension(utf8 * buffer, size_t bufferSize, const utf8 * path); utf8 * GetAbsolute(utf8 * buffer, size_t bufferSize, const utf8 * relativePath); + bool Equals(const utf8 * a, const utf8 * b); } diff --git a/src/core/String.cpp b/src/core/String.cpp index 4252cab76c..ba000bc7f7 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -144,7 +144,7 @@ namespace String utf8 * SkipBOM(utf8 * buffer) { - return (utf8*)SkipBOM(buffer); + return (utf8*)SkipBOM((const utf8 *)buffer); } const utf8 * SkipBOM(const utf8 * buffer) diff --git a/src/core/String.hpp b/src/core/String.hpp index f60998ce19..c286ebd27e 100644 --- a/src/core/String.hpp +++ b/src/core/String.hpp @@ -7,6 +7,8 @@ extern "C" namespace String { + constexpr utf8 * Empty = ""; + bool Equals(const utf8 * a, const utf8 * b, bool ignoreCase = false); bool StartsWith(const utf8 * str, const utf8 * match, bool ignoreCase = false); diff --git a/src/interface/Theme.cpp b/src/interface/Theme.cpp index 52f52b9265..f40961a88c 100644 --- a/src/interface/Theme.cpp +++ b/src/interface/Theme.cpp @@ -3,6 +3,7 @@ extern "C" { #include "../common.h" + #include "themes.h" #include "window.h" } @@ -19,13 +20,6 @@ struct WindowThemeDesc; // Don't try to load theme files that exceed 64 MiB constexpr uint64 MAX_THEME_SIZE = 64 * 1024 * 1024; -enum { - UITHEME_FLAG_PREDEFINED = 1 << 0, - UITHEME_FLAG_USE_LIGHTS_RIDE = 1 << 1, - UITHEME_FLAG_USE_LIGHTS_PARK = 1 << 2, - UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT = 1 << 3, -}; - /** * Represents a window theming style such as the colour scheme. */ @@ -473,17 +467,35 @@ UITheme UITheme::CreatePredefined(const utf8 * name, const UIThemeWindowEntry * namespace ThemeManager { - const utf8 * CurrentThemePath; - UITheme * CurrentTheme; - struct AvailableTheme { utf8 Path[MAX_PATH]; utf8 Name[64]; }; + const utf8 * CurrentThemePath; + UITheme * CurrentTheme; + List AvailableThemes; + size_t ActiveAvailableThemeIndex = SIZE_MAX; + size_t NumPredefinedThemes = 0; + void GetAvailableThemes(List * outThemes) { + Guard::ArgumentNotNull(outThemes); + + outThemes->Clear(); + + NumPredefinedThemes = 0; + for (const UITheme * * predefinedTheme = PredefinedThemes; *predefinedTheme != nullptr; predefinedTheme++) + { + AvailableTheme theme; + String::Set(theme.Path, sizeof(theme.Path), String::Empty); + String::Set(theme.Name, sizeof(theme.Name), (*predefinedTheme)->Name); + outThemes->Add(theme); + + NumPredefinedThemes++; + } + utf8 themesPattern[MAX_PATH]; platform_get_user_directory(themesPattern, "themes"); Path::Append(themesPattern, sizeof(themesPattern), "*.json"); @@ -496,12 +508,99 @@ namespace ThemeManager String::Set(theme.Path, sizeof(theme.Path), path); Path::GetFileNameWithoutExtension(theme.Path, sizeof(theme.Path), path); outThemes->Add(theme); + + if (Path::Equals(CurrentThemePath, path)) + { + ActiveAvailableThemeIndex = outThemes->GetCount() - 1; + } } } + + void LoadTheme(UITheme * theme) + { + if (CurrentTheme != nullptr) + { + if (!(theme->Flags & UITHEME_FLAG_PREDEFINED)) + { + delete CurrentTheme; + } + } + + CurrentTheme = theme; + + gfx_invalidate_screen(); + } + + void LoadTheme(const utf8 * path) + { + UITheme * theme = UITheme::FromFile(path); + if (theme == nullptr) + { + // Fall-back to default + theme = (UITheme *)&PredefinedThemeRCT2; + } + LoadTheme(theme); + } + + void Initialise() + { + ThemeManager::GetAvailableThemes(&ThemeManager::AvailableThemes); + LoadTheme((UITheme *)&PredefinedThemeRCT2); + ActiveAvailableThemeIndex = 1; + } } extern "C" { + void theme_manager_load_available_themes() + { + ThemeManager::GetAvailableThemes(&ThemeManager::AvailableThemes); + } + + size_t theme_manager_get_num_available_themes() + { + return ThemeManager::AvailableThemes.GetCount(); + } + + const utf8 * theme_manager_get_available_theme_path(size_t index) + { + return ThemeManager::AvailableThemes[index].Path; + } + + const utf8 * theme_manager_get_available_theme_name(size_t index) + { + return ThemeManager::AvailableThemes[index].Name; + } + + size_t theme_manager_get_active_available_theme_index() + { + return ThemeManager::ActiveAvailableThemeIndex; + } + + void theme_manager_set_active_available_theme(size_t index) + { + if (index < ThemeManager::NumPredefinedThemes) + { + ThemeManager::LoadTheme((UITheme *)PredefinedThemes[index]); + } + else + { + const utf8 * path = ThemeManager::AvailableThemes[index].Path; + ThemeManager::LoadTheme(path); + } + ThemeManager::ActiveAvailableThemeIndex = index; + } + + uint8 theme_get_flags() + { + return ThemeManager::CurrentTheme->Flags; + } + + void theme_manager_initialise() + { + ThemeManager::Initialise(); + } + void colour_scheme_update(rct_window * window) { colour_scheme_update_by_class(window, window->classification); diff --git a/src/interface/themes.c b/src/interface/themes.c index 54d1891b58..2fb4d36e5a 100644 --- a/src/interface/themes.c +++ b/src/interface/themes.c @@ -1,3 +1,5 @@ +#if 0 + /***************************************************************************** * Copyright (c) 2014 Ted John, Peter Hill * OpenRCT2, an open source clone of Roller Coaster Tycoon 2. @@ -249,3 +251,5 @@ void theme_rename_preset(int preset, const char *newName) } } } + +#endif diff --git a/src/interface/themes.h b/src/interface/themes.h index 2c9acd3671..fb5feda672 100644 --- a/src/interface/themes.h +++ b/src/interface/themes.h @@ -18,11 +18,37 @@ * along with this program. If not, see . *****************************************************************************/ -#ifndef _COLOUR_SCHEMES_H_ -#define _COLOUR_SCHEMES_H_ +#ifndef _THEMES_H_ +#define _THEMES_H_ #include "../common.h" #include "window.h" + +enum { + UITHEME_FLAG_PREDEFINED = 1 << 0, + UITHEME_FLAG_USE_LIGHTS_RIDE = 1 << 1, + UITHEME_FLAG_USE_LIGHTS_PARK = 1 << 2, + UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT = 1 << 3, +}; + +void colour_scheme_update(rct_window *window); +void colour_scheme_update_by_class(rct_window *window, rct_windowclass classification); + +void theme_manager_initialise(); +void theme_manager_load_available_themes(); +size_t theme_manager_get_num_available_themes(); +const utf8 * theme_manager_get_available_theme_path(size_t index); +const utf8 * theme_manager_get_available_theme_name(size_t index); +size_t theme_manager_get_active_available_theme_index(); +void theme_manager_set_active_available_theme(size_t index); + +uint8 theme_get_flags(); + + +#if 0 + + + #include "../config.h" typedef struct { @@ -52,13 +78,12 @@ theme_preset* theme_get_preset(); theme_window_definition* theme_window_definition_get_by_class(rct_windowclass classification); theme_window* theme_window_get_by_class(rct_windowclass classification); -void colour_scheme_update(rct_window *window); -void colour_scheme_update_by_class(rct_window *window, rct_windowclass classification); - void theme_change_preset(int preset); void theme_create_preset(int duplicate, const char *name); void theme_delete_preset(int preset); void theme_rename_preset(int preset, const char *newName); +#endif + #endif diff --git a/src/localisation/localisation.c b/src/localisation/localisation.c index 42ec0dcfd1..40b5760184 100644 --- a/src/localisation/localisation.c +++ b/src/localisation/localisation.c @@ -662,8 +662,10 @@ void format_string_code(unsigned int format_code, char **dest, char **args) value = *((uint32*)*args); *args += 4; - strcpy(*dest, (char*)value); - *dest += strlen(*dest); + if (value != 0) { + strcpy(*dest, (char*)value); + *dest += strlen(*dest); + } break; case FORMAT_MONTHYEAR: // Pop argument diff --git a/src/openrct2.c b/src/openrct2.c index 8cc31ed9bb..b550d93581 100644 --- a/src/openrct2.c +++ b/src/openrct2.c @@ -26,6 +26,7 @@ #include "game.h" #include "hook.h" #include "interface/chat.h" +#include "interface/themes.h" #include "interface/window.h" #include "interface/viewport.h" #include "localisation/localisation.h" @@ -229,8 +230,7 @@ bool openrct2_initialise() } http_init(); - themes_set_default(); - themes_load_presets(); + theme_manager_initialise(); title_sequences_set_default(); title_sequences_load_presets(); diff --git a/src/windows/editor_object_selection.c b/src/windows/editor_object_selection.c index 9c81989ef7..2d0476f39a 100644 --- a/src/windows/editor_object_selection.c +++ b/src/windows/editor_object_selection.c @@ -21,6 +21,7 @@ #include "../addresses.h" #include "../audio/audio.h" +#include "../config.h" #include "../game.h" #include "../interface/themes.h" #include "../interface/widget.h" @@ -33,10 +34,10 @@ #include "../ride/ride_data.h" #include "../ride/track.h" #include "../scenario.h" -#include "dropdown.h" -#include "error.h" #include "../util/util.h" #include "../world/footpath.h" +#include "dropdown.h" +#include "error.h" enum { FILTER_RCT2 = (1 << 0), diff --git a/src/windows/options.c b/src/windows/options.c index f7224468b4..960fcf8bff 100644 --- a/src/windows/options.c +++ b/src/windows/options.c @@ -989,16 +989,12 @@ static void window_options_mousedown(int widgetIndex, rct_window*w, rct_widget* case WINDOW_OPTIONS_PAGE_CONTROLS_AND_INTERFACE: switch (widgetIndex) { case WIDX_THEMES_DROPDOWN: - num_items = gConfigThemes.num_presets; + theme_manager_get_num_available_themes(); + num_items = (int)theme_manager_get_num_available_themes(); - gDropdownItemsFormat[0] = 2777; - gDropdownItemsArgs[0] = (uint32)&gConfigThemes.presets[1].name; - gDropdownItemsFormat[1] = 2777; - gDropdownItemsArgs[1] = (uint32)&gConfigThemes.presets[0].name; - - for (i = 2; i < num_items; i++) { + for (int i = 0; i < num_items; i++) { gDropdownItemsFormat[i] = 2777; - gDropdownItemsArgs[i] = (uint32)&gConfigThemes.presets[i].name; + gDropdownItemsArgs[i] = (uint32)theme_manager_get_available_theme_name(i); } window_dropdown_show_text_custom_width( @@ -1009,13 +1005,10 @@ static void window_options_mousedown(int widgetIndex, rct_window*w, rct_widget* DROPDOWN_FLAG_STAY_OPEN, num_items, widget->right - widget->left - 3 - ); + ); - if (gCurrentTheme == 0 || gCurrentTheme == 1) { - dropdown_set_checked(gCurrentTheme ^ 1, true); - } else { - dropdown_set_checked(gCurrentTheme, true); - } + dropdown_set_checked(theme_manager_get_active_available_theme_index(), true); + widget_invalidate(w, WIDX_THEMES_DROPDOWN); break; case WIDX_SCENARIO_GROUPING_DROPDOWN: @@ -1243,9 +1236,7 @@ static void window_options_dropdown(rct_window *w, int widgetIndex, int dropdown switch (widgetIndex) { case WIDX_THEMES_DROPDOWN: if (dropdownIndex != -1) { - if (dropdownIndex == 0 || dropdownIndex == 1) - dropdownIndex ^= 1; - theme_change_preset(dropdownIndex); + theme_manager_set_active_available_theme(dropdownIndex); } config_save_default(); break; @@ -1642,7 +1633,11 @@ static void window_options_paint(rct_window *w, rct_drawpixelinfo *dpi) break; case WINDOW_OPTIONS_PAGE_CONTROLS_AND_INTERFACE: gfx_draw_string_left(dpi, STR_SHOW_TOOLBAR_BUTTONS_FOR, w, w->colours[1], w->x + 10, w->y + window_options_controls_and_interface_widgets[WIDX_TOOLBAR_BUTTONS_GROUP].top + 15); - RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS + 0, uint32) = (uint32)&gConfigThemes.presets[gCurrentTheme].name; + + int activeAvailableThemeIndex = theme_manager_get_active_available_theme_index(); + const utf8 * activeThemeName = theme_manager_get_available_theme_name(activeAvailableThemeIndex); + RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS + 0, uint32) = (uint32)activeThemeName; + gfx_draw_string_left(dpi, 5238, NULL, w->colours[1], w->x + 10, w->y + window_options_controls_and_interface_widgets[WIDX_THEMES].top + 1); gfx_draw_string_left_clipped( dpi, diff --git a/src/windows/park.c b/src/windows/park.c index f8f408f14d..00923ef1f7 100644 --- a/src/windows/park.c +++ b/src/windows/park.c @@ -979,7 +979,7 @@ static void window_park_entrance_invalidate(rct_window *w) window_park_entrance_widgets[WIDX_STATUS].top = w->height - 13; window_park_entrance_widgets[WIDX_STATUS].bottom = w->height - 3; - if (theme_get_preset()->features.rct1_park_lights) { + if (theme_get_flags() & UITHEME_FLAG_USE_LIGHTS_PARK) { window_park_entrance_widgets[WIDX_OPEN_OR_CLOSE].type = WWT_EMPTY; if (RCT2_GLOBAL(RCT2_ADDRESS_OBJECTIVE_TYPE, uint8) == OBJECTIVE_GUESTS_AND_RATING) { window_park_entrance_widgets[WIDX_CLOSE_LIGHT].type = WWT_FLATBTN; diff --git a/src/windows/ride.c b/src/windows/ride.c index 2d47e7bbb4..5d9dfe3d96 100644 --- a/src/windows/ride.c +++ b/src/windows/ride.c @@ -1696,7 +1696,7 @@ static void window_ride_main_resize(rct_window *w) w->flags |= WF_RESIZABLE; int minHeight = 180; - if (theme_get_preset()->features.rct1_ride_lights) + if (theme_get_flags() & UITHEME_FLAG_USE_LIGHTS_RIDE) minHeight = 200 + RCT1_LIGHT_OFFSET - (ride_type_has_flag(get_ride(w->number)->type, RIDE_TYPE_FLAG_NO_TEST_MODE) ? 14 : 0); window_set_resize(w, 316, minHeight, 500, 450); @@ -2037,7 +2037,7 @@ static void window_ride_main_invalidate(rct_window *w) window_align_tabs(w, WIDX_TAB_1, WIDX_TAB_10); - if (theme_get_preset()->features.rct1_ride_lights) { + if (theme_get_flags() & UITHEME_FLAG_USE_LIGHTS_RIDE) { window_ride_main_widgets[WIDX_OPEN].type = WWT_EMPTY; window_ride_main_widgets[WIDX_CLOSE_LIGHT].type = WWT_IMGBTN; window_ride_main_widgets[WIDX_TEST_LIGHT].type = (ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_NO_TEST_MODE) ? WWT_EMPTY : WWT_IMGBTN); diff --git a/src/windows/ride_construction.c b/src/windows/ride_construction.c index 60766f810a..0481b677ae 100644 --- a/src/windows/ride_construction.c +++ b/src/windows/ride_construction.c @@ -21,6 +21,7 @@ #include "../addresses.h" #include "../audio/audio.h" #include "../cheats.h" +#include "../config.h" #include "../drawing/drawing.h" #include "../game.h" #include "../input.h" diff --git a/src/windows/ride_list.c b/src/windows/ride_list.c index 928dcf8184..a716625af6 100644 --- a/src/windows/ride_list.c +++ b/src/windows/ride_list.c @@ -403,7 +403,7 @@ static void window_ride_list_invalidate(rct_window *w) w->widgets[WIDX_OPEN_LIGHT].right = w->width - 7; w->widgets[WIDX_OPEN_LIGHT].left = w->width - 20; - if (theme_get_preset()->features.rct1_ride_lights) { + if (theme_get_flags() & UITHEME_FLAG_USE_LIGHTS_RIDE) { w->widgets[WIDX_OPEN_CLOSE_ALL].type = WWT_EMPTY; w->widgets[WIDX_CLOSE_LIGHT].type = WWT_IMGBTN; w->widgets[WIDX_OPEN_LIGHT].type = WWT_IMGBTN; diff --git a/src/windows/server_list.c b/src/windows/server_list.c index 062288c5d1..8d17f43184 100644 --- a/src/windows/server_list.c +++ b/src/windows/server_list.c @@ -18,6 +18,7 @@ * along with this program. If not, see . *****************************************************************************/ +#include "../config.h" #include "../interface/colour.h" #include "../interface/themes.h" #include "../interface/widget.h" diff --git a/src/windows/themes.c b/src/windows/themes.c index 51694d493d..6e9da3803d 100644 --- a/src/windows/themes.c +++ b/src/windows/themes.c @@ -36,6 +36,8 @@ #include "error.h" #include "../util/util.h" +#if 0 + enum { WINDOW_THEMES_TAB_SETTINGS, WINDOW_THEMES_TAB_MAIN_UI, @@ -891,3 +893,9 @@ void window_themes_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scroll y += _row_height; } } + +#endif + +void window_themes_open() +{ +} diff --git a/src/windows/title_scenarioselect.c b/src/windows/title_scenarioselect.c index 021d63548e..40b21a6fa4 100644 --- a/src/windows/title_scenarioselect.c +++ b/src/windows/title_scenarioselect.c @@ -351,7 +351,7 @@ static void window_scenarioselect_paint(rct_window *w, rct_drawpixelinfo *dpi) window_draw_widgets(w, dpi); - format = (theme_get_preset()->features.rct1_scenario_font) ? 5138 : 1193; + format = (theme_get_flags() & UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT) ? 5138 : 1193; // Text for each tab for (i = 0; i < 8; i++) { @@ -426,8 +426,8 @@ static void window_scenarioselect_scrollpaint(rct_window *w, rct_drawpixelinfo * colour = (colour << 24) | (colour << 16) | (colour << 8) | colour; gfx_clear(dpi, colour); - int highlighted_format = (theme_get_preset()->features.rct1_scenario_font) ? 5139 : 1193; - int unhighlighted_format = (theme_get_preset()->features.rct1_scenario_font) ? 5139 : 1191; + int highlighted_format = (theme_get_flags() & UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT) ? 5139 : 1193; + int unhighlighted_format = (theme_get_flags() & UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT) ? 5139 : 1191; bool wide = gConfigGeneral.scenario_select_mode == SCENARIO_SELECT_MODE_ORIGIN; From 7e1ce4fcc677420994f1c088db7516b2515a3b01 Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Fri, 29 Jan 2016 23:18:28 +0000 Subject: [PATCH 07/13] reinstate theme editor and fix bugs --- openrct2.vcxproj | 3 +- openrct2.vcxproj.filters | 7 +- src/config.h | 7 -- src/core/Path.cpp | 17 ++- src/core/Path.hpp | 1 + src/core/String.cpp | 29 ++++- src/core/String.hpp | 4 +- src/interface/Theme.cpp | 222 +++++++++++++++++++++++++++++--- src/interface/themes.c | 255 ------------------------------------- src/interface/themes.h | 52 ++------ src/windows/themes.c | 265 ++++++++++++++++----------------------- 11 files changed, 369 insertions(+), 493 deletions(-) delete mode 100644 src/interface/themes.c diff --git a/openrct2.vcxproj b/openrct2.vcxproj index 16dfe52ca6..73fe4a6021 100644 --- a/openrct2.vcxproj +++ b/openrct2.vcxproj @@ -53,7 +53,6 @@ - @@ -110,6 +109,7 @@ + @@ -179,7 +179,6 @@ - diff --git a/openrct2.vcxproj.filters b/openrct2.vcxproj.filters index df0b6eb8d6..3fddff8512 100644 --- a/openrct2.vcxproj.filters +++ b/openrct2.vcxproj.filters @@ -453,12 +453,6 @@ Source\Network - - Source\Interface - - - Source\Windows - Source\Windows @@ -586,6 +580,7 @@ Source\Interface + diff --git a/src/config.h b/src/config.h index 60f733f26c..6372bfeecd 100644 --- a/src/config.h +++ b/src/config.h @@ -253,13 +253,6 @@ typedef struct { bool guest_died; } notification_configuration; -typedef struct theme_window { - uint8 colours[6]; - - // Define any other settings for all windows here - -} theme_window; - // Define structures for any other settings here typedef struct { uint8 rct1_ride_lights; diff --git a/src/core/Path.cpp b/src/core/Path.cpp index 90b8e0f031..514f53ddeb 100644 --- a/src/core/Path.cpp +++ b/src/core/Path.cpp @@ -18,11 +18,26 @@ namespace Path return safe_strcat_path(buffer, src, bufferSize); } + utf8 * GetDirectory(utf8 * buffer, size_t bufferSize, const utf8 * path) + { + const char pathSeperator = platform_get_path_separator(); + size_t lastPathSepIndex = String::LastIndexOf(path, pathSeperator); + if (lastPathSepIndex == SIZE_MAX) + { + return String::Set(buffer, bufferSize, String::Empty); + } + + size_t copyLength = Math::Min(lastPathSepIndex, bufferSize - 1); + Memory::Copy(buffer, path, copyLength); + buffer[copyLength] = '\0'; + return buffer; + } + utf8 * GetFileNameWithoutExtension(utf8 * buffer, size_t bufferSize, const utf8 * path) { const utf8 * lastDot = nullptr; const utf8 * ch = path; - for (; ch != '\0'; ch++) + for (; *ch != '\0'; ch++) { if (*ch == '.') { diff --git a/src/core/Path.hpp b/src/core/Path.hpp index 1186f86e51..b3aa163f72 100644 --- a/src/core/Path.hpp +++ b/src/core/Path.hpp @@ -8,6 +8,7 @@ extern "C" namespace Path { utf8 * Append(utf8 * buffer, size_t bufferSize, const utf8 * src); + utf8 * GetDirectory(utf8 * buffer, size_t bufferSize, const utf8 * path); utf8 * GetFileNameWithoutExtension(utf8 * buffer, size_t bufferSize, const utf8 * path); utf8 * GetAbsolute(utf8 * buffer, size_t bufferSize, const utf8 * relativePath); bool Equals(const utf8 * a, const utf8 * b); diff --git a/src/core/String.cpp b/src/core/String.cpp index ba000bc7f7..ca1a7f97a9 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -10,6 +10,11 @@ extern "C" namespace String { + bool IsNullOrEmpty(const utf8 * str) + { + return str == nullptr || str[0] == '\0'; + } + bool Equals(const utf8 * a, const utf8 * b, bool ignoreCase) { if (a == b) return true; @@ -51,6 +56,28 @@ namespace String } } + size_t LastIndexOf(const utf8 * str, utf8 match) + { + const utf8 * lastOccurance = nullptr; + const utf8 * ch = str; + for (; ch != '\0'; ch++) + { + if (*ch == match) + { + lastOccurance = ch; + } + } + + if (lastOccurance == nullptr) + { + return SIZE_MAX; + } + else + { + return (size_t)(lastOccurance - str); + } + } + size_t LengthOf(const utf8 * str) { return utf8_length(str); @@ -137,7 +164,7 @@ namespace String return replacement; } - utf8 * DiscardDuplicate(utf8 * * ptr, utf8 * replacement) + utf8 * DiscardDuplicate(utf8 * * ptr, const utf8 * replacement) { return DiscardUse(ptr, String::Duplicate(replacement)); } diff --git a/src/core/String.hpp b/src/core/String.hpp index c286ebd27e..7af30db4f4 100644 --- a/src/core/String.hpp +++ b/src/core/String.hpp @@ -9,8 +9,10 @@ namespace String { constexpr utf8 * Empty = ""; + bool IsNullOrEmpty(const utf8 * str); bool Equals(const utf8 * a, const utf8 * b, bool ignoreCase = false); bool StartsWith(const utf8 * str, const utf8 * match, bool ignoreCase = false); + size_t LastIndexOf(const utf8 * str, utf8 match); /** * Gets the length of the given string in codepoints. @@ -37,7 +39,7 @@ namespace String /** * Helper method to free the string a string pointer points to and set it to a copy of a replacement string. */ - utf8 * DiscardDuplicate(utf8 * * ptr, utf8 * replacement); + utf8 * DiscardDuplicate(utf8 * * ptr, const utf8 * replacement); utf8 * SkipBOM(utf8 * buffer); const utf8 * SkipBOM(const utf8 * buffer); diff --git a/src/interface/Theme.cpp b/src/interface/Theme.cpp index f40961a88c..6459751713 100644 --- a/src/interface/Theme.cpp +++ b/src/interface/Theme.cpp @@ -3,6 +3,7 @@ extern "C" { #include "../common.h" + #include "../config.h" #include "themes.h" #include "window.h" } @@ -55,6 +56,7 @@ public: UITheme(const UITheme & name); ~UITheme(); + void SetName(const utf8 * name); const UIThemeWindowEntry * GetEntry(rct_windowclass windowClass) const; void SetEntry(const UIThemeWindowEntry * entry); void RemoveEntry(rct_windowclass windowClass); @@ -290,6 +292,11 @@ UITheme::~UITheme() Memory::Free(Name); } +void UITheme::SetName(const utf8 * name) +{ + String::DiscardDuplicate(&Name, name); +} + const UIThemeWindowEntry * UITheme::GetEntry(rct_windowclass windowClass) const { for (size_t i = 0; i < Entries.GetCount(); i++) @@ -363,7 +370,7 @@ bool UITheme::WriteToFile(const utf8 * path) const bool result; try { - Json::WriteToFile(path, jsonTheme, JSON_INDENT(4)); + Json::WriteToFile(path, jsonTheme, JSON_INDENT(4) | JSON_PRESERVE_ORDER); result = true; } catch (Exception ex) @@ -386,10 +393,6 @@ UITheme * UITheme::FromJson(const json_t * json) json_t * jsonEntries = json_object_get(json, "entries"); size_t numEntries = json_object_size(jsonEntries); - if (numEntries == 0) - { - ThrowThemeLoadException(); - } UITheme * result = nullptr; try @@ -441,6 +444,7 @@ UITheme * UITheme::FromFile(const utf8 * path) } catch (Exception ex) { + log_error("Unable to read theme: %s", path); result = nullptr; } @@ -470,15 +474,17 @@ namespace ThemeManager struct AvailableTheme { utf8 Path[MAX_PATH]; - utf8 Name[64]; + utf8 Name[96]; }; - const utf8 * CurrentThemePath; + utf8 * CurrentThemePath; UITheme * CurrentTheme; List AvailableThemes; size_t ActiveAvailableThemeIndex = SIZE_MAX; size_t NumPredefinedThemes = 0; + void GetThemeFileName(utf8 * buffer, size_t bufferSize, const utf8 * name); + void GetAvailableThemes(List * outThemes) { Guard::ArgumentNotNull(outThemes); @@ -500,33 +506,44 @@ namespace ThemeManager platform_get_user_directory(themesPattern, "themes"); Path::Append(themesPattern, sizeof(themesPattern), "*.json"); - utf8 path[MAX_PATH]; int handle = platform_enumerate_files_begin(themesPattern); - while (platform_enumerate_directories_next(handle, path)) + if (handle != INVALID_HANDLE) { - AvailableTheme theme; - String::Set(theme.Path, sizeof(theme.Path), path); - Path::GetFileNameWithoutExtension(theme.Path, sizeof(theme.Path), path); - outThemes->Add(theme); - - if (Path::Equals(CurrentThemePath, path)) + file_info fileInfo; + while (platform_enumerate_files_next(handle, &fileInfo)) { - ActiveAvailableThemeIndex = outThemes->GetCount() - 1; + AvailableTheme theme; + Path::GetFileNameWithoutExtension(theme.Name, sizeof(theme.Name), fileInfo.path); + GetThemeFileName(theme.Path, sizeof(theme.Path), theme.Name); + + outThemes->Add(theme); + + if (Path::Equals(CurrentThemePath, fileInfo.path)) + { + ActiveAvailableThemeIndex = outThemes->GetCount() - 1; + } } + platform_enumerate_files_end(handle); } } void LoadTheme(UITheme * theme) { + if (CurrentTheme == theme) + { + return; + } + if (CurrentTheme != nullptr) { - if (!(theme->Flags & UITHEME_FLAG_PREDEFINED)) + if (!(CurrentTheme->Flags & UITHEME_FLAG_PREDEFINED)) { delete CurrentTheme; } } CurrentTheme = theme; + String::DiscardUse(&CurrentThemePath, nullptr); gfx_invalidate_screen(); } @@ -538,8 +555,35 @@ namespace ThemeManager { // Fall-back to default theme = (UITheme *)&PredefinedThemeRCT2; + LoadTheme(theme); } - LoadTheme(theme); + else + { + LoadTheme(theme); + String::DiscardDuplicate(&CurrentThemePath, path); + } + } + + bool LoadThemeByName(const utf8 * name) + { + for (size_t i = 0; i < ThemeManager::AvailableThemes.GetCount(); i++) + { + if (String::Equals(name, ThemeManager::AvailableThemes[i].Name)) + { + const utf8 * path = ThemeManager::AvailableThemes[i].Path; + if (String::IsNullOrEmpty(path)) + { + LoadTheme((UITheme *)PredefinedThemes[i]); + } + else + { + LoadTheme(ThemeManager::AvailableThemes[i].Path); + } + ActiveAvailableThemeIndex = i; + return true; + } + } + return false; } void Initialise() @@ -547,6 +591,27 @@ namespace ThemeManager ThemeManager::GetAvailableThemes(&ThemeManager::AvailableThemes); LoadTheme((UITheme *)&PredefinedThemeRCT2); ActiveAvailableThemeIndex = 1; + + bool configValid = false; + if (!String::IsNullOrEmpty(gConfigInterface.current_theme_preset)) + { + if (LoadThemeByName(gConfigInterface.current_theme_preset)) + { + configValid = true; + } + } + + if (!configValid) + { + String::DiscardDuplicate(&gConfigInterface.current_theme_preset, theme_manager_get_available_theme_name(1)); + } + } + + void GetThemeFileName(utf8 * buffer, size_t bufferSize, const utf8 * name) + { + platform_get_user_directory(buffer, "themes"); + Path::Append(buffer, bufferSize, name); + String::Append(buffer, bufferSize, ".json"); } } @@ -587,8 +652,51 @@ extern "C" { const utf8 * path = ThemeManager::AvailableThemes[index].Path; ThemeManager::LoadTheme(path); + + // HACK Check if theme load failed and fell back to RCT2 + if (ThemeManager::CurrentThemePath == nullptr) + { + index = 1; + } } ThemeManager::ActiveAvailableThemeIndex = index; + String::DiscardDuplicate(&gConfigInterface.current_theme_preset, theme_manager_get_available_theme_name(index)); + } + + uint8 theme_get_colour(rct_windowclass wc, uint8 index) + { + const UIThemeWindowEntry * entry = ThemeManager::CurrentTheme->GetEntry(wc); + if (entry == nullptr) + { + const WindowThemeDesc * desc = GetWindowThemeDescriptor(wc); + return desc->DefaultTheme.Colours[index]; + } + else + { + return entry->Theme.Colours[index]; + } + } + + void theme_set_colour(rct_windowclass wc, uint8 index, colour_t colour) + { + UIThemeWindowEntry entry; + entry.WindowClass = wc; + + auto currentEntry = (UIThemeWindowEntry *)ThemeManager::CurrentTheme->GetEntry(wc); + if (currentEntry != nullptr) + { + entry.Theme = currentEntry->Theme; + } + else + { + const WindowThemeDesc * desc = GetWindowThemeDescriptor(wc); + entry.Theme = desc->DefaultTheme; + } + + entry.Theme.Colours[index] = colour; + ThemeManager::CurrentTheme->SetEntry(&entry); + + theme_save(); } uint8 theme_get_flags() @@ -596,11 +704,89 @@ extern "C" return ThemeManager::CurrentTheme->Flags; } + void theme_set_flags(uint8 flags) + { + ThemeManager::CurrentTheme->Flags = flags; + theme_save(); + } + + void theme_save() + { + ThemeManager::CurrentTheme->WriteToFile(ThemeManager::CurrentThemePath); + } + + void theme_rename(const utf8 * name) + { + const utf8 * oldPath = ThemeManager::CurrentThemePath; + utf8 newPath[MAX_PATH]; + + ThemeManager::GetThemeFileName(newPath, sizeof(newPath), name); + platform_file_move(oldPath, newPath); + String::DiscardDuplicate(&ThemeManager::CurrentThemePath, newPath); + + ThemeManager::CurrentTheme->SetName(name); + ThemeManager::CurrentTheme->WriteToFile(ThemeManager::CurrentThemePath); + + theme_manager_load_available_themes(); + for (size_t i = 0; i < ThemeManager::AvailableThemes.GetCount(); i++) + { + if (Path::Equals(newPath, ThemeManager::AvailableThemes[i].Path)) + { + ThemeManager::ActiveAvailableThemeIndex = i; + String::DiscardDuplicate(&gConfigInterface.current_theme_preset, theme_manager_get_available_theme_name(1)); + break; + } + } + } + + void theme_duplicate(const utf8 * name) + { + utf8 newPath[MAX_PATH]; + + ThemeManager::GetThemeFileName(newPath, sizeof(newPath), name); + String::DiscardDuplicate(&ThemeManager::CurrentThemePath, newPath); + + ThemeManager::CurrentTheme->SetName(name); + ThemeManager::CurrentTheme->Flags &= ~UITHEME_FLAG_PREDEFINED; + ThemeManager::CurrentTheme->WriteToFile(ThemeManager::CurrentThemePath); + + theme_manager_load_available_themes(); + for (size_t i = 0; i < ThemeManager::AvailableThemes.GetCount(); i++) + { + if (Path::Equals(newPath, ThemeManager::AvailableThemes[i].Path)) + { + ThemeManager::ActiveAvailableThemeIndex = i; + String::DiscardDuplicate(&gConfigInterface.current_theme_preset, theme_manager_get_available_theme_name(i)); + break; + } + } + } + + void theme_delete() + { + platform_file_delete(ThemeManager::CurrentThemePath); + ThemeManager::LoadTheme((UITheme *)&PredefinedThemeRCT2); + ThemeManager::ActiveAvailableThemeIndex = 1; + String::DiscardDuplicate(&gConfigInterface.current_theme_preset, theme_manager_get_available_theme_name(1)); + } + void theme_manager_initialise() { ThemeManager::Initialise(); } + uint8 theme_desc_get_num_colours(rct_windowclass wc) + { + const WindowThemeDesc * desc = GetWindowThemeDescriptor(wc); + return desc->NumColours; + } + + rct_string_id theme_desc_get_name(rct_windowclass wc) + { + const WindowThemeDesc * desc = GetWindowThemeDescriptor(wc); + return desc->WindowName; + } + void colour_scheme_update(rct_window * window) { colour_scheme_update_by_class(window, window->classification); diff --git a/src/interface/themes.c b/src/interface/themes.c deleted file mode 100644 index 2fb4d36e5a..0000000000 --- a/src/interface/themes.c +++ /dev/null @@ -1,255 +0,0 @@ -#if 0 - -/***************************************************************************** -* Copyright (c) 2014 Ted John, Peter Hill -* OpenRCT2, an open source clone of Roller Coaster Tycoon 2. -* -* This file is part of OpenRCT2. -* -* OpenRCT2 is free software: you can redistribute it and/or modify -* it under the terms of the GNU General Public License as published by -* the Free Software Foundation, either version 3 of the License, or -* (at your option) any later version. - -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. - -* You should have received a copy of the GNU General Public License -* along with this program. If not, see . -*****************************************************************************/ - -#include "../localisation/string_ids.h" -#include "../util/util.h" -#include "colour.h" -#include "window.h" -#include "themes.h" - -#define COLOURS_1(c0) 1, { { (c0), 0, 0, 0, 0, 0 } } -#define COLOURS_2(c0, c1) 2, { { (c0), (c1), 0, 0, 0, 0 } } -#define COLOURS_3(c0, c1, c2) 3, { { (c0), (c1), (c2), 0, 0, 0 } } -#define COLOURS_4(c0, c1, c2, c3) 4, { { (c0), (c1), (c2), (c3), 0, 0 } } -#define COLOURS_5(c0, c1, c2, c3, c4) 5, { { (c0), (c1), (c2), (c3), (c4), 0 } } -#define COLOURS_6(c0, c1, c2, c3, c4, c5) 6, { { (c0), (c1), (c2), (c3), (c4), (c5) } } - -#define THEME_DEF_END { 0xFF, { 0, 0, 0, 0, 0, 0 } } - -#define TWINDOW(window_class, window_name, window_string_id, theme) { window_class, window_name, window_string_id, theme } - -theme_window_definition gThemeWindowDefinitions[] = { - /* Window Class ini section name stringid window defaults */ - { WC_TOP_TOOLBAR, "top_toolbar", 5245, COLOURS_4(COLOUR_LIGHT_BLUE, COLOUR_DARK_GREEN, COLOUR_DARK_BROWN, COLOUR_GREY ) }, - { WC_BOTTOM_TOOLBAR, "bottom_toolbar", 5246, COLOURS_4(TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN), COLOUR_BLACK, COLOUR_BRIGHT_GREEN ) }, - { WC_RIDE, "ride", 5203, COLOURS_3(COLOUR_GREY, COLOUR_BORDEAUX_RED, COLOUR_GREY ) }, - { WC_RIDE_CONSTRUCTION, "ride_construction", 5199, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, - { WC_RIDE_LIST, "ride_list", 5204, COLOURS_3(COLOUR_GREY, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, - { WC_SAVE_PROMPT, "save_prompt", 5223, COLOURS_1(TRANSLUCENT(COLOUR_BORDEAUX_RED) ) }, - { WC_CONSTRUCT_RIDE, "new_ride", 5201, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, - { WC_DEMOLISH_RIDE_PROMPT, "demolish_ride_prompt", 5224, COLOURS_1(TRANSLUCENT(COLOUR_BORDEAUX_RED) ) }, - { WC_SCENERY, "scenery", 5197, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_GREEN, COLOUR_DARK_GREEN ) }, - { WC_OPTIONS, "options", 5219, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, - { WC_FOOTPATH, "footpath", 5198, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, - { WC_LAND, "land", 5193, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, - { WC_WATER, "water", 5194, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, - { WC_PEEP, "guest", 5205, COLOURS_3(COLOUR_GREY, COLOUR_OLIVE_GREEN, COLOUR_OLIVE_GREEN ) }, - { WC_GUEST_LIST, "guest_list", 5206, COLOURS_3(COLOUR_GREY, COLOUR_OLIVE_GREEN, COLOUR_OLIVE_GREEN ) }, - { WC_STAFF_LIST, "staff_list", 5208, COLOURS_3(COLOUR_GREY, COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE ) }, - { WC_FIRE_PROMPT, "staff_fire_prompt", 5225, COLOURS_1(TRANSLUCENT(COLOUR_BORDEAUX_RED) ) }, - { WC_PARK_INFORMATION, "park_information", 5253, COLOURS_3(COLOUR_GREY, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, - { WC_FINANCES, "finances", 5187, COLOURS_3(COLOUR_GREY, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, - { WC_TITLE_MENU, "title_menu", 5249, COLOURS_3(TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN) ) }, - { WC_TITLE_EXIT, "title_exit", 5250, COLOURS_3(TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN) ) }, - { WC_RECENT_NEWS, "recent_news", 5192, COLOURS_3(COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK ) }, - { WC_SCENARIO_SELECT, "scenario_select", 5252, COLOURS_3(COLOUR_GREY, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, - { WC_TRACK_DESIGN_LIST, "track_design_list", 5202, COLOURS_3(COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, - { WC_TRACK_DESIGN_PLACE, "track_design_place", 5200, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, - { WC_NEW_CAMPAIGN, "new_campaign", 5188, COLOURS_3(COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, - { WC_KEYBOARD_SHORTCUT_LIST, "keyboard_shortcuts", 5220, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, - { WC_CHANGE_KEYBOARD_SHORTCUT, "change_keyboard_shortcut", 5221, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, - { WC_MAP, "map", 5190, COLOURS_2(COLOUR_DARK_GREEN, COLOUR_DARK_BROWN ) }, - { WC_BANNER, "banner", 5209, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, - { WC_EDITOR_OBJECT_SELECTION, "editor_object_selection", 5210, COLOURS_3(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY ) }, - { WC_EDITOR_INVENTION_LIST, "editor_invention_list", 5211, COLOURS_3(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY ) }, - { WC_EDITOR_SCENARIO_OPTIONS, "editor_scenario_options", 5212, COLOURS_3(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY ) }, - { WC_EDTIOR_OBJECTIVE_OPTIONS, "editor_objection_options", 5213, COLOURS_3(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY ) }, - { WC_MANAGE_TRACK_DESIGN, "manage_track_design", 5215, COLOURS_3(COLOUR_GREY, COLOUR_GREY, COLOUR_GREY ) }, - { WC_TRACK_DELETE_PROMPT, "track_delete_prompt", 5226, COLOURS_3(COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, - { WC_INSTALL_TRACK, "install_track", 5216, COLOURS_3(COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED ) }, - { WC_CLEAR_SCENERY, "clear_scenery", 5195, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, - { WC_CHEATS, "cheats", 5217, COLOURS_3(COLOUR_GREY, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, - { WC_RESEARCH, "research", 5189, COLOURS_3(COLOUR_GREY, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, - { WC_VIEWPORT, "viewport", 5191, COLOURS_3(COLOUR_DARK_BROWN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, - { WC_MAPGEN, "map_generation", 5214, COLOURS_3(COLOUR_DARK_GREEN, COLOUR_DARK_BROWN, COLOUR_DARK_BROWN ) }, - { WC_LOADSAVE, "loadsave", 5222, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, - { WC_LOADSAVE_OVERWRITE_PROMPT, "loadsave_overwrite_prompt", 5227, COLOURS_1(TRANSLUCENT(COLOUR_BORDEAUX_RED) ) }, - { WC_TITLE_OPTIONS, "title_options", 5251, COLOURS_3(TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN), TRANSLUCENT(COLOUR_DARK_GREEN) ) }, - { WC_LAND_RIGHTS, "land_rights", 5196, COLOURS_3(COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW, COLOUR_DARK_YELLOW ) }, - { WC_THEMES, "themes", 5218, COLOURS_3(COLOUR_GREY, COLOUR_DARK_GREEN, COLOUR_DARK_GREEN ) }, - { WC_STAFF, "staff", 5207, COLOURS_3(COLOUR_GREY, COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE ) }, - { WC_EDITOR_TRACK_BOTTOM_TOOLBAR, "editor_track_bottom_toolbar", 5247, COLOURS_3(TRANSLUCENT(COLOUR_LIGHT_BLUE), TRANSLUCENT(COLOUR_LIGHT_BLUE), TRANSLUCENT(COLOUR_LIGHT_BLUE) ) }, - { WC_EDITOR_SCENARIO_BOTTOM_TOOLBAR, "editor_scenario_bottom_toolbar", 5248, COLOURS_3(TRANSLUCENT(COLOUR_LIGHT_BROWN), TRANSLUCENT(COLOUR_LIGHT_BROWN), TRANSLUCENT(COLOUR_MOSS_GREEN) ) }, - { WC_TITLE_EDITOR, "title_sequences", 5433, COLOURS_3(COLOUR_GREY, COLOUR_OLIVE_GREEN, COLOUR_OLIVE_GREEN ) }, -}; - -#define COLOURS_RCT1(c0, c1, c2, c3, c4, c5) { { (c0), (c1), (c2), (c3), (c4), (c5) } } - -theme_window_preset gThemeWindowsRCT1[] = { - { WC_TOP_TOOLBAR, COLOURS_RCT1(COLOUR_GREY, COLOUR_GREY, COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_BOTTOM_TOOLBAR, COLOURS_RCT1(TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), COLOUR_BLACK, COLOUR_YELLOW, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_RIDE, COLOURS_RCT1(COLOUR_BORDEAUX_RED, COLOUR_GREY, COLOUR_SATURATED_GREEN, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_RIDE_LIST, COLOURS_RCT1(COLOUR_BORDEAUX_RED, COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_CONSTRUCT_RIDE, COLOURS_RCT1(COLOUR_BORDEAUX_RED, COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_PEEP, COLOURS_RCT1(COLOUR_LIGHT_BROWN, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_GUEST_LIST, COLOURS_RCT1(COLOUR_LIGHT_BROWN, COLOUR_BORDEAUX_RED, COLOUR_BORDEAUX_RED, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_STAFF_LIST, COLOURS_RCT1(COLOUR_DARK_GREEN, COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_FINANCES, COLOURS_RCT1(COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_TITLE_MENU, COLOURS_RCT1(TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_TITLE_EXIT, COLOURS_RCT1(TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_NEW_CAMPAIGN, COLOURS_RCT1(COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE, COLOUR_GREY, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_TITLE_OPTIONS, COLOURS_RCT1(TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), TRANSLUCENT(COLOUR_GREY), COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - { WC_STAFF, COLOURS_RCT1(COLOUR_DARK_GREEN, COLOUR_LIGHT_PURPLE, COLOUR_LIGHT_PURPLE, COLOUR_BLACK, COLOUR_BLACK, COLOUR_BLACK) }, - THEME_DEF_END -}; - -uint16 gCurrentTheme = 0; -uint32 gNumThemeWindows = sizeof(gThemeWindowDefinitions) / sizeof(theme_window_definition); - - -theme_preset* theme_get_preset() -{ - return &gConfigThemes.presets[gCurrentTheme]; -} - -theme_window_definition* theme_window_definition_get_by_class(rct_windowclass classification) -{ - for (int i = 0; i < (int)gNumThemeWindows; i++) { - if (gThemeWindowDefinitions[i].classification == classification) { - return &gThemeWindowDefinitions[i]; - } - } - return NULL; -} - -theme_window* theme_window_get_by_class(rct_windowclass classification) -{ - for (int i = 0; i < (int)gNumThemeWindows; i++) { - if (gThemeWindowDefinitions[i].classification == classification) { - return &gConfigThemes.presets[gCurrentTheme].windows[i]; - } - } - return NULL; -} - -void colour_scheme_update(rct_window *window) -{ - theme_window* theme = theme_window_get_by_class(window->classification); - - bool transparent = false; - for (int i = 0; i < 6; i++) { - window->colours[i] = theme->colours[i]; - if (theme->colours[i] & 0x80) { - transparent = true; - } - } - // Some windows need to be transparent even if the colours aren't. - // There doesn't seem to be any side-effects for all windows being transparent - window->flags |= WF_TRANSPARENT; -} - -void colour_scheme_update_by_class(rct_window *window, rct_windowclass classification) -{ - theme_window* theme = theme_window_get_by_class(classification); - - bool transparent = false; - for (int i = 0; i < 6; i++) { - window->colours[i] = theme->colours[i]; - if (theme->colours[i] & 0x80) { - transparent = true; - } - } - // Some windows need to be transparent even if the colours aren't. - // There doesn't seem to be any side-effects for all windows being transparent - window->flags |= WF_TRANSPARENT; -} - -static void theme_set_preset_string(const utf8string preset) -{ - SafeFree(gConfigInterface.current_theme_preset); - gConfigInterface.current_theme_preset = _strdup(preset); -} - -void theme_change_preset(int preset) -{ - if (preset >= 0 && preset < gConfigThemes.num_presets) { - switch (preset) { - case 0: - theme_set_preset_string("*RCT2"); - break; - case 1: - theme_set_preset_string("*RCT1"); - break; - default: - theme_set_preset_string(gConfigThemes.presets[preset].name); - break; - } - gCurrentTheme = preset; - } - window_invalidate_all(); -} - -void theme_create_preset(int duplicate, const char *name) -{ - int preset = gConfigThemes.num_presets; - gConfigThemes.num_presets++; - gConfigThemes.presets = realloc(gConfigThemes.presets, sizeof(theme_preset) * gConfigThemes.num_presets); - safe_strcpy(gConfigThemes.presets[preset].name, name, THEME_PRESET_NAME_SIZE); - gConfigThemes.presets[preset].windows = malloc(sizeof(theme_window) * gNumThemeWindows); - for (int i = 0; i < (int)gNumThemeWindows; i++) { - gConfigThemes.presets[preset].windows[i] = gConfigThemes.presets[duplicate].windows[i]; - } - gConfigThemes.presets[preset].features = gConfigThemes.presets[duplicate].features; - themes_save_preset(preset); - theme_change_preset(preset); -} - -void theme_delete_preset(int preset) -{ - if (preset >= 2) { - utf8 path[MAX_PATH]; - platform_get_user_directory(path, "themes"); - strcat(path, gConfigThemes.presets[preset].name); - strcat(path, ".ini"); - platform_file_delete(path); - - free(gConfigThemes.presets[preset].windows); - - for (int i = preset; i < gConfigThemes.num_presets - 1; i++) { - gConfigThemes.presets[i] = gConfigThemes.presets[i + 1]; - } - gConfigThemes.num_presets--; - theme_change_preset(0); - } -} - -void theme_rename_preset(int preset, const char *newName) -{ - if (preset >= 2) { - utf8 src[MAX_PATH], dest[MAX_PATH]; - platform_get_user_directory(src, "themes"); - platform_get_user_directory(dest, "themes"); - strcat(src, gConfigThemes.presets[preset].name); - strcat(dest, newName); - strcat(src, ".ini"); - strcat(dest, ".ini"); - platform_file_move(src, dest); - - safe_strcpy(gConfigThemes.presets[preset].name, newName, THEME_PRESET_NAME_SIZE); - - if (preset == gCurrentTheme) { - gConfigInterface.current_theme_preset = gConfigThemes.presets[preset].name; - } - } -} - -#endif diff --git a/src/interface/themes.h b/src/interface/themes.h index fb5feda672..3de9401afc 100644 --- a/src/interface/themes.h +++ b/src/interface/themes.h @@ -42,48 +42,16 @@ const utf8 * theme_manager_get_available_theme_name(size_t index); size_t theme_manager_get_active_available_theme_index(); void theme_manager_set_active_available_theme(size_t index); -uint8 theme_get_flags(); - - -#if 0 - - - -#include "../config.h" - -typedef struct { - rct_windowclass classification; - char *section_name; - rct_string_id name; - uint8 num_colours; - theme_window window; -} theme_window_definition; - -typedef struct { - rct_windowclass classification; - theme_window window; -} theme_window_preset; - -// The definitions for window themes as well as the RCT2 preset -extern theme_window_definition gThemeWindowDefinitions[]; -// The preset for RCT1 window themes -extern theme_window_preset gThemeWindowsRCT1[]; - -// The index of the current theme -extern uint16 gCurrentTheme; -// The number of theme-able windows -extern uint32 gNumThemeWindows; - -theme_preset* theme_get_preset(); -theme_window_definition* theme_window_definition_get_by_class(rct_windowclass classification); -theme_window* theme_window_get_by_class(rct_windowclass classification); - -void theme_change_preset(int preset); -void theme_create_preset(int duplicate, const char *name); -void theme_delete_preset(int preset); -void theme_rename_preset(int preset, const char *newName); - -#endif +colour_t theme_get_colour(rct_windowclass wc, uint8 index); +void theme_set_colour(rct_windowclass wc, uint8 index, colour_t colour); +uint8 theme_get_flags(); +void theme_set_flags(uint8 flags); +void theme_save(); +void theme_rename(const utf8 * name); +void theme_duplicate(const utf8 * name); +void theme_delete(); +uint8 theme_desc_get_num_colours(rct_windowclass wc); +rct_string_id theme_desc_get_name(rct_windowclass wc); #endif diff --git a/src/windows/themes.c b/src/windows/themes.c index 6e9da3803d..eddb70fe94 100644 --- a/src/windows/themes.c +++ b/src/windows/themes.c @@ -36,8 +36,6 @@ #include "error.h" #include "../util/util.h" -#if 0 - enum { WINDOW_THEMES_TAB_SETTINGS, WINDOW_THEMES_TAB_MAIN_UI, @@ -256,6 +254,17 @@ static rct_windowclass window_themes_tab_7_classes[] = { WC_LOADSAVE_OVERWRITE_PROMPT }; +static rct_windowclass *window_themes_tab_classes[] = { + NULL, + window_themes_tab_1_classes, + window_themes_tab_2_classes, + window_themes_tab_3_classes, + window_themes_tab_4_classes, + window_themes_tab_5_classes, + window_themes_tab_6_classes, + window_themes_tab_7_classes, +}; + static uint8 _selected_tab = 0; static sint16 _color_index_1 = -1; static sint8 _color_index_2 = -1; @@ -269,57 +278,10 @@ void window_themes_init_vars() _selected_tab = WINDOW_THEMES_TAB_SETTINGS; } -static theme_window_definition* get_colour_scheme_tab_definition() +static rct_windowclass get_window_class_tab_index(int index) { - switch (_selected_tab) { - case 1: return theme_window_definition_get_by_class(window_themes_tab_1_classes[_color_index_1]); - case 2: return theme_window_definition_get_by_class(window_themes_tab_2_classes[_color_index_1]); - case 3: return theme_window_definition_get_by_class(window_themes_tab_3_classes[_color_index_1]); - case 4: return theme_window_definition_get_by_class(window_themes_tab_4_classes[_color_index_1]); - case 5: return theme_window_definition_get_by_class(window_themes_tab_5_classes[_color_index_1]); - case 6: return theme_window_definition_get_by_class(window_themes_tab_6_classes[_color_index_1]); - case 7: return theme_window_definition_get_by_class(window_themes_tab_7_classes[_color_index_1]); - } - return NULL; -} -static theme_window_definition* get_colour_scheme_tab_definition_by_index(int index) -{ - switch (_selected_tab) { - case 1: return theme_window_definition_get_by_class(window_themes_tab_1_classes[index]); - case 2: return theme_window_definition_get_by_class(window_themes_tab_2_classes[index]); - case 3: return theme_window_definition_get_by_class(window_themes_tab_3_classes[index]); - case 4: return theme_window_definition_get_by_class(window_themes_tab_4_classes[index]); - case 5: return theme_window_definition_get_by_class(window_themes_tab_5_classes[index]); - case 6: return theme_window_definition_get_by_class(window_themes_tab_6_classes[index]); - case 7: return theme_window_definition_get_by_class(window_themes_tab_7_classes[index]); - } - return NULL; -} -static theme_window* get_colour_scheme_tab() -{ - switch (_selected_tab) { - case 1: return theme_window_get_by_class(window_themes_tab_1_classes[_color_index_1]); - case 2: return theme_window_get_by_class(window_themes_tab_2_classes[_color_index_1]); - case 3: return theme_window_get_by_class(window_themes_tab_3_classes[_color_index_1]); - case 4: return theme_window_get_by_class(window_themes_tab_4_classes[_color_index_1]); - case 5: return theme_window_get_by_class(window_themes_tab_5_classes[_color_index_1]); - case 6: return theme_window_get_by_class(window_themes_tab_6_classes[_color_index_1]); - case 7: return theme_window_get_by_class(window_themes_tab_7_classes[_color_index_1]); - } - return NULL; -} -static theme_window* get_colour_scheme_tab_by_index(int index) -{ - switch (_selected_tab) { - case 1: return theme_window_get_by_class(window_themes_tab_1_classes[index]); - case 2: return theme_window_get_by_class(window_themes_tab_2_classes[index]); - case 3: return theme_window_get_by_class(window_themes_tab_3_classes[index]); - case 4: return theme_window_get_by_class(window_themes_tab_4_classes[index]); - case 5: return theme_window_get_by_class(window_themes_tab_5_classes[index]); - case 6: return theme_window_get_by_class(window_themes_tab_6_classes[index]); - case 7: return theme_window_get_by_class(window_themes_tab_7_classes[index]); - } - return NULL; + rct_windowclass * classes = window_themes_tab_classes[_selected_tab]; + return classes[index]; } static int get_colour_scheme_tab_count() @@ -336,19 +298,6 @@ static int get_colour_scheme_tab_count() return 0; } -/*static int get_colour_scheme_index() { - switch (_selected_tab) { - case 1: return theme_window_get_index_by_class(window_themes_tab_1_classes[_color_index_1]); - case 2: return theme_window_get_index_by_class(window_themes_tab_2_classes[_color_index_1]); - case 3: return theme_window_get_index_by_class(window_themes_tab_3_classes[_color_index_1]); - case 4: return theme_window_get_index_by_class(window_themes_tab_4_classes[_color_index_1]); - case 5: return theme_window_get_index_by_class(window_themes_tab_5_classes[_color_index_1]); - case 6: return theme_window_get_index_by_class(window_themes_tab_6_classes[_color_index_1]); - case 7: return theme_window_get_index_by_class(window_themes_tab_7_classes[_color_index_1]); - } - return -1; -}*/ - static void window_themes_draw_tab_images(rct_drawpixelinfo *dpi, rct_window *w) { int sprite_idx; @@ -407,27 +356,32 @@ void window_themes_open() static void window_themes_mouseup(rct_window *w, int widgetIndex) { + int activeAvailableThemeIndex; + const utf8 * activeThemeName; + switch (widgetIndex) { case WIDX_THEMES_CLOSE: window_close(w); break; - case WIDX_THEMES_DUPLICATE_BUTTON: - window_text_input_open(w, widgetIndex, 5239, 5240, 1170, (uint32)&gConfigThemes.presets[gCurrentTheme].name, 64); + case WIDX_THEMES_DUPLICATE_BUTTON:; + activeAvailableThemeIndex = theme_manager_get_active_available_theme_index(); + activeThemeName = theme_manager_get_available_theme_name(activeAvailableThemeIndex); + window_text_input_open(w, widgetIndex, 5239, 5240, 1170, (uint32)activeThemeName, 64); break; case WIDX_THEMES_DELETE_BUTTON: - if (gCurrentTheme >= 2) { - theme_delete_preset(gCurrentTheme); - } - else { + if (theme_get_flags() & UITHEME_FLAG_PREDEFINED) { window_error_open(5241, STR_NONE); + } else { + theme_delete(); } break; case WIDX_THEMES_RENAME_BUTTON: - if (gCurrentTheme >= 2) { - window_text_input_open(w, widgetIndex, 3348, 5240, 1170, (uint32)&gConfigThemes.presets[gCurrentTheme].name, 64); - } - else { + if (theme_get_flags() & UITHEME_FLAG_PREDEFINED) { window_error_open(5241, STR_NONE); + } else { + activeAvailableThemeIndex = theme_manager_get_active_available_theme_index(); + activeThemeName = theme_manager_get_available_theme_name(activeAvailableThemeIndex); + window_text_input_open(w, widgetIndex, 3348, 5240, 1170, (uint32)activeThemeName, 64); } break; } @@ -509,7 +463,7 @@ static void window_themes_resize(rct_window *w) static void window_themes_mousedown(int widgetIndex, rct_window* w, rct_widget* widget) { short newSelectedTab; - int num_items, i; + int num_items; switch (widgetIndex) { case WIDX_THEMES_SETTINGS_TAB: @@ -531,19 +485,15 @@ static void window_themes_mousedown(int widgetIndex, rct_window* w, rct_widget* window_invalidate(w); break; case WIDX_THEMES_PRESETS_DROPDOWN: - num_items = gConfigThemes.num_presets; + theme_manager_load_available_themes(); + num_items = (int)theme_manager_get_num_available_themes(); widget--; - gDropdownItemsFormat[0] = 2777; - gDropdownItemsArgs[0] = (uint32)&gConfigThemes.presets[1].name; - gDropdownItemsFormat[1] = 2777; - gDropdownItemsArgs[1] = (uint32)&gConfigThemes.presets[0].name; - - for (i = 2; i < num_items; i++) { + for (int i = 0; i < num_items; i++) { gDropdownItemsFormat[i] = 2777; - gDropdownItemsArgs[i] = (uint32)&gConfigThemes.presets[i].name; + gDropdownItemsArgs[i] = (uint32)theme_manager_get_available_theme_name(i); } - + window_dropdown_show_text_custom_width( w->x + widget->left, w->y + widget->top, @@ -552,42 +502,35 @@ static void window_themes_mousedown(int widgetIndex, rct_window* w, rct_widget* DROPDOWN_FLAG_STAY_OPEN, num_items, widget->right - widget->left - 3 - ); + ); - if (gCurrentTheme == 0 || gCurrentTheme == 1) { - dropdown_set_checked(gCurrentTheme ^ 1, true); - } else { - dropdown_set_checked(gCurrentTheme, true); - } + dropdown_set_checked(theme_manager_get_active_available_theme_index(), true); break; case WIDX_THEMES_RCT1_RIDE_LIGHTS: - if (gCurrentTheme >= 2) { - theme_get_preset()->features.rct1_ride_lights ^= 1; - themes_save_preset(gCurrentTheme); - window_invalidate_all(); - } - else { + if (theme_get_flags() & UITHEME_FLAG_PREDEFINED) { window_error_open(5241, STR_NONE); + } else { + theme_set_flags(theme_get_flags() ^ UITHEME_FLAG_USE_LIGHTS_RIDE); + theme_save(); + window_invalidate_all(); } break; case WIDX_THEMES_RCT1_PARK_LIGHTS: - if (gCurrentTheme >= 2) { - theme_get_preset()->features.rct1_park_lights ^= 1; - themes_save_preset(gCurrentTheme); - window_invalidate_all(); - } - else { + if (theme_get_flags() & UITHEME_FLAG_PREDEFINED) { window_error_open(5241, STR_NONE); + } else { + theme_set_flags(theme_get_flags() ^ UITHEME_FLAG_USE_LIGHTS_PARK); + theme_save(); + window_invalidate_all(); } break; case WIDX_THEMES_RCT1_SCENARIO_FONT: - if (gCurrentTheme >= 2) { - theme_get_preset()->features.rct1_scenario_font ^= 1; - themes_save_preset(gCurrentTheme); - window_invalidate_all(); - } - else { + if (theme_get_flags() & UITHEME_FLAG_PREDEFINED) { window_error_open(5241, STR_NONE); + } else { + theme_set_flags(theme_get_flags() ^ UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT); + theme_save(); + window_invalidate_all(); } break; } @@ -598,22 +541,22 @@ static void window_themes_dropdown(rct_window *w, int widgetIndex, int dropdownI switch (widgetIndex) { case WIDX_THEMES_LIST: if (dropdownIndex != -1) { - get_colour_scheme_tab()->colours[_color_index_2] = dropdownIndex | (get_colour_scheme_tab()->colours[_color_index_2] & 0x80); + rct_windowclass wc = get_window_class_tab_index(_color_index_1); + uint8 colour = theme_get_colour(wc, _color_index_2); + colour = (colour & COLOUR_FLAG_TRANSLUCENT) | dropdownIndex; + theme_set_colour(wc, _color_index_2, colour); window_invalidate_all(); _color_index_1 = -1; _color_index_2 = -1; - if (gCurrentTheme >= 2) - themes_save_preset(gCurrentTheme); + // if (gCurrentTheme >= 2) + // themes_save_preset(gCurrentTheme); } break; case WIDX_THEMES_PRESETS_DROPDOWN: if (dropdownIndex != -1) { - if (dropdownIndex == 0 || dropdownIndex == 1) - dropdownIndex ^= 1; - theme_change_preset(dropdownIndex); + theme_manager_set_active_available_theme(dropdownIndex); } - config_save_default(); break; } @@ -653,33 +596,36 @@ void window_themes_scrollmousedown(rct_window *w, int scrollIndex, int x, int y) int y2 = y % _row_height; _color_index_1 = y / _row_height; _color_index_2 = ((x - _button_offset_x) / 12); - if (_color_index_2 < get_colour_scheme_tab_definition()->num_colours) { + + rct_windowclass wc = get_window_class_tab_index(_color_index_1); + int numColours = theme_desc_get_num_colours(wc); + if (_color_index_2 < numColours) { if (x >= _button_offset_x && x < _button_offset_x + 12 * 6 && y2 >= _button_offset_y && y2 < _button_offset_y + 11) { - if (gCurrentTheme >= 2) { + if (theme_get_flags() & UITHEME_FLAG_PREDEFINED) { + window_error_open(5241, 5256); + } else { window_themes_widgets[WIDX_THEMES_COLORBTN_MASK].left = _button_offset_x + _color_index_2 * 12 + window_themes_widgets[WIDX_THEMES_LIST].left; window_themes_widgets[WIDX_THEMES_COLORBTN_MASK].top = _color_index_1 * _row_height + _button_offset_y - w->scrolls[0].v_top + window_themes_widgets[WIDX_THEMES_LIST].top; window_themes_widgets[WIDX_THEMES_COLORBTN_MASK].right = window_themes_widgets[WIDX_THEMES_COLORBTN_MASK].left + 12; window_themes_widgets[WIDX_THEMES_COLORBTN_MASK].bottom = window_themes_widgets[WIDX_THEMES_COLORBTN_MASK].top + 12; - window_dropdown_show_colour(w, &(window_themes_widgets[WIDX_THEMES_COLORBTN_MASK]), w->colours[1], get_colour_scheme_tab()->colours[_color_index_2]); + + uint8 colour = theme_get_colour(wc, _color_index_2); + window_dropdown_show_colour(w, &(window_themes_widgets[WIDX_THEMES_COLORBTN_MASK]), w->colours[1], colour); widget_invalidate(w, WIDX_THEMES_LIST); } - else { - window_error_open(5241, 5256); - } } else if (x >= _button_offset_x && x < _button_offset_x + 12 * 6 - 1 && y2 >= _check_offset_y && y2 < _check_offset_y + 11) { - if (gCurrentTheme >= 2) { - if (get_colour_scheme_tab()->colours[_color_index_2] & 0x80) { - get_colour_scheme_tab()->colours[_color_index_2] &= 0x7F; - } - else { - get_colour_scheme_tab()->colours[_color_index_2] |= 0x80; - } - themes_save_preset(gCurrentTheme); - window_invalidate_all(); - } - else { + if (theme_get_flags() & UITHEME_FLAG_PREDEFINED) { window_error_open(5241, 5256); + } else { + uint8 colour = theme_get_colour(wc, _color_index_2); + if (colour & COLOUR_FLAG_TRANSLUCENT) { + colour &= ~COLOUR_FLAG_TRANSLUCENT; + } else { + colour |= COLOUR_FLAG_TRANSLUCENT; + } + theme_set_colour(wc, _color_index_2, colour); + window_invalidate_all(); } } } @@ -702,25 +648,26 @@ static void window_themes_textinput(rct_window *w, int widgetIndex, char *text) case WIDX_THEMES_RENAME_BUTTON: if (filename_valid_characters(text)) { bool nameTaken = false; - for (int i = 0; i < gConfigThemes.num_presets; i++) { - if (strcmp(gConfigThemes.presets[i].name, text) == 0) { - window_error_open(5242, STR_NONE); + int numAvailableThemes = (int)theme_manager_get_num_available_themes(); + for (int i = 0; i < numAvailableThemes; i++) { + const utf8 * themeName = theme_manager_get_available_theme_name(i); + if (strcmp(themeName, text) == 0) { + if (widgetIndex != WIDX_THEMES_RENAME_BUTTON) { + window_error_open(5242, STR_NONE); + } nameTaken = true; break; } } if (!nameTaken) { if (widgetIndex == WIDX_THEMES_DUPLICATE_BUTTON) { - theme_create_preset(gCurrentTheme, text); + theme_duplicate(text); + } else { + theme_rename(text); } - else { - theme_rename_preset(gCurrentTheme, text); - } - config_save_default(); window_invalidate(w); } - } - else { + } else { window_error_open(5243, STR_NONE); } break; @@ -789,9 +736,9 @@ void window_themes_invalidate(rct_window *w) window_themes_widgets[WIDX_THEMES_PRESETS].type = WWT_EMPTY; window_themes_widgets[WIDX_THEMES_PRESETS_DROPDOWN].type = WWT_EMPTY; - widget_set_checkbox_value(w, WIDX_THEMES_RCT1_RIDE_LIGHTS, theme_get_preset()->features.rct1_ride_lights); - widget_set_checkbox_value(w, WIDX_THEMES_RCT1_PARK_LIGHTS, theme_get_preset()->features.rct1_park_lights); - widget_set_checkbox_value(w, WIDX_THEMES_RCT1_SCENARIO_FONT, theme_get_preset()->features.rct1_scenario_font); + widget_set_checkbox_value(w, WIDX_THEMES_RCT1_RIDE_LIGHTS, theme_get_flags() & UITHEME_FLAG_USE_LIGHTS_RIDE); + widget_set_checkbox_value(w, WIDX_THEMES_RCT1_PARK_LIGHTS, theme_get_flags() & UITHEME_FLAG_USE_LIGHTS_PARK); + widget_set_checkbox_value(w, WIDX_THEMES_RCT1_SCENARIO_FONT, theme_get_flags() & UITHEME_FLAG_USE_ALTERNATIVE_SCENARIO_SELECT_FONT); } else { window_themes_widgets[WIDX_THEMES_LIST].type = WWT_SCROLL; @@ -813,7 +760,9 @@ void window_themes_paint(rct_window *w, rct_drawpixelinfo *dpi) window_themes_draw_tab_images(dpi, w); if (_selected_tab == WINDOW_THEMES_TAB_SETTINGS) { - RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS + 0, uint32) = (uint32)&gConfigThemes.presets[gCurrentTheme].name; + int activeAvailableThemeIndex = theme_manager_get_active_available_theme_index(); + const utf8 * activeThemeName = theme_manager_get_available_theme_name(activeAvailableThemeIndex); + RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS + 0, uint32) = (uint32)activeThemeName; gfx_draw_string_left(dpi, 5238, NULL, w->colours[1], w->x + 10, w->y + window_themes_widgets[WIDX_THEMES_PRESETS].top + 1); gfx_draw_string_left_clipped( dpi, @@ -823,7 +772,7 @@ void window_themes_paint(rct_window *w, rct_drawpixelinfo *dpi) w->x + window_themes_widgets[WIDX_THEMES_PRESETS].left + 1, w->y + window_themes_widgets[WIDX_THEMES_PRESETS].top, w->x + window_themes_widgets[WIDX_THEMES_PRESETS_DROPDOWN].left - window_themes_widgets[WIDX_THEMES_PRESETS].left - 4 - ); + ); } else if (_selected_tab == WINDOW_THEMES_TAB_FEATURES) { @@ -871,18 +820,20 @@ void window_themes_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scroll } } - for (int j = 0; j < get_colour_scheme_tab_definition_by_index(i)->num_colours; j++) { + rct_windowclass wc = get_window_class_tab_index(i); + int numColours = theme_desc_get_num_colours(wc); + for (uint8 j = 0; j < numColours; j++) { + gfx_draw_string_left(dpi, theme_desc_get_name(wc), NULL, w->colours[1], 2, y + 4); - gfx_draw_string_left(dpi, get_colour_scheme_tab_definition_by_index(i)->name, NULL, w->colours[1], 2, y + 4); - - uint32 image = ((get_colour_scheme_tab_by_index(i)->colours[j] & 0x7F) << 19) + 0x600013C3; + uint8 colour = theme_get_colour(wc, j); + uint32 image = ((colour & ~COLOUR_FLAG_TRANSLUCENT) << 19) + 0x600013C3; if (i == _color_index_1 && j == _color_index_2) { - image = ((get_colour_scheme_tab_by_index(i)->colours[j] & 0x7F) << 19) + 0x600013C4; + image = ((colour & ~COLOUR_FLAG_TRANSLUCENT) << 19) + 0x600013C4; } gfx_draw_sprite(dpi, image, _button_offset_x + 12 * j, y + _button_offset_y, 0); gfx_fill_rect_inset(dpi, _button_offset_x + 12 * j, y + _check_offset_y, _button_offset_x + 12 * j + 9, y + _check_offset_y + 10, w->colours[1], 0xE0); - if (get_colour_scheme_tab_by_index(i)->colours[j] & 0x80) { + if (colour & COLOUR_FLAG_TRANSLUCENT) { RCT2_GLOBAL(RCT2_ADDRESS_CURRENT_FONT_SPRITE_BASE, sint16) = -1; gfx_draw_string(dpi, (char*)CheckBoxMarkString, w->colours[1] & 0x7F, _button_offset_x + 12 * j, y + _check_offset_y); } @@ -893,9 +844,3 @@ void window_themes_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scroll y += _row_height; } } - -#endif - -void window_themes_open() -{ -} From 5c2e951d3f37d09940b69e34e836d71d4cedb3ec Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Sat, 30 Jan 2016 11:12:25 +0000 Subject: [PATCH 08/13] fix remaining issues with theme refactor branch --- src/core/Json.cpp | 2 +- src/core/List.hpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/Json.cpp b/src/core/Json.cpp index 0c89336d73..bfa0ba3f48 100644 --- a/src/core/Json.cpp +++ b/src/core/Json.cpp @@ -13,7 +13,7 @@ namespace Json size_t fileLength = (size_t)fs.GetLength(); if (fileLength > maxSize) { - throw IOException("Language file too large."); + throw IOException("Json file too large."); } utf8 * fileData = Memory::Allocate(fileLength + 1); diff --git a/src/core/List.hpp b/src/core/List.hpp index 2c750f4833..78706d7a3a 100644 --- a/src/core/List.hpp +++ b/src/core/List.hpp @@ -47,7 +47,7 @@ public: void Insert(T item, size_t index) { Guard::ArgumentInRange(index, (size_t)0, this->size()); - this->insert() + this->insert(this->begin() + index, item); } bool Remove(T item) From c99ec93295b251ece4eda065c16e77673d1df0fb Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Sat, 30 Jan 2016 14:00:41 +0000 Subject: [PATCH 09/13] add theme entries for new windows --- data/language/english_uk.txt | 2 ++ src/interface/Theme.cpp | 6 ++++++ src/windows/changelog.c | 14 +++++++------- src/windows/multiplayer.c | 5 ++--- src/windows/network_status.c | 5 ++--- src/windows/player.c | 4 +--- src/windows/server_list.c | 2 ++ src/windows/server_start.c | 5 ++--- src/windows/themes.c | 10 ++++++++-- src/windows/tile_inspector.c | 6 +++--- 10 files changed, 35 insertions(+), 24 deletions(-) diff --git a/data/language/english_uk.txt b/data/language/english_uk.txt index ce2a16a2ea..44c976010f 100644 --- a/data/language/english_uk.txt +++ b/data/language/english_uk.txt @@ -4040,6 +4040,8 @@ STR_5733 :Use NN scaling at integer scales # tooltip for tab in options window STR_5734 :{SMALLFONT}{BLACK}Rendering +STR_5719 :Network Status + ############# # Scenarios # ################ diff --git a/src/interface/Theme.cpp b/src/interface/Theme.cpp index 6459751713..8d10b3f956 100644 --- a/src/interface/Theme.cpp +++ b/src/interface/Theme.cpp @@ -150,6 +150,12 @@ WindowThemeDesc WindowThemeDescriptors[] = { THEME_WC(WC_EDITOR_TRACK_BOTTOM_TOOLBAR), 5247, COLOURS_3(TRANSLUCENT(COLOUR_LIGHT_BLUE), TRANSLUCENT(COLOUR_LIGHT_BLUE), TRANSLUCENT(COLOUR_LIGHT_BLUE) ) }, { THEME_WC(WC_EDITOR_SCENARIO_BOTTOM_TOOLBAR), 5248, COLOURS_3(TRANSLUCENT(COLOUR_LIGHT_BROWN), TRANSLUCENT(COLOUR_LIGHT_BROWN), TRANSLUCENT(COLOUR_MOSS_GREEN) ) }, { THEME_WC(WC_TITLE_EDITOR), 5433, COLOURS_3(COLOUR_GREY, COLOUR_OLIVE_GREEN, COLOUR_OLIVE_GREEN ) }, + { THEME_WC(WC_TILE_INSPECTOR), 5314, COLOURS_2(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_CHANGELOG), 5344, COLOURS_2(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_MULTIPLAYER), 5502, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_PLAYER), 5496, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_NETWORK_STATUS), 5719, COLOURS_1(COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_SERVER_LIST), 5498, COLOURS_2(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, }; #pragma endregion diff --git a/src/windows/changelog.c b/src/windows/changelog.c index fa37953a29..ee067b94d4 100644 --- a/src/windows/changelog.c +++ b/src/windows/changelog.c @@ -1,14 +1,15 @@ #include "../addresses.h" #include "../localisation/localisation.h" +#include "../interface/themes.h" #include "../interface/widget.h" #include "../interface/window.h" #include "../interface/viewport.h" -#include "../world/scenery.h" -#include "../world/map.h" -#include "../world/footpath.h" -#include "../util/util.h" #include "../openrct2.h" #include "../platform/platform.h" +#include "../util/util.h" +#include "../world/footpath.h" +#include "../world/map.h" +#include "../world/scenery.h" enum { WIDX_BACKGROUND, @@ -105,9 +106,6 @@ rct_window *window_changelog_open() window->enabled_widgets = (1 << WIDX_CLOSE); window_init_scroll_widgets(window); - window->colours[0] = 7; - window->colours[1] = 7; - window->colours[2] = 7; window->min_width = MIN_WW; window->min_height = MIN_WH; window->max_width = MIN_WW; @@ -157,6 +155,8 @@ static void window_changelog_scrollgetsize(rct_window *w, int scrollIndex, int * static void window_changelog_invalidate(rct_window *w) { + colour_scheme_update(w); + window_changelog_widgets[WIDX_BACKGROUND].right = w->width - 1; window_changelog_widgets[WIDX_BACKGROUND].bottom = w->height - 1; window_changelog_widgets[WIDX_TITLE].right = w->width - 2; diff --git a/src/windows/multiplayer.c b/src/windows/multiplayer.c index 7760a5129b..cf6aa6004e 100644 --- a/src/windows/multiplayer.c +++ b/src/windows/multiplayer.c @@ -205,9 +205,6 @@ void window_multiplayer_open() window->page = WINDOW_MULTIPLAYER_PAGE_PLAYERS; window->list_information_type = 0; - window->colours[0] = 7; - window->colours[1] = 7; - window->colours[2] = 7; } static void window_multiplayer_set_page(rct_window* w, int page){ @@ -366,6 +363,8 @@ static void window_multiplayer_players_scrollmouseover(rct_window *w, int scroll static void window_multiplayer_players_invalidate(rct_window *w) { + colour_scheme_update(w); + window_multiplayer_set_pressed_tab(w); window_multiplayer_anchor_border_widgets(w); window_multiplayer_players_widgets[WIDX_LIST].right = w->width - 4; diff --git a/src/windows/network_status.c b/src/windows/network_status.c index 7720824cc2..05820b355c 100644 --- a/src/windows/network_status.c +++ b/src/windows/network_status.c @@ -106,9 +106,6 @@ void window_network_status_open(const char* text) window->page = 0; window->list_information_type = 0; - window->colours[0] = 7; - window->colours[1] = 7; - window->colours[2] = 7; } void window_network_status_close() @@ -158,6 +155,8 @@ static void window_network_status_textinput(rct_window *w, int widgetIndex, char static void window_network_status_invalidate(rct_window *w) { + colour_scheme_update(w); + window_network_status_widgets[WIDX_BACKGROUND].right = w->width - 1; window_network_status_widgets[WIDX_BACKGROUND].bottom = w->height - 1; window_network_status_widgets[WIDX_TITLE].right = w->width - 2; diff --git a/src/windows/player.c b/src/windows/player.c index 9011428f94..f6b20375fc 100644 --- a/src/windows/player.c +++ b/src/windows/player.c @@ -376,9 +376,7 @@ void window_player_overview_invalidate(rct_window *w) window_init_scroll_widgets(w); } - w->colours[0] = 7; - w->colours[1] = 7; - w->colours[2] = 7; + colour_scheme_update(w); w->pressed_widgets &= ~(WIDX_TAB_1); w->pressed_widgets |= 1ULL << (w->page + WIDX_TAB_1); diff --git a/src/windows/server_list.c b/src/windows/server_list.c index 8d17f43184..2dfac03f2a 100644 --- a/src/windows/server_list.c +++ b/src/windows/server_list.c @@ -367,6 +367,8 @@ static void window_server_list_textinput(rct_window *w, int widgetIndex, char *t static void window_server_list_invalidate(rct_window *w) { + colour_scheme_update(w); + RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS, char *) = gVersion; window_server_list_widgets[WIDX_BACKGROUND].right = w->width - 1; window_server_list_widgets[WIDX_BACKGROUND].bottom = w->height - 1; diff --git a/src/windows/server_start.c b/src/windows/server_start.c index 5f4321d3ed..483db2280a 100644 --- a/src/windows/server_start.c +++ b/src/windows/server_start.c @@ -136,9 +136,6 @@ void window_server_start_open() window->page = 0; window->list_information_type = 0; - window->colours[0] = 1; - window->colours[1] = 26; - window->colours[2] = 26; sprintf(_port, "%u", gConfigNetwork.default_port); safe_strcpy(_name, gConfigNetwork.server_name, sizeof(_name)); @@ -251,6 +248,8 @@ static void window_server_start_textinput(rct_window *w, int widgetIndex, char * static void window_server_start_invalidate(rct_window *w) { + colour_scheme_update_by_class(w, WC_SERVER_LIST); + widget_set_checkbox_value(w, WIDX_ADVERTISE_CHECKBOX, gConfigNetwork.advertise); RCT2_GLOBAL(RCT2_ADDRESS_COMMON_FORMAT_ARGS + 18, uint16) = gConfigNetwork.maxplayers; } diff --git a/src/windows/themes.c b/src/windows/themes.c index eddb70fe94..b24dfa3bab 100644 --- a/src/windows/themes.c +++ b/src/windows/themes.c @@ -238,12 +238,17 @@ static rct_windowclass window_themes_tab_5_classes[] = { static rct_windowclass window_themes_tab_6_classes[] = { WC_CHEATS, + WC_TILE_INSPECTOR, WC_THEMES, WC_TITLE_EDITOR, WC_OPTIONS, WC_KEYBOARD_SHORTCUT_LIST, WC_CHANGE_KEYBOARD_SHORTCUT, - WC_LOADSAVE + WC_LOADSAVE, + WC_CHANGELOG, + WC_SERVER_LIST, + WC_MULTIPLAYER, + WC_PLAYER, }; static rct_windowclass window_themes_tab_7_classes[] = { @@ -251,7 +256,8 @@ static rct_windowclass window_themes_tab_7_classes[] = { WC_DEMOLISH_RIDE_PROMPT, WC_FIRE_PROMPT, WC_TRACK_DELETE_PROMPT, - WC_LOADSAVE_OVERWRITE_PROMPT + WC_LOADSAVE_OVERWRITE_PROMPT, + WC_NETWORK_STATUS, }; static rct_windowclass *window_themes_tab_classes[] = { diff --git a/src/windows/tile_inspector.c b/src/windows/tile_inspector.c index 1425bd957d..770cd81ab8 100644 --- a/src/windows/tile_inspector.c +++ b/src/windows/tile_inspector.c @@ -20,6 +20,7 @@ #include "../addresses.h" #include "../localisation/localisation.h" +#include "../interface/themes.h" #include "../interface/widget.h" #include "../interface/window.h" #include "../interface/viewport.h" @@ -170,9 +171,6 @@ void window_tile_inspector_open() window->disabled_widgets = (1 << WIDX_CORRUPT) | (1 << WIDX_MOVE_UP) | (1 << WIDX_MOVE_DOWN) | (1 << WIDX_REMOVE); window_init_scroll_widgets(window); - window->colours[0] = 7; - window->colours[1] = 7; - window->colours[2] = 7; window->min_width = MIN_WW; window->min_height = MIN_WH; window->max_width = MAX_WW; @@ -433,6 +431,8 @@ static void window_tile_inspector_scrollmouseover(rct_window *w, int scrollIndex static void window_tile_inspector_invalidate(rct_window *w) { + colour_scheme_update(w); + window_tile_inspector_widgets[WIDX_BACKGROUND].right = w->width - 1; window_tile_inspector_widgets[WIDX_BACKGROUND].bottom = w->height - 1; window_tile_inspector_widgets[WIDX_CLOSE].left = w->width - 13; From d274abdcceb4b92bcfd7b73df74f53209ac6d50a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Sun, 31 Jan 2016 12:11:09 +0100 Subject: [PATCH 10/13] Fixes for GCC Makes jansson library required as well --- CMakeLists.txt | 10 +++++----- src/core/List.hpp | 4 +++- src/core/Memory.hpp | 6 +++--- src/core/String.cpp | 2 +- src/core/String.hpp | 2 +- 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index fd2eba5f94..6fd2bdbabf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -51,6 +51,7 @@ if (NOT PNG_FOUND) endif (NOT PNG_FOUND) PKG_CHECK_MODULES(ZLIB REQUIRED zlib) +PKG_CHECK_MODULES(JANSSON REQUIRED jansson>=2.7) # Handle creating the rct2 text and data files on OS X and Linux # See details in src/openrct2.c:openrct2_setup_rct2_segment for how the values @@ -143,23 +144,22 @@ else (STATIC) endif (STATIC) if (STATIC) - SET(REQUIREDLIBS ${PNG_STATIC_LIBRARIES} ${ZLIB_STATIC_LIBRARIES}) + SET(REQUIREDLIBS ${PNG_STATIC_LIBRARIES} ${JANSSON_STATIC_LIBRARIES} ${ZLIB_STATIC_LIBRARIES}) else (STATIC) - SET(REQUIREDLIBS ${PNG_LIBRARIES} ${ZLIB_LIBRARIES}) + SET(REQUIREDLIBS ${PNG_LIBRARIES} ${JANSSON_LIBRARIES} ${ZLIB_LIBRARIES}) endif (STATIC) if (NOT DISABLE_HTTP_TWITCH) PKG_CHECK_MODULES(LIBCURL REQUIRED libcurl) - PKG_CHECK_MODULES(JANSSON REQUIRED jansson>=2.7) if (WIN32) # Curl depends on openssl and ws2 in mingw builds, but is not wired up in pkg-config PKG_CHECK_MODULES(SSL REQUIRED openssl) set(WSLIBS ws2_32) endif (WIN32) if (STATIC) - SET(HTTPLIBS ${LIBCURL_STATIC_LIBRARIES} ${JANSSON_STATIC_LIBRARIES} ${SSL_STATIC_LIBRARIES} ${WSLIBS}) + SET(HTTPLIBS ${LIBCURL_STATIC_LIBRARIES} ${SSL_STATIC_LIBRARIES} ${WSLIBS}) else (STATIC) - SET(HTTPLIBS ${LIBCURL_LIBRARIES} ${JANSSON_LIBRARIES} ${SSL_LIBRARIES} ${WSLIBS}) + SET(HTTPLIBS ${LIBCURL_LIBRARIES} ${SSL_LIBRARIES} ${WSLIBS}) endif (STATIC) endif (NOT DISABLE_HTTP_TWITCH) diff --git a/src/core/List.hpp b/src/core/List.hpp index 78706d7a3a..80fdf724f6 100644 --- a/src/core/List.hpp +++ b/src/core/List.hpp @@ -14,6 +14,8 @@ template class List : public std::vector { public: + typedef typename std::vector::const_reference const_reference; + typedef typename std::vector::reference reference; size_t GetCapacity() const { return this->capacity(); } size_t GetCount() const { return this->size(); } const T * GetItems() const { return this->data(); } @@ -54,7 +56,7 @@ public: { for (size_t i = 0; i < this->size(); i++) { - if (_items[i] == item) + if (*this[i] == item) { RemoveAt(i); return true; diff --git a/src/core/Memory.hpp b/src/core/Memory.hpp index 50d9dfbf6a..713422a6d8 100644 --- a/src/core/Memory.hpp +++ b/src/core/Memory.hpp @@ -31,7 +31,7 @@ namespace Memory template T * Reallocate(T * ptr, size_t size) { - if (ptr == NULL) + if (ptr == nullptr) { return (T*)malloc(size); } @@ -44,7 +44,7 @@ namespace Memory template T * ReallocateArray(T * ptr, size_t count) { - if (ptr == NULL) + if (ptr == nullptr) { return (T*)malloc(count * sizeof(T)); } @@ -105,7 +105,7 @@ namespace Memory { for (size_t i = 0; i < count; i++) { - ptr[i]::~T(); + ptr[i].~T(); } Free(ptr); } diff --git a/src/core/String.cpp b/src/core/String.cpp index ca1a7f97a9..d721bf2b24 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -60,7 +60,7 @@ namespace String { const utf8 * lastOccurance = nullptr; const utf8 * ch = str; - for (; ch != '\0'; ch++) + for (; *ch != '\0'; ch++) { if (*ch == match) { diff --git a/src/core/String.hpp b/src/core/String.hpp index 7af30db4f4..8708176937 100644 --- a/src/core/String.hpp +++ b/src/core/String.hpp @@ -7,7 +7,7 @@ extern "C" namespace String { - constexpr utf8 * Empty = ""; + constexpr const utf8 * Empty = ""; bool IsNullOrEmpty(const utf8 * str); bool Equals(const utf8 * a, const utf8 * b, bool ignoreCase = false); From 0b8567fa8081839900257db8e9ab50f4566ff527 Mon Sep 17 00:00:00 2001 From: LRFLEW Date: Mon, 1 Feb 2016 21:29:28 -0700 Subject: [PATCH 11/13] Updated Xcode Project for theme-refactor --- OpenRCT2.xcodeproj/project.pbxproj | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/OpenRCT2.xcodeproj/project.pbxproj b/OpenRCT2.xcodeproj/project.pbxproj index b9765ac3e7..70e37754c6 100644 --- a/OpenRCT2.xcodeproj/project.pbxproj +++ b/OpenRCT2.xcodeproj/project.pbxproj @@ -15,6 +15,8 @@ D43532601C34730200BA219B /* libpng.dylib in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = D435325E1C3472E500BA219B /* libpng.dylib */; settings = {ATTRIBUTES = (CodeSignOnCopy, ); }; }; D46105CE1C38828D00DB1EE3 /* scenario_sources.c in Sources */ = {isa = PBXBuildFile; fileRef = D46105CD1C38828D00DB1EE3 /* scenario_sources.c */; }; D47304D51C4FF8250015C0EA /* libz.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = D47304D41C4FF8250015C0EA /* libz.tbd */; }; + D4A3511E1C6067C400CBCBA4 /* Json.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D4A3511C1C6067C400CBCBA4 /* Json.cpp */; }; + D4A351211C60680300CBCBA4 /* Theme.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D4A351201C60680300CBCBA4 /* Theme.cpp */; }; D4ABAB061C2F812B0080CAD9 /* news_options.c in Sources */ = {isa = PBXBuildFile; fileRef = D4ABAB051C2F812B0080CAD9 /* news_options.c */; }; D4B63B8F1C43025600367A37 /* CommandLine.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D4B63B8A1C43025600367A37 /* CommandLine.cpp */; }; D4B63B901C43025600367A37 /* RootCommands.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D4B63B8C1C43025600367A37 /* RootCommands.cpp */; }; @@ -54,7 +56,6 @@ D4EC47F81C26342F0024B507 /* graph.c in Sources */ = {isa = PBXBuildFile; fileRef = D4EC470F1C26342F0024B507 /* graph.c */; }; D4EC47F91C26342F0024B507 /* keyboard_shortcut.c in Sources */ = {isa = PBXBuildFile; fileRef = D4EC47111C26342F0024B507 /* keyboard_shortcut.c */; }; D4EC47FA1C26342F0024B507 /* screenshot.c in Sources */ = {isa = PBXBuildFile; fileRef = D4EC47131C26342F0024B507 /* screenshot.c */; }; - D4EC47FB1C26342F0024B507 /* themes.c in Sources */ = {isa = PBXBuildFile; fileRef = D4EC47151C26342F0024B507 /* themes.c */; }; D4EC47FC1C26342F0024B507 /* title_sequences.c in Sources */ = {isa = PBXBuildFile; fileRef = D4EC47171C26342F0024B507 /* title_sequences.c */; }; D4EC47FD1C26342F0024B507 /* viewport.c in Sources */ = {isa = PBXBuildFile; fileRef = D4EC47191C26342F0024B507 /* viewport.c */; }; D4EC47FE1C26342F0024B507 /* viewport_interaction.c in Sources */ = {isa = PBXBuildFile; fileRef = D4EC471B1C26342F0024B507 /* viewport_interaction.c */; }; @@ -229,6 +230,12 @@ D47304D41C4FF8250015C0EA /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; }; D4895D321C23EFDD000CD788 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = Info.plist; path = distribution/osx/Info.plist; sourceTree = SOURCE_ROOT; }; D497D0781C20FD52002BF46A /* OpenRCT2.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = OpenRCT2.app; sourceTree = BUILT_PRODUCTS_DIR; }; + D4A3511A1C6067B000CBCBA4 /* Diagnostics.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Diagnostics.hpp; sourceTree = ""; }; + D4A3511B1C6067B900CBCBA4 /* Guard.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Guard.hpp; sourceTree = ""; }; + D4A3511C1C6067C400CBCBA4 /* Json.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Json.cpp; sourceTree = ""; }; + D4A3511D1C6067C400CBCBA4 /* Json.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = Json.hpp; sourceTree = ""; }; + D4A3511F1C6067E500CBCBA4 /* List.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = List.hpp; sourceTree = ""; }; + D4A351201C60680300CBCBA4 /* Theme.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Theme.cpp; sourceTree = ""; }; D4ABAB051C2F812B0080CAD9 /* news_options.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = news_options.c; sourceTree = ""; }; D4B63B8A1C43025600367A37 /* CommandLine.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CommandLine.cpp; sourceTree = ""; }; D4B63B8B1C43025600367A37 /* CommandLine.hpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.h; path = CommandLine.hpp; sourceTree = ""; }; @@ -304,7 +311,6 @@ D4EC47121C26342F0024B507 /* keyboard_shortcut.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = keyboard_shortcut.h; sourceTree = ""; }; D4EC47131C26342F0024B507 /* screenshot.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = screenshot.c; sourceTree = ""; }; D4EC47141C26342F0024B507 /* screenshot.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = screenshot.h; sourceTree = ""; }; - D4EC47151C26342F0024B507 /* themes.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = themes.c; sourceTree = ""; }; D4EC47161C26342F0024B507 /* themes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = themes.h; sourceTree = ""; }; D4EC47171C26342F0024B507 /* title_sequences.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = title_sequences.c; sourceTree = ""; }; D4EC47181C26342F0024B507 /* title_sequences.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = title_sequences.h; sourceTree = ""; }; @@ -728,10 +734,15 @@ children = ( D4B63B931C43028200367A37 /* Console.cpp */, D4B63B941C43028200367A37 /* Console.hpp */, + D4A3511A1C6067B000CBCBA4 /* Diagnostics.hpp */, D4EC46E61C26342F0024B507 /* Exception.hpp */, D4EC46E71C26342F0024B507 /* FileStream.hpp */, + D4A3511B1C6067B900CBCBA4 /* Guard.hpp */, D4EC46E81C26342F0024B507 /* IDisposable.hpp */, D4EC46E91C26342F0024B507 /* IStream.hpp */, + D4A3511C1C6067C400CBCBA4 /* Json.cpp */, + D4A3511D1C6067C400CBCBA4 /* Json.hpp */, + D4A3511F1C6067E500CBCBA4 /* List.hpp */, D4EC46EA1C26342F0024B507 /* Math.hpp */, D4EC46EB1C26342F0024B507 /* Memory.hpp */, D4D35E2A1C45BD9B00AAFCB4 /* Path.cpp */, @@ -781,7 +792,7 @@ D4EC47121C26342F0024B507 /* keyboard_shortcut.h */, D4EC47131C26342F0024B507 /* screenshot.c */, D4EC47141C26342F0024B507 /* screenshot.h */, - D4EC47151C26342F0024B507 /* themes.c */, + D4A351201C60680300CBCBA4 /* Theme.cpp */, D4EC47161C26342F0024B507 /* themes.h */, D4EC47171C26342F0024B507 /* title_sequences.c */, D4EC47181C26342F0024B507 /* title_sequences.h */, @@ -1351,6 +1362,7 @@ D4EC48401C26342F0024B507 /* guest_list.c in Sources */, D4EC482F1C26342F0024B507 /* banner.c in Sources */, D4EC47E91C26342F0024B507 /* font.c in Sources */, + D4A3511E1C6067C400CBCBA4 /* Json.cpp in Sources */, D4EC48021C26342F0024B507 /* convert.c in Sources */, D4EC48481C26342F0024B507 /* mapgen.c in Sources */, D4EC48441C26342F0024B507 /* loadsave.c in Sources */, @@ -1368,6 +1380,7 @@ D4EC481A1C26342F0024B507 /* posix.c in Sources */, D4D35E2C1C45BD9B00AAFCB4 /* Path.cpp in Sources */, D4B63B981C43028F00367A37 /* String.cpp in Sources */, + D4A351211C60680300CBCBA4 /* Theme.cpp in Sources */, D4EC47E31C26342F0024B507 /* cmdline_sprite.c in Sources */, D4EC48611C26342F0024B507 /* text_input.c in Sources */, D4EC48431C26342F0024B507 /* land_rights.c in Sources */, @@ -1481,7 +1494,6 @@ D4EC480D1C26342F0024B507 /* marketing.c in Sources */, D4EC48721C26342F0024B507 /* balloon.c in Sources */, D4EC48571C26342F0024B507 /* save_prompt.c in Sources */, - D4EC47FB1C26342F0024B507 /* themes.c in Sources */, D4EC48701C26342F0024B507 /* viewport.c in Sources */, ); runOnlyForDeploymentPostprocessing = 0; @@ -1515,9 +1527,7 @@ GCC_DYNAMIC_NO_PIC = NO; GCC_NO_COMMON_BLOCKS = YES; GCC_OPTIMIZATION_LEVEL = 0; - GCC_PREPROCESSOR_DEFINITIONS = ( - "DEBUG=1", - ); + GCC_PREPROCESSOR_DEFINITIONS = "DEBUG=1"; GCC_WARN_64_TO_32_BIT_CONVERSION = YES; GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; GCC_WARN_FOUR_CHARACTER_CONSTANTS = YES; From 72bed4f6772d74be4c1b7f238c6792d84fb80386 Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Wed, 3 Feb 2016 18:28:25 +0000 Subject: [PATCH 12/13] fix theme window strings --- data/language/english_uk.txt | 4 ++-- src/interface/Theme.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/data/language/english_uk.txt b/data/language/english_uk.txt index 44c976010f..1785482a2b 100644 --- a/data/language/english_uk.txt +++ b/data/language/english_uk.txt @@ -4039,8 +4039,8 @@ STR_5732 :Anisotropic STR_5733 :Use NN scaling at integer scales # tooltip for tab in options window STR_5734 :{SMALLFONT}{BLACK}Rendering - -STR_5719 :Network Status +STR_5735 :Network Status +STR_5736 :Player ############# # Scenarios # diff --git a/src/interface/Theme.cpp b/src/interface/Theme.cpp index 8d10b3f956..27bd6f2822 100644 --- a/src/interface/Theme.cpp +++ b/src/interface/Theme.cpp @@ -153,8 +153,8 @@ WindowThemeDesc WindowThemeDescriptors[] = { THEME_WC(WC_TILE_INSPECTOR), 5314, COLOURS_2(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, { THEME_WC(WC_CHANGELOG), 5344, COLOURS_2(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, { THEME_WC(WC_MULTIPLAYER), 5502, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, - { THEME_WC(WC_PLAYER), 5496, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, - { THEME_WC(WC_NETWORK_STATUS), 5719, COLOURS_1(COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_PLAYER), 5736, COLOURS_3(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, + { THEME_WC(WC_NETWORK_STATUS), 5735, COLOURS_1(COLOUR_LIGHT_BLUE ) }, { THEME_WC(WC_SERVER_LIST), 5498, COLOURS_2(COLOUR_LIGHT_BLUE, COLOUR_LIGHT_BLUE ) }, }; From 17a48db98c6c696dbbe097bdbc1862d97114e12f Mon Sep 17 00:00:00 2001 From: IntelOrca Date: Wed, 3 Feb 2016 18:39:08 +0000 Subject: [PATCH 13/13] fix duplication of theme --- src/interface/Theme.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/interface/Theme.cpp b/src/interface/Theme.cpp index 27bd6f2822..d8dc0cee60 100644 --- a/src/interface/Theme.cpp +++ b/src/interface/Theme.cpp @@ -53,7 +53,7 @@ public: uint8 Flags; UITheme(const utf8 * name); - UITheme(const UITheme & name); + UITheme(const UITheme & copy); ~UITheme(); void SetName(const utf8 * name); @@ -750,11 +750,15 @@ extern "C" utf8 newPath[MAX_PATH]; ThemeManager::GetThemeFileName(newPath, sizeof(newPath), name); - String::DiscardDuplicate(&ThemeManager::CurrentThemePath, newPath); - ThemeManager::CurrentTheme->SetName(name); - ThemeManager::CurrentTheme->Flags &= ~UITHEME_FLAG_PREDEFINED; - ThemeManager::CurrentTheme->WriteToFile(ThemeManager::CurrentThemePath); + // Copy the theme, save it and then load it back in + UITheme * newTheme = new UITheme(*ThemeManager::CurrentTheme); + newTheme->SetName(name); + newTheme->Flags &= ~UITHEME_FLAG_PREDEFINED; + newTheme->WriteToFile(newPath); + delete newTheme; + + ThemeManager::LoadTheme(newPath); theme_manager_load_available_themes(); for (size_t i = 0; i < ThemeManager::AvailableThemes.GetCount(); i++)