tweak C1-era CARR
[openc2e.git] / qtgui / openc2eview.cpp
blob26fd3a2c443c4fec9e49b80e449057825360a08d
1 /*
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.
17 #include "World.h"
18 #include "MetaRoom.h"
19 #include "openc2eview.h"
20 #include <QtGui>
21 #include "QtBackend.h"
23 #ifdef _WIN32
24 #include <windows.h>
25 #endif
28 * TODO:
30 * provide input to the game engine
31 * non-X11 support
32 * provide a resize event to the engine as needed
36 openc2eView::openc2eView(QWidget *parent, boost::shared_ptr<QtBackend> b) : QAbstractScrollArea(parent) {
37 backend = b;
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();
52 lastMetaroom = NULL;
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());
66 } else {
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 *) {
77 if (firsttime) {
78 backend->setup(viewport());
79 firsttime = false;
82 backend->resized(viewport()->width(), viewport()->height());
83 currentwidth = viewport()->width();
84 currentheight = viewport()->height();
86 resizescrollbars();
88 horizontalScrollBar()->setPageStep(width());
89 verticalScrollBar()->setPageStep(height());
92 void openc2eView::paintEvent(QPaintEvent *) {
93 ((QApplication *)QApplication::instance())->syncX();
95 if (!firsttime) {
96 // TODO: mad hax
97 if (currentwidth == viewport()->width() && currentheight == viewport()->height()) {
98 world.drawWorld();
99 backend->renderDone();
104 void openc2eView::mouseMoveEvent(QMouseEvent *m) {
105 // add mouse move event to backend queue
106 SomeEvent e;
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();
112 e.button = 0;
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);
119 // helper function
120 void openc2eView::mouseEvent(QMouseEvent *m, eventtype t) {
121 SomeEvent e;
122 e.type = t;
123 e.x = m->x();
124 e.y = m->y();
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;
129 default: return;
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) {
143 SomeEvent e;
144 e.type = eventmousebuttondown;
145 e.x = w->x();
146 e.y = w->y();
147 // TODO: qt combines events, we should generate one event for every 120 delta, maybe
148 if (w->delta() < 0)
149 e.button = buttonwheelup;
150 else
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) {
164 (void)dx;
165 (void)dy;
166 if (lastMetaroom)
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();
173 resizescrollbars();
177 bool openc2eView::needsRender() {
178 return backend->needsRender();