Beispiel #1
0
def drawIcon(image_file, pos_x, pos_y):
    bgl.glPushClientAttrib(bgl.GL_CURRENT_BIT|bgl.GL_ENABLE_BIT)

    # transparence
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

    bgl.glRasterPos2f(pos_x, pos_y)

    buf = buffers[image_file]
    bgl.glDrawPixels(ICON_SIZE, ICON_SIZE, bgl.GL_RGBA, bgl.GL_FLOAT, buf)

    bgl.glPopClientAttrib()
def draw_callback_px(self, context):
    wm = context.window_manager
    r = 0.8
    g = 0.1
    b = 0.2

    if (context.window_manager.leap_nui_keyboardless_active):
        #print("Draw Callback True")
        # draw text in the 3D View
        bgl.glPushClientAttrib(bgl.GL_CURRENT_BIT | bgl.GL_ENABLE_BIT)

        blf.size(0, 12, 72)
        blf.position(0, 10, 10, 0)
        bgl.glColor4f(r, g, b, 0.7)
        blf.blur(0, 1)
        # shadow?
        blf.enable(0, blf.SHADOW)
        blf.shadow_offset(0, 1, -1)
        blf.shadow(0, 5, 0.0, 0.0, 0.0, 0.8)

        blf.draw(0, "Leap Active!")

        bgl.glPopClientAttrib()
    else:
        #print("Draw Callback False")
        pass

    #
    # Draw the LeapInfo log messages
    font_size = 24
    messages = wm.leap_info.log_messages
    n_messages = len(messages)
    log_y_size = LeapInfo.MAX_LOG_MESSAGES * font_size
    #pos_y = context.region.height - ((context.region.height - log_y_size) / 2)
    pos_y = ((context.region.height - log_y_size) / 2)
    pos_x = 0
    bgl.glPushClientAttrib(bgl.GL_CURRENT_BIT | bgl.GL_ENABLE_BIT)
    blf.size(0, font_size, 72)
    bgl.glColor4f(r, g, b, 0.7)

    for time, msg in reversed(messages):
        blf.position(0, pos_x, pos_y, 0)
        blf.draw(0, msg)
        pos_y += font_size

    bgl.glPopClientAttrib()

    #
    # Draw icon if the hand is visible to the Leap
    if (wm.leap_info.leap_listener.getHandId() == None):  # NO HAND
        pass
    else:
        pos_x = context.region.width - (Icons.ICON_SIZE * 1.5)
        pos_y = (Icons.ICON_SIZE / 2)

        if (wm.leap_info.isTracking()):  # GREEN HAND
            Icons.drawIcon("5-spreadfingers-icon-green.png", pos_x, pos_y)
            pass
        else:  # RED HAND
            Icons.drawIcon("5-spreadfingers-icon-red.png", pos_x, pos_y)
            pass
    def draw_callback_px_moving_arrow(self, context):
        #print("drawing")

        DPI = bpy.context.user_preferences.system.dpi
        print("Rendering moving arrow with DPI " + str(DPI))

        int_selection_num = int(self.selection_num)
        n_items = len(self.selectable_items)

        # Phylosophy
        # We try to use the fraction of the current heght of the area, but up to a maximum font size

        # Number of items to really display
        n_items_to_display = min(MAX_DISPLAY_ELEMENTS,
                                 len(self.selectable_items))

        # Calculate the top point
        text_top_y = context.region.height * (
            (1 + SELECTION_DISPLAY_HEIGHT) / 2)

        # calc the desired text area height
        desired_bottom_y = context.region.height * (
            (1 - SELECTION_DISPLAY_HEIGHT) / 2)
        desired_text_area_height = text_top_y - desired_bottom_y
        # calc the desired the font size
        font_size = int(desired_text_area_height / n_items_to_display)
        # But limit the font size to their maximum
        font_size = min(font_size, self.FONT_MAX_SIZE)
        # The effective size of the text area, according to the chosen font size
        text_area_height = font_size * n_items_to_display
        # recompute the bottom point according to chosen font size
        text_bottom_y = text_top_y - text_area_height

        #print("Font_size = " + str(font_size))

        bgl.glPushAttrib(bgl.GL_CLIENT_ALL_ATTRIB_BITS)

        blf.size(0, font_size, DPI)

        #
        # Draw background
        max_text_width = 0
        for item in self.selectable_items:
            item_w, item_h = blf.dimensions(0, item)
            if (item_w > max_text_width):
                max_text_width = item_w

        self.draw_bg(context=context,
                     top_y=text_top_y,
                     bottom_y=text_bottom_y,
                     width=max_text_width * 1.5)

        #
        # Draw entries
        pos_x = 0
        pos_y = text_top_y - font_size
        pos_y += self.selection_window_first_item * font_size

        for item_id in range(0, len(self.selectable_items)):
            item = self.selectable_items[item_id]

            item_w, item_h = blf.dimensions(0, item)
            pos_x = (context.region.width / 2) - item_w

            blf.position(0, pos_x, pos_y, 0)

            if (item_id == int_selection_num):
                bgl.glColor4f(*self.SELECTED_FONT_RGBA)
            else:
                bgl.glColor4f(*self.FONT_RGBA)

            blf.draw(0, item)
            pos_y -= font_size

        bgl.glPopAttrib()

        #
        # Draw pointing finger
        bgl.glPushClientAttrib(bgl.GL_CURRENT_BIT | bgl.GL_ENABLE_BIT)

        # transparence
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

        if (not self.selector_visible):
            pos_x = (context.region.width / 2) + self.ICON_SIZE
            pos_y = text_top_y - self.ICON_SIZE
            bgl.glRasterPos2f(pos_x, pos_y)
            bgl.glDrawPixels(self.ICON_SIZE, self.ICON_SIZE, bgl.GL_RGBA,
                             bgl.GL_FLOAT, icon_pointing_finger_missing)
        else:
            pos_x = (context.region.width / 2)
            pos_y = text_bottom_y + self.normalized_finger_y * text_area_height - (
                self.ICON_SIZE * 0.625)
            bgl.glRasterPos2f(pos_x, pos_y)
            bgl.glDrawPixels(self.ICON_SIZE, self.ICON_SIZE, bgl.GL_RGBA,
                             bgl.GL_FLOAT, icon_pointing_finger)

        bgl.glPopClientAttrib()

        pass
    def draw_callback_px_moving_text(self, context):

        DPI = bpy.context.user_preferences.system.dpi
        #print("Rendering moving text with DPI "+str(DPI))

        #print("selection= "+str(self.selection_num))
        int_selection_num = int(self.selection_num)

        n_items = len(self.selectable_items)

        # Phylosophy
        # We try to use the fraction of the current heght of the area, but up to a maximum font size

        # Number of items to really display
        n_items_to_display = min(MAX_DISPLAY_ELEMENTS,
                                 len(self.selectable_items))

        # Calculate the top point
        text_top_y = context.region.height * (
            (1 + SELECTION_DISPLAY_HEIGHT) / 2)

        # calc the desired text area height
        desired_bottom_y = context.region.height * (
            (1 - SELECTION_DISPLAY_HEIGHT) / 2)
        desired_text_area_height = text_top_y - desired_bottom_y
        # calc the desired the font size
        font_size = int(desired_text_area_height / n_items_to_display)
        # But limit the font size to their maximum
        font_size = min(font_size, self.FONT_MAX_SIZE)
        # The effective size of the text area, according to the chosen font size
        text_area_height = font_size * n_items_to_display
        # recompute the bottom point according to chosen font size
        text_bottom_y = text_top_y - text_area_height

        #print(str(self.FONT_MAX_SIZE)+" fs="+str(font_size))

        central_y = text_bottom_y + int(text_area_height / 2)

        bgl.glPushAttrib(bgl.GL_CLIENT_ALL_ATTRIB_BITS)

        # Set the font size now, because it will be needed to estimate the background size.
        blf.size(0, font_size, DPI)

        #
        # Draw background
        max_text_width = 0
        for item in self.selectable_items:
            item_w, item_h = blf.dimensions(0, item)
            if (item_w > max_text_width):
                max_text_width = item_w
        self.draw_bg(context,
                     top_y=text_top_y,
                     bottom_y=text_bottom_y,
                     width=max_text_width * 1.5,
                     cover_pointer=True)

        #
        # Draw entries
        #
        pos_x = 0

        # The first item will be drawn on the very top, according to the current selection and its position on screen
        # The offset uses (self.selection_num - 1) because the text is written with the y at the bottom line.
        # However, it is easier to calculate thinking of its beginning at the top. So we shift the text up of one line.
        pos_y = central_y + ((self.selection_num - 1) * font_size)

        for item_id in range(0, n_items):
            item = self.selectable_items[item_id]

            item_w, item_h = blf.dimensions(0, item)
            pos_x = (context.region.width / 2) - item_w

            blf.position(0, pos_x, pos_y, 0)

            if (item_id == int_selection_num):
                bgl.glColor4f(*self.SELECTED_FONT_RGBA)
            else:
                bgl.glColor4f(*self.FONT_RGBA)

            #print("Drawing item at "+str(pos_x) + "\t" + str(pos_y))
            blf.draw(0, item)

            pos_y -= font_size

        bgl.glPopAttrib()

        #
        # Draw pointing finger
        bgl.glPushClientAttrib(bgl.GL_CURRENT_BIT | bgl.GL_ENABLE_BIT)

        # transparence
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

        # The finger icon (64x64) has the finget tip at pixel 23 from the top, or 40 from the bottom
        pos_y = central_y - 40

        if (not self.selector_visible):
            pos_x = (context.region.width / 2) + self.ICON_SIZE
            bgl.glRasterPos2f(pos_x, pos_y)
            bgl.glDrawPixels(self.ICON_SIZE, self.ICON_SIZE, bgl.GL_RGBA,
                             bgl.GL_FLOAT, icon_pointing_finger_missing)
        else:
            pos_x = (context.region.width / 2)
            bgl.glRasterPos2f(pos_x, pos_y)
            bgl.glDrawPixels(self.ICON_SIZE, self.ICON_SIZE, bgl.GL_RGBA,
                             bgl.GL_FLOAT, icon_pointing_finger)

        bgl.glPopClientAttrib()

        pass
    def drawIcons(self):
        global s_registration_active_space

        #print("Callback drawIcons")

        context = bpy.context

        #print("Drawing Icons in active space "+str(context.area.spaces.active.as_pointer()))
        if (s_registration_active_space != None):
            if (not (s_registration_active_space.as_pointer()
                     == context.area.spaces.active.as_pointer())):
                #print("Skipping...")
                return

        #print("Drawing icons on context " + str(context) + "\tarea is " + str(context.area) + "(" + context.area.type +")")

        #glDrawPixels(width, height, format, type, pixels)
        #Write a block of pixels to the frame buffer
        #
        #height (width,) – Specify the dimensions of the pixel rectangle to be written into the frame buffer.
        #format (Enumerated constant) – Specifies the format of the pixel data.
        #type (Enumerated constant) – Specifies the data type for pixels.
        #pixels (bgl.Buffer object) – Specifies a pointer to the pixel data.

        bgl.glPushClientAttrib(bgl.GL_CURRENT_BIT | bgl.GL_ENABLE_BIT)

        # transparence
        bgl.glEnable(bgl.GL_BLEND)
        bgl.glBlendFunc(bgl.GL_SRC_ALPHA, bgl.GL_ONE_MINUS_SRC_ALPHA)

        ICON_SIZE = 64
        GRID_SIZE = 3
        BORDER_SIZE = 5

        blf.size(0, self.FONT_SIZE, 72)
        #bgl.glColor4f(*self.FONT_RGBA)
        blf.blur(0, 1)

        #bgl.glScalef(0.5,0.5,0.5)

        # Cycle to draw all the cells
        cell_num = 0
        pos_y = context.region.height - ICON_SIZE - BORDER_SIZE
        for buffers_line in self.buffers:
            #pos_x = (context.region.width - (ICON_SIZE * GRID_SIZE) - (BORDER_SIZE * (GRID_SIZE-1)) ) / 2
            pos_x = context.region.width - (
                (ICON_SIZE + BORDER_SIZE) * GRID_SIZE)

            for buf in buffers_line:
                #print("Drawing icon for pos "+ str(cell_num))
                bgl.glRasterPos2f(pos_x, pos_y)
                bgl.glDrawPixels(ICON_SIZE, ICON_SIZE, bgl.GL_RGBA,
                                 bgl.GL_FLOAT, buf)

                # Print the char
                blf.position(0, pos_x, pos_y, 0)
                #bgl.glPushClientAttrib(bgl.GL_CLIENT_ALL_ATTRIB_BITS) #CURRENT_BIT|bgl.GL_ENABLE_BIT)
                bgl.glPushAttrib(bgl.GL_CLIENT_ALL_ATTRIB_BITS)
                blf.size(0, self.FONT_SIZE, 72)
                #blf.blur(0, 10)
                #blf.shadow(0, 3, 0.5, 0.5, 0.5, 0.2)
                #blf.shadow_offset(0, int(self.FONT_SIZE/10), int(self.FONT_SIZE/10))
                bgl.glColor4f(*self.FONT_RGBA)
                blf.draw(0, self.shortcut_keys[cell_num])
                bgl.glPopAttrib()
                #bgl.glPopClientAttrib()

                pos_x += ICON_SIZE + BORDER_SIZE
                cell_num += 1

            pos_y -= ICON_SIZE + BORDER_SIZE

        bgl.glPopClientAttrib()

        pass