diff --git a/.gitignore b/.gitignore index abc5026..79e0d34 100644 --- a/.gitignore +++ b/.gitignore @@ -1,8 +1,9 @@ *.egg-info *.pyc *.swp +*~ .coverage .tox/ MANIFEST build/ -dist/ \ No newline at end of file +dist/ diff --git a/mopidy_touchscreen/.idea/workspace.xml b/mopidy_touchscreen/.idea/workspace.xml index 9c4e301..3c4cc4f 100644 --- a/mopidy_touchscreen/.idea/workspace.xml +++ b/mopidy_touchscreen/.idea/workspace.xml @@ -26,23 +26,9 @@ - - - - - - - - - - - - - - - - - + + + @@ -50,7 +36,7 @@ - + @@ -356,19 +342,7 @@ - - - - - - - - - - - - - + @@ -394,19 +368,7 @@ - - - - - - - - - - - - - + @@ -432,19 +394,7 @@ - - - - - - - - - - - - - + @@ -470,19 +420,7 @@ - - - - - - - - - - - - - + @@ -500,16 +438,6 @@ - - - - - - - - - - @@ -524,23 +452,19 @@ - + - - - - - + + + - - - - - + + + diff --git a/mopidy_touchscreen/main_screen.py b/mopidy_touchscreen/main_screen.py index 379d85f..fe83580 100644 --- a/mopidy_touchscreen/main_screen.py +++ b/mopidy_touchscreen/main_screen.py @@ -8,7 +8,7 @@ import urllib import urllib2 import json from mopidy.audio import PlaybackState -from .touch_text_manager import TouchTextManager +from .screen_objects import ScreenObjectsManager from .dynamic_background import DynamicBackground logger = logging.getLogger(__name__) @@ -24,7 +24,7 @@ class MainScreen(): self.track = None self.cache = cache self.image = None - self.touch_text_manager = TouchTextManager(size,self.base_size) + self.touch_text_manager = ScreenObjectsManager(size,self.base_size) self.touch_text_manager.add_touch_object("pause_play","Play",(0,0),(255,255,255)) diff --git a/mopidy_touchscreen/screen_objects.py b/mopidy_touchscreen/screen_objects.py new file mode 100644 index 0000000..a96811c --- /dev/null +++ b/mopidy_touchscreen/screen_objects.py @@ -0,0 +1,155 @@ +import pygame +import logging + +logger = logging.getLogger(__name__) + +class ScreenObjectsManager(): + + + + def __init__(self,size,base_size): + self.size = size + self.base_size = base_size + self.touch_objects = {} + self.text_objects = {} + + def add_object(self, key, text, pos, pos2, color): + self.text_objects[key] = TextItem(text, pos,pos2,color,self.base_size) + + def get_object(self, key): + return self.text_objects[key] + + def add_touch_object(self, key, text, pos, color): + self.touch_objects['key'] = TouchAndTextItem(text, pos, color, self.base_size) + + def get_touch_object(self,key): + return self.touch_objects['key'] + + def add_progressbar(self, key, pos, pos2, max): + self.touch_objects['key'] = Progressbar(pos,pos2,max) + + def render(self, surface): + for key in self.text_objects: + self.text_objects[key].update() + self.text_objects[key].render(surface) + for key in self.touch_objects: + self.touch_objects[key].render(surface) + + +class BaseItem(): + + def __init__(self,pos,pos2): + self.pos = pos + self.pos2 = pos2 + self.size=[0,0] + self.size[0] = self.pos2[0] - self.pos[0] + self.size[1] = self.pos2[1] - self.pos[1] + self.rect = pygame.Rect(0,0,self.size[0],self.size[1]) + + +class TextItem(BaseItem): + + def __init__(self, text, pos,pos2, color,text_size): + if pos2 is not None: + BaseItem.__init__(self,pos,pos2) + self.text_size = text_size + self.font = pygame.font.SysFont("arial", text_size) + self.text = text + self.color = color + self.box = self.font.render(text, True, self.color) + if pos2 is not None: + if self.pos[0] + self.box.get_rect().width > pos2[0]: + self.fit_horizontal = False + self.text = self.text + " " + self.original_text = self.text + self.step = 0 + else: + self.fit_horizontal = True + if self.pos[1] + self.box.get_rect().height > pos2[1]: + self.fit_vertical = False + else: + self.fit_vertical = True + else: + BaseItem.__init__(self,pos,(pos[0]+self.box.get_rect().width,pos[1]+self.box.get_rect().height)) + self.fit_horizontal = True + self.fit_vertical = True + + + def update(self): + if not self.fit_horizontal: + if self.text == self.original_text: + if self.step > 90: + self.step = 0 + new_text = self.text[1:] + new_text = new_text + self.text[:1] + self.text = new_text + else: + self.step = self.step + 1 + elif self.step > 5: + self.step = 0 + new_text = self.text[1:] + new_text = new_text + self.text[:1] + self.text = new_text + else: + self.step = self.step + 1 + + def render(self,surface): + if self.fit_horizontal: + self.box + else: + self.box = self.font.render(self.text, True, self.color) + surface.blit(self.box,self.pos,area=self.rect) + + def set_text(self, text): + self.__init__(text,self.pos,self.pos2,self.color,self.text_size) + +class TouchObject(BaseItem): + + def __init__(self,pos,pos2): + BaseItem.__init__(self,pos,pos2) + self.active = False + self.background_box = pygame.Surface(self.size) + self.background_box.fill((0,128,255)) + + def render(self,surface): + surface.blit(self.background_box, self.pos) + + +class TouchAndTextItem(TouchObject, TextItem): + + def __init__(self, text, pos, color,text_size): + TextItem.__init__(self,text, pos,None, color,text_size) + TouchObject.__init__(self,pos,self.pos2) + + def update(self): + TextItem.update() + + def render(self,surface): + TouchObject.render(self,surface) + TextItem.render(self,surface) + +class Progressbar(BaseItem): + + def __init__(self, pos, pos2, max): + BaseItem.__init__(self, pos, pos2) + logger.error(pos2) + self.value = 0 + self.max = max + self.back_color = (0,0,0,128) + self.main_color = (255,255,255) + self.surface = pygame.Surface(self.size, pygame.SRCALPHA) + self.surface.fill(self.back_color) + + def update(self): + pass + + def render(self, surface): + surface.blit(self.surface, self.pos) + + def set_value(self, value): + self.value = value + self.surface.fill(self.back_color) + pos_pixel = value * self.size[0] / self.max + rect = pygame.Rect(0,0,pos_pixel,self.size[1]) + self.surface.fill(self.main_color, rect) +