Hello everyone,
Im doing some UI tests and there is something wrong.
I created egg font via “egg-mkfont” and trying to write a Label class;
from direct.showbase.ShowBase import ShowBase
from panda3d.core import *
confVars="""
win-size 800 600
window-title UI-Test
show-frame-rate-meter 1
sync-video 0
"""
FONT_PATH='bin/neuera/fonts/egg/'
DEFAULT_FONT=None
DEFAULT_FONT_PPU=12
WIN_SIZE=(0,0)
ASPECT_RATIO=(0,0)
def pixelpos_to_aspect(x, y):
global WIN_SIZE,ASPECT_RATIO
norm_x = float(x)/(WIN_SIZE[1] * 0.5)-ASPECT_RATIO[0]
norm_y = ASPECT_RATIO[1]-float(y)/(WIN_SIZE[1] * 0.5)
if norm_x>=ASPECT_RATIO[0]:x-=10
if norm_y>=ASPECT_RATIO[1]:y+=10
return LVecBase3f(float(x)/(WIN_SIZE[1] * 0.5)-ASPECT_RATIO[0],0,ASPECT_RATIO[1]-float(y)/(WIN_SIZE[1] * 0.5))
def InitUI():
global FONT_PATH
global DEFAULT_FONT
global WIN_SIZE, ASPECT_RATIO
DEFAULT_FONT=base.loader.loadFont(FONT_PATH+'UbuntuMono-R.egg')
WIN_SIZE=(base.win.getXSize(),base.win.getYSize())
ASPECT_RATIO=[float(WIN_SIZE[0])/WIN_SIZE[1],1]
return True
class UiElement:
def __init__(self, parent=None, pos=(0, 0), size=(1, 1)):
self.parent = parent
if not parent:self.parent=aspect2d
self.pos = pos
self.size = size
self.cm = CardMaker('ui_element')
self.cm.setColor(0,0,0,0)
self.cm.setFrame(-size[0] / 2, size[0] / 2, -size[1] / 2, size[1] / 2)
self.np = self.parent.attachNewNode(self.cm.generate())
self.np.setPos(pos[0], 0, pos[1])
self.np.setTransparency(True)
def generate_text_image(_text, _imfont, _style=0, _w=8, _h=16, _space=0):
cell_w = _w
cell_h = _h
font_len = 96
font_chars = """ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`!@#$%^&*()-_=+[]{}\\|;:'"<>,.?/~` """
styles = 3
_text_base = PNMImage(len(_text) * cell_w, cell_h)
_text_base.alpha_fill(0.0)
imfont = PNMImage()
imfont.read('bin/neuera/fonts/IMFONT_' + _imfont + '.png')
for i, _char in enumerate(_text):
if _char in font_chars:
_char_index = font_chars.index(_char)
_char_image = PNMImage(cell_w, cell_h)
_char_image.alpha_fill(0.0)
_char_image.copySubImage(imfont, 0, 0, _char_index * cell_w - 1, _style * cell_h - 1, cell_w, cell_h)
_text_base.blendSubImage(_char_image, (i*cell_w)-(i*(2-_space)), 0, 0, 0, 255)
return _text_base
class Label(TextNode):
def __init__(self, parent=None, text='', pos=(0,0)):
global DEFAULT_FONT, DEFAULT_FONT_PPU, WIN_SIZE
TextNode.__init__(self,'LABEL')
if not parent:self.np=aspect2d.attachNewNode(self)
else:self.np=parent.attachNewNode(self)
self.setFont(DEFAULT_FONT)
self.setText(text)
#self.setAlign(self.ALeft)
scale = (2.0 * DEFAULT_FONT_PPU) / WIN_SIZE[1]
self.np.setScale(scale)
self.np.setPos(pos)
class uiTest(UiElement):
def __init__(self):
UiElement.__init__(self)
self.border_lt=Label(parent=self.np, text='+', pos=pixelpos_to_aspect(0,0))
self.coord_lt=Label(parent=self.np, text='0,0', pos=pixelpos_to_aspect(0,20))
self.border_rt=Label(parent=self.np, text='+', pos=pixelpos_to_aspect(WIN_SIZE[0],0))
self.coord_rt=Label(parent=self.np, text='800,0', pos=pixelpos_to_aspect(WIN_SIZE[0]-40,20))
self.border_lb=Label(parent=self.np, text='+', pos=pixelpos_to_aspect(0,WIN_SIZE[1]))
self.coord_lb=Label(parent=self.np, text='0,600', pos=pixelpos_to_aspect(0,WIN_SIZE[1]-10))
self.border_rb=Label(parent=self.np, text='+', pos=pixelpos_to_aspect(WIN_SIZE[0],WIN_SIZE[1]))
self.coord_lb=Label(parent=self.np, text='800,600', pos=pixelpos_to_aspect(WIN_SIZE[0]-50,WIN_SIZE[1]-10))
self.test_label=Label(parent=self.np, text='This is nice rendered text without anti-aliasing', pos=pixelpos_to_aspect(40,40))
self.test_label2=Label(parent=self.np, text="""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789`!@#$%^&*()-_=+[]{}\\|;:'"<>,.?/~` """, pos=pixelpos_to_aspect(40,60))
class MyApp(ShowBase):
def __init__(self):
loadPrcFileData("", confVars);
ShowBase.__init__(self)
self.login_screen=None
if InitUI():self.ui_loaded=True
self.createUiScreen()
def createUiScreen(self):
self.login_screen=uiTest()
app = MyApp()
app.run()
and this is the font png;
and result;
But as you can see, there is something wrong with some characters (like; !, -, _…)
They are fine in font png file but not in-game. Do you have any idea?