def _draw_exception(self, context): """OpenGL drawing code for the EXCEPTION state.""" import textwrap content_height, content_width = self._window_size(context) bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(0.2, 0.0, 0.0, 0.6) bgl.glRectf(0, 0, content_width, content_height) font_id = 0 ex = self.async_task.exception() if isinstance(ex, pillar.UserNotLoggedInError): ex_msg = 'You are not logged in on Blender ID. Please log in at User Preferences, ' \ 'System, Blender ID.' else: ex_msg = str(ex) if not ex_msg: ex_msg = str(type(ex)) text = "An error occurred:\n%s" % ex_msg lines = textwrap.wrap(text) bgl.glColor4f(1.0, 1.0, 1.0, 1.0) blf.size(font_id, 20, 72) _, text_height = blf.dimensions(font_id, 'yhBp') def position(line_nr): blf.position(font_id, content_width * 0.1, content_height * 0.8 - line_nr * text_height, 0) for line_idx, line in enumerate(lines): position(line_idx) blf.draw(font_id, line) bgl.glDisable(bgl.GL_BLEND)
def draw(self, context, render=False): if self.image is None: return bgl.glPushAttrib(bgl.GL_ENABLE_BIT) p0 = self.pts[0] p1 = self.pts[1] bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(*self.colour) bgl.glRectf(p0.x, p0.y, p1.x, p1.y) self.image.gl_load() bgl.glEnable(bgl.GL_BLEND) bgl.glBindTexture(bgl.GL_TEXTURE_2D, self.image.bindcode[0]) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) # bgl.glColor4f(1, 1, 1, 1) bgl.glBegin(bgl.GL_QUADS) bgl.glTexCoord2d(0, 0) bgl.glVertex2d(p0.x, p0.y) bgl.glTexCoord2d(0, 1) bgl.glVertex2d(p0.x, p1.y) bgl.glTexCoord2d(1, 1) bgl.glVertex2d(p1.x, p1.y) bgl.glTexCoord2d(1, 0) bgl.glVertex2d(p1.x, p0.y) bgl.glEnd() self.image.gl_free() bgl.glDisable(bgl.GL_TEXTURE_2D)
def _draw_browser(self, context): """OpenGL drawing code for the BROWSING state.""" window_region = self._window_region(context) content_width = window_region.width - ITEM_MARGIN_X * 2 content_height = window_region.height - ITEM_MARGIN_Y * 2 content_x = ITEM_MARGIN_X content_y = context.area.height - ITEM_MARGIN_Y - TARGET_ITEM_HEIGHT col_count = content_width // TARGET_ITEM_WIDTH item_width = (content_width - (col_count * ITEM_PADDING_X)) / col_count item_height = TARGET_ITEM_HEIGHT block_width = item_width + ITEM_PADDING_X block_height = item_height + ITEM_MARGIN_Y bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(0.0, 0.0, 0.0, 0.6) bgl.glRectf(0, 0, window_region.width, window_region.height) if self.current_display_content: bottom_y = float('inf') # The -1 / +2 are for extra rows that are drawn only half at the top/bottom. first_item_idx = max(0, int(-self.scroll_offset // block_height - 1) * col_count) items_per_page = int(content_height // item_height + 2) * col_count last_item_idx = first_item_idx + items_per_page for item_idx, item in enumerate(self.current_display_content): x = content_x + (item_idx % col_count) * block_width y = content_y - (item_idx // col_count) * block_height - self.scroll_offset item.update_placement(x, y, item_width, item_height) if first_item_idx <= item_idx < last_item_idx: # Only draw if the item is actually on screen. item.draw(highlighted=item.hits(self.mouse_x, self.mouse_y)) bottom_y = min(y, bottom_y) self.scroll_offset_space_left = window_region.height - bottom_y self.scroll_offset_max = (self.scroll_offset - self.scroll_offset_space_left + 0.25 * block_height) else: font_id = 0 text = "Communicating with Blender Cloud" bgl.glColor4f(1.0, 1.0, 1.0, 1.0) blf.size(font_id, 20, 72) text_width, text_height = blf.dimensions(font_id, text) blf.position(font_id, content_x + content_width * 0.5 - text_width * 0.5, content_y - content_height * 0.3 + text_height * 0.5, 0) blf.draw(font_id, text) bgl.glDisable(bgl.GL_BLEND)
def drawCallbackMenu(self, context): global targetItemWidth global targetItemHeight marginX = 20 marginY = 5 paddingX = 5 bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(0.0, 0.0, 0.0, 0.6) bgl.glRectf(0, 0, context.area.regions[4].width, context.area.regions[4].height) contentWidth = context.area.regions[4].width - marginX * 2 contentHeight = context.area.regions[4].height - marginY * 2 contentX = marginX contentY = context.area.height - marginY - targetItemHeight - 50 colCount = int(contentWidth / targetItemWidth) itemWidth = (contentWidth - (colCount * paddingX)) / (colCount + 1) itemHeight = targetItemHeight col = 0 row = 0 x = contentX y = contentY if len(self.current_dir_content) == 0: font_id = 0 text = "Folder doesn't contain any assets!" bgl.glColor4f(1.0, 1.0, 1.0, 1.0) blf.size(font_id, 20, 72) textWidth, textHeight = blf.dimensions(font_id, text) blf.position(font_id, contentX + contentWidth * 0.5 - textWidth * 0.5, contentY - contentHeight * 0.5 + textHeight * 0.5, 0) blf.draw(font_id, text) else: for item in self.current_dir_content: if self.mouseX > x and self.mouseX < x + itemWidth and self.mouseY > y and self.mouseY < y + itemHeight: item['highlighted'] = True else: item['highlighted'] = False drawMenuItem(item, x, y, itemWidth, itemHeight) x = x + itemWidth + paddingX col += 1 if col > colCount: col = 0 x = contentX y = y - itemHeight - marginY row += 1 bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_TEXTURE_2D)
def drawCallbackMenu(self, context): global targetItemWidth global targetItemHeight marginX = 20 marginY = 5 paddingX = 5 bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(0.0, 0.0, 0.0, 0.6) bgl.glRectf(0,0,context.area.regions[4].width,context.area.regions[4].height) contentWidth = context.area.regions[4].width - marginX * 2 contentHeight = context.area.regions[4].height - marginY * 2 contentX = marginX contentY = context.area.height - marginY - targetItemHeight - 50 colCount = int(contentWidth / targetItemWidth) itemWidth = (contentWidth - (colCount * paddingX)) / (colCount + 1) itemHeight = targetItemHeight col = 0 row = 0 x = contentX y = contentY if len(self.current_dir_content ) == 0: font_id = 0 text = "Folder doesn't contain any assets!" bgl.glColor4f(1.0, 1.0, 1.0, 1.0) blf.size(font_id, 20, 72) textWidth, textHeight = blf.dimensions(font_id, text) blf.position(font_id, contentX + contentWidth * 0.5 - textWidth * 0.5, contentY - contentHeight * 0.5 + textHeight * 0.5, 0) blf.draw(font_id, text) else: for item in self.current_dir_content: if self.mouseX > x and self.mouseX < x + itemWidth and self.mouseY > y and self.mouseY < y + itemHeight: item['highlighted'] = True else: item['highlighted'] = False drawMenuItem(item, x, y, itemWidth, itemHeight) x = x + itemWidth + paddingX col += 1 if col > colCount: col = 0 x = contentX y = y - itemHeight - marginY row += 1 bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_TEXTURE_2D)
def draw_button_boarder(self, highlighted: bool): bgl.glEnable(bgl.GL_BLEND) if highlighted: bgl.glColor4f(0.555, 0.555, 0.555, 0.8) else: bgl.glColor4f(0.447, 0.447, 0.447, 0.8) bgl.glRectf(self.x_location, self.y_location, self.width + self.x_location, self.height + self.y_location) bgl.glDisable(bgl.GL_BLEND)
def drawMenuItem(item, x, y, width, height): global iconWidth global iconHeight iconMarginX = 4 iconMarginY = 4 textMarginX = 6 textHeight = 16 textWidth = 72 bgl.glEnable(bgl.GL_BLEND) if item['highlighted']: bgl.glColor4f(0.555, 0.555, 0.555, 0.8) else: bgl.glColor4f(0.447, 0.447, 0.447, 0.8) bgl.glRectf(x, y, x + width, y + height) texture = item['icon'] texture.gl_load() bgl.glColor4f(0.0, 0.0, 1.0, 0.5) #bgl.glLineWidth(1.5) #------ TEXTURE ---------# bgl.glEnable(bgl.GL_BLEND) bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.bindcode[0]) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri( bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST ) #GL_LINEAR seems to be used in Blender for background images bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glColor4f(1, 1, 1, 1) bgl.glBegin(bgl.GL_QUADS) bgl.glTexCoord2d(0, 0) bgl.glVertex2d(x + iconMarginX, y) bgl.glTexCoord2d(0, 1) bgl.glVertex2d(x + iconMarginX, y + iconHeight) bgl.glTexCoord2d(1, 1) bgl.glVertex2d(x + iconMarginX + iconWidth, y + iconHeight) bgl.glTexCoord2d(1, 0) bgl.glVertex2d(x + iconMarginX + iconWidth, y) bgl.glEnd() texture.gl_free() # draw some text font_id = 0 blf.position(font_id, x + iconMarginX + iconWidth + textMarginX, y + iconHeight * 0.5 - 0.25 * textHeight, 0) blf.size(font_id, textHeight, textWidth) blf.draw(font_id, item['text'])
def draw(self, context, render=False): self.render = render bgl.glPushAttrib(bgl.GL_ENABLE_BIT) bgl.glEnable(bgl.GL_BLEND) if render: # enable anti-alias on polygons bgl.glEnable(bgl.GL_POLYGON_SMOOTH) bgl.glColor4f(*self.colour) p0 = self.pts[0] p1 = self.pts[1] bgl.glRectf(p0.x, p0.y, p1.x, p1.y) self._end()
def drawMenuItem(item, x, y, width, height): global iconWidth global iconHeight iconMarginX = 4 iconMarginY = 4 textMarginX = 6 textHeight = 16 textWidth = 72 bgl.glEnable(bgl.GL_BLEND) if item["highlighted"]: bgl.glColor4f(0.555, 0.555, 0.555, 0.8) else: bgl.glColor4f(0.447, 0.447, 0.447, 0.8) bgl.glRectf(x, y, x + width, y + height) texture = item["icon"] texture.gl_load() bgl.glColor4f(0.0, 0.0, 1.0, 0.5) # bgl.glLineWidth(1.5) # ------ TEXTURE ---------# bgl.glEnable(bgl.GL_BLEND) bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.bindcode) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri( bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST ) # GL_LINEAR seems to be used in Blender for background images bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glColor4f(1, 1, 1, 1) bgl.glBegin(bgl.GL_QUADS) bgl.glTexCoord2d(0, 0) bgl.glVertex2d(x + iconMarginX, y) bgl.glTexCoord2d(0, 1) bgl.glVertex2d(x + iconMarginX, y + iconHeight) bgl.glTexCoord2d(1, 1) bgl.glVertex2d(x + iconMarginX + iconWidth, y + iconHeight) bgl.glTexCoord2d(1, 0) bgl.glVertex2d(x + iconMarginX + iconWidth, y) bgl.glEnd() texture.gl_free() # draw some text font_id = 0 blf.position(font_id, x + iconMarginX + iconWidth + textMarginX, y + iconHeight * 0.5 - 0.25 * textHeight, 0) blf.size(font_id, textHeight, textWidth) blf.draw(font_id, item["text"])
def _draw_text_on_colour(self, context, text, bgcolour): content_height, content_width = self._window_size(context) bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(*bgcolour) bgl.glRectf(0, 0, content_width, content_height) font_id = 0 bgl.glColor4f(1.0, 1.0, 1.0, 1.0) blf.size(font_id, 20, 72) text_width, text_height = blf.dimensions(font_id, text) blf.position(font_id, content_width * 0.5 - text_width * 0.5, content_height * 0.7 + text_height * 0.5, 0) blf.draw(font_id, text) bgl.glDisable(bgl.GL_BLEND)
def draw(self, context, render=False): self.render = render bgl.glPushAttrib(bgl.GL_ENABLE_BIT) bgl.glEnable(bgl.GL_BLEND) if render: # enable anti-alias on polygons bgl.glEnable(bgl.GL_POLYGON_SMOOTH) bgl.glColor4f(*self.colour) pts = [ self.position_2d_from_coord(context, pt, render) for pt in self.pts_3d ] p0 = pts[0] p1 = pts[1] bgl.glRectf(p0.x, p0.y, p1.x, p1.y) self._end()
def draw(self, highlighted: bool): bgl.glEnable(bgl.GL_BLEND) if highlighted: bgl.glColor4f(0.555, 0.555, 0.555, 0.8) else: bgl.glColor4f(0.447, 0.447, 0.447, 0.8) bgl.glRectf(self.x, self.y, self.x + self.width, self.y + self.height) texture = self.icon err = texture.gl_load(filter=bgl.GL_NEAREST, mag=bgl.GL_NEAREST) assert not err, 'OpenGL error: %i' % err bgl.glColor4f(0.0, 0.0, 1.0, 0.5) # bgl.glLineWidth(1.5) # ------ TEXTURE ---------# bgl.glBindTexture(bgl.GL_TEXTURE_2D, texture.bindcode[0]) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glColor4f(1, 1, 1, 1) bgl.glBegin(bgl.GL_QUADS) bgl.glTexCoord2d(0, 0) bgl.glVertex2d(self.x + self.icon_margin_x, self.y) bgl.glTexCoord2d(0, 1) bgl.glVertex2d(self.x + self.icon_margin_x, self.y + ICON_HEIGHT) bgl.glTexCoord2d(1, 1) bgl.glVertex2d(self.x + self.icon_margin_x + ICON_WIDTH, self.y + ICON_HEIGHT) bgl.glTexCoord2d(1, 0) bgl.glVertex2d(self.x + self.icon_margin_x + ICON_WIDTH, self.y) bgl.glEnd() bgl.glDisable(bgl.GL_TEXTURE_2D) bgl.glDisable(bgl.GL_BLEND) texture.gl_free() # draw some text font_id = 0 blf.position( font_id, self.x + self.icon_margin_x + ICON_WIDTH + self.text_margin_x, self.y + ICON_HEIGHT * 0.5 - 0.25 * self.text_height, 0) blf.size(font_id, self.text_height, self.text_width) blf.draw(font_id, self.label_text)
def draw_panel(self): bgl.glEnable(bgl.GL_BLEND) #PANEL bgl.glColor4f(0.0, 0.0, 0.0, 0.1) bgl.glRectf(self.x_location, self.y_location, self.width, self.height) #HEADER bgl.glColor4f(0.1, 0.1, 0.1, 1) bgl.glRectf(self.x_location, self.height - self.header_height, self.width, self.height) bgl.glDisable(bgl.GL_BLEND) #HEADER TEXT bgl.glColor4f(1, 1, 1, 1) font_id = 0 blf.position(font_id, 20, self.height - self.text_height - self.icon_margin_y, 0) blf.size(font_id, self.text_height, self.text_width) blf.draw(font_id, "hAndy Tool Library")
def draw_channel_color(scroller_width, channel_coords, curx, color): from bgl import glColor4f, glRectf, glEnable, glDisable, glBlendFunc, GL_BLEND, GL_ONE, GL_SRC_ALPHA context = bpy.context # Strip coords s_x1, s_y1, s_x2, s_y2 = channel_coords # Drawing coords x = 0 d_y1 = s_y1 d_y2 = s_y2 d_x1 = s_x1 d_x2 = s_x2 # be careful not to override the current frame line cf_x = context.scene.frame_current_final y = 0 r, g, b, a = color glColor4f(r, g, b, a) glEnable(GL_BLEND) # glBlendFunc(GL_SRC_ALPHA, GL_ONE); if d_x1 < cf_x and cf_x < d_x2: # current frame line over strip glRectf(d_x1, d_y1, cf_x - curx, d_y2) glRectf(cf_x + curx, d_y1, d_x2, d_y2) else: # Normal, full rectangle draw glRectf(d_x1, d_y1, d_x2, d_y2) glDisable(GL_BLEND)
def draw_exp_strings(self, font_id, ofsx, ofsy, minwidth=40, \ pertition=', ', start='', end='', col=None, errcol=None): if start: if col: bgl.glColor4f(*col) blf.position(font_id, ofsx, ofsy, 0) blf.draw(font_id, start) text_width, text_height = blf.dimensions(font_id, start) ofsx += text_width for i, string in enumerate(self.exp_strings): value = self.get_exp_value(i) if col and value is not None: bgl.glColor4f(*col) elif errcol and value is None: bgl.glColor4f(*errcol) name = self.exp_names[i] text = name.format(exp=string) if len(self.exp_strings) > 1 and 0 < i < len(exp_strings) - 1: text = text + pertition blf.position(font_id, ofsx, ofsy, 0) blf.draw(font_id, text) text_width, text_height = blf.dimensions(font_id, text) text_width = max(minwidth, text_width) # caret if i == self.caret[0]: if col: bgl.glColor4f(*col) t = name.split('{')[0] + string[:self.caret[1]] t_width, t_height = blf.dimensions(font_id, t) x = ofsx + t_width glRectf(x, ofsy - 4, x + 1, ofsy + 14) ofsx += text_width if end and i == len(self.exp_strings) - 1: if col: bgl.glColor4f(*col) blf.position(font_id, ofsx, ofsy, 0) blf.draw(font_id, end)
def draw_image(self, image, x, y, w, h, color=(0, 0, 0, 0.1)): bgl.glColor4f(0.5, 0.0, 0.5, 0.7) # draw main line and handles # bgl.glBegin(bgl.GL_LINES) bgl.glRectf(x, y, x + w, y + h) # bgl.glEnd() x1 = x y1 = y x2 = x + w y2 = y + h color = [0.5, 0.5, 0.5, 1] idx = image.gl_load(bgl.GL_NEAREST, bgl.GL_NEAREST) print([i for i in image.bindcode]) bgl.glBindTexture(bgl.GL_TEXTURE_2D, image.bindcode[0]) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glEnable(bgl.GL_BLEND) #bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glColor4f(color[0], color[1], color[2], color[3]) bgl.glBegin(bgl.GL_QUADS) bgl.glTexCoord2f(0, 0) bgl.glVertex2f(x1, y1) bgl.glTexCoord2f(0, 1) bgl.glVertex2f(x1, y2) bgl.glTexCoord2f(1, 1) bgl.glVertex2f(x2, y2) bgl.glTexCoord2f(1, 0) bgl.glVertex2f(x2, y1) bgl.glEnd() bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_TEXTURE_2D)
def draw_image(self, image, x, y, w, h, color=(0, 0, 0, 0.1)): bgl.glColor4f(0.5, 0.0, 0.5, 0.7) # draw main line and handles # bgl.glBegin(bgl.GL_LINES) bgl.glRectf(x, y, x + w, y + h) # bgl.glEnd() x1 = x y1 = y x2 = x + w y2 = y + h color = [0.5, 0.5, 0.5, 1] idx = image.gl_load(bgl.GL_NEAREST, bgl.GL_NEAREST) print([i for i in image.bindcode]) bgl.glBindTexture(bgl.GL_TEXTURE_2D, image.bindcode[0]) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MIN_FILTER, bgl.GL_NEAREST) bgl.glTexParameteri(bgl.GL_TEXTURE_2D, bgl.GL_TEXTURE_MAG_FILTER, bgl.GL_NEAREST) bgl.glEnable(bgl.GL_TEXTURE_2D) bgl.glEnable(bgl.GL_BLEND) # bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glColor4f(color[0], color[1], color[2], color[3]) bgl.glBegin(bgl.GL_QUADS) bgl.glTexCoord2f(0, 0) bgl.glVertex2f(x1, y1) bgl.glTexCoord2f(0, 1) bgl.glVertex2f(x1, y2) bgl.glTexCoord2f(1, 1) bgl.glVertex2f(x2, y2) bgl.glTexCoord2f(1, 0) bgl.glVertex2f(x2, y1) bgl.glEnd() bgl.glDisable(bgl.GL_BLEND) bgl.glDisable(bgl.GL_TEXTURE_2D)
def draw_underline_in_strip(scroller_width, strip_coords, curx, color): from bgl import glColor4f, glRectf, glEnable, glDisable, GL_BLEND context = bpy.context # Strip coords s_x1, s_y1, s_x2, s_y2 = strip_coords # Drawing coords x = 0 d_y1 = s_y1 d_y2 = s_y2 d_x1 = s_x1 d_x2 = s_x2 # be careful not to override the current frame line cf_x = context.scene.frame_current_final y = 0 r, g, b, a = color glColor4f(r, g, b, a) glEnable(GL_BLEND) # // this checks if the strip range overlaps the current f. label range # // then it would need a polygon? to draw around it # // TODO: check also if label display is ON # Check if the current frame label overlaps the strip # label_height = scroller_width * 2 # if d_y1 < label_height: # if cf_x < d_x2 and d_x1 < cf_x + label_height: # print("ALARM!!") if d_x1 < cf_x and cf_x < d_x2: # Bad luck, the line passes our strip glRectf(d_x1, d_y1, cf_x - curx, d_y2) glRectf(cf_x + curx, d_y1, d_x2, d_y2) else: # Normal, full rectangle draw glRectf(d_x1, d_y1, d_x2, d_y2) glDisable(GL_BLEND)
def aabox(v1: Float2, v2: Float2, rgba: Float4): """Draw an axis-aligned box.""" bgl.glColor4f(*rgba) bgl.glRectf(*v1, *v2)
def draw_callback(self, context): """ :type context: bpy.types.Context """ prefs = QuickBooleanPreferences.get_instance() color = prefs.color snap_color = prefs.snap_color region = context.region glsettings = vagl.GLSettings(context) glsettings.push() bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(*color) show_reversed = False if self.reverse: if self.mode == 'POLYGON': if len(self.mouse_coords) >= 3: show_reversed = True else: if len(self.mouse_coords) >= 2: if self.mouse_coords[0] != self.mouse_coords[1]: show_reversed = True if show_reversed: bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glClearDepth(1.0) bgl.glClear(bgl.GL_DEPTH_BUFFER_BIT) bgl.glDepthMask(1) bgl.glColorMask(0, 0, 0, 0) lines = [] if self.mouse_coords: if self.mode == 'LINE': w = region.width h = region.height p1, p2 = self.mouse_coords line = (p2 - p1).normalized() normal = Vector([-line[1], line[0]]) corners = [Vector([0, 0]), Vector([w, 0]), Vector([w, h]), Vector([0, h])] corners_ofs = [v - p1 for v in corners] dists = [v.project(line).dot(line) for v in corners_ofs] i = dists.index(min(dists)) line_min = corners_ofs[i].project(line) + p1 i = dists.index(max(dists)) line_max = corners_ofs[i].project(line) + p1 dists = [v.project(normal).dot(normal) for v in corners_ofs] i = dists.index(max(dists)) normal_max_f = corners_ofs[i].project(normal).dot(normal) vec = normal * normal_max_f coords = [line_min, line_max, line_max + vec, line_min + vec] bgl.glBegin(bgl.GL_QUADS) for co in coords: bgl.glVertex2f(*co) bgl.glEnd() lines = self.mouse_coords elif self.mode == 'BOX': p1, p2 = self.mouse_coords bgl.glRectf(p1[0], p1[1], p2[0], p2[1]) lines = [p1, Vector((p2[0], p1[1])), Vector((p2[0], p2[1])), Vector((p1[0], p2[1])), p1] elif self.mode == 'CIRCLE': p1, p2 = self.mouse_coords bgl.glBegin(bgl.GL_TRIANGLE_FAN) bgl.glVertex2f(*p1) r = (p2 - p1).length coords = calc_circle_coords(p1, r, self.circle_segments, self.circle_direction) for co in coords: bgl.glVertex2f(*co) bgl.glVertex2f(*coords[0]) bgl.glEnd() lines = coords + [coords[0]] elif self.mode == 'POLYGON': if len(self.mouse_coords) >= 3: tris = mathutils.geometry.tessellate_polygon( [[co.to_3d() for co in self.mouse_coords]]) bgl.glBegin(bgl.GL_TRIANGLES) for tri in tris: for i in tri: bgl.glVertex2f(*self.mouse_coords[i]) bgl.glEnd() if len(self.mouse_coords) > 1: lines = self.mouse_coords + [self.mouse_coords[0]] if show_reversed: bgl.glColorMask(1, 1, 1, 1) bgl.glBegin(bgl.GL_QUADS) bgl.glVertex3f(0, 0, -1) bgl.glVertex3f(region.width, 0, -1) bgl.glVertex3f(region.width, region.height, -1) bgl.glVertex3f(0, region.height, -1) bgl.glEnd() bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glColor4f(*color[:3], 1.0) bgl.glPointSize(1) bgl.glLineWidth(1) if len(lines) > 1: bgl.glBegin(bgl.GL_LINE_STRIP) for co in lines: bgl.glVertex2f(*co) bgl.glEnd() if self.mode == 'POLYGON': if len(self.mouse_coords) == 1: bgl.glPointSize(5) bgl.glBegin(bgl.GL_POINTS) for co in self.mouse_coords: bgl.glVertex2f(*co) bgl.glEnd() bgl.glPointSize(1) bgl.glLineWidth(1) if self.mco_ctrl: SIZE = 12 bgl.glColor4f(*snap_color) bgl.glBegin(bgl.GL_LINE_LOOP) v = self.mco_mod x = v[0] - SIZE / 2 y = v[1] - SIZE / 2 bgl.glVertex2f(x, y) bgl.glVertex2f(x + SIZE, y) bgl.glVertex2f(x + SIZE, y + SIZE) bgl.glVertex2f(x, y + SIZE) bgl.glEnd() glsettings.pop() glsettings.font_size()
def renderRect(color, x, y, width, height): bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(color[0], color[1], color[2], color[3]) bgl.glRectf(x, y, x + width, y + height)
def draw_callback(self, context): """ :type context: bpy.types.Context """ prefs = QuickBooleanPreferences.get_instance() color = prefs.color snap_color = prefs.snap_color region = context.region glsettings = vagl.GLSettings(context) glsettings.push() bgl.glEnable(bgl.GL_BLEND) bgl.glColor4f(*color) show_reversed = False if self.reverse: if self.mode == 'POLYGON': if len(self.mouse_coords) >= 3: show_reversed = True else: if len(self.mouse_coords) >= 2: if self.mouse_coords[0] != self.mouse_coords[1]: show_reversed = True if show_reversed: bgl.glEnable(bgl.GL_DEPTH_TEST) bgl.glClearDepth(1.0) bgl.glClear(bgl.GL_DEPTH_BUFFER_BIT) bgl.glDepthMask(1) bgl.glColorMask(0, 0, 0, 0) lines = [] if self.mouse_coords: if self.mode == 'LINE': w = region.width h = region.height p1, p2 = self.mouse_coords line = (p2 - p1).normalized() normal = Vector([-line[1], line[0]]) corners = [ Vector([0, 0]), Vector([w, 0]), Vector([w, h]), Vector([0, h]) ] corners_ofs = [v - p1 for v in corners] dists = [v.project(line).dot(line) for v in corners_ofs] i = dists.index(min(dists)) line_min = corners_ofs[i].project(line) + p1 i = dists.index(max(dists)) line_max = corners_ofs[i].project(line) + p1 dists = [v.project(normal).dot(normal) for v in corners_ofs] i = dists.index(max(dists)) normal_max_f = corners_ofs[i].project(normal).dot(normal) vec = normal * normal_max_f coords = [line_min, line_max, line_max + vec, line_min + vec] bgl.glBegin(bgl.GL_QUADS) for co in coords: bgl.glVertex2f(*co) bgl.glEnd() lines = self.mouse_coords elif self.mode == 'BOX': p1, p2 = self.mouse_coords bgl.glRectf(p1[0], p1[1], p2[0], p2[1]) lines = [ p1, Vector((p2[0], p1[1])), Vector((p2[0], p2[1])), Vector((p1[0], p2[1])), p1 ] elif self.mode == 'CIRCLE': p1, p2 = self.mouse_coords bgl.glBegin(bgl.GL_TRIANGLE_FAN) bgl.glVertex2f(*p1) r = (p2 - p1).length coords = calc_circle_coords(p1, r, self.circle_segments, self.circle_direction) for co in coords: bgl.glVertex2f(*co) bgl.glVertex2f(*coords[0]) bgl.glEnd() lines = coords + [coords[0]] elif self.mode == 'POLYGON': if len(self.mouse_coords) >= 3: tris = mathutils.geometry.tessellate_polygon( [[co.to_3d() for co in self.mouse_coords]]) bgl.glBegin(bgl.GL_TRIANGLES) for tri in tris: for i in tri: bgl.glVertex2f(*self.mouse_coords[i]) bgl.glEnd() if len(self.mouse_coords) > 1: lines = self.mouse_coords + [self.mouse_coords[0]] if show_reversed: bgl.glColorMask(1, 1, 1, 1) bgl.glBegin(bgl.GL_QUADS) bgl.glVertex3f(0, 0, -1) bgl.glVertex3f(region.width, 0, -1) bgl.glVertex3f(region.width, region.height, -1) bgl.glVertex3f(0, region.height, -1) bgl.glEnd() bgl.glDisable(bgl.GL_DEPTH_TEST) bgl.glColor4f(*color[:3], 1.0) bgl.glPointSize(1) bgl.glLineWidth(1) if len(lines) > 1: bgl.glBegin(bgl.GL_LINE_STRIP) for co in lines: bgl.glVertex2f(*co) bgl.glEnd() if self.mode == 'POLYGON': if len(self.mouse_coords) == 1: bgl.glPointSize(5) bgl.glBegin(bgl.GL_POINTS) for co in self.mouse_coords: bgl.glVertex2f(*co) bgl.glEnd() bgl.glPointSize(1) bgl.glLineWidth(1) if self.mco_ctrl: SIZE = 12 bgl.glColor4f(*snap_color) bgl.glBegin(bgl.GL_LINE_LOOP) v = self.mco_mod x = v[0] - SIZE / 2 y = v[1] - SIZE / 2 bgl.glVertex2f(x, y) bgl.glVertex2f(x + SIZE, y) bgl.glVertex2f(x + SIZE, y + SIZE) bgl.glVertex2f(x, y + SIZE) bgl.glEnd() glsettings.pop() glsettings.font_size()