def write(): """write on screen""" width = render.getWindowWidth() height = render.getWindowHeight() # OpenGL setup bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glLoadIdentity() bgl.gluOrtho2D(0, width, 0, height) #bgl.glColor(1,1,1,1) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glLoadIdentity() # BLF drawing routine font_id = logic.font_id RED = (1, 0, 0, 1) pcol = ("Blue ", RED) #blf.color(font_id, 1, 1, 0, 0.5) blf.blur(font_id, 500) blf.rotation(font_id, 90) blf.position(font_id, (width * 0.5), (height * 0.5), 0.5) blf.size(font_id, 20, 100) blf.draw(font_id, "Hello World1") blf.size(font_id, 50, 72) blf.position(font_id, (width * 0.0), (height * 0.5), 0.5) blf.draw(font_id, "Hello World2")
def draw_text(self, text, x, y, angle, font_height, colour): # dirty fast assignment dpi, font_id = 72, 0 bgl.glColor4f(*colour) blf.position(font_id, x, y, 0) blf.rotation(font_id, angle) blf.size(font_id, font_height, dpi) blf.draw(font_id, text)
def draw_callback_px(self, context, position, rotation): font_id = font_info["font_id"] blf.enable(font_id, blf.ROTATION) blf.rotation(font_id, math.radians(45)) blf.position(font_id, position[0] - 280, position[1] - 280, 0) # blf.position(font_id, 3, 3, 0) blf.size(font_id, 10, 300) blf.draw(font_id, "Hello World")
def text_size(self, context): """ overall on-screen size in pixels """ dpi, font_id = context.user_preferences.system.dpi, 0 if self.angle != 0: blf.enable(font_id, blf.ROTATION) blf.rotation(font_id, self.angle) blf.aspect(font_id, 1.0) blf.size(font_id, self.font_size, dpi) x, y = blf.dimensions(font_id, self.text) if self.angle != 0: blf.disable(font_id, blf.ROTATION) return Vector((x, y))
def draw(self, context, render=False): self.render = render x, y = self.position_2d_from_coord(context, self.pts[0], render) # dirty fast assignment dpi, font_id = context.user_preferences.system.dpi, 0 bgl.glColor4f(*self.colour) if self.angle != 0: blf.enable(font_id, blf.ROTATION) blf.rotation(font_id, self.angle) blf.size(font_id, self.font_size, dpi) blf.position(font_id, x, y, 0) blf.draw(font_id, self.text) if self.angle != 0: blf.disable(font_id, blf.ROTATION)
def draw(self, context, render=False): # print("draw_text %s %s" % (self.text, type(self).__name__)) self.render = render x, y = self.position_2d_from_coord(context, self.pts[0], render) # dirty fast assignment dpi, font_id = context.user_preferences.system.dpi, 0 bgl.glColor4f(*self.colour) if self.angle != 0: blf.enable(font_id, blf.ROTATION) blf.rotation(font_id, self.angle) blf.size(font_id, self.font_size, dpi) blf.position(font_id, x, y, 0) blf.draw(font_id, self.text) if self.angle != 0: blf.disable(font_id, blf.ROTATION)
def draw(self, context, render=False): # print("draw_text %s %s" % (self.text, type(self).__name__)) self.render = render p = self.position_2d_from_coord(context, self.pts[0], render) # dirty fast assignment dpi, font_id = context.user_preferences.system.dpi, 0 bgl.glColor4f(*self.colour) if self.angle != 0: blf.enable(font_id, blf.ROTATION) blf.rotation(font_id, self.angle) blf.size(font_id, self.font_size, dpi) blf.position(font_id, p.x, p.y, 0) blf.draw(font_id, self.text) if self.angle != 0: blf.disable(font_id, blf.ROTATION)
def __render_text_index( cls, context, region, index, uv, # uv vector to text position uvt=Vector([1.0, 0.0]), # tangent unit vector on uv uvn=Vector([0.0, 1.0]), # normal unit vector on uv loop_offset=(0.0, 0.0), # additional offset to loop text pos bg_color=None, ): text = str(index) ruvi_props = scene = context.scene.ruvi_properties additional_offset = loop_offset[ruvi_props.edges] # Calcurate position and angle v = Vector(region.view2d.view_to_region(uv.x, uv.y)) text_w, text_h = blf.dimensions(0, text) offset = (text_w * uvt + text_h * uvn) / 2 sub_offset = additional_offset * text_h * uvn vo = Vector([1, 0]) if uvt.y >= 0: angle = uvt.angle(vo) else: angle = -uvt.angle(vo) if uvt.x + uvt.y >= 0: v = v - offset + sub_offset else: v = v + offset + sub_offset angle -= pi blf.rotation(0, angle) # Render index if bg_color is not None: cls.__draw_background(bg_color, text, v, angle) font_size = ruvi_props.font_size cls.__render_text(font_size, v, text)
def scribble(text, x, y, px=12, pivot=(0.5, 0.0), rotation=0.0): blf.enable(0, blf.ROTATION) blf.size(0, px, 72) dimensions = blf.dimensions(0, text) sin = math.sin(rotation) cos = math.cos(rotation) offset = (dimensions[0] * pivot[0] * cos - dimensions[1] * pivot[1] * sin, dimensions[0] * pivot[0] * sin + dimensions[1] * pivot[1] * cos) position = (x - offset[0], y - offset[1]) blf.position(0, position[0], position[1], 0.0) blf.rotation(0, rotation) blf.draw(0, text) blf.disable(0, blf.ROTATION)
def draw(self, context, render=False): # print("draw_text %s %s" % (self.text, type(self).__name__)) self.render = render p = self.position_2d_from_coord(context, self.pts[0], render) # dirty fast assignment dpi, font_id = context.preferences.system.dpi, 0 # self.cbuff[0:4] = self.colour # bgl.glEnableClientState(bgl.GL_COLOR_ARRAY) # bgl.glColorPointer(4, bgl.GL_FLOAT, 0, self.cbuff) blf.color(0, *self.colour) if self.angle != 0: blf.enable(font_id, blf.ROTATION) blf.rotation(font_id, self.angle) blf.size(font_id, self.font_size, dpi) blf.position(font_id, p.x, p.y, 0) blf.draw(font_id, self.text) if self.angle != 0: blf.disable(font_id, blf.ROTATION)
def rotation(angle, fontid=None): return blf.rotation(FontManager.load(fontid), angle)
def setRot(self, angle): blf.rotation(self.font_info['font_id'], angle)
def display_instructions(region, rv3d, instruct, keys_aff, keys_nav, keys_neg): rw = region.width rh = region.height print('rw, rh', rw, rh) expand = False crop = False len_aff_max = rw - 140 len_aff = len(keys_aff) * 5 len_neg = len(keys_neg) * 5 len_ins = len(instruct) * 18 if len_aff > len_aff_max: expand = True rw_min = 480 rh_min = 280 if rw < rw_min or rh < rh_min: crop = True version = '020' font_id = 0 keys_aff_1 = copy.deepcopy(keys_aff) keys_aff_2 = ' ' if expand: keys_aff_1 = '' keys_aff_2 = '' print('len(keys_aff)', len(keys_aff)) stop = 0 for i in range (0, len(keys_aff)-1): #print(keys_aff[i]) if keys_aff[i] == ',' and i * 5 <= len_aff_max and i > stop: stop = i print ('stop', stop) for i in range(0, stop + 1): keys_aff_1 = keys_aff_1 + keys_aff[i] for i in range(stop + 2, len(keys_aff)): keys_aff_2 = keys_aff_2 + keys_aff[i] print(keys_aff_1) print(keys_aff_2) field_keys_y = 46 field_keys_x = 80 field_keys_aff_1 = [[field_keys_x, field_keys_y + 21], [field_keys_x, field_keys_y + 39], [rw - int(field_keys_x / 2), field_keys_y + 39], [rw - int(field_keys_x / 2), field_keys_y + 21]] field_keys_aff_2 = copy.deepcopy(field_keys_aff_1) field_keys_neg = [[field_keys_x, field_keys_y], [field_keys_x, field_keys_y + 18], [rw - field_keys_x / 2, field_keys_y + 18], [rw - field_keys_x / 2, field_keys_y]] if expand: field_keys_aff_2 = copy.deepcopy(field_keys_neg) field_keys_neg = [[field_keys_x, field_keys_y - 21], [field_keys_x, field_keys_y - 3], [rw - field_keys_x / 2, field_keys_y - 3], [rw - field_keys_x / 2, field_keys_y - 21]] size_font_np = 25 size_font_instruct = 21 size_font_keys = 11 len_np_ins = len_ins + int(size_font_np * 2.1) pos_font_np_x = (rw - (len_np_ins / 2)) / 2 pos_font_np_y = 150 if crop: pos_font_np_y = 75 pos_font_instruct_x = pos_font_np_x + int(size_font_np * 2.1) pos_font_instruct_y = pos_font_np_y + 4 pos_font_keys_aff_1_x = field_keys_x + 8 pos_font_keys_aff_1_y = field_keys_y + 26 pos_font_keys_aff_2_x = copy.deepcopy(pos_font_keys_aff_1_x) pos_font_keys_aff_2_y = copy.deepcopy(pos_font_keys_aff_1_y) pos_font_keys_nav_x = field_keys_x + 8 pos_font_keys_nav_y = field_keys_y + 5 pos_font_keys_neg_x = rw - 52 - len_neg print ('len_neg', len_neg) print ('pos_font_keys_neg_x', pos_font_keys_neg_x) pos_font_keys_neg_y = field_keys_y + 5 if expand: pos_font_keys_aff_2_x = field_keys_x + 8 pos_font_keys_aff_2_y = field_keys_y + 5 pos_font_keys_nav_x = field_keys_x + 8 pos_font_keys_nav_y = field_keys_y - 16 pos_font_keys_neg_x = rw - 52 - len_neg pos_font_keys_neg_y = field_keys_y - 16 col_font_np = addon_settings_graph()['col_font_np'] col_font_instruct_main = addon_settings_graph()['col_font_instruct_main'] col_font_instruct_shadow = addon_settings_graph()['col_font_instruct_shadow'] col_font_keys = addon_settings_graph()['col_font_keys'] col_field_keys_aff = addon_settings_graph()['col_field_keys_aff'] col_field_keys_neg = addon_settings_graph()['col_field_keys_neg'] # instructions - NP: bgl.glColor4f(*col_font_np) blf.size(font_id, size_font_np, 72) blf.position(font_id, pos_font_np_x, pos_font_np_y, 0) blf.draw(font_id, 'NP') blf.enable(font_id, ROTATION) ang = radians(90) blf.size(font_id, int(size_font_np / 2.2), 72) blf.rotation(font_id, ang) blf.position(font_id, pos_font_np_x + int(size_font_np * 1.72) , pos_font_np_y - 2, 0) blf.draw(font_id, version) blf.disable(font_id, ROTATION) # instructions - instruct: bgl.glColor4f(*col_font_instruct_shadow) blf.position(font_id, pos_font_instruct_x + 1, pos_font_instruct_y - 1, 0) blf.size(font_id, size_font_instruct, 72) blf.draw(font_id, instruct) bgl.glColor4f(*col_font_instruct_main) blf.position(font_id, pos_font_instruct_x, pos_font_instruct_y, 0) blf.size(font_id, size_font_instruct, 72) blf.draw(font_id, instruct) bgl.glDisable(bgl.GL_BLEND) # instructions - keys - backdrop fields: bgl.glEnable(bgl.GL_BLEND) if crop == False: bgl.glColor4f(*col_field_keys_aff) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_aff_1: bgl.glVertex2f(*co) bgl.glEnd() bgl.glColor4f(*col_field_keys_aff) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_aff_2: bgl.glVertex2f(*co) bgl.glEnd() bgl.glColor4f(*col_field_keys_neg) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_neg: bgl.glVertex2f(*co) bgl.glEnd() # instructions - keys - writing: bgl.glColor4f(*col_font_keys) blf.size(font_id, size_font_keys, 72) blf.position(font_id, pos_font_keys_aff_1_x, pos_font_keys_aff_1_y, 0) blf.draw(font_id, keys_aff_1) blf.position(font_id, pos_font_keys_aff_2_x, pos_font_keys_aff_2_y, 0) blf.draw(font_id, keys_aff_2) blf.position(font_id, pos_font_keys_nav_x, pos_font_keys_nav_y, 0) blf.draw(font_id, keys_nav) blf.position(font_id, pos_font_keys_neg_x, pos_font_keys_neg_y, 0) blf.draw(font_id, keys_neg)
def display_instructions(region, rv3d, instruct, keys_aff, keys_nav, keys_neg): userpref = bpy.context.user_preferences system = userpref.system rwtools = 0 rwui = 0 np_print(system.window_draw_method, system.use_region_overlap) if system.use_region_overlap: if system.window_draw_method in ('TRIPLE_BUFFER', 'AUTOMATIC') : area = bpy.context.area np_print('GO', area.regions) for r in area.regions: if r.type == 'TOOLS': rwtools = r.width elif r.type == 'UI': rwui = r.width np_print('rwtools', rwtools, 'rwui', rwui) field_keys_y = 46 field_keys_x = 80 rw = region.width rh = region.height np_print('rw, rh', rw, rh) expand = False crop = False len_aff_max = rw - 140 - rwtools - rwui len_aff = len(keys_aff) * 5 len_neg = len(keys_neg) * 5 len_ins = len(instruct) * 18 if len_aff > len_aff_max: expand = True rw_min = 480 rh_min = 280 if rw - rwtools - rwui < rw_min or rh < rh_min: crop = True version = '020' font_id = 0 keys_aff_1 = copy.deepcopy(keys_aff) keys_aff_2 = ' ' if expand: keys_aff_1 = '' keys_aff_2 = '' np_print('len(keys_aff)', len(keys_aff)) stop = 0 for i in range (0, len(keys_aff)-1): #np_print(keys_aff[i]) if keys_aff[i] == ',' and i * 5 <= len_aff_max and i > stop: stop = i np_print('stop', stop) for i in range(0, stop + 1): keys_aff_1 = keys_aff_1 + keys_aff[i] for i in range(stop + 2, len(keys_aff)): keys_aff_2 = keys_aff_2 + keys_aff[i] np_print(keys_aff_1) np_print(keys_aff_2) field_keys_aff_1 = [[field_keys_x + rwtools, field_keys_y + 21], [field_keys_x + rwtools, field_keys_y + 39], [rw - int(field_keys_x / 2) - rwui, field_keys_y + 39], [rw - int(field_keys_x / 2) - rwui, field_keys_y + 21]] field_keys_aff_2 = copy.deepcopy(field_keys_aff_1) field_keys_neg = [[field_keys_x + rwtools, field_keys_y], [field_keys_x + rwtools, field_keys_y + 18], [rw - int(field_keys_x / 2) - rwui, field_keys_y + 18], [rw - int(field_keys_x / 2) - rwui, field_keys_y]] if expand: field_keys_aff_2 = copy.deepcopy(field_keys_neg) field_keys_neg = [[field_keys_x + rwtools, field_keys_y - 21], [field_keys_x + rwtools, field_keys_y - 3], [rw - int(field_keys_x / 2) - rwui, field_keys_y - 3], [rw - int(field_keys_x / 2) - rwui, field_keys_y - 21]] size_font_np = 25 size_font_instruct = 21 size_font_keys = 11 len_np_ins = len_ins + int(size_font_np * 2.1) pos_font_np_x = (rw - len_np_ins / 2) / 2 + rwtools / 2 - rwui / 2 pos_font_np_y = 150 if crop: pos_font_np_y = 75 pos_font_instruct_x = pos_font_np_x + int(size_font_np * 2.1) pos_font_instruct_y = pos_font_np_y + 4 pos_font_keys_aff_1_x = field_keys_x + 8 + rwtools pos_font_keys_aff_1_y = field_keys_y + 26 pos_font_keys_aff_2_x = copy.deepcopy(pos_font_keys_aff_1_x) pos_font_keys_aff_2_y = copy.deepcopy(pos_font_keys_aff_1_y) pos_font_keys_nav_x = field_keys_x + 8 + rwtools pos_font_keys_nav_y = field_keys_y + 5 pos_font_keys_neg_x = rw - 52 - len_neg - rwui np_print('len_neg', len_neg) np_print('pos_font_keys_neg_x', pos_font_keys_neg_x) pos_font_keys_neg_y = field_keys_y + 5 if expand: pos_font_keys_aff_2_x = field_keys_x + 8 + rwtools pos_font_keys_aff_2_y = field_keys_y + 5 pos_font_keys_nav_x = field_keys_x + 8 + rwtools pos_font_keys_nav_y = field_keys_y - 16 pos_font_keys_neg_x = rw - 52 - len_neg - rwui pos_font_keys_neg_y = field_keys_y - 16 - rwui col_font_np = addon_settings_graph()['col_font_np'] col_font_instruct_main = addon_settings_graph()['col_font_instruct_main'] col_font_instruct_shadow = addon_settings_graph()['col_font_instruct_shadow'] col_font_keys = addon_settings_graph()['col_font_keys'] col_field_keys_aff = addon_settings_graph()['col_field_keys_aff'] col_field_keys_neg = addon_settings_graph()['col_field_keys_neg'] # instructions - NP: bgl.glColor4f(*col_font_np) blf.size(font_id, size_font_np, 72) blf.position(font_id, pos_font_np_x, pos_font_np_y, 0) blf.draw(font_id, 'NP') blf.enable(font_id, ROTATION) ang = radians(90) blf.size(font_id, int(size_font_np / 2.2), 72) blf.rotation(font_id, ang) blf.position(font_id, pos_font_np_x + int(size_font_np * 1.72) , pos_font_np_y - 2, 0) blf.draw(font_id, version) blf.disable(font_id, ROTATION) # instructions - instruct: bgl.glColor4f(*col_font_instruct_shadow) blf.position(font_id, pos_font_instruct_x + 1, pos_font_instruct_y - 1, 0) blf.size(font_id, size_font_instruct, 72) blf.draw(font_id, instruct) bgl.glColor4f(*col_font_instruct_main) blf.position(font_id, pos_font_instruct_x, pos_font_instruct_y, 0) blf.size(font_id, size_font_instruct, 72) blf.draw(font_id, instruct) bgl.glDisable(bgl.GL_BLEND) # instructions - keys - backdrop fields: bgl.glEnable(bgl.GL_BLEND) if crop == False: bgl.glColor4f(*col_field_keys_aff) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_aff_1: bgl.glVertex2f(*co) bgl.glEnd() bgl.glColor4f(*col_field_keys_aff) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_aff_2: bgl.glVertex2f(*co) bgl.glEnd() bgl.glColor4f(*col_field_keys_neg) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_neg: bgl.glVertex2f(*co) bgl.glEnd() # instructions - keys - writing: bgl.glColor4f(*col_font_keys) blf.size(font_id, size_font_keys, 72) blf.position(font_id, pos_font_keys_aff_1_x, pos_font_keys_aff_1_y, 0) blf.draw(font_id, keys_aff_1) blf.position(font_id, pos_font_keys_aff_2_x, pos_font_keys_aff_2_y, 0) blf.draw(font_id, keys_aff_2) blf.position(font_id, pos_font_keys_nav_x, pos_font_keys_nav_y, 0) blf.draw(font_id, keys_nav) blf.position(font_id, pos_font_keys_neg_x, pos_font_keys_neg_y, 0) blf.draw(font_id, keys_neg)
def draw_callback_px(self, context): """Draws Code Editors Minimap and indentation marks""" def draw_line(origin, length, thickness, vertical=False): """Drawing lines with polys, its faster""" x = (origin[0] + thickness) if vertical else (origin[0] + length) y = (origin[1] + length) if vertical else (origin[1] + thickness) bgl.glBegin(bgl.GL_QUADS) for v1, v2 in [origin, (x, origin[1]), (x, y), (origin[0], y)]: bgl.glVertex2i(v1, v2) bgl.glEnd() return # abort if another text editor if self.area == context.area and self.window == context.window: bgl.glEnable(bgl.GL_BLEND) else: return start = time.clock() # init params font_id = 0 self.width = next(region.width for region in context.area.regions if region.type == "WINDOW") self.height = next(region.height for region in context.area.regions if region.type == "WINDOW") dpi_r = context.user_preferences.system.dpi / 72.0 self.left_edge = self.width - round(dpi_r * (self.width + 5 * self.minimap_width) / 10.0) self.right_edge = self.width - round(dpi_r * 15) self.opacity = min(max(0, (self.width - self.min_width) / 100.0), 1) # compute character dimensions mcw = dpi_r * self.minimap_symbol_width # minimap char width mlh = round(dpi_r * self.minimap_line_height) # minimap line height fs = context.space_data.font_size cw = round(dpi_r * round(2 + 0.6 * (fs - 4))) # char width ch = round(dpi_r * round(2 + 1.3 * (fs - 2) + ((fs % 10) == 0))) # char height # panel background box self.tab_width = round(dpi_r * 25) if (self.tabs and len(bpy.data.texts) > 1) else 0 bgl.glColor4f(self.background.r, self.background.g, self.background.b, (1 - self.bg_opacity) * self.opacity) bgl.glBegin(bgl.GL_QUADS) for x, y in [ (self.left_edge - self.tab_width, self.height), (self.right_edge, self.height), (self.right_edge, 0), (self.left_edge - self.tab_width, 0), ]: bgl.glVertex2i(x, y) bgl.glEnd() # line numbers background space = context.space_data if space.text: lines = len(space.text.lines) lines_digits = len(str(lines)) if space.show_line_numbers else 0 self.line_bar_width = int(dpi_r * 5) + cw * (lines_digits) bgl.glColor4f(self.background.r, self.background.g, self.background.b, 1) bgl.glBegin(bgl.GL_QUADS) for x, y in [(0, self.height), (self.line_bar_width, self.height), (self.line_bar_width, 0), (0, 0)]: bgl.glVertex2i(x, y) bgl.glEnd() # shadow bgl.glLineWidth(1.0 * dpi_r) for id, intensity in enumerate([0.2, 0.1, 0.07, 0.05, 0.03, 0.02, 0.01]): bgl.glColor4f(0.0, 0.0, 0.0, intensity) bgl.glBegin(bgl.GL_LINE_STRIP) for x, y in [(self.line_bar_width + id, 0), (self.line_bar_width + id, self.height)]: bgl.glVertex2i(x, y) bgl.glEnd() # minimap shadow for id, intensity in enumerate([0.2, 0.1, 0.07, 0.05, 0.03, 0.02, 0.01]): bgl.glColor4f(0.0, 0.0, 0.0, intensity * self.opacity) bgl.glBegin(bgl.GL_LINE_STRIP) for x, y in [(self.left_edge - id - self.tab_width, 0), (self.left_edge - id - self.tab_width, self.height)]: bgl.glVertex2i(x, y) bgl.glEnd() # divider if self.tab_width: bgl.glColor4f(0.0, 0.0, 0.0, 0.2 * self.opacity) bgl.glBegin(bgl.GL_LINE_STRIP) for x, y in [(self.left_edge, 0), (self.left_edge, self.height)]: bgl.glVertex2i(x, y) bgl.glEnd() # if there is text in window if space.text and self.opacity: # minimap horizontal sliding based on text block length max_slide = max(0, mlh * (lines + self.height / ch) - self.height) self.slide = int(max_slide * space.top / lines) minimap_top_line = int(self.slide / mlh) minimap_bot_line = int((self.height + self.slide) / mlh) # draw minimap visible box if self.in_minimap: bgl.glColor4f(1.0, 1.0, 1.0, 0.1 * self.opacity) else: bgl.glColor4f(1.0, 1.0, 1.0, 0.07 * self.opacity) bgl.glBegin(bgl.GL_QUADS) for x, y in [ (self.left_edge, self.height - mlh * space.top + self.slide), (self.right_edge, self.height - mlh * space.top + self.slide), (self.right_edge, self.height - mlh * (space.top + space.visible_lines) + self.slide), (self.left_edge, self.height - mlh * (space.top + space.visible_lines) + self.slide), ]: bgl.glVertex2i(x, y) bgl.glEnd() # draw minimap code for segment in self.segments[:-1]: bgl.glColor4f(segment["col"][0], segment["col"][1], segment["col"][2], 0.4 * self.opacity) for id, element in enumerate(segment["elements"][minimap_top_line:minimap_bot_line]): loc_y = mlh * (id + minimap_top_line + 3) - self.slide for sub_element in element: draw_line( (self.left_edge + int(mcw * (sub_element[0] + 4)), self.height - loc_y), int(mcw * (sub_element[1] - sub_element[0])), int(0.5 * mlh), ) # minimap code marks bgl.glColor4f( self.segments[-2]["col"][0], self.segments[-2]["col"][1], self.segments[-2]["col"][2], 0.3 * self.block_trans * self.opacity, ) for id, element in enumerate(self.segments[-2]["elements"]): for sub_element in element: if sub_element[2] >= space.top or id < space.top + space.visible_lines: draw_line( (self.left_edge + int(mcw * (sub_element[0] + 4)), self.height - mlh * (id + 3) + self.slide), -int(mlh * (sub_element[2] - id - 1)), int(0.5 * mlh), True, ) # draw dotted indentation marks bgl.glLineWidth(1.0 * dpi_r) if space.text: bgl.glColor4f( self.segments[0]["col"][0], self.segments[0]["col"][1], self.segments[0]["col"][2], self.indent_trans ) for id, element in enumerate(self.segments[-1]["elements"][space.top : space.top + space.visible_lines]): loc_y = id for sub_element in element: draw_line( (int(dpi_r * 10) + cw * (lines_digits + sub_element[0] + 4), self.height - ch * (loc_y)), -ch, int(1 * dpi_r), True, ) # draw code block marks bgl.glColor4f( self.segments[-2]["col"][0], self.segments[-2]["col"][1], self.segments[-2]["col"][2], self.block_trans ) for id, element in enumerate(self.segments[-2]["elements"]): for sub_element in element: if sub_element[2] >= space.top or id < space.top + space.visible_lines: bgl.glBegin(bgl.GL_LINE_STRIP) bgl.glVertex2i( int(dpi_r * 10 + cw * (lines_digits + sub_element[0])), self.height - ch * (id + 1 - space.top) ) bgl.glVertex2i( int(dpi_r * 10 + cw * (lines_digits + sub_element[0])), self.height - int(ch * (sub_element[2] - space.top)), ) bgl.glVertex2i( int(dpi_r * 10 + cw * (lines_digits + sub_element[0] + 1)), self.height - int(ch * (sub_element[2] - space.top)), ) bgl.glEnd() # tab dividers if self.tab_width and self.opacity: self.tab_height = min(200, int(self.height / len(bpy.data.texts))) y_loc = self.height - 5 for text in bpy.data.texts: # tab selection if text.name == self.in_tab: bgl.glColor4f(1.0, 1.0, 1.0, 0.05 * self.opacity) bgl.glBegin(bgl.GL_QUADS) for x, y in [ (self.left_edge - self.tab_width, y_loc), (self.left_edge, y_loc), (self.left_edge, y_loc - self.tab_height), (self.left_edge - self.tab_width, y_loc - self.tab_height), ]: bgl.glVertex2i(x, y) bgl.glEnd() # tab active if context.space_data.text and text.name == context.space_data.text.name: bgl.glColor4f(1.0, 1.0, 1.0, 0.05 * self.opacity) bgl.glBegin(bgl.GL_QUADS) for x, y in [ (self.left_edge - self.tab_width, y_loc), (self.left_edge, y_loc), (self.left_edge, y_loc - self.tab_height), (self.left_edge - self.tab_width, y_loc - self.tab_height), ]: bgl.glVertex2i(x, y) bgl.glEnd() bgl.glColor4f(0.0, 0.0, 0.0, 0.2 * self.opacity) y_loc -= self.tab_height bgl.glBegin(bgl.GL_LINE_STRIP) for x, y in [(self.left_edge - self.tab_width, y_loc), (self.left_edge, y_loc)]: bgl.glVertex2i(x, y) bgl.glEnd() # draw fps # bgl.glColor4f(1, 1, 1, 0.2) # blf.size(font_id, fs, int(dpi_r*72)) # blf.position(font_id, self.left_edge-50, 5, 0) # blf.draw(font_id, str(round(1/(time.clock() - start),3))) # draw line numbers if space.text: bgl.glColor4f(self.segments[0]["col"][0], self.segments[0]["col"][1], self.segments[0]["col"][2], 0.5) for id in range(space.top, min(space.top + space.visible_lines + 1, lines + 1)): if self.in_line_bar and self.segments[-2]["elements"][id - 1]: bgl.glColor4f(self.segments[-2]["col"][0], self.segments[-2]["col"][1], self.segments[-2]["col"][2], 1) blf.position( font_id, 2 + int(0.5 * cw * (len(str(lines)) - 1)), self.height - ch * (id - space.top) + 3, 0 ) # blf.draw(font_id, '→') blf.draw(font_id, "↓") bgl.glColor4f(self.segments[0]["col"][0], self.segments[0]["col"][1], self.segments[0]["col"][2], 0.5) else: blf.position( font_id, 2 + int(0.5 * cw * (len(str(lines)) - len(str(id)))), self.height - ch * (id - space.top) + 3, 0, ) blf.draw(font_id, str(id)) # draw file names if self.tab_width: blf.enable(font_id, blf.ROTATION) blf.rotation(font_id, 1.570796) y_loc = self.height for text in bpy.data.texts: text_max_length = max(2, int((self.tab_height - 40) / cw)) name = text.name[:text_max_length] if text_max_length < len(text.name): name += "..." bgl.glColor4f( self.segments[0]["col"][0], self.segments[0]["col"][1], self.segments[0]["col"][2], (0.7 if text.name == self.in_tab else 0.4) * self.opacity, ) blf.position( font_id, self.left_edge - round((self.tab_width - ch) / 2.0) - 5, round(y_loc - (self.tab_height / 2) - cw * len(name) / 2), 0, ) blf.draw(font_id, name) y_loc -= self.tab_height # restore opengl defaults bgl.glColor4f(0, 0, 0, 1) bgl.glLineWidth(1.0) bgl.glDisable(bgl.GL_BLEND) blf.disable(font_id, blf.ROTATION) return
def draw(self): if self.visible == False: return module.post_draw_step += 1 cam = self.scene.active_camera orth = cam.ortho_scale height = render.getWindowHeight() width = render.getWindowWidth() near = cam.near far = cam.far h = cam.worldPosition.z font_id = Label._fontname_id[self._font] unit = width/orth self._glunit = unit rpos = self._position - cam.worldPosition bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glLoadIdentity() bgl.gluOrtho2D(0, width, 0, height) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glLoadIdentity() bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glAlphaFunc(bgl.GL_SRC_ALPHA, 1) #Z AXIS oh = (far-near)/2 ortho_unit = 1/oh dh = oh pos = list([width/2+rpos[0]*unit, height/2+rpos[1]*unit, dh*ortho_unit + rpos[2]*ortho_unit]) if self._lastscale != self.scale or True: blf.size(font_id, int(self.scale.x*unit), 72) else: if self._lastorth != orth: sc = (float(self._lastorth) / float(orth)) * self.scale.x bgl.glScalef(sc,sc,1) print(str(self._lastorth) + " " + str(orth)) pos[0] /= sc pos[1] /= sc else: self._lastorth = orth x, y = blf.dimensions(font_id, self._text) #NOTE: Always after blf.size() if self.align == ALIGN_CENTER: pos[0] -= (x)/2 * math.cos(self._rotation.z) pos[1] -= x/2 * math.sin(self._rotation.z) if self.align == ALIGN_RIGHT: pos[0] -= x * math.cos(self._rotation.z) pos[1] -= x * math.sin(self._rotation.z) if self.middle_height == True: pos[0] -= y/4 * math.sin(self._rotation.z) pos[1] -= y/4 * math.cos(self._rotation.z) blf.position(font_id, pos[0], pos[1], pos[2]) blf.enable(font_id, blf.ROTATION) if self.rotation.z > 0.01 or self.rotation.z < -0.01: blf.rotation(font_id, self._rotation.z) else: blf.rotation(font_id, 0) if self.shadow == True: blf.position(font_id, pos[0]+self.shadow_offset[0], pos[1]+self.shadow_offset[1], pos[2]) bgl.glColor4f(*self.shadow_color) blf.blur(font_id, self.shadow_blur) blf.draw(font_id, self._text) blf.position(font_id, pos[0], pos[1], pos[2]) bgl.glColor4f(*self._color) blf.blur(font_id, self.blur) blf.draw(font_id, self._text) blf.disable(font_id, blf.ROTATION) self._lastscale = self.scale
def draw(self): if self.visible == False: return module.post_draw_step += 1 cam = self.scene.active_camera orth = cam.ortho_scale height = render.getWindowHeight() width = render.getWindowWidth() near = cam.near far = cam.far h = cam.worldPosition.z font_id = Label._fontname_id[self._font] unit = width / orth self._glunit = unit rpos = self._position - cam.worldPosition bgl.glMatrixMode(bgl.GL_PROJECTION) bgl.glLoadIdentity() bgl.gluOrtho2D(0, width, 0, height) bgl.glMatrixMode(bgl.GL_MODELVIEW) bgl.glLoadIdentity() bgl.glEnable(bgl.GL_BLEND) bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA) bgl.glAlphaFunc(bgl.GL_SRC_ALPHA, 1) #Z AXIS oh = (far - near) / 2 ortho_unit = 1 / oh dh = oh pos = list([ width / 2 + rpos[0] * unit, height / 2 + rpos[1] * unit, dh * ortho_unit + rpos[2] * ortho_unit ]) if self._lastscale != self.scale or True: blf.size(font_id, int(self.scale.x * unit), 72) else: if self._lastorth != orth: sc = (float(self._lastorth) / float(orth)) * self.scale.x bgl.glScalef(sc, sc, 1) print(str(self._lastorth) + " " + str(orth)) pos[0] /= sc pos[1] /= sc else: self._lastorth = orth x, y = blf.dimensions(font_id, self._text) #NOTE: Always after blf.size() if self.align == ALIGN_CENTER: pos[0] -= (x) / 2 * math.cos(self._rotation.z) pos[1] -= x / 2 * math.sin(self._rotation.z) if self.align == ALIGN_RIGHT: pos[0] -= x * math.cos(self._rotation.z) pos[1] -= x * math.sin(self._rotation.z) if self.middle_height == True: pos[0] -= y / 4 * math.sin(self._rotation.z) pos[1] -= y / 4 * math.cos(self._rotation.z) blf.position(font_id, pos[0], pos[1], pos[2]) blf.enable(font_id, blf.ROTATION) if self.rotation.z > 0.01 or self.rotation.z < -0.01: blf.rotation(font_id, self._rotation.z) else: blf.rotation(font_id, 0) if self.shadow == True: blf.position(font_id, pos[0] + self.shadow_offset[0], pos[1] + self.shadow_offset[1], pos[2]) bgl.glColor4f(*self.shadow_color) blf.blur(font_id, self.shadow_blur) blf.draw(font_id, self._text) blf.position(font_id, pos[0], pos[1], pos[2]) bgl.glColor4f(*self._color) blf.blur(font_id, self.blur) blf.draw(font_id, self._text) blf.disable(font_id, blf.ROTATION) self._lastscale = self.scale
def display_instructions(region, rv3d, instruct, keys_aff, keys_nav, keys_neg): rw = region.width rh = region.height print('rw, rh', rw, rh) expand = False crop = False len_aff_max = rw - 140 len_aff = len(keys_aff) * 5 len_neg = len(keys_neg) * 5 len_ins = len(instruct) * 18 if len_aff > len_aff_max: expand = True rw_min = 480 rh_min = 280 if rw < rw_min or rh < rh_min: crop = True version = '020' font_id = 0 keys_aff_1 = copy.deepcopy(keys_aff) keys_aff_2 = ' ' if expand: keys_aff_1 = '' keys_aff_2 = '' print('len(keys_aff)', len(keys_aff)) stop = 0 for i in range(0, len(keys_aff) - 1): #print(keys_aff[i]) if keys_aff[i] == ',' and i * 5 <= len_aff_max and i > stop: stop = i print('stop', stop) for i in range(0, stop + 1): keys_aff_1 = keys_aff_1 + keys_aff[i] for i in range(stop + 2, len(keys_aff)): keys_aff_2 = keys_aff_2 + keys_aff[i] print(keys_aff_1) print(keys_aff_2) field_keys_y = 46 field_keys_x = 80 field_keys_aff_1 = [[field_keys_x, field_keys_y + 21], [field_keys_x, field_keys_y + 39], [rw - int(field_keys_x / 2), field_keys_y + 39], [rw - int(field_keys_x / 2), field_keys_y + 21]] field_keys_aff_2 = copy.deepcopy(field_keys_aff_1) field_keys_neg = [[field_keys_x, field_keys_y], [field_keys_x, field_keys_y + 18], [rw - field_keys_x / 2, field_keys_y + 18], [rw - field_keys_x / 2, field_keys_y]] if expand: field_keys_aff_2 = copy.deepcopy(field_keys_neg) field_keys_neg = [[field_keys_x, field_keys_y - 21], [field_keys_x, field_keys_y - 3], [rw - field_keys_x / 2, field_keys_y - 3], [rw - field_keys_x / 2, field_keys_y - 21]] size_font_np = 25 size_font_instruct = 21 size_font_keys = 11 len_np_ins = len_ins + int(size_font_np * 2.1) pos_font_np_x = (rw - (len_np_ins / 2)) / 2 pos_font_np_y = 150 if crop: pos_font_np_y = 75 pos_font_instruct_x = pos_font_np_x + int(size_font_np * 2.1) pos_font_instruct_y = pos_font_np_y + 4 pos_font_keys_aff_1_x = field_keys_x + 8 pos_font_keys_aff_1_y = field_keys_y + 26 pos_font_keys_aff_2_x = copy.deepcopy(pos_font_keys_aff_1_x) pos_font_keys_aff_2_y = copy.deepcopy(pos_font_keys_aff_1_y) pos_font_keys_nav_x = field_keys_x + 8 pos_font_keys_nav_y = field_keys_y + 5 pos_font_keys_neg_x = rw - 52 - len_neg print('len_neg', len_neg) print('pos_font_keys_neg_x', pos_font_keys_neg_x) pos_font_keys_neg_y = field_keys_y + 5 if expand: pos_font_keys_aff_2_x = field_keys_x + 8 pos_font_keys_aff_2_y = field_keys_y + 5 pos_font_keys_nav_x = field_keys_x + 8 pos_font_keys_nav_y = field_keys_y - 16 pos_font_keys_neg_x = rw - 52 - len_neg pos_font_keys_neg_y = field_keys_y - 16 col_font_np = addon_settings_graph()['col_font_np'] col_font_instruct_main = addon_settings_graph()['col_font_instruct_main'] col_font_instruct_shadow = addon_settings_graph( )['col_font_instruct_shadow'] col_font_keys = addon_settings_graph()['col_font_keys'] col_field_keys_aff = addon_settings_graph()['col_field_keys_aff'] col_field_keys_neg = addon_settings_graph()['col_field_keys_neg'] # instructions - NP: bgl.glColor4f(*col_font_np) blf.size(font_id, size_font_np, 72) blf.position(font_id, pos_font_np_x, pos_font_np_y, 0) blf.draw(font_id, 'NP') blf.enable(font_id, ROTATION) ang = radians(90) blf.size(font_id, int(size_font_np / 2.2), 72) blf.rotation(font_id, ang) blf.position(font_id, pos_font_np_x + int(size_font_np * 1.72), pos_font_np_y - 2, 0) blf.draw(font_id, version) blf.disable(font_id, ROTATION) # instructions - instruct: bgl.glColor4f(*col_font_instruct_shadow) blf.position(font_id, pos_font_instruct_x + 1, pos_font_instruct_y - 1, 0) blf.size(font_id, size_font_instruct, 72) blf.draw(font_id, instruct) bgl.glColor4f(*col_font_instruct_main) blf.position(font_id, pos_font_instruct_x, pos_font_instruct_y, 0) blf.size(font_id, size_font_instruct, 72) blf.draw(font_id, instruct) bgl.glDisable(bgl.GL_BLEND) # instructions - keys - backdrop fields: bgl.glEnable(bgl.GL_BLEND) if crop == False: bgl.glColor4f(*col_field_keys_aff) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_aff_1: bgl.glVertex2f(*co) bgl.glEnd() bgl.glColor4f(*col_field_keys_aff) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_aff_2: bgl.glVertex2f(*co) bgl.glEnd() bgl.glColor4f(*col_field_keys_neg) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_neg: bgl.glVertex2f(*co) bgl.glEnd() # instructions - keys - writing: bgl.glColor4f(*col_font_keys) blf.size(font_id, size_font_keys, 72) blf.position(font_id, pos_font_keys_aff_1_x, pos_font_keys_aff_1_y, 0) blf.draw(font_id, keys_aff_1) blf.position(font_id, pos_font_keys_aff_2_x, pos_font_keys_aff_2_y, 0) blf.draw(font_id, keys_aff_2) blf.position(font_id, pos_font_keys_nav_x, pos_font_keys_nav_y, 0) blf.draw(font_id, keys_nav) blf.position(font_id, pos_font_keys_neg_x, pos_font_keys_neg_y, 0) blf.draw(font_id, keys_neg)
def draw_label(self, context, text, pos, dir, gap=4, center=True, vcenter=False): """Draw text label Args: pos: bottom-center aligned and centered at segment middle """ # 0 is the default font, but we're fancier than that font_id = self.font_id dpi = context.preferences.system.dpi color = context.scene.DocProperties.decorations_colour ang = -Vector((1, 0)).angle_signed(dir) cos = math.cos(ang) sin = math.sin(ang) # Horrific prototype code factor = self.camera_zoom_to_factor( context.space_data.region_3d.view_camera_zoom) camera_width_px = factor * context.region.width mm_to_px = camera_width_px / self.camera_width_mm # 0.004118616 is a magic constant number I visually discovered to get the right number. # In particular it works only for the OpenGOST font and produces a 2.5mm font size. # It probably should be dynamically calculated using system.dpi or something. # font_size = 16 <-- this is a good default font_size = int(0.004118616 * mm_to_px) blf.size(font_id, font_size, dpi) w, h = 0, 0 if center or vcenter: w, h = blf.dimensions(font_id, text) if center: # horizontal centering pos -= Vector((cos, sin)) * w * 0.5 if vcenter: # vertical centering pos -= Vector((-sin, cos)) * h * 0.5 if gap: # side-shifting pos += Vector((-sin, cos)) * gap blf.enable(font_id, blf.ROTATION) blf.position(font_id, pos.x, pos.y, 0) blf.rotation(font_id, ang) blf.color(font_id, *color) # blf.enable(font_id, blf.SHADOW) # blf.shadow(font_id, 5, 0, 0, 0, 1) # blf.shadow_offset(font_id, 1, -1) blf.draw(font_id, text) blf.disable(font_id, blf.ROTATION)
def display_instructions(region, rv3d, instruct, keys_aff, keys_nav, keys_neg): userpref = bpy.context.user_preferences system = userpref.system rwtools = 0 rwui = 0 np_print(system.window_draw_method, system.use_region_overlap) if system.use_region_overlap: if system.window_draw_method in ('TRIPLE_BUFFER', 'AUTOMATIC'): area = bpy.context.area np_print('GO', area.regions) for r in area.regions: if r.type == 'TOOLS': rwtools = r.width elif r.type == 'UI': rwui = r.width np_print('rwtools', rwtools, 'rwui', rwui) field_keys_y = 46 field_keys_x = 80 rw = region.width rh = region.height np_print('rw, rh', rw, rh) expand = False crop = False len_aff_max = rw - 140 - rwtools - rwui len_aff = len(keys_aff) * 5 len_neg = len(keys_neg) * 5 len_ins = len(instruct) * 18 if len_aff > len_aff_max: expand = True rw_min = 480 rh_min = 280 if rw - rwtools - rwui < rw_min or rh < rh_min: crop = True version = '020' font_id = 0 keys_aff_1 = copy.deepcopy(keys_aff) keys_aff_2 = ' ' if expand: keys_aff_1 = '' keys_aff_2 = '' np_print('len(keys_aff)', len(keys_aff)) stop = 0 for i in range(0, len(keys_aff) - 1): #np_print(keys_aff[i]) if keys_aff[i] == ',' and i * 5 <= len_aff_max and i > stop: stop = i np_print('stop', stop) for i in range(0, stop + 1): keys_aff_1 = keys_aff_1 + keys_aff[i] for i in range(stop + 2, len(keys_aff)): keys_aff_2 = keys_aff_2 + keys_aff[i] np_print(keys_aff_1) np_print(keys_aff_2) field_keys_aff_1 = [[field_keys_x + rwtools, field_keys_y + 21], [field_keys_x + rwtools, field_keys_y + 39], [rw - int(field_keys_x / 2) - rwui, field_keys_y + 39], [rw - int(field_keys_x / 2) - rwui, field_keys_y + 21]] field_keys_aff_2 = copy.deepcopy(field_keys_aff_1) field_keys_neg = [[field_keys_x + rwtools, field_keys_y], [field_keys_x + rwtools, field_keys_y + 18], [rw - int(field_keys_x / 2) - rwui, field_keys_y + 18], [rw - int(field_keys_x / 2) - rwui, field_keys_y]] if expand: field_keys_aff_2 = copy.deepcopy(field_keys_neg) field_keys_neg = [ [field_keys_x + rwtools, field_keys_y - 21], [field_keys_x + rwtools, field_keys_y - 3], [rw - int(field_keys_x / 2) - rwui, field_keys_y - 3], [rw - int(field_keys_x / 2) - rwui, field_keys_y - 21] ] size_font_np = 25 size_font_instruct = 21 size_font_keys = 11 len_np_ins = len_ins + int(size_font_np * 2.1) pos_font_np_x = (rw - len_np_ins / 2) / 2 + rwtools / 2 - rwui / 2 pos_font_np_y = 150 if crop: pos_font_np_y = 75 pos_font_instruct_x = pos_font_np_x + int(size_font_np * 2.1) pos_font_instruct_y = pos_font_np_y + 4 pos_font_keys_aff_1_x = field_keys_x + 8 + rwtools pos_font_keys_aff_1_y = field_keys_y + 26 pos_font_keys_aff_2_x = copy.deepcopy(pos_font_keys_aff_1_x) pos_font_keys_aff_2_y = copy.deepcopy(pos_font_keys_aff_1_y) pos_font_keys_nav_x = field_keys_x + 8 + rwtools pos_font_keys_nav_y = field_keys_y + 5 pos_font_keys_neg_x = rw - 52 - len_neg - rwui np_print('len_neg', len_neg) np_print('pos_font_keys_neg_x', pos_font_keys_neg_x) pos_font_keys_neg_y = field_keys_y + 5 if expand: pos_font_keys_aff_2_x = field_keys_x + 8 + rwtools pos_font_keys_aff_2_y = field_keys_y + 5 pos_font_keys_nav_x = field_keys_x + 8 + rwtools pos_font_keys_nav_y = field_keys_y - 16 pos_font_keys_neg_x = rw - 52 - len_neg - rwui pos_font_keys_neg_y = field_keys_y - 16 - rwui col_font_np = addon_settings_graph('col_font_np') col_font_instruct_main = addon_settings_graph('col_font_instruct_main') col_font_instruct_shadow = addon_settings_graph('col_font_instruct_shadow') col_font_keys = addon_settings_graph('col_font_keys') col_field_keys_aff = addon_settings_graph('col_field_keys_aff') col_field_keys_neg = addon_settings_graph('col_field_keys_neg') # instructions - NP: bgl.glColor4f(*col_font_np) blf.size(font_id, size_font_np, 72) blf.position(font_id, pos_font_np_x, pos_font_np_y, 0) blf.draw(font_id, 'NP') blf.enable(font_id, ROTATION) ang = radians(90) blf.size(font_id, int(size_font_np / 2.2), 72) blf.rotation(font_id, ang) blf.position(font_id, pos_font_np_x + int(size_font_np * 1.72), pos_font_np_y - 2, 0) blf.draw(font_id, version) blf.disable(font_id, ROTATION) # instructions - instruct: bgl.glColor4f(*col_font_instruct_shadow) blf.position(font_id, pos_font_instruct_x + 1, pos_font_instruct_y - 1, 0) blf.size(font_id, size_font_instruct, 72) blf.draw(font_id, instruct) bgl.glColor4f(*col_font_instruct_main) blf.position(font_id, pos_font_instruct_x, pos_font_instruct_y, 0) blf.size(font_id, size_font_instruct, 72) blf.draw(font_id, instruct) bgl.glDisable(bgl.GL_BLEND) # instructions - keys - backdrop fields: bgl.glEnable(bgl.GL_BLEND) if crop == False: bgl.glColor4f(*col_field_keys_aff) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_aff_1: bgl.glVertex2f(*co) bgl.glEnd() bgl.glColor4f(*col_field_keys_aff) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_aff_2: bgl.glVertex2f(*co) bgl.glEnd() bgl.glColor4f(*col_field_keys_neg) bgl.glBegin(bgl.GL_TRIANGLE_FAN) for co in field_keys_neg: bgl.glVertex2f(*co) bgl.glEnd() # instructions - keys - writing: bgl.glColor4f(*col_font_keys) blf.size(font_id, size_font_keys, 72) blf.position(font_id, pos_font_keys_aff_1_x, pos_font_keys_aff_1_y, 0) blf.draw(font_id, keys_aff_1) blf.position(font_id, pos_font_keys_aff_2_x, pos_font_keys_aff_2_y, 0) blf.draw(font_id, keys_aff_2) blf.position(font_id, pos_font_keys_nav_x, pos_font_keys_nav_y, 0) blf.draw(font_id, keys_nav) blf.position(font_id, pos_font_keys_neg_x, pos_font_keys_neg_y, 0) blf.draw(font_id, keys_neg)
def draw_callback_px(self, context): """Draws Code Editors Minimap and indentation marks""" def draw_line(origin, length, thickness, vertical = False): """Drawing lines with polys, its faster""" x = (origin[0] + thickness) if vertical else (origin[0] + length) y = (origin[1] + length) if vertical else (origin[1] + thickness) bgl.glBegin(bgl.GL_QUADS) for v1, v2 in [origin, (x, origin[1]), (x, y), (origin[0], y)]: bgl.glVertex2i(v1, v2) bgl.glEnd() return # abort if another text editor if self.area == context.area and self.window == context.window: bgl.glEnable(bgl.GL_BLEND) else: return start = time.clock() # init params font_id = 0 self.width = next(region.width for region in context.area.regions if region.type=='WINDOW') self.height = next(region.height for region in context.area.regions if region.type=='WINDOW') dpi_r = context.user_preferences.system.dpi / 72.0 self.left_edge = self.width - round(dpi_r*(self.width+5*self.minimap_width)/10.0) self.right_edge = self.width - round(dpi_r*15) self.opacity = min(max(0,(self.width-self.min_width) / 100.0),1) # compute character dimensions mcw = dpi_r * self.minimap_symbol_width # minimap char width mlh = round(dpi_r * self.minimap_line_height) # minimap line height fs = context.space_data.font_size cw = round(dpi_r * round(2 + 0.6 * (fs - 4))) # char width ch = round(dpi_r * round(2 + 1.3 * (fs - 2) + ((fs % 10) == 0))) # char height # panel background box self.tab_width = round(dpi_r * 25) if (self.tabs and len(bpy.data.texts) > 1) else 0 bgl.glColor4f(self.background.r, self.background.g, self.background.b, (1-self.bg_opacity)*self.opacity) bgl.glBegin(bgl.GL_QUADS) for x, y in [(self.left_edge-self.tab_width, self.height), (self.right_edge, self.height), (self.right_edge, 0), (self.left_edge-self.tab_width, 0)]: bgl.glVertex2i(x, y) bgl.glEnd() # line numbers background space = context.space_data if space.text: lines = len(space.text.lines) lines_digits = len(str(lines)) if space.show_line_numbers else 0 self.line_bar_width = int(dpi_r*5)+cw*(lines_digits) bgl.glColor4f(self.background.r, self.background.g, self.background.b, 1) bgl.glBegin(bgl.GL_QUADS) for x, y in [(0, self.height), (self.line_bar_width, self.height), (self.line_bar_width, 0), (0, 0)]: bgl.glVertex2i(x, y) bgl.glEnd() # shadow bgl.glLineWidth(1.0 * dpi_r) for id, intensity in enumerate([0.2,0.1,0.07,0.05,0.03,0.02,0.01]): bgl.glColor4f(0.0, 0.0, 0.0, intensity) bgl.glBegin(bgl.GL_LINE_STRIP) for x, y in [(self.line_bar_width+id, 0), (self.line_bar_width+id, self.height)]: bgl.glVertex2i(x, y) bgl.glEnd() # minimap shadow for id, intensity in enumerate([0.2,0.1,0.07,0.05,0.03,0.02,0.01]): bgl.glColor4f(0.0, 0.0, 0.0, intensity*self.opacity) bgl.glBegin(bgl.GL_LINE_STRIP) for x, y in [(self.left_edge-id-self.tab_width, 0), (self.left_edge-id-self.tab_width, self.height)]: bgl.glVertex2i(x, y) bgl.glEnd() # divider if self.tab_width: bgl.glColor4f(0.0, 0.0, 0.0, 0.2*self.opacity) bgl.glBegin(bgl.GL_LINE_STRIP) for x, y in [(self.left_edge, 0), (self.left_edge, self.height)]: bgl.glVertex2i(x, y) bgl.glEnd() # if there is text in window if space.text and self.opacity: # minimap horizontal sliding based on text block length max_slide = max(0, mlh*(lines+self.height/ch) - self.height) self.slide = int(max_slide * space.top / lines) minimap_top_line = int(self.slide/mlh) minimap_bot_line = int((self.height+self.slide)/mlh) # draw minimap visible box if self.in_minimap: bgl.glColor4f(1.0, 1.0, 1.0, 0.1*self.opacity) else: bgl.glColor4f(1.0, 1.0, 1.0, 0.07*self.opacity) bgl.glBegin(bgl.GL_QUADS) for x, y in [(self.left_edge, self.height-mlh*space.top + self.slide), (self.right_edge, self.height-mlh*space.top + self.slide), (self.right_edge, self.height-mlh*(space.top+space.visible_lines) + self.slide), (self.left_edge, self.height-mlh*(space.top+space.visible_lines) + self.slide)]: bgl.glVertex2i(x, y) bgl.glEnd() # draw minimap code for segment in self.segments[:-1]: bgl.glColor4f(segment['col'][0], segment['col'][1], segment['col'][2], 0.4*self.opacity) for id, element in enumerate(segment['elements'][minimap_top_line:minimap_bot_line]): loc_y = mlh*(id+minimap_top_line+3) - self.slide for sub_element in element: draw_line((self.left_edge+int(mcw*(sub_element[0]+4)), self.height-loc_y), int(mcw*(sub_element[1]-sub_element[0])), int(0.5 * mlh)) # minimap code marks bgl.glColor4f(self.segments[-2]['col'][0], self.segments[-2]['col'][1], self.segments[-2]['col'][2], 0.3*self.block_trans*self.opacity) for id, element in enumerate(self.segments[-2]['elements']): for sub_element in element: if sub_element[2] >= space.top or id < space.top+space.visible_lines: draw_line((self.left_edge+int(mcw*(sub_element[0]+4)), self.height-mlh*(id+3)+self.slide), -int(mlh*(sub_element[2]-id-1)), int(0.5 * mlh), True) # draw dotted indentation marks bgl.glLineWidth(1.0 * dpi_r) if space.text: bgl.glColor4f(self.segments[0]['col'][0], self.segments[0]['col'][1], self.segments[0]['col'][2], self.indent_trans) for id, element in enumerate(self.segments[-1]['elements'][space.top:space.top+space.visible_lines]): loc_y = id for sub_element in element: draw_line((int(dpi_r*10)+cw*(lines_digits+sub_element[0]+4), self.height-ch*(loc_y)), -ch, int(1 * dpi_r), True) # draw code block marks bgl.glColor4f(self.segments[-2]['col'][0], self.segments[-2]['col'][1], self.segments[-2]['col'][2], self.block_trans) for id, element in enumerate(self.segments[-2]['elements']): for sub_element in element: if sub_element[2] >= space.top or id < space.top+space.visible_lines: bgl.glBegin(bgl.GL_LINE_STRIP) bgl.glVertex2i(int(dpi_r*10+cw*(lines_digits+sub_element[0])), self.height-ch*(id+1-space.top)) bgl.glVertex2i(int(dpi_r*10+cw*(lines_digits+sub_element[0])), self.height-int(ch*(sub_element[2]-space.top))) bgl.glVertex2i(int(dpi_r*10+cw*(lines_digits+sub_element[0]+1)), self.height-int(ch*(sub_element[2]-space.top))) bgl.glEnd() # tab dividers if self.tab_width and self.opacity: self.tab_height = min(200, int(self.height/len(bpy.data.texts))) y_loc = self.height-5 for text in bpy.data.texts: # tab selection if text.name == self.in_tab: bgl.glColor4f(1.0, 1.0, 1.0, 0.05*self.opacity) bgl.glBegin(bgl.GL_QUADS) for x, y in [(self.left_edge-self.tab_width, y_loc), (self.left_edge, y_loc), (self.left_edge, y_loc-self.tab_height), (self.left_edge-self.tab_width, y_loc-self.tab_height)]: bgl.glVertex2i(x, y) bgl.glEnd() # tab active if context.space_data.text and text.name == context.space_data.text.name: bgl.glColor4f(1.0, 1.0, 1.0, 0.05*self.opacity) bgl.glBegin(bgl.GL_QUADS) for x, y in [(self.left_edge-self.tab_width, y_loc), (self.left_edge, y_loc), (self.left_edge, y_loc-self.tab_height), (self.left_edge-self.tab_width, y_loc-self.tab_height)]: bgl.glVertex2i(x, y) bgl.glEnd() bgl.glColor4f(0.0, 0.0, 0.0, 0.2*self.opacity) y_loc -= self.tab_height bgl.glBegin(bgl.GL_LINE_STRIP) for x, y in [(self.left_edge-self.tab_width, y_loc), (self.left_edge, y_loc)]: bgl.glVertex2i(x, y) bgl.glEnd() # draw fps # bgl.glColor4f(1, 1, 1, 0.2) # blf.size(font_id, fs, int(dpi_r*72)) # blf.position(font_id, self.left_edge-50, 5, 0) # blf.draw(font_id, str(round(1/(time.clock() - start),3))) # draw line numbers if space.text: bgl.glColor4f(self.segments[0]['col'][0], self.segments[0]['col'][1], self.segments[0]['col'][2], 0.5) for id in range(space.top, min(space.top+space.visible_lines+1, lines+1)): if self.in_line_bar and self.segments[-2]['elements'][id-1]: bgl.glColor4f(self.segments[-2]['col'][0], self.segments[-2]['col'][1], self.segments[-2]['col'][2], 1) blf.position(font_id, 2+int(0.5*cw*(len(str(lines))-1)), self.height-ch*(id-space.top)+3, 0) #blf.draw(font_id, '→') blf.draw(font_id, '↓') bgl.glColor4f(self.segments[0]['col'][0], self.segments[0]['col'][1], self.segments[0]['col'][2], 0.5) else: blf.position(font_id, 2+int(0.5*cw*(len(str(lines))-len(str(id)))), self.height-ch*(id-space.top)+3, 0) blf.draw(font_id, str(id)) # draw file names if self.tab_width: blf.enable(font_id, blf.ROTATION) blf.rotation(font_id, 1.570796) y_loc = self.height for text in bpy.data.texts: text_max_length = max(2,int((self.tab_height - 40)/cw)) name = text.name[:text_max_length] if text_max_length < len(text.name): name += '...' bgl.glColor4f(self.segments[0]['col'][0], self.segments[0]['col'][1], self.segments[0]['col'][2], (0.7 if text.name == self.in_tab else 0.4)*self.opacity) blf.position(font_id, self.left_edge-round((self.tab_width-ch)/2.0)-5, round(y_loc-(self.tab_height/2)-cw*len(name)/2), 0) blf.draw(font_id, name) y_loc -= self.tab_height # restore opengl defaults bgl.glColor4f(0, 0, 0, 1) bgl.glLineWidth(1.0) bgl.glDisable(bgl.GL_BLEND) blf.disable(font_id, blf.ROTATION) return