Skip to content

Commit

Permalink
Merge pull request #830 from ganimtron-10/textblock-attribute
Browse files Browse the repository at this point in the history
UI: Adding getters and setters for the `TextBlock2D` properties
  • Loading branch information
skoudoro committed Aug 7, 2023
2 parents 2551b7f + b053c88 commit 9a9cf7f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 4 deletions.
60 changes: 57 additions & 3 deletions fury/ui/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ class TextBlock2D(UI):
Adds text shadow.
size : (int, int)
Size (width, height) in pixels of the text bounding box.
auto_font_scale : bool
Automatically scale font according to the text bounding box.
dynamic_bbox : bool
Automatically resize the bounding box according to the content.
"""

def __init__(
Expand Down Expand Up @@ -749,10 +753,8 @@ def __init__(
self.italic = italic
self.shadow = shadow
self._vertical_justification = vertical_justification
self._dynamic_bbox = dynamic_bbox
self.auto_font_scale = auto_font_scale
if self.auto_font_scale:
self.actor.SetTextScaleModeToProp()
self.dynamic_bbox = dynamic_bbox
self.message = text
self.font_size = font_size
if size is not None:
Expand Down Expand Up @@ -1047,6 +1049,58 @@ def background_color(self, color):
self.background.set_visibility(True)
self.background.color = color

@property
def auto_font_scale(self):
"""Return whether text font is automatically scaled.
Returns
-------
bool
Text is auto_font_scaled if True.
"""
return self._auto_font_scale

@auto_font_scale.setter
def auto_font_scale(self, flag):
"""Add/remove text auto_font_scale.
Parameters
----------
flag : bool
Automatically scales the text font if True.
"""
self._auto_font_scale = flag
if flag:
self.actor.SetTextScaleModeToProp()
self._justification = "left"
self.update_bounding_box(self.size)
else:
self.actor.SetTextScaleModeToNone()

@property
def dynamic_bbox(self):
"""Automatically resize the bounding box according to the content.
Returns
-------
bool
Bounding box is dynamic if True.
"""
return self._dynamic_bbox

@dynamic_bbox.setter
def dynamic_bbox(self, flag):
"""Add/remove dynamic_bbox.
Parameters
----------
flag : bool
The text bounding box is dynamic if True.
"""
self._dynamic_bbox = flag
if flag:
self.update_bounding_box()

def update_alignment(self):
"""Update Text Alignment.
"""
Expand Down
16 changes: 15 additions & 1 deletion fury/ui/tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ def test_text_block_2d_size():
text_block_0 = ui.TextBlock2D()

npt.assert_equal(text_block_0.actor.GetTextScaleMode(), 0)
npt.assert_equal(text_block_0.size, (180,18))
npt.assert_equal(text_block_0.size, (180, 18))

text_block_0.font_size = 50
npt.assert_equal(text_block_0.size, (500, 50))
Expand Down Expand Up @@ -437,6 +437,20 @@ def test_text_block_2d_size():

text_block_3.message = "Hey Trying\nBig Text"
npt.assert_equal(text_block_3.size, (500, 200))
text_block_3.dynamic_bbox = True
npt.assert_equal(text_block_3.size, text_block_3.cal_size_from_message())
text_block_3.message = "Hello\nLine 1\nLine 2\nLine 3\nLine 4"
npt.assert_equal(text_block_3.size, text_block_3.cal_size_from_message())

bb_size = text_block_3.size
text_block_3.dynamic_bbox = False
text_block_3.message = "Hey Trying\nBig Text"
npt.assert_equal(text_block_3.size, bb_size)

text_block_3.auto_font_scale = True
npt.assert_equal(text_block_3.actor.GetTextScaleMode(), 1)
npt.assert_equal(text_block_3.justification, "left")
npt.assert_equal(text_block_3.size, bb_size)


# test_ui_button_panel(recording=True)

0 comments on commit 9a9cf7f

Please sign in to comment.