var alpha, beta ,gamma ;
var uhol = 0;
var socket;
var deltaX = 0;
var deltaY = 0;

function setup(){
  createCanvas(300,300);
  socket = io.connect(':3000');

  var options = {
    preventDefault: true
  };

  var hammer = new Hammer(document.body, options);
  hammer.get('swipe').set({
    direction: Hammer.DIRECTION_ALL,
  });
  hammer.on("swipe", swiped);


}

function draw(){
  fill(255);
  background(50);
  ellipse(gamma,beta,40,40);
}


window.addEventListener('deviceorientation', function(e)
{
  //alpha = e.alpha;
  beta = map(e.beta,-90,90,0,300);
  gamma = map(e.gamma,-90,90,0,300);
  uhol = vypocetUhlu(gamma,beta);
  //print(uhol);
  socket.emit('vodovaha', [uhol,beta/300,deltaX,deltaY]);
});


function vypocetUhlu(x,y){
  //vypocet uhlu dotyku od stredu a prepocet 0-1 proti hodinovym rucickam pre PD
  push();
  translate(displayWidth/2, displayHeight/2);
  var angle = atan2((displayHeight/2)-y,(displayWidth/2)-x);
  pop();
  return map(((degrees(angle)+270)%360),0,360,1,0);

}

function swiped(e){
  deltaY = abs(e.deltaY);
  deltaX = map(abs(e.deltaX),0,displayWidth,0,1);
  print(deltaX,deltaY);

}
