write nodesets

This commit is contained in:
Frank DeMarco 2014-01-22 23:57:13 +09:00
parent b74c5e6d5a
commit daeb3f1e66
2 changed files with 208 additions and 120 deletions

View File

@ -1,6 +1,7 @@
from os import sep, getcwd
from os.path import join, exists, basename, dirname, expanduser
from sys import argv
from re import match
from pprint import pformat
from ConfigParser import RawConfigParser
@ -15,6 +16,8 @@ class Configuration(RawConfigParser):
RawConfigParser.__init__(self)
self.project_file_rel_path = project_file_rel_path
self.resource_path = resource_path
self.modifiable = {}
self.order = []
self.set_type_declarations(type_declarations)
self.set_defaults()
self.read_project_config_file()
@ -37,103 +40,119 @@ class Configuration(RawConfigParser):
set_option = self.set
section = "setup"
add_section(section)
set_option(section, "package-root", basename(getcwd()))
set_option(section, "additional-packages", "")
set_option(section, "title", "")
set_option(section, "classifiers", "")
set_option(section, "resource-search-path", "./, resource/")
set_option(section, "installation-dir", "/usr/local/share/games/")
set_option(section, "changelog", "changelog")
set_option(section, "description-file", "")
set_option(section, "init-script", "")
set_option(section, "version", "")
set_option(section, "summary", "")
set_option(section, "license", "")
set_option(section, "platforms", "")
set_option(section, "contact-name", "")
set_option(section, "contact-email", "")
set_option(section, "url", "")
set_option(section, "requirements", "")
set_option(section, "main-object", "pgfw/Game.py")
set_option(section, "resource-path-identifier", "resource_path")
set_option(section, "special-char-placeholder", "_")
set_option(section, "whitespace-placeholder", "-")
set_option(section, "windows-dist-path", "dist/win/")
set_option(section, "windows-icon-path", "")
set_option(section, "lowercase-boolean-true", "yes")
set_option(section, "package-root", basename(getcwd()), False)
set_option(section, "additional-packages", "", False)
set_option(section, "title", "", False)
set_option(section, "classifiers", "", False)
set_option(section, "resource-search-path", "./, resource/", False)
set_option(section, "installation-dir", "/usr/local/share/games/",
False)
set_option(section, "changelog", "changelog", False)
set_option(section, "description-file", "", False)
set_option(section, "init-script", "", False)
set_option(section, "version", "", False)
set_option(section, "summary", "", False)
set_option(section, "license", "", False)
set_option(section, "platforms", "", False)
set_option(section, "contact-name", "", False)
set_option(section, "contact-email", "", False)
set_option(section, "url", "", False)
set_option(section, "requirements", "", False)
set_option(section, "main-object", "pgfw/Game.py", False)
set_option(section, "resource-path-identifier", "resource_path", False)
set_option(section, "special-char-placeholder", "_", False)
set_option(section, "whitespace-placeholder", "-", False)
set_option(section, "windows-dist-path", "dist/win/", False)
set_option(section, "windows-icon-path", "", False)
set_option(section, "lowercase-boolean-true", "yes", False)
section = "display"
add_section(section)
set_option(section, "dimensions", "480, 360")
set_option(section, "frame-duration", "40")
set_option(section, "wait-duration", "2")
set_option(section, "caption", "")
set_option(section, "centered", "yes")
set_option(section, "icon-path", "")
set_option(section, "skip-frames", "no")
set_option(section, "fullscreen", "no")
set_option(section, "windowed-flag", "wi")
set_option(section, "show-framerate", "no")
set_option(section, "framerate-display-flag", "fr")
set_option(section, "framerate-text-size", "16")
set_option(section, "framerate-text-color", "0, 0, 0")
set_option(section, "framerate-text-background", "255, 255, 255")
set_option(section, "dimensions", "480, 360", False)
set_option(section, "frame-duration", "40", False)
set_option(section, "wait-duration", "2", False)
set_option(section, "caption", "", False)
set_option(section, "centered", "yes", False)
set_option(section, "icon-path", "", False)
set_option(section, "skip-frames", "no", False)
set_option(section, "fullscreen", "no", False)
set_option(section, "windowed-flag", "wi", False)
set_option(section, "show-framerate", "no", False)
set_option(section, "framerate-display-flag", "fr", False)
set_option(section, "framerate-text-size", "16", False)
set_option(section, "framerate-text-color", "0, 0, 0", False)
set_option(section, "framerate-text-background", "255, 255, 255", False)
section = "input"
add_section(section)
set_option(section, "release-suffix", "-release")
set_option(section, "release-suffix", "-release", False)
section = "sprite"
add_section(section)
set_option(section, "transparent-color", "magenta")
set_option(section, "transparent-color", "magenta", False)
section = "screen-captures"
add_section(section)
set_option(section, "rel-path", "caps")
set_option(section, "file-name-format", "%Y%m%d%H%M%S")
set_option(section, "file-extension", "png")
set_option(section, "rel-path", "caps", False)
set_option(section, "file-name-format", "%Y%m%d%H%M%S", False)
set_option(section, "file-extension", "png", False)
section = "video-recordings"
add_section(section)
set_option(section, "rel-path", "vids")
set_option(section, "directory-name-format", "%Y%m%d%H%M%S")
set_option(section, "file-extension", "png")
set_option(section, "frame-format", "RGB")
set_option(section, "framerate", "100")
set_option(section, "rel-path", "vids", False)
set_option(section, "directory-name-format", "%Y%m%d%H%M%S", False)
set_option(section, "file-extension", "png", False)
set_option(section, "frame-format", "RGB", False)
set_option(section, "framerate", "100", False)
section = "mouse"
add_section(section)
set_option(section, "visible", "yes")
set_option(section, "double-click-time-limit", ".5")
set_option(section, "visible", "yes", False)
set_option(section, "double-click-time-limit", ".5", False)
section = "keys"
add_section(section)
set_option(section, "up", "K_UP, K_w")
set_option(section, "right", "K_RIGHT, K_d")
set_option(section, "down", "K_DOWN, K_s")
set_option(section, "left", "K_LEFT, K_a")
set_option(section, "capture-screen", "K_F9")
set_option(section, "toggle-fullscreen", "K_F11")
set_option(section, "reset-game", "K_F8")
set_option(section, "record-video", "K_F10")
set_option(section, "mute", "K_F12")
set_option(section, "toggle-interpolator", "K_F7")
set_option(section, "up", "K_UP, K_w", False)
set_option(section, "right", "K_RIGHT, K_d", False)
set_option(section, "down", "K_DOWN, K_s", False)
set_option(section, "left", "K_LEFT, K_a", False)
set_option(section, "capture-screen", "K_F9", False)
set_option(section, "toggle-fullscreen", "K_F11", False)
set_option(section, "reset-game", "K_F8", False)
set_option(section, "record-video", "K_F10", False)
set_option(section, "mute", "K_F12", False)
set_option(section, "toggle-interpolator", "K_F7", False)
section = "joy"
add_section(section)
set_option(section, "advance", "7")
set_option(section, "pause", "7")
set_option(section, "select", "6")
set_option(section, "advance", "7", False)
set_option(section, "pause", "7", False)
set_option(section, "select", "6", False)
section = "event"
add_section(section)
set_option(section, "user-event-id", "USEREVENT")
set_option(section, "command-id-offset", "1")
set_option(section, "command-key", "command")
set_option(section, "cancel-flag-key", "cancel")
set_option(section, "user-event-id", "USEREVENT", False)
set_option(section, "command-id-offset", "1", False)
set_option(section, "command-key", "command", False)
set_option(section, "cancel-flag-key", "cancel", False)
section = "audio"
add_section(section)
set_option(section, "sfx-path", "aud/fx/")
set_option(section, "sfx-path", "aud/fx/", False)
section = "interpolator-gui"
add_section(section)
set_option(section, "margin", "80")
set_option(section, "marker-color", "255, 0, 0")
set_option(section, "marker-size", "11")
set_option(section, "curve-color", "0, 255, 0")
set_option(section, "label-size", "16")
set_option(section, "label-precision", "2")
set_option(section, "axis-label-count", "8")
set_option(section, "margin", "80", False)
set_option(section, "marker-color", "255, 0, 0", False)
set_option(section, "marker-size", "11", False)
set_option(section, "curve-color", "0, 255, 0", False)
set_option(section, "label-size", "16", False)
set_option(section, "label-precision", "2", False)
set_option(section, "axis-label-count", "8", False)
def add_section(self, name):
if name not in self.order:
self.order.append(name)
RawConfigParser.add_section(self, name)
def set(self, section, option, value, modifiable=True):
if modifiable:
if section not in self.order:
self.order.append(section)
if section not in self.modifiable:
self.modifiable[section] = []
if option not in self.modifiable[section]:
self.modifiable[section].append(option)
RawConfigParser.set(self, section, option, value)
def read_project_config_file(self):
path = self.locate_project_config_file()
@ -142,10 +161,30 @@ class Configuration(RawConfigParser):
self.set_modifiable(fp)
fp.seek(0)
self.readfp(fp)
fp.seek(0)
self.set_order(fp)
fp.close()
else:
self.print_debug("No configuration file found")
def set_order(self, fp):
self.order = order = []
for line in file(self.locate_project_config_file()):
result = match("^\s*\[(.*)\]\s*$", line)
if result:
order.append(result.group(1))
def set_modifiable(self, fp):
config = RawConfigParser()
config.readfp(fp)
modifiable = self.modifiable
for section in config._sections:
if section not in modifiable:
modifiable[section] = []
for option in config._sections[section]:
if option != "__name__" and option not in modifiable[section]:
modifiable[section].append(option)
def locate_project_config_file(self):
rel_path = self.project_file_rel_path
if not rel_path:
@ -160,17 +199,6 @@ class Configuration(RawConfigParser):
def is_shared_mode(self):
return "-s" in argv
def set_modifiable(self, fp):
config = RawConfigParser()
config.readfp(fp)
modifiable = []
for section in config._sections:
modifiable.append([section, []])
for option in config._sections[section]:
if option != "__name__":
modifiable[-1][1].append(option)
self.modifiable = modifiable
def print_debug(self, statement):
if self.is_debug_mode():
print statement
@ -189,7 +217,7 @@ class Configuration(RawConfigParser):
def set_installation_path(self):
self.set("setup", "installation-path",
join(self.get("setup", "installation-dir"),
self.get("setup", "package-root")))
self.get("setup", "package-root")), False)
def set_resource_search_path(self):
section, option = "setup", "resource-search-path"
@ -198,7 +226,7 @@ class Configuration(RawConfigParser):
search_path.append(self.resource_path)
else:
search_path.append(self.get("setup", "installation-path"))
self.set(section, option, search_path)
self.set(section, option, search_path, False)
def get(self, section, option):
value = RawConfigParser.get(self, section, option)
@ -240,7 +268,8 @@ class Configuration(RawConfigParser):
section, option = "screen-captures", "path"
if not self.has_option(section, option):
self.set(section, option, join(self.build_home_path(),
self.get(section, "rel-path")))
self.get(section, "rel-path")),
False)
def build_home_path(self):
return join("~", "." + self.get("setup", "package-root"))
@ -249,7 +278,8 @@ class Configuration(RawConfigParser):
section, option = "video-recordings", "path"
if not self.has_option(section, option):
self.set(section, option, join(self.build_home_path(),
self.get(section, "rel-path")))
self.get(section, "rel-path")),
False)
def set_data_exclusion_list(self):
section, option = "setup", "data-exclude"
@ -262,7 +292,7 @@ class Configuration(RawConfigParser):
self.get("setup", "package-root")]
for location in self.get("setup", "additional-packages"):
exclude.append(location)
self.set(section, option, exclude)
self.set(section, option, exclude, False)
def set_requirements(self):
section, option = "setup", "requirements"
@ -271,7 +301,7 @@ class Configuration(RawConfigParser):
requirements = self.get(section, option)
if "pygame" not in requirements:
requirements.append("pygame")
self.set(section, option, requirements)
self.set(section, option, requirements, False)
def get_section(self, section):
assignments = {}
@ -292,12 +322,14 @@ class Configuration(RawConfigParser):
return items
def write(self, fp):
for section in self.modifiable:
fp.write("[%s]\n" % section[0])
for name in section[1]:
value = self.get(section[0], name)
fp.write("%s = %s\n" % (name, self.get_raw_value(value)))
fp.write("\n")
modifiable = self.modifiable
for section in self.order:
if section in modifiable:
fp.write("[%s]\n" % section)
for option in modifiable[section]:
value = self.get(section, option)
fp.write("%s = %s\n" % (option, self.get_raw_value(value)))
fp.write("\n")
def get_raw_value(self, value):
if isinstance(value, list):

