Skip to content

Commit

Permalink
added post solve to constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
shawn42 committed Mar 13, 2013
1 parent be99b51 commit b7c5fa4
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
29 changes: 29 additions & 0 deletions ext/chipmunk/rb_cpConstraint.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ rb_cpConstraintPreSolveFunc(cpConstraint *constraint, cpSpace *space) {

}

static void
rb_cpConstraintPostSolveFunc(cpConstraint *constraint, cpSpace *space) {
VALUE rb_constraint = (VALUE)constraint->data;
VALUE callback_block = rb_iv_get(rb_constraint, "@_cp_post_solve");

ID call_id = rb_intern("call");
int arity = NUM2INT(rb_funcall(callback_block, rb_intern("arity"), 0));
switch(arity) {
case 1:
rb_funcall(callback_block, call_id, 1, (VALUE)space->data);
break;
default:
rb_funcall(callback_block, call_id, 0);
}

}

static VALUE
rb_cpConstraintSetPreSolve(int argc, VALUE * argv, VALUE self) {
VALUE callback_block;
Expand All @@ -285,6 +302,17 @@ rb_cpConstraintSetPreSolve(int argc, VALUE * argv, VALUE self) {
return Qnil;
}

static VALUE
rb_cpConstraintSetPostSolve(int argc, VALUE * argv, VALUE self) {
VALUE callback_block;
rb_scan_args(argc, argv, "0&", &callback_block);
rb_iv_set(self, "@_cp_post_solve", callback_block);

cpConstraintSetPostSolveFunc(CONSTRAINT(self), rb_cpConstraintPostSolveFunc);

return Qnil;
}

#define STRINGIFY(v) # v
#define ACCESSOR_METHODS(s, m, name) \
rb_define_method(c_ ## s, STRINGIFY(name), rb_ ## s ## Get ## m, 0); \
Expand Down Expand Up @@ -313,6 +341,7 @@ Init_cpConstraint(void) {
rb_define_method(m_cpConstraint, "max_bias=", rb_cpConstraint_set_maxBias, 1);
rb_define_method(m_cpConstraint, "impulse", rb_cpConstraintGetImpulse, 0);
rb_define_method(m_cpConstraint, "pre_solve", rb_cpConstraintSetPreSolve, -1);
rb_define_method(m_cpConstraint, "post_solve", rb_cpConstraintSetPostSolve, -1);

VALUE c_cpDampedRotarySpring = make_class("DampedRotarySpring", rb_cpDampedRotarySpring_alloc, rb_cpDampedRotarySpring_init, 5);
ACCESSOR_METHODS(cpDampedRotarySpring, RestAngle, rest_angle)
Expand Down
35 changes: 35 additions & 0 deletions spec/constraint_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@
space.step 1
called.should be_true
end

it 'can set post solve func with no args' do
called = false
con.post_solve do
called = true
end

space.step 1
called.should be_true
end

it 'can set post solve func with space arg' do
called = false
con.post_solve do |space|
called = true
space.should == space
end

space.step 1
called.should be_true
end

it 'calls pre then post' do
calls = []
con.pre_solve do
calls << :pre
end
con.post_solve do
calls << :post
end
space.step 1

calls.should == [:pre, :post]

end
end

it 'has these' do
Expand Down

0 comments on commit b7c5fa4

Please sign in to comment.