Skip to content

Commit

Permalink
feat: add toggle option shortcut keys to applicable operators
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan-hm committed Apr 24, 2022
1 parent 86356b8 commit c10b066
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 59 deletions.
65 changes: 32 additions & 33 deletions lib/events.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
from . preferences import get_preferences


def has(event): return event != None
def pressed(event, keys): return has(event) and event.type in keys and event.value == 'PRESS'
def detected(event, types): return has(event) and event.type in types


def capture_modifier_keys(cls, event=None, mouse_x=0):
cls.key_no_modifiers = False if event == None else not event.ctrl and not event.alt
cls.key_ctrl = False if event == None else event.ctrl and not event.alt
cls.key_shift_ctrl = False if event == None else event.shift and cls.key_ctrl
cls.key_alt = False if event == None else not event.ctrl and event.alt
cls.key_shift_alt = False if event == None else event.shift and cls.key_alt
cls.key_ctrl_alt = False if event == None else event.ctrl and event.alt
cls.key_shift_ctrl_alt = False if event == None else event.shift and cls.key_ctrl_alt
cls.key_shift = False if event == None else event.shift
cls.key_shift_no_modifiers = False if event == None else event.shift and cls.key_no_modifiers
cls.key_toggle_pin_overlay = False if event == None else event.type == get_preferences().overlay_pin_key and event.value == 'PRESS'
cls.key_one = False if event == None else event.type == 'ONE' and event.value == 'PRESS'
cls.key_two = False if event == None else event.type == 'TWO' and event.value == 'PRESS'
cls.key_three = False if event == None else event.type == 'THREE' and event.value == 'PRESS'
cls.key_toggle_operator_passthrough = False if event == None else event.type == get_preferences().overlay_pause_key and event.value == 'PRESS'
cls.key_increase_factor = False if event == None else event.type in {'PLUS', 'EQUAL', 'NUMPAD_PLUS'} and event.value == 'PRESS'
cls.key_decrease_factor = False if event == None else event.type in {'MINUS', 'NUMPAD_MINUS'} and event.value == 'PRESS'

cls.key_step_up = False if event == None else event.type == 'WHEELUPMOUSE' or (
event.value == 'PRESS' and event.type == 'UP_ARROW') or (
event.value == 'PRESS' and event.type == 'RIGHT_ARROW') or (
event.value == 'PRESS' and event.type == 'D') or (
event.value == 'PRESS' and event.type == 'W')

cls.key_step_down = False if event == None else event.type == 'WHEELDOWNMOUSE' or (
event.value == 'PRESS' and event.type == 'DOWN_ARROW') or (
event.value == 'PRESS' and event.type == 'LEFT_ARROW') or (
event.value == 'PRESS' and event.type == 'A') or (
event.value == 'PRESS' and event.type == 'S')
cls.key_no_modifiers = has(event) and not event.ctrl and not event.alt
cls.key_ctrl = has(event) and event.ctrl and not event.alt
cls.key_shift_ctrl = has(event) and event.shift and cls.key_ctrl
cls.key_alt = has(event) and not event.ctrl and event.alt
cls.key_shift_alt = has(event) and event.shift and cls.key_alt
cls.key_ctrl_alt = has(event) and event.ctrl and event.alt
cls.key_shift_ctrl_alt = has(event) and event.shift and cls.key_ctrl_alt
cls.key_shift = has(event) and event.shift
cls.key_shift_no_modifiers = has(event) and event.shift and cls.key_no_modifiers

cls.key_one = pressed(event, {'ONE'})
cls.key_two = pressed(event, {'TWO'})
cls.key_three = pressed(event, {'THREE'})

cls.key_toggle_pin_overlay = pressed(event, {get_preferences().overlay_pin_key})
cls.key_toggle_operator_passthrough = pressed(event, {get_preferences().overlay_pause_key})

cls.key_increase_factor = pressed(event, {'PLUS', 'EQUAL', 'NUMPAD_PLUS'})
cls.key_decrease_factor = pressed(event, {'MINUS', 'NUMPAD_MINUS'})

cls.key_step_up = detected(event, {'WHEELUPMOUSE'}) or pressed(event, {'UP_ARROW'}) or pressed(event, {'RIGHT_ARROW'}) or pressed(event, {'D'}) or pressed(event, {'W'})
cls.key_step_down = detected(event, {'WHEELDOWNMOUSE'}) or pressed(event, {'DOWN_ARROW'}) or pressed(event, {'LEFT_ARROW'}) or pressed(event, {'A'}) or pressed(event, {'S'})