View File

@ -42,12 +42,17 @@ class Nodeset(list):
def parse_raw(self, raw):
raw = raw.strip()
if raw[0].upper() == "L":
self.interpolation_method = self.LINEAR
self.set_interpolation_method(self.LINEAR, False)
else:
self.interpolation_method = self.CUBIC
self.set_interpolation_method(self.CUBIC, False)
for node in raw[1:].strip().split(","):
self.add_node(map(float, node.strip().split()), False)
def set_interpolation_method(self, method, refresh=True):
self.interpolation_method = method
if refresh:
self.set_splines()
def add_node(self, coordinates, refresh=True):
x = coordinates[0]
inserted = False
@ -121,16 +126,6 @@ class Nodeset(list):
list.remove(self, node)
self.set_splines()
def __repr__(self):
return "<%i, %s>" % (self.interpolation_method, list(self))
def print_checkpoints(self):
print self
start, end = int(self[0].x), int(self[-1].x)
step = (end - start) / 8
for t in xrange(start, end + step, step):
print "%i\t%.3f" % (t, self.get_y(t))
class Node(list):
@ -179,6 +174,8 @@ class LinearSpline(Spline):
class GUI(GameChild):
B_WRITE, B_LINEAR, B_CUBIC = range(3)
def __init__(self, parent):
GameChild.__init__(self, parent)
self.audio = self.get_audio()
@ -187,11 +184,12 @@ class GUI(GameChild):
self.time_filter = self.get_game().time_filter
self.delegate = self.get_delegate()
self.load_configuration()
self.font = Font(None, self.label_size)
self.set_background()
self.set_plot_rect()
self.set_marker_frame()
self.set_buttons()
self.active = False
self.font = Font(None, self.label_size)
self.set_nodeset_index()
self.subscribe(self.respond_to_command)
self.subscribe(self.respond_to_mouse_down, MOUSEBUTTONDOWN)
@ -227,6 +225,14 @@ class GUI(GameChild):
aaline(surface, line_color, (0, size - 1), (size - 1, 0))
self.marker_frame = surface
def set_buttons(self):
self.buttons = buttons = []
text = "Write", "Linear", "Cubic"
x = 0
for ii in xrange(3):
buttons.append(Button(self, text[ii], x))
x += buttons[-1].location.w + 10
def set_nodeset_index(self, increment=None):
parent = self.parent
if not increment:
@ -333,14 +339,28 @@ class GUI(GameChild):
if nodeset_rect.collidepoint(pos):
self.set_nodeset_index(1)
redraw = True
elif plot_rect.collidepoint(pos) and \
not self.collide_markers(pos):
xp, yp = pos[0] - plot_rect.left, pos[1] - plot_rect.top
self.get_nodeset().add_node(
self.get_function_coordinates(xp, yp))
self.set_yrange()
self.set_markers()
redraw = True
else:
bi = self.collide_buttons(pos)
if bi is not None:
if bi == self.B_WRITE:
self.write_configuration()
else:
nodeset = self.get_nodeset()
if bi == self.B_LINEAR:
nodeset.set_interpolation_method(Nodeset.LINEAR)
else:
nodeset.set_interpolation_method(Nodeset.CUBIC)
self.set_yrange()
self.set_markers()
redraw = True
elif plot_rect.collidepoint(pos) and \
not self.collide_markers(pos):
xp, yp = pos[0] - plot_rect.left, pos[1] - plot_rect.top
self.get_nodeset().add_node(
self.get_function_coordinates(xp, yp))
self.set_yrange()
self.set_markers()
redraw = True
elif event.button == 3:
pos = event.pos
if nodeset_rect.collidepoint(pos):
@ -355,6 +375,24 @@ class GUI(GameChild):
redraw = True
redraw and self.draw()
def collide_buttons(self, pos):
for ii, button in enumerate(self.buttons):
if button.location.collidepoint(pos):
return ii
def write_configuration(self):
config = self.get_configuration()
for nodeset in self.parent:
code = "L" if nodeset.interpolation_method == Nodeset.LINEAR else \
"C"
for ii, node in enumerate(nodeset):
if ii > 0:
code += ","
code += " {0} {1}".format(*map(self.get_formatted_measure,
node))
config.set("interpolate", nodeset.name, code)
config.write(open("/tmp/config", "w"))
def collide_markers(self, pos):
for marker in self.markers:
if marker.location.collidepoint(pos):
@ -391,6 +429,7 @@ class GUI(GameChild):
self.draw_axes()
self.draw_function()
self.draw_markers()
self.draw_buttons()
def draw_axes(self):
display_surface = self.display_surface
@ -402,7 +441,7 @@ class GUI(GameChild):
rect = self.plot_rect
surface = self.display_surface
nodeset = self.get_nodeset()
step = 5
step = 1
for x in xrange(rect.left, rect.right + step, step):
ii = x - rect.left
fx = nodeset.get_y(self.get_function_coordinates(ii)[0])
@ -415,6 +454,10 @@ class GUI(GameChild):
for marker in self.markers:
marker.update()
def draw_buttons(self):
for button in self.buttons:
button.update()
class Marker(Sprite):
@ -422,3 +465,16 @@ class Marker(Sprite):
Sprite.__init__(self, parent)
self.add_frame(parent.marker_frame)
self.node = node
class Button(Sprite):
def __init__(self, parent, text, left):
Sprite.__init__(self, parent)
self.set_frame(text)
self.location.bottomleft = left, \
self.get_display_surface().get_rect().bottom
def set_frame(self, text):
self.add_frame(self.parent.font.render(text, True, (0, 0, 0),
(255, 255, 255)))