Nocte- / rhea

A constraint solver based on Cassowary
MIT License
200 stars 19 forks source link

Unexpected behavior of constraints #10

Closed hfossli closed 10 years ago

hfossli commented 10 years ago

Expectations: I am expecting greenView to be positioned wherever blueView is positioned with an offset of 210 on the x-axis.

    solver.add_constraints({
        greenLeft == blueLeft + 210,
    });

Result: blueView is not moving when suggesting new values.

Code

#import "ViewController.h"
#include "rhea/simplex_solver.hpp"
#include "rhea/iostream.hpp"

static rhea::simplex_solver solver;
static rhea::variable blueLeft (10), blueTop (10), blueWidth (200), blueHeight (200);
static rhea::variable greenLeft (10), greenTop (10), greenWidth (200), greenHeight (200);
static rhea::variable boundsWidth (1000), boundsHeight (768);

@interface ViewController ()

@property (nonatomic, strong) UIView *blueView;
@property (nonatomic, strong) UIView *greenView;

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    [self setupViews];
    [self setupConstraints];
    [self updateFrames];
}

- (void)setupViews
{
    self.blueView = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 200, 200)];
    self.blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.blueView];

    UIPanGestureRecognizer *blueRecognizer = [UIPanGestureRecognizer new];
    [blueRecognizer addTarget:self action:@selector(blueRecognizerUpdated:)];
    [self.blueView addGestureRecognizer:blueRecognizer];

    self.greenView = [[UIView alloc] initWithFrame:CGRectMake(220, 10, 200, 200)];
    self.greenView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:self.greenView];

    UIPanGestureRecognizer *greenRecognizer = [UIPanGestureRecognizer new];
    [greenRecognizer addTarget:self action:@selector(greeRecognizerUpdated:)];
    [self.greenView addGestureRecognizer:greenRecognizer];
}

- (void)setupConstraints
{
    solver.set_autosolve(false);

    solver.add_constraints({
        greenLeft == blueLeft + 210,
    });

    solver.add_constraints({
        greenTop == blueTop,
    });

    solver.solve();
}

- (void)updateFrames
{
    CGRect blueFrame;
    blueFrame.origin.x = blueLeft.value();
    blueFrame.origin.y = blueTop.value();
    blueFrame.size.width = blueWidth.value();
    blueFrame.size.height = blueHeight.value();
    self.blueView.frame = blueFrame;

    CGRect greenFrame;
    greenFrame.origin.x = greenLeft.value();
    greenFrame.origin.y = greenTop.value();
    greenFrame.size.width = greenWidth.value();
    greenFrame.size.height = greenHeight.value();
    self.greenView.frame = greenFrame;
}

- (void)blueRecognizerUpdated:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:recognizer.view.superview];
    [recognizer setTranslation:CGPointZero inView:recognizer.view.superview];

    solver.suggest({
        { blueLeft, self.blueView.frame.origin.x + translation.x },
        { blueTop, self.blueView.frame.origin.y + translation.y }
    });

    solver.solve();
    [self updateFrames];
}

- (void)greeRecognizerUpdated:(UIPanGestureRecognizer *)recognizer
{
    CGPoint translation = [recognizer translationInView:recognizer.view.superview];
    [recognizer setTranslation:CGPointZero inView:recognizer.view.superview];

    solver.suggest({
        { greenLeft, self.greenView.frame.origin.x + translation.x },
        { greenTop, self.greenView.frame.origin.y + translation.y }
    });

    solver.solve();
    [self updateFrames];
}

@end
hfossli commented 10 years ago

I'm assuming this is because I don't add the variables as "stay" variables