forked from panda3d/panda3d
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirectUtil.py
More file actions
87 lines (78 loc) · 2.56 KB
/
DirectUtil.py
File metadata and controls
87 lines (78 loc) · 2.56 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
from .DirectGlobals import *
from panda3d.core import VBase4
from direct.task.Task import Task
# Routines to adjust values
def ROUND_TO(value, divisor):
return round(value/float(divisor)) * divisor
def ROUND_INT(val):
return int(round(val))
def CLAMP(val, minVal, maxVal):
return min(max(val, minVal), maxVal)
# Create a tk compatible color string
def getTkColorString(color):
"""
Print out a Tk compatible version of a color string
"""
def toHex(intVal):
val = int(round(intVal))
if val < 16:
return "0" + hex(val)[2:]
else:
return hex(val)[2:]
r = toHex(color[0])
g = toHex(color[1])
b = toHex(color[2])
return "#" + r + g + b
## Background Color ##
def lerpBackgroundColor(r, g, b, duration):
"""
Function to lerp background color to a new value
"""
def lerpColor(state):
dt = globalClock.getDt()
state.time += dt
sf = state.time / state.duration
if sf >= 1.0:
base.setBackgroundColor(state.ec[0], state.ec[1], state.ec[2])
return Task.done
else:
r = sf * state.ec[0] + (1 - sf) * state.sc[0]
g = sf * state.ec[1] + (1 - sf) * state.sc[1]
b = sf * state.ec[2] + (1 - sf) * state.sc[2]
base.setBackgroundColor(r, g, b)
return Task.cont
taskMgr.remove('lerpBackgroundColor')
t = taskMgr.add(lerpColor, 'lerpBackgroundColor')
t.time = 0.0
t.duration = duration
t.sc = base.getBackgroundColor()
t.ec = VBase4(r, g, b, 1)
# Set direct drawing style for an object
# Never light object or draw in wireframe
def useDirectRenderStyle(nodePath, priority = 0):
"""
Function to force a node path to use direct render style:
no lighting, and no wireframe
"""
nodePath.setLightOff(priority)
nodePath.setRenderModeFilled()
# File data util
def getFileData(filename, separator = ','):
"""
Open the specified file and strip out unwanted whitespace and
empty lines. Return file as list of lists, one file line per element,
list elements based upon separator
"""
f = open(filename.toOsSpecific(), 'r')
rawData = f.readlines()
f.close()
fileData = []
for line in rawData:
# First strip whitespace from both ends of line
l = line.strip()
if l:
# If its a valid line, split on separator and
# strip leading/trailing whitespace from each element
data = [s.strip() for s in l.split(separator)]
fileData.append(data)
return fileData