@urso/core 0.4.2 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@urso/core",
3
- "version": "0.4.2",
3
+ "version": "0.4.3",
4
4
  "description": "HTML5 game engine",
5
5
  "main": "build/js/index.js",
6
6
  "author": "Megbrimef",
package/src/js/app.js CHANGED
@@ -44,6 +44,7 @@ class App {
44
44
 
45
45
  //Modules
46
46
  Urso.assets = Urso.getInstance('Modules.Assets.Controller');
47
+ Urso.i18n = Urso.getInstance('Modules.I18n.Controller');
47
48
  Urso.transport = Urso.getInstance('Modules.Transport.Controller');
48
49
  Urso.logic = Urso.getInstance('Modules.Logic.Controller');
49
50
  Urso.objects = Urso.getInstance('Modules.Objects.Controller');
@@ -1,5 +1,6 @@
1
1
  Urso.Core.Modules = {};
2
2
  require('./assets/_info.js');
3
+ require('./i18n/_info.js');
3
4
  require('./instances/_info.js');
4
5
  require('./logic/_info.js');
5
6
  require('./objects/_info.js');
@@ -38,7 +38,7 @@ class ModulesAssetsService {
38
38
  Urso.scenes.loadUpdate(Math.floor(param.progress));
39
39
  }
40
40
 
41
- loadGroup(group, callback) {
41
+ loadGroup(group, callback = () => { }) {
42
42
  if (!this.assets[group])
43
43
  return Urso.logger.error('ModulesAssetsService group error, no assets:' + group + ' Check ModulesAssetsConfig please');
44
44
 
@@ -0,0 +1,4 @@
1
+ Urso.Core.Modules.I18n = {
2
+ Config: require('./config.js'),
3
+ Controller: require('./controller.js')
4
+ };
@@ -0,0 +1,17 @@
1
+ class ModulesI18nConfig {
2
+ constructor() {
3
+ this.singleton = true;
4
+
5
+ /**
6
+ * example of locales. Set please your keys and paths
7
+ */
8
+ this.locales = {
9
+ 'de': 'i18n/de.json',
10
+ 'en': 'i18n/en.json',
11
+ 'ru': 'i18n/ru.json'
12
+ }
13
+ }
14
+ };
15
+
16
+ module.exports = ModulesI18nConfig;
17
+
@@ -0,0 +1,64 @@
1
+ class ModulesI18nController {
2
+
3
+ #vocabulary = null;
4
+
5
+ /**
6
+ * get text by localeId
7
+ * @param {String} localeId
8
+ */
9
+ get(localeId) {
10
+ return this.#vocabulary && this.#vocabulary[localeId] || localeId;
11
+ }
12
+
13
+ /**
14
+ * set alredy loaded as asset locale
15
+ * @param {String} localeKey - loaded json asset key with locale
16
+ */
17
+ setLocale(localeKey) {
18
+ const jsonResource = Urso.cache.getJson(localeKey);
19
+
20
+ if (!jsonResource)
21
+ return Urso.logger.error('ModulesI18nController setLocale error, no loaded json:' + localeKey + '. Check assets please');
22
+
23
+ this.#vocabulary = jsonResource.data;
24
+ this.emit(Urso.events.MODULES_I18N_NEW_LOCALE_WAS_SET, localeKey);
25
+ }
26
+
27
+ /**
28
+ * load json with locale, store as asset witn localeKey as key and set locale
29
+ * @param {String} localeKey
30
+ * @param {String} [pathToLocaleJson] if this param will not used - we will check localeKey in config
31
+ */
32
+ loadAndSetLocale(localeKey, pathToLocaleJson) {
33
+ //check if localeKey was alredy loaded
34
+ const jsonResource = Urso.cache.getJson(localeKey);
35
+
36
+ if (jsonResource) {
37
+ this.setLocale(localeKey);
38
+ }
39
+
40
+ //load locale by path
41
+ if (pathToLocaleJson) {
42
+ this._loadLocaleByPath(localeKey, pathToLocaleJson);
43
+ } else { //we will check localeKey in config
44
+ this._loadLocaleByConfig(localeKey);
45
+ }
46
+ }
47
+
48
+ _loadLocaleByConfig(localeKey) {
49
+ const locales = this.getInstance('Config').locales;
50
+ const pathToLocaleJson = locales[localeKey];
51
+
52
+ if (!pathToLocaleJson)
53
+ return Urso.logger.error('ModulesI18nController _loadLocaleByConfig error, no locale data in config:' + localeKey + '. Check ModulesI18nConfig please');
54
+
55
+ this._loadLocaleByPath(localeKey, pathToLocaleJson);
56
+ }
57
+
58
+ _loadLocaleByPath(localeKey, pathToLocaleJson) {
59
+ const localeAsset = { type: Urso.types.assets.JSON, key: localeKey, path: pathToLocaleJson };
60
+ Urso.assets.preload(localeAsset, () => this.setLocale(localeKey));
61
+ }
62
+ }
63
+
64
+ module.exports = ModulesI18nController;
@@ -10,6 +10,8 @@ class ModulesObjectsModelsBitmapText extends Urso.Core.Modules.Objects.BaseModel
10
10
  super.setupParams(params);
11
11
 
12
12
  this.text = Urso.helper.recursiveGet('text', params, false);
13
+ this.localeId = Urso.helper.recursiveGet('localeId', params, false); //you can use this instead text for localization
14
+
13
15
  this.fontName = Urso.helper.recursiveGet('fontName', params, false);
14
16
  this.fontSize = Urso.helper.recursiveGet('fontSize', params, false);
15
17
 
@@ -17,8 +19,20 @@ class ModulesObjectsModelsBitmapText extends Urso.Core.Modules.Objects.BaseModel
17
19
  }
18
20
 
19
21
  _addBaseObject() {
22
+ if (this.localeId)
23
+ this._originalModel.text = this.text = Urso.i18n.get(this.localeId);
24
+
20
25
  this._baseObject = new PIXI.BitmapText(this.text, { fontName: this.fontName, fontSize: this.fontSize });
21
26
  };
27
+
28
+ _newLocaleHandler() {
29
+ this.text = this._baseObject.text = Urso.i18n.get(this.localeId);
30
+ }
31
+
32
+ _subscribeOnce() {
33
+ if (this.localeId)
34
+ this.addListener(Urso.events.MODULES_I18N_NEW_LOCALE_WAS_SET, this._newLocaleHandler.bind(this));
35
+ }
22
36
  }
23
37
 
24
38
  module.exports = ModulesObjectsModelsBitmapText;
@@ -10,6 +10,7 @@ class ModulesObjectsModelsText extends Urso.Core.Modules.Objects.BaseModel {
10
10
  super.setupParams(params);
11
11
 
12
12
  this.text = Urso.helper.recursiveGet('text', params, false);
13
+ this.localeId = Urso.helper.recursiveGet('localeId', params, false); //you can use this instead text for localization
13
14
 
14
15
  this.lineHeight = Urso.helper.recursiveGet('lineHeight', params, 0);
15
16
  this.fontFamily = Urso.helper.recursiveGet('fontFamily', params, 'Arial');
@@ -31,8 +32,20 @@ class ModulesObjectsModelsText extends Urso.Core.Modules.Objects.BaseModel {
31
32
  }
32
33
 
33
34
  _addBaseObject() {
35
+ if (this.localeId)
36
+ this._originalModel.text = this.text = Urso.i18n.get(this.localeId);
37
+
34
38
  this._baseObject = new PIXI.Text(this.text);
35
39
  };
40
+
41
+ _newLocaleHandler() {
42
+ this.text = this._baseObject.text = Urso.i18n.get(this.localeId);
43
+ }
44
+
45
+ _subscribeOnce() {
46
+ if (this.localeId)
47
+ this.addListener(Urso.events.MODULES_I18N_NEW_LOCALE_WAS_SET, this._newLocaleHandler.bind(this));
48
+ }
36
49
  }
37
50
 
38
51
  module.exports = ModulesObjectsModelsText;
@@ -6,6 +6,7 @@ class ModulesObserverConfig {
6
6
  MODULES_ASSETS_GROUP_LOADED: 'modules.assets.group.loaded',
7
7
  MODULES_ASSETS_LOAD_PROGRESS: 'modules.assets.load.progress',
8
8
  MODULES_ASSETS_LAZYLOAD_FINISHED: 'modules.assets.lazyLoad.finished',
9
+ MODULES_I18N_NEW_LOCALE_WAS_SET: 'modules.i18n.new.locale.was.set',
9
10
  MODULES_INSTANCES_MODES_CHANGED: 'modules.instances.modes.changed',
10
11
  MODULES_OBJECTS_BUTTON_PRESS: 'modules.objects.button.press',
11
12
  MODULES_OBJECTS_HIT_AREA_PRESS: 'modules.objects.hitArea.press',