mirror of
https://github.com/Febbweiss/mopidy-touchscreen.git
synced 2026-03-05 06:35:43 +00:00
Lower CPU usage
Screen will not be always updated
This commit is contained in:
54
mopidy_touchscreen/graphic_utils/background_manager.py
Normal file
54
mopidy_touchscreen/graphic_utils/background_manager.py
Normal file
@@ -0,0 +1,54 @@
|
||||
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
|
||||
|
||||
|
||||
def get_aspect_scale_size(img, (bx, by)):
|
||||
size = img.get_size()
|
||||
aspect_x = bx / float(size[0])
|
||||
aspect_y = by / 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
|
||||
Reference in New Issue
Block a user