Skip to content

Commit

Permalink
rewrite example input_box.py
Browse files Browse the repository at this point in the history
  • Loading branch information
marcomusy committed Feb 7, 2024
1 parent db05779 commit 5899e99
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 64 deletions.
25 changes: 13 additions & 12 deletions docs/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
## Main Changes and Fixes

- fixes to `extrude()` thanks to @JeffreyWardman
- filter out triangle strips in Ribbon and extrude()
- filter out triangle strips in Ribbon and `extrude()`
- improvements in doc strings
- add `utils.madcad2vedo` conversion as per #976 by @JeffreyWardman
- add `utils.camera_to_dict()`
Expand Down Expand Up @@ -46,14 +46,17 @@
- add slot for triangle strips in constructor `Mesh([verts, faces, lines, strips])` in #1019
- internally use "import vedo.vtkclasses as vtki" instead of "vtk" to avoid confusion
- add `join_with_strips()` in #1043
- improvements to `shapes.Ellipsoid()` and bug fixes in #978 by @daniel-a-diaz
- improvements to `pointcloud.pca_ellipsoid()` and bug fixes
- improvements to `pointcloud.pca_ellipse()` and bug fixes
- fix plotter `a` toggle
- fix viz on jupyter notebook as per #994
- fix `mesh.imprint()`


## Soft Breaking Changes
Changes that will break existing code whose fixing is trivial
Changes that will break existing code whose fixing is trivial:

- improvements to `shapes.Ellipsoid()` and bug fixes in #978 by @daniel-a-diaz
- improvements to `pointcloud.pca_ellipsoid()` and bug fixes
- improvements to `pointcloud.pca_ellipse()` and bug fixes
- change `clone2d(scale=...)` to `clone2d(size=...)`
- remove `shapes.StreamLines()` becoming `object.compute_streamlines()`
- split `mesh.decimate()` into `mesh.decimate()`, `mesh.decimate_pro()` and `mesh.decimate_binned()` as per #992
Expand All @@ -64,7 +67,7 @@ Changes that will break existing code whose fixing is trivial
- modified API for `mesh.binarize()`
- `plotter.add_hover_legend()` now returns the id of the callback.
- removed `settings.render_lines_as_tubes` and `settings.hidden_line_removal`, add `plotter.render_hidden_lines()` method
- fix `close()`, `close_window()` is obsolete and removed.
- fix `close()`, `close_window()` is now obsolete and removed.


## Hard Breaking Changes
Expand All @@ -73,16 +76,12 @@ Changes that will break existing code and need active thinking and work to adapt
- None


### Bug Fixes
- fix plotter `a` toggle
- fix viz on jupyter notebook as per #994


## New/Revised Examples
```
examples/basic/sliders_hsv.py
examples/basic/buttons1.py
examples/basic/buttons2.py
examples/basic/input_box.py
examples/advanced/warp4b.py
examples/advanced/diffuse_data.py
Expand Down Expand Up @@ -115,6 +114,9 @@ tests/snippets/test_compare_fit1.py
```

### Broken Examples

Examples that are not fully functional and still need fixing:

