Skip to content

Commit

Permalink
improved rounded border on options, code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
vxlcoder committed Nov 25, 2018
1 parent 98138c4 commit 0b142bc
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 50 deletions.
131 changes: 85 additions & 46 deletions common/ui.py
Expand Up @@ -412,18 +412,26 @@ def _draw(self):


class UI_Background(UI_Element):
def __init__(self, background=None, rounded=False, border=None, border_thickness=1, ui_item=None, margin=0):
super().__init__(margin=margin)
def __init__(self, **kwargs):
opts = kwargopts(kwargs, {
'background': None,
'rounded': 0,
'border': None,
'border_thickness': 1,
'ui_item': None,
'margin': 0,
})
super().__init__(margin=opts.margin)
self.defer_recalc = True

self.ui_item = None

self.background = background
self.rounded_background = rounded
self.border = border
self.background = opts.background
self.rounded = opts.rounded
self.border = opts.border
# TODO: should border_thickness add to margin?
self.border_thickness = 0
self.set_ui_item(ui_item)
self.set_ui_item(opts.ui_item)

self.defer_recalc = False

Expand Down Expand Up @@ -461,47 +469,62 @@ def _draw(self):
if not self.ui_item or not self.ui_item.visible: return
l,t = self.pos
w,h = self.size
r,b = l + w, t - h + 1
rounded_count = 10 # number of segments for each rounded corner

bgl.glEnable(bgl.GL_BLEND)

if self.background:
bgl.glColor4f(*self.background)
bgl.glBegin(bgl.GL_QUADS)
if self.rounded_background:
bgl.glVertex2f(l+1, t)
bgl.glVertex2f(l+w-1, t)
bgl.glVertex2f(l+w-1, t-h)
bgl.glVertex2f(l+1, t-h)

bgl.glVertex2f(l, t-1)
bgl.glVertex2f(l+1, t-1)
bgl.glVertex2f(l+1, t-h+1)
bgl.glVertex2f(l, t-h+1)

bgl.glVertex2f(l+w-1, t-1)
bgl.glVertex2f(l+w, t-1)
bgl.glVertex2f(l+w, t-h+1)
bgl.glVertex2f(l+w-1, t-h+1)
if self.rounded:
cos,sin,radians = math.cos,math.sin,math.radians
rounded = self.rounded
lr,rr = l + rounded, r - rounded
tr,br = t - rounded, b + rounded
first = True
for i in range(rounded_count+1):
ir = 90 * i / rounded_count
rad0, rad1 = radians(90 + ir), radians(90 - ir)
bx0,by0 = lr + rounded * cos(rad0), tr + rounded * sin(rad0)
bx1,by1 = rr + rounded * cos(rad1), tr + rounded * sin(rad1)
if not first:
bgl.glVertex2f(tx1, ty1)
bgl.glVertex2f(tx0, ty0)
bgl.glVertex2f(bx0, by0)
bgl.glVertex2f(bx1, by1)
first = False
tx0,ty0,tx1,ty1 = bx0,by0,bx1,by1
for i in range(rounded_count+1):
ir = 90 * (rounded_count - i) / rounded_count
rad0, rad1 = radians(270 - ir), radians(270 + ir)
bx0,by0 = lr + rounded * cos(rad0), br + rounded * sin(rad0)
bx1,by1 = rr + rounded * cos(rad1), br + rounded * sin(rad1)
bgl.glVertex2f(tx1, ty1)
bgl.glVertex2f(tx0, ty0)
bgl.glVertex2f(bx0, by0)
bgl.glVertex2f(bx1, by1)
tx0,ty0,tx1,ty1 = bx0,by0,bx1,by1
else:
bgl.glVertex2f(l, t)
bgl.glVertex2f(l+w, t)
bgl.glVertex2f(l+w, t-h)
bgl.glVertex2f(l, t-h)
bgl.glEnd()

if self.border:
bgl.glColor4f(*self.border)
self.drawing.line_width(self.border_thickness)
bgl.glBegin(bgl.GL_LINE_STRIP)
if self.rounded_background:
bgl.glVertex2f(l+1,t)
bgl.glVertex2f(l,t-1)
bgl.glVertex2f(l,t-h+1)
bgl.glVertex2f(l+1,t-h)
bgl.glVertex2f(l+w-1,t-h)
bgl.glVertex2f(l+w,t-h+1)
bgl.glVertex2f(l+w,t-1)
bgl.glVertex2f(l+w-1,t)
bgl.glVertex2f(l+1,t)
if self.rounded:
cos,sin,radians = math.cos,math.sin,math.radians
rounded = self.rounded
lr,rr = l + rounded, r - rounded
tr,br = t - rounded, b + rounded
for ci,cx,cy in [(0,rr,tr),(90,lr,tr),(180,lr,br),(270,rr,br)]:
for i in range(rounded_count+1):
rad = radians(ci + 90 * i / rounded_count)
bgl.glVertex2f(cx + cos(rad) * rounded, cy + sin(rad) * rounded)
else:
bgl.glVertex2f(l,t)
bgl.glVertex2f(l,t-h)
Expand Down Expand Up @@ -706,18 +729,25 @@ def _draw(self):


