示例#1
0
def Load():
    lines = []
    zoom = 0
    with open("saved.lines", "r") as loadfile:
        zoom = eval(loadfile.readline())
        for line in loadfile.readlines():
            l = eval(line)
            # polyline
            if type(l) == list:
                p = Polyline()
                p.load(l)
                lines.append(p)
            # regular line
            else:
                lines.append(Drawable(Line, **l))
    return zoom, lines
示例#2
0
    def on_touch_down(self, touch): #defines the starting point of a new line
        if self.ui.btnc.state == 'down':
            eraser = Drawable(Rectangle, size = self.zoom, pos=(touch.x, touch.y), color = (1, 1, 1, 0.5))
            eraser.snap(list(eraser.item.pos), self.zoom, self.lat, self.longit)
            eraser.draw(self.canvas)
            check_erase(eraser, self.lines, self.zoom, self.lat, self.longit)
            self.refresh_screen()
        if self.ui.btnline.state == 'down':
            self.coord = [[touch.x, touch.y]]

        if self.ui.btndraw.state == 'down':
            self.polyline = Polyline(self.color)
        else: self.coord = []
        self.old_x, self.old_y = touch.x, touch.y
示例#3
0
class Platform_draw(Widget):

    def undo(self, instance):  #removes the last line placed and then redraws the screen
        self.lines = self.lines[:-1]
        self.refresh_screen()
        
    def on_startup(self): #defines everything that needs to be defined before startup
        #defines variables to be used
        self.lines = []
        self.coord = []
        # drawn lines is what your fingure is tracing, disappears on touch up
        self.drawn_lines = []
        self.polyline = None
        self.straight_type = 'single'
        self.lat = ()
        self.longit = ()
        self.old_x = self.old_y = 0
        self.x = self.y = 0
        self.size = (height/12, height/12)
        self.colors = color_load()
        self.color = self.colors[-1]
        self.currentLayer = 0
        
        #refreshes screen ... just lateral lines
        self.lat, self.longit, self.lines = set_zoom([], self.zoom)
        self.refresh_screen()
    
    def save(self):
        Save(self.zoom, self.lines)
    
    def load(self):
        self.zoom, self.lines = Load()
        self.lat, self.longit, self.lines = set_zoom(self.lines, self.zoom, self.zoom, self.lat, self.longit) # changes the size of all lines
        self.coord = []
        self.x = self.y = 0
        setattr(self.zoom_slider, 'value' ,self.zoom[0])
        self.refresh_screen()
    
    def refresh_screen(self):
        self.canvas.clear()
              
        #draws grid lines
        for x in self.lat:
            self.canvas.add(Color(*self.gridColor))
            self.canvas.add(Line(points = (x, 0, x, height), width = 1))    
        for y in self.longit:
            self.canvas.add(Color(*self.gridColor))
            self.canvas.add(Line(points = (0, y, width, y), width = 1))

        #draws lines that the user has drawn
        for line in self.lines:
            line.draw(self.canvas)
            # self.canvas.add(Color(*line[1]))
            # self.canvas.add(line[0])
            
        for line in self.drawn_lines:
            self.canvas.add(Color(*line[1]))
            self.canvas.add(line[0])
            
        with self.canvas:
            Color(0.2, 0.2, 0.2)
            Rectangle(pos = (0, height-height/12), size = (width, self.size[1]))
            if len(self.upper.parent.children) > 0:
                if self.upper.btnar.pos[0] == width-self.size[0]*1.5:
                    Rectangle(pos = (width-self.size[0], 0), size = (self.size[0], height))
            
    def on_touch_down(self, touch): #defines the starting point of a new line
        if self.ui.btnc.state == 'down':
            eraser = Drawable(Rectangle, size = self.zoom, pos=(touch.x, touch.y), color = (1, 1, 1, 0.5))
            eraser.snap(list(eraser.item.pos), self.zoom, self.lat, self.longit)
            eraser.draw(self.canvas)
            check_erase(eraser, self.lines, self.zoom, self.lat, self.longit)
            self.refresh_screen()
        if self.ui.btnline.state == 'down':
            self.coord = [[touch.x, touch.y]]

        if self.ui.btndraw.state == 'down':
            self.polyline = Polyline(self.color)
        else: self.coord = []
        self.old_x, self.old_y = touch.x, touch.y
        
    def on_touch_up(self, touch):
        if self.x == 0 or self.y == 0 or [0, 0] in self.coord:
            self.x, self.y = touch.x, touch.y #sets the start point of line
        if self.coord and self.ui.btnline.state == 'down' and self.straight_type == 'single' and touch.y < height-height/12:
            self.coord.append([touch.x, touch.y]) #defines end point of new line
            line = Drawable(Line, color = self.color, points = (self.coord[0][0], self.coord[0][1], self.coord[1][0], self.coord[1][1]), width = 2)
            if not line.snap(self.coord, self.zoom, self.lat, self.longit):
                return
            if not line in self.lines:
                self.lines.append(line)
            self.coord = []
            self.refresh_screen()
        if self.ui.btndraw.state == 'down' and self.polyline != None:
            self.lines.append(self.polyline)
            self.polyline = None
            self.coord = []
            self.drawn_lines = []
            self.refresh_screen()
        self.old_x, self.old_y = touch.x, touch.y
        self.x = self.y = 0 # resets variables for finger lines
    def on_touch_move(self, touch):
        if self.x == 0 or self.y == 0 or [0, 0] in self.coord:
            self.x, self.y = touch.x, touch.y #sets the start point of line
        if self.ui.btnc.state == 'down':
            self.refresh_screen()
            eraser = Drawable(Rectangle, size = self.zoom, pos=(touch.x, touch.y), color = (1, 1, 1, 0.5))
            eraser.snap(list(eraser.item.pos), self.zoom, self.lat, self.longit)
            eraser.draw(self.canvas)
            check_erase(eraser, self.lines, self.zoom, self.lat, self.longit)
        if self.ui.btnline.state == 'down' or self.ui.btndraw.state == 'down' and touch.y < height-height/12:
            if not self.coord:
                self.coord = [[touch.x, touch.y]] #used as a start point if finger just entered screen
            self.refresh_screen()
            self.canvas.add(Color(*self.color))
            coord_temp = [[self.x, self.y], [touch.x, touch.y]]
            if self.straight_type == 'single' and self.ui.btnline.state == 'down':
                coord_temp[0] = self.coord[0]
                line = Drawable(Line, color = self.color, points = (coord_temp[0][0], coord_temp[0][1], coord_temp[1][0], coord_temp[1][1]), width = 1)
                line.snap(coord_temp, self.zoom, self.lat, self.longit)
                line.draw(self.canvas)
            elif self.ui.btnline.state == 'down':
                if self.x == 0 or self.y == 0 or [0, 0] in self.coord:
                    self.x, self.y = touch.x, touch.y #sets the start point of line
                    return
                line = Drawable(Line, color = self.color, points = (coord_temp[0][0], coord_temp[0][1], coord_temp[1][0], coord_temp[1][1]), width = 2)
                if not line.snap(coord_temp, self.zoom, self.lat, self.longit):
                    return
                if not line in self.lines:
                    self.lines.append(line)
            if self.ui.btndraw.state == 'down' and self.polyline != None:
                self.polyline.add_line(coord_temp[0][0], coord_temp[0][1], coord_temp[1][0], coord_temp[1][1])
                self.drawn_lines.append([Line(points = (coord_temp[0][0], coord_temp[0][1], coord_temp[1][0], coord_temp[1][1])), self.color])
            self.x, self.y = touch.x, touch.y #updates points
             
        if self.ui.btnm.state == 'down':
            if touch.x > width - height/12 or touch.y > height-height/12:
                return
            movex = touch.x - self.old_x
            movey = touch.y - self.old_y
            for line in self.lines:
                line.move(movex, movey)
            for line in range(len(self.drawn_lines)):
                points = (self.drawn_lines[line][0].points[0] + movex, self.drawn_lines[line][0].points[1] + movey, self.drawn_lines[line][0].points[2] + movex, self.drawn_lines[line][0].points[3] + movey)
                self.drawn_lines[line][0] = Line(points = points, width = 1)
            for line in range(len(self.lat)):
                self.lat[line] += movex
            for line in range(len(self.longit)):
                self.longit[line] += movey
            self.refresh_screen()
            self.old_x, self.old_y = touch.x, touch.y
            
            # ensures that their are always grid lines on the screen
            while min(*self.lat) < 0:
                self.lat[self.lat.index(min(*self.lat))] = max(*self.lat) + self.zoom[0]
            while max(*self.lat) > width:
                self.lat[self.lat.index(max(*self.lat))] = min(*self.lat) - self.zoom[0]
            while min(*self.longit) < 0:
                self.longit[self.longit.index(min(*self.longit))] = max(*self.longit) + self.zoom[0]
            while max(*self.longit) > height:
                self.longit[self.longit.index(max(*self.longit))] = min(*self.longit) - self.zoom[0]