mirror of
https://github.com/Febbweiss/mopidy-touchscreen.git
synced 2026-03-04 22:25:39 +00:00
Merge branch 'background-cover' into develop
Conflicts: mopidy_touchscreen/graphic_utils/dynamic_background.py mopidy_touchscreen/graphic_utils/screen_objects.py mopidy_touchscreen/screen_manager.py mopidy_touchscreen/screens/main_screen.py
This commit is contained in:
@@ -1,35 +1,44 @@
|
||||
import random
|
||||
|
||||
import pygame
|
||||
|
||||
|
||||
change_speed = 2
|
||||
|
||||
|
||||
class DynamicBackground():
|
||||
def __init__(self):
|
||||
|
||||
def __init__(self, size):
|
||||
self.current = get_valid_color()
|
||||
self.target = get_valid_color()
|
||||
self.auto_mode = True
|
||||
self.image_loaded = False
|
||||
self.size = size
|
||||
self.surface = pygame.Surface(self.size).convert()
|
||||
self.target_current_same = False
|
||||
|
||||
def draw_background(self, surfaces):
|
||||
if not self.target_current_same:
|
||||
for x in range(0, 3):
|
||||
if abs(self.current[x]-self.target[x]) < change_speed:
|
||||
self.current[x] = self.target[x]
|
||||
self.target_current_same = True
|
||||
else:
|
||||
self.target_current_same = False
|
||||
if self.current[x] > self.target[x]:
|
||||
self.current[x] -= change_speed
|
||||
elif self.current[x] < self.target[x]:
|
||||
self.current[x] += change_speed
|
||||
if self.auto_mode and self.target_current_same:
|
||||
self.target = get_valid_color()
|
||||
self.target_current_same = False
|
||||
for surface in surfaces:
|
||||
surface.fill(self.current)
|
||||
def draw_background(self):
|
||||
if self.image_loaded:
|
||||
return self.surface.copy()
|
||||
else:
|
||||
if not self.target_current_same:
|
||||
for x in range(0, 3):
|
||||
if abs(self.current[x]-self.target[x]) < change_speed:
|
||||
self.current[x] = self.target[x]
|
||||
self.target_current_same = True
|
||||
else:
|
||||
self.target_current_same = False
|
||||
if self.current[x] > self.target[x]:
|
||||
self.current[x] -= change_speed
|
||||
elif self.current[x] < self.target[x]:
|
||||
self.current[x] += change_speed
|
||||
if self.auto_mode and self.target_current_same:
|
||||
self.target = get_valid_color()
|
||||
self.target_current_same = False
|
||||
self.surface.fill(self.current)
|
||||
return self.surface.copy()
|
||||
|
||||
def set_target_color(self, color):
|
||||
def set_target_color(self, color, image):
|
||||
if color is not None:
|
||||
self.auto_mode = False
|
||||
self.target_current_same = False
|
||||
@@ -39,6 +48,18 @@ class DynamicBackground():
|
||||
self.target = get_valid_color()
|
||||
self.target_current_same = False
|
||||
|
||||
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.fill((0, 0, 0))
|
||||
pos = ((self.size[0] - image_size[0])/2,
|
||||
(self.size[1] - image_size[1])/2)
|
||||
self.surface.blit(target, pos)
|
||||
else:
|
||||
self.image_loaded = False
|
||||
|
||||
# It will return the same color if sum is less than 510
|
||||
# Otherwise a darker color will be returned
|
||||
# White text should be seen ok with this background color
|
||||
@@ -72,3 +93,17 @@ def get_valid_color():
|
||||
i = random.randint(0, 2)
|
||||
color[i] -= extra
|
||||
return color
|
||||
|
||||
|
||||
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
|
||||
|
||||
|
||||
@@ -91,6 +91,7 @@ class TextItem(BaseItem):
|
||||
self.text = text
|
||||
self.color = (255, 255, 255)
|
||||
self.box = self.font.render(text, True, self.color)
|
||||
self.box = self.box.convert_alpha()
|
||||
if size is not None:
|
||||
if size[1] == -1:
|
||||
height = self.font.size(text)[1]
|
||||
@@ -174,9 +175,11 @@ class TouchObject(BaseItem):
|
||||
self.active = False
|
||||
self.selected = False
|
||||
self.selected_box = pygame.Surface(size, pygame.SRCALPHA)
|
||||
self.selected_box = self.selected_box.convert_alpha()
|
||||
self.selected_box.fill((0, 0, 0, 128))
|
||||
self.selected_box_rectangle = pygame.Surface(size,
|
||||
pygame.SRCALPHA)
|
||||
self.selected_box_rectangle = pygame.Surface(size, pygame.SRCALPHA)
|
||||
self.selected_box_rectangle = \
|
||||
self.selected_box_rectangle.convert_alpha()
|
||||
pygame.draw.rect(self.selected_box_rectangle, (255, 255, 255),
|
||||
self.selected_box_rectangle.get_rect(),
|
||||
size[1]/10+1)
|
||||
@@ -242,7 +245,8 @@ class Progressbar(TouchObject):
|
||||
self.max = max_value
|
||||
self.back_color = (0, 0, 0, 128)
|
||||
self.main_color = (0, 150, 255)
|
||||
self.surface = pygame.Surface(self.size, pygame.SRCALPHA)
|
||||
self.surface = pygame.Surface(self.size, pygame.SRCALPHA)\
|
||||
.convert_alpha()
|
||||
self.surface.fill(self.back_color)
|
||||
self.value_text = value_text
|
||||
if value_text:
|
||||
@@ -259,8 +263,8 @@ class Progressbar(TouchObject):
|
||||
2, self.text.pos[1])
|
||||
|
||||
# Rectangle
|
||||
self.rectangle = pygame.Surface(size,
|
||||
pygame.SRCALPHA)
|
||||
self.rectangle = pygame.Surface(size, pygame.SRCALPHA)\
|
||||
.convert_alpha()
|
||||
pygame.draw.rect(self.rectangle, (255, 255, 255),
|
||||
self.rectangle.get_rect(),
|
||||
size[1]/20+1)
|
||||
@@ -301,7 +305,8 @@ class ScrollBar(TouchObject):
|
||||
self.max = max_value
|
||||
self.items_on_screen = items_on_screen
|
||||
self.current_item = 0
|
||||
self.back_bar = pygame.Surface(self.size, pygame.SRCALPHA)
|
||||
self.back_bar = pygame.Surface(self.size, pygame.SRCALPHA)\
|
||||
.convert_alpha()
|
||||
self.back_bar.fill((255, 255, 255, 128))
|
||||
self.bar_pos = 0
|
||||
if self.max < 1:
|
||||
@@ -310,7 +315,7 @@ class ScrollBar(TouchObject):
|
||||
self.bar_size = math.ceil(
|
||||
float(self.items_on_screen) / float(self.max) * float(
|
||||
self.size[1]))
|
||||
self.bar = pygame.Surface((self.size[0], self.bar_size))
|
||||
self.bar = pygame.Surface((self.size[0], self.bar_size)).convert()
|
||||
self.bar.fill((255, 255, 255))
|
||||
|
||||
def render(self, surface):
|
||||
|
||||
Reference in New Issue
Block a user