class UI_Container(UI_Element):
def __init__(self, vertical=True, background=None, margin=0, separation=2):
def __init__(self, **kwargs):
opts = kwargopts(kwargs, {
'vertical': True,
'background': None,
'rounded': 0,
'margin': 0,
'separation': 2,
})
self._vertical = None
self._separation = None

super().__init__(margin=margin)
super().__init__(margin=opts.margin)
self.defer_recalc = True

self.vertical = vertical
self.ui_items = []
self.background = background
self.rounded_background = False
self.separation = separation
self.vertical = opts.vertical
self.background = opts.background
self.rounded = opts.rounded
self.separation = opts.separation

self.defer_recalc = False

Expand Down Expand Up @@ -803,7 +833,7 @@ def _draw(self):
bgl.glEnable(bgl.GL_BLEND)
bgl.glColor4f(*self.background)
bgl.glBegin(bgl.GL_QUADS)
if self.rounded_background:
if self.rounded:
bgl.glVertex2f(l+1, t)
bgl.glVertex2f(l+w-1, t)
bgl.glVertex2f(l+w-1, t-h)
Expand Down Expand Up @@ -1308,6 +1338,7 @@ def __init__(self, fn_get_option, fn_set_option, **kwargs):
'margin': 2,
'separation': 0,
'hovercolor': (1,1,1,0.1),
'rounded': 1,
})
super().__init__(vertical=opts.vertical, margin=opts.margin)
self.defer_recalc = True
Expand All @@ -1324,6 +1355,7 @@ def __init__(self, fn_get_option, fn_set_option, **kwargs):
self.mouse_prev = None
self.defer_recalc = False
self.separation = opts.separation
self.rounded = opts.rounded

def set_label(self, label, fontsize=None, align=None, margin=None):
self.ui_label.visible = label is not None
Expand All @@ -1335,7 +1367,7 @@ def set_label(self, label, fontsize=None, align=None, margin=None):
class UI_Option(UI_Background):
def __init__(self, options, label, value, **kwargs):
opts = kwargopts(kwargs)
super().__init__(rounded=True, margin=0)
super().__init__(rounded=opts.rounded, margin=0)
self.defer_recalc = True
self.label = label
self.value = value
Expand Down Expand Up @@ -1384,6 +1416,7 @@ def add_option(self, label, **kwargs):
'align': -1,
'showlabel': True,
'margin': 2,
'rounded': self.rounded,
})
value = opts.value or label
assert value not in self.values, "All option values must be unique!"
Expand Down Expand Up @@ -1446,24 +1479,30 @@ def _draw(self):
class UI_Image(UI_Element):
executor = ThreadPoolExecutor()

def __init__(self, image_data, margin=0, async_load=True, width=None, height=None):
def __init__(self, image_data, **kwargs):
opts = kwargopts(kwargs, {
'margin': 0,
'async_load': True,
'width': None,
'height': None,
})
super().__init__()
self.defer_recalc = True
self.image_data = image_data
self.image_width,self.image_height = 16,16
self.width = width or 16
self.height = height or 16
self.size_set = (width is not None) or (height is not None)
self.width = opts.width or 16
self.height = opts.height or 16
self.size_set = (opts.width is not None) or (opts.height is not None)
self.loaded = False
self.buffered = False
self.deleted = False
self.margin = margin
self.margin = opts.margin

self.texbuffer = bgl.Buffer(bgl.GL_INT, [1])
bgl.glGenTextures(1, self.texbuffer)
self.texture_id = self.texbuffer[0]

if async_load: self.executor.submit(self.load_image)
if opts.async_load: self.executor.submit(self.load_image)
else: self.load_image()
self.defer_recalc = False

Expand Down Expand Up @@ -1721,7 +1760,7 @@ def __init__(self, label, fn_get_checked, fn_set_checked, **kwopts):
super().__init__(margin=0)
self.defer_recalc = True

self.bg = self.add(UI_Background(border_thickness=1, rounded=True))
self.bg = self.add(UI_Background(border_thickness=1, rounded=1))
self.bg.set_ui_item(UI_Label(label, align=0))
self.fn_get_checked = fn_get_checked
self.fn_set_checked = fn_set_checked
Expand Down
7 changes: 3 additions & 4 deletions op_polytrim/polytrim_ui_init.py
Expand Up @@ -72,11 +72,10 @@ def radius_setter(v):

win_tools = self.wm.create_window('Polytrim Tools', {'pos':7, 'movable':True, 'bgcolor':(0.50, 0.50, 0.50, 0.90)})

precut_container = win_tools.add(ui.UI_Container())
precut_container.rounded_background = True

precut_container = win_tools.add(ui.UI_Container(rounded=1))

precut_tools = precut_container.add(ui.UI_Frame('Pre Cut Tools', fontsize=16, spacer=0))
precut_mode = precut_tools.add(ui.UI_Options(mode_getter, mode_setter, separation=0))
precut_mode = precut_tools.add(ui.UI_Options(mode_getter, mode_setter, separation=0, rounded=1))
precut_mode.add_option('Boundary Edit', value='spline', icon=ui.UI_Image('polyline.png', width=32, height=32))
precut_mode.add_option('Boundary > Region', value='seed', icon=ui.UI_Image('seed.png', width=32, height=32))
precut_mode.add_option('Region Paint', value='region', icon=ui.UI_Image('paint.png', width=32, height=32))
Expand Down

0 comments on commit 0b142bc

Please sign in to comment.