''' SAL's presentation viewer animation effects (c) 2005,2020 Jan ONDREJ (SAL) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. ''' import pygame import time import random ALL_EFFECTS = [ 'fade', 'fadeover', 'zoom', 'zoom_up', 'zoom_half', 'zoom_down', 'fold', 'move', 'open', 'close', 'open_black', 'close_black', 'close_open', 'bricks', 'bricks_black', 'blind', 'blind_black', # 'circles','circles_black', # 'quadrangle','quadrangle_black' ] RANDOM_EFFECTS = \ [ALL_EFFECTS[0]] + \ random.Random().sample(ALL_EFFECTS[1:], len(ALL_EFFECTS)-1) IN = 10000 OUT = 20000 class nothing: sleep = 0.01 step = 25 def __init__(self, size): self.size = size self.pg_num_old = -1 self.counter = 0 self.screen_old = pygame.Surface(self.size) self.screen_cur = pygame.display.get_surface() def begin(self, screen_new, pg_num): self.screen_new = screen_new self.pg_num = pg_num self.temp = {} def done(self): # display current image self.screen_cur.blit(self.screen_new, (0, 0)) pygame.display.flip() # store old image self.screen_old.blit(self.screen_new, (0, 0)) self.pg_num_old = self.pg_num def copyfrom(self, effect): self.screen_old = effect.screen_old self.pg_num_old = effect.pg_num_old def do(self, inout=0): self.done() # alias none = nothing class black(nothing): sleep = 0.005 def parent_out(self): self.parent(IN) def parent_in(self): self.parent(OUT) def do(self, inout=0): self.backup_new = self.screen_new self.backup_old = self.screen_old self.backup_black = pygame.Surface(self.size) self.backup_black.fill((0, 0, 0)) # prepare self.screen_new = self.backup_black.copy() try: self.prepare(IN) except AttributeError: pass self.screen_new = self.backup_new self.screen_old = self.backup_black.copy() try: self.prepare(OUT) except AttributeError: pass # do it self.screen_new = self.backup_black.copy() self.screen_old = self.backup_old self.parent_out() self.screen_new = self.backup_new self.screen_old = self.backup_black self.parent_in() # done self.done() class zoom(nothing): def pos(self, x): return ((self.size[0]-self.size[0]*x/self.step)/2, (self.size[1]-self.size[1]*x/self.step)/2) def do(self, inout=0): self.screen_cur.blit(self.screen_old, (0, 0)) for x in range(1, self.step+1): img = pygame.transform.scale(self.screen_new, (self.size[0]*x//self.step, self.size[1]*x//self.step)) self.screen_cur.blit(img, self.pos(x)) pygame.display.flip() time.sleep(self.sleep) self.done() class zoom_up(zoom): def pos(self, x): if self.pg_num_old > self.pg_num: return (0, 0) elif self.pg_num_old < self.pg_num: return (self.size[0]-self.size[0]*x/self.step, 0) class zoom_half(zoom): def pos(self, x): if self.pg_num_old > self.pg_num: return (0, (self.size[1]-self.size[1]*x/self.step)/2) elif self.pg_num_old < self.pg_num: return (self.size[0]-self.size[0]*x/self.step, (self.size[1]-self.size[1]*x/self.step)/2) class zoom_down(zoom): def pos(self, x): if self.pg_num_old > self.pg_num: return (0, self.size[1]-self.size[1]*x/self.step) elif self.pg_num_old < self.pg_num: return (self.size[0]-self.size[0]*x/self.step, self.size[1]-self.size[1]*x/self.step) class fold(nothing): def pos(self, x): return self.size[0]*x/self.step def do(self, inout=0): self.screen_cur.blit(self.screen_old, (0, 0)) for x in range(1, self.step+1): if self.pg_num_old < self.pg_num: self.screen_cur.blit( self.screen_new, (self.size[0]-self.pos(x), 0)) elif self.pg_num_old > self.pg_num: self.screen_cur.blit( self.screen_new, (self.pos(x)-self.size[0], 0)) pygame.display.flip() time.sleep(self.sleep) self.done() class move(nothing): def pos(self, x): return self.size[0]*x/self.step def do(self, inout=0): for i in range(1, self.step+1): x = self.pos(i) if self.pg_num_old < self.pg_num: self.screen_cur.blit(self.screen_old, (-x, 0)) self.screen_cur.blit(self.screen_new, (self.size[0]-x, 0)) elif self.pg_num_old > self.pg_num: self.screen_cur.blit(self.screen_old, (x, 0)) self.screen_cur.blit(self.screen_new, (x-self.size[0], 0)) pygame.display.flip() time.sleep(self.sleep) self.done() class random(nothing): def do(self, inout=0): if self.pg_num == self.pg_num_old: return effect = ALL_EFFECTS[int(random.Random().random()*len(ALL_EFFECTS))] re = eval(effect+'('+str(self.size)+')') re.screen_old = self.screen_old re.pg_num_old = self.pg_num_old re.begin(self.screen_new, self.pg_num) re.do(inout) self.done() class all(nothing): def do(self, inout=0): if self.pg_num == self.pg_num_old: return effect = ALL_EFFECTS[self.counter] self.counter += 1 if self.counter >= len(ALL_EFFECTS): self.counter = 0 re = eval(effect+'('+str(self.size)+')') re.screen_old = self.screen_old re.pg_num_old = self.pg_num_old re.begin(self.screen_new, self.pg_num) re.do(inout) self.done() class any(nothing): def do(self, inout=0): if self.pg_num == self.pg_num_old: return effect = RANDOM_EFFECTS[self.counter] self.counter += 1 if self.counter >= len(ALL_EFFECTS): self.counter = 0 re = eval(effect+'('+str(self.size)+')') re.screen_old = self.screen_old re.pg_num_old = self.pg_num_old re.begin(self.screen_new, self.pg_num) re.do(inout) self.done() class fadeover(nothing): def do(self, inout=0): alpha = pygame.Surface(self.size) for i in range(20): alpha.blit(self.screen_new, (0, 0)) alpha.set_alpha(255/20*i) self.screen_cur.blit(self.screen_old, (0, 0)) self.screen_cur.blit(alpha, (0, 0)) pygame.display.flip() time.sleep(self.sleep) self.done() class fade(black, fadeover): def parent(self, inout=0): fadeover.do(self, inout) class open(nothing): def do(self, inout=0): self.screen_cur.blit(self.screen_old, (0, 0)) for i in range(20): y1 = self.size[1]/40*(20-i) rect = pygame.Rect(0, y1, self.size[0], self.size[1]-2*y1) sf = self.screen_new.subsurface(rect) self.screen_cur.blit(sf, (0, y1)) pygame.display.flip() time.sleep(self.sleep) self.done() class close(nothing): def do(self, inout=0): self.screen_cur.blit(self.screen_old, (0, 0)) for i in range(20): y1 = self.size[1]/40*i rect = pygame.Rect(0, 0, self.size[0]-1, y1) sf = self.screen_new.subsurface(rect) self.screen_cur.blit(sf, (0, 0)) rect = pygame.Rect(0, self.size[1]-y1, self.size[0]-1, y1) sf = self.screen_new.subsurface(rect) self.screen_cur.blit(sf, (0, self.size[1]-y1)) pygame.display.flip() time.sleep(self.sleep) self.done() class open_black(black, open): def parent(self, inout=0): open.do(self, inout) class close_black(black, close): def parent(self, inout=0): close.do(self, inout) class close_open(black, open, close): def parent_out(self): close.do(self, IN) def parent_in(self): open.do(self, OUT) class bricks(nothing): step = 20 def rect(self, x, y, size): # print x+self.step/2-size/2,y+self.step/2.0-size/2.0,size return x+self.step//2-size//2, y+self.step//2-size//2, \ pygame.Rect(x+self.step//2-size//2, y+self.step//2-size//2, size, size) def do(self, inout=0): for i in range(1, self.step, 2): for y in range(0, self.size[1], self.step): for x in range(0, self.size[0], self.step): x0, y0, r = self.rect(x, y, i) if y0+i >= self.size[1]: continue sf = self.screen_new.subsurface(r) self.screen_cur.blit(sf, (x0, y0)) pygame.display.flip() time.sleep(self.sleep) self.done() class bricks_black(black, bricks): def parent(self, inout=0): bricks.do(self, inout) class blind(nothing): step = 20 def do(self, inout=0): for i in range(1, self.step, 2): for y in range(0, self.size[1]-self.step, self.step): r = pygame.Rect(0, y, self.size[0]-1, i) sf = self.screen_new.subsurface(r) self.screen_cur.blit(sf, (0, y)) pygame.display.flip() time.sleep(self.sleep) self.done() class blind_black(black, blind): def parent(self, inout=0): blind.do(self, inout) class circles(nothing): transparent_color = (1, 1, 1) def draw(self, surface, x, y, r): pygame.draw.circle(surface, self.transparent_color, (x, y), r) def prepare(self, inout=0): for i in range(self.step*2, 1, -2): self.temp[inout+i] = self.screen_new.copy() self.temp[inout+i].set_alpha(None) self.temp[inout+i].set_colorkey(self.transparent_color) for y in range(0, self.size[1]-1, self.step): for x in range(0, self.size[0]-1, self.step): self.draw(self.temp[inout+i], x, y, i) def do(self, inout=0): for i in range(self.step*2, 1, -2): self.screen_cur.blit(self.temp[inout+i], (0, 0)) pygame.display.flip() time.sleep(self.sleep/2) self.done() class circles_black(black, circles): def parent(self, inout=0): circles.do(self, inout) class quadrangle(circles): def draw(self, surface, x, y, r): pygame.draw.polygon(surface, self.transparent_color, ((x-r, y), (x, y-r), (x+r, y), (x, y+r), (x-r, y))) class quadrangle_black(black, quadrangle): def parent(self, inout=0): quadrangle.do(self, inout)