Skip to content

Commit

Permalink
feat: create a dedicated overlay manager, drawing utilities, and upda…
Browse files Browse the repository at this point in the history
…te all operators respectively
  • Loading branch information
tristan-hm committed Jan 24, 2022
1 parent 1dcd835 commit 1520b36
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 210 deletions.
102 changes: 16 additions & 86 deletions bolt.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bpy
import bmesh
import blf
from math import radians
from . overlay import update_overlay, init_overlay, register_draw_handler, unregister_draw_handler, draw_header, draw_property, redraw_regions


class ND_OT_bolt(bpy.types.Operator):
Expand All @@ -16,10 +16,10 @@ def modal(self, context, event):
radius_factor = 0.001 if event.shift else 0.01
thickness_factor = 0.001 if event.shift else 0.01

self.mouse_x = event.mouse_x
self.mouse_y = event.mouse_y
if event.type == 'MOUSEMOVE':
update_overlay(self, context, event)

if event.type == 'WHEELUPMOUSE':
elif event.type == 'WHEELUPMOUSE':
if event.alt and event.ctrl:
self.offset += offset_factor
elif event.alt:
Expand Down Expand Up @@ -74,42 +74,14 @@ def invoke(self, context, event):
self.add_solidify_modifier()
self.align_object_to_3d_cursor(context)

self.register_draw_handler(context)
init_overlay(self, event)
register_draw_handler(self, draw_text_callback, "nd_draw_bolt")

context.window_manager.modal_handler_add(self)

return {'RUNNING_MODAL'}


def register_draw_handler(self, context):
handler = bpy.app.driver_namespace.get('nd_draw_bolt')

if not handler:
handler = bpy.types.SpaceView3D.draw_handler_add(draw_text_callback, (self, context), 'WINDOW', 'POST_PIXEL')
dns = bpy.app.driver_namespace
dns['nd_draw_bolt'] = handler

self.redraw_regions(context)


def unregister_draw_handler(self, context):
handler = bpy.app.driver_namespace.get('nd_draw_bolt')

if handler:
bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW')
del bpy.app.driver_namespace['nd_draw_bolt']

self.redraw_regions(context)


def redraw_regions(self, context):
for area in context.window.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
region.tag_redraw()


@classmethod
def poll(cls, context):
return context.mode == 'OBJECT'
Expand Down Expand Up @@ -215,65 +187,23 @@ def select_bolt(self, context):
def finish(self, context):
self.handle_optional_boolean_ops(context)
self.select_bolt(context)
self.unregister_draw_handler(context)
unregister_draw_handler(self, "nd_draw_bolt")


def revert(self, context):
self.select_bolt(context)
bpy.ops.object.delete()
self.unregister_draw_handler(context)
unregister_draw_handler(self, "nd_draw_bolt")


def draw_text_callback(self, context):
cursor_offset_x = 20
cursor_offset_y = -100

blf.size(0, 24, 72)
blf.color(0, 1.0, 0.529, 0.216, 1.0)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y, 0)
blf.draw(0, "ND — Bolt")

blf.size(0, 16, 72)
blf.color(0, 1.0, 1.0, 1.0, 1.0)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 25, 0)
blf.draw(0, "Segments: {}".format(self.segments))

blf.size(0, 11, 72)
blf.color(0, 1.0, 1.0, 1.0, 0.3)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 40, 0)
blf.draw(0, "(±1)")

blf.size(0, 16, 72)
blf.color(0, 1.0, 1.0, 1.0, 1.0)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 65, 0)
blf.draw(0, "Radius: {0:.0f}mm".format(self.radius * 1000))

blf.size(0, 11, 72)
blf.color(0, 1.0, 1.0, 1.0, 0.3)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 80, 0)
blf.draw(0, "Alt (±10mm) | Shift + Alt (±1mm)")

blf.size(0, 16, 72)
blf.color(0, 1.0, 1.0, 1.0, 1.0)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 105, 0)
blf.draw(0, "Thickness: {0:.0f}mm".format(self.thickness * 1000))

blf.size(0, 11, 72)
blf.color(0, 1.0, 1.0, 1.0, 0.3)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 120, 0)
blf.draw(0, "Ctrl (±10mm) | Shift + Ctrl (±1mm)")

blf.size(0, 16, 72)
blf.color(0, 1.0, 1.0, 1.0, 1.0)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 145, 0)
blf.draw(0, "Offset: {0:.3f}".format(self.offset))

blf.size(0, 11, 72)
blf.color(0, 1.0, 1.0, 1.0, 0.3)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 160, 0)
blf.draw(0, "Ctrl + Alt (±0.01) | Shift + Ctrl + Alt (±0.001)")

self.redraw_regions(context)
def draw_text_callback(self):
draw_header(self, "ND — Bolt")
draw_property(self, "Segments: {}".format(self.segments), "(±1)")
draw_property(self, "Radius: {0:.0f}mm".format(self.radius * 1000), "Alt (±10mm) | Shift + Alt (±1mm)")
draw_property(self, "Thickness: {0:.0f}mm".format(self.thickness * 1000), "Ctrl (±10mm) | Shift + Ctrl (±1mm)")
draw_property(self, "Offset: {0:.3f}".format(self.offset), "Ctrl + Alt (±0.01) | Shift + Ctrl + Alt (±0.001)")

redraw_regions()


def menu_func(self, context):
Expand Down
67 changes: 12 additions & 55 deletions faux_bevel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import bpy
import bmesh
import blf
from math import radians
from . overlay import update_overlay, init_overlay, register_draw_handler, unregister_draw_handler, draw_header, draw_property, redraw_regions


class ND_OT_faux_bevel(bpy.types.Operator):
Expand All @@ -14,10 +14,10 @@ class ND_OT_faux_bevel(bpy.types.Operator):
def modal(self, context, event):
width_factor = 0.0001 if event.shift else 0.001

