You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.8 KiB
Python

3 years ago
from .mapfile import MapFile
import pygame
colors = {
3 years ago
'?' : (64 , 64 , 64) , # Non-tried
3 years ago
'*' : (0 , 0 , 255), # Non-trimmed
'/' : (255, 255, 0) , # Non-scraped
'-' : (255, 0 , 0) , # Bad
'+' : (0 , 0 , 0) , # Recovered
'@' : (0 , 255, 255), # Own position
3 years ago
None: (0 , 0 , 0) , # Not on disk
}
BORDERCOLOR = (64,64,64)
W = 1920 // 2 - 2
W = 1920 - 2
3 years ago
H = 1040
CELLSIZE = 8
CELLBORDER = 0
CELLMARGIN = 3
3 years ago
CELLOFFSET = CELLSIZE + 2*CELLBORDER + CELLMARGIN
COLUMNS = W // CELLOFFSET
ROWS = H // CELLOFFSET
CELLS = COLUMNS * ROWS
class Visualisation:
def __init__(self, fn, **kwargs):
3 years ago
pygame.init()
3 years ago
# https://stackoverflow.com/questions/46160971/pygame-full-core-usage-in-simple-loop
pygame.mixer.quit()
3 years ago
self.disp = pygame.display.set_mode((W, H))
self.mapfile = MapFile(fn, **kwargs)
3 years ago
self.mapfile.load()
self.histograms = None
self.squares = None
# Order of colors to show...
self.order = ['@', '-', '/', '*', '?', '+']
def gen_histograms(self):
self.histograms = []
3 years ago
sz = self.mapfile.size
3 years ago
sqsz = max(sz / CELLS, 4096)
3 years ago
for cid in range(CELLS):
start = cid * sqsz
end = (cid + 1) * sqsz
hist = self.mapfile.get_hist(start, end)
self.histograms.append(hist)
def gen_squares(self):
if self.histograms is None: self.gen_histograms()
self.squares = []
for hist in self.histograms:
symb = self.get_symb(hist)
3 years ago
color = colors[symb]
self.squares.append(color)
3 years ago
def draw(self):
self.disp.fill((0,0,0)) # Dark theme :-)
if self.squares is None: self.gen_squares()
for cell, color in enumerate(self.squares):
line = cell // COLUMNS
col = cell % COLUMNS
3 years ago
pos = (line, col)
# DARKEN = 0.7
# if line % 2 == 0: color = (color[0] * DARKEN, color[1] * DARKEN, color[2] * DARKEN)
3 years ago
rect = pygame.Rect(col * CELLOFFSET + CELLBORDER, line * CELLOFFSET + CELLBORDER, CELLSIZE+2*CELLBORDER, CELLSIZE+2*CELLBORDER)
pygame.draw.rect(self.disp, color, rect)
if CELLBORDER != 0: pygame.draw.rect(self.disp, BORDERCOLOR, rect, width=CELLBORDER)
3 years ago
pygame.display.flip()
def reload(self):
self.mapfile.load()
3 years ago
self.histograms = None
self.squares = None
self.draw()
def get_symb(self, hist):
## Majority:
#symb, _count = max(hist.items(), key=lambda x: x[1])
#return symb
for ch in self.order:
if ch in hist:
return ch
def run(self, *, refresh=1800):
pygame.time.set_timer(pygame.USEREVENT, refresh * 1000)
pygame.event.set_blocked(None)
3 years ago
pygame.event.set_allowed([pygame.USEREVENT, pygame.VIDEORESIZE, pygame.VIDEOEXPOSE, pygame.QUIT, pygame.KEYDOWN])
# pygame.event.set_allowed([pygame.VIDEORESIZE, pygame.VIDEOEXPOSE, pygame.QUIT, pygame.KEYDOWN])
self.draw()
while True:
ev = pygame.event.wait()
if ev.type == pygame.QUIT or ev.type == pygame.KEYDOWN and ev.key == pygame.K_q:
return
if ev.type in [pygame.VIDEOEXPOSE, pygame.VIDEORESIZE]:
self.draw()
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_r:
3 years ago
self.reload()
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_a:
self.order = ['@', '-', '/', '*', '?', '+']
self.squares = None
self.draw()
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_m:
self.order = ['@', '?', '-', '/', '*', '+']
self.squares = None
self.draw()
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_g:
colors['+'] = (0, 128, 0)
self.squares = None
self.draw()
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_b:
colors['+'] = (0, 0, 0)
self.squares = None
self.draw()
3 years ago
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_x:
self.order = ['@', '+', '?', '-', '/', '*']
self.squares = None
self.draw()
3 years ago
if ev.type == pygame.KEYDOWN and ev.key == pygame.K_p:
sz = self.mapfile.size
sqsz = max(sz / CELLS, 4096)
print(f"Each square represents {sqsz} bytes.")
3 years ago
if ev.type == pygame.USEREVENT:
self.reload()