cls.key_confirm = False if event == None else event.type == 'LEFTMOUSE'
cls.key_confirm_alternative = False if event == None else event.type == 'SPACE'
cls.key_cancel = False if event == None else event.type in {'RIGHTMOUSE', 'ESC'}
cls.key_confirm = detected(event, {'LEFTMOUSE'})
cls.key_confirm_alternative = pressed(event, {'SPACE'})
cls.key_cancel = detected(event, {'RIGHTMOUSE'}) or pressed(event, {'ESC'})

cls.key_movement_passthrough = False if event == None else event.type == 'MIDDLEMOUSE' or (
event.alt and event.type in {'LEFTMOUSE', 'RIGHTMOUSE'}) or event.type.startswith('NDOF')
cls.key_movement_passthrough = detected(event, {'MIDDLEMOUSE'}) or (has(event) and event.alt and event.type in {'LEFTMOUSE', 'RIGHTMOUSE'}) or (has(event) and event.type.startswith('NDOF'))

cls.mouse_delta = 0 if event == None else (event.mouse_x - cls.prev_mouse_x) * get_preferences().mouse_value_scalar
cls.mouse_value = cls.mouse_delta * (0.1 if cls.key_shift else 1)
Expand Down
8 changes: 6 additions & 2 deletions power_mods/array_cubed.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bpy
import bmesh
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys
from .. lib.events import capture_modifier_keys, pressed
from .. lib.preferences import get_preferences
from .. lib.axis import init_axis, register_axis_handler, unregister_axis_handler

Expand Down Expand Up @@ -40,6 +40,10 @@ def modal(self, context, event):

return {'CANCELLED'}

elif pressed(event, {'R'}):
self.axis = (self.axis + 1) % 3
self.dirty = True

elif self.key_increase_factor:
if self.key_ctrl:
self.base_offset_factor = min(1, self.base_offset_factor * 10.0)
Expand Down Expand Up @@ -206,7 +210,7 @@ def draw_text_callback(self):

draw_property(
self,
"Axis: {0}".format(['X', 'Y', 'Z'][self.axis]),
"Axis [R]: {0}".format(['X', 'Y', 'Z'][self.axis]),
"(X, Y, Z)",
active=self.key_no_modifiers,
alt_mode=False)
Expand Down
8 changes: 6 additions & 2 deletions power_mods/bevel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bpy
import bmesh
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys
from .. lib.events import capture_modifier_keys, pressed
from .. lib.preferences import get_preferences


Expand Down Expand Up @@ -48,6 +48,10 @@ def modal(self, context, event):
if self.key_no_modifiers:
self.base_width_factor = max(0.001, self.base_width_factor / 10.0)

elif pressed(event, {'H'}):
self.harden_normals = not self.harden_normals
self.dirty = True

elif self.key_step_up:
if self.key_alt:
self.segments = 2 if self.segments == 1 else self.segments + segment_factor
Expand Down Expand Up @@ -222,7 +226,7 @@ def draw_text_callback(self):

draw_property(
self,
"Harden Normals: {0}".format("Yes" if self.harden_normals else "No"),
"Harden Normals [H]: {0}".format("Yes" if self.harden_normals else "No"),
"Ctrl + Alt (Yes, No)",
active=self.key_ctrl_alt,
alt_mode=False)
Expand Down
8 changes: 6 additions & 2 deletions power_mods/circular_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from math import radians
from mathutils import Euler
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys
from .. lib.events import capture_modifier_keys, pressed
from .. lib.collections import move_to_utils_collection
from .. lib.preferences import get_preferences
from .. lib.objects import set_origin
Expand Down Expand Up @@ -41,6 +41,10 @@ def modal(self, context, event):

return {'CANCELLED'}

elif pressed(event, {'R'}):
self.axis = (self.axis + 1) % 3
self.dirty = True

elif self.key_step_up:
if self.key_alt:
self.axis = (self.axis + 1) % 3
Expand Down Expand Up @@ -216,7 +220,7 @@ def draw_text_callback(self):

draw_property(
self,
"Axis: {}".format(['X', 'Y', 'Z'][self.axis]),
"Axis [R]: {}".format(['X', 'Y', 'Z'][self.axis]),
"Alt (X, Y, Z)",
active=self.key_alt,
alt_mode=False)
Expand Down
8 changes: 6 additions & 2 deletions power_mods/edge_bevel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bpy
import bmesh
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys
from .. lib.events import capture_modifier_keys, pressed
from .. lib.preferences import get_preferences


