Exemplo n.º 1
0
class SequenceCanvas(wx.glcanvas.GLCanvas, packets.SequenceDiagram):
    def __init__(self, parent, actors):
        wx.glcanvas.GLCanvas.__init__(self, parent,-1)
        self.line_batch = pyglet.graphics.Batch()
        self.text_batch = pyglet.graphics.Batch()
        self.parent = parent
        wx.EVT_PAINT(self, self.OnPaint)
        self.Bind(wx.EVT_TIMER, self.OnPaint)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftMouseButtonDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnLeftMouseButtonUp)
        self.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseScroll)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        self.selected_sprite = None
        self.is_dragging =False
        self.is_fullscreen_set = False
        self.__zoom = 3
        self.target_zoom = 8
        color_white = (1, 1, 1, 1)
        pyglet.gl.glClearColor(*color_white)
        pyglet.gl.glEnable(pyglet.gl.GL_LINE_SMOOTH)
        pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
        pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
        self.Bind(wx.EVT_KEY_DOWN, self.parent.OnKeyDown)
        self.camera = Camera((0, 0), pow(2, 18-self.__zoom))
        packets.SequenceDiagram.__init__(self, parent.world.packets, actors, xzoom = 200, yzoom = -200, spritezoom = 3)
        self.SetCurrent()
        self.timer.Start(100)

    def OnMouseScroll(self, event):
        delta = event.m_wheelRotation
        self.zoom+= max(min(delta,2),-2)

    def OnMotion(self, event):
        x,y = event.GetPositionTuple()
        if event.Dragging():
            self.SetCursor(wx.StockCursor(wx.CURSOR_SIZING))
            if self.parent.target_obj:
                self.parent.target_obj = None
            self.move_camera(self.mouse_x-x,
                            -(self.mouse_y-y))
        self.mouse_x, self.mouse_y = x,y
    
    def move_camera(self, x, y):
        self.camera.target.x += self.camera.scale*x/100
        self.camera.target.y += self.camera.scale*y/100
        
            
    def OnLeftMouseButtonDown(self, event):
        pass
        
    def OnLeftMouseButtonUp(self, event):
        self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))

    @property
    def world(self):
        return self.parent.world
        
    def OnPaint(self,event):
        self.SetCurrent()
        pyglet.gl.glClear(pyglet.gl.GL_COLOR_BUFFER_BIT| pyglet.gl.GL_DEPTH_BUFFER_BIT)
        if self.world:
            self.width, self.height = self.GetSize().width, self.GetSize().height
            if self.target_zoom <> self.__zoom:
                self.camera.zoom(pow(2, self.__zoom - self.target_zoom))
                self.__zoom = self.target_zoom
            # Updating the camera
            self.camera.update()
            self.camera.focus(self.width, self.height)
            self.line_batch.draw()
            # self.camera.focus3(self.width, self.height)
            self.text_batch.draw()
            # self.camera.hud_mode(self.width, self.height)
        self.SwapBuffers()
    
    @property
    def zoom(self):
        return self.__zoom
    
    @zoom.setter
    def zoom(self, new_zoom):
        self.target_zoom = min( max(new_zoom,-20), 40)
    
    def zoom_in(self):
        self.zoom +=1
    
    def zoom_out(self):
        self.zoom -=1

    def draw_text(self, x, y, label, font_size):
        l = gui_shapes.Text(self.text_batch, label, font_size = font_size, delta_y = 0, anchor_y = 'bottom')
        l.update(x, y)

    def draw_line(self, x1, y1, x2, y2, color, width):
        l = gui_shapes.Line(self.line_batch, color1 = [0,0,0,1])
        l.update(x1, y1, x2, y2)

    def draw_sprite(self, x, y, sprite, color, size):
        l = gui_shapes.Polygon(self.line_batch, sides = 12, size = size, color1 = color)
        l.update(x, y)
