import sys from Tkinter import PhotoImage import Globals class Shape: ######################################################################## # Constructor # def __init__(self, canvas, shapeType, x, y): self.__type = shapeType self.__canvas = canvas self.__x = x self.__y = y self.__height = 20 if (shapeType == Globals.CIRCLE): self.__width = 30 elif (shapeType == Globals.TRIANGLE): self.__width = 40 elif (shapeType == Globals.RECTANGLE): self.__width = 40 else: print("Error: " + shapeType + " is not a valid Shape type") sys.exit(0) ######################################################################## # Deletes the small squares # def __deleteBoundaries(self): if (self.__boundary1 != None and self.__boundary2 != None and self.__boundary3 != None and self.__boundary4 != None): self.__canvas.delete(self.__boundary1) self.__canvas.delete(self.__boundary2) self.__canvas.delete(self.__boundary3) self.__canvas.delete(self.__boundary4) ######################################################################## # Draws the shape in the given the color # def draw(self, color = "black"): if (self.__type == Globals.CIRCLE): self.__handle = self.__canvas.create_oval(self.__x, self.__y, self.__x + self.__width, self.__y + self.__height, outline = color) elif (self.__type == Globals.TRIANGLE): self.__handle = self.__canvas.create_polygon( self.__x - self.__width / 2, self.__y + self.__height / 2, self.__x, self.__y - self.__height / 2, self.__x + self.__width / 2, self.__y + self.__height / 2, fill = "", outline = color) elif (self.__type == Globals.RECTANGLE): self.__handle = self.__canvas.create_rectangle(self.__x, self.__y, self.__x + self.__width, self.__y + self.__height, outline = color) ######################################################################## # Returns the shape's type # def getType(self): return self.__type ######################################################################## # Deletes the shape from the canvas # def delete(self): self.__deleteBoundaries() self.__canvas.delete(self.__handle) ######################################################################## # Moves the shape around the canvas by x and y # def move(self, x, y): self.__deleteBoundaries() self.__canvas.move(self.__handle, x - self.__x, y - self.__y) self.__x = x self.__y = y self.select() ######################################################################## # Resizes the shape by x and y # def resize(self, corner, new_x, new_y): x1 = self.__x y1 = self.__y x2 = self.__x + self.__width y2 = self.__y + self.__height x2_tri = self.__x + self.__width / 2 y2_tri = self.__y + self.__height / 2 if corner == 1: self.__x = new_x + 8 self.__y = new_y + 8 if self.__type == Globals.TRIANGLE: self.__x = new_x + 8 + self.__width / 2 self.__y = new_y + 8 + self.__height / 2 if self.__x < 10: self.__x = 10 if self.__y < 10: self.__y = 10 if self.__type == Globals.TRIANGLE: self.__height = (y2_tri - self.__y) * 2 self.__width = (x2_tri - self.__x) * 2 else: self.__width = x2 - self.__x self.__height = y2 - self.__y elif corner == 2: if self.__type == Globals.TRIANGLE: self.__y = new_y + 8 + self.__height / 2 else: self.__y = new_y + 8 if self.__y < 10: self.__y = 10 self.__width = new_x - 8 - self.__x if self.__type == Globals.TRIANGLE: self.__height = (y2_tri - self.__y) * 2 self.__width *= 2 else: self.__height = y2 - self.__y if self.__x + self.__width > Globals.CANVAS_WIDTH - 10: self.__width = Globals.CANVAS_WIDTH - self.__x - 10 elif corner == 3: self.__x = new_x + 8 if self.__type == Globals.TRIANGLE: self.__x = new_x + 8 + self.__width / 2 self.__width = x2 - self.__x self.__height = new_y - 8 - self.__y if self.__type == Globals.TRIANGLE: self.__height *= 2 self.__width = (x2_tri - self.__x) * 2 elif corner == 4: self.__width = new_x - 8 - self.__x self.__height = new_y - 8 - self.__y if self.__type == Globals.TRIANGLE: self.__height *= 2 self.__width *= 2 if self.__x + self.__width > Globals.CANVAS_WIDTH - 10: self.__width = Globals.CANVAS_WIDTH - self.__x - 10 else: return if self.__width <= 1: self.__width = 1 self.__x = x1 if self.__height <= 1: self.__height = 1 self.__y = y1 self.__deleteBoundaries() self.select() ######################################################################## # Determines whether the given x and y are for a point inside the # shape's boundary. # def isSelected(self, x, y): x_org = self.__x y_org = self.__y if self.__type == Globals.TRIANGLE: x_org -= self.__width / 2 y_org -= self.__height / 2 selected = (x >= x_org - 10 and x <= x_org + self.__width + 10 and y >= y_org - 10 and y <= y_org + self.__height + 10) return selected ######################################################################## # Anotates the shape as selected by drawing it in red and drawing small # squares at each corner of the boundary # def select(self): # draw the shape in red self.__canvas.delete(self.__handle) self.draw("red"); x_org = self.__x y_org = self.__y if self.__type == Globals.TRIANGLE: x_org -= self.__width / 2 y_org -= self.__height / 2 # draw small square at the corner of the boundary self.__boundary1 = self.__canvas.create_rectangle( x_org - 10, y_org - 10, x_org - 5, y_org - 5) self.__boundary2 = self.__canvas.create_rectangle( x_org + self.__width + 10, y_org - 10, x_org + self.__width + 5, y_org - 5) self.__boundary3 = self.__canvas.create_rectangle( x_org - 5, y_org + self.__height + 10, x_org - 10, y_org + self.__height + 5) self.__boundary4 = self.__canvas.create_rectangle( x_org + self.__width + 5, y_org + self.__height + 10, x_org + self.__width + 10, y_org + self.__height + 5) ######################################################################## # Deanotates the shape # def deselect(self): # draw the shape in black by deleting it first and then redrawing it self.delete() self.draw(); self.__deleteBoundaries() ######################################################################## # Determines which corner (i.e. boundary) the user clicked on # def whichCorner(self, x, y): x_org = self.__x y_org = self.__y if self.__type == Globals.TRIANGLE: x_org -= self.__width / 2 y_org -= self.__height / 2 if (x >= x_org - 10 and x <= x_org - 5 and y >= y_org - 10 and y <= y_org - 5): return 1 if (x >= x_org + self.__width + 5 and x <= x_org + self.__width + 10 and y >= y_org - 10 and y <= y_org - 5): return 2 if (x >= x_org - 10 and x <= x_org - 5 and y >= y_org + self.__height + 5 and y <= y_org + self.__height + 10): return 3 if (x >= x_org + self.__width + 5 and x <= x_org + self.__width + 10 and y >= y_org + self.__height + 5 and y <= y_org + self.__height + 10): return 4