Skip to content

Commit

Permalink
Fix #389: Fix shortcuts metadata, format with black
Browse files Browse the repository at this point in the history
  • Loading branch information
NathanLovato committed Aug 17, 2019
1 parent 3bb4775 commit 2542fbc
Showing 1 changed file with 69 additions and 115 deletions.
184 changes: 69 additions & 115 deletions operators/mouse_cut.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
from .utils.trim_strips import trim_strips
from .utils.find_snap_candidate import find_snap_candidate

from .utils.draw import draw_line, draw_arrow_head, get_color_gizmo_primary, get_color_gizmo_secondary
from .utils.draw import (
draw_line,
draw_arrow_head,
get_color_gizmo_primary,
get_color_gizmo_secondary,
)
from .utils.doc import doc_name, doc_idname, doc_brief, doc_description

if not bpy.app.background:
Expand All @@ -28,72 +33,32 @@ class POWER_SEQUENCER_OT_mouse_cut(bpy.types.Operator):
"""

doc = {
"name":
doc_name(__qualname__),
"demo":
"https://i.imgur.com/wVvX4ex.gif",
"description":
doc_description(__doc__),
"name": doc_name(__qualname__),
"demo": "https://i.imgur.com/wVvX4ex.gif",
"description": doc_description(__doc__),
"shortcuts": [
(
{
"type": "T",
"value": "PRESS"
},
{
"select_mode": "contextual"
},
{
"gap_remove": False
},
{"type": "T", "value": "PRESS"},
{"select_mode": "CONTEXT", "gap_remove": False},
"Trim using the mouse cursor",
),
(
{
"type": "T",
"value": "PRESS",
"alt": True
},
{
"select_mode": "contextual"
},
{
"gap_remove": True
},
{"type": "T", "value": "PRESS", "alt": True},
{"select_mode": "CONTEXT", "gap_remove": True},
"Trim using the mouse cursor and remove gaps",
),
(
{
"type": "T",
"value": "PRESS",
"shift": True
},
{
"select_mode": "cursor"
},
{
"gap_remove": False
},
{"type": "T", "value": "PRESS", "shift": True},
{"select_mode": "CURSOR", "gap_remove": True},
"Trim in all channels",
),
(
{
"type": "T",
"value": "PRESS",
"shift": True,
"alt": True
},
{
"select_mode": "cursor"
},
{
"gap_remove": True
},
{"type": "T", "value": "PRESS", "shift": True, "alt": True},
{"select_mode": "CURSOR", "gap_remove": True},
"Trim in all channels and remove gaps",
),
],
"keymap":
"Sequencer",
"keymap": "Sequencer",
}
bl_idname = doc_idname(__qualname__)
bl_label = doc["name"]
Expand All @@ -102,20 +67,16 @@ class POWER_SEQUENCER_OT_mouse_cut(bpy.types.Operator):

select_mode: bpy.props.EnumProperty(
items=[
("cursor", "Time cursor",
"Select all of the strips the time cursor overlaps"),
("contextual", "Smart",
"Uses the selection if possible, else uses the other modes"),
("CURSOR", "Time cursor", "Select all of the strips the time cursor overlaps"),
("CONTEXT", "Smart", "Uses the selection if possible, else uses the other modes"),
],
name="Selection mode",
description=
"Cut only the strip under the mouse or all strips under the time cursor",
default="contextual",
description="Cut only the strip under the mouse or all strips under the time cursor",
default="CONTEXT",
)
select_linked: bpy.props.BoolProperty(
name="Use linked time",
description=
"In mouse or contextual mode, always cut linked strips if this is checked",
description="In mouse or CONTEXT mode, always cut linked strips if this is checked",
default=False,
)
gap_remove: bpy.props.BoolProperty(
Expand Down Expand Up @@ -164,8 +125,7 @@ def modal(self, context, event):
return {"CANCELLED"}

# Start and end trim
if event.type == "LEFTMOUSE" or (event.type in ["RET", "T"]
and event.value == "PRESS"):
if event.type == "LEFTMOUSE" or (event.type in ["RET", "T"] and event.value == "PRESS"):
self.trim_apply(context, event)
self.draw_stop()

Expand Down Expand Up @@ -201,11 +161,15 @@ def trim_initialize(self, event):

def trim_apply(self, context, event):
start_x = context.region.view2d.region_to_view(
x=event.mouse_region_x, y=event.mouse_region_y)[0]
x=event.mouse_region_x, y=event.mouse_region_y
)[0]
distance_to_start = abs(event.mouse_region_x - start_x)

is_cutting = (self.trim_start == self.trim_end or event.is_tablet and
distance_to_start <= self.TABLET_TRIM_DISTANCE_THRESHOLD)
is_cutting = (
self.trim_start == self.trim_end
or event.is_tablet
and distance_to_start <= self.TABLET_TRIM_DISTANCE_THRESHOLD
)
if is_cutting:
self.cut(context)
else:
Expand All @@ -218,11 +182,12 @@ def update_time_cursor(self, context, event):
if event.ctrl:
self.trim_end = find_snap_candidate(context, frame)
else:
self.trim_end = get_frame_and_channel(event)[0]
self.trim_end = frame

context.scene.frame_current = self.trim_end

def draw_start(self, context, event):
"""Initializes the drawing handler, see draw()"""
to_select, to_delete = self.find_strips_to_trim(context)
target_strips = to_select + to_delete

Expand All @@ -236,12 +201,12 @@ def draw_start(self, context, event):
self.gap_remove,
)
self.draw_handler = bpy.types.SpaceSequenceEditor.draw_handler_add(
draw, draw_args, "WINDOW", "POST_PIXEL")
draw, draw_args, "WINDOW", "POST_PIXEL"
)

def draw_stop(self):
if self.draw_handler:
bpy.types.SpaceSequenceEditor.draw_handler_remove(
self.draw_handler, "WINDOW")
bpy.types.SpaceSequenceEditor.draw_handler_remove(self.draw_handler, "WINDOW")

def cut(self, context):
to_select = self.find_strips_to_cut(context)
Expand All @@ -251,35 +216,34 @@ def cut(self, context):

frame_current = context.scene.frame_current
context.scene.frame_current = self.trim_start
bpy.ops.sequencer.cut(frame=context.scene.frame_current,
type="SOFT",
side="BOTH")
bpy.ops.sequencer.cut(frame=context.scene.frame_current, type="SOFT", side="BOTH")
context.scene.frame_current = frame_current

def trim(self, context):
to_select, to_delete = self.find_strips_to_trim(context)
trim_strips(context, self.trim_start, self.trim_end, self.select_mode,
to_select, to_delete)
if self.gap_remove and self.select_mode == "cursor":
trim_strips(context, self.trim_start, self.trim_end, self.select_mode, to_select, to_delete)
if self.gap_remove and self.select_mode == "CURSOR":
context.scene.frame_current = min(self.trim_start, self.trim_end)
bpy.ops.power_sequencer.gap_remove()
else:
context.scene.frame_current = self.trim_end

def find_strips_to_cut(self, context):
"""
Returns a list of strips to cut
Returns a list of strips to cut, either the strip hovered by the mouse or all strips under the
time cursor, depending on the select_mode
"""
to_cut = []
overlapping_strips = []
if self.select_mode == "contextual":
overlapping_strips = find_strips_mouse(context, self.trim_start,
self.channel_start,
self.select_linked)
if self.select_mode == "CONTEXT":
overlapping_strips = find_strips_mouse(
context, self.trim_start, self.channel_start, self.select_linked
)
to_cut.extend(overlapping_strips)

if self.select_mode == "cursor" or (not overlapping_strips and
self.select_mode == "contextual"):
if self.select_mode == "CURSOR" or (
not overlapping_strips and self.select_mode == "CONTEXT"
):
for s in context.sequences:
if s.lock:
continue
Expand All @@ -293,9 +257,7 @@ def cut_strips_or_gap(self, context, frame_cut):
else:
frame_current = context.scene.frame_current
context.scene.frame_current = frame_cut
bpy.ops.sequencer.cut(frame=context.scene.frame_current,
type="SOFT",
side="BOTH")
bpy.ops.sequencer.cut(frame=context.scene.frame_current, type="SOFT", side="BOTH")
context.scene.frame_current = frame_current

def find_strips_to_trim(self, context):
Expand All @@ -307,33 +269,32 @@ def find_strips_to_trim(self, context):
trim_start = min(self.trim_start, self.trim_end)
trim_end = max(self.trim_start, self.trim_end)

under_mouse = find_strips_mouse(context, self.trim_start,
self.channel_start, self.select_linked)
under_mouse = find_strips_mouse(
context, self.trim_start, self.channel_start, self.select_linked
)
channel = under_mouse[0].channel if len(under_mouse) > 0 else -1

for s in context.sequences:
if s.lock:
continue
if self.select_mode == "contextual" and channel != -1 and s.channel != channel:
if self.select_mode == "CONTEXT" and channel != -1 and s.channel != channel:
continue

if trim_start <= s.frame_final_start and trim_end >= s.frame_final_end:
to_delete.append(s)
continue
if (s.frame_final_start <= trim_start <= s.frame_final_end
or s.frame_final_start <= trim_end <= s.frame_final_end):
if (
s.frame_final_start <= trim_start <= s.frame_final_end
or s.frame_final_start <= trim_end <= s.frame_final_end
):
to_trim.append(s)

return to_trim, to_delete


def draw(self,
context,
frame_start=-1,
frame_end=-1,
mouse_y=-1,
target_strips=[],
draw_arrows=False):
def draw(
self, context, frame_start=-1, frame_end=-1, mouse_y=-1, target_strips=[], draw_arrows=False
):
"""
Draws the line and arrows that represent the trim
Expand Down Expand Up @@ -374,27 +335,19 @@ def draw(self,

bgl.glLineWidth(3)
draw_line(SHADER, start, end, color_primary)
draw_line(SHADER, Vector((start.x, min_bottom)), Vector(
(start.x, max_top)), color_primary)
draw_line(SHADER, Vector((end.x, min_bottom)), Vector((end.x, max_top)),
color_primary)
draw_line(SHADER, Vector((start.x, min_bottom)), Vector((start.x, max_top)), color_primary)
draw_line(SHADER, Vector((end.x, min_bottom)), Vector((end.x, max_top)), color_primary)

if draw_arrows:
center_arrow_1 = Vector(
[start.x + ((end.x - start.x) * 0.25), start.y])
center_arrow_1 = Vector([start.x + ((end.x - start.x) * 0.25), start.y])
center_arrow_2 = Vector([end.x - ((end.x - start.x) * 0.25), start.y])
arrow_size = Vector([10, 20])

bgl.glLineWidth(6)
draw_arrow_head(SHADER,
center_arrow_1,
arrow_size,
color=color_secondary)
draw_arrow_head(SHADER,
center_arrow_2,
arrow_size,
points_right=False,
color=color_secondary)
draw_arrow_head(SHADER, center_arrow_1, arrow_size, color=color_secondary)
draw_arrow_head(
SHADER, center_arrow_2, arrow_size, points_right=False, color=color_secondary
)

bgl.glLineWidth(1)
bgl.glDisable(bgl.GL_BLEND)
Expand All @@ -405,5 +358,6 @@ def get_frame_and_channel(event):
Returns a tuple of (frame, channel)
"""
frame_float, channel_float = bpy.context.region.view2d.region_to_view(
x=event.mouse_region_x, y=event.mouse_region_y)
x=event.mouse_region_x, y=event.mouse_region_y
)
return round(frame_float), floor(channel_float)

0 comments on commit 2542fbc

Please sign in to comment.