intergrated design templates for buttons

new-menus
Max Lange 8 years ago
parent 417fc0a5fa
commit 61ce18285e

Binary file not shown.

Binary file not shown.

@ -1,4 +1,4 @@
<#box1 = (./assets/sprites/Button1.tif,./assets/sprites/Button2.tif,./assets/sprites/Button3.tif,6)
<#box1 = (./assets/templates/nr1.design,6)
<#box2 = (./assets/sprites/Button1.tif,./assets/sprites/knob1.tif,6)
<~size = 13
<"typeface = monospace

@ -1,5 +1,6 @@
corner = ./assets/sprites/design_template.tif
#line = ./assets/sprites/design.tif
type = button
#corner = ./assets/sprites/design1.tif
line = ./assets/sprites/design1.tif
#line_orientation = vertical or horiontal
#inner_color = (255,128,128,100)
#Can create templates if line xor corner is missing

@ -1,5 +1,5 @@
&./assets/templates/default.vars
/#TODO halp
*|Load saved game|30 |$typeface|$color |$half + 0|%10 + 0|
*|$savename |20 |$typeface|$color |$half + 0|%20 + 0|
/add project stats here

@ -1,5 +1,5 @@
&./assets/templates/default.vars
/#TODO: images
/$image1-9 need to be provided and should be klickable pictures
/so the world is selectable
@ -11,3 +11,4 @@
@|world6|$image6|$size|$typeface|$color|$box1|%25 + 0|%75 + 0|
@|world7|$image7|$size|$typeface|$color|$box1|%50 + 0|%75 + 0|
@|world8|$image8|$size|$typeface|$color|$box1|%75 + 0|%75 + 0|
@|Exit |$size|$typeface|$color|$box1|$half + 0|$qrt + 160|

@ -3,9 +3,11 @@ import pygame
import disp_elem
from .disp_elem import button
from .disp_elem import slider
from .disp_elem import create_outline
#button and sliders would be unsued
button
slider
create_outline
def convert2list(string):
@ -19,102 +21,6 @@ def convert2list(string):
return elements
class create_outline():
def __init__(self, template_file):
self.resources = {}
self.read_file(template_file)
self.create_slices()
def read_file(self, template_file):
def split(line, splitter):
rline = line[line.index(splitter) + 1:].strip()
lline = line[:line.index(splitter)].strip()
return lline, rline
with open(template_file) as conf_file:
for line in conf_file:
if line[0] != "#":
option, var = split(line, "=")
self.resources[option] = var
def create_slices(self):
self.corner = None
self.line = None
self.line_orient = None
self.color = None
if "corner" in self.resources:
self.corner = pygame.image.load(self.resources["corner"])
if "line" in self.resources:
self.line = pygame.image.load(self.resources["line"])
if "line_orientation" in self.resources:
self.line_orient = pygame.image.load(self.resources["line_orientation"])
if "inner_color" in self.resources:
color = convert2list(self.resources["inner_color"])
if len(color) == 3:
self.color = (int(color[0]), int(color[1]), int(color[2]))
if len(color) == 4:
self.color = (int(color[0]), int(color[1]), int(color[2]), int(color[3]))
else:
self.color = (0, 0, 0, 0)
if self.corner is None:
if self.line is None:
print("No image given to create design.")
else:
if self.line_orient == "vertical":
self.line = pygame.transform.rotate(self.line, -90)
line_rect = self.line.get_rect()
size = line_rect.h
self.pixels = {}
self.pattern = pygame.Surface((1, size))
for a in range(size):
self.pattern.set_at((0, a), self.line.get_at((0, a)))
self.corner = pygame.Surface((size, size))
for a in range(size):
for x in range(size):
for y in range(size):
if x >= a and y >= a:
self.corner.set_at((x, y), self.pattern.get_at((0, a)))
else:
if self.line is None:
size = self.corner.get_height()
self.line = pygame.Surface((1, size))
for a in range(size):
self.line.set_at((0, a), self.corner.get_at((size - 1, a)))
def create_box(self, width, height):
border = self.line.get_height()
width += border * 2
height += border * 2
self.top = pygame.Surface((width, border))
#creating top frame line
for pos in range(width):
self.top.blit(self.line, pygame.Rect(pos, 0, 0, 0))
#blit left top corner
self.top.blit(self.corner, pygame.Rect(0, 0, 0, 0))
#blit right top corner
self.top.blit(pygame.transform.flip(self.corner, True, False),
pygame.Rect(width - border, 0, 0, 0))
#create bottom line
self.bottom = pygame.transform.flip(self.top, False, True)
#create left frame line
self.left = pygame.Surface((border, height))
tmp_line = pygame.transform.rotate(self.line, 90)
for pos in range(height):
self.left.blit(tmp_line, pygame.Rect(0, pos, 0, 0))
#create right frame line
self.right = pygame.transform.flip(self.left, True, False)
#Merge all together
final = pygame.Surface((width, height), pygame.SRCALPHA)
final.fill(self.color)
print self.color
final.blit(self.left, pygame.Rect(0, 0, 0, 0))
final.blit(self.right, pygame.Rect(width - border, 0, 0, 0))
final.blit(self.top, pygame.Rect(0, 0, 0, 0))
final.blit(self.bottom, pygame.Rect(0, height - border, 0, 0))
self.box = final
def analyse_num(string, variables):
string = string.strip()
lstring = string[:string.index("+")].rstrip()
@ -278,11 +184,15 @@ class create_menu():
if text[0] == "$":
text = self.vars[text[1:]]
if line.count("|") == 8:
#TODO: This:
img
line = line[line.index("|") + 1:].lstrip()
if line.strip()[0] == "$":
maxsize = self.vars[line[1: line.index("|")].strip()]
size = self.vars[line[1: line.index("|")].strip()]
else:
maxsize = float(line[: line.index("|")].strip())
size = float(line[: line.index("|")].strip())
line = line[line.index("|") + 1:].lstrip()
if line[0] == "$":
@ -300,7 +210,7 @@ class create_menu():
line = line[line.index("|") + 1:].lstrip()
if line[0] == "$":
img = self.vars[line[1: line.index("|")].strip()]
design = self.vars[line[1: line.index("|")].strip()]
else:
print("Error: Outline only accepts only variables")
quit()
@ -312,8 +222,7 @@ class create_menu():
rel_y, abs_y = analyse_num(line, self.vars)
self.elems["buttons"].append(disp_elem.button(rel_x, abs_x, rel_y, abs_y,
ref, text, typeface, maxsize, color, img[:3],
int(img[3])))
ref, text, typeface, size, color, design[:3]))
if line[0] == "-":
line = line[2:]

