def drawSearched(): #draw searched images draw.rect(screen, c("white"), imgsearch) draw.rect(screen, c("blue"), searchbtn) screen.blit(searchicon, (200 + searchbarx, searchbary)) if text != '': #only render font if it exists searchinput = searchfont.render(text, True, c("black")) screen.blit(searchinput, (searchbarx + 10, searchbarx + 15)) if modes[mode] == "searching": draw.rect(screen, c("red"), imgsearch, 3) else: draw.rect(screen, c("black"), imgsearch, 3) #bliting searched images for i in range(len(imglist)): screen.blit(imglist[i], (searchbarx, searchbarx + i * 140 + 65))
def showinfo(mx, my): #shows on the bottom right, info about the current tool, current colour, mouse position, and playing track #shows position relative to canvas left, top, width, height = canvas mx = mx - left my = my - top #make sure coordinates are not negative if mx < 0: mx = 0 elif mx > left + width: mx = left + width if my < 0: my = 0 elif my > top + height: my = top + height text = f'Current tool: {modes[mode]} Current colour: {(chosencolour[0],chosencolour[1],chosencolour[2])} Mouse position: {mx}, {my} Current Song: {musicnames[cursong]}' text = infofont.render(text, True, c("white")) screen.blit(text, (1175 - text.get_size()[0], 682))
def main(): #main program loop running = True clock = time.Clock() while running: mx, my = mouse.get_pos() mb = mouse.get_pressed() kp = key.get_pressed() screen.set_clip() if not action: arrowchangedrawwidth(kp) ########################### event handling, calculations ########################### for evt in event.get(): if evt.type == QUIT: running = handleQuit() if evt.type == MOUSEBUTTONDOWN: #mouse down event handleMousePress(evt) handleMusic(evt) if evt.type == MOUSEBUTTONUP: #button up event handleMouseUp(evt, mx, my) if evt.type == KEYDOWN: #keydown event handleKeydown(evt) if evt.type == MOUSEMOTION: #only used for image movement if imgmoving == 1: imgrect.move_ip(evt.rel) if evt.type == MUSICEND: #self defined event: when the music finishes handleMusicNext() ########################### draw stuff ########################### #draw background rects drawBG() #search section drawSearched() #draw buttons drawButtons() #draw title screen.blit(title, (483, 520)) #show info showinfo(mx, my) #draw canvas screen.set_clip( Rect(canvas[0], canvas[1], canvas[2] + 1, canvas[3] + 1)) screen.blit(screencap, (475, 25)) draw.rect(screen, c("black"), canvas, 2) #show action showaction(mb, mx, my) #draw cursor blitcursor(mx, my) ########################### adjust framerate, update ########################### clock.tick(120) #control framerate display.flip() #update display screen.blit( screencap, (475, 25) ) #after showing action, erase what was shown in showaction() so that its temporary
def drawButtons(): #draw all the buttons global mode, playnextborder, playprevborder, borderframecount for b in buttons: draw.rect(screen, c("white"), b.rect) screen.blit(woodenbtn, b.rect) screen.blit(buttonimgs[b.mode - 2], b.rect) if mode == b.mode: draw.rect(screen, c("red"), b.rect, 2) screen.blit(colours, cprect) draw.rect(screen, chosencolour, (400 - drawwidth, 435 - drawwidth, drawwidth * 2, drawwidth * 2)) draw.rect(screen, c("black"), cprect, 2) draw.rect(screen, c("black"), (400 - drawwidth, 435 - drawwidth, drawwidth * 2, drawwidth * 2), 2) draw.rect(screen, c("white"), playerrect) #player draw.rect(screen, c("black"), playrect) #play draw.rect(screen, c("black"), prevrect) #previous draw.rect(screen, c("black"), nextrect) #next screen.blit(playerimg, playerrect) screen.blit(cur, playrect) screen.blit(previmg, prevrect) screen.blit(nextimg, nextrect) screen.blit(inorderimg, inorderrect) screen.blit(loopimg, looprect) screen.blit(shuffleimg, shufflerect) draw.rect(screen, c("black"), inorderrect, 2) draw.rect(screen, c("black"), looprect, 2) draw.rect(screen, c("black"), shufflerect, 2) draw.rect(screen, c("black"), playerrect, 2) draw.rect(screen, c("black"), prevrect, 2) draw.rect(screen, c("black"), nextrect, 2) if musicmode == "inorder": draw.rect(screen, c("red"), inorderrect, 2) elif musicmode == "loop": draw.rect(screen, c("red"), looprect, 2) elif musicmode == "shuffle": draw.rect(screen, c("red"), shufflerect, 2) if playnextborder: if borderframecount != 0: draw.rect(screen, c("red"), nextrect, 2) borderframecount -= 1 else: playnextborder = 0 if playprevborder: if borderframecount != 0: draw.rect(screen, c("red"), prevrect, 2) borderframecount -= 1 else: playprevborder = 0
def showaction(mb, mx, my): #show temporarily what the user is doing (depending on the mode) if action: if mb[0]: #only left clicks left, top = min(startx, mx), min(starty, my) rwidth, rheight = abs(mx - startx), abs(my - starty) if modes[mode] == "pencil": global pencilcircles pencilcircles = addstroke(mx, my, pencilcircles) brush(pencilcircles, 1, c("black")) elif modes[mode] == "eraser": global erasercircles erasercircles = addstroke(mx, my, erasercircles) brush(erasercircles, drawwidth, c("white")) elif modes[mode] == "brush": global brushcircles brushcircles = addstroke(mx, my, brushcircles) brush(brushcircles, drawwidth, chosencolour) elif modes[mode] == "spraypaint": global spraylist, spraycirclelist l = len(spraycirclelist) spraycirclelist = addstroke(mx, my, spraycirclelist) for x, y in spraycirclelist[l:]: tempspraypoints = randpoints(x, y) spraylist += tempspraypoints for px, py in spraylist: screen.set_at((px, py), chosencolour) #draw.circle(screen, chosencolour, (px,py), 0.5) elif modes[mode] == "highlight": global highlightlist, alphasf highlightlist = addstroke(mx, my, highlightlist) for x, y in highlightlist: draw.circle(alphasf, (chosencolour[0], chosencolour[1], chosencolour[2], 100), (x - 475, y - 25), drawwidth) screen.blit(alphasf, (475, 25)) elif modes[mode] == "line": drawline(startx, starty, mx, my) elif modes[mode] == "rect": draw.rect(screen, chosencolour, (left, top, rwidth, rheight)) elif modes[mode] == "ellipse": draw.ellipse(screen, chosencolour, (left, top, rwidth, rheight)) elif modes[mode] == "unfilled rect": draw.rect(screen, chosencolour, (left, top, rwidth, drawwidth)) draw.rect(screen, chosencolour, (left, top, drawwidth, rheight)) draw.rect(screen, chosencolour, (left + rwidth - drawwidth, top, drawwidth, rheight)) draw.rect(screen, chosencolour, (left, top + rheight - drawwidth, rwidth, drawwidth)) elif modes[mode] == "unfilled ellipse": for i in range(-1, 2): try: draw.ellipse(screen, chosencolour, (left - i, top - i, rwidth + 2 * i, rheight + 2 * i), min(drawwidth, min(rwidth, rheight) // 2)) except: pass #resizing and moving images if modes[mode] == "import": global importimg, imgrect if imgresize == 1: #if they are currently resizing, perform transformation importimg = transform.scale( ogimportimg, (max(0, mx - imgrect[0]), max(0, my - imgrect[1]))) imgrect = Rect(imgrect[0], imgrect[1], max(0, mx - imgrect[0]), max(0, my - imgrect[1])) draw.rect(screen, c("black"), imgrect, 3) screen.blit(importimg, imgrect) draw.circle(screen, c("green"), (imgrect[0] + imgrect[2], imgrect[1] + imgrect[3]), 5) draw.circle(screen, c("black"), (imgrect[0] + imgrect[2], imgrect[1] + imgrect[3]), 5, 1) #canvas border to look nice draw.rect(screen, c("black"), canvas, 2)
def __init__(self, dim): self.w = dim[0] self.h = dim[1] self.surface = Surface(dim) self.surface.set_alpha(100) self.surface.fill(c("black"))
def handleMouseUp(evt, mx, my): #handle MOUSEBUTTONUP event. permanently completes the drawing actions, depending on the mode. global drawwidth, importimg, imgrect, imgmoving, imgresize, mode if not imgrect.collidepoint( mx, my) and hypot(abs(evt.pos[0] - imgrect[0] - imgrect[2]), abs(evt.pos[1] - imgrect[1] - imgrect[3])) > 5: if imgmoving == 1: imgmoving = 0 if action: #check if there is canvas action, if so draw according to the mode left, top = min(startx, mx), min(starty, my) rwidth, rheight = abs(mx - startx), abs(my - starty) if modes[mode] == "pencil": global pencilcircles brush(pencilcircles, 1, c("black")) pencilcircles = [] elif modes[mode] == "eraser": global erasercircles brush(erasercircles, drawwidth, c("white")) erasercircles = [] elif modes[mode] == "brush": global brushcircles brush(brushcircles, drawwidth, chosencolour) brushcircles = [] elif modes[mode] == "spraypaint": global spraylist, spraycirclelist for px, py in spraylist: screen.set_at((px, py), chosencolour) spraylist = [] spraycirclelist = [] elif modes[mode] == "highlight": global highlightlist, alphasf for x, y in highlightlist: draw.circle( alphasf, (chosencolour[0], chosencolour[1], chosencolour[2], 100), (x - 475, y - 25), drawwidth) screen.blit(alphasf, (475, 25)) highlightlist = [] alphasf = Surface((700, 450), SRCALPHA) elif modes[mode] == "fill bucket": global startcolour startcolour = screen.get_at((mx, my)) if startcolour != chosencolour: floodfill(mx, my) elif modes[mode] == "line": drawline(startx, starty, mx, my) elif modes[mode] == "rect": draw.rect(screen, chosencolour, (left, top, rwidth, rheight)) elif modes[mode] == "ellipse": draw.ellipse(screen, chosencolour, (left, top, rwidth, rheight)) elif modes[mode] == "unfilled rect": draw.rect(screen, chosencolour, (left, top, rwidth, drawwidth)) draw.rect(screen, chosencolour, (left, top, drawwidth, rheight)) draw.rect(screen, chosencolour, (left + rwidth - drawwidth, top, drawwidth, rheight)) draw.rect(screen, chosencolour, (left, top + rheight - drawwidth, rwidth, drawwidth)) elif modes[mode] == "unfilled ellipse": #drawing 3 overlapping ellipses makes the ellipse border look more filled for i in range(-1, 2): try: draw.ellipse( screen, chosencolour, (left - i, top - i, rwidth + 2 * i, rheight + 2 * i), min(drawwidth, min(rwidth, rheight) // 2)) except: pass #capture to make changes permanent capture() elif modes[ mode] == "clear screen": #outside of "if action" because there doesnt need to be canvas action, only button draw.rect(screen, c("white"), canvas) capture() #similarly with importing/exporting images, and undo/redo elif buttons[6].rect.collidepoint(evt.pos): try: importimg = image.load( filedialog.askopenfilename(initialdir="./images")) importimg.convert() global ogimportimg ogimportimg = importimg.copy( ) #this is an important step. keep a copy of the original image, and each time perform a transformation on the original copy, not a transformed copy. this perserves image quality significantly imgrect = importimg.get_rect() imgrect.center = 825, 250 screen.blit(importimg, (imgrect)) except Exception as e: print(e) elif modes[mode] == "export": global saved try: fname = filedialog.asksaveasfilename(defaultextension=".png", initialdir="./saved images/") image.save(screen.subsurface(canvas), fname) saved = 1 except Exception as e: print(e) elif modes[mode] == "undo": handleUndo() elif modes[mode] == "redo": handleRedo()
#import img variables if not path.exists("./images"): makedirs("./images") imgmoving = 0 imgresize = 0 imgrect = Rect(0, 0, 0, 0) importimg = Surface((0, 0)) #colour picker colours = image.load("static/colourpicker.jpg") cix, ciy = colours.get_size() ch = 180 colours = transform.scale(colours, (80, ch)) cprect = Rect(360, 485, 80, ch) chosencolrect = Rect(360, 395, 80, 80) chosencolour = c("white") #canvas undo = [] redo = [] canvas = Rect(475, 25, 700, 450) draw.rect(screen, (255, 255, 255), canvas) capture() #searchbar searchbarx, searchbary = 50, 50 imgsearch = Rect(searchbarx, searchbary, 200, 50) searchbtn = Rect(200 + searchbarx, searchbary, 50, 50) text = '' searchfont = font.SysFont("Calibri", 24)