-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathtext.py
More file actions
147 lines (114 loc) · 4.43 KB
/
text.py
File metadata and controls
147 lines (114 loc) · 4.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
from typing import *
import pygfx
import numpy as np
from ._base import Graphic
class TextGraphic(Graphic):
def __init__(
self,
text: str,
position: Tuple[int] = (0, 0, 0),
size: int = 14,
face_color: Union[str, np.ndarray] = "w",
outline_color: Union[str, np.ndarray] = "w",
outline_thickness=0,
screen_space: bool = True,
anchor: str = "middle-center",
*args,
**kwargs,
):
"""
Create a text Graphic
Parameters
----------
text: str
display text
position: int tuple, default (0, 0, 0)
int tuple indicating location of text in scene
size: int, default 10
text size
face_color: str or array, default "w"
str or RGBA array to set the color of the text
outline_color: str or array, default "w"
str or RGBA array to set the outline color of the text
outline_thickness: int, default 0
text outline thickness
screen_space: bool = True
whether the text is rendered in screen space, in contrast to world space
name: str, optional
name of graphic, passed to Graphic
anchor: str, default "middle-center"
position of the origin of the text
a string representing the vertical and horizontal anchors, separated by a dash
* Vertical values: "top", "middle", "baseline", "bottom"
* Horizontal values: "left", "center", "right"
"""
super().__init__(*args, **kwargs)
self._text = text
world_object = pygfx.Text(
pygfx.TextGeometry(
text=str(text),
font_size=size,
screen_space=screen_space,
anchor=anchor,
),
pygfx.TextMaterial(
color=face_color,
outline_color=outline_color,
outline_thickness=outline_thickness,
pick_write=True,
),
)
self._set_world_object(world_object)
self.world_object.position = position
@property
def text(self):
"""Returns the text of this graphic."""
return self._text
@text.setter
def text(self, text: str):
"""Set the text of this graphic."""
if not isinstance(text, str):
raise ValueError("Text must be of type str.")
self._text = text
self.world_object.geometry.set_text(self._text)
@property
def text_size(self):
"""Returns the text size of this graphic."""
return self.world_object.geometry.font_size
@text_size.setter
def text_size(self, size: Union[int, float]):
"""Set the text size of this graphic."""
if not (isinstance(size, int) or isinstance(size, float)):
raise ValueError("Text size must be of type int or float")
self.world_object.geometry.font_size = size
@property
def face_color(self):
"""Returns the face color of this graphic."""
return self.world_object.material.color
@face_color.setter
def face_color(self, color: Union[str, np.ndarray]):
"""Set the face color of this graphic."""
if not (isinstance(color, str) or isinstance(color, np.ndarray)):
raise ValueError("Face color must be of type str or np.ndarray")
color = pygfx.Color(color)
self.world_object.material.color = color
@property
def outline_size(self):
"""Returns the outline size of this graphic."""
return self.world_object.material.outline_thickness
@outline_size.setter
def outline_size(self, size: Union[int, float]):
"""Set the outline size of this text graphic."""
if not (isinstance(size, int) or isinstance(size, float)):
raise ValueError("Outline size must be of type int or float")
self.world_object.material.outline_thickness = size
@property
def outline_color(self):
"""Returns the outline color of this graphic."""
return self.world_object.material.outline_color
@outline_color.setter
def outline_color(self, color: Union[str, np.ndarray]):
"""Set the outline color of this graphic"""
if not (isinstance(color, str) or isinstance(color, np.ndarray)):
raise ValueError("Outline color must be of type str or np.ndarray")
self.world_object.material.outline_color = color