Skip to content

Commit

Permalink
Fix syntax for Python3 and bump version number
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbueno committed Apr 19, 2019
1 parent 82db839 commit 89c3f56
Show file tree
Hide file tree
Showing 27 changed files with 1,395 additions and 1,573 deletions.
2 changes: 1 addition & 1 deletion examples/gui17.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def lkey(_event):
try:
code = compile(val,'<string>','single')
eval(code,globals(),_locals)
except:
except Exception:
e_type,e_value,e_traceback = sys.exc_info()
print('Traceback (most recent call last):')
traceback.print_tb(e_traceback,None,s)
Expand Down
2 changes: 1 addition & 1 deletion pgu/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"""

__version__ = '0.18'
__version__ = '0.21.dev0'


32 changes: 12 additions & 20 deletions pgu/ani.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,51 +9,45 @@
import math
import pygame

# Quick fix for python3
try:
xrange
except:
xrange = range

def _ani_load(tv,name,parts,frames,shape):
l = len(frames)
n = parts.pop()
if len(parts):
s = l/n
for i in xrange(0,n):
for i in range(0,n):
_ani_load(tv,name + ".%d"%i,parts[:],frames[s*i:s*(i+1)],shape)
return
for i in xrange(0,n):

for i in range(0,n):
tv.images[name+".%d"%i] = frames[i],shape

def ani_load(tv,name,img,size,shape,parts):
"""Load an animation from an image
Arguments:
Arguments:
tv -- vid to load into
name -- prefix name to give the images
image -- image to load anis from
size -- w,h size of image
shape -- shape of image (usually a subset of 0,0,w,h) used for collision detection
parts -- list of parts to divide the animation into
parts -- list of parts to divide the animation into
for example parts = [4,5] would yield 4 animations 5 frames long, 20 total
for example parts = [a,b,c] would yield ... images['name.a.b.c'] ..., a*b*c total
"""
parts = parts[:]
parts.reverse()
w,h = size
frames = []
for y in xrange(0,img.get_height(),h):
for x in xrange(0,img.get_width(),w):
for y in range(0,img.get_height(),h):
for x in range(0,img.get_width(),w):
frames.append(img.subsurface(x,y,w,h))
_ani_load(tv,name,parts,frames,shape)


def image_rotate(tv,name,img,shape,angles,diff=0):
"""Rotate an image and put it into tv.images
Arguments:
tv -- vid to load into
name -- prefix name to give the images
Expand All @@ -75,7 +69,7 @@ def image_rotate(tv,name,img,shape,angles,diff=0):
a2 = math.radians(a+diff)
#NOTE: the + and - are switched from the normal formula because of
#the weird way that pygame does the angle...
x2 = x*math.cos(a2) + y*math.sin(a2)
x2 = x*math.cos(a2) + y*math.sin(a2)
y2 = y*math.cos(a2) - x*math.sin(a2)
x2,y2 = x2+w2/2,y2+h2/2
minx = min(minx,x2)
Expand All @@ -85,5 +79,3 @@ def image_rotate(tv,name,img,shape,angles,diff=0):
r = pygame.Rect(minx,miny,maxx-minx,maxy-miny)
#((ww-w)/2,(hh-h)/2,w,h)
tv.images["%s.%d"%(name,a)] = img2,r


121 changes: 56 additions & 65 deletions pgu/fonts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,14 @@
import pygame
from pygame.locals import *

# Quick fix for python3
try:
xrange
except:
xrange = range

