From 44861c14aaa58c8b04b642fed22c963b1fa68bcf Mon Sep 17 00:00:00 2001 From: cecilkorik Date: Tue, 16 May 2017 22:52:00 -0400 Subject: [PATCH] adding sprite loader to models fixed some bugs in the ui module --- roc/models.py | 54 ++++++++++++++++++++++++++++++++++++++++--------- roc/roc_core.py | 3 +++ roc/ui.py | 34 +++++++++++++++++++++++-------- 3 files changed, 74 insertions(+), 17 deletions(-) diff --git a/roc/models.py b/roc/models.py index ceaec45..c0426b7 100755 --- a/roc/models.py +++ b/roc/models.py @@ -31,6 +31,29 @@ class Model_Manager(object): def create(self, mname): 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): if isinstance(textype, TextureType): @@ -56,6 +79,17 @@ class Model_Manager(object): for tt in list(self.textypes.values()): 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): texdata = gamedata.get('textures') @@ -64,14 +98,7 @@ class Model_Manager(object): self.textures = {} self.materials = {} for texd in texdata.texture: - fp = files.mgr.canonize_path(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] + tf = self.get_texfile(texd['file']) to = Texture() to.load(texd, tf) self.textures[to.id] = to @@ -173,7 +200,16 @@ class Texture(object): def texcoords_direct(self, 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): self.id = data['id'] self.texid = tf.id diff --git a/roc/roc_core.py b/roc/roc_core.py index aa3a0c9..f447cfa 100755 --- a/roc/roc_core.py +++ b/roc/roc_core.py @@ -6,6 +6,8 @@ from . import models from . import roc_main from . import roc_engine from . import config +from . import ui + base_engine = roc_engine.base_engine def init2d(): @@ -28,6 +30,7 @@ def init(videoinit): video.enable_vsync() models.init() + ui.init() def set_engine(new_engine): roc_main.set_engine(new_engine) diff --git a/roc/ui.py b/roc/ui.py index b245021..9aa342c 100755 --- a/roc/ui.py +++ b/roc/ui.py @@ -8,6 +8,7 @@ from . import inputs from . import depgraph from . import gamedata import itertools +import inspect class UITheme(object): def __init__(self): @@ -38,9 +39,12 @@ class UIFrame(object): def constrain_size(self, x, y): + print("starting constrained to %s, %s" % (x, y)) + if self.content == ui_frametype.grid: x += self.padding[0] * len(self.data) * 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: x += self.padding[0] * 2 y += self.padding[1] * 2 @@ -51,6 +55,7 @@ class UIFrame(object): if self.maxsize: x = min(self.maxsize[0], x) y = min(self.maxsize[1], y) + print("Constrained to %s, %s" % (x, y)) return (x, y) @@ -202,6 +207,7 @@ class UIFrame(object): col.autosize() self.gridsize[0][ic] = max(self.gridsize[0][ic], col.size[0]) 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])) print(("grid CONSTRAINED: %s - %s" % (self.size, self.padding))) @@ -232,7 +238,7 @@ class UIFrame(object): for ic, col in enumerate(row): xlo = self.size[0] - sum(self.gridsize[0]) 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 y = 0 if xlo > 0: @@ -571,43 +577,54 @@ class UIManager(object): return uf - def make_button(self, text, clickfunc): + def make_button(self, text, clickfunc, padding=(0,0)): text = models.TextModel(self.theme.font, text) textframe = UIFrame(ui_frametype.model, text) 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 return btnframe def load_button(self, data): name = data["id"] - btn = self.make_button(data["label"], data["callback"]) - btn.padding = (data.padding["x"], data.padding["y"]) + print("loading button %s" % (name,)) + btn = self.make_button(data["label"], data["callback"], (data.padding["x"], data.padding["y"])) self.frame_library[name] = btn def load_frame(self, data): name = data["id"] + print("loading frame %s" % (name,)) 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.filltype = enums.index(ui_filltype, data['filltype']) + frame.padding = (data.padding['x'], data.padding['y']) self.frame_library[name] = frame def load_wrapper(self, data): name = data["id"] + print("loading wrapper %s" % (name,)) frame = UIFrame(ui_frametype.composite, [self.frame_library[data.content['id']]]) frame.postype = enums.index(ui_postype, data['postype']) frame.filltype = enums.index(ui_filltype, data['filltype']) + frame.padding = (data.padding['x'], data.padding['y']) self.frame_library[name] = frame def load_grid(self, data): name = data["id"] + print("loading grid %s" % (name,)) rowcols = [] for r in data.row: rnum = r['number'] @@ -615,13 +632,14 @@ class UIManager(object): for c in r.col: cnum = c['number'] 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 = zip(*sorted(rowcols))[1] + rowcols = list(zip(*sorted(rowcols)))[1] frame = UIFrame(ui_frametype.grid, rowcols) frame.postype = enums.index(ui_postype, data['postype']) frame.filltype = enums.index(ui_filltype, data['filltype']) + frame.padding = (data.padding['x'], data.padding['y']) self.frame_library[name] = frame