Skip to content

Commit

Permalink
feat: add mouse values mode, and integrate all applicable operators
Browse files Browse the repository at this point in the history
  • Loading branch information
tristan-hm committed Apr 18, 2022
1 parent a83b908 commit a873f74
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 36 deletions.
24 changes: 23 additions & 1 deletion __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import bpy
import rna_keymap_ui
from bpy.types import AddonPreferences
from bpy.props import BoolProperty, IntProperty, StringProperty, EnumProperty
from bpy.props import BoolProperty, IntProperty, StringProperty, EnumProperty, FloatProperty
from . import lib
from . import booleans
from . import interface
Expand Down Expand Up @@ -54,6 +54,20 @@ class NDPreferences(AddonPreferences):
default=False,
)

enable_mouse_values: BoolProperty(
name="Enable Mouse Values",
default=False,
)

mouse_value_scalar: FloatProperty(
name="Mouse Value Scalar",
default=0.0025,
min=0.0001,
max=10,
precision=4,
step=0.01,
)

utils_collection_name: StringProperty(
name="Utils Collection Name",
default="Utils",
Expand Down Expand Up @@ -126,6 +140,14 @@ def draw_ui(self, box):
row = column.row()
row.prop(self, "overlay_dpi")

column = box.column(align=True)
row = column.row()
row.prop(self, "enable_mouse_values")

column = box.column(align=True)
row = column.row()
row.prop(self, "mouse_value_scalar")

column = box.column(align=True)
row = column.row()
row.prop(self, "enable_quick_favourites")
Expand Down
18 changes: 15 additions & 3 deletions generators/recon_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
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.objects import add_single_vertex_object, align_object_to_3d_cursor
from .. lib.events import capture_modifier_keys
from .. lib.preferences import get_preferences


mod_displace = "Radius — ND RCP"
Expand Down Expand Up @@ -84,6 +85,15 @@ def modal(self, context, event):
elif self.key_movement_passthrough:
return {'PASS_THROUGH'}

if get_preferences().enable_mouse_values:
if self.key_alt:
self.inner_radius = max(0, self.inner_radius + self.mouse_value)
self.width = max(self.inner_radius * -0.5, self.width)
elif self.key_ctrl:
self.width = max(self.inner_radius * -0.5, self.width + self.mouse_value)

self.dirty = True

if self.dirty:
self.operate(context)

Expand Down Expand Up @@ -111,7 +121,7 @@ def invoke(self, context, event):

self.operate(context)

capture_modifier_keys(self)
capture_modifier_keys(self, None, event.mouse_x)

init_overlay(self, event)
register_draw_handler(self, draw_text_callback)
Expand Down Expand Up @@ -270,14 +280,16 @@ def draw_text_callback(self):
"Inner Radius: {0:.1f}".format(self.inner_radius * 1000),
"Alt (±{0:.1f}) | Shift + Alt (±{1:.1f})".format(self.base_inner_radius_factor * 1000, (self.base_inner_radius_factor / 10) * 1000),
active=self.key_alt,
alt_mode=self.key_shift_alt)
alt_mode=self.key_shift_alt,
mouse_value=True)

draw_property(
self,
"{0}: {1:.1f}".format("Width" if self.inner_radius > 0 else "Radius", self.width * 2000),
"Ctrl (±{0:.1f}) | Shift + Ctrl (±{1:.1f})".format(self.base_width_factor * 1000, (self.base_width_factor / 10) * 1000),
active=self.key_ctrl,
alt_mode=self.key_shift_ctrl)
alt_mode=self.key_shift_ctrl,
mouse_value=True)


def menu_func(self, context):
Expand Down
17 changes: 14 additions & 3 deletions generators/screw_head.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from .. lib.events import capture_modifier_keys
from .. lib.assets import get_asset_path
from .. lib.objects import align_object_to_3d_cursor
from .. lib.preferences import get_preferences


mod_displace = "Offset — ND SH"
Expand Down Expand Up @@ -83,6 +84,14 @@ def modal(self, context, event):
elif self.key_movement_passthrough:
return {'PASS_THROUGH'}

if get_preferences().enable_mouse_values:
if self.key_alt:
self.offset += self.mouse_value
elif self.key_ctrl:
self.scale += self.mouse_value

self.dirty = True

if self.dirty:
self.operate(context)

Expand Down Expand Up @@ -113,7 +122,7 @@ def invoke(self, context, event):

