<commit_before>//
//  GameTouchWindow.m
//  GameEngine
//
//  Created by <NAME> on 2012-02-26.
//  Copyright 2012 Sharkable. All rights reserved.
//

#import "GameTouchWindow.h"

#include <vector>
using std::vector;

#import "game_engine.h"
#import "Touch.h"

@interface GameTouchWindow ()
- (std::vector<Touch>)convertTouches:(NSSet *)touches;
@end

@implementation GameTouchWindow {
 @private
  GameEngine *gameEngine_;  // weak
  CGFloat scale_;
  double move_factor_;
}

@synthesize gameEngine = gameEngine_;

- (id)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    scale_ = [UIScreen mainScreen].scale;
  }
  return self;
}

#pragma mark - UIWindow

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  move_factor_ = 0;
  vector<Touch> converted_touches = [self convertTouches:touches];
  for (auto i = converted_touches.begin(); i != converted_touches.end(); i++) {
    gameEngine_->AddTouchBegan(*i);
  }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  vector<Touch> converted_touches = [self convertTouches:touches];
  for (auto i = converted_touches.begin(); i != converted_touches.end(); i++) {
    gameEngine_->AddTouchMoved(*i);
  }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  vector<Touch> converted_touches = [self convertTouches:touches];
  for (auto i = converted_touches.begin(); i != converted_touches.end(); i++) {
    gameEngine_->AddTouchEnded(*i);
  }
}

#pragma mark - Private

- (vector<Touch>)convertTouches:(NSSet *)touches {
  vector<Touch> converted_touches;
  for (UITouch *touch in touches) {
    Touch converted_touch;
    CGPoint location = [touch locationInView:touch.view];

    // Fudge the point by some factor of the velocity, to prevent the lag in movement.
    CGPoint previous_location = [touch previousLocationInView:touch.view];
    double diff_x = location.x - previous_location.x;
    double diff_y = location.y - previous_location.y;
    double new_factor = 0;
    double velocity_squared = diff_x * diff_x + diff_y * diff_y;
    if (velocity_squared > 25 * 25) {
      new_factor = 1.5;
    } else {
      new_factor = velocity_squared / (25 * 25 / 1.5);
    }
    move_factor_ = (move_factor_ + new_factor) / 2;
    location.x = location.x + diff_x * move_factor_;
    location.y = location.y + diff_y * move_factor_;

    ScreenPoint l = screen_point_make(location.x * scale_, location.y * scale_);<commit_msg>Removing touch move fudging for now. This was used in Air Hockey. Consider a solution.<commit_after>//
//  GameTouchWindow.m
//  GameEngine
//
//  Created by <NAME> on 2012-02-26.
//  Copyright 2012 Sharkable. All rights reserved.
//

#import "GameTouchWindow.h"

#include <vector>
using std::vector;

#import "game_engine.h"
#import "Touch.h"

@interface GameTouchWindow ()
- (std::vector<Touch>)convertTouches:(NSSet *)touches;
@end

@implementation GameTouchWindow {
 @private
  GameEngine *gameEngine_;  // weak
  CGFloat scale_;
  double move_factor_;
}

@synthesize gameEngine = gameEngine_;

- (id)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    scale_ = [UIScreen mainScreen].scale;
  }
  return self;
}

#pragma mark - UIWindow

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  move_factor_ = 0;
  vector<Touch> converted_touches = [self convertTouches:touches];
  for (auto i = converted_touches.begin(); i != converted_touches.end(); i++) {
    gameEngine_->AddTouchBegan(*i);
  }
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
  vector<Touch> converted_touches = [self convertTouches:touches];
  for (auto i = converted_touches.begin(); i != converted_touches.end(); i++) {
    gameEngine_->AddTouchMoved(*i);
  }
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  vector<Touch> converted_touches = [self convertTouches:touches];
  for (auto i = converted_touches.begin(); i != converted_touches.end(); i++) {
    gameEngine_->AddTouchEnded(*i);
  }
}

#pragma mark - Private

- (vector<Touch>)convertTouches:(NSSet *)touches {
  vector<Touch> converted_touches;
  for (UITouch *touch in touches) {
    Touch converted_touch;
    CGPoint location = [touch locationInView:touch.view];

// TODO: Should this be a game option? I was using it in Air Hockey.
#if 0
    // Fudge the point by some factor of the velocity, to prevent the lag in movement.
    CGPoint previous_location = [touch previousLocationInView:touch.view];
    double diff_x = location.x - previous_location.x;
    double diff_y = location.y - previous_location.y;
    double new_factor = 0;
    double velocity_squared = diff_x * diff_x + diff_y * diff_y;
    if (velocity_squared > 25 * 25) {
      new_factor = 1.5;
    } else {
      new_factor = velocity_squared / (25 * 25 / 1.5);
    }
    move_factor_ = (move_factor_ + new_factor) / 2;
    location.x = location.x + diff_x * move_factor_;
    location.y = location.y + diff_y * move_factor_;
#endif

    ScreenPoint l = screen_point_make(location.x * scale_, location.y * scale_);
{'fraction_non_alphanumeric': 0.13068293642983664, 'fraction_numerical': 0.01082464081873647, 'mean_word_length': 4.0366699702675914, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3400166', 'n_tokens_mistral': 1843, 'n_tokens_neox': 1744, 'n_words': 410}