def test_add_surface_to_long_term_cache_too_large(
         self, _init_pygame, _display_surface_return_none):
     cache = SurfaceCache()
     with pytest.warns(UserWarning,
                       match="Unable to cache surfaces larger than"):
         cache.add_surface_to_long_term_cache(
             [pygame.Surface((2048, 2048)), 1], string_id="test_surface")
    def test_add_surface_to_cache(self, _init_pygame,
                                  _display_surface_return_none):
        cache = SurfaceCache()
        cache.add_surface_to_cache(pygame.Surface((64, 64)), 'doop')

        assert isinstance(cache.find_surface_in_cache('doop'), pygame.Surface)
        assert cache.find_surface_in_cache('doop').get_width() == 64
    def __init__(self, resource_loader: IResourceLoader, locale: str):

        self._resource_loader = resource_loader
        self._locale = locale
        # the base colours are the default colours all UI elements use if they
        # don't have a more specific colour defined for their element
        self.base_colours = {}

        # colours for specific elements stored by element id then colour id
        self.ui_element_colours = {}
        self.font_dict = UIFontDictionary(self._resource_loader, locale)
        self.shadow_generator = ShadowGenerator()
        self.shape_cache = SurfaceCache()

        self.unique_theming_ids = {}

        self.ui_element_fonts_info = {}
        self.ui_element_image_locs = {}
        self.ele_font_res = {}
        self.ui_element_image_surfaces = {}
        self.ui_element_misc_data = {}
        self.image_resources = {}  # type: Dict[str,ImageResource]
        self.surface_resources = {}  # type: Dict[str,SurfaceResource]

        self._theme_file_last_modified = None
        self._theme_file_path = None

        self._load_default_theme_file()

        self.st_cache_duration = 10.0
        self.st_cache_clear_timer = 0.0
    def test_find_surface_in_cache(self, _init_pygame, _display_surface_return_none):
        cache = SurfaceCache()
        cache.add_surface_to_cache(pygame.Surface((64, 64)), 'doop')

        assert isinstance(cache.find_surface_in_cache('doop'), pygame.Surface)

        cache.update()
        assert isinstance(cache.find_surface_in_cache('doop'), pygame.Surface)

        assert cache.find_surface_in_cache('derp') is None
Example #5
0
    def __init__(self, resource_loader: IResourceLoader):

        self._resource_loader = resource_loader

        # the base colours are the default colours all UI elements use if they
        # don't have a more specific colour defined for their element
        self.base_colours = {
            'normal_bg': pygame.Color('#25292e'),
            'hovered_bg': pygame.Color('#35393e'),
            'disabled_bg': pygame.Color('#25292e'),
            'selected_bg': pygame.Color('#193754'),
            'active_bg': pygame.Color('#193754'),
            'dark_bg': pygame.Color('#15191e'),
            'normal_text': pygame.Color('#c5cbd8'),
            'hovered_text': pygame.Color('#FFFFFF'),
            'selected_text': pygame.Color('#FFFFFF'),
            'active_text': pygame.Color('#FFFFFF'),
            'disabled_text': pygame.Color('#6d736f'),
            'normal_border': pygame.Color('#DDDDDD'),
            'hovered_border': pygame.Color('#EDEDED'),
            'disabled_border': pygame.Color('#909090'),
            'selected_border': pygame.Color('#294764'),
            'active_border': pygame.Color('#294764'),
            'link_text': pygame.Color('#c5cbFF'),
            'link_hover': pygame.Color('#a5abDF'),
            'link_selected': pygame.Color('#DFabDF'),
            'text_shadow': pygame.Color('#777777'),
            'filled_bar': pygame.Color("#f4251b"),
            'unfilled_bar': pygame.Color("#CCCCCC")
        }

        # colours for specific elements stored by element id then colour id
        self.ui_element_colours = {}
        self.font_dictionary = UIFontDictionary(self._resource_loader)
        self.shadow_generator = ShadowGenerator()
        self.shape_cache = SurfaceCache()

        self.unique_theming_ids = {}

        self.ui_element_fonts_info = {}
        self.ui_element_image_locs = {}
        self.ele_font_res = {}
        self.ui_element_image_surfaces = {}
        self.ui_element_misc_data = {}
        self.image_resources = {}  # type: Dict[str,ImageResource]
        self.surface_resources = {}  # type: Dict[str,SurfaceResource]

        self._theme_file_last_modified = None
        self._theme_file_path = None

        self._load_default_theme_file()

        self.st_cache_duration = 10.0
        self.st_cache_clear_timer = 0.0
    def test_remove_user_from_cache_item(self, _init_pygame, _display_surface_return_none):
        cache = SurfaceCache()
        cache.add_surface_to_cache(pygame.Surface((64, 64)), 'doop')
        cache.update()

        assert cache.cache_long_term_lookup['doop']['current_uses'] == 1
        assert len(cache.consider_purging_list) == 0

        cache.remove_user_from_cache_item('doop')
        assert cache.cache_long_term_lookup['doop']['current_uses'] == 0
        assert len(cache.consider_purging_list) == 1
    def test_update(self, _init_pygame, _display_surface_return_none):
        cache = SurfaceCache()
        cache.add_surface_to_cache(pygame.Surface((64, 64)), 'doop')

        assert len(cache.cache_short_term_lookup) == 1
        assert len(cache.cache_long_term_lookup) == 0
        cache.update()
        assert len(cache.cache_short_term_lookup) == 0
        assert len(cache.cache_long_term_lookup) == 1

        cache.low_on_space = True
        cache.cache_long_term_lookup['doop']['current_uses'] = 0
        cache.consider_purging_list.append('doop')

        cache.update()
        assert len(cache.consider_purging_list) == 0
        assert len(cache.cache_long_term_lookup) == 0
        assert not cache.low_on_space
    def test_add_surface_to_long_term_cache(self, _init_pygame, _display_surface_return_none):
        cache = SurfaceCache()
        cache.add_surface_to_long_term_cache([pygame.Surface((256, 256)), 1],
                                             string_id="test_surface_1")
        cache.add_surface_to_long_term_cache([pygame.Surface((512, 256)), 1],
                                             string_id="test_surface_2")
        cache.add_surface_to_long_term_cache([pygame.Surface((256, 512)), 1],
                                             string_id="test_surface_3")
        cache.add_surface_to_long_term_cache([pygame.Surface((256, 128)), 1],
                                             string_id="test_surface_4")
        cache.add_surface_to_long_term_cache([pygame.Surface((128, 256)), 1],
                                             string_id="test_surface_5")
        cache.add_surface_to_long_term_cache([pygame.Surface((256, 256)), 1],
                                             string_id="test_surface_6")
        cache.add_surface_to_long_term_cache([pygame.Surface((128, 128)), 1],
                                             string_id="test_surface_7")
        cache.add_surface_to_long_term_cache([pygame.Surface((640, 64)), 1],
                                             string_id="test_surface_8")
        cache.add_surface_to_long_term_cache([pygame.Surface((640, 32)), 1],
                                             string_id="test_surface_9")
        cache.add_surface_to_long_term_cache([pygame.Surface((784, 784)), 1],
                                             string_id="test_surface_10")

        assert type(cache.find_surface_in_cache("test_surface_1")) == pygame.Surface
 def test_creation(self, _init_pygame):
     SurfaceCache()