Exemplo n.º 2
0
class MapCanvas(wx.glcanvas.GLCanvas):
    def __init__(self, parent):
        wx.glcanvas.GLCanvas.__init__(self, parent,-1)
        self.parent = parent
        wx.EVT_PAINT(self, self.OnPaint)
        self.Bind(wx.EVT_TIMER, self.OnPaint)
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_LEFT_DOWN, self.OnLeftMouseButtonDown)
        self.Bind(wx.EVT_LEFT_UP, self.OnLeftMouseButtonUp)
        self.Bind(wx.EVT_MOUSEWHEEL, self.OnMouseScroll)
        self.Bind(wx.EVT_MOTION, self.OnMotion)
        # self.Bind(wx.EVT_SIZE, self.OnResize)

        self.init = 0
        self.selected_sprite = None
        self.is_dragging =False
        self.is_fullscreen_set = False
        self.__zoom = 1
        self.target_zoom = 3
        self.labels = []
        # color_gray = 0.95, 0.93, 0.91, 1
        color_gray = 1, 1, 1, 1
        pyglet.gl.glClearColor(*color_gray)
        pyglet.gl.glEnable(pyglet.gl.GL_LINE_SMOOTH)
        pyglet.gl.glEnable(pyglet.gl.GL_BLEND)
        pyglet.gl.glBlendFunc(pyglet.gl.GL_SRC_ALPHA, pyglet.gl.GL_ONE_MINUS_SRC_ALPHA)
        
        self.Bind(wx.EVT_KEY_DOWN, self.parent.OnKeyDown)
        self.scene = gui_sprites.Scene(self)
    
        #win.add_label(lambda: str(win.world_map.batch_down.remaining()), 340, 18)
        self.add_label(lambda: 'zoom: '+ str(self.zoom))

    # def OnResize(self, event):
    #     pass
    
    def add_item(self, item):
        class_name = item.__class__.__name__+'Sprite'
        sprite_class = gui_sprites.__dict__[class_name]
        item.sprite = sprite_class(self.scene, item)
        
        
    def OnMouseScroll(self, event):
        delta = event.m_wheelRotation
        self.zoom += max(min(delta,2),-2)

    def add_label(self, lambda_text):
        self.labels.append(gui_sprites.Label(self.scene, lambda_text))

    def OnMotion(self, event):
        x,y = event.GetPositionTuple()
        if event.Dragging():
            self.SetCursor(wx.StockCursor(wx.CURSOR_SIZING))
            if self.parent.target_obj:
                self.parent.target_obj = None
            self.move_camera(self.mouse_x-x,
                                        -(self.mouse_y-y))
        self.mouse_x, self.mouse_y = x,y
    
    def move_camera(self, x, y):
        self.camera.target.x += self.camera.scale*x/100
        self.camera.target.y += self.camera.scale*y/100
        
            
    def OnLeftMouseButtonDown(self, event):
        pass
        
    def OnLeftMouseButtonUp(self, event):
        self.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))

    @property
    def world(self):
        return self.parent.world
        
    def OnPaint(self,event):
        if not self.init:
            self.InitGL()
            self.init = 1
            self.timer.Start(50)
        self.SetCurrent()
        self.OnDraw()
        return
    
    def InitGL(self):        
        
        # self.group = pyglet.graphics.OrderedGroup(20)
        self.world_map = WorldMap(self, 128, 1)
        self.camera = Camera((0, 0), pow(2, 7+18-self.__zoom))
    

    def OnDraw(self):
        pyglet.gl.glClear(pyglet.gl.GL_COLOR_BUFFER_BIT ) #| pyglet.gl.GL_DEPTH_BUFFER_BIT
                
        # Looking for objects to draw
        if self.world:
            self.width, self.height = self.GetSize().width, self.GetSize().height
            if self.target_zoom <> self.__zoom:
                self.camera.zoom(pow(2, self.__zoom - self.target_zoom))
                self.world_map.zoom = self.target_zoom
                self.__zoom = self.target_zoom
        
            # Updating the camera
            self.camera.update()
            self.camera.focus(self.width, self.height)
            if self.parent.target_obj:
                try:
                    point = self.parent.target_obj.position
                    self.world_map.geo_focus(point)
                    self.geo_target(point)
                except AttributeError:
                    pass
            else:
                self.map_target(self.camera.target.x, self.camera.target.y)

            # 
            for obj in self.parent.items:
                obj.sprite.update()

            # Draw the world Map
            self.world_map.draw()
            

            # Draw the paths and the ranges
            self.scene.draw_ranges()

            # Change the projection matrix of the camera to draw places, nodes, ...
            self.camera.focus2(self.width, self.height)
        
            # draw places, nodes, ifaces, apps, etc.
            self.scene.draw_icons()
        
            # Change the projection matrix again
            self.camera.hud_mode(self.width, self.height)
            
            for l in self.labels:
                    l.update()
                    
            self.scene.draw_labels()
        self.SwapBuffers()
    
    @property
    def zoom(self):
        return self.__zoom
    
    @zoom.setter
    def zoom(self, new_zoom):
        self.target_zoom = min( max(new_zoom,0), 20)
    
    def zoom_in(self):
        self.zoom +=1
    
    def zoom_out(self):
        self.zoom -=1
    
    def geo_target(self, point):
        self.world_map.geo_focus(point)
        self.camera.target.x = point.x
        self.camera.target.y = point.y
    
    def map_target(self, xm, ym):
        xo, yo = self.world_map.map2osm(xm, ym)
        self.world_map.map_focus((xo, yo))
        self.camera.target.x = xm
        self.camera.target.y = ym
    
    def scr2map(self, xs, ys):
        ratio = float(self.width)/self.height
        xm = ((float(xs) / self.width)-0.5)*self.camera.scale*2.0*ratio + self.camera.x
        ym = ((float(ys) /self.height)-0.5)*self.camera.scale*2.0 + self.camera.y
        return xm, ym
    
    def map2scr(self, xm, ym):
        ratio = float(self.width)/self.height
        xs = (((float(xm)-self.camera.x)/(self.camera.scale*2.0*ratio))+0.5)*self.width
        ys = (((float(ym)-self.camera.y)/(self.camera.scale*2.0))+0.5)*self.height
        return xs, ys
    
    def map2scr2(self, xm, ym):
        diff = self.camera.desc_layer_scale/self.camera.scale
        return (xm)*diff, (ym)*diff