Skip to content

Commit

Permalink
Merge pull request #36 from martinRenou/underwater_effect_texturing
Browse files Browse the repository at this point in the history
UnderWater effect: Add ability to change default color/texture
  • Loading branch information
martinRenou authored Jul 24, 2020
2 parents 924de32 + a045f84 commit 83ee461
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 8 deletions.
5 changes: 4 additions & 1 deletion ipygany/ipygany.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from ipywidgets import (
widget_serialization,
DOMWidget, Widget,
Color
Color, Image
)

from .serialization import array_serialization, component_array_serialization
Expand Down Expand Up @@ -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
Expand Down
33 changes: 30 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,10 @@
},
"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",
"ganyjs": "^0.5.0",
"three": "^0.118.0",
"uuid": "^3.3.3"
},
Expand Down
53 changes: 50 additions & 3 deletions src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) },
}

}


Expand Down

0 comments on commit 83ee461

Please sign in to comment.