forked from wezu/Avolition
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDirectTooltip.py
117 lines (94 loc) · 3.97 KB
/
DirectTooltip.py
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
"""This snippet shows how to create a tooltip text that will be attached
to the cursor and check it's position to not move out of the screen."""
import sys
from panda3d.core import TextNode
from direct.gui.DirectGui import DirectLabel
class DirectTooltip():
def __init__(self):
self.tooltipText = DirectLabel(
text = "Tooltip",
text_fg = (1,1,1,1),
text_scale = 0.05,
text_align = TextNode.ALeft,
frameColor = (0, 0, 0, 0.75),
borderWidth = (0.1, 0.1))
self.tooltipText.setTransparency(True)
self.textXShift = 0.05
self.textYShift = -0.08
self.mousePos = None
# this will determine when the tooltip should be moved in the
# respective direction, whereby
# 1 : display edge
# <1 : margin inside the window
# >1 : margin outside the window
self.xEdgeStartShift = 0.99
self.yEdgeStartShift = 0.99
self.tooltipText.hide()
def show(self, text=None, args=None):
if text is not None:
self.tooltipText.setText(text)
self.tooltipText.resetFrameSize()
self.tooltipText.show()
# add the tooltips update task so it will be updated every frame
base.taskMgr.add(self.updateTooltipPos, "task_updateTooltipPos")
def delete(self, args=None):
self.tooltipText.removeNode()
# remove the tooltips update task
base.taskMgr.remove("task_updateTooltipPos")
def updateTooltipPos(self, task):
# calculate new aspec tratio
wp = base.win.getProperties()
aspX = 1.0
aspY = 1.0
wpXSize = wp.getXSize()
wpYSize = wp.getYSize()
if wpXSize > wpYSize:
aspX = wpXSize / float(wpYSize)
else:
aspY = wpYSize / float(wpXSize)
# variables to store the mouses current x and y position
x = 0.0
y = 0.0
if base.mouseWatcherNode.hasMouse():
self.tooltipText.show()
# get the mouse position
x = base.mouseWatcherNode.getMouseX()
y = base.mouseWatcherNode.getMouseY()
# Move the tooltip to the mouse
# set the text to the current mouse position
self.tooltipText.setPos(
(x*aspX) + self.textXShift,
0,
(y*aspY)+self.textYShift)
bounds = self.tooltipText.getBounds()
# bounds = left, right, bottom, top
# calculate the texts bounds respecting its current position
xLeft = self.tooltipText.getX() + bounds[0]*self.tooltipText.getScale()[0]
xRight = self.tooltipText.getX() + bounds[1]*self.tooltipText.getScale()[0]
yUp = self.tooltipText.getZ() + bounds[3]*self.tooltipText.getScale()[1]
yDown = self.tooltipText.getZ() + bounds[2]*self.tooltipText.getScale()[1]
# these will be used to shift the text in the desired direction
xShift = 0.0
yShift = 0.0
if xRight/aspX > self.xEdgeStartShift:
# shift to the left
xShift = self.xEdgeStartShift - xRight/aspX
elif xLeft/aspX < -self.xEdgeStartShift:
# shift to the right
xShift = -(self.xEdgeStartShift + xLeft/aspX)
if yUp/aspY > self.yEdgeStartShift:
# shift down
yShift = self.yEdgeStartShift - yUp/aspY
elif yDown/aspY < -self.yEdgeStartShift:
# shift up
yShift = -(self.yEdgeStartShift + yDown/aspY)
# some aspect ratio calculation
xShift *= aspX
yShift *= aspY
# move the tooltip to the new position
self.tooltipText.setX(self.tooltipText.getX() + xShift)
self.tooltipText.setZ(self.tooltipText.getZ() + yShift)
else:
self.tooltipText.delete()
# continue the task until it got manually stopped
return task.cont