remove node
This commit is contained in:
parent
32859baec4
commit
a60c234c5f
|
@ -129,7 +129,7 @@ class Configuration(RawConfigParser):
|
|||
add_section(section)
|
||||
set_option(section, "margin", "80")
|
||||
set_option(section, "marker-color", "255, 0, 0")
|
||||
set_option(section, "marker-size", "8")
|
||||
set_option(section, "marker-size", "11")
|
||||
set_option(section, "curve-color", "0, 255, 0")
|
||||
set_option(section, "label-size", "16")
|
||||
set_option(section, "axis-label-count", "8")
|
||||
|
|
|
@ -4,6 +4,7 @@ from pygame.draw import aaline
|
|||
from pygame.locals import *
|
||||
|
||||
from GameChild import GameChild
|
||||
from Sprite import Sprite
|
||||
|
||||
class Interpolator(list, GameChild):
|
||||
|
||||
|
@ -15,8 +16,10 @@ class Interpolator(list, GameChild):
|
|||
self.gui = GUI(self)
|
||||
|
||||
def set_nodesets(self):
|
||||
for name, value in self.get_configuration("interpolate").iteritems():
|
||||
self.append(Nodeset(name, value))
|
||||
config = self.get_configuration()
|
||||
if config.has_section("interpolate"):
|
||||
for name, value in config.get_section("interpolate").iteritems():
|
||||
self.append(Nodeset(name, value))
|
||||
|
||||
def is_gui_active(self):
|
||||
return self.gui_enabled and self.gui.active
|
||||
|
@ -91,6 +94,10 @@ class Nodeset(list):
|
|||
return splines[ii].get_y(t)
|
||||
return splines[-1].get_y(t)
|
||||
|
||||
def remove(self, node):
|
||||
list.remove(self, node)
|
||||
self.set_splines()
|
||||
|
||||
def __repr__(self):
|
||||
return "<%i, %s>" % (self.interpolation_method, list(self))
|
||||
|
||||
|
@ -159,6 +166,7 @@ class GUI(GameChild):
|
|||
self.load_configuration()
|
||||
self.set_background()
|
||||
self.set_plot_rect()
|
||||
self.set_marker_frame()
|
||||
self.active = False
|
||||
self.font = Font(None, self.label_size)
|
||||
self.set_nodeset_index()
|
||||
|
@ -172,6 +180,8 @@ class GUI(GameChild):
|
|||
self.axis_label_count = config["axis-label-count"]
|
||||
self.margin = config["margin"]
|
||||
self.curve_color = config["curve-color"]
|
||||
self.marker_size = config["marker-size"]
|
||||
self.marker_color = config["marker-color"]
|
||||
|
||||
def set_background(self):
|
||||
surface = Surface(self.display_surface.get_size())
|
||||
|
@ -183,6 +193,17 @@ class GUI(GameChild):
|
|||
self.plot_rect = self.display_surface.get_rect().inflate(-margin,
|
||||
-margin)
|
||||
|
||||
def set_marker_frame(self):
|
||||
size = self.marker_size
|
||||
surface = Surface((size, size))
|
||||
transparent_color = (255, 0, 255)
|
||||
surface.fill(transparent_color)
|
||||
surface.set_colorkey(transparent_color)
|
||||
line_color = self.marker_color
|
||||
aaline(surface, line_color, (0, 0), (size - 1, size - 1))
|
||||
aaline(surface, line_color, (0, size - 1), (size - 1, 0))
|
||||
self.marker_frame = surface
|
||||
|
||||
def set_nodeset_index(self, increment=None):
|
||||
parent = self.parent
|
||||
if not increment:
|
||||
|
@ -197,6 +218,7 @@ class GUI(GameChild):
|
|||
self.nodeset_index = index
|
||||
self.set_nodeset_label()
|
||||
self.set_axis_labels()
|
||||
self.set_markers()
|
||||
|
||||
def set_nodeset_label(self):
|
||||
surface = self.font.render(self.get_nodeset().name, True, (0, 0, 0),
|
||||
|
@ -263,23 +285,49 @@ class GUI(GameChild):
|
|||
redraw = False
|
||||
if self.active:
|
||||
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
|
||||
plot_rect = self.plot_rect
|
||||
if event.button == 1:
|
||||
if nodeset_rect.collidepoint(event.pos):
|
||||
self.set_nodeset_index(1)
|
||||
redraw = True
|
||||
elif event.button == 3:
|
||||
if nodeset_rect.collidepoint(event.pos):
|
||||
self.set_nodeset_index(-1)
|
||||
redraw = True
|
||||
elif plot_rect.collidepoint(event.pos):
|
||||
for marker in self.markers:
|
||||
if marker.location.collidepoint(event.pos):
|
||||
self.get_nodeset().remove(marker.node)
|
||||
self.set_markers()
|
||||
redraw = True
|
||||
redraw and self.draw()
|
||||
|
||||
def respond_to_mouse_up(self, event):
|
||||
pass
|
||||
|
||||
def set_markers(self):
|
||||
self.markers = markers = []
|
||||
for node in self.get_nodeset()[1:-1]:
|
||||
markers.append(Marker(self, node))
|
||||
markers[-1].location.center = self.get_plot_coordinates(*node)
|
||||
|
||||
def get_plot_coordinates(self, x=0, y=0):
|
||||
nodeset = self.get_nodeset()
|
||||
(x_min, y_min), (x_max, y_max) = nodeset[0], nodeset[-1]
|
||||
x_ratio = float(x - x_min) / (x_max - x_min)
|
||||
rect = self.plot_rect
|
||||
xp = x_ratio * (rect.right - rect.left) + rect.left
|
||||
y_ratio = float(y - y_min) / (y_max - y_min)
|
||||
yp = rect.bottom - y_ratio * (rect.bottom - rect.top)
|
||||
return xp, yp
|
||||
|
||||
def draw(self):
|
||||
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()
|
||||
self.draw_markers()
|
||||
|
||||
def draw_axes(self):
|
||||
display_surface = self.display_surface
|
||||
|
@ -292,7 +340,7 @@ class GUI(GameChild):
|
|||
surface = self.display_surface
|
||||
nodeset = self.get_nodeset()
|
||||
step = 5
|
||||
for x in xrange(rect.left, rect.right, step):
|
||||
for x in xrange(rect.left, rect.right + step, step):
|
||||
ii = x - rect.left
|
||||
fx = nodeset.get_y(float(ii) / (rect.right - rect.left) * \
|
||||
(nodeset[-1].x - nodeset[0].x) + nodeset[0].x)
|
||||
|
@ -301,3 +349,15 @@ class GUI(GameChild):
|
|||
if ii > 0:
|
||||
aaline(surface, self.curve_color, (x - step, last_y), (x, y))
|
||||
last_y = y
|
||||
|
||||
def draw_markers(self):
|
||||
for marker in self.markers:
|
||||
marker.update()
|
||||
|
||||
|
||||
class Marker(Sprite):
|
||||
|
||||
def __init__(self, parent, node):
|
||||
Sprite.__init__(self, parent)
|
||||
self.add_frame(parent.marker_frame)
|
||||
self.node = node
|
||||
|
|
|
@ -145,6 +145,7 @@ class Sprite(Animation):
|
|||
base.move(*current_offset)))
|
||||
current_offset[0] += offset[0]
|
||||
current_offset[1] += offset[1]
|
||||
return self.locations[-1]
|
||||
|
||||
def fade(self, length=0, out=None, index=None):
|
||||
if index is None:
|
||||
|
@ -169,6 +170,12 @@ class Sprite(Animation):
|
|||
def unhide(self):
|
||||
self.hidden = False
|
||||
|
||||
def remove_locations(self, location=None):
|
||||
if location:
|
||||
self.locations.remove(location)
|
||||
else:
|
||||
self.locations = []
|
||||
|
||||
def update(self):
|
||||
Animation.update(self)
|
||||
self.draw()
|
||||
|
|
Loading…
Reference in New Issue