class TileFont:
"""Creates an instance of the TileFont class. Interface compatible
class TileFont(object):
"""Creates an instance of the TileFont class. Interface compatible
with pygame.Font
TileFonts are fonts that are stored in a tiled image. Where the image
opaque, it assumed that the font is visible. Font color is changed
TileFonts are fonts that are stored in a tiled image. Where the image
opaque, it assumed that the font is visible. Font color is changed
automatically, so it does not work with fonts with stylized coloring.
Arguments:
size -- the dimensions of the characters
hints -- a string of hints "abcdefg..."
Expand All @@ -31,104 +25,101 @@ class TileFont:
"""

def __init__(self,fname,size,hints,scale=None,sensitive=False):
def __init__(self, fname, size, hints, scale=None, sensitive=False):

self.image = pygame.image.load(fname)
w,h = self.image.get_width(),self.image.get_height()
tw,th = size

w, h = self.image.get_width(), self.image.get_height()
tw, th = size
if not scale: scale = size
self._size = size
self.scale = scale

self.chars = {}
x,y = 0,0
x, y = 0, 0
self.sensitive = sensitive
if not self.sensitive: hints = hints.lower()
for c in hints:
if c not in ('\r','\n','\t'):
img = self.image.subsurface(x,y,tw,th)
if c not in ('\r', '\n', '\t'):
img = self.image.subsurface(x, y, tw, th)
self.chars[c] = img
x += tw
if x >= w: x,y = 0,y+th
if x >= w: x, y = 0, y+th

self.colors = {}
def size(self,text):
tw,th = self.scale
return len(text)*tw,th
def render(self,text,antialias=0,color=(255,255,255),background=None):

def size(self, text):
tw, th = self.scale
return len(text)*tw, th

def render(self, text, antialias=0, color=(255, 255, 255), background=None):
size = self.size(text)
scale = self.scale
tw,th = self._size
tw, th = self._size
if background == None:
s = pygame.Surface(size).convert_alpha()
s.fill((0,0,0,0))
s.fill((0, 0, 0, 0))
else:
s = pygame.Surface(size).convert()
s.fill(background)

if not self.sensitive: text = text.lower()

if color not in self.colors: self.colors[color] = {}
colored = self.colors[color]
x,y = 0,0

x, y = 0, 0
for c in text:
if c in self.chars:
if c not in colored:
img = self.chars[c].convert_alpha()
for yy in xrange(0,th):
for xx in xrange(0,tw):
r,g,b,a = img.get_at((xx,yy))
for yy in range(0, th):
for xx in range(0, tw):
r, g, b, a = img.get_at((xx, yy))
if a > 128:
img.set_at((xx,yy),color)
img.set_at((xx, yy), color)
colored[c] = img
img = colored[c]
if scale != (tw,th): img = pygame.transform.scale(img,scale)
s.blit(img,(x,y))
if scale != (tw, th): img = pygame.transform.scale(img, scale)
s.blit(img, (x, y))
x += scale[0]
return s
class BorderFont:


class BorderFont(object):
"""A decorator for normal fonts, adds a border. Interface compatible with pygame.Font.
Arguments:
size -- width of border; defaults 0
color -- color of border; default (0,0,0)
color -- color of border; default (0, 0, 0)
"""
def __init__(self,font,size=1,color=(0,0,0)):

def __init__(self, font, size=1, color=(0, 0, 0)):
self.font = font
self._size = size
self.color = color
def size(self,text):
w,h = self.font.size(text)

def size(self, text):
w, h = self.font.size(text)
s = self._size
return w+s*2,h+s*2
def render(self,text,antialias=0,color=(255,255,255),background=None):
return w+s*2, h+s*2

def render(self, text, antialias=0, color=(255, 255, 255), background=None):
size = self.size(text)

if background == None:
s = pygame.Surface(size).convert_alpha()
s.fill((0,0,0,0))
s.fill((0, 0, 0, 0))
else:
s = pygame.Surface(size).convert()
s.fill(background)

bg = self.font.render(text,antialias,self.color)
fg = self.font.render(text,antialias,color)

si = self._size
dirs = [(-1,-1),(-1,0),(-1,1),(0,-1),(0,1),(1,-1),(1,0),(1,1)]
for dx,dy in dirs: s.blit(bg,(si+dx*si,si+dy*si))
s.blit(fg,(si,si))

return s
bg = self.font.render(text, antialias, self.color)
fg = self.font.render(text, antialias, color)

si = self._size
dirs = [(-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1)]
for dx, dy in dirs: s.blit(bg, (si+dx*si, si+dy*si))
s.blit(fg, (si, si))

return s
7 changes: 3 additions & 4 deletions pgu/gui/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"""Modules for creating a widget-based user interface. See the examples folder
"""Modules for creating a widget-based user interface. See the examples folder
for sample scripts that use this module."""

import pygame

# The basestring class was removed in Python 3, but we want to keep it to maintain
# The basestring class was removed in Python 3, but we want to keep it to maintain
# compatibility with previous versions of python.
try:
__builtins__["basestring"]
Expand All @@ -23,7 +23,7 @@
from .table import Table
from .document import Document
#html
from .area import SlideBox, ScrollArea, List
from .area import SlideBox, ScrollArea, List

from .form import Form
from .group import Group
Expand All @@ -41,4 +41,3 @@
from .textarea import TextArea

from .deprecated import Toolbox, action_open, action_setvalue, action_quit, action_exec

Loading

0 comments on commit 89c3f56

Please sign in to comment.