2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License along
13 with this program; if not, write to the Free Software Foundation, Inc.,
14 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include "openc2eview.h"
21 #include "QtBackend.h"
30 * provide input to the game engine
32 * provide a resize event to the engine as needed
36 openc2eView::openc2eView(QWidget
*parent
, boost::shared_ptr
<QtBackend
> b
) : QAbstractScrollArea(parent
) {
39 viewport()->setAttribute(Qt::WA_PaintOnScreen
); // disable double-buffering
40 viewport()->setAttribute(Qt::WA_OpaquePaintEvent
); // no need for Qt to draw a background
42 // keyboard focus. needed? better way?
43 setFocusPolicy(Qt::StrongFocus
);
45 // we need the mouse move events
46 setMouseTracking(true);
48 // TODO: sane? do we really need relx/rely, anyway?
49 lastmousex
= mapFromGlobal(QCursor::pos()).x();
50 lastmousey
= mapFromGlobal(QCursor::pos()).y();
55 openc2eView::~openc2eView() {
58 boost::shared_ptr
<class Backend
> openc2eView::getBackend() {
59 return boost::dynamic_pointer_cast
<class Backend
, class QtBackend
>(backend
);
62 void openc2eView::resizescrollbars() {
63 if (world
.camera
.getMetaRoom()) {
64 if (world
.camera
.getMetaRoom()->wraparound()) {
65 horizontalScrollBar()->setRange(0,world
.camera
.getMetaRoom()->width());
67 horizontalScrollBar()->setRange(0,world
.camera
.getMetaRoom()->width() - viewport()->width());
69 verticalScrollBar()->setRange(0,world
.camera
.getMetaRoom()->height() - viewport()->height());
73 bool firsttime
= true;
74 int currentwidth
, currentheight
;
76 void openc2eView::resizeEvent(QResizeEvent
*) {
78 backend
->setup(viewport());
82 backend
->resized(viewport()->width(), viewport()->height());
83 currentwidth
= viewport()->width();
84 currentheight
= viewport()->height();
88 horizontalScrollBar()->setPageStep(width());
89 verticalScrollBar()->setPageStep(height());
92 void openc2eView::paintEvent(QPaintEvent
*) {
93 ((QApplication
*)QApplication::instance())->syncX();
97 if (currentwidth
== viewport()->width() && currentheight
== viewport()->height()) {
99 backend
->renderDone();
104 void openc2eView::mouseMoveEvent(QMouseEvent
*m
) {
105 // add mouse move event to backend queue
107 e
.type
= eventmousemove
;
108 e
.xrel
= m
->x() - lastmousex
;
109 e
.yrel
= m
->y() - lastmousey
;
110 lastmousex
= e
.x
= m
->x();
111 lastmousey
= e
.y
= m
->y();
113 if (m
->buttons() & Qt::LeftButton
) e
.button
|= buttonleft
;
114 if (m
->buttons() & Qt::MidButton
) e
.button
|= buttonmiddle
;
115 if (m
->buttons() & Qt::RightButton
) e
.button
|= buttonright
;
116 backend
->pushEvent(e
);
120 void openc2eView::mouseEvent(QMouseEvent
*m
, eventtype t
) {
125 switch (m
->button()) {
126 case Qt::LeftButton
: e
.button
= buttonleft
; break;
127 case Qt::MidButton
: e
.button
= buttonmiddle
; break;
128 case Qt::RightButton
: e
.button
= buttonright
; break;
131 backend
->pushEvent(e
);
134 void openc2eView::mousePressEvent(QMouseEvent
*m
) {
135 mouseEvent(m
, eventmousebuttondown
);
138 void openc2eView::mouseReleaseEvent(QMouseEvent
*m
) {
139 mouseEvent(m
, eventmousebuttonup
);
142 void openc2eView::wheelEvent(QWheelEvent
*w
) {
144 e
.type
= eventmousebuttondown
;
147 // TODO: qt combines events, we should generate one event for every 120 delta, maybe
149 e
.button
= buttonwheelup
;
151 e
.button
= buttonwheeldown
;
152 backend
->pushEvent(e
);
155 void openc2eView::keyPressEvent(QKeyEvent
*k
) {
156 backend
->keyEvent(k
, true);
159 void openc2eView::keyReleaseEvent(QKeyEvent
*k
) {
160 backend
->keyEvent(k
, false);
163 void openc2eView::scrollContentsBy(int dx
, int dy
) {
167 world
.camera
.moveTo(horizontalScrollBar()->value() + lastMetaroom
->x(), verticalScrollBar()->value() + lastMetaroom
->y());
170 void openc2eView::tick() {
171 if (lastMetaroom
!= world
.camera
.getMetaRoom()) {
172 lastMetaroom
= world
.camera
.getMetaRoom();
177 bool openc2eView::needsRender() {
178 return backend
->needsRender();