```
markpoint.py
cut_and_cap.py
Expand All @@ -141,5 +143,4 @@ texturecubes.py
meshquality.py
streamlines1.py


madcad2 blocks tsts
64 changes: 25 additions & 39 deletions examples/basic/input_box.py
Original file line number Diff line number Diff line change
@@ -1,51 +1,37 @@
"""Start typing a color name then press return
E.g. pink4"""
from vedo import settings, dataurl, Plotter, Mesh
"""Start typing a color name for the mesh.
E.g.: pink4
(Press 'Esc' to exit)"""
from vedo import settings, dataurl, get_color_name
from vedo import Plotter, Mesh, Text2D

settings.enable_default_keyboard_callbacks = False

def kfunc(evt):
global msg
evt.keypress = evt.keypress.replace("period", ".")
if evt.keypress == "BackSpace" and msg:
msg = msg[:-1]
evt.keypress = ''
elif evt.keypress == "Return":
bfunc(0)
return
elif evt.keypress == "Escape":
plt.close()
key = evt.keypress.lower()
field_txt = field.text().strip() # strip leading/trailing spaces

if len(evt.keypress) > 1:
if key == "backspace" and field_txt:
key = ""
field_txt = field_txt[:-1]
elif key == "escape":
plt.close()
return
elif len(key) > 1:
return

msg += f"{evt.keypress}"
bu.text(msg)
plt.render()

def bfunc(obj, ename=""):
mesh.color(msg)
color_name = field_txt + key
field.text(f"{color_name:^12}").frame(color_name, lw=8)
mesh.color(color_name)
msg.text(get_color_name(color_name))
plt.render()


plt = Plotter(axes=1)
plt.remove_callback("CharEvent") # might be needed

msg = ""
plt.add_callback("key press", kfunc)
settings["enable_default_keyboard_callbacks"] = False

bu = plt.add_button(
bfunc,
pos=(0.5, 0.1), # x,y fraction from bottom left corner
states=["input box"],
c=["w"],
bc=["dg"], # colors of states
font="courier", # arial, courier, times
size=45,
bold=True,
)
mesh = Mesh(dataurl+"magnolia.vtk").color("black").flat()

mesh = Mesh(dataurl+"magnolia.vtk").c("v").flat()

plt.show(mesh, __doc__).close()
field = Text2D("black", pos="bottom-center",s=3, font="Meson", bg="k2", c="w", alpha=1)
msg = Text2D(pos="top-right", s=2, font="Quikhand", c="k1", bg="k7", alpha=1)

plt = Plotter()
plt.add_callback("key press", kfunc)
plt.show(mesh, field, msg, __doc__).close()
6 changes: 3 additions & 3 deletions vedo/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -777,9 +777,9 @@ def get_color(rgb=None, hsv=None):
if c.lower() in color_nicks:
c = color_nicks[c.lower()]
else:
vedo.logger.warning(
f"Unknown color nickname {c}\nAvailable abbreviations: {color_nicks}"
)
# vedo.logger.warning(
# f"Unknown color nickname {c}\nAvailable abbreviations: {color_nicks}"
# )
return (0.5, 0.5, 0.5)

if c.lower() in colors: # matplotlib name color
Expand Down
12 changes: 9 additions & 3 deletions vedo/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -706,7 +706,10 @@ def join_with_strips(self, b1, closed=True):
lines0 = b0.lines
lines1 = b1.lines
m = len(lines0)
assert m == len(lines1)
assert m == len(lines1), (
"lines must have the same number of points\n"
f"line {j} has {m} points in b0 and {len(lines1)} in b1"
)

strips = []
points = []
Expand All @@ -717,7 +720,10 @@ def join_with_strips(self, b1, closed=True):
ids1j = list(lines1[j])

n = len(ids0j)
assert n == len(ids1j)
assert n == len(ids1j), (
"lines must have the same number of points\n"
f"line {j} has {n} points in b0 and {len(ids1j)} in b1"
)

if closed:
ids0j.append(ids0j[0])
Expand All @@ -732,7 +738,7 @@ def join_with_strips(self, b1, closed=True):
points.append(vertices0[ids0j[ipt]])
points.append(vertices1[ids1j[ipt]])

strip = list(range(npt, npt+2*n))
strip = list(range(npt, npt + 2*n))
strips.append(strip)

return Mesh([points, [], [], strips], c="k6")
Expand Down
16 changes: 9 additions & 7 deletions vedo/shapes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4334,19 +4334,20 @@ def __init__(self):
else:
self.fontname = settings.default_font

def angle(self, a):
def angle(self, value):
"""Orientation angle in degrees"""
self.properties.SetOrientation(a)
self.properties.SetOrientation(value)
return self

def line_spacing(self, ls):
"""Set the extra spacing between lines, expressed as a text height multiplication factor."""
self.properties.SetLineSpacing(ls)
def line_spacing(self, value):
"""Set the extra spacing between lines
expressed as a text height multiplicative factor."""
self.properties.SetLineSpacing(value)
return self

def line_offset(self, lo):
def line_offset(self, value):
"""Set/Get the vertical offset (measured in pixels)."""
self.properties.SetLineOffset(lo)
self.properties.SetLineOffset(value)
return self

def bold(self, value=True):
Expand Down Expand Up @@ -4497,6 +4498,7 @@ def __init__(
font : (str)
built-in available fonts are:
- Antares
- Arial
- Bongas
- Calco
Expand Down

0 comments on commit 5899e99

Please sign in to comment.