DirectUtil.py 2.5 KB

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