DirectUtil.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from .DirectGlobals import *
  2. from panda3d.core import VBase4
  3. from direct.task.Task import Task
  4. # Routines to adjust values
  5. def ROUND_TO(value, divisor):
  6. return round(value/float(divisor)) * divisor
  7. def ROUND_INT(val):
  8. return int(round(val))
  9. def CLAMP(val, minVal, maxVal):
  10. return min(max(val, minVal), maxVal)
  11. # Create a tk compatible color string
  12. def getTkColorString(color):
  13. """
  14. Print out a Tk compatible version of a color string
  15. """
  16. def toHex(intVal):
  17. val = int(intVal)
  18. if val < 16:
  19. return "0" + hex(val)[2:]
  20. else:
  21. return hex(val)[2:]
  22. r = toHex(color[0])
  23. g = toHex(color[1])
  24. b = toHex(color[2])
  25. return "#" + r + g + b
  26. ## Background Color ##
  27. def lerpBackgroundColor(r, g, b, duration):
  28. """
  29. Function to lerp background color to a new value
  30. """
  31. def lerpColor(state):
  32. dt = globalClock.getDt()
  33. state.time += dt
  34. sf = state.time / state.duration
  35. if sf >= 1.0:
  36. base.setBackgroundColor(state.ec[0], state.ec[1], state.ec[2])
  37. return Task.done
  38. else:
  39. r = sf * state.ec[0] + (1 - sf) * state.sc[0]
  40. g = sf * state.ec[1] + (1 - sf) * state.sc[1]
  41. b = sf * state.ec[2] + (1 - sf) * state.sc[2]
  42. base.setBackgroundColor(r, g, b)
  43. return Task.cont
  44. taskMgr.remove('lerpBackgroundColor')
  45. t = taskMgr.add(lerpColor, 'lerpBackgroundColor')
  46. t.time = 0.0
  47. t.duration = duration
  48. t.sc = base.getBackgroundColor()
  49. t.ec = VBase4(r, g, b, 1)
  50. # Set direct drawing style for an object
  51. # Never light object or draw in wireframe
  52. def useDirectRenderStyle(nodePath, priority = 0):
  53. """
  54. Function to force a node path to use direct render style:
  55. no lighting, and no wireframe
  56. """
  57. nodePath.setLightOff(priority)
  58. nodePath.setRenderModeFilled()
  59. # File data util
  60. def getFileData(filename, separator = ','):
  61. """
  62. Open the specified file and strip out unwanted whitespace and
  63. empty lines. Return file as list of lists, one file line per element,
  64. list elements based upon separator
  65. """
  66. f = open(filename.toOsSpecific(), 'r')
  67. rawData = f.readlines()
  68. f.close()
  69. fileData = []
  70. for line in rawData:
  71. # First strip whitespace from both ends of line
  72. l = line.strip()
  73. if l:
  74. # If its a valid line, split on separator and
  75. # strip leading/trailing whitespace from each element
  76. data = [s.strip() for s in l.split(separator)]
  77. fileData.append(data)
  78. return fileData