draw function, axes

This commit is contained in:
Frank DeMarco 2014-01-16 01:16:22 +09:00
parent fdded8701b
commit 32859baec4
2 changed files with 70 additions and 6 deletions

View File

@ -127,11 +127,12 @@ class Configuration(RawConfigParser):
set_option(section, "sfx-path", "aud/fx/")
section = "interpolator-gui"
add_section(section)
set_option(section, "margin", "40")
set_option(section, "margin", "80")
set_option(section, "marker-color", "255, 0, 0")
set_option(section, "marker-size", "8")
set_option(section, "curve-color", "0, 255, 0")
set_option(section, "label-size", "18")
set_option(section, "label-size", "16")
set_option(section, "axis-label-count", "8")
def read_project_config_file(self):
path = self.locate_project_config_file()
@ -350,7 +351,8 @@ class TypeDeclarations(dict):
"event": {"int": "command-id-offset"},
"interpolator-gui": {"int": ["margin", "marker-size", "label-size"],
"interpolator-gui": {"int": ["margin", "marker-size", "label-size",
"axis-label-count"],
"int-list": ["marker-color", "curve-color"]},

View File

@ -1,5 +1,6 @@
from pygame import Surface
from pygame.font import Font
from pygame.draw import aaline
from pygame.locals import *
from GameChild import GameChild
@ -157,6 +158,7 @@ class GUI(GameChild):
self.delegate = self.get_delegate()
self.load_configuration()
self.set_background()
self.set_plot_rect()
self.active = False
self.font = Font(None, self.label_size)
self.set_nodeset_index()
@ -167,12 +169,20 @@ class GUI(GameChild):
def load_configuration(self):
config = self.get_configuration("interpolator-gui")
self.label_size = config["label-size"]
self.axis_label_count = config["axis-label-count"]
self.margin = config["margin"]
self.curve_color = config["curve-color"]
def set_background(self):
surface = Surface(self.display_surface.get_size())
surface.fill((0, 0, 0))
self.background = surface
def set_plot_rect(self):
margin = self.margin
self.plot_rect = self.display_surface.get_rect().inflate(-margin,
-margin)
def set_nodeset_index(self, increment=None):
parent = self.parent
if not increment:
@ -186,15 +196,36 @@ class GUI(GameChild):
index = limit
self.nodeset_index = index
self.set_nodeset_label()
self.draw()
self.set_axis_labels()
def set_nodeset_label(self):
surface = self.font.render(self.get_nodeset().name, False, (0, 0, 0),
surface = self.font.render(self.get_nodeset().name, True, (0, 0, 0),
(255, 255, 255))
rect = surface.get_rect()
rect.bottomright = self.display_surface.get_rect().bottomright
self.nodeset_label, self.nodeset_label_rect = surface, rect
def set_axis_labels(self):
nodeset = self.get_nodeset()
labels = []
xr, yr = (nodeset[0].x, nodeset[-1].x), (nodeset[0].y, nodeset[-1].y)
count = self.axis_label_count
rect = self.plot_rect
for ii in xrange(count):
ratio = float(ii) / (count - 1)
x = "%.3f" % ((xr[1] - xr[0]) * ratio + xr[0])
xs = self.font.render(x, True, (0, 0, 0), (255, 255, 255))
xsr = xs.get_rect()
xsr.midtop = (rect.right - rect.left) * ratio + rect.left, \
rect.bottom
y = "%.3f" % ((yr[1] - yr[0]) * ratio + yr[0])
ys = self.font.render(y, True, (0, 0, 0), (255, 255, 255))
ysr = ys.get_rect()
ysr.midright = rect.left, rect.bottom - (rect.bottom - rect.top) * \
ratio
labels.append(((xs, xsr), (ys, ysr)))
self.axis_labels = labels
def get_nodeset(self):
return self.parent[self.nodeset_index]
@ -229,8 +260,16 @@ class GUI(GameChild):
self.saved_mouse_state = self.display.set_mouse_visibility(True)
def respond_to_mouse_down(self, event):
redraw = False
if self.active:
self.set_nodeset_index(1)
nodeset_rect = self.nodeset_label_rect
if event.button == 1 and nodeset_rect.collidepoint(event.pos):
self.set_nodeset_index(1)
redraw = True
elif event.button == 3 and nodeset_rect.collidepoint(event.pos):
self.set_nodeset_index(-1)
redraw = True
redraw and self.draw()
def respond_to_mouse_up(self, event):
pass
@ -239,3 +278,26 @@ class GUI(GameChild):
display_surface = self.display_surface
display_surface.blit(self.background, (0, 0))
display_surface.blit(self.nodeset_label, self.nodeset_label_rect)
self.draw_axes()
self.draw_function()
def draw_axes(self):
display_surface = self.display_surface
for xl, yl in self.axis_labels:
display_surface.blit(*xl)
display_surface.blit(*yl)
def draw_function(self):
rect = self.plot_rect
surface = self.display_surface
nodeset = self.get_nodeset()
step = 5
for x in xrange(rect.left, rect.right, step):
ii = x - rect.left
fx = nodeset.get_y(float(ii) / (rect.right - rect.left) * \
(nodeset[-1].x - nodeset[0].x) + nodeset[0].x)
y = rect.bottom - (rect.bottom - rect.top) * fx / \
(nodeset[-1].y - nodeset[0].y)
if ii > 0:
aaline(surface, self.curve_color, (x - step, last_y), (x, y))
last_y = y