Files
mopidy-touchscreen/mopidy_touchscreen/graphic_utils/background_manager.py
2015-05-23 15:04:02 +02:00

55 lines
1.5 KiB
Python

import pygame
change_speed = 2
class DynamicBackground:
def __init__(self, size):
self.image_loaded = False
self.size = size
self.surface = pygame.Surface(self.size).convert()
self.surface.fill((145, 16, 16))
self.surface_image = pygame.Surface(self.size).convert()
self.update = True
def draw_background(self):
if self.image_loaded:
return self.surface_image.copy()
else:
return self.surface.copy()
def should_update(self):
if self.update:
self.update = False
return True
else:
return False
def set_background_image(self, image):
if image is not None:
image_size = get_aspect_scale_size(image, self.size)
target = pygame.transform.smoothscale(image, image_size)
target.set_alpha(150)
self.image_loaded = True
self.surface_image.fill((0, 0, 0))
pos = ((self.size[0] - image_size[0])/2,
(self.size[1] - image_size[1])/2)
self.surface_image.blit(target, pos)
else:
self.image_loaded = False
self.update = True
@staticmethod
def get_aspect_scale_size(img, size):
size = img.get_size()
aspect_x = size[0] / float(size[0])
aspect_y = size[1] / float(size[1])
if aspect_x > aspect_y:
aspect = aspect_x
else:
aspect = aspect_y
new_size = (int(aspect*size[0]), int(aspect*size[1]))
return new_size