From 6e691bed7bec2ab15fc2d43f226d9b0fa401620e Mon Sep 17 00:00:00 2001 From: cecilkorik Date: Tue, 16 May 2017 03:04:42 -0400 Subject: [PATCH] mostly finished conversion of roc to importable module improved ui component further, now capable of importing UI layouts from xml data files --- roc/__init__.py | 24 +-- roc/camera.py | 2 +- roc/config.py | 2 +- roc/depgraph.py | 116 ++++++++++++++ roc/gamedata.py | 8 +- roc/inputs.py | 2 +- roc/lua.py | 40 +++++ roc/lupa_sandbox.py | 40 +++++ roc/models.py | 8 +- roc/physics.py | 2 +- roc/pipeline.py | 6 +- roc/platform.py | 6 +- roc/roc_core.py | 14 +- roc/roc_engine.py | 4 +- roc/roc_main.py | 14 +- roc/shader.py | 4 +- roc/ui.py | 373 ++++++++++++++++++++++++++++++++------------ roc/video.py | 6 +- 18 files changed, 519 insertions(+), 152 deletions(-) create mode 100644 roc/depgraph.py create mode 100644 roc/lua.py create mode 100644 roc/lupa_sandbox.py diff --git a/roc/__init__.py b/roc/__init__.py index 6a72088..bce5a84 100755 --- a/roc/__init__.py +++ b/roc/__init__.py @@ -1,15 +1,15 @@ -from roc_core import * -import roc_engine -import pipeline -import inputs -import physics -import models -import gamedata -import enums -import files -import gametimer -import shader -import video +from .roc_core import * +from . import roc_engine +from . import pipeline +from . import inputs +from . import physics +from . import models +from . import gamedata +from . import enums +from . import files +from . import gametimer +from . import shader +from . import video #from .universe import * #from .pipeline import * diff --git a/roc/camera.py b/roc/camera.py index 5e84c93..aa4f358 100755 --- a/roc/camera.py +++ b/roc/camera.py @@ -3,7 +3,7 @@ import math from quat import * from py3dutil import vect from OpenGL.GL import * -import files +from . import files class game_camera(game_object): def __init__(self): diff --git a/roc/config.py b/roc/config.py index 1e85d31..1d45637 100644 --- a/roc/config.py +++ b/roc/config.py @@ -1,5 +1,5 @@ import os -import files +from . import files class configmanager(object): def __init__(self): diff --git a/roc/depgraph.py b/roc/depgraph.py new file mode 100644 index 0000000..c62c82d --- /dev/null +++ b/roc/depgraph.py @@ -0,0 +1,116 @@ +class CircularReferenceInternal(ValueError): + def __init__(self, msg, node): + super(CircularReferenceInternal, self).__init__(self, msg) + self.node = node +class CircularReferenceError(ValueError): + pass + +class DepGraph(object): + def __init__(self): + self.graph = [] + self.reference_map = {} + self.resolving_map = {} + self.resolved_map = {} + self.resolution = [] + self.resolving = [] + + def add(self, ident, deps): + dgn = (ident, deps) + self.graph.append(dgn) + self.reference_map[ident] = dgn + + def resolve_node(self, node): + self.resolving.append(node[0]) + if node[0] in self.resolving_map: + raise CircularReferenceInternal("Circular reference", node[0]) + + if node[0] in self.resolved_map: + #print("already resolved") + return + + self.resolving_map[node[0]] = 1 + if node[1]: + for dep in node[1]: + self.resolve_node(self.reference_map[dep]) + self.resolution.append(node[0]) + self.resolved_map[node[0]] = node + + + + def resolve(self): + self.resolved_map = {} + self.resolution = [] + for g in self.graph: + self.resolving_map = {} + self.resolving = [] + err = None + try: + self.resolve_node(g) + except CircularReferenceInternal: + circ = [] + for v in reversed(self.resolving): + if v in self.resolved_map: + continue + if v == self.resolving[-1]: + break + circ.append(v) + + err = CircularReferenceError("Circular reference detected between %s" % (tuple(circ),)) + + if err: + raise err + + + def bottomup_iter(self): + return self.resolution + + + + + + + + +if __name__ == "__main__": + + dg = DepGraph() + dg.add("a", ["d", "c"]) + dg.add("b", ["c"]) + dg.add("d", []) + dg.add("c", []) + dg.resolve() + print(list(dg.bottomup_iter())) + + dg = DepGraph() + dg.add("a", ["d", "c"]) + dg.add("b", ["c"]) + dg.add("d", []) + dg.add("e", []) + dg.add("f", ["e"]) + dg.add("ko", ["f"]) + dg.add("j", []) + dg.add("d", []) + dg.add("c", []) + dg.resolve() + print(list(dg.bottomup_iter())) + + try: + dg = DepGraph() + dg.add("a", ["d", "c"]) + dg.add("b", ["c"]) + dg.add("d", []) + dg.add("c", ["j"]) + dg.resolve() + print(list(dg.bottomup_iter())) + except: + print("error (as expected)") + + dg = DepGraph() + dg.add("a", ["d", "c"]) + dg.add("b", ["c"]) + dg.add("d", []) + dg.add("c", ["d"]) + dg.resolve() + print(list(dg.bottomup_iter())) + + diff --git a/roc/gamedata.py b/roc/gamedata.py index 29c9470..4a22519 100755 --- a/roc/gamedata.py +++ b/roc/gamedata.py @@ -4,9 +4,9 @@ try: from xml.etree.cElementTree import parse except ImportError: from elementtree.ElementTree import parse -import files -import shader -from deprecate import deprecated +from . import files +from . import shader +from .deprecate import deprecated class MissingNode(object): def __init__(self): @@ -178,7 +178,7 @@ class XMLGameDataReader(object): for child in xml.getchildren(): if not child.tag in tagdict: - raise ValueError + raise ValueError("Unrecognized tag %s" % (child.tag,)) continue binchild = tagdict[child.tag] xmlchild = child diff --git a/roc/inputs.py b/roc/inputs.py index 61680b3..b41a7fa 100755 --- a/roc/inputs.py +++ b/roc/inputs.py @@ -1,5 +1,5 @@ from pygame.locals import * -import video +from . import video state = {} commands = {} diff --git a/roc/lua.py b/roc/lua.py new file mode 100644 index 0000000..81f809f --- /dev/null +++ b/roc/lua.py @@ -0,0 +1,40 @@ +import lupa + +sandbox = """ +function sandbox_enter(func) + sandbox_env = { + ipairs = ipairs, + next = next, + pairs = pairs, + pcall = pcall, + tonumber = tonumber, + tostring = tostring, + type = type, + unpack = unpack, + coroutine = { create = coroutine.create, resume = coroutine.resume, + running = coroutine.running, status = coroutine.status, + wrap = coroutine.wrap }, + string = { byte = string.byte, char = string.char, find = string.find, + format = string.format, gmatch = string.gmatch, gsub = string.gsub, + len = string.len, lower = string.lower, match = string.match, + rep = string.rep, reverse = string.reverse, sub = string.sub, + upper = string.upper }, + table = { insert = table.insert, maxn = table.maxn, remove = table.remove, + sort = table.sort }, + math = { abs = math.abs, acos = math.acos, asin = math.asin, + atan = math.atan, atan2 = math.atan2, ceil = math.ceil, cos = math.cos, + cosh = math.cosh, deg = math.deg, exp = math.exp, floor = math.floor, + fmod = math.fmod, frexp = math.frexp, huge = math.huge, + ldexp = math.ldexp, log = math.log, log10 = math.log10, max = math.max, + min = math.min, modf = math.modf, pi = math.pi, pow = math.pow, + rad = math.rad, random = math.random, sin = math.sin, sinh = math.sinh, + sqrt = math.sqrt, tan = math.tan, tanh = math.tanh }, + os = { clock = os.clock, difftime = os.difftime, time = os.time }, + } + setfenv(func, sandbox_env) + return pcall(func) +end + +""" + + diff --git a/roc/lupa_sandbox.py b/roc/lupa_sandbox.py new file mode 100644 index 0000000..81f809f --- /dev/null +++ b/roc/lupa_sandbox.py @@ -0,0 +1,40 @@ +import lupa + +sandbox = """ +function sandbox_enter(func) + sandbox_env = { + ipairs = ipairs, + next = next, + pairs = pairs, + pcall = pcall, + tonumber = tonumber, + tostring = tostring, + type = type, + unpack = unpack, + coroutine = { create = coroutine.create, resume = coroutine.resume, + running = coroutine.running, status = coroutine.status, + wrap = coroutine.wrap }, + string = { byte = string.byte, char = string.char, find = string.find, + format = string.format, gmatch = string.gmatch, gsub = string.gsub, + len = string.len, lower = string.lower, match = string.match, + rep = string.rep, reverse = string.reverse, sub = string.sub, + upper = string.upper }, + table = { insert = table.insert, maxn = table.maxn, remove = table.remove, + sort = table.sort }, + math = { abs = math.abs, acos = math.acos, asin = math.asin, + atan = math.atan, atan2 = math.atan2, ceil = math.ceil, cos = math.cos, + cosh = math.cosh, deg = math.deg, exp = math.exp, floor = math.floor, + fmod = math.fmod, frexp = math.frexp, huge = math.huge, + ldexp = math.ldexp, log = math.log, log10 = math.log10, max = math.max, + min = math.min, modf = math.modf, pi = math.pi, pow = math.pow, + rad = math.rad, random = math.random, sin = math.sin, sinh = math.sinh, + sqrt = math.sqrt, tan = math.tan, tanh = math.tanh }, + os = { clock = os.clock, difftime = os.difftime, time = os.time }, + } + setfenv(func, sandbox_env) + return pcall(func) +end + +""" + + diff --git a/roc/models.py b/roc/models.py index 23c9dc3..ceaec45 100755 --- a/roc/models.py +++ b/roc/models.py @@ -1,12 +1,12 @@ import os import glob -import enums -import files -import gamedata +from . import enums +from . import files +from . import gamedata import pygame from OpenGL.GL import * from py3dutil import vect -import fonts +from . import fonts import collections class Vertex(object): diff --git a/roc/physics.py b/roc/physics.py index 136cf2b..4f7e4f3 100755 --- a/roc/physics.py +++ b/roc/physics.py @@ -1,5 +1,5 @@ from py3dutil import vect, quat -import gametimer +from . import gametimer import math floor = math.floor diff --git a/roc/pipeline.py b/roc/pipeline.py index 3462a87..d62e1ad 100755 --- a/roc/pipeline.py +++ b/roc/pipeline.py @@ -1,8 +1,8 @@ from OpenGL.GL import * -import shader -import physics +from . import shader +from . import physics from py3dutil import * -from enums import * +from .enums import * class pipeline_manager(object): def __init__(self): diff --git a/roc/platform.py b/roc/platform.py index 08f6a71..d8ed1ee 100644 --- a/roc/platform.py +++ b/roc/platform.py @@ -1,9 +1,9 @@ import sys if sys.platform == 'win32': - from platform_win32 import * + from .platform_win32 import * elif sys.platform == 'darwin': - from platform_darwin import * + from .platform_darwin import * elif sys.platform in ('posix', 'linux'): - from platform_posix import * + from .platform_posix import * else: raise NotImplementedError("Not ported to this platform") diff --git a/roc/roc_core.py b/roc/roc_core.py index b6f6c6e..aa3a0c9 100755 --- a/roc/roc_core.py +++ b/roc/roc_core.py @@ -1,11 +1,11 @@ import pygame -import files -import video -import shader -import models -import roc_main -import roc_engine -import config +from . import files +from . import video +from . import shader +from . import models +from . import roc_main +from . import roc_engine +from . import config base_engine = roc_engine.base_engine def init2d(): diff --git a/roc/roc_engine.py b/roc/roc_engine.py index 571854b..8a9e355 100755 --- a/roc/roc_engine.py +++ b/roc/roc_engine.py @@ -1,5 +1,5 @@ -import models -import pipeline +from . import models +from . import pipeline import pygame from OpenGL.GL import * from py3dutil import vect, quat diff --git a/roc/roc_main.py b/roc/roc_main.py index d1c91ea..dfe2ac0 100755 --- a/roc/roc_main.py +++ b/roc/roc_main.py @@ -1,12 +1,12 @@ import pygame from pygame.locals import * -import gamedata -import gametimer -import video -import shader -import inputs -import models -import config +from . import gamedata +from . import gametimer +from . import video +from . import shader +from . import inputs +from . import models +from . import config from OpenGL.GL import * diff --git a/roc/shader.py b/roc/shader.py index 7f9eb39..9c04f1c 100755 --- a/roc/shader.py +++ b/roc/shader.py @@ -7,7 +7,7 @@ except ImportError: pass from OpenGL.arrays import GLintArray, GLcharArray, GLsizeiArray, GLcharARBArray import sys -import gamedata +from . import gamedata class shader_manager(object): @@ -84,7 +84,7 @@ class shader_manager(object): return self.initialized = True - import files + from . import files #print glGetString(GL_EXTENSIONS) if True: self.use_gl2_shaders() diff --git a/roc/ui.py b/roc/ui.py index 60b7d0e..b245021 100755 --- a/roc/ui.py +++ b/roc/ui.py @@ -1,11 +1,19 @@ -import enums -from enums import ui_frametype, ui_postype, ui_filltype +from . import enums +from .enums import ui_frametype, ui_postype, ui_filltype from pygame.locals import * -import models -import video +from . import models +from . import video from OpenGL.GL import * -import inputs - +from . import inputs +from . import depgraph +from . import gamedata +import itertools + +class UITheme(object): + def __init__(self): + self.font = "micross20" + + class UIFrame(object): def __init__(self, content, contentdata): self.func = None @@ -23,7 +31,10 @@ class UIFrame(object): self.draggable = False self.padding = (0, 0) self.color = None - self.stretchiness = None + if content == ui_frametype.grid: + self.set_stretch_equal() + else: + self.stretchiness = None def constrain_size(self, x, y): @@ -58,18 +69,19 @@ class UIFrame(object): if col == child: x = self.gridsize[0][j] y = self.gridsize[1][i] - if False and self.stretchiness: - xlo = self.size[0] - sum(self.gridsize[0]) - ylo = self.size[1] - sum(self.gridsize[1]) - if xlo > 0: - xs = self.stretchiness[0][j] - xst = sum(self.stretchiness[0]) - x += int(float(xlo) * float(xs) / float(xst)) - if ylo > 0: - ys = self.stretchiness[1][i] - yst = sum(self.stretchiness[1]) - y += int(float(ylo) * float(ys) / float(yst)) - + # not sure why I originally disabled this but it seems to work ok for now? + #if self.stretchiness: + # xlo = self.size[0] - sum(self.gridsize[0]) + # ylo = self.size[1] - sum(self.gridsize[1]) + # if xlo > 0: + # xs = self.stretchiness[0][j] + # xst = sum(self.stretchiness[0]) + # x += int(float(xlo) * float(xs) / float(xst)) + # if ylo > 0: + # ys = self.stretchiness[1][i] + # yst = sum(self.stretchiness[1]) + # y += int(float(ylo) * float(ys) / float(yst)) + # return (x, y) else: @@ -98,6 +110,39 @@ class UIFrame(object): return (0, 0) + def set_stretch_cell(self, col, row): + if self.content != ui_frametype.grid: + raise TypeError("Stretch ratios only apply to grid type UIFrames") + stretchiness_col = [0] * len(self.data) + stretchiness_row = [0] * len(self.data[0]) + + stretchiness_col[col] = 1 + stretchiness_row[row] = 1 + + self.set_stretch_ratios(stretchiness_col, stretchiness_row) + + def set_stretch_equal(self): + if self.content != ui_frametype.grid: + raise TypeError("Stretch ratios only apply to grid type UIFrames") + stretchiness_col = [1] * len(self.data) + stretchiness_row = [1] * len(self.data[0]) + + self.set_stretch_ratios(stretchiness_col, stretchiness_row) + + + def set_stretch_ratios(self, stretchiness_col, stretchiness_row): + if self.content != ui_frametype.grid: + raise TypeError("Stretch ratios only apply to grid type UIFrames") + if len(self.data) != len(stretchiness_col): + raise ValueError("Grid has %d columns, but %d stretch ratios were provided" % (len(self.data), len(stretchiness_col))) + if len(self.data[0]) != len(stretchiness_row): + raise ValueError("Grid has %d rows, but %d stretch ratios were provided" % (len(self.data[0]), len(stretchiness_row))) + + stretchiness_col = [float(x) / float(sum(stretchiness_col)) for x in stretchiness_col] + stretchiness_row = [float(y) / float(sum(stretchiness_row)) for y in stretchiness_row] + + self.stretchiness = (tuple(stretchiness_col), tuple(stretchiness_row)) + def autopos(self): if self.postype == ui_postype.center: ppos = self.get_parent_pos() @@ -123,17 +168,17 @@ class UIFrame(object): self.pos = self.posset alist = [x for x in self.ancestors()] - if mgr.debugobj in alist: - print(("FUCK. OK DUDER. I AM YOUR DEBUG OBJECT: %s" % (alist.index(mgr.debugobj),))) - print((self.parent.pos)) - print((self.get_parent_pos())) - print((self.parent.get_child_pos(self))) - #print self.fill == ui_filltype.none - #print self.postype == ui_postype.center - print((self.pos)) - #print self.gridsize - - print("END DEBUG OBJECT") + #if mgr.debugobj in alist: + # print(("FUCK. OK DUDER. I AM YOUR DEBUG OBJECT: %s" % (alist.index(mgr.debugobj),))) + # print((self.parent.pos)) + # print((self.get_parent_pos())) + # print((self.parent.get_child_pos(self))) + # #print self.fill == ui_filltype.none + # #print self.postype == ui_postype.center + # print((self.pos)) + # #print self.gridsize + # + # print("END DEBUG OBJECT") """calculate position top-down, since child's position will depend on having an accurate parent position?""" @@ -180,7 +225,7 @@ class UIFrame(object): self.size = self.parent.get_child_size(self) - if self.fill != ui_filltype.none and self.content == ui_frametype.grid: + if self.fill != ui_filltype.none and self.content == ui_frametype.grid: print(("Hello I am grid %s, and my size is %s" % (self.gridsize, self.size))) if self.stretchiness: for ir, row in enumerate(self.data): @@ -192,12 +237,10 @@ class UIFrame(object): y = 0 if xlo > 0: xs = self.stretchiness[0][ic] - xst = sum(self.stretchiness[0]) - x = int(float(xlo) * float(xs) / float(xst)) + x = int(float(xlo) * xs) if ylo > 0: ys = self.stretchiness[1][ir] - yst = sum(self.stretchiness[1]) - y = int(float(ylo) * float(ys) / float(yst)) + y = int(float(ylo) * ys) self.gridsize[0][ic] += x self.gridsize[1][ir] += y @@ -278,7 +321,10 @@ class UIFrame(object): if self.contains_pos(mgr.mousedown.pos): """ welp, this is a valid click -- down was inside us, and so is up """ if self.func != None: - self.func(ev) + if type(self.func) == str: + mgr.get_callback(self.func)(ev) + else: + self.func(ev) return True elif ev.type == KEYUP: @@ -396,77 +442,55 @@ class UIFrame(object): self.data.render() glPopMatrix() - - -def make_box(sprite, center, color): - l = [['tl', 'tc', 'tr'], ['cl', 'cc', 'cr'], ['bl', 'bc', 'br']] - for i, r in enumerate(l): - for j, c in enumerate(r): - mdl = models.mgr.create("%s_%s" % (sprite, c)) - - l[i][j] = UIFrame(ui_frametype.model, mdl) - #if len(mdl.layers) > 1: - mdl.layers[-1].color = color - if c == 'cc': - l[i][j] = UIFrame(ui_frametype.composite, [l[i][j], center]) - elif 'c' in c: - """Border edges should be repeated to fill!""" - l[i][j].fill = ui_filltype.tile - - - #l[i] = tuple(l[i]) - #l = tuple(l) - uf = UIFrame(ui_frametype.grid, l) - uf.stretchiness = [[0, 1, 0], [0, 1, 0]] - return uf - - -def make_button(text, clickfunc): - text = models.TextModel("micross20", text) - textframe = UIFrame(ui_frametype.model, text) - textframe.postype = ui_postype.center - - btnframe = make_box("ui_button", textframe, (0.3, 0.3, 1.0, 1.0)) - btnframe.func = clickfunc - return btnframe - -def startgame(ev): - mgr.remove("mainmenu") - -def clicktest(ev): - print("I'm an event!") - -def exitgame(ev): - inputs.add_command('exit') - -def make_mainmenu(): - b1 = make_button("Start", startgame) - b2 = make_button("Options", clicktest) - b3 = make_button("Exit", exitgame) - - b1.padding = (20,0) - b2.padding = (20,0) - b3.padding = (20,0) - buttongrid = UIFrame(ui_frametype.grid, [[b1], [b2], [b3]]) - buttongrid.postype = ui_postype.center - buttongrid.fill = ui_filltype.stretch - buttongrid.stretchiness = [[1], [1, 1, 1]] - mgr.debugobj = b1 - buttongrid = UIFrame(ui_frametype.composite, [buttongrid]) - buttongrid.padding = (20, 30) - mainframe = make_box("ui_frame", buttongrid, (0.0, 0.0, 1.0, 0.35)) - mainframe.postype = ui_postype.center - return mainframe +#THIS IS OLD EXAMPLE CODE +# +#def startgame(ev): +# mgr.remove("mainmenu") +# +#def clicktest(ev): +# print("I'm an event!") +# +#def exitgame(ev): +# inputs.add_command('exit') +# +#def make_mainmenu(): +# b1 = make_button("Start", startgame) +# b2 = make_button("Options", clicktest) +# b3 = make_button("Exit", exitgame) +# +# b1.padding = (20,0) +# b2.padding = (20,0) +# b3.padding = (20,0) +# buttongrid = UIFrame(ui_frametype.grid, [[b1], [b2], [b3]]) +# buttongrid.postype = ui_postype.center +# buttongrid.fill = ui_filltype.stretch +# buttongrid.stretchiness = [[1], [1, 1, 1]] +# mgr.debugobj = b1 +# buttongrid = UIFrame(ui_frametype.composite, [buttongrid]) +# buttongrid.padding = (20, 30) +# mainframe = make_box("ui_frame", buttongrid, (0.0, 0.0, 1.0, 0.35)) +# mainframe.postype = ui_postype.center +# return mainframe +# +#END OLD EXAMPLE CODE +# class UIManager(object): def __init__(self): self.frames = [] self.framenames = {} + self.frame_library = {} self.focus = None self.last_mousedown = None self.mousemove_hook = None self.debug = 0 + self.theme = UITheme() + self.callback_config_list = [] + self.callback_configs = {} + + def set_theme(self, theme): + self.theme = theme def set_focus(self, frame): self.focus = frame @@ -476,9 +500,9 @@ class UIManager(object): self.framenames[name] = frame frame.index = len(self.frames) frame.auto() - print((frame.pos)) - for child in frame: - print(("%s-%s" % (child.pos, child.size))) + #print((frame.pos)) + #for child in frame: + # print(("%s-%s" % (child.pos, child.size))) """ print "Ok" print frame.data[1][1].data[1].gridsize @@ -521,6 +545,153 @@ class UIManager(object): if ev.type == MOUSEBUTTONUP: self.drag_hook = None self.mousedown = None + + + def make_box(self, sprite, center, color): + l = [['tl', 'tc', 'tr'], ['cl', 'cc', 'cr'], ['bl', 'bc', 'br']] + for i, r in enumerate(l): + for j, c in enumerate(r): + mdl = models.mgr.create("%s_%s" % (sprite, c)) + + l[i][j] = UIFrame(ui_frametype.model, mdl) + #if len(mdl.layers) > 1: + mdl.layers[-1].color = color + if c == 'cc': + l[i][j] = UIFrame(ui_frametype.composite, [l[i][j], center]) + elif 'c' in c: + """Border edges should be repeated to fill!""" + l[i][j].fill = ui_filltype.tile + + + #l[i] = tuple(l[i]) + #l = tuple(l) + uf = UIFrame(ui_frametype.grid, l) + #uf.set_stretch_ratios((0, 1, 0), (0, 1, 0)) + uf.set_stretch_cell(1,1) + return uf -mgr = UIManager() + + def make_button(self, text, clickfunc): + text = models.TextModel(self.theme.font, text) + textframe = UIFrame(ui_frametype.model, text) + textframe.postype = ui_postype.center + + btnframe = self.make_box("ui_button", textframe, (0.3, 0.3, 1.0, 1.0)) + 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"]) + self.frame_library[name] = btn + + + + def load_frame(self, data): + name = data["id"] + 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.postype = enums.index(ui_postype, data['postype']) + frame.filltype = enums.index(ui_filltype, data['filltype']) + + self.frame_library[name] = frame + + def load_wrapper(self, data): + name = data["id"] + 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']) + self.frame_library[name] = frame + + def load_grid(self, data): + name = data["id"] + rowcols = [] + for r in data.row: + rnum = r['number'] + rdata = [] + for c in r.col: + cnum = c['number'] + rdata.append((cnum, self.frame_library[c.content['id']])) + rdata = zip(*sorted(rdata))[1] + rowcols.append((rnum, rdata)) + rowcols = 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']) + + self.frame_library[name] = frame + + def load_uidata(self): + uidata = gamedata.get('ui') + framelibdata = {} + # start by preloading the names + for data in itertools.chain(uidata.button, uidata.grid, uidata.frame, uidata.wrapper): + framelibdata[data['id']] = data + + dg = depgraph.DepGraph() + + for data in uidata.button: + framelibdata[data['id']] = (self.load_button, data) + dg.add(data['id'], []) + + for data in uidata.wrapper: + framelibdata[data['id']] = (self.load_wrapper, data) + dg.add(data['id'], [data.content['id']]) + + for data in uidata.frame: + framelibdata[data['id']] = (self.load_frame, data) + dg.add(data['id'], [data.content['id']]) + + for data in uidata.grid: + framelibdata[data['id']] = (self.load_grid, data) + gcells = [] + for r in data.row: + for c in r.col: + gcells.append(c.content['id']) + dg.add(data['id'], gcells) + + dg.resolve() + + for dn in dg.bottomup_iter(): + df, data = framelibdata[dn] + df(data) + + + def open_ui(self, name, callback_map, obj=None): + self.callback_config_list.append(name) + self.callback_configs[name] = (callback_map, obj) + frame = self.frame_library[name] + self.add(frame, name) + + def close_ui(self, name): + i = self.callback_config_list.index(name) + del self.callback_config_list[i] + del self.callback_configs[name] + self.remove(name) + + + + def get_callback(self, func): + for config in self.callback_config_list: + cc = self.callback_configs[config][0] + if func in cc: + return cc[func] + + raise KeyError("Callback function %s not defined" % (func,)) + + + +def init(): + global mgr + mgr = UIManager() + mgr.load_uidata() + + + + +mgr = None + diff --git a/roc/video.py b/roc/video.py index 567e022..0db3cf1 100755 --- a/roc/video.py +++ b/roc/video.py @@ -5,9 +5,9 @@ from OpenGL.arrays import GLcharArray import pygame from pygame.locals import * import sys -import shader -import config -import platform +from . import shader +from . import config +from . import platform height=None width=None