Example #10
0
    def __init__(self):

        # the base colours are the default colours all UI elements use if they
        # don't have a more specific colour defined for their element
        self.base_colours = {
            'normal_bg': pygame.Color('#25292e'),
            'hovered_bg': pygame.Color('#35393e'),
            'disabled_bg': pygame.Color('#25292e'),
            'selected_bg': pygame.Color('#193754'),
            'active_bg': pygame.Color('#193754'),
            'dark_bg': pygame.Color('#15191e'),
            'normal_text': pygame.Color('#c5cbd8'),
            'hovered_text': pygame.Color('#FFFFFF'),
            'selected_text': pygame.Color('#FFFFFF'),
            'active_text': pygame.Color('#FFFFFF'),
            'disabled_text': pygame.Color('#6d736f'),
            'normal_border': pygame.Color('#DDDDDD'),
            'hovered_border': pygame.Color('#EDEDED'),
            'disabled_border': pygame.Color('#909090'),
            'selected_border': pygame.Color('#294764'),
            'active_border': pygame.Color('#294764'),
            'link_text': pygame.Color('#c5cbFF'),
            'link_hover': pygame.Color('#a5abDF'),
            'link_selected': pygame.Color('#DFabDF'),
            'text_shadow': pygame.Color('#777777'),
            'filled_bar': pygame.Color("#f4251b"),
            'unfilled_bar': pygame.Color("#CCCCCC")
        }

        # colours for specific elements stored by element id then colour id
        self.ui_element_colours = {}

        # font dictionary that stores loaded fonts
        self.font_dictionary = UIFontDictionary()

        # shadow generator
        self.shadow_generator = ShadowGenerator()

        # shape cache
        self.shape_cache = SurfaceCache()

        # the font to use if no other font is specified
        # these hardcoded paths should be OK for PyInstaller right now because they will never actually used while
        # fira_code is the default pre-loaded font. May need to re-visit this later.
        module_root_path = os.path.abspath(
            os.path.dirname(os.path.dirname(__file__)))
        self.base_font_info = {
            'name':
            'fira_code',
            'size':
            14,
            'bold':
            False,
            'italic':
            False,
            'regular_path':
            os.path.normpath(
                os.path.join(module_root_path, 'data/FiraCode-Regular.ttf')),
            'bold_path':
            os.path.normpath(
                os.path.join(module_root_path, 'data/FiraCode-Bold.ttf')),
            'italic_path':
            os.path.normpath(
                os.path.join(module_root_path,
                             'data/FiraMono-RegularItalic.ttf')),
            'bold_italic_path':
            os.path.normpath(
                os.path.join(module_root_path, 'data/FiraMono-BoldItalic.ttf'))
        }

        # fonts for specific elements stored by element id
        self.ui_element_fonts_info = {}
        self.ui_element_fonts = {}

        # stores any images specified in themes that need loading at the appropriate time
        self.ui_element_image_paths = {}
        self.ui_element_image_surfaces = {}
        self.loaded_image_files = {
        }  # just a dictionary of all images paths & image files loaded by the UI

        # stores everything that doesn't have a specific place elsewhere and doesn't need any time-consuming loading
        # all will be stored as strings and will have to do any further processing in their specific elements.
        # misc data that doesn't have a value defined in a theme will return None so elements should be prepared
        # to handle that with a default behaviour
        self.ui_element_misc_data = {}

        self._theme_file_last_modified = None
        self._theme_file_path = None

        # Only load  the 'stringified' data if we can't find the actual default theme file
        # This is need for PyInstaller build
        default_theme_file_path = os.path.normpath(
            os.path.join(module_root_path, 'data', 'default_theme.json'))
        self.load_default_theme_file(default_theme_file_path)
Example #11
0
 def test_creation(self, _init_pygame, _display_surface_return_none):
     SurfaceCache()