mostly finished conversion of roc to importable module
improved ui component further, now capable of importing UI layouts from xml data files
This commit is contained in:
parent
bcd74e64c5
commit
6e691bed7b
18 changed files with 519 additions and 152 deletions
|
@ -1,15 +1,15 @@
|
||||||
from roc_core import *
|
from .roc_core import *
|
||||||
import roc_engine
|
from . import roc_engine
|
||||||
import pipeline
|
from . import pipeline
|
||||||
import inputs
|
from . import inputs
|
||||||
import physics
|
from . import physics
|
||||||
import models
|
from . import models
|
||||||
import gamedata
|
from . import gamedata
|
||||||
import enums
|
from . import enums
|
||||||
import files
|
from . import files
|
||||||
import gametimer
|
from . import gametimer
|
||||||
import shader
|
from . import shader
|
||||||
import video
|
from . import video
|
||||||
|
|
||||||
#from .universe import *
|
#from .universe import *
|
||||||
#from .pipeline import *
|
#from .pipeline import *
|
||||||
|
|
|
@ -3,7 +3,7 @@ import math
|
||||||
from quat import *
|
from quat import *
|
||||||
from py3dutil import vect
|
from py3dutil import vect
|
||||||
from OpenGL.GL import *
|
from OpenGL.GL import *
|
||||||
import files
|
from . import files
|
||||||
|
|
||||||
class game_camera(game_object):
|
class game_camera(game_object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import os
|
import os
|
||||||
import files
|
from . import files
|
||||||
|
|
||||||
class configmanager(object):
|
class configmanager(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
116
roc/depgraph.py
Normal file
116
roc/depgraph.py
Normal file
|
@ -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()))
|
||||||
|
|
||||||
|
|
|
@ -4,9 +4,9 @@ try:
|
||||||
from xml.etree.cElementTree import parse
|
from xml.etree.cElementTree import parse
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from elementtree.ElementTree import parse
|
from elementtree.ElementTree import parse
|
||||||
import files
|
from . import files
|
||||||
import shader
|
from . import shader
|
||||||
from deprecate import deprecated
|
from .deprecate import deprecated
|
||||||
|
|
||||||
class MissingNode(object):
|
class MissingNode(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
@ -178,7 +178,7 @@ class XMLGameDataReader(object):
|
||||||
|
|
||||||
for child in xml.getchildren():
|
for child in xml.getchildren():
|
||||||
if not child.tag in tagdict:
|
if not child.tag in tagdict:
|
||||||
raise ValueError
|
raise ValueError("Unrecognized tag %s" % (child.tag,))
|
||||||
continue
|
continue
|
||||||
binchild = tagdict[child.tag]
|
binchild = tagdict[child.tag]
|
||||||
xmlchild = child
|
xmlchild = child
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
import video
|
from . import video
|
||||||
|
|
||||||
state = {}
|
state = {}
|
||||||
commands = {}
|
commands = {}
|
||||||
|
|
40
roc/lua.py
Normal file
40
roc/lua.py
Normal file
|
@ -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
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
40
roc/lupa_sandbox.py
Normal file
40
roc/lupa_sandbox.py
Normal file
|
@ -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
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import os
|
import os
|
||||||
import glob
|
import glob
|
||||||
import enums
|
from . import enums
|
||||||
import files
|
from . import files
|
||||||
import gamedata
|
from . import gamedata
|
||||||
import pygame
|
import pygame
|
||||||
from OpenGL.GL import *
|
from OpenGL.GL import *
|
||||||
from py3dutil import vect
|
from py3dutil import vect
|
||||||
import fonts
|
from . import fonts
|
||||||
import collections
|
import collections
|
||||||
|
|
||||||
class Vertex(object):
|
class Vertex(object):
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from py3dutil import vect, quat
|
from py3dutil import vect, quat
|
||||||
import gametimer
|
from . import gametimer
|
||||||
import math
|
import math
|
||||||
|
|
||||||
floor = math.floor
|
floor = math.floor
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
from OpenGL.GL import *
|
from OpenGL.GL import *
|
||||||
import shader
|
from . import shader
|
||||||
import physics
|
from . import physics
|
||||||
from py3dutil import *
|
from py3dutil import *
|
||||||
from enums import *
|
from .enums import *
|
||||||
|
|
||||||
class pipeline_manager(object):
|
class pipeline_manager(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
import sys
|
import sys
|
||||||
if sys.platform == 'win32':
|
if sys.platform == 'win32':
|
||||||
from platform_win32 import *
|
from .platform_win32 import *
|
||||||
elif sys.platform == 'darwin':
|
elif sys.platform == 'darwin':
|
||||||
from platform_darwin import *
|
from .platform_darwin import *
|
||||||
elif sys.platform in ('posix', 'linux'):
|
elif sys.platform in ('posix', 'linux'):
|
||||||
from platform_posix import *
|
from .platform_posix import *
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError("Not ported to this platform")
|
raise NotImplementedError("Not ported to this platform")
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
import pygame
|
import pygame
|
||||||
import files
|
from . import files
|
||||||
import video
|
from . import video
|
||||||
import shader
|
from . import shader
|
||||||
import models
|
from . import models
|
||||||
import roc_main
|
from . import roc_main
|
||||||
import roc_engine
|
from . import roc_engine
|
||||||
import config
|
from . import config
|
||||||
base_engine = roc_engine.base_engine
|
base_engine = roc_engine.base_engine
|
||||||
|
|
||||||
def init2d():
|
def init2d():
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
import models
|
from . import models
|
||||||
import pipeline
|
from . import pipeline
|
||||||
import pygame
|
import pygame
|
||||||
from OpenGL.GL import *
|
from OpenGL.GL import *
|
||||||
from py3dutil import vect, quat
|
from py3dutil import vect, quat
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
import pygame
|
import pygame
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
import gamedata
|
from . import gamedata
|
||||||
import gametimer
|
from . import gametimer
|
||||||
import video
|
from . import video
|
||||||
import shader
|
from . import shader
|
||||||
import inputs
|
from . import inputs
|
||||||
import models
|
from . import models
|
||||||
import config
|
from . import config
|
||||||
from OpenGL.GL import *
|
from OpenGL.GL import *
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ except ImportError:
|
||||||
pass
|
pass
|
||||||
from OpenGL.arrays import GLintArray, GLcharArray, GLsizeiArray, GLcharARBArray
|
from OpenGL.arrays import GLintArray, GLcharArray, GLsizeiArray, GLcharARBArray
|
||||||
import sys
|
import sys
|
||||||
import gamedata
|
from . import gamedata
|
||||||
|
|
||||||
|
|
||||||
class shader_manager(object):
|
class shader_manager(object):
|
||||||
|
@ -84,7 +84,7 @@ class shader_manager(object):
|
||||||
return
|
return
|
||||||
|
|
||||||
self.initialized = True
|
self.initialized = True
|
||||||
import files
|
from . import files
|
||||||
#print glGetString(GL_EXTENSIONS)
|
#print glGetString(GL_EXTENSIONS)
|
||||||
if True:
|
if True:
|
||||||
self.use_gl2_shaders()
|
self.use_gl2_shaders()
|
||||||
|
|
373
roc/ui.py
373
roc/ui.py
|
@ -1,11 +1,19 @@
|
||||||
import enums
|
from . import enums
|
||||||
from enums import ui_frametype, ui_postype, ui_filltype
|
from .enums import ui_frametype, ui_postype, ui_filltype
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
import models
|
from . import models
|
||||||
import video
|
from . import video
|
||||||
from OpenGL.GL import *
|
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):
|
class UIFrame(object):
|
||||||
def __init__(self, content, contentdata):
|
def __init__(self, content, contentdata):
|
||||||
self.func = None
|
self.func = None
|
||||||
|
@ -23,7 +31,10 @@ class UIFrame(object):
|
||||||
self.draggable = False
|
self.draggable = False
|
||||||
self.padding = (0, 0)
|
self.padding = (0, 0)
|
||||||
self.color = None
|
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):
|
def constrain_size(self, x, y):
|
||||||
|
@ -58,18 +69,19 @@ class UIFrame(object):
|
||||||
if col == child:
|
if col == child:
|
||||||
x = self.gridsize[0][j]
|
x = self.gridsize[0][j]
|
||||||
y = self.gridsize[1][i]
|
y = self.gridsize[1][i]
|
||||||
if False and self.stretchiness:
|
# not sure why I originally disabled this but it seems to work ok for now?
|
||||||
xlo = self.size[0] - sum(self.gridsize[0])
|
#if self.stretchiness:
|
||||||
ylo = self.size[1] - sum(self.gridsize[1])
|
# xlo = self.size[0] - sum(self.gridsize[0])
|
||||||
if xlo > 0:
|
# ylo = self.size[1] - sum(self.gridsize[1])
|
||||||
xs = self.stretchiness[0][j]
|
# if xlo > 0:
|
||||||
xst = sum(self.stretchiness[0])
|
# xs = self.stretchiness[0][j]
|
||||||
x += int(float(xlo) * float(xs) / float(xst))
|
# xst = sum(self.stretchiness[0])
|
||||||
if ylo > 0:
|
# x += int(float(xlo) * float(xs) / float(xst))
|
||||||
ys = self.stretchiness[1][i]
|
# if ylo > 0:
|
||||||
yst = sum(self.stretchiness[1])
|
# ys = self.stretchiness[1][i]
|
||||||
y += int(float(ylo) * float(ys) / float(yst))
|
# yst = sum(self.stretchiness[1])
|
||||||
|
# y += int(float(ylo) * float(ys) / float(yst))
|
||||||
|
#
|
||||||
return (x, y)
|
return (x, y)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
|
@ -98,6 +110,39 @@ class UIFrame(object):
|
||||||
return (0, 0)
|
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):
|
def autopos(self):
|
||||||
if self.postype == ui_postype.center:
|
if self.postype == ui_postype.center:
|
||||||
ppos = self.get_parent_pos()
|
ppos = self.get_parent_pos()
|
||||||
|
@ -123,17 +168,17 @@ class UIFrame(object):
|
||||||
self.pos = self.posset
|
self.pos = self.posset
|
||||||
|
|
||||||
alist = [x for x in self.ancestors()]
|
alist = [x for x in self.ancestors()]
|
||||||
if mgr.debugobj in alist:
|
#if mgr.debugobj in alist:
|
||||||
print(("FUCK. OK DUDER. I AM YOUR DEBUG OBJECT: %s" % (alist.index(mgr.debugobj),)))
|
# print(("FUCK. OK DUDER. I AM YOUR DEBUG OBJECT: %s" % (alist.index(mgr.debugobj),)))
|
||||||
print((self.parent.pos))
|
# print((self.parent.pos))
|
||||||
print((self.get_parent_pos()))
|
# print((self.get_parent_pos()))
|
||||||
print((self.parent.get_child_pos(self)))
|
# print((self.parent.get_child_pos(self)))
|
||||||
#print self.fill == ui_filltype.none
|
# #print self.fill == ui_filltype.none
|
||||||
#print self.postype == ui_postype.center
|
# #print self.postype == ui_postype.center
|
||||||
print((self.pos))
|
# print((self.pos))
|
||||||
#print self.gridsize
|
# #print self.gridsize
|
||||||
|
#
|
||||||
print("END DEBUG OBJECT")
|
# print("END DEBUG OBJECT")
|
||||||
|
|
||||||
|
|
||||||
"""calculate position top-down, since child's position will depend on having an accurate parent position?"""
|
"""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)
|
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)))
|
print(("Hello I am grid %s, and my size is %s" % (self.gridsize, self.size)))
|
||||||
if self.stretchiness:
|
if self.stretchiness:
|
||||||
for ir, row in enumerate(self.data):
|
for ir, row in enumerate(self.data):
|
||||||
|
@ -192,12 +237,10 @@ class UIFrame(object):
|
||||||
y = 0
|
y = 0
|
||||||
if xlo > 0:
|
if xlo > 0:
|
||||||
xs = self.stretchiness[0][ic]
|
xs = self.stretchiness[0][ic]
|
||||||
xst = sum(self.stretchiness[0])
|
x = int(float(xlo) * xs)
|
||||||
x = int(float(xlo) * float(xs) / float(xst))
|
|
||||||
if ylo > 0:
|
if ylo > 0:
|
||||||
ys = self.stretchiness[1][ir]
|
ys = self.stretchiness[1][ir]
|
||||||
yst = sum(self.stretchiness[1])
|
y = int(float(ylo) * ys)
|
||||||
y = int(float(ylo) * float(ys) / float(yst))
|
|
||||||
|
|
||||||
self.gridsize[0][ic] += x
|
self.gridsize[0][ic] += x
|
||||||
self.gridsize[1][ir] += y
|
self.gridsize[1][ir] += y
|
||||||
|
@ -278,7 +321,10 @@ class UIFrame(object):
|
||||||
if self.contains_pos(mgr.mousedown.pos):
|
if self.contains_pos(mgr.mousedown.pos):
|
||||||
""" welp, this is a valid click -- down was inside us, and so is up """
|
""" welp, this is a valid click -- down was inside us, and so is up """
|
||||||
if self.func != None:
|
if self.func != None:
|
||||||
self.func(ev)
|
if type(self.func) == str:
|
||||||
|
mgr.get_callback(self.func)(ev)
|
||||||
|
else:
|
||||||
|
self.func(ev)
|
||||||
return True
|
return True
|
||||||
|
|
||||||
elif ev.type == KEYUP:
|
elif ev.type == KEYUP:
|
||||||
|
@ -396,77 +442,55 @@ class UIFrame(object):
|
||||||
self.data.render()
|
self.data.render()
|
||||||
glPopMatrix()
|
glPopMatrix()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#THIS IS OLD EXAMPLE CODE
|
||||||
def make_box(sprite, center, color):
|
#
|
||||||
l = [['tl', 'tc', 'tr'], ['cl', 'cc', 'cr'], ['bl', 'bc', 'br']]
|
#def startgame(ev):
|
||||||
for i, r in enumerate(l):
|
# mgr.remove("mainmenu")
|
||||||
for j, c in enumerate(r):
|
#
|
||||||
mdl = models.mgr.create("%s_%s" % (sprite, c))
|
#def clicktest(ev):
|
||||||
|
# print("I'm an event!")
|
||||||
l[i][j] = UIFrame(ui_frametype.model, mdl)
|
#
|
||||||
#if len(mdl.layers) > 1:
|
#def exitgame(ev):
|
||||||
mdl.layers[-1].color = color
|
# inputs.add_command('exit')
|
||||||
if c == 'cc':
|
#
|
||||||
l[i][j] = UIFrame(ui_frametype.composite, [l[i][j], center])
|
#def make_mainmenu():
|
||||||
elif 'c' in c:
|
# b1 = make_button("Start", startgame)
|
||||||
"""Border edges should be repeated to fill!"""
|
# b2 = make_button("Options", clicktest)
|
||||||
l[i][j].fill = ui_filltype.tile
|
# b3 = make_button("Exit", exitgame)
|
||||||
|
#
|
||||||
|
# b1.padding = (20,0)
|
||||||
#l[i] = tuple(l[i])
|
# b2.padding = (20,0)
|
||||||
#l = tuple(l)
|
# b3.padding = (20,0)
|
||||||
uf = UIFrame(ui_frametype.grid, l)
|
# buttongrid = UIFrame(ui_frametype.grid, [[b1], [b2], [b3]])
|
||||||
uf.stretchiness = [[0, 1, 0], [0, 1, 0]]
|
# buttongrid.postype = ui_postype.center
|
||||||
return uf
|
# buttongrid.fill = ui_filltype.stretch
|
||||||
|
# buttongrid.stretchiness = [[1], [1, 1, 1]]
|
||||||
|
# mgr.debugobj = b1
|
||||||
def make_button(text, clickfunc):
|
# buttongrid = UIFrame(ui_frametype.composite, [buttongrid])
|
||||||
text = models.TextModel("micross20", text)
|
# buttongrid.padding = (20, 30)
|
||||||
textframe = UIFrame(ui_frametype.model, text)
|
# mainframe = make_box("ui_frame", buttongrid, (0.0, 0.0, 1.0, 0.35))
|
||||||
textframe.postype = ui_postype.center
|
# mainframe.postype = ui_postype.center
|
||||||
|
# return mainframe
|
||||||
btnframe = make_box("ui_button", textframe, (0.3, 0.3, 1.0, 1.0))
|
#
|
||||||
btnframe.func = clickfunc
|
#END OLD EXAMPLE CODE
|
||||||
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
|
|
||||||
|
|
||||||
class UIManager(object):
|
class UIManager(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.frames = []
|
self.frames = []
|
||||||
self.framenames = {}
|
self.framenames = {}
|
||||||
|
self.frame_library = {}
|
||||||
self.focus = None
|
self.focus = None
|
||||||
self.last_mousedown = None
|
self.last_mousedown = None
|
||||||
self.mousemove_hook = None
|
self.mousemove_hook = None
|
||||||
self.debug = 0
|
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):
|
def set_focus(self, frame):
|
||||||
self.focus = frame
|
self.focus = frame
|
||||||
|
@ -476,9 +500,9 @@ class UIManager(object):
|
||||||
self.framenames[name] = frame
|
self.framenames[name] = frame
|
||||||
frame.index = len(self.frames)
|
frame.index = len(self.frames)
|
||||||
frame.auto()
|
frame.auto()
|
||||||
print((frame.pos))
|
#print((frame.pos))
|
||||||
for child in frame:
|
#for child in frame:
|
||||||
print(("%s-%s" % (child.pos, child.size)))
|
# print(("%s-%s" % (child.pos, child.size)))
|
||||||
"""
|
"""
|
||||||
print "Ok"
|
print "Ok"
|
||||||
print frame.data[1][1].data[1].gridsize
|
print frame.data[1][1].data[1].gridsize
|
||||||
|
@ -521,6 +545,153 @@ class UIManager(object):
|
||||||
if ev.type == MOUSEBUTTONUP:
|
if ev.type == MOUSEBUTTONUP:
|
||||||
self.drag_hook = None
|
self.drag_hook = None
|
||||||
self.mousedown = 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
|
||||||
|
|
||||||
|
|
|
@ -5,9 +5,9 @@ from OpenGL.arrays import GLcharArray
|
||||||
import pygame
|
import pygame
|
||||||
from pygame.locals import *
|
from pygame.locals import *
|
||||||
import sys
|
import sys
|
||||||
import shader
|
from . import shader
|
||||||
import config
|
from . import config
|
||||||
import platform
|
from . import platform
|
||||||
|
|
||||||
height=None
|
height=None
|
||||||
width=None
|
width=None
|
||||||
|
|
Loading…
Add table
Reference in a new issue