示例#1
0
class FLGLWidget(Fl.Gl_Window):
    def __init__(self, X, Y, W, H):
        Fl.Gl_Window.__init__(self, X, Y, W, H)
        
        # Set OpenGL mode
        flg = Fl.RGB | Fl.ALPHA | Fl.DOUBLE | Fl.DEPTH | Fl.STENCIL | \
              Fl.MULTISAMPLE
        self.mode(flg)
        
        self.vgcontext = None
        self.renderer = None
        
        self.dx, self.dy = 0., 0.
        self.scale = 1.
        
        self.mouse_x = None
        self.mouse_y = None
    
    def openFile(self, widget = None, filename = None):
        if filename is None:
            native = Fl.Native_File_Chooser()
            native.title("Select SVG file")
            native.filter('SVG\t*.svg')
            ret = native.show()
            if ret == -1 or ret == 1:
               return
            
            filename = native.filename()
        
        xmltree = readFile(filename)
        self.renderer = Renderer(xmltree, imageprovider = image_cb)
        
        if isinstance(filename, basestring):
            name = os.path.split(filename)[-1]
        else:
            name = 'unamed.svg'
            
        self.updateScale()
        self.redraw()
        
    def updateScale(self):
        # scale to bounding box
        if not self.renderer is None:
            width, height = self.w(), self.h()
            bbox = self.box = self.renderer.bounds
            self.scale = .9*min(width/bbox.width, height/bbox.height)
            
    def draw(self):
        width, height = self.w(), self.h()
       
        if not self.context_valid():
            self.vgcontext = vg.CreateContextSH(width, height)
            self.updateScale()
        
        if not self.valid():
            vg.ResizeSurfaceSH(width,height)
            self.updateScale()
        
        # Clear to white
        vg.Setfv(vg.CLEAR_COLOR, 4, [1.,1.,1.,1.])
        vg.Clear(0, 0, width, height)
        
        if self.renderer is None:
            return
        
        # setup transform
        box = self.box
        scale = self.scale
        dx, dy = self.dx, self.dy
        
        vg.Seti(vg.MATRIX_MODE, vg.MATRIX_PATH_USER_TO_SURFACE)
        vg.LoadIdentity()
        
        vg.Scale(scale, scale)
        vg.Translate(0., 1.5*box.height)
        vg.Scale(1., -1.)
        vg.Translate(-box.minx - dx, -box.miny + .5*box.height - dy)
        
        # render paths
        self.renderer.render()
        
        # fix for garbage when drawing gradients
        vg.Clear(0, 0, width, height)
        self.renderer.render()
            
    def handle(self, event):
        if event == Fl.FOCUS:
            self.redraw()
            return 1   
        elif event == Fl.MOUSEWHEEL:
            self.scale *= 1 + .1*Fl.event_dy()
            self.redraw()
            return 1
        elif event == Fl.PUSH:
            if Fl.event_button() == Fl.MIDDLE_MOUSE:
                self.mouse_x = Fl.event_x()
                self.mouse_y = Fl.event_y()
            return 1
        
        elif event == Fl.RELEASE:
            self.mouse_x = None
            self.mouse_y = None
            return 0
            
        elif event == Fl.DRAG:
            if Fl.event_button() == Fl.MIDDLE_MOUSE:
                if not self.mouse_x is None:
                    dx = Fl.event_x() - self.mouse_x
                    dy = Fl.event_y() - self.mouse_y
                    
                    mat = vg.GetMatrix()
                    sx, sy = mat[0], mat[4]
                    
                    self.dx -= dx/sx
                    self.dy += dy/sy
                    
                    self.mouse_x += dx
                    self.mouse_y += dy
                    
                    self.redraw()
                    return 1
            
        return 0