@ -1,4 +1,4 @@
# lint:ok
# lint:ok
# -*- coding: utf-8 -*-
import pygame
@ -36,15 +36,17 @@ def getmaxsize(typeface, size, text, antialias, color, maxsize, borderoff):
class button():
def __init__(self, rel_x, x, rel_y, y, ref, text, typeface, size,
color, buttons_files, borderoff):
color, button_designs):
"""Initalises with x and y as center point"""
#basic font and then everything should be clear
#going to be simplified next refactoring
self.buttons = (pygame.image.load(buttons_files[0]),
pygame.image.load(buttons_files[1]),
pygame.image.load(buttons_files[2]))
self.img = self.buttons[0]
self.pos = self.buttons[0].get_rect()
#three different instances!
#this way three images can be generated
#is faster …
normal = create_outline(button_designs[0])
hover = create_outline(button_designs[0])
klick = create_outline(button_designs[0])
self.buttons = [normal, hover, klick]
self.state = 0
self.x = x
self.y = y
self.rel_x = rel_x
@ -52,13 +54,20 @@ class button():
self.typeface = typeface
self.text = text
self.name = text
self.text_img = modrender(typeface, 30,
text, True, color,
self.pos.size, borderoff)
self.textpos = self.text_img.get_rect()
self.textpos.center = self.pos.center
self.klicked = False
#create font and text
font = pygame.font.SysFont(typeface, int(size))
self.text_img = font.render(self.text, True, color)
#define pos and size
self.pos = self.text_img.get_rect()
self.textpos = self.pos
#update position
self.move(self.x, self.rel_x, self.y, self.rel_y, ref)
#move buttons and create images
for num in range(len(self.buttons)):
self.buttons[num].create_box(num, self.pos)
#reposition text
self.textpos.center = self.pos.center
def changetext(self, text, color):
"""Changes the text inside the button"""
@ -70,7 +79,7 @@ class button():
def move(self, x, rel_x, y, rel_y, ref):
"""Moves the button so that x and y are the center"""
self.pos = self.buttons[0].get_rect()
self.pos = pygame.Rect((0, 0), self.pos.size)
rel_x *= float(ref.w)
rel_y *= float(ref.h)
x += rel_x
@ -80,24 +89,24 @@ class button():
self.textpos.center = self.pos.center
def update(self, events):
#blitts the button and changes image when hovered over or being clicked
#blits the button and changes image when hovered over or being clicked
#also posts a menu event to show that a button has been clicked
if self.pos.collidepoint(pygame.mouse.get_pos()) and not self.klicked:
self.img = self.buttons[1]
self.state = 1
for event in events:
if event.type == MOUSEBUTTONDOWN and event.button == 1:
menue = pygame.event.Event(USEREVENT, code="MENU")
pygame.fastevent.post(menue)
self.klicked = True
self.img = self.buttons[2]
self.state = 2
elif not self.klicked:
self.img = self.buttons[0]
self.state = 0
else:
self.img = self.buttons[2]
self.state = 2
def blit(self, screen):
"""Blits the button"""
screen.blit(self.img, self.pos)
screen.blit(self.buttons[self.state].box, self.buttons[self.state].pos)
screen.blit(self.text_img, self.textpos)
@ -229,4 +238,115 @@ class slider():
self.textpos.center = self.pos.center
screen.blit(self.box, self.pos)
screen.blit(self.knob, self.knob_pos)
screen.blit(self.render_text, self.textpos)
screen.blit(self.render_text, self.textpos)
class create_outline():
def __init__(self, template_file):
self.resources = {}
self.read_file(template_file)
self.modes = {}
for a in range(3):
self.modes[a] = self.create_template(a)
def read_file(self, template_file):
def split(line, splitter):
rline = line[line.index(splitter) + 1:].strip()
lline = line[:line.index(splitter)].strip()
return lline, rline
with open(template_file) as conf_file:
for line in conf_file:
if line[0] != "#":
option, var = split(line, "=")
self.resources[option] = var
def create_template(self, pos):
corner = None
line = None
line_orient = None
self.color = None
if "corner" in self.resources:
corner = pygame.image.load(self.resources["corner"])
if "line" in self.resources:
line = pygame.image.load(self.resources["line"])
if "line_orientation" in self.resources:
line_orient = pygame.image.load(self.resources["line_orientation"])
if "inner_color" in self.resources:
color = convert2list(self.resources["inner_color"])
if len(color) == 3:
self.color = (int(color[0]), int(color[1]), int(color[2]))
if len(color) == 4:
self.color = (int(color[0]), int(color[1]), int(color[2]), int(color[3]))
else:
self.color = (0, 0, 0, 0)
if corner is None:
if line is None:
print("No image given to create design.")
else:
if line_orient == "vertical":
line = pygame.transform.rotate(line, -90)
line_rect = line.get_rect()
size = line_rect.h
#crop the line to the wished string
line_string = pygame.Surface((1, size))
line_string.blit(line, (0, 0), pygame.Rect(pos, 0, 1, size))
line = line_string
line_rect = line.get_rect()
self.pixels = {}
self.pattern = pygame.Surface((1, size))
for a in range(size):
self.pattern.set_at((0, a), line.get_at((0, a)))
corner = pygame.Surface((size, size))
for a in range(size):
for x in range(size):
for y in range(size):
if x >= a and y >= a:
corner.set_at((x, y), self.pattern.get_at((0, a)))
else:
if line is None:
size = corner.get_height()
self.line = pygame.Surface((1, size))
for a in range(size):
self.line.set_at((0, a), corner.get_at((size - 1, a)))
return [line, corner]
def create_box(self, mode, rect):
posx = rect.x
posy = rect.y
width = rect.w
height = rect.height
border = self.modes[mode][0].get_height()
width += border * 2
height += border * 2
self.top = pygame.Surface((width, border))
#creating top frame line
for pos in range(width):
self.top.blit(self.modes[mode][0], pygame.Rect(pos, 0, 0, 0))
#blit left top corner
self.top.blit(self.modes[mode][1], pygame.Rect(0, 0, 0, 0))
#blit right top corner
self.top.blit(pygame.transform.flip(self.modes[mode][1], True, False),
pygame.Rect(width - border, 0, 0, 0))
#create bottom line
self.bottom = pygame.transform.flip(self.top, False, True)
#create left frame line
self.left = pygame.Surface((border, height))
tmp_line = pygame.transform.rotate(self.modes[mode][0], 90)
for pos in range(height):
self.left.blit(tmp_line, pygame.Rect(0, pos, 0, 0))
#create right frame line
self.right = pygame.transform.flip(self.left, True, False)
#Merge all together
final = pygame.Surface((width, height), pygame.SRCALPHA)
final.fill(self.color)
final.blit(self.left, pygame.Rect(0, 0, 0, 0))
final.blit(self.right, pygame.Rect(width - border, 0, 0, 0))
final.blit(self.top, pygame.Rect(0, 0, 0, 0))
final.blit(self.bottom, pygame.Rect(0, height - border, 0, 0))
self.box = final
self.pos = self.box.get_rect()
self.pos.x = posx - border
self.pos.y = posy - border
return self.box

@ -101,11 +101,11 @@ class menu():
self.__init__(self.menu_name, self.fade_step, self.fade_step2, self.fade_max,
self.variables, self.externals)
design = creator.create_outline("./assets/templates/nr1.design")
design.create_box(200, 40)
#design = creator.create_outline("./assets/templates/nr1.design")
#design.create_box(0, 0, 200, 40)
men = menu("savescreen", 0, 255, 255, {"savename": "This funny savegame"}, {})
men.menu.elems["surfs"]["test"] = [design.box, pygame.Rect(0, 0, 0, 0)]
men = menu("pause", 0, 255, 255, {"savename": "This funny savegame"}, {})
#men.menu.elems["surfs"]["test"] = [design.box, pygame.Rect(0, 0, 0, 0)]
run = True
while run:
events = men.run()

Loading…
Cancel
Save