DirectUtil.py 2.6 KB

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