示例#2
0
class FLGLWidget(Fl.Gl_Window):
    def __init__(self, X, Y, W, H):
        Fl.Gl_Window.__init__(self, X, Y, W, H)

        # Set OpenGL mode
        flg = Fl.RGB | Fl.ALPHA | Fl.DOUBLE | Fl.DEPTH | Fl.STENCIL | \
              Fl.MULTISAMPLE
        self.mode(flg)

        self.vgcontext = None
        self.renderer = None

        self.dx, self.dy = 0., 0.
        self.scale = 1.

        self.mouse_x = None
        self.mouse_y = None

    def openFile(self, widget=None, filename=None):
        if filename is None:
            native = Fl.Native_File_Chooser()
            native.title("Select SVG file")
            native.filter('SVG\t*.svg')
            ret = native.show()
            if ret == -1 or ret == 1:
                return

            filename = native.filename()

        xmltree = readFile(filename)
        self.renderer = Renderer(xmltree, imageprovider=image_cb)

        if isinstance(filename, basestring):
            name = os.path.split(filename)[-1]
        else:
            name = 'unamed.svg'

        self.updateScale()
        self.redraw()

    def updateScale(self):
        # scale to bounding box
        if not self.renderer is None:
            width, height = self.w(), self.h()
            bbox = self.box = self.renderer.bounds
            self.scale = .9 * min(width / bbox.width, height / bbox.height)

    def draw(self):
        width, height = self.w(), self.h()

        if not self.context_valid():
            self.vgcontext = vg.CreateContextSH(width, height)
            self.updateScale()

        if not self.valid():
            vg.ResizeSurfaceSH(width, height)
            self.updateScale()

        # Clear to white
        vg.Setfv(vg.CLEAR_COLOR, 4, [1., 1., 1., 1.])
        vg.Clear(0, 0, width, height)

        if self.renderer is None:
            return

        # setup transform
        box = self.box
        scale = self.scale
        dx, dy = self.dx, self.dy

        vg.Seti(vg.MATRIX_MODE, vg.MATRIX_PATH_USER_TO_SURFACE)
        vg.LoadIdentity()

        vg.Scale(scale, scale)
        vg.Translate(0., 1.5 * box.height)
        vg.Scale(1., -1.)
        vg.Translate(-box.minx - dx, -box.miny + .5 * box.height - dy)

        # render paths
        self.renderer.render()

        # fix for garbage when drawing gradients
        vg.Clear(0, 0, width, height)
        self.renderer.render()

    def handle(self, event):
        if event == Fl.FOCUS:
            self.redraw()
            return 1
        elif event == Fl.MOUSEWHEEL:
            self.scale *= 1 + .1 * Fl.event_dy()
            self.redraw()
            return 1
        elif event == Fl.PUSH:
            if Fl.event_button() == Fl.MIDDLE_MOUSE:
                self.mouse_x = Fl.event_x()
                self.mouse_y = Fl.event_y()
            return 1

        elif event == Fl.RELEASE:
            self.mouse_x = None
            self.mouse_y = None
            return 0

        elif event == Fl.DRAG:
            if Fl.event_button() == Fl.MIDDLE_MOUSE:
                if not self.mouse_x is None:
                    dx = Fl.event_x() - self.mouse_x
                    dy = Fl.event_y() - self.mouse_y

                    mat = vg.GetMatrix()
                    sx, sy = mat[0], mat[4]

                    self.dx -= dx / sx
                    self.dy += dy / sy

                    self.mouse_x += dx
                    self.mouse_y += dy

                    self.redraw()
                    return 1

        return 0
示例#3
0
    
    pixels = vg.PixelBuffer(width,height)
    ctx = vg.CreateOffScreenSH()
    vg.StartOffScreenSH(ctx, width, height)
    
    vg.Setfv(vg.CLEAR_COLOR, 4, [1.,1.,1.,1.])
    vg.Clear(0, 0, width, height)
    
    # center on bounding box
    box = renderer.bounds
    scale = min(width/box.width, height/box.height)
    
    vg.Seti(vg.MATRIX_MODE, vg.MATRIX_PATH_USER_TO_SURFACE)
    vg.LoadIdentity()
    
    vg.Scale(scale, scale)
    vg.Translate(0., 1.5*box.height)
    vg.Scale(1., -1.)
    vg.Translate(-box.minx, -box.miny + .5*box.height)
    
    renderer.render()
    vg.EndOffScreenSH(ctx, pixels)
    vg.DestroyOffScreenSH(ctx)
    
    img = Fl.RGB_Image(width,height, 4, 0, pixels)
    
    widget.set_image(img)
    window.show()
    Fl.run()

示例#4
0
    widget = Fl.Button(10, 10, width, height)

    pixels = vg.PixelBuffer(width, height)
    ctx = vg.CreateOffScreenSH()
    vg.StartOffScreenSH(ctx, width, height)

    vg.Setfv(vg.CLEAR_COLOR, 4, [1., 1., 1., 1.])
    vg.Clear(0, 0, width, height)

    # center on bounding box
    box = renderer.bounds
    scale = min(width / box.width, height / box.height)

    vg.Seti(vg.MATRIX_MODE, vg.MATRIX_PATH_USER_TO_SURFACE)
    vg.LoadIdentity()

    vg.Scale(scale, scale)
    vg.Translate(0., 1.5 * box.height)
    vg.Scale(1., -1.)
    vg.Translate(-box.minx, -box.miny + .5 * box.height)

    renderer.render()
    vg.EndOffScreenSH(ctx, pixels)
    vg.DestroyOffScreenSH(ctx)

    img = Fl.RGB_Image(width, height, 4, 0, pixels)

    widget.set_image(img)
    window.show()
    Fl.run()