UI_math.py 5.7 KB

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