Blender-Pipeline/UI/UI_math.py

197 lines
5.7 KiB
Python
Raw Normal View History

2020-12-03 16:05:02 +01:00
# THIS FILE IS A PART OF VCStudio
# PYTHON 3
import re
2020-12-03 16:05:02 +01:00
# This is collection of simple geometric math operations that I gonna use trough
# out the UI of the VCStudio. Stuff like collision detections, overlap detections
# and other verious little tiny, but overused functions.
def line_overlap(line1, line2):
# This function will check if 2 one dimenshional line overlap.
overlap = False
# Let's sort them just incase.
line1.sort()
line2.sort()
# Well. I guess this is the least ambiguos way of doing so. In my opinion
if line1[0] > line2[0] and line1[0] < line2[1]:
overlap = True
elif line1[1] > line2[0] and line1[1] < line2[1]:
overlap = True
elif line2[0] > line1[0] and line2[0] < line1[1]:
overlap = True
elif line2[1] > line1[0] and line2[1] < line1[1]:
overlap = True
return overlap
def rectangle_overlap(rectangle1, rectangle2):
# This function will see if 2 rectangle overlap. It returns either True or
# False.
overlap = False
# Now since it's going to be easier for me to use the same type of coordi-
# nates as in cairo to draw rectangles. I need to purify them first. Meaning
# convert the width and height into real points on a plane.
r1x, r1y, r1w, r1h = rectangle1
r2x, r2y, r2w, r2h = rectangle2
r1w += r1x
r1h += r1y
r2w += r2x
r2h += r2y
# Now we going to simply compare if overlapping lines of x coordinates and if
# yes. Coordinates of y coordinates.
if line_overlap([r1x, r1w],[r2x, r2w]) and line_overlap([r1y, r1h],[r2y, r2h]):
overlap = True
return overlap
2020-12-06 14:51:23 +01:00
def rectangle_surround( win, name, target_pos, target_size, now_pos, now_size ):
# This function does surrounding operations. For example you have N amount
# of nodes selected. And you want to draw a rectangle arround all of them.
# for this you will have to find out the smallest of all values and biggest
# and so this is what this funtion is for.
now_pos = now_pos.copy()
now_size = now_size.copy()
# We are going to start by refrashing the current position of the rectangle
# for this we need to know whether at this frame there was a refresh already.
if win.current["frame"] != win.surround["frame"]:# and win.current["frame"] % 2 == 0:
win.surround["frame"] = win.current["frame"]
win.surround["rects"] = []
# Now let's see if a given name is in the data of those refrashed.
if name not in win.surround["rects"]:
target_pos[0] = now_pos[0]
target_pos[1] = now_pos[1]
target_size[0] = now_size[0]
target_size[1] = now_size[1]
win.surround["rects"].append(name)
# Now you maybe wondering why I would not just make the whole targer_size
# for example = [0,0]. And it because lists are linked from some other
# data. And I don't create new lists. But change the link lists.
# Now let's do the checking and overwritting of all the rest of the data.
if now_pos[0] < target_pos[0]:
target_size[0] += target_pos[0] - now_pos[0]
target_pos[0] = now_pos[0]
if now_pos[1] < target_pos[1]:
target_size[1] += target_pos[1] - now_pos[1]
target_pos[1] = now_pos[1]
# Now the size. It's a bit trickier. But not too hard. We need to remember
# the the ultimate bottom, right point is a combintation of position and
# size.
if now_pos[0] + now_size[0] > target_pos[0] + target_size[0]:
target_size[0] = now_size[0] + now_pos[0] - target_pos[0]
if now_pos[1] + now_size[1] > target_pos[1] + target_size[1]:
target_size[1] = now_size[1] + now_pos[1] - target_pos[1]
def filter_arrows(win):
# This function filters the arrow connections. I copied it from the
# Blender_Organizer's code. at py_data/modules/story_editor.py
# Doing some clever magic I forgot the purpose of
for num, arrow in enumerate(win.story["arrows"]):
for num2, arrow2 in enumerate(win.story["arrows"]):
if arrow[0] == arrow2[0] or arrow[1] == arrow2[1]:
win.story["arrows"][num] = arrow2
# Removing doubles
tmp = []
for i in win.story["arrows"]:
if i not in tmp:
tmp.append(i)
win.story["arrows"] = tmp
2020-12-23 14:52:59 +01:00
def timestring(tleft):
2020-12-06 14:51:23 +01:00
2020-12-23 14:52:59 +01:00
# This crazy function will convert the microsecond into something a bit
# more usefull. Like 03:20:90.06 Kind a thing.
tleftX = tleft
seconds = int(tleftX)
2020-12-23 14:52:59 +01:00
addend = tleftX - seconds
afterdot = str(int(addend*100))
if len(afterdot) < 2:
afterdot = "0"+afterdot
2020-12-23 14:52:59 +01:00
# This part of the function was provided by byliz55 on notabug.org for
# FastLBRY GTK. But since I'm a bad programmer. I'm using it here.
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
if h:
return '{:d}:{:02d}:{:02d}'.format(h, m, s)+"."+afterdot
else:
return '{:d}:{:02d}'.format(m, s)+"."+afterdot
2020-12-23 14:52:59 +01:00
def tryint(s):
try:
return int(s)
except:
return s
def alphanum_key(s):
return [ tryint(c) for c in re.split('([0-9]+)', s) ]
def sort_nicely(l):
l.sort(key=alphanum_key)
def found(t, q):
# Function for search.
# Making sure that both text and query are not case sensetive.
t = t.lower()
q = q.lower()
ors = []
for c in q.split("|"): # OR
f = True
for w in c.split(" "): # AND
if w not in t:
f = False
ors.append(f)
return any(ors)