Lower CPU usage

Screen will not be always updated
This commit is contained in:
Ander
2015-05-19 17:04:47 +02:00
parent 2ee9d41393
commit e98e198224
17 changed files with 325 additions and 174 deletions

View File

@@ -1,4 +1,4 @@
# flake8: noqa
from dynamic_background import DynamicBackground
from background_manager import DynamicBackground
from list_view import ListView
from screen_objects import *

View 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

View File

@@ -1,108 +0,0 @@
import random
import pygame
change_speed = 2
class DynamicBackground():
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):
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, image):
if color is not None:
self.auto_mode = False
self.target_current_same = False
self.target = get_similar_valid_color(color)
else:
self.auto_mode = True
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
def get_similar_valid_color(color):
sum = color[0] + color[1] + color[2]
new_color = [0, 0, 0]
if sum > 510:
rest = (sum - 510)/3 + 1
for x in range(0, 3):
new_color[x] = color[x] - rest
return new_color
else:
return color
# Returns an array with 3 integers in range of 0-255
# The sum of the three integers will be lower than 255*2
# (510) to avoid very bright colors
# White text should be seen ok with this background color
def get_valid_color():
color = [0, 0, 0]
total = 0
for i in range(0, 3):
color[i] = random.randint(0, 255)
total += color[i]
extra = total - 510
if extra > 0:
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

View File

@@ -23,6 +23,7 @@ class ListView():
self.selected = None
self.active = []
self.set_list([])
self.update_keys = []
# Sets the list for the lisview.
# It should be an iterable of strings
@@ -49,6 +50,7 @@ class ListView():
# Will load items currently displaying in item_pos
def load_new_item_position(self, item_pos):
self.update_keys = []
self.current_item = item_pos
if self.scrollbar:
self.screen_objects.clear_touch(["scrollbar"])
@@ -60,19 +62,36 @@ class ListView():
width = self.size[0] - self.base_size
else:
width = self.size[0]
self.should_update_always = False
while i < self.list_size and z < self.max_rows:
item = TouchAndTextItem(self.font, self.list[i], (
self.pos[0],
self.pos[1] + self.base_size * z), (width, -1))
if not item.fit_horizontal:
self.update_keys.append(str(i))
self.screen_objects.set_touch_object(str(i), item)
i += 1
z += 1
self.reload_selected()
def render(self, surface):
self.screen_objects.render(surface)
def should_update(self):
if len(self.update_keys) > 0:
return True
else:
return False
def render(self, surface, update_all, rects):
if update_all:
self.screen_objects.render(surface)
else:
for key in self.update_keys:
object = self.screen_objects.get_touch_object(key)
object.update()
object.render(surface)
rects.append(object.rect_in_pos)
def touch_event(self, touch_event):
self.must_update = True
if touch_event.type == InputManager.click \
or touch_event.type == InputManager.long_click:
objects = self.screen_objects.get_touch_objects_in_pos(
@@ -127,6 +146,7 @@ class ListView():
# Set active items
def set_active(self, active):
self.must_update = True
for number in self.active:
try:
self.screen_objects.get_touch_object(
@@ -144,6 +164,7 @@ class ListView():
self.active = active
def set_selected(self, selected):
self.must_update = True
if selected > -1 and selected < len(self.list):
if self.selected is not None:
try:
@@ -163,6 +184,7 @@ class ListView():
self.set_selected_on_screen()
def set_selected_on_screen(self):
self.must_update = True
if self.current_item + self.max_rows <= self.selected:
self.move_to(1)
self.set_selected_on_screen()
@@ -171,6 +193,7 @@ class ListView():
self.set_selected_on_screen()
def reload_selected(self):
self.must_update = True
if self.selected is not None:
try:
self.screen_objects.get_touch_object(

View File

@@ -79,12 +79,12 @@ class BaseItem():
return self.pos[0] + self.size[0]
def update(self):
pass
return False
class TextItem(BaseItem):
scroll_speed = 5
scroll_speed = 2
def __init__(self, font, text, pos, size, center=False, background=None):
self.font = font
@@ -228,7 +228,7 @@ class TouchAndTextItem(TouchObject, TextItem):
self.active_color)
def update(self):
TextItem.update(self)
return TextItem.update(self)
def set_text(self, text, change_size):
TextItem.set_text(self, text, change_size)