self.mouse_x = event.mouse_x
self.mouse_y = event.mouse_y
if event.type == 'MOUSEMOVE':
update_overlay(self, context, event)

if event.type == 'WHEELUPMOUSE':
elif event.type == 'WHEELUPMOUSE':
self.width += width_factor

elif event.type == 'WHEELDOWNMOUSE':
Expand Down Expand Up @@ -50,42 +50,14 @@ def invoke(self, context, event):
self.add_bevel_modifier(context)
self.add_weighted_normal_modifer(context)

self.register_draw_handler(context)
init_overlay(self, event)
register_draw_handler(self, draw_text_callback, "nd_draw_faux_bevel")

context.window_manager.modal_handler_add(self)

return {'RUNNING_MODAL'}


def register_draw_handler(self, context):
handler = bpy.app.driver_namespace.get('nd_draw_faux_bevel')

if not handler:
handler = bpy.types.SpaceView3D.draw_handler_add(draw_text_callback, (self, context), 'WINDOW', 'POST_PIXEL')
dns = bpy.app.driver_namespace
dns['nd_draw_faux_bevel'] = handler

self.redraw_regions(context)


def unregister_draw_handler(self, context):
handler = bpy.app.driver_namespace.get('nd_draw_faux_bevel')

if handler:
bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW')
del bpy.app.driver_namespace['nd_draw_faux_bevel']

self.redraw_regions(context)


def redraw_regions(self, context):
for area in context.window.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
region.tag_redraw()


@classmethod
def poll(cls, context):
if context.mode == 'OBJECT':
Expand All @@ -112,35 +84,20 @@ def operate(self, context):


def finish(self, context):
self.unregister_draw_handler(context)
unregister_draw_handler(self, "nd_draw_faux_bevel")


def revert(self, context):
bpy.ops.object.modifier_remove(modifier=self.bevel.name)
bpy.ops.object.modifier_remove(modifier=self.wn.name)
self.unregister_draw_handler(context)
unregister_draw_handler(self, "nd_draw_faux_bevel")


def draw_text_callback(self, context):
cursor_offset_x = 20
cursor_offset_y = -100

blf.size(0, 24, 72)
blf.color(0, 1.0, 0.529, 0.216, 1.0)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y, 0)
blf.draw(0, "ND — Faux Bevel")

blf.size(0, 16, 72)
blf.color(0, 1.0, 1.0, 1.0, 1.0)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 25, 0)
blf.draw(0, "Width: {0:.1f}mm".format(self.width * 1000))

blf.size(0, 11, 72)
blf.color(0, 1.0, 1.0, 1.0, 0.3)
blf.position(0, self.mouse_x + cursor_offset_x, self.mouse_y + cursor_offset_y - 40, 0)
blf.draw(0, "(±1mm) | Shift (±0.1mm)")
def draw_text_callback(self):
draw_header(self, "ND — Faux Bevel")
draw_property(self, "Width: {0:.1f}mm".format(self.width * 1000), "(±1mm) | Shift (±0.1mm)")

self.redraw_regions(context)
redraw_regions()


def menu_func(self, context):
Expand Down
87 changes: 87 additions & 0 deletions overlay.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import bpy
import blf


def register_draw_handler(self, callback, name):
handler = bpy.app.driver_namespace.get(name)

if not handler:
handler = bpy.types.SpaceView3D.draw_handler_add(callback, (self, ), 'WINDOW', 'POST_PIXEL')
dns = bpy.app.driver_namespace
dns[name] = handler

redraw_regions()


def unregister_draw_handler(self, name):
handler = bpy.app.driver_namespace.get(name)

if handler:
bpy.types.SpaceView3D.draw_handler_remove(handler, 'WINDOW')
del bpy.app.driver_namespace[name]

redraw_regions()


def redraw_regions():
for area in bpy.context.window.screen.areas:
if area.type == 'VIEW_3D':
for region in area.regions:
if region.type == 'WINDOW':
region.tag_redraw()


def init_overlay(self, event):
self.overlay_offset_x = 25
self.overlay_offset_y = -15

self.property_step = 0
self.property_spacer = 40

self.region_offset_x = event.mouse_x - event.mouse_region_x
self.region_offset_y = event.mouse_y - event.mouse_region_y

self.overlay_x = event.mouse_x - self.region_offset_x + self.overlay_offset_x
self.overlay_y = event.mouse_y - self.region_offset_y + self.overlay_offset_y


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

region_buffer = 5

if event.mouse_region_x <= 0:
context.window.cursor_warp(context.region.width + self.region_offset_x - region_buffer, event.mouse_y)

if event.mouse_region_x >= context.region.width - 1:
context.window.cursor_warp(self.region_offset_x + region_buffer, event.mouse_y)

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

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


def draw_header(self, content):
blf.size(0, 24, 72)
blf.color(0, 1.0, 0.529, 0.216, 1.0)
blf.position(0, self.overlay_x, self.overlay_y, 0)
blf.draw(0, content)

self.property_step = 0


def draw_property(self, property_content, metadata_content):
blf.size(0, 16, 72)
blf.color(0, 1.0, 1.0, 1.0, 1.0)
blf.position(0, self.overlay_x, self.overlay_y - (25 + (self.property_spacer * self.property_step)), 0)
blf.draw(0, property_content)

blf.size(0, 11, 72)
blf.color(0, 1.0, 1.0, 1.0, 0.3)
blf.position(0, self.overlay_x, self.overlay_y - (40 + (self.property_spacer * self.property_step)), 0)
blf.draw(0, metadata_content)

self.property_step += 1
Loading

0 comments on commit 1520b36

Please sign in to comment.