From 53aebc0ef8f3cdd62831ff6669b96d3495ea1716 Mon Sep 17 00:00:00 2001 From: Michael Moore Date: Mon, 9 Sep 2024 12:33:15 -0500 Subject: [PATCH] feat(ui): separate map mode controls from other map settings --- .../src/lib/SettingControl/index.svelte | 7 ++ src/renderer/src/lib/Sidebar.svelte | 28 ++++++- src/renderer/src/lib/map/MapContainer.svelte | 25 +++--- src/renderer/src/lib/map/SystemIcons.svelte | 79 ++++++++++++------- src/renderer/src/lib/map/data/mapModes.ts | 5 ++ .../src/lib/settings/mapSettingsConfig.ts | 2 + src/renderer/src/lib/settings/utils.ts | 27 ++++++- 7 files changed, 123 insertions(+), 50 deletions(-) diff --git a/src/renderer/src/lib/SettingControl/index.svelte b/src/renderer/src/lib/SettingControl/index.svelte index 0025f30..8f0af99 100644 --- a/src/renderer/src/lib/SettingControl/index.svelte +++ b/src/renderer/src/lib/SettingControl/index.svelte @@ -19,6 +19,7 @@ import StrokeSettingControl from './StrokeSettingControl.svelte'; export let settings: Writable>; + export let writeToSettings: Writable>[] = []; export let config: UnknownSettingConfig; let value: any = get(settings)[config.id]; @@ -31,6 +32,12 @@ ...prev, [config.id]: value, })); + for (const otherSettings of writeToSettings) { + otherSettings.update((prev) => ({ + ...prev, + [config.id]: value, + })); + } } } diff --git a/src/renderer/src/lib/Sidebar.svelte b/src/renderer/src/lib/Sidebar.svelte index 5e26c1c..a1f67cb 100644 --- a/src/renderer/src/lib/Sidebar.svelte +++ b/src/renderer/src/lib/Sidebar.svelte @@ -19,6 +19,7 @@ import { applyMapSettings, asUnknownSettingConfig, + copyGroupSettings, countryOptions, editedMapSettings, lastProcessedMapSettings, @@ -160,7 +161,12 @@ ? presetMapSettings.find((preset) => preset.name === loadedSettingsName) : $customSavedSettings.find((saved) => saved.name === loadedSettingsName); let confirmed = true; - if (!loadedSettings || settingsAreDifferent(loadedSettings.settings, $editedMapSettings)) { + if ( + !loadedSettings || + settingsAreDifferent(loadedSettings.settings, $editedMapSettings, { + excludeGroups: ['mapMode'], + }) + ) { confirmed = await new Promise((resolve) => { modalStore.trigger({ type: 'confirm', @@ -172,8 +178,12 @@ } if (confirmed) { loadedSettingsKey.set(`${type}|${savedSettings.name}`); - if (settingsAreDifferent(savedSettings.settings, $mapSettings)) { - const validated = validateAndResetMapSettings(savedSettings.settings); + if ( + settingsAreDifferent(savedSettings.settings, $mapSettings, { excludeGroups: ['mapMode'] }) + ) { + const validated = validateAndResetMapSettings( + copyGroupSettings('mapMode', $editedMapSettings, savedSettings.settings), + ); editedMapSettings.set(validated); mapSettings.set(validated); lastProcessedMapSettings.set(validated); @@ -271,6 +281,16 @@ +
+ {#each mapSettingsConfig[0]?.settings ?? [] as config (config.id)} + + {/each} +
+

{$t('side_bar.map_settings')}

{/if} + {#if dataOrNull} + + {/if}
- {#if dataOrNull} - - {/if} {#if tooltip != null && openedSystem == null && !tooltip.hidden && !zooming && !resizing}
diff --git a/src/renderer/src/lib/map/SystemIcons.svelte b/src/renderer/src/lib/map/SystemIcons.svelte index 4b31a57..1878d94 100644 --- a/src/renderer/src/lib/map/SystemIcons.svelte +++ b/src/renderer/src/lib/map/SystemIcons.svelte @@ -155,7 +155,41 @@ } -{#each getMapModeIcons(data.systems) as system} +{#each data.systems + .filter((s) => s.systemIsKnown || !$mapSettings.terraIncognita) + .map((s) => getSystemIcons(s, $mapSettings)) as systemIcons} + {#each [...(systemIcons.center ? [systemIcons.center] : []), ...systemIcons.left, ...systemIcons.right, ...systemIcons.top, ...systemIcons.bottom] as systemIcon} + + {/each} + {#if shouldShowLabel(systemIcons.system)} + + {systemIcons.system.name} + + {/if} +{/each} + +{#each getMapModeIcons(data.systems.filter((s) => s.systemIsKnown || !$mapSettings.terraIncognita)) as system} {#if system.arcs.length <= 1} {/each} + {#each data.systems .filter((s) => s.systemIsKnown || !$mapSettings.terraIncognita) + .filter(shouldShowLabel) .map((s) => getSystemIcons(s, $mapSettings)) as systemIcons} - {#each [...(systemIcons.center ? [systemIcons.center] : []), ...systemIcons.left, ...systemIcons.right, ...systemIcons.top, ...systemIcons.bottom] as systemIcon} - - {/each} - {#if shouldShowLabel(systemIcons.system)} - - {systemIcons.system.name} - - {/if} + + {systemIcons.system.name} + {/each} diff --git a/src/renderer/src/lib/map/data/mapModes.ts b/src/renderer/src/lib/map/data/mapModes.ts index 0c8f987..5c74a7b 100644 --- a/src/renderer/src/lib/map/data/mapModes.ts +++ b/src/renderer/src/lib/map/data/mapModes.ts @@ -10,6 +10,8 @@ interface MapMode { tooltipLabel?: MessageID; country?: MapModeBorder[]; system?: MapModeSystem; + hasPov?: boolean; + hasSpecies?: boolean; } interface MapModeBorder { @@ -47,6 +49,7 @@ export const mapModes: Record = { id: 'wars', name: 'map_mode.wars.name', tooltipLabel: 'map_mode.wars.tooltip_label', + hasPov: true, country: [ { label: 'map_mode.common.selected_country', @@ -175,6 +178,7 @@ export const mapModes: Record = { id: 'populationSpecies', name: 'map_mode.population.name_species', tooltipLabel: 'map_mode.population.tooltip_label', + hasSpecies: true, country: [ { label: null, @@ -244,6 +248,7 @@ export const mapModes: Record = { id: 'fleetPowerAlliedAndHostile', name: 'map_mode.fleet_power.name_allied_and_hostile', tooltipLabel: 'map_mode.fleet_power.tooltip_label', + hasPov: true, country: [ { label: null, diff --git a/src/renderer/src/lib/settings/mapSettingsConfig.ts b/src/renderer/src/lib/settings/mapSettingsConfig.ts index c1a7375..94ac585 100644 --- a/src/renderer/src/lib/settings/mapSettingsConfig.ts +++ b/src/renderer/src/lib/settings/mapSettingsConfig.ts @@ -24,6 +24,7 @@ export const mapSettingsConfig: MapSettingConfigGroup[] = [ options: [{ id: 'player', name: 'option.country.player' }], dynamicOptions: countryOptions, requiresReprocessing: true, + hideIf: (settings) => !mapModes[settings.mapMode]?.hasPov, }, { id: 'mapModeSpecies', @@ -31,6 +32,7 @@ export const mapSettingsConfig: MapSettingConfigGroup[] = [ options: [{ id: 'player', name: 'option.country.player' }], dynamicOptions: speciesOptions, requiresReprocessing: true, + hideIf: (settings) => !mapModes[settings.mapMode]?.hasSpecies, }, ], }, diff --git a/src/renderer/src/lib/settings/utils.ts b/src/renderer/src/lib/settings/utils.ts index 2cabfed..9fbf7a5 100644 --- a/src/renderer/src/lib/settings/utils.ts +++ b/src/renderer/src/lib/settings/utils.ts @@ -7,8 +7,7 @@ import { colorSettingSchema } from './ColorSetting'; import { iconSettingSchema } from './IconSetting'; import { defaultMapSettings, type MapSettings } from './mapSettings'; import { mapSettingsConfig } from './mapSettingsConfig'; -import type { UnknownSettingConfig } from './SettingConfig'; -import type { AppSettingConfig, MapSettingConfig } from './SettingConfig'; +import type { AppSettingConfig, MapSettingConfig, UnknownSettingConfig } from './SettingConfig'; import { strokeSettingSchema } from './StrokeSetting'; export function isColorDynamic(color: string, settings: MapSettings): boolean { @@ -46,6 +45,22 @@ export function validateAndResetMapSettings(unvalidatedSettings: MapSettings): M return settings; } +export function copyGroupSettings( + groupId: string, + fromSettings: MapSettings, + toSettings: MapSettings, +) { + const newSettings = { + ...toSettings, + }; + const group = mapSettingsConfig.find((group) => group.id === groupId); + group?.settings.forEach((setting) => { + // @ts-expect-error -- ts doesn't recognize that since the same expression is used for the key, the value will be the right type + newSettings[setting.id] = fromSettings[setting.id]; + }); + return newSettings; +} + export function validateSetting( value: unknown, config: T, @@ -139,12 +154,18 @@ export function asUnknownSettingConfig(config: AppSettingConfig | MapSettingConf export function asKnownSettingId(id: string) { return id as keyof MapSettings | keyof AppSettings; } + +interface SettingsAreDifferentOptions { + requiresReprocessingOnly?: boolean; + excludeGroups?: string[]; +} export function settingsAreDifferent( a: MapSettings, b: MapSettings, - { requiresReprocessingOnly = false } = {}, + { requiresReprocessingOnly = false, excludeGroups }: SettingsAreDifferentOptions = {}, ) { return mapSettingsConfig + .filter((group) => !excludeGroups?.includes(group.id)) .flatMap((group) => group.settings) .filter((setting) => !requiresReprocessingOnly || setting.requiresReprocessing) .some((setting) => {