From 87fdeeb000e9268f64668fe9ac6364d8a3bd6b34 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Fri, 24 Jul 2020 11:27:38 +0200 Subject: [PATCH 1/2] UnderWater effect: Add ability to change default color/texture Signed-off-by: martinRenou --- ipygany/ipygany.py | 5 ++++- package-lock.json | 27 +++++++++++++++++++++++ package.json | 1 + src/widget.ts | 53 +++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 82 insertions(+), 4 deletions(-) diff --git a/ipygany/ipygany.py b/ipygany/ipygany.py index 64209ee..f760026 100644 --- a/ipygany/ipygany.py +++ b/ipygany/ipygany.py @@ -11,7 +11,7 @@ from ipywidgets import ( widget_serialization, DOMWidget, Widget, - Color + Color, Image ) from .serialization import array_serialization, component_array_serialization @@ -459,6 +459,9 @@ class UnderWater(Effect): input = Union((Tuple(trait=Unicode, minlen=2, maxlen=2), Unicode())).tag(sync=True) + default_color = Color('#F2FFD2').tag(sync=True) + texture = Instance(Image, allow_none=True, default_value=None).tag(sync=True, **widget_serialization) + @default('input') def _default_input(self): return self.parent.data[0].name diff --git a/package-lock.json b/package-lock.json index b9a5b01..5c324fd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -31,6 +31,23 @@ } } }, + "@jupyter-widgets/controls": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@jupyter-widgets/controls/-/controls-2.0.0.tgz", + "integrity": "sha512-g0SRSXJ/P5yM23scZl+8zXR1Jz783Uf7H5tq6ucTibMk8mXxDIXn09Rgal4K17XH2LSesd0dkhTzMmlLPMAH7g==", + "requires": { + "@jupyter-widgets/base": "^3.0.0", + "@lumino/algorithm": "^1.1.0", + "@lumino/domutils": "^1.1.0", + "@lumino/messaging": "^1.2.1", + "@lumino/signaling": "^1.2.0", + "@lumino/widgets": "^1.3.0", + "d3-format": "^1.3.0", + "jquery": "^3.1.1", + "jquery-ui": "^1.12.1", + "underscore": "^1.8.3" + } + }, "@jupyterlab/coreutils": { "version": "4.2.0", "resolved": "https://registry.npmjs.org/@jupyterlab/coreutils/-/coreutils-4.2.0.tgz", @@ -1800,6 +1817,11 @@ "integrity": "sha1-WW6WmP0MgOEgOMK4LW6xs1tiJNk=", "dev": true }, + "d3-format": { + "version": "1.4.4", + "resolved": "https://registry.npmjs.org/d3-format/-/d3-format-1.4.4.tgz", + "integrity": "sha512-TWks25e7t8/cqctxCmxpUuzZN11QxIA7YrMbram94zMQ0PXjE4LVIMe/f6a4+xxL8HQ3OsAFULOINQi1pE62Aw==" + }, "date-format": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/date-format/-/date-format-1.2.0.tgz", @@ -3446,6 +3468,11 @@ "resolved": "https://registry.npmjs.org/jquery/-/jquery-3.5.1.tgz", "integrity": "sha512-XwIBPqcMn57FxfT+Go5pzySnm4KWkT1Tv7gjrpT1srtf8Weynl6R273VJ5GjkRb51IzMp5nbaPjJXMWeju2MKg==" }, + "jquery-ui": { + "version": "1.12.1", + "resolved": "https://registry.npmjs.org/jquery-ui/-/jquery-ui-1.12.1.tgz", + "integrity": "sha1-vLQEXI3QU5wTS8FIjN0+dop6nlE=" + }, "js-yaml": { "version": "3.14.0", "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.14.0.tgz", diff --git a/package.json b/package.json index 8f7d081..d181d1f 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ }, "dependencies": { "@jupyter-widgets/base": "^1.1.10 || ^2 || ^3", + "@jupyter-widgets/controls": "^2.0.0", "backbone": "^1.4.0", "binary-search-tree": "^0.2.6", "ganyjs": "^0.4.0", diff --git a/src/widget.ts b/src/widget.ts index c60c8a2..30834ae 100644 --- a/src/widget.ts +++ b/src/widget.ts @@ -7,6 +7,10 @@ import { WidgetModel, DOMWidgetModel, DOMWidgetView, ISerializers, unpack_models } from '@jupyter-widgets/base'; +import { + ImageView +} from '@jupyter-widgets/controls'; + import { Message } from '@phosphor/messaging'; @@ -186,8 +190,8 @@ abstract class BlockModel extends _GanyWidgetModel { return this.get('environment_meshes').map((model: any) => model.obj); } - get defaultColor () : string { - return this.get('default_color'); + get defaultColor () : THREE.Color { + return new THREE.Color(this.get('default_color')); } initEventListeners() : void { @@ -588,23 +592,66 @@ class UnderWaterModel extends EffectModel { defaults() { return {...super.defaults(), _model_name: UnderWaterModel.model_name, + default_color: '#F2FFD2', + texture: null, }; } + initialize (attributes: any, options: any) { + super.initialize(attributes, options); + + this.setTexture(); + } + get input () { const input = this.get('input'); return typeof input == 'string' ? input : [input]; } + get defaultcolor () { + return new THREE.Color(this.get('default_color')); + } + + setTexture () { + const image = this.get('texture'); + + if (image === null) { + this.block.texture = null; + + return; + } + + this.widget_manager.create_view(image, {}).then((imageView: ImageView) => { + const textureLoader = new THREE.TextureLoader(); + + textureLoader.load(imageView.el.src, (texture: THREE.Texture) => { + this.block.texture = texture; + }); + }); + } + createBlock () { - return new UnderWater(this.parent.block, this.input); + return new UnderWater(this.parent.block, this.input, { defaultColor: this.defaultColor }); + } + + initEventListeners() : void { + super.initEventListeners(); + + this.on('change:texture', () => { + this.setTexture(); + }); } block: UnderWater; static model_name = 'UnderWaterModel'; + static serializers: ISerializers = { + ...EffectModel.serializers, + texture: { deserialize: (unpack_models as any) }, + } + } From a045f84df902f4520f99d292b10e67ca7381d454 Mon Sep 17 00:00:00 2001 From: martinRenou Date: Fri, 24 Jul 2020 15:50:00 +0200 Subject: [PATCH 2/2] Update GanyJS to latest Signed-off-by: martinRenou --- package-lock.json | 6 +++--- package.json | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5c324fd..86b28b7 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2745,9 +2745,9 @@ "dev": true }, "ganyjs": { - "version": "0.4.0", - "resolved": "https://registry.npmjs.org/ganyjs/-/ganyjs-0.4.0.tgz", - "integrity": "sha512-CC0VYr+l8FGHaoDrMQavGpxdAvQknkiqUPcCuwKpikI63gYakUh7ie+f/dXUmg6U2J+RyIPEVX05/D9H2byCFA==", + "version": "0.5.0", + "resolved": "https://registry.npmjs.org/ganyjs/-/ganyjs-0.5.0.tgz", + "integrity": "sha512-qAHgYGDxM3AjeL4wFCClecSdxBG7MPMU5l1UCh5Q/Nbea5ooiyb0JUkDRSBTObER3VBT9B6xI4+yWBcmMsPAAA==", "requires": { "backbone": "^1.4.0", "binary-search-tree": "^0.2.6", diff --git a/package.json b/package.json index d181d1f..e852177 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,7 @@ "@jupyter-widgets/controls": "^2.0.0", "backbone": "^1.4.0", "binary-search-tree": "^0.2.6", - "ganyjs": "^0.4.0", + "ganyjs": "^0.5.0", "three": "^0.118.0", "uuid": "^3.3.3" },