UI_math.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. # THIS FILE IS A PART OF VCStudio
  2. # PYTHON 3
  3. # This is collection of simple geometric math operations that I gonna use trough
  4. # out the UI of the VCStudio. Stuff like collision detections, overlap detections
  5. # and other verious little tiny, but overused functions.
  6. def line_overlap(line1, line2):
  7. # This function will check if 2 one dimenshional line overlap.
  8. overlap = False
  9. # Let's sort them just incase.
  10. line1.sort()
  11. line2.sort()
  12. # Well. I guess this is the least ambiguos way of doing so. In my opinion
  13. if line1[0] > line2[0] and line1[0] < line2[1]:
  14. overlap = True
  15. elif line1[1] > line2[0] and line1[1] < line2[1]:
  16. overlap = True
  17. elif line2[0] > line1[0] and line2[0] < line1[1]:
  18. overlap = True
  19. elif line2[1] > line1[0] and line2[1] < line1[1]:
  20. overlap = True
  21. return overlap
  22. def rectangle_overlap(rectangle1, rectangle2):
  23. # This function will see if 2 rectangle overlap. It returns either True or
  24. # False.
  25. overlap = False
  26. # Now since it's going to be easier for me to use the same type of coordi-
  27. # nates as in cairo to draw rectangles. I need to purify them first. Meaning
  28. # convert the width and height into real points on a plane.
  29. r1x, r1y, r1w, r1h = rectangle1
  30. r2x, r2y, r2w, r2h = rectangle2
  31. r1w += r1x
  32. r1h += r1y
  33. r2w += r2x
  34. r2h += r2y
  35. # Now we going to simply compare if overlapping lines of x coordinates and if
  36. # yes. Coordinates of y coordinates.
  37. if line_overlap([r1x, r1w],[r2x, r2w]) and line_overlap([r1y, r1h],[r2y, r2h]):
  38. overlap = True
  39. return overlap
  40. def rectangle_surround( win, name, target_pos, target_size, now_pos, now_size ):
  41. # This function does surrounding operations. For example you have N amount
  42. # of nodes selected. And you want to draw a rectangle arround all of them.
  43. # for this you will have to find out the smallest of all values and biggest
  44. # and so this is what this funtion is for.
  45. now_pos = now_pos.copy()
  46. now_size = now_size.copy()
  47. # We are going to start by refrashing the current position of the rectangle
  48. # for this we need to know whether at this frame there was a refresh already.
  49. if win.current["frame"] != win.surround["frame"]:# and win.current["frame"] % 2 == 0:
  50. win.surround["frame"] = win.current["frame"]
  51. win.surround["rects"] = []
  52. # Now let's see if a given name is in the data of those refrashed.
  53. if name not in win.surround["rects"]:
  54. target_pos[0] = now_pos[0]
  55. target_pos[1] = now_pos[1]
  56. target_size[0] = now_size[0]
  57. target_size[1] = now_size[1]
  58. win.surround["rects"].append(name)
  59. # Now you maybe wondering why I would not just make the whole targer_size
  60. # for example = [0,0]. And it because lists are linked from some other
  61. # data. And I don't create new lists. But change the link lists.
  62. # Now let's do the checking and overwritting of all the rest of the data.
  63. if now_pos[0] < target_pos[0]:
  64. target_size[0] += target_pos[0] - now_pos[0]
  65. target_pos[0] = now_pos[0]
  66. if now_pos[1] < target_pos[1]:
  67. target_size[1] += target_pos[1] - now_pos[1]
  68. target_pos[1] = now_pos[1]
  69. # Now the size. It's a bit trickier. But not too hard. We need to remember
  70. # the the ultimate bottom, right point is a combintation of position and
  71. # size.
  72. if now_pos[0] + now_size[0] > target_pos[0] + target_size[0]:
  73. target_size[0] = now_size[0] + now_pos[0] - target_pos[0]
  74. if now_pos[1] + now_size[1] > target_pos[1] + target_size[1]:
  75. target_size[1] = now_size[1] + now_pos[1] - target_pos[1]
  76. def filter_arrows(win):
  77. # This function filters the arrow connections. I copied it from the
  78. # Blender_Organizer's code. at py_data/modules/story_editor.py
  79. # Doing some clever magic I forgot the purpose of
  80. for num, arrow in enumerate(win.story["arrows"]):
  81. for num2, arrow2 in enumerate(win.story["arrows"]):
  82. if arrow[0] == arrow2[0] or arrow[1] == arrow2[1]:
  83. win.story["arrows"][num] = arrow2
  84. # Removing doubles
  85. tmp = []
  86. for i in win.story["arrows"]:
  87. if i not in tmp:
  88. tmp.append(i)
  89. win.story["arrows"] = tmp
  90. def timestring(tleft):
  91. # This crazy function will convert the microsecond into something a bit
  92. # more usefull. Like 03:20:90.06 Kind a thing.
  93. tleftX = tleft
  94. tleft = int(tleftX)
  95. addend = tleftX - tleft
  96. valt = str(tleft)
  97. if tleft > 60 :
  98. le = tleft
  99. tleft = int(tleft / 60)
  100. le = le - int(tleft * 60)
  101. stleft = "0"*(2-len(str(tleft)))+str(tleft)
  102. sle = "0"*(2-len(str(le)))+str(le)
  103. valt = stleft+":"+ sle
  104. if tleft > 60 :
  105. lele = le
  106. le = tleft
  107. tleft = int(tleft / 60)
  108. le = le - int(tleft * 60)
  109. lele = (lele - le)
  110. if lele < 0:
  111. lele = int(lele * -1)
  112. stleft = "0"*(2-len(str(tleft)))+str(tleft)
  113. sle = "0"*(2-len(str(le)))+str(le)
  114. slele = "0"*(2-len(str(lele)))+str(lele)
  115. valt = stleft+":"+ sle + ":" + slele
  116. if tleft > 24 :
  117. le = tleft
  118. tleft = int(tleft / 24)
  119. le = le - int(tleft * 24)
  120. valt = str(tleft)+" DAYS AND "+ str(le) + " HRS"
  121. return valt + "." + str(int(addend*100))