adding sprite loader to models
fixed some bugs in the ui module
This commit is contained in:
parent
6e691bed7b
commit
44861c14aa
3 changed files with 74 additions and 17 deletions
|
@ -31,6 +31,29 @@ class Model_Manager(object):
|
||||||
def create(self, mname):
|
def create(self, mname):
|
||||||
return self.models[mname].instance()
|
return self.models[mname].instance()
|
||||||
|
|
||||||
|
def create_sprite(self, name, path):
|
||||||
|
tf = self.get_texfile(path)
|
||||||
|
to = Texture()
|
||||||
|
to.load_from_texfile(tf)
|
||||||
|
mat = Material()
|
||||||
|
if noaa:
|
||||||
|
typeidx = enums.tt.noaa
|
||||||
|
else:
|
||||||
|
typeidx = enums.tt.diffuse
|
||||||
|
mat.textures[typeidx] = to
|
||||||
|
|
||||||
|
rl = RenderLayer()
|
||||||
|
rl.mesh = SpriteMesh()
|
||||||
|
rl.material = mat
|
||||||
|
rl.scale = [float(tf.w), float(tf.h), 1.0]
|
||||||
|
|
||||||
|
model = Model()
|
||||||
|
model.layers = [rl]
|
||||||
|
self.models[name] = model
|
||||||
|
|
||||||
|
return model.instance()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def get_textype(self, textype):
|
def get_textype(self, textype):
|
||||||
if isinstance(textype, TextureType):
|
if isinstance(textype, TextureType):
|
||||||
|
@ -56,6 +79,17 @@ class Model_Manager(object):
|
||||||
for tt in list(self.textypes.values()):
|
for tt in list(self.textypes.values()):
|
||||||
tt.initialize()
|
tt.initialize()
|
||||||
|
|
||||||
|
def get_texfile(self, path):
|
||||||
|
fp = files.mgr.canonize_path(path)
|
||||||
|
if not fp in self.texture_files:
|
||||||
|
tf = TextureFile()
|
||||||
|
tf.load(fp)
|
||||||
|
self.texture_files[fp] = tf
|
||||||
|
self.texture_ids[tf.id] = tf
|
||||||
|
else:
|
||||||
|
tf = self.texture_files[fp]
|
||||||
|
return tf
|
||||||
|
|
||||||
|
|
||||||
def load_textures(self):
|
def load_textures(self):
|
||||||
texdata = gamedata.get('textures')
|
texdata = gamedata.get('textures')
|
||||||
|
@ -64,14 +98,7 @@ class Model_Manager(object):
|
||||||
self.textures = {}
|
self.textures = {}
|
||||||
self.materials = {}
|
self.materials = {}
|
||||||
for texd in texdata.texture:
|
for texd in texdata.texture:
|
||||||
fp = files.mgr.canonize_path(texd['file'])
|
tf = self.get_texfile(texd['file'])
|
||||||
if not fp in self.texture_files:
|
|
||||||
tf = TextureFile()
|
|
||||||
tf.load(fp)
|
|
||||||
self.texture_files[fp] = tf
|
|
||||||
self.texture_ids[tf.id] = tf
|
|
||||||
else:
|
|
||||||
tf = self.texture_files[fp]
|
|
||||||
to = Texture()
|
to = Texture()
|
||||||
to.load(texd, tf)
|
to.load(texd, tf)
|
||||||
self.textures[to.id] = to
|
self.textures[to.id] = to
|
||||||
|
@ -173,7 +200,16 @@ class Texture(object):
|
||||||
|
|
||||||
def texcoords_direct(self, u, v):
|
def texcoords_direct(self, u, v):
|
||||||
return (u, v)
|
return (u, v)
|
||||||
|
|
||||||
|
def load_from_texfile(self, tf):
|
||||||
|
self.texid = tf.id
|
||||||
|
self.x = 0
|
||||||
|
self.y = 0
|
||||||
|
self.w = tf.w
|
||||||
|
self.h = tf.h
|
||||||
|
self.texcoords = self.texcoords_direct
|
||||||
|
|
||||||
|
|
||||||
def load(self, data, tf):
|
def load(self, data, tf):
|
||||||
self.id = data['id']
|
self.id = data['id']
|
||||||
self.texid = tf.id
|
self.texid = tf.id
|
||||||
|
|
|
@ -6,6 +6,8 @@ from . import models
|
||||||
from . import roc_main
|
from . import roc_main
|
||||||
from . import roc_engine
|
from . import roc_engine
|
||||||
from . import config
|
from . import config
|
||||||
|
from . import ui
|
||||||
|
|
||||||
base_engine = roc_engine.base_engine
|
base_engine = roc_engine.base_engine
|
||||||
|
|
||||||
def init2d():
|
def init2d():
|
||||||
|
@ -28,6 +30,7 @@ def init(videoinit):
|
||||||
video.enable_vsync()
|
video.enable_vsync()
|
||||||
|
|
||||||
models.init()
|
models.init()
|
||||||
|
ui.init()
|
||||||
|
|
||||||
def set_engine(new_engine):
|
def set_engine(new_engine):
|
||||||
roc_main.set_engine(new_engine)
|
roc_main.set_engine(new_engine)
|
||||||
|
|
34
roc/ui.py
34
roc/ui.py
|
@ -8,6 +8,7 @@ from . import inputs
|
||||||
from . import depgraph
|
from . import depgraph
|
||||||
from . import gamedata
|
from . import gamedata
|
||||||
import itertools
|
import itertools
|
||||||
|
import inspect
|
||||||
|
|
||||||
class UITheme(object):
|
class UITheme(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -38,9 +39,12 @@ class UIFrame(object):
|
||||||
|
|
||||||
|
|
||||||
def constrain_size(self, x, y):
|
def constrain_size(self, x, y):
|
||||||
|
print("starting constrained to %s, %s" % (x, y))
|
||||||
|
|
||||||
if self.content == ui_frametype.grid:
|
if self.content == ui_frametype.grid:
|
||||||
x += self.padding[0] * len(self.data) * 2
|
x += self.padding[0] * len(self.data) * 2
|
||||||
y += self.padding[1] * len(self.data[0]) * 2
|
y += self.padding[1] * len(self.data[0]) * 2
|
||||||
|
print("Added %s, %s of padding %s, %s" % (x, y, self.padding[0], self.padding[1]))
|
||||||
else:
|
else:
|
||||||
x += self.padding[0] * 2
|
x += self.padding[0] * 2
|
||||||
y += self.padding[1] * 2
|
y += self.padding[1] * 2
|
||||||
|
@ -51,6 +55,7 @@ class UIFrame(object):
|
||||||
if self.maxsize:
|
if self.maxsize:
|
||||||
x = min(self.maxsize[0], x)
|
x = min(self.maxsize[0], x)
|
||||||
y = min(self.maxsize[1], y)
|
y = min(self.maxsize[1], y)
|
||||||
|
print("Constrained to %s, %s" % (x, y))
|
||||||
return (x, y)
|
return (x, y)
|
||||||
|
|
||||||
|
|
||||||
|
@ -202,6 +207,7 @@ class UIFrame(object):
|
||||||
col.autosize()
|
col.autosize()
|
||||||
self.gridsize[0][ic] = max(self.gridsize[0][ic], col.size[0])
|
self.gridsize[0][ic] = max(self.gridsize[0][ic], col.size[0])
|
||||||
self.gridsize[1][ir] = max(self.gridsize[1][ir], col.size[1])
|
self.gridsize[1][ir] = max(self.gridsize[1][ir], col.size[1])
|
||||||
|
print(("cell autosize: %s, %s" % (self.gridsize[1][ir], self.gridsize[0][ic])))
|
||||||
|
|
||||||
self.size = self.constrain_size(sum(self.gridsize[0]), sum(self.gridsize[1]))
|
self.size = self.constrain_size(sum(self.gridsize[0]), sum(self.gridsize[1]))
|
||||||
print(("grid CONSTRAINED: %s - %s" % (self.size, self.padding)))
|
print(("grid CONSTRAINED: %s - %s" % (self.size, self.padding)))
|
||||||
|
@ -232,7 +238,7 @@ class UIFrame(object):
|
||||||
for ic, col in enumerate(row):
|
for ic, col in enumerate(row):
|
||||||
xlo = self.size[0] - sum(self.gridsize[0])
|
xlo = self.size[0] - sum(self.gridsize[0])
|
||||||
ylo = self.size[1] - sum(self.gridsize[1])
|
ylo = self.size[1] - sum(self.gridsize[1])
|
||||||
print(("Stretching cell %s from %s to %s" % ((ic, ir), self.size, (self.size[0]+xlo, self.size[1]+ylo))))
|
#print(("Stretching cell %s from %s to %s" % ((ic, ir), self.size, (self.size[0]+xlo, self.size[1]+ylo))))
|
||||||
x = 0
|
x = 0
|
||||||
y = 0
|
y = 0
|
||||||
if xlo > 0:
|
if xlo > 0:
|
||||||
|
@ -571,43 +577,54 @@ class UIManager(object):
|
||||||
return uf
|
return uf
|
||||||
|
|
||||||
|
|
||||||
def make_button(self, text, clickfunc):
|
def make_button(self, text, clickfunc, padding=(0,0)):
|
||||||
text = models.TextModel(self.theme.font, text)
|
text = models.TextModel(self.theme.font, text)
|
||||||
textframe = UIFrame(ui_frametype.model, text)
|
textframe = UIFrame(ui_frametype.model, text)
|
||||||
textframe.postype = ui_postype.center
|
textframe.postype = ui_postype.center
|
||||||
|
textframe.filltype = ui_filltype.none
|
||||||
|
|
||||||
btnframe = self.make_box("ui_button", textframe, (0.3, 0.3, 1.0, 1.0))
|
paddingframe = UIFrame(ui_frametype.composite, [textframe])
|
||||||
|
paddingframe.padding = padding
|
||||||
|
paddingframe.postype = ui_postype.center
|
||||||
|
|
||||||
|
btnframe = self.make_box("ui_button", paddingframe, (0.3, 0.3, 1.0, 1.0))
|
||||||
|
btnframe.filltype = ui_filltype.none
|
||||||
btnframe.func = clickfunc
|
btnframe.func = clickfunc
|
||||||
return btnframe
|
return btnframe
|
||||||
|
|
||||||
|
|
||||||
def load_button(self, data):
|
def load_button(self, data):
|
||||||
name = data["id"]
|
name = data["id"]
|
||||||
btn = self.make_button(data["label"], data["callback"])
|
print("loading button %s" % (name,))
|
||||||
btn.padding = (data.padding["x"], data.padding["y"])
|
btn = self.make_button(data["label"], data["callback"], (data.padding["x"], data.padding["y"]))
|
||||||
self.frame_library[name] = btn
|
self.frame_library[name] = btn
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def load_frame(self, data):
|
def load_frame(self, data):
|
||||||
name = data["id"]
|
name = data["id"]
|
||||||
|
print("loading frame %s" % (name,))
|
||||||
framecolor = (data.color['r'], data.color['g'], data.color['b'], data.color['a'])
|
framecolor = (data.color['r'], data.color['g'], data.color['b'], data.color['a'])
|
||||||
frame = self.make_box(name, self.frame_library[data.content['id']], framecolor)
|
frame = self.make_box("ui_frame", self.frame_library[data.content['id']], framecolor)
|
||||||
frame.postype = enums.index(ui_postype, data['postype'])
|
frame.postype = enums.index(ui_postype, data['postype'])
|
||||||
frame.filltype = enums.index(ui_filltype, data['filltype'])
|
frame.filltype = enums.index(ui_filltype, data['filltype'])
|
||||||
|
frame.padding = (data.padding['x'], data.padding['y'])
|
||||||
|
|
||||||
self.frame_library[name] = frame
|
self.frame_library[name] = frame
|
||||||
|
|
||||||
def load_wrapper(self, data):
|
def load_wrapper(self, data):
|
||||||
name = data["id"]
|
name = data["id"]
|
||||||
|
print("loading wrapper %s" % (name,))
|
||||||
frame = UIFrame(ui_frametype.composite, [self.frame_library[data.content['id']]])
|
frame = UIFrame(ui_frametype.composite, [self.frame_library[data.content['id']]])
|
||||||
frame.postype = enums.index(ui_postype, data['postype'])
|
frame.postype = enums.index(ui_postype, data['postype'])
|
||||||
frame.filltype = enums.index(ui_filltype, data['filltype'])
|
frame.filltype = enums.index(ui_filltype, data['filltype'])
|
||||||
|
frame.padding = (data.padding['x'], data.padding['y'])
|
||||||
|
|
||||||
self.frame_library[name] = frame
|
self.frame_library[name] = frame
|
||||||
|
|
||||||
def load_grid(self, data):
|
def load_grid(self, data):
|
||||||
name = data["id"]
|
name = data["id"]
|
||||||
|
print("loading grid %s" % (name,))
|
||||||
rowcols = []
|
rowcols = []
|
||||||
for r in data.row:
|
for r in data.row:
|
||||||
rnum = r['number']
|
rnum = r['number']
|
||||||
|
@ -615,13 +632,14 @@ class UIManager(object):
|
||||||
for c in r.col:
|
for c in r.col:
|
||||||
cnum = c['number']
|
cnum = c['number']
|
||||||
rdata.append((cnum, self.frame_library[c.content['id']]))
|
rdata.append((cnum, self.frame_library[c.content['id']]))
|
||||||
rdata = zip(*sorted(rdata))[1]
|
rdata = list(zip(*sorted(rdata)))[1]
|
||||||
rowcols.append((rnum, rdata))
|
rowcols.append((rnum, rdata))
|
||||||
rowcols = zip(*sorted(rowcols))[1]
|
rowcols = list(zip(*sorted(rowcols)))[1]
|
||||||
|
|
||||||
frame = UIFrame(ui_frametype.grid, rowcols)
|
frame = UIFrame(ui_frametype.grid, rowcols)
|
||||||
frame.postype = enums.index(ui_postype, data['postype'])
|
frame.postype = enums.index(ui_postype, data['postype'])
|
||||||
frame.filltype = enums.index(ui_filltype, data['filltype'])
|
frame.filltype = enums.index(ui_filltype, data['filltype'])
|
||||||
|
frame.padding = (data.padding['x'], data.padding['y'])
|
||||||
|
|
||||||
self.frame_library[name] = frame
|
self.frame_library[name] = frame
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue