diff --git a/direct/src/gui/OnscreenText.py b/direct/src/gui/OnscreenText.py index 1a35152f40..13a284622f 100644 --- a/direct/src/gui/OnscreenText.py +++ b/direct/src/gui/OnscreenText.py @@ -313,43 +313,98 @@ class OnscreenText(NodePath): text = property(getText, setText) + def setTextX(self, x): + self.setTextPos(x, self.__pos[1]) + def setX(self, x): - self.setPos(x, self.__pos[1]) + """ + .. deprecated:: 1.11.0 + Use `.setTextX()` method instead. + """ + self.setTextPos(x, self.__pos[1]) + + def setTextY(self, y): + self.setTextPos(self.__pos[0], y) def setY(self, y): - self.setPos(self.__pos[0], y) + """ + .. deprecated:: 1.11.0 + Use `.setTextY()` method instead. + """ + self.setTextPos(self.__pos[0], y) + + def setTextPos(self, x, y=None): + """ + Position the onscreen text in 2d screen space + """ + if y is None: + self.__pos = tuple(x) + else: + self.__pos = (x, y) + self.updateTransformMat() + + def getTextPos(self): + return self.__pos + + text_pos = property(getTextPos, setTextPos) def setPos(self, x, y): """setPos(self, float, float) Position the onscreen text in 2d screen space + + .. deprecated:: 1.11.0 + Use `.setTextPos()` method or `.text_pos` property instead. """ self.__pos = (x, y) self.updateTransformMat() def getPos(self): + """ + .. deprecated:: 1.11.0 + Use `.getTextPos()` method or `.text_pos` property instead. + """ return self.__pos - pos = property(getPos, setPos) + pos = property(getPos) + + def setTextR(self, r): + """setTextR(self, float) + Rotates the text around the screen's normal. + """ + self.__roll = -r + self.updateTransformMat() + + def getTextR(self): + return -self.__roll + + text_r = property(getTextR, setTextR) def setRoll(self, roll): """setRoll(self, float) - Rotate the onscreen text around the screen's normal + Rotate the onscreen text around the screen's normal. + + .. deprecated:: 1.11.0 + Use ``setTextR(-roll)`` instead (note the negated sign). """ self.__roll = roll self.updateTransformMat() def getRoll(self): + """ + .. deprecated:: 1.11.0 + Use ``-getTextR()`` instead (note the negated sign). + """ return self.__roll roll = property(getRoll, setRoll) - def setScale(self, sx, sy = None): - """setScale(self, float, float) + def setTextScale(self, sx, sy = None): + """setTextScale(self, float, float) Scale the text in 2d space. You may specify either a single uniform scale, or two scales, or a tuple of two scales. """ - if sy == None: + if sy is None: if isinstance(sx, tuple): self.__scale = sx else: @@ -358,6 +413,38 @@ class OnscreenText(NodePath): self.__scale = (sx, sy) self.updateTransformMat() + def getTextScale(self): + return self.__scale + + text_scale = property(getTextScale, setTextScale) + + def setScale(self, sx, sy = None): + """setScale(self, float, float) + Scale the text in 2d space. You may specify either a single + uniform scale, or two scales, or a tuple of two scales. + + .. deprecated:: 1.11.0 + Use `.setTextScale()` method or `.text_scale` property instead. + """ + + if sy is None: + if isinstance(sx, tuple): + self.__scale = sx + else: + self.__scale = (sx, sx) + else: + self.__scale = (sx, sy) + self.updateTransformMat() + + def getScale(self): + """ + .. deprecated:: 1.11.0 + Use `.getTextScale()` method or `.text_scale` property instead. + """ + return self.__scale + + scale = property(getScale, setScale) + def updateTransformMat(self): assert(isinstance(self.textNode, TextNode)) mat = ( @@ -367,11 +454,6 @@ class OnscreenText(NodePath): ) self.textNode.setTransform(mat) - def getScale(self): - return self.__scale - - scale = property(getScale, setScale) - def setWordwrap(self, wordwrap): self.__wordwrap = wordwrap diff --git a/tests/gui/test_OnscreenText.py b/tests/gui/test_OnscreenText.py new file mode 100644 index 0000000000..2ec74b9f14 --- /dev/null +++ b/tests/gui/test_OnscreenText.py @@ -0,0 +1,171 @@ +from direct.gui.OnscreenText import OnscreenText + + +def test_onscreentext_text_pos(): + text = OnscreenText(pos=(1, 2)) + assert text['pos'] == (1, 2) + assert text.pos == (1, 2) + assert text.getPos() == (1, 2) + assert text.text_pos == (1, 2) + assert text.getTextPos() == (1, 2) + assert text.get_pos() == (0, 0, 0) + + text.setTextPos(3, 4) + assert text['pos'] == (3, 4) + assert text.pos == (3, 4) + assert text.getPos() == (3, 4) + assert text.text_pos == (3, 4) + assert text.getTextPos() == (3, 4) + assert text.get_pos() == (0, 0, 0) + + text.text_pos = (7, 8) + assert text['pos'] == (7, 8) + assert text.pos == (7, 8) + assert text.getPos() == (7, 8) + assert text.text_pos == (7, 8) + assert text.getTextPos() == (7, 8) + assert text.get_pos() == (0, 0, 0) + + text.setPos(9, 10) + assert text['pos'] == (9, 10) + assert text.pos == (9, 10) + assert text.getPos() == (9, 10) + assert text.text_pos == (9, 10) + assert text.getTextPos() == (9, 10) + assert text.get_pos() == (0, 0, 0) + + text['pos'] = (11, 12) + assert text['pos'] == (11, 12) + assert text.pos == (11, 12) + assert text.getPos() == (11, 12) + assert text.text_pos == (11, 12) + assert text.getTextPos() == (11, 12) + assert text.get_pos() == (0, 0, 0) + + +def test_onscreentext_node_pos(): + text = OnscreenText() + + text.set_pos(1, 2, 3) + assert text['pos'] == (0, 0) + assert text.pos == (0, 0) + assert text.getPos() == (0, 0) + assert text.text_pos == (0, 0) + assert text.getTextPos() == (0, 0) + assert text.get_pos() == (1, 2, 3) + + +def test_onscreentext_text_roll(): + text = OnscreenText(roll=1) + assert text['roll'] == 1 + assert text.roll == 1 + assert text.getRoll() == 1 + assert text.text_r == -1 + assert text.getTextR() == -1 + assert text.get_r() == 0 + + text.setTextR(2) + assert text['roll'] == -2 + assert text.roll == -2 + assert text.getRoll() == -2 + assert text.text_r == 2 + assert text.getTextR() == 2 + assert text.get_r() == 0 + + text.text_r = 3 + assert text['roll'] == -3 + assert text.roll == -3 + assert text.getRoll() == -3 + assert text.text_r == 3 + assert text.getTextR() == 3 + assert text.get_r() == 0 + + text.setRoll(4) + assert text['roll'] == 4 + assert text.roll == 4 + assert text.getRoll() == 4 + assert text.text_r == -4 + assert text.getTextR() == -4 + assert text.get_r() == 0 + + text['roll'] = 5 + assert text['roll'] == 5 + assert text.roll == 5 + assert text.getRoll() == 5 + assert text.text_r == -5 + assert text.getTextR() == -5 + assert text.get_r() == 0 + + +def test_onscreentext_node_roll(): + text = OnscreenText() + + text.set_r(45) + assert text['roll'] == 0 + assert text.roll == 0 + assert text.getRoll() == 0 + assert text.text_r == 0 + assert text.getTextR() == 0 + assert text.get_r() == 45 + + +def test_onscreentext_text_scale(): + text = OnscreenText(scale=(1, 2)) + assert text['scale'] == (1, 2) + assert text.scale == (1, 2) + assert text.getScale() == (1, 2) + assert text.text_scale == (1, 2) + assert text.getTextScale() == (1, 2) + assert text.get_scale() == (1, 1, 1) + + text.setTextScale(3, 4) + assert text['scale'] == (3, 4) + assert text.scale == (3, 4) + assert text.getScale() == (3, 4) + assert text.text_scale == (3, 4) + assert text.getTextScale() == (3, 4) + assert text.get_scale() == (1, 1, 1) + + text.text_scale = (7, 8) + assert text['scale'] == (7, 8) + assert text.scale == (7, 8) + assert text.getScale() == (7, 8) + assert text.text_scale == (7, 8) + assert text.getTextScale() == (7, 8) + assert text.get_scale() == (1, 1, 1) + + text.setScale(9, 10) + assert text['scale'] == (9, 10) + assert text.scale == (9, 10) + assert text.getScale() == (9, 10) + assert text.text_scale == (9, 10) + assert text.getTextScale() == (9, 10) + assert text.get_scale() == (1, 1, 1) + + text['scale'] = (11, 12) + assert text['scale'] == (11, 12) + assert text.scale == (11, 12) + assert text.getScale() == (11, 12) + assert text.text_scale == (11, 12) + assert text.getTextScale() == (11, 12) + assert text.get_scale() == (1, 1, 1) + + text.scale = 13 + assert text['scale'] == (13, 13) + assert text.scale == (13, 13) + assert text.getScale() == (13, 13) + assert text.text_scale == (13, 13) + assert text.getTextScale() == (13, 13) + assert text.get_scale() == (1, 1, 1) + + +def test_onscreentext_node_scale(): + text = OnscreenText() + + text.set_scale(1, 2, 3) + assert text['scale'] == (0.07, 0.07) + assert text.scale == (0.07, 0.07) + assert text.getScale() == (0.07, 0.07) + assert text.text_scale == (0.07, 0.07) + assert text.getTextScale() == (0.07, 0.07) + assert text.get_scale() == (1, 2, 3)