Expand Down Expand Up @@ -40,6 +40,10 @@ def modal(self, context, event):

return {'CANCELLED'}

elif pressed(event, {'H'}):
self.harden_normals = not self.harden_normals
self.dirty = True

elif self.key_increase_factor:
if self.key_no_modifiers:
self.base_weight_factor = min(1, self.base_weight_factor * 10.0)
Expand Down Expand Up @@ -267,7 +271,7 @@ def draw_text_callback(self):

draw_property(
self,
"Width: {0:.0f} / Harden Normals: {1}".format(self.width * 1000, "Yes" if self.harden_normals else "No"),
"Width: {0:.0f} / Harden Normals [H]: {1}".format(self.width * 1000, "Yes" if self.harden_normals else "No"),
"Ctrl + Alt (±100) | Shift + Ctrl + Alt (Yes, No)",
active=self.key_ctrl_alt,
alt_mode=self.key_shift_ctrl_alt,
Expand Down
14 changes: 11 additions & 3 deletions power_mods/mirror.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import bmesh
from math import radians
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys
from .. lib.events import capture_modifier_keys, pressed
from .. lib.axis import init_axis, register_axis_handler, unregister_axis_handler


Expand Down Expand Up @@ -32,6 +32,14 @@ def modal(self, context, event):

return {'CANCELLED'}

elif pressed(event, {'R'}):
self.axis = (self.axis + 1) % 3
self.dirty = True

elif pressed(event, {'F'}):
self.flip = not self.flip
self.dirty = True

elif self.key_step_up:
if self.key_alt:
self.flip = not self.flip
Expand Down Expand Up @@ -152,14 +160,14 @@ def draw_text_callback(self):

draw_property(
self,
"Axis: {}".format(['X', 'Y', 'Z'][self.axis]),
"Axis [R]: {}".format(['X', 'Y', 'Z'][self.axis]),
"X, Y, Z",
active=self.key_no_modifiers,
alt_mode=False)

draw_property(
self,
"Flipped: {}".format('Yes' if self.flip else 'No'),
"Flipped [F]: {}".format('Yes' if self.flip else 'No'),
"Alt (Yes, No)",
active=self.key_alt,
alt_mode=False)
Expand Down
18 changes: 13 additions & 5 deletions power_mods/profile_extrude.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bpy
import bmesh
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys
from .. lib.events import capture_modifier_keys, pressed
from .. lib.preferences import get_preferences
from .. lib.axis import init_axis, register_axis_handler, unregister_axis_handler

Expand Down Expand Up @@ -39,6 +39,14 @@ def modal(self, context, event):

return {'CANCELLED'}

elif pressed(event, {'R'}):
self.axis = (self.axis + 1) % 3
self.dirty = True

elif pressed(event, {'T'}):
self.weighting = self.weighting + 1 if self.weighting < 1 else -1
self.dirty = True

elif self.key_increase_factor:
self.base_extrude_factor = min(1, self.base_extrude_factor * 10.0)

Expand All @@ -47,7 +55,7 @@ def modal(self, context, event):

elif self.key_step_up:
if self.key_alt:
self.weighting = min(1, self.weighting + 1)
self.weighting = self.weighting + 1 if self.weighting < 1 else -1
elif self.key_ctrl:
self.axis = (self.axis + 1) % 3
elif self.key_no_modifiers:
Expand All @@ -57,7 +65,7 @@ def modal(self, context, event):

elif self.key_step_down:
if self.key_alt:
self.weighting = max(-1, self.weighting - 1)
self.weighting = self.weighting - 1 if self.weighting > -1 else 1
elif self.key_ctrl:
self.axis = (self.axis - 1) % 3
elif self.key_no_modifiers:
Expand Down Expand Up @@ -227,14 +235,14 @@ def draw_text_callback(self):

draw_property(
self,
"Weighting: {}".format(['Negative', 'Neutral', 'Positive'][1 + round(self.weighting)]),
"Weighting [T]: {}".format(['Negative', 'Neutral', 'Positive'][1 + round(self.weighting)]),
"Alt (Negative, Neutral, Positive)",
active=self.key_alt,
alt_mode=False)

draw_property(
self,
"Axis: {}".format(['X', 'Y', 'Z'][self.axis]),
"Axis [R]: {}".format(['X', 'Y', 'Z'][self.axis]),
"Ctrl (X, Y, Z)",
active=self.key_ctrl,
alt_mode=False)
Expand Down
12 changes: 10 additions & 2 deletions power_mods/screw.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import bmesh
from math import radians, degrees
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys
from .. lib.events import capture_modifier_keys, pressed
from .. lib.preferences import get_preferences
from .. lib.axis import init_axis, register_axis_handler, unregister_axis_handler

Expand Down Expand Up @@ -42,6 +42,14 @@ def modal(self, context, event):

return {'CANCELLED'}

elif pressed(event, {'R'}):
self.axis = (self.axis + 1) % 3
self.dirty = True

elif pressed(event, {'O'}):
self.offset_axis = (self.offset_axis + 1) % 3
self.dirty = True

elif self.key_increase_factor:
if self.key_ctrl_alt:
self.base_offset_factor = min(1, self.base_offset_factor * 10.0)
Expand Down Expand Up @@ -238,7 +246,7 @@ def draw_text_callback(self):

draw_property(
self,
"Screw Axis: {} / Offset Axis: {}".format(['X', 'Y', 'Z'][self.axis], ['X', 'Y', 'Z'][self.offset_axis]),
"Screw Axis [R]: {} / Offset Axis [O]: {}".format(['X', 'Y', 'Z'][self.axis], ['X', 'Y', 'Z'][self.offset_axis]),
"Alt (Screw X, Y, Z) | Shift + Alt (Offset X, Y, Z)",
active=self.key_alt,
alt_mode=self.key_shift_alt)
Expand Down
12 changes: 8 additions & 4 deletions power_mods/solidify.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import bmesh
from math import radians
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys
from .. lib.events import capture_modifier_keys, pressed
from .. lib.preferences import get_preferences


Expand Down Expand Up @@ -40,6 +40,10 @@ def modal(self, context, event):

return {'CANCELLED'}

elif pressed(event, {'T'}):
self.weighting = self.weighting + 1 if self.weighting < 1 else -1
self.dirty = True

elif self.key_increase_factor:
if self.key_ctrl:
self.base_offset_factor = min(1, self.base_offset_factor * 10.0)
Expand All @@ -54,7 +58,7 @@ def modal(self, context, event):

elif self.key_step_up:
if self.key_alt:
self.weighting = min(1, self.weighting + 1)
self.weighting = self.weighting + 1 if self.weighting < 1 else -1
elif self.key_ctrl:
self.offset += offset_factor
elif self.key_no_modifiers:
Expand All @@ -64,7 +68,7 @@ def modal(self, context, event):

elif self.key_step_down:
if self.key_alt:
self.weighting = max(-1, self.weighting - 1)
self.weighting = self.weighting - 1 if self.weighting > -1 else 1
elif self.key_ctrl:
self.offset -= offset_factor
elif self.key_no_modifiers:
Expand Down Expand Up @@ -208,7 +212,7 @@ def draw_text_callback(self):

draw_property(
self,
"Weighting: {}".format(['Negative', 'Neutral', 'Positive'][1 + round(self.weighting)]),
"Weighting [T]: {}".format(['Negative', 'Neutral', 'Positive'][1 + round(self.weighting)]),
"Alt (Negative, Neutral, Positive)",
active=self.key_alt,
alt_mode=False)
Expand Down
8 changes: 6 additions & 2 deletions utils/hydrate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bpy
import bmesh
from .. lib.overlay import update_overlay, init_overlay, toggle_pin_overlay, toggle_operator_passthrough, register_draw_handler, unregister_draw_handler, draw_header, draw_property
from .. lib.events import capture_modifier_keys
from .. lib.events import capture_modifier_keys, pressed


class ND_OT_hydrate(bpy.types.Operator):
Expand Down Expand Up @@ -30,6 +30,10 @@ def modal(self, context, event):

return {'CANCELLED'}

elif pressed(event, {'C'}):
self.clear_parent = not self.clear_parent
self.dirty = True

elif self.key_step_up:
if self.key_alt:
self.clear_parent = not self.clear_parent
Expand Down Expand Up @@ -135,7 +139,7 @@ def draw_text_callback(self):

draw_property(
self,
"Clear parent: {0}".format("Yes" if self.clear_parent else "No"),
"Clear parent [C]: {0}".format("Yes" if self.clear_parent else "No"),
"Alt (Yes, No)",
active=self.key_alt,
alt_mode=False)
Expand Down
Loading

0 comments on commit c10b066

Please sign in to comment.