time filter ticks; interpolator gui activate

This commit is contained in:
Frank DeMarco 2014-01-14 21:27:11 +09:00
parent 3a6000886a
commit 27467b393a
8 changed files with 116 additions and 16 deletions

View File

@ -1,11 +1,10 @@
from pygame.time import get_ticks
from GameChild import GameChild
class Animation(GameChild):
def __init__(self, parent, method=None, interval=None):
GameChild.__init__(self, parent)
self.time_filter = self.get_game().time_filter
self.default_method = method or self.build_frame
self.accounts = {}
self.register(self.default_method, interval=interval)
@ -21,7 +20,7 @@ class Animation(GameChild):
interval = kwargs["interval"]
for method in args:
if method not in self.accounts:
self.accounts[method] = Account(interval)
self.accounts[method] = Account(interval, self.time_filter)
else:
self.accounts[method].set_interval(interval)
@ -66,7 +65,8 @@ class Animation(GameChild):
class Account:
def __init__(self, interval):
def __init__(self, interval, time_filter):
self.time_filter = time_filter
self.set_interval(interval)
self.set_delay(0)
self.set_play_once(False)
@ -97,7 +97,7 @@ class Account:
def update(self):
if self.playing:
ticks = get_ticks()
ticks = self.time_filter.get_ticks()
self.update_delay(ticks)
if not self.delay:
interval = self.interval

View File

@ -32,7 +32,11 @@ class Audio(GameChild):
self.mute()
def mute(self):
self.muted = not self.muted
self.muted = True
self.set_volume()
def unmute(self):
self.muted = False
self.set_volume()
def set_volume(self):

View File

@ -110,6 +110,7 @@ class Configuration(RawConfigParser):
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")
section = "joy"
add_section(section)
set_option(section, "advance", "7")

View File

@ -1,5 +1,4 @@
import pygame
from pygame.time import get_ticks, wait
from pygame.locals import *
from GameChild import GameChild
@ -13,6 +12,7 @@ from ScreenGrabber import ScreenGrabber
from Profile import Profile
from VideoRecorder import VideoRecorder
from Interpolator import Interpolator
from TimeFilter import TimeFilter
class Game(GameChild):
@ -37,6 +37,7 @@ class Game(GameChild):
self.type_declarations)
def set_children(self):
self.time_filter = TimeFilter(self)
self.delegate = Delegate(self)
self.display = Display(self)
self.mainloop = Mainloop(self)
@ -47,8 +48,12 @@ class Game(GameChild):
self.interpolator = Interpolator(self)
def frame(self):
self.time_filter.update()
self.delegate.dispatch()
self.update()
if self.interpolator.is_gui_active():
self.interpolator.update()
else:
self.update()
self.video_recorder.update()
def run(self):

View File

@ -29,7 +29,7 @@ class Input(GameChild):
def set_any_press_ignore_list(self):
self.any_press_ignored = set(["capture-screen", "toggle-fullscreen",
"reset-game", "record-video", "quit",
"mute"])
"mute", "toggle-interpolator"])
self.any_press_ignored_keys = set()
def unsuppress(self):

View File

@ -1,15 +1,26 @@
from pygame.locals import *
from GameChild import GameChild
class Interpolator(dict, GameChild):
def __init__(self, parent):
GameChild.__init__(self, parent)
self.read()
self.set_nodes()
self.gui_enabled = self.check_command_line("-interpolator")
if self.gui_enabled:
self.gui = GUI(self)
def read(self):
def set_nodes(self):
for name, value in self.get_configuration("interpolate").iteritems():
self[name] = Nodes(value)
self[name].print_checkpoints()
def is_gui_active(self):
return self.gui_enabled and self.gui.active
def update(self):
if self.gui_enabled:
self.gui.update()
class Nodes(list):
@ -134,3 +145,50 @@ class LinearSpline(Spline):
def get_y(self, t):
return self.m * (t - self.x) + self.y
class GUI(GameChild):
def __init__(self, parent):
GameChild.__init__(self, parent)
self.time_filter = self.get_game().time_filter
self.delegate = self.get_delegate()
self.subscribe(self.respond_to_command)
self.subscribe(self.respond_to_mouse_down, MOUSEBUTTONDOWN)
self.subscribe(self.respond_to_mouse_up, MOUSEBUTTONUP)
self.active = False
def deactivate(self):
self.active = False
self.time_filter.open()
self.get_audio().unmute()
def respond_to_command(self, event):
compare = self.delegate.compare
if compare(event, "toggle-interpolator"):
self.toggle()
elif self.active and compare(event, "reset-game"):
self.deactivate()
def toggle(self):
if self.active:
self.deactivate()
else:
self.activate()
def activate(self):
self.active = True
self.time_filter.close()
self.get_audio().mute()
def respond_to_mouse_down(self, event):
pass
def respond_to_mouse_up(self, event):
pass
def update(self):
from pygame.font import Font
font = Font(None, 59)
surface = font.render(str(self.time_filter.get_ticks()), False, (255, 255, 255), (0, 0, 0))
self.get_display_surface().blit(surface, (0, 0))

View File

@ -8,7 +8,6 @@ from pygame import Color, Rect, Surface
from pygame.image import load
from pygame.transform import flip
from pygame.locals import *
from pygame.time import get_ticks
from Animation import Animation
from Vector import Vector
@ -210,6 +209,7 @@ class Fader(Surface):
def __init__(self, location):
self.location = location
self.time_filter = location.sprite.get_game().time_filter
self.reset()
def reset(self):
@ -240,7 +240,7 @@ class Fader(Surface):
out = alpha == maximum
if out and alpha > 0 or not out and alpha < maximum:
self.fade_length = self.fade_remaining = length
self.start_time = get_ticks()
self.start_time = self.time_filter.get_ticks()
self.fading_out = out
def draw(self):
@ -264,8 +264,8 @@ class Fader(Surface):
self.location.sprite.display_surface.blit(frame, self.location)
def update_alpha(self):
remaining = self.fade_remaining = self.fade_length - (get_ticks() - \
self.start_time)
remaining = self.fade_remaining = self.fade_length - \
(self.time_filter.get_ticks() - self.start_time)
ratio = self.fade_length and float(remaining) / self.fade_length
if not self.fading_out:
ratio = 1 - ratio

32
pgfw/TimeFilter.py Normal file
View File

@ -0,0 +1,32 @@
from pygame.time import get_ticks
from GameChild import GameChild
class TimeFilter(GameChild):
def __init__(self, parent):
GameChild.__init__(self, parent)
self.ticks = self.last_ticks = get_ticks()
self.open()
def close(self):
self.closed = True
def open(self):
self.closed = False
def get_ticks(self):
return self.ticks
def get_last_ticks(self):
return self.last_ticks
def get_last_frame_duration(self):
return self.last_frame_duration
def update(self):
ticks = get_ticks()
self.last_frame_duration = duration = ticks - self.last_ticks
if not self.closed:
self.ticks += duration
self.last_ticks = ticks