refactoring

This commit is contained in:
Yonokid
2025-04-23 19:14:09 -04:00
parent 0012868d4e
commit 3291eccdcd
11 changed files with 634 additions and 482 deletions

View File

@@ -10,6 +10,14 @@ import tomllib
#TJA Format creator is unknown. I did not create the format, but I did write the parser though.
def get_zip_filenames(zip_path: str) -> list[str]:
result = []
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
file_list = zip_ref.namelist()
for file_name in file_list:
result.append(file_name)
return result
def load_image_from_zip(zip_path: str, filename: str) -> ray.Image:
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
with zip_ref.open(filename) as image_file:
@@ -30,6 +38,28 @@ def load_texture_from_zip(zip_path: str, filename: str) -> ray.Texture:
os.remove(temp_file_path)
return texture
def load_all_textures_from_zip(zip_path: str) -> dict[str, list[ray.Texture]]:
result_dict = dict()
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
files = zip_ref.namelist()
for file in files:
with zip_ref.open(file) as image_file:
with tempfile.NamedTemporaryFile(delete=False, suffix='.png') as temp_file:
temp_file.write(image_file.read())
temp_file_path = temp_file.name
texture = ray.load_texture(temp_file_path)
os.remove(temp_file_path)
true_filename, index = file.split('_img')
index = int(index.split('.')[0])
if true_filename not in result_dict:
result_dict[true_filename] = []
while len(result_dict[true_filename]) <= index:
result_dict[true_filename].append(None)
result_dict[true_filename][index] = texture
return result_dict
def rounded(num: float) -> int:
sign = 1 if (num >= 0) else -1
num = abs(num)
@@ -41,7 +71,7 @@ def rounded(num: float) -> int:
def get_current_ms() -> int:
return rounded(time.time() * 1000)
def strip_comments(code: str):
def strip_comments(code: str) -> str:
result = ''
index = 0
for line in code.splitlines():
@@ -53,7 +83,7 @@ def strip_comments(code: str):
index += 1
return result
def get_pixels_per_frame(bpm: float, time_signature: float, distance: float):
def get_pixels_per_frame(bpm: float, time_signature: float, distance: float) -> float:
beat_duration = 60 / bpm
total_time = time_signature * beat_duration
total_frames = 60 * total_time
@@ -66,6 +96,7 @@ def get_config() -> dict[str, Any]:
@dataclass
class GlobalData:
videos_cleared = False
start_song: bool = False
selected_song: str = ''
selected_difficulty: int = -1
@@ -73,3 +104,66 @@ class GlobalData:
result_ok: int = -1
result_bad: int = -1
result_score: int = -1
songs_played: int = 0
global_data = GlobalData()
@dataclass
class OutlinedText:
font: ray.Font
text: str
font_size: int
text_color: ray.Color
outline_color: ray.Color
outline_thickness: int = 2
def __post_init__(self):
self.texture = self._create_texture()
def _create_texture(self):
text_size = ray.measure_text_ex(self.font, self.text, self.font_size, 1.0)
padding = self.outline_thickness * 2
width = int(text_size.x + padding * 2)
height = int(text_size.y + padding * 2)
image = ray.gen_image_color(width, height, ray.Color(0, 0, 0, 0))
for dx in range(-self.outline_thickness, self.outline_thickness + 1):
for dy in range(-self.outline_thickness, self.outline_thickness + 1):
if dx == 0 and dy == 0:
continue
distance = (dx * dx + dy * dy) ** 0.5
if distance <= self.outline_thickness:
ray.image_draw_text_ex(
image,
self.font,
self.text,
ray.Vector2(padding + dx, padding + dy),
self.font_size,
1.0,
self.outline_color
)
ray.image_draw_text_ex(
image,
self.font,
self.text,
ray.Vector2(padding, padding),
self.font_size,
1.0,
self.text_color
)
texture = ray.load_texture_from_image(image)
ray.unload_image(image)
return texture
def draw(self, x: int, y: int, color: ray.Color):
ray.draw_texture(self.texture, x, y, color)
def unload(self):
ray.unload_texture(self.texture)