Add keybindings to the base surface

master
LEdoian 8 months ago
parent eea3379477
commit 8a049027cc

@ -1,15 +1,21 @@
import pygame as pg
import pygame.freetype as pgft
from triditko.renderers import DrawItemNameRenderer
from triditko.renderers import DrawItemNameRenderer, MultiLineStringRenderer
from triditko.utils import align
import json
import importlib
import argparse
from collections import defaultdict
def get_base_surface(screen_size, categories) -> (pg.Surface, (int, int)):
def get_base_surface(screen_size, categories, keys) -> (pg.Surface, (int, int)):
"""Returns a surface template with legend, suitable as a basis for rendering the rest.
Very barebones, will need to be remade for advanced features."""
bindings_by_category = defaultdict(lambda:[])
for key, cat in keys.items():
bindings_by_category[cat].append(key)
bindings = {cat: ', '.join((pg.key.name(key) for key in keys)) for cat, keys in bindings_by_category.items()}
w, h = screen_size
result = pg.Surface((w, h))
result.fill((0,0,0))
@ -17,10 +23,10 @@ def get_base_surface(screen_size, categories) -> (pg.Surface, (int, int)):
label_height = h//5
label_width = w//len(categories)
label_pos_y = label_height*4
renderer = DrawItemNameRenderer()
renderer = MultiLineStringRenderer()
# NOTE: I wanted them to have a border, but too lazy.
for i, cat in enumerate(categories):
label = renderer.render(cat, label_width, label_height)
label = renderer.render(f'{cat}\n{bindings[cat]}', label_width, label_height)
result.blit(label, (i*label_width, label_pos_y))
return result, (w, label_pos_y)
@ -89,7 +95,7 @@ def main():
# TODO: draw first or at least create an event to do so?
base_surface, (item_w, item_h) = get_base_surface(initial_screen_size, categories)
base_surface, (item_w, item_h) = get_base_surface(initial_screen_size, categories, bindings)
current_item = 0
running = True
pressed = False
@ -99,7 +105,7 @@ def main():
if event.type == pg.QUIT:
running = False # The while is too outer :-/
elif event.type == pg.VIDEORESIZE:
base_surface, (item_w, item_h) = get_base_surface(event.size, categories)
base_surface, (item_w, item_h) = get_base_surface(event.size, categories, bindings)
elif event.type == pg.VIDEOEXPOSE:
pg.display.flip() # Do we need this?
elif event.type == pg.KEYDOWN and not pressed:

Loading…
Cancel
Save