self.operate(context)

capture_modifier_keys(self)
capture_modifier_keys(self, None, event.mouse_x)

init_overlay(self, event)
register_draw_handler(self, draw_text_callback)
Expand Down Expand Up @@ -208,14 +217,16 @@ def draw_text_callback(self):
"Offset: {0:.1f}".format(self.offset * 1000),
"Alt (±{0:.1f}) | Shift + Alt (±{1:.1f})".format(self.base_offset_factor * 1000, (self.base_offset_factor / 10) * 1000),
active=self.key_alt,
alt_mode=self.key_shift_alt)
alt_mode=self.key_shift_alt,
mouse_value=True)

draw_property(
self,
"Scale: {0:.2f}%".format(self.scale * 100),
"Ctrl (±{0:.2f}%) | Shift + Ctrl (±{1:.2f}%)".format(self.base_scale_factor * 100, (self.base_scale_factor / 10) * 100),
active=self.key_ctrl,
alt_mode=self.key_shift_ctrl)
alt_mode=self.key_shift_ctrl,
mouse_value=True)


def menu_func(self, context):
Expand Down
13 changes: 12 additions & 1 deletion lib/events.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from . preferences import get_preferences

def capture_modifier_keys(cls, event=None):
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
Expand Down Expand Up @@ -36,3 +36,14 @@ def capture_modifier_keys(cls, event=None):

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.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)
cls.prev_mouse_x = mouse_x if event == None else event.mouse_x

if event == None or cls.mouse_warped:
cls.mouse_delta = 0
cls.mouse_value = 0
cls.mouse_warped = False

cls.mouse_value_mag = cls.mouse_value * 100
31 changes: 30 additions & 1 deletion lib/overlay.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,40 @@ def init_overlay(cls, event):

cls.pin_overlay = False
cls.operator_passthrough = False
cls.mouse_warped = False


def update_overlay(cls, context, event):
if not cls.pin_overlay:
cls.overlay_x = event.mouse_x - cls.region_offset_x + cls.overlay_offset_x
cls.overlay_y = event.mouse_y - cls.region_offset_y + cls.overlay_offset_y

if not cls.operator_passthrough and get_preferences().enable_mouse_values:
wrap_cursor(cls, context, event)

redraw_regions()


def wrap_cursor(cls, context, event):
if event.mouse_region_x <= 0:
mouse_x = context.region.width + cls.region_offset_x - 10
context.window.cursor_warp(mouse_x, event.mouse_y)
cls.mouse_warped = True

if event.mouse_region_x >= context.region.width - 1:
mouse_x = cls.region_offset_x + 10
context.window.cursor_warp(mouse_x, event.mouse_y)
cls.mouse_warped = True

if event.mouse_region_y <= 0:
mouse_y = context.region.height + cls.region_offset_y - 10
context.window.cursor_warp(event.mouse_x, mouse_y)

if event.mouse_region_y >= context.region.height - 1:
mouse_y = cls.region_offset_y + 100
context.window.cursor_warp(event.mouse_x, mouse_y)


def draw_header(cls):
is_summoned = getattr(cls, "summoned", False)

Expand Down Expand Up @@ -101,7 +125,7 @@ def draw_header(cls):
cls.line_step = 0


def draw_property(cls, property_content, metadata_content, active=False, alt_mode=False):
def draw_property(cls, property_content, metadata_content, active=False, alt_mode=False, mouse_value=False):
blf.size(0, 28, cls.dpi)

