extracted the ItemSelection from athmos and created a parent class

This commit is contained in:
2025-08-03 18:29:25 +02:00
parent f55522f03c
commit 9f9f667a86
3 changed files with 58 additions and 33 deletions

View File

@ -1,50 +1,30 @@
from .fonts import * from .fonts import *
from .colors import * from .colors import *
from .item_selection import ItemSelection
import pygame import pygame
import math
MAX_COUNT = 9 MAX_COUNT = 9
class Athmos: class Athmos(ItemSelection):
def __init__(self, screen): def __init__(self, screen):
self.index = 0
self.filenames = []
self.screen = screen self.screen = screen
self.show_list = True self.show_list = True
self.surface = pygame.Surface((0,0), pygame.SRCALPHA) self.surface = pygame.Surface((0,0), pygame.SRCALPHA)
self.max_name_width = 350 self.max_name_width = 350
super().__init__(self.surface, self.max_name_width, MAX_COUNT)
def set_filenames(self,filenames): def set_filenames(self,filenames):
self.filenames = filenames super().set_items(filenames)
self.surface = pygame.Surface(
(self.screen.get_width()*2/3, self.get_display_count()*40),
pygame.SRCALPHA)
def get_display_count(self): def get_name_by_index(self, index):
return min(MAX_COUNT, len(self.filenames)) return self.items[index]
def get_count(self):
return len(self.items)
def update(self): def update(self):
if self.show_list: if self.show_list:
self.surface.fill("black") super().update()
for i in range(self.get_display_count()):
text = self.filenames[(i+self.index-math.floor(self.get_display_count()/2))%len(self.filenames)]
if len(text)> math.floor(self.max_name_width/10):
text = text[0:math.floor(self.max_name_width/10)-2]
text += '...'
#text = self.filenames[i]
if i == math.floor(self.get_display_count()/2):
#if i == self.index:
text_surface = font_helvetica16.render(text, False, color_primary_light)
else:
text_surface = font_helvetica16.render(text, False, color_primary)
opacity = -20 + math.floor((i+1) * 2 * 255 / MAX_COUNT
if i < MAX_COUNT/2
else (MAX_COUNT-i) * 2 * 255 / MAX_COUNT)
text_surface.set_alpha(opacity)
#if text_surface.get_width() > self.max_name_width: self.max_name_width = text_surface.get_width()
self.surface.blit(text_surface, ((10, self.surface.get_height()/self.get_display_count()*i)))
pygame.draw.rect(self.surface, color_primary_light, (0, self.surface.get_height()/2-25, self.max_name_width+20, 40), 2)
self.screen.blit(self.surface, ((self.screen.get_width()-self.max_name_width)/2, self.screen.blit(self.surface, ((self.screen.get_width()-self.max_name_width)/2,
(self.screen.get_height() - self.surface.get_height())/2)) (self.screen.get_height() - self.surface.get_height())/2))

44
item_selection.py Normal file
View File

@ -0,0 +1,44 @@
from .fonts import *
from .colors import *
import pygame
import math
class ItemSelection:
def __init__(self, surface, max_item_width, max_count):
self.index = 0
self.items = []
self.surface = surface
self.max_item_width = max_item_width
self.max_count = max_count
def set_items(self, items):
self.items = items
self.surface = pygame.Surface(
(self.max_item_width+50, self.get_display_count()*40),
pygame.SRCALPHA)
def get_display_count(self):
return min(self.max_count, len(self.items))
def update(self):
self.surface.fill("black")
for i in range(self.get_display_count()):
text = self.items[(i+self.index-math.floor(self.get_display_count()/2))%len(self.items)]
if len(text)> math.floor(self.max_item_width/10):
text = text[0:math.floor(self.max_item_width/10)-2]
text += '...'
#text = self.items[i]
if i == math.floor(self.get_display_count()/2):
#if i == self.index:
text_surface = font_helvetica16.render(text, False, color_primary_light)
else:
text_surface = font_helvetica16.render(text, False, color_primary)
opacity = -20 + math.floor((i+1) * 2 * 255 / self.max_count
if i < self.max_count/2
else (self.max_count-i) * 2 * 255 / self.max_count)
text_surface.set_alpha(opacity)
#if text_surface.get_width() > self.max_item_width: self.max_item_width = text_surface.get_width()
self.surface.blit(text_surface, ((10, self.surface.get_height()/self.get_display_count()*i)))
pygame.draw.rect(self.surface, color_primary_light, (0, self.surface.get_height()/2-25, self.max_item_width+20, 40), 2)

View File

@ -116,7 +116,8 @@ class GuiMain:
self.athmos.index = index self.athmos.index = index
def set_selected_athmo(self): def set_selected_athmo(self):
self.selected_athmo_srf = font_helvetica16.render(self.athmos.filenames[self.athmos.index], False, color_primary) self.selected_athmo_srf = font_helvetica16.render(
self.athmos.get_name_by_index(self.athmos.index), False, color_primary)
def set_athmo_filenames(self, filenames): def set_athmo_filenames(self, filenames):
self.athmos.set_filenames(filenames) self.athmos.set_filenames(filenames)
@ -185,11 +186,11 @@ class GuiMain:
if event.key == pygame.K_UP: if event.key == pygame.K_UP:
if self.athmos.show_list: if self.athmos.show_list:
self.athmos.index -= 1 self.athmos.index -= 1
self.athmos.index %= len(self.athmos.filenames) self.athmos.index %= self.athmos.get_count()
if event.key == pygame.K_DOWN: if event.key == pygame.K_DOWN:
if self.athmos.show_list: if self.athmos.show_list:
self.athmos.index += 1 self.athmos.index += 1
self.athmos.index %= len(self.athmos.filenames) self.athmos.index %= self.athmos.get_count()
elif event.type == pygame.QUIT: elif event.type == pygame.QUIT:
self.running = False self.running = False
elif event.type == pygame.MOUSEBUTTONDOWN: elif event.type == pygame.MOUSEBUTTONDOWN: