summaryrefslogtreecommitdiff
path: root/src/outputwindow.cc
blob: 0f66a0bdacca3493f433637a8a485940eac33521 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set et sw=2 ts=2: */
/***************************************************************************
 *            outputwindow.cc
 *
 *  Sat Aug  4 13:44:41 CEST 2012
 *  Copyright 2012 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of Kaiman.
 *
 *  Kaiman is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  Kaiman is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Kaiman; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#include "outputwindow.h"

#include <QPixmap>
#include <QPainter>

#include <math.h>

OutputWindow::OutputWindow()
{
  QPixmap img("gfx/kaiman.png");
  //QPixmap img("gfx/arrow.png");
  kaiman = img.toImage();
  /*
  QTransform t;
  t.scale(0.2, 0.2);
  kaiman = kaiman.transformed(t, Qt::SmoothTransformation);
  */
  connect(&timer, SIGNAL(timeout()), this, SLOT(timeout()));
  timer.setSingleShot(true);
  timer.start(25);

  reset();
}

void OutputWindow::timeout()
{
  /*
  x++;
  y++;
  r++;
  */
  repaint();
  timer.start(25);
}

void OutputWindow::paintEvent(QPaintEvent *)
{
  //  sem.acquire();
  QPainter p(this);

  QTransform t;
  t.rotate(-r + 90);
  t.scale(100.0 / kaiman.width(), 100.0 / kaiman.width());
  QImage img = kaiman.transformed(t, Qt::SmoothTransformation);
  p.drawImage(x - img.width()/2, y - img.height()/2, img);

  QPen pen(QColor(255,0,0,100));
  pen.setWidth(4);
  p.setPen(pen);
  p.drawLines(points);

  sem.release();
}

void OutputWindow::reset()
{
  points.clear();
  x = width() / 2;
  y = height() / 2;
  r = 0;
}

static inline int sign(int x)
{
  if(x > 0) return 1;
  return -1;
}

void OutputWindow::forward(int x)
{
  sem.acquire();
  float target_x = sin(r * (M_PI / 180.0)) * x;
  float target_y = cos(r * (M_PI / 180.0)) * x;

  float source_x = this->x;
  float source_y = this->y;

  for(int i = 0; i < abs(x); i++) {
    float d = (float)i / (float)x * sign(x);
    this->x = source_x + target_x * d;
    this->y = source_y + target_y * d;
    points.append(QPointF(this->x, this->y));
    sem.acquire();
  }
}

void OutputWindow::turn(int x)
{
  sem.acquire();
  for(int i = 0; i < abs(x) * 2; i++) {
    this->r += sign(x) * 0.5;
    sem.acquire();
  }
}