.
This commit is contained in:
233
gui.py
233
gui.py
@@ -1,9 +1,8 @@
|
||||
import pygame
|
||||
# pylint: disable=E0611
|
||||
from pygame.locals import KEYDOWN
|
||||
# pylint: enable=E0611
|
||||
|
||||
from configfile import cfg
|
||||
from pygame.locals import KEYDOWN
|
||||
|
||||
from config import cfg
|
||||
from utils import wrap_multi_line, image_from_data
|
||||
|
||||
# pylint: disable=E1121,R0902,R0903,R0912,R0913
|
||||
@@ -13,19 +12,9 @@ BLACK = (0, 0, 0)
|
||||
|
||||
|
||||
class baseobject(object):
|
||||
def __init__(self,
|
||||
surface,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
font,
|
||||
border=False,
|
||||
fg=WHITE,
|
||||
bg=BLACK,
|
||||
name=None,
|
||||
title=None):
|
||||
def __init__(
|
||||
self, surface, x, y, width, height, data, font, border=False, fg=WHITE, bg=BLACK, name=None, title=None
|
||||
):
|
||||
self.surface = surface
|
||||
self.x = x
|
||||
self.y = y
|
||||
@@ -91,22 +80,23 @@ class baseobject(object):
|
||||
|
||||
|
||||
class menu(baseobject):
|
||||
def __init__(self,
|
||||
surface,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
font,
|
||||
border=False,
|
||||
fg=WHITE,
|
||||
bg=BLACK,
|
||||
name=None,
|
||||
startitem=0,
|
||||
itemattop=0):
|
||||
baseobject.__init__(self, surface, x, y, width, height, data, font,
|
||||
border, fg, bg, name)
|
||||
def __init__(
|
||||
self,
|
||||
surface,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
font,
|
||||
border=False,
|
||||
fg=WHITE,
|
||||
bg=BLACK,
|
||||
name=None,
|
||||
startitem=0,
|
||||
itemattop=0,
|
||||
):
|
||||
baseobject.__init__(self, surface, x, y, width, height, data, font, border, fg, bg, name)
|
||||
self.currentitem = startitem
|
||||
self.itemattop = itemattop
|
||||
self.itemsperpage = int(self.textheight / self.fontheight)
|
||||
@@ -120,8 +110,7 @@ class menu(baseobject):
|
||||
if self.currentitem == self.itemattop:
|
||||
self.itemattop -= 1
|
||||
self.currentitem -= 1
|
||||
if event.key in cfg["KeyDown"] and self.currentitem < len(
|
||||
self.data) - 1:
|
||||
if event.key in cfg["KeyDown"] and self.currentitem < len(self.data) - 1:
|
||||
self.currentitem += 1
|
||||
if self.itemattop + self.itemsperpage <= self.currentitem:
|
||||
self.itemattop += 1
|
||||
@@ -132,8 +121,7 @@ class menu(baseobject):
|
||||
if self.itemattop < 0:
|
||||
self.itemattop = 0
|
||||
self.currentitem = 0
|
||||
if event.key in cfg["KeyPgDn"] and self.currentitem < len(
|
||||
self.data) - 1:
|
||||
if event.key in cfg["KeyPgDn"] and self.currentitem < len(self.data) - 1:
|
||||
self.currentitem += self.itemsperpage - 1
|
||||
while self.currentitem - self.itemattop >= self.itemsperpage:
|
||||
self.itemattop += 1
|
||||
@@ -143,28 +131,23 @@ class menu(baseobject):
|
||||
if event.key in cfg["KeyHome"] and self.currentitem > 0:
|
||||
self.currentitem = 0
|
||||
self.itemattop = 0
|
||||
if event.key in cfg["KeyEnd"] and self.currentitem < len(
|
||||
self.data) - 1:
|
||||
if event.key in cfg["KeyEnd"] and self.currentitem < len(self.data) - 1:
|
||||
self.currentitem = len(self.data) - 1
|
||||
self.itemattop = self.currentitem - self.itemsperpage + 1
|
||||
|
||||
def render(self):
|
||||
pygame.Surface.fill(
|
||||
self.surface,
|
||||
self.bg,
|
||||
rect=pygame.Rect(self.x, self.y, self.width, self.height))
|
||||
pygame.Surface.fill(self.surface, self.bg, rect=pygame.Rect(self.x, self.y, self.width, self.height))
|
||||
|
||||
if self.border:
|
||||
pygame.draw.rect(self.surface, self.fg,
|
||||
pygame.Rect(self.x + 1, self.y + 1,
|
||||
self.width - 2, self.height - 2), 1)
|
||||
pygame.draw.rect(
|
||||
self.surface, self.fg, pygame.Rect(self.x + 1, self.y + 1, self.width - 2, self.height - 2), 1
|
||||
)
|
||||
|
||||
for i in range(0, self.itemsperpage):
|
||||
if i > len(self.data) - 1:
|
||||
t = pygame.Surface((self.textwidth, self.fontheight))
|
||||
t.fill(self.bg)
|
||||
self.surface.blit(t, (self.textx,
|
||||
self.texty + i * self.fontheight))
|
||||
self.surface.blit(t, (self.textx, self.texty + i * self.fontheight))
|
||||
continue
|
||||
|
||||
if self.isrom:
|
||||
@@ -177,33 +160,20 @@ class menu(baseobject):
|
||||
t = pygame.Surface((self.textwidth, self.fontheight))
|
||||
t.fill(self.fg)
|
||||
t.blit(s, (0, 0))
|
||||
self.surface.blit(t, (self.textx,
|
||||
self.texty + i * self.fontheight))
|
||||
self.surface.blit(t, (self.textx, self.texty + i * self.fontheight))
|
||||
else:
|
||||
s = self.font.render(item, True, self.fg, self.bg)
|
||||
t = pygame.Surface((self.textwidth, self.fontheight))
|
||||
t.fill(self.bg)
|
||||
t.blit(s, (0, 0))
|
||||
self.surface.blit(t, (self.textx,
|
||||
self.texty + i * self.fontheight))
|
||||
self.surface.blit(t, (self.textx, self.texty + i * self.fontheight))
|
||||
|
||||
|
||||
class notepad(baseobject):
|
||||
def __init__(self,
|
||||
surface,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
font,
|
||||
border=False,
|
||||
fg=WHITE,
|
||||
bg=BLACK,
|
||||
name="None",
|
||||
title=None):
|
||||
baseobject.__init__(self, surface, x, y, width, height, data, font,
|
||||
border, fg, bg, name, title)
|
||||
def __init__(
|
||||
self, surface, x, y, width, height, data, font, border=False, fg=WHITE, bg=BLACK, name="None", title=None
|
||||
):
|
||||
baseobject.__init__(self, surface, x, y, width, height, data, font, border, fg, bg, name, title)
|
||||
self.data = wrap_multi_line(data, self.font, self.textwidth)
|
||||
self.lineattop = 0
|
||||
self.linesperpage = int(self.textheight / self.fontheight)
|
||||
@@ -229,52 +199,30 @@ class notepad(baseobject):
|
||||
self.lineattop = self.lasttopline
|
||||
|
||||
def render(self):
|
||||
pygame.Surface.fill(
|
||||
self.surface,
|
||||
self.bg,
|
||||
rect=pygame.Rect(self.x, self.y, self.width, self.height))
|
||||
pygame.Surface.fill(self.surface, self.bg, rect=pygame.Rect(self.x, self.y, self.width, self.height))
|
||||
if self.border:
|
||||
pygame.draw.rect(self.surface, self.fg,
|
||||
pygame.Rect(self.x + 1, self.y + 1,
|
||||
self.width - 2, self.height - 2), 1)
|
||||
pygame.draw.rect(
|
||||
self.surface, self.fg, pygame.Rect(self.x + 1, self.y + 1, self.width - 2, self.height - 2), 1
|
||||
)
|
||||
if self.title:
|
||||
x = (self.textwidth / 2) - (
|
||||
self.font.size('%s' % self.title)[0] / 2)
|
||||
title_surface = pygame.Surface((self.textwidth,
|
||||
self.fontheight))
|
||||
x = (self.textwidth / 2) - (self.font.size("%s" % self.title)[0] / 2)
|
||||
title_surface = pygame.Surface((self.textwidth, self.fontheight))
|
||||
title_surface.fill(self.fg)
|
||||
title_surface.blit(
|
||||
self.font.render('%s' % self.title, True, BLACK, self.fg),
|
||||
(x, 0))
|
||||
self.surface.blit(title_surface,
|
||||
(self.textx, self.texty - self.fontheight))
|
||||
title_surface.blit(self.font.render("%s" % self.title, True, BLACK, self.fg), (x, 0))
|
||||
self.surface.blit(title_surface, (self.textx, self.texty - self.fontheight))
|
||||
for c in range(0, self.linesperpage):
|
||||
try:
|
||||
itemsurface = self.font.render(self.data[c + self.lineattop],
|
||||
True, self.fg, self.bg)
|
||||
self.surface.blit(itemsurface,
|
||||
(self.textx,
|
||||
self.texty + c * self.fontheight))
|
||||
itemsurface = self.font.render(self.data[c + self.lineattop], True, self.fg, self.bg)
|
||||
self.surface.blit(itemsurface, (self.textx, self.texty + c * self.fontheight))
|
||||
except:
|
||||
pass
|
||||
|
||||
|
||||
class image_notepad(baseobject):
|
||||
def __init__(self,
|
||||
surface,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
font,
|
||||
border=False,
|
||||
fg=WHITE,
|
||||
bg=BLACK,
|
||||
name="None",
|
||||
title=None):
|
||||
baseobject.__init__(self, surface, x, y, width, height, data, font,
|
||||
border, fg, bg, name, title)
|
||||
def __init__(
|
||||
self, surface, x, y, width, height, data, font, border=False, fg=WHITE, bg=BLACK, name="None", title=None
|
||||
):
|
||||
baseobject.__init__(self, surface, x, y, width, height, data, font, border, fg, bg, name, title)
|
||||
self.currentitem = 0
|
||||
self.data = data
|
||||
|
||||
@@ -288,25 +236,18 @@ class image_notepad(baseobject):
|
||||
self.currentitem -= 1
|
||||
|
||||
def render(self):
|
||||
pygame.Surface.fill(
|
||||
self.surface,
|
||||
self.bg,
|
||||
rect=pygame.Rect(self.x, self.y, self.width, self.height))
|
||||
pygame.Surface.fill(self.surface, self.bg, rect=pygame.Rect(self.x, self.y, self.width, self.height))
|
||||
if self.border:
|
||||
pygame.draw.rect(self.surface, self.fg,
|
||||
pygame.Rect(self.x + 1, self.y + 1,
|
||||
self.width - 2, self.height - 2), 1)
|
||||
x = (self.textwidth / 2) - (self.font.size('%s' % self.title)[0] / 2)
|
||||
pygame.draw.rect(
|
||||
self.surface, self.fg, pygame.Rect(self.x + 1, self.y + 1, self.width - 2, self.height - 2), 1
|
||||
)
|
||||
x = (self.textwidth / 2) - (self.font.size("%s" % self.title)[0] / 2)
|
||||
title_surface = pygame.Surface((self.textwidth, self.fontheight))
|
||||
title_surface.fill(self.fg)
|
||||
title_surface.blit(
|
||||
self.font.render('%s' % self.data[self.currentitem][0], True,
|
||||
BLACK, self.fg), (x, 0))
|
||||
self.surface.blit(title_surface, (self.textx,
|
||||
self.texty - self.fontheight))
|
||||
title_surface.blit(self.font.render("%s" % self.data[self.currentitem][0], True, BLACK, self.fg), (x, 0))
|
||||
self.surface.blit(title_surface, (self.textx, self.texty - self.fontheight))
|
||||
|
||||
image_surface = image_from_data(self.data[self.currentitem][1],
|
||||
(self.textwidth, self.textheight))
|
||||
image_surface = image_from_data(self.data[self.currentitem][1], (self.textwidth, self.textheight))
|
||||
|
||||
w, h = image_surface.get_width(), image_surface.get_height()
|
||||
|
||||
@@ -316,12 +257,11 @@ class image_notepad(baseobject):
|
||||
self.surface.blit(image_surface, (x, y))
|
||||
|
||||
|
||||
class gui():
|
||||
class gui:
|
||||
def __init__(self, surface):
|
||||
self.surface = surface
|
||||
self.objects = []
|
||||
self.font = pygame.font.Font(
|
||||
pygame.font.match_font(cfg["Font"], bold=True), cfg["FontSize"])
|
||||
self.font = pygame.font.Font(pygame.font.match_font(cfg["Font"], bold=True), cfg["FontSize"])
|
||||
# self.fontheight = self.font.get_height()
|
||||
self.currentobject = 0
|
||||
|
||||
@@ -333,54 +273,23 @@ class gui():
|
||||
|
||||
return event
|
||||
|
||||
def add_menu(self,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
border=False,
|
||||
fg=WHITE,
|
||||
bg=BLACK,
|
||||
name="None",
|
||||
startitem=0,
|
||||
itemattop=0):
|
||||
def add_menu(
|
||||
self, x, y, width, height, data, border=False, fg=WHITE, bg=BLACK, name="None", startitem=0, itemattop=0
|
||||
):
|
||||
self.currentobject += 1
|
||||
self.objects.append(
|
||||
menu(self.surface, x, y, width, height, data, self.font, border,
|
||||
fg, bg, name, startitem, itemattop))
|
||||
menu(self.surface, x, y, width, height, data, self.font, border, fg, bg, name, startitem, itemattop)
|
||||
)
|
||||
|
||||
def add_notepad(self,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
border=False,
|
||||
fg=WHITE,
|
||||
bg=BLACK,
|
||||
name="None",
|
||||
title=None):
|
||||
def add_notepad(self, x, y, width, height, data, border=False, fg=WHITE, bg=BLACK, name="None", title=None):
|
||||
self.currentobject += 1
|
||||
self.objects.append(
|
||||
notepad(self.surface, x, y, width, height, data, self.font, border,
|
||||
fg, bg, name, title))
|
||||
self.objects.append(notepad(self.surface, x, y, width, height, data, self.font, border, fg, bg, name, title))
|
||||
|
||||
def add_image_notepad(self,
|
||||
x,
|
||||
y,
|
||||
width,
|
||||
height,
|
||||
data,
|
||||
border=False,
|
||||
fg=WHITE,
|
||||
bg=BLACK,
|
||||
name="None",
|
||||
title=None):
|
||||
def add_image_notepad(self, x, y, width, height, data, border=False, fg=WHITE, bg=BLACK, name="None", title=None):
|
||||
self.currentobject += 1
|
||||
self.objects.append(
|
||||
image_notepad(self.surface, x, y, width, height, data, self.font,
|
||||
border, fg, bg, name, title))
|
||||
image_notepad(self.surface, x, y, width, height, data, self.font, border, fg, bg, name, title)
|
||||
)
|
||||
|
||||
def deletelastobject(self):
|
||||
del self.objects[-1]
|
||||
|
||||
Reference in New Issue
Block a user