Skip to content

Commit

Permalink
Update credits to align to the bottom of the window
Browse files Browse the repository at this point in the history
Restrict the max height of images in credits
  • Loading branch information
r-veenstra committed Jul 12, 2023
1 parent 67c8b2a commit c8a7d54
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 52 deletions.
48 changes: 28 additions & 20 deletions exts/cesium.omniverse/cesium/omniverse/ui/credits_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,25 +97,30 @@ def _button_clicked(link: str):

def _build_ui_elements(self, parsed_credits: List[ParsedCredit], label_alignment: ui.Alignment):
for parsed_credit in parsed_credits:
if parsed_credit.image_uri is not None:
if parsed_credit.link is not None:
CesiumImageButton(
src=parsed_credit.image_uri,
padding=4,
clicked_fn=partial(self._button_clicked, parsed_credit.link),
)
else:
CesiumUriImage(src=parsed_credit.image_uri)
elif parsed_credit.text is not None:
if parsed_credit.link is not None:
ui.Button(
parsed_credit.text,
clicked_fn=partial(self._button_clicked, parsed_credit.link),
height=0,
width=0,
)
else:
ui.Label(parsed_credit.text, height=0, word_wrap=True, alignment=label_alignment)

# VStack + Spacer pushes our content to the bottom of the Stack to account for varying heights
with ui.VStack(spacing=0, width=0):
ui.Spacer()
if parsed_credit.image_uri is not None:
if parsed_credit.link is not None:
CesiumImageButton(
src=parsed_credit.image_uri,
padding=4,
height=28,
clicked_fn=partial(self._button_clicked, parsed_credit.link),
)
else:
CesiumUriImage(src=parsed_credit.image_uri, padding=4, height=28)
elif parsed_credit.text is not None:
if parsed_credit.link is not None:
ui.Button(
parsed_credit.text,
clicked_fn=partial(self._button_clicked, parsed_credit.link),
height=0,
width=0,
)
else:
ui.Label(parsed_credit.text, height=0, word_wrap=True, alignment=label_alignment)

def _build_ui(self, parsed_credits: List[ParsedCredit], combine_labels: bool, label_alignment: ui.Alignment):
if combine_labels:
Expand All @@ -133,7 +138,10 @@ def _build_ui(self, parsed_credits: List[ParsedCredit], combine_labels: bool, la
# Add the label even if the string is empty. The label will expand to fill the parent HStack
# which acts like a spacer that right-aligns the image and button elements. Eventually we
# should find a different solution here.
ui.Label(label_strings_combined, height=0, word_wrap=True, alignment=label_alignment)
# VStack + Spacer pushes our content to the bottom of the Stack to account for varying heights
with ui.VStack(spacing=0):
ui.Spacer()
ui.Label(label_strings_combined, height=0, word_wrap=True, alignment=label_alignment)

self._build_ui_elements(other_credits, label_alignment)
else:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,16 @@ def _build_fn(self):
label_alignment=ui.Alignment.RIGHT,
)

self._data_attribution_button = ui.Button(
"Data Attribution",
visible=False,
width=0,
height=0,
clicked_fn=self._on_data_attribution_button_clicked,
)
# VStack + Spacer pushes our content to the bottom of the Stack to account for varying heights
with ui.VStack(spacing=0, width=0):
ui.Spacer()
self._data_attribution_button = ui.Button(
"Data Attribution",
visible=False,
width=0,
height=0,
clicked_fn=self._on_data_attribution_button_clicked,
)

def _on_credits_changed(self, _e: carb.events.IEvent):
credits_json = _e.payload["credits"]
Expand Down
23 changes: 17 additions & 6 deletions exts/cesium.omniverse/cesium/omniverse/ui/image_button.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class CesiumImageButton:
"""A button with an image from a URL or base64 encoded string. Based off of Nvidia's ButtonWithProvider sample."""