if cls.operator_passthrough:
Expand All @@ -118,6 +142,11 @@ def draw_property(cls, property_content, metadata_content, active=False, alt_mod
else:
blf.draw(0, "●")

if get_preferences().enable_mouse_values and mouse_value:
blf.size(0, 22, cls.dpi)
blf.position(0, cls.overlay_x - (15 * cls.dpi_scalar), cls.overlay_y - ((34 * cls.dpi_scalar) + (cls.line_spacer * cls.line_step)), 0)
blf.draw(0, "»")

blf.size(0, 16, cls.dpi)

if cls.operator_passthrough:
Expand Down
16 changes: 12 additions & 4 deletions power_mods/bevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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.preferences import get_preferences


mod_bevel = "Bevel — ND B"
Expand Down Expand Up @@ -46,15 +47,15 @@ def modal(self, context, event):
elif self.key_step_up:
if self.key_alt:
self.segments = 2 if self.segments == 1 else self.segments + segment_factor
else:
elif self.key_no_modifiers:
self.width += width_factor

self.dirty = True

elif self.key_step_down:
if self.key_alt:
self.segments = max(1, self.segments - segment_factor)
else:
elif self.key_no_modifiers:
self.width = max(0.0001, self.width - width_factor)

self.dirty = True
Expand All @@ -67,6 +68,12 @@ def modal(self, context, event):
elif self.key_movement_passthrough:
return {'PASS_THROUGH'}

if get_preferences().enable_mouse_values:
if self.key_no_modifiers:
self.width = max(0.0001, self.width + self.mouse_value)

self.dirty = True

if self.dirty:
self.operate(context)

Expand All @@ -91,7 +98,7 @@ def invoke(self, context, event):

self.operate(context)

capture_modifier_keys(self)
capture_modifier_keys(self, None, event.mouse_x)

init_overlay(self, event)
register_draw_handler(self, draw_text_callback)
Expand Down Expand Up @@ -161,7 +168,8 @@ def draw_text_callback(self):
"Width: {0:.1f}".format(self.width * 1000),
"(±{0:.1f}) | Shift (±{1:.1f})".format(self.base_width_factor * 1000, (self.base_width_factor / 10) * 1000),
active=self.key_no_modifiers,
alt_mode=self.key_shift_no_modifiers)
alt_mode=self.key_shift_no_modifiers,
mouse_value=True)

draw_property(
self,
Expand Down
14 changes: 11 additions & 3 deletions power_mods/circular_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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.collections import move_to_utils_collection
from .. lib.preferences import get_preferences


class ND_OT_circular_array(bpy.types.Operator):
Expand Down Expand Up @@ -65,6 +66,12 @@ def modal(self, context, event):
elif self.key_movement_passthrough:
return {'PASS_THROUGH'}

if get_preferences().enable_mouse_values:
if self.key_ctrl:
self.angle = max(-360, min(360, self.angle + self.mouse_value_mag))

self.dirty = True

if self.dirty:
self.operate(context)

Expand All @@ -89,7 +96,7 @@ def invoke(self, context, event):
self.add_array_modifier()
self.operate(context)

capture_modifier_keys(self)
capture_modifier_keys(self, None, event.mouse_x)

init_overlay(self, event)
register_draw_handler(self, draw_text_callback)
Expand Down Expand Up @@ -171,10 +178,11 @@ def draw_text_callback(self):

draw_property(
self,
"Angle: {}".format('Circle (360°)' if abs(self.angle) == 360 else "Arc ({}°)".format(self.angle)),
"Angle: {}".format('Circle (360°)' if abs(self.angle) == 360 else "Arc ({0:.1f}°)".format(self.angle)),
"Ctrl (±15) | Shift (±1)",
active=self.key_ctrl,
alt_mode=self.key_shift_ctrl)
alt_mode=self.key_shift_ctrl,
mouse_value=True)


def menu_func(self, context):
Expand Down
12 changes: 10 additions & 2 deletions power_mods/profile_extrude.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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.preferences import get_preferences


mod_screw = "Extrusion — ND PE"
Expand Down Expand Up @@ -71,6 +72,12 @@ def modal(self, context, event):
elif self.key_movement_passthrough:
return {'PASS_THROUGH'}

if get_preferences().enable_mouse_values:
if self.key_no_modifiers:
self.extrusion_length = max(0, self.extrusion_length + self.mouse_value)

self.dirty = True

if self.dirty:
self.operate(context)

Expand All @@ -97,7 +104,7 @@ def invoke(self, context, event):

self.operate(context)

capture_modifier_keys(self)
capture_modifier_keys(self, None, event.mouse_x)

init_overlay(self, event)
register_draw_handler(self, draw_text_callback)
Expand Down Expand Up @@ -209,7 +216,8 @@ def draw_text_callback(self):
"Extrusion: {0:.1f}".format(self.extrusion_length * 1000),
"(±{0:.1f}) | Shift + (±{1:.1f})".format(self.base_extrude_factor * 1000, (self.base_extrude_factor / 10) * 1000),
active=self.key_no_modifiers,
alt_mode=self.key_shift_no_modifiers)
alt_mode=self.key_shift_no_modifiers,
mouse_value=True)

draw_property(
self,
Expand Down
Loading

0 comments on commit a873f74

Please sign in to comment.