Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2657 create settingsjs file #2659

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions metaversefile-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import React from 'react';
import * as ReactThreeFiber from '@react-three/fiber';
import metaversefile from 'metaversefile';
import {getRenderer, scene, sceneHighPriority, sceneLowPriority, rootScene, camera} from './renderer.js';
import settingsManager from './settings-manager.js';
import cameraManager from './camera-manager.js';
import physicsManager from './physics-manager.js';
import Avatar from './avatars/avatars.js';
Expand Down Expand Up @@ -725,6 +726,9 @@ metaversefile.setApi({
useProcGen() {
return procgen;
},
useSettingsManager() {
return settingsManager;
},
useCameraManager() {
return cameraManager;
},
Expand Down
42 changes: 42 additions & 0 deletions settings-manager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class SettingsManager extends EventTarget {
constructor() {
super();

this.addEventListener('gfxSettingsChanged', e => {
Copy link
Contributor

@avaer avaer Apr 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The casing between event names and data is inconsistent. Also, we generally do not camelcase in event names, which is the standard for the web platform; it's usually all lowercase.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noted

const settings = e.data;
this.#setSettings('GfxSettings', settings);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to separate these events at all?

Is there ever a case, besides the settings panel itself, where we are generally interested in a page of settings as opposed to a setting itself? What would be the purpose of such a notification/categorizing it this way for users of this API, other than confusion?

IMO it makes more sense to emit that a specific setting value changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll change it to a single event.

I also agree that a single value makes more sense. It's currently accepting the full set of perameters because of how the saveSettings functions are written in the settings tabs, But I'll adjust accordingly

});

this.addEventListener('controlSettingsChanged', e => {
const settings = e.data;
this.#setSettings('ControlsSettings', settings);
});

this.addEventListener('aiSettingsChanged', e => {
const settings = e.data;
this.#setSettings('AiSettings', settings);
});

this.addEventListener('audioSettingsChanged', e => {
const settings = e.data;
this.#setSettings('AudioSettings', settings);
});


}

getSettings(key) {
return localStorage.getItem(key);
}

getSettingsJson(key) {
return JSON.parse(localStorage.getItem(key));
}

#setSettings(key, settings) {
localStorage.setItem(key, JSON.stringify(settings));
}
}

const settingsManager = new SettingsManager();
export default settingsManager;
8 changes: 5 additions & 3 deletions src/components/general/settings/TabAi.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import React, { useEffect, useState } from 'react';
import classNames from 'classnames';

import metaversefileApi from '../../../../metaversefile-api';
import { Switch } from './switch';
import loreAI from '../../../../ai/lore/lore-ai';
import preauthenticator from '../../../../preauthenticator';
Expand Down Expand Up @@ -112,16 +113,17 @@ export const TabAi = ({ active }) => {
}
}

localStorage.setItem( 'AiSettings', JSON.stringify( settings ) );
metaversefileApi.useSettingsManager().dispatchEvent(new MessageEvent('aiSettingsChanged', { data: settings }));

updateLoreEndpoint(apiType);

};

async function loadSettings () {

// load local storage
const settingsString = localStorage.getItem( 'AiSettings' );
// load ai settings
const settingsString = metaversefileApi.useSettingsManager().getSettings('AiSettings');

let settings;

try {
Expand Down
6 changes: 4 additions & 2 deletions src/components/general/settings/TabAudio.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { voicePacksUrl, voiceEndpointsUrl } from '../../../../constants';
import game from '../../../../game';
import { Slider } from './slider';
import * as voices from '../../../../voices';
import metaversefileApi from '../../../../metaversefile-api';

import styles from './settings.module.css';

Expand Down Expand Up @@ -55,13 +56,14 @@ export const TabAudio = ({ active }) => {
voiceEndpoint: voiceEndpoint,
};

localStorage.setItem( 'AudioSettings', JSON.stringify( settings ) );
metaversefileApi.useSettingsManager().dispatchEvent(new MessageEvent('audioSettingsChanged', { data: settings }));

};

function loadSettings () {

const settingsString = localStorage.getItem( 'AudioSettings' );
const settingsString = metaversefileApi.useSettingsManager().getSettings('AudioSettings');

let settings;

try {
Expand Down
6 changes: 4 additions & 2 deletions src/components/general/settings/TabControls.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { useState, useEffect } from 'react';
import classNames from 'classnames';

import { KeyInput } from './key-input';
import metaversefileApi from '../../../../metaversefile-api';

import styles from './settings.module.css';

Expand Down Expand Up @@ -55,13 +56,14 @@ export const TabControls = ({ active }) => {
inventory
};

localStorage.setItem( 'ControlsSettings', JSON.stringify( settings ) );
metaversefileApi.useSettingsManager().dispatchEvent(new MessageEvent('controlSettingsChanged', { data: settings }));

};

function loadSettings () {

const settingsString = localStorage.getItem( 'ControlsSettings' );
const settingsString = metaversefileApi.useSettingsManager().getSettings('ControlsSettings');

let settings;

try {
Expand Down
6 changes: 3 additions & 3 deletions src/components/general/settings/TabGraphics.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, { useEffect, useState } from 'react';
import classNames from 'classnames';

import game from '../../../../game.js';
import metaversefileApi from '../../../../metaversefile-api'
import metaversefileApi from '../../../../metaversefile-api';
import { Switch } from './switch';

import styles from './settings.module.css';
Expand Down Expand Up @@ -65,13 +65,13 @@ export const TabGraphics = ({ active }) => {
}
};

localStorage.setItem( 'GfxSettings', JSON.stringify( settings ) );
metaversefileApi.useSettingsManager().dispatchEvent(new MessageEvent('gfxSettingsChanged', { data: settings }));

};

function loadSettings () {

const settingsString = localStorage.getItem( 'GfxSettings' );
const settingsString = metaversefileApi.useSettingsManager().getSettings('GfxSettings');
let settings;

try {
Expand Down