def __init__(self, src: str, button_type=ui.Button, padding=0, **kwargs):
def __init__(self, src: str, button_type=ui.Button, padding=0, height=None, **kwargs):
style_type = kwargs.pop("style_type_name_override", self.__class__.__name__)
name = kwargs.pop("name", "")

Expand All @@ -22,14 +22,25 @@ def __init__(self, src: str, button_type=ui.Button, padding=0, **kwargs):
provider = ui.ByteImageProvider()
provider.set_bytes_data(pixels, [image.size[0], image.size[1]])

if height is None:
width = image.size[0]
height = image.size[1]
else:
# If the user is explicitely setting the height of the button, we need to calc an appropriate width
width = image.size[0] * (height / image.size[1])

# Add padding for all sides
height += padding * 2
width += padding * 2

# The styles here are very specific to this stuff so they shouldn't be included
# in the CesiumOmniverseUiStyles class.
self._button = button_type(
text=" ", # Workaround Buttons without text do not expand vertically
style_type_name_override=style_type,
name=name,
width=image.size[0] + padding,
height=image.size[1] + padding,
width=width,
height=height,
style={
"border_radius": 6,
"background_color": ui.color.transparent,
Expand All @@ -41,10 +52,10 @@ def __init__(self, src: str, button_type=ui.Button, padding=0, **kwargs):

self._image = ui.ImageWithProvider(
provider,
width=image.size[0],
height=image.size[1],
width=width,
height=height,
fill_policy=ui.IwpFillPolicy.IWP_PRESERVE_ASPECT_FIT,
style={"alignment": ui.Alignment.CENTER},
style={"alignment": ui.Alignment.CENTER, "margin": padding},
style_type_name_override=style_type,
name=name,
)
Expand Down
38 changes: 19 additions & 19 deletions exts/cesium.omniverse/cesium/omniverse/ui/uri_image.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import logging
import urllib.request
from io import BytesIO
from PIL import Image
Expand All @@ -9,14 +8,12 @@ class CesiumUriImage:
"""A wrapper around an ui.ImageProvider that provides a clean way to load images from URIs or base64 encoded data
strings."""

def __init__(self, src: str, **kwargs):
self._logger = logging.getLogger(__name__)

def __init__(self, src: str, padding=0, height=None, **kwargs):
style_type = kwargs.pop("style_type_name_override", self.__class__.__name__)
name = kwargs.pop("name", "")

try:
# This is copied to image_button.py since we seem to blow the stack if we nest any deeper when rendering.
with ui.ZStack(height=0, width=0):
# This is copied from uri_image.py since we seem to blow the stack if we nest any deeper when rendering.
data = urllib.request.urlopen(src).read()
img_data = BytesIO(data)
image = Image.open(img_data)
Expand All @@ -26,24 +23,27 @@ def __init__(self, src: str, **kwargs):
provider = ui.ByteImageProvider()
provider.set_bytes_data(pixels, [image.size[0], image.size[1]])

if height is None:
width = image.size[0]
height = image.size[1]
else:
# If the user is explicitely setting the height of the image, we need to calc an appropriate width
width = image.size[0] * (height / image.size[1])

# Add padding for all sides
height += padding * 2
width += padding * 2

self._image = ui.ImageWithProvider(
provider,
width=image.size[0],
height=image.size[1],
width=width,
height=height,
fill_policy=ui.IwpFillPolicy.IWP_PRESERVE_ASPECT_FIT,
style={"alignment": ui.Alignment.CENTER, "margin": padding},
style_type_name_override=style_type,
name=name,
)

except Exception as e:
self._logger.warning(f"Failed to load image from url: {src}")
self._logger.error(e)

def __getattr__(self, attr):
return getattr(self._image, attr)
def get_image(self):
return self._image

def __setattr__(self, attr, value):
if attr == "_image":
super().__setattr__(attr, value)
else:
self._image.__setattr__(attr, value)

0 comments on commit c8a7d54

Please sign in to comment.