Skip to content

Commit 1ccaf45

Browse files
authored
Add range attribute to Threshold (#78)
* add range attribute * link range to min/max * fix defaults * link min/max to range * set range defaults from min/max
1 parent 14e8571 commit 1ccaf45

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

docs/source/api_reference/threshold.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Examples
2424
.. jupyter-execute::
2525

2626
import numpy as np
27-
from ipywidgets import FloatSlider, VBox, jslink
27+
from ipywidgets import FloatSlider, FloatRangeSlider, VBox, jslink
2828
from ipygany import Scene, Threshold, PolyMesh, Component
2929

3030

@@ -76,8 +76,10 @@ Examples
7676
# Create a slider that will dynamically change the boundaries of the threshold
7777
threshold_slider_min = FloatSlider(value=height_min, min=-0.3, max=1., step=0.006)
7878
threshold_slider_max = FloatSlider(value=height_max, min=-0.3, max=1., step=0.006)
79+
threshold_slider_range = FloatRangeSlider(value=[height_min, height_max], min=-0.3, max=1.0, step=0.006)
7980

8081
jslink((threshold_mesh, 'min'), (threshold_slider_min, 'value'))
8182
jslink((threshold_mesh, 'max'), (threshold_slider_max, 'value'))
83+
jslink((threshold_mesh, 'range'), (threshold_slider_range, 'value'))
8284

83-
VBox((Scene([threshold_mesh]), threshold_slider_min, threshold_slider_max))
85+
VBox((Scene([threshold_mesh]), threshold_slider_min, threshold_slider_max, threshold_slider_range))

ipygany/ipygany.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,9 +603,14 @@ class Threshold(Effect):
603603

604604
min = CFloat(0.).tag(sync=True)
605605
max = CFloat(0.).tag(sync=True)
606+
range = Tuple((0., 0.)).tag(sync=True)
606607
dynamic = Bool(False).tag(sync=True)
607608
inclusive = Bool(True).tag(sync=True)
608609

610+
def __init__(self, parent, **kwargs):
611+
super().__init__(parent, **kwargs)
612+
self.range = (self.min, self.max)
613+
609614
@property
610615
def input_dim(self):
611616
"""Input dimension."""

src/widget.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,7 @@ class ThresholdModel extends EffectModel {
586586
_model_name: ThresholdModel.model_name,
587587
min: 0.,
588588
max: 0.,
589+
range: [0., 0.],
589590
dynamic: false,
590591
inclusive: true,
591592
};
@@ -595,10 +596,26 @@ class ThresholdModel extends EffectModel {
595596
return this.get('min');
596597
}
597598

599+
set min (value: number) {
600+
this.set('min', value)
601+
}
602+
598603
get max () {
599604
return this.get('max');
600605
}
601606

607+
set max (value: number) {
608+
this.set('max', value)
609+
}
610+
611+
get range () {
612+
return this.get('range');
613+
}
614+
615+
set range (value: [number, number]) {
616+
this.set('range', value)
617+
}
618+
602619
get dynamic () {
603620
return this.get('dynamic');
604621
}
@@ -623,8 +640,18 @@ class ThresholdModel extends EffectModel {
623640
initEventListeners () : void {
624641
super.initEventListeners();
625642

626-
this.on('change:min', () => { this.block.min = this.min });
627-
this.on('change:max', () => { this.block.max = this.max });
643+
this.on('change:min', () => {
644+
this.block.min = this.min;
645+
this.range = [this.min, this.range[1]];
646+
});
647+
this.on('change:max', () => {
648+
this.block.max = this.max;
649+
this.range = [this.range[0], this.max];
650+
});
651+
this.on('change:range', () => {
652+
this.min = this.range[0];
653+
this.max = this.range[1];
654+
});
628655
this.on('change:inclusive', () => { this.block.inclusive = this.inclusive });
629656
}
630657

0 commit comments

Comments
 (0)