summaryrefslogtreecommitdiffstats
path: root/konquest/map_widget.cc
blob: 9bb1a632e4302baff92ecd29b2bd3d0265aaafa9 (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <qpixmap.h>
#include <qpainter.h>
#include <qcolor.h>

#include <kapplication.h>
#include <kiconloader.h>
#include <kglobalsettings.h>

#include <kglobal.h>
#include "map_widget.moc"

ConquestMap::ConquestMap(  Map *newMap, QWidget *parent )
    : QGridView( parent ),
    SECTOR_HEIGHT( 28 ), SECTOR_WIDTH( 28 ),
    BOARD_HEIGHT( newMap->getRows() * SECTOR_HEIGHT ),
    BOARD_WIDTH( newMap->getColumns() * SECTOR_WIDTH ),
    map( newMap ), gridColor( 50, 80, 50 ),
    hiLiteRow( -1 ), hiLiteCol( -1 )
{
    labelFont = KGlobalSettings::generalFont();
    labelFont.setPointSize( 8 );

    setFrameStyle( NoFrame );
    setPaletteBackgroundColor( black );
    setMinimumSize( BOARD_HEIGHT, BOARD_WIDTH );

    setCellWidth( SECTOR_WIDTH );
    setCellHeight( SECTOR_HEIGHT );
    setNumRows( map->getRows() );
    setNumCols( map->getColumns() );

    setMinimumSize( BOARD_HEIGHT, BOARD_WIDTH );
    setMaximumSize( BOARD_HEIGHT, BOARD_WIDTH );

    connect( map, SIGNAL( update() ), this, SLOT( mapUpdate() ) );

    QTimer *timer = new QTimer( this );
    connect( timer, SIGNAL(timeout()), this, SLOT(squareBlink()) );
    timer->start( 500, false );

    viewport()->setMouseTracking( true );
    setMouseTracking( true );
	
    show();


}

ConquestMap::~ConquestMap()
{
}

	
void
ConquestMap::contentsMousePressEvent( QMouseEvent *e )
{
    int row, col;

    row = rowAt( e->y() );
    col = columnAt( e->x() );

    if( map->getSector( row, col ).hasPlanet() ) {
        emit planetSelected( map->getSector( row, col ).getPlanet() );
    }

}

void
ConquestMap::contentsMouseMoveEvent( QMouseEvent *e )
{
    // highlight the square under the mouse
    int row, col;

    row = rowAt( e->y() );
    col = columnAt( e->x() );

    // Check to make sure the mouse is in a valid grid location
    if( (row < 0 || col < 0) ||
    	(row >= BOARD_ROWS || col >= BOARD_COLS) )  {
	    return;
    }


    if( (hiLiteRow != -1) && (hiLiteCol != -1)  ) {
        QPainter p( viewport() );

        p.translate( hiLiteCol * cellWidth(), hiLiteRow * cellHeight() );

        drawSector( &p, map->getSector(hiLiteRow,hiLiteCol) );

        hiLiteRow = -1;
        hiLiteCol = -1;

     }

    if( map->getSector( row, col ).hasPlanet() ) {
        QPainter p( viewport() );

        p.translate( col * cellWidth(),row * cellHeight() );

        drawSector( &p, map->getSector(row,col), false, true );
        emit planetHighlighted(map->getSector( row, col ).getPlanet() );

        hiLiteRow = row;
        hiLiteCol = col;

    }

}

void
ConquestMap::unselectPlanet()
{
    map->setSelectedSector();
}


void
ConquestMap::paintCell( QPainter *p, int row, int col )
{
    drawSector( p, map->getSector( row, col ) );
}

void
ConquestMap::squareBlink()
{
    static bool blinkState = true;

    int row, col;
    if( map->selectedSector( row, col ) ) {
        QPainter p( this, true );

        p.translate( col * cellWidth(), row * cellHeight() );

        if( blinkState ) {
            drawSector( &p, map->getSector(row,col), true );
        } else {
            drawSector( &p, map->getSector(row,col), false );
        }
    }

    if( blinkState )
        blinkState = false;
    else
        blinkState = true;
}


void
ConquestMap::mapUpdate()
{
    viewport()->repaint(false);
}


void
ConquestMap::drawSector( QPainter *p, Sector &sector, bool borderStrobe, bool highlight )
{
    QColor labelColor( white );
    QPoint labelCorner;

    if( sector.hasPlanet() ) {
        QPixmap pm;

        // simple (pathetic) way to "randomize"
        // the planet graphic
	// and also a really dirty hack to make the planet
	// name more visible (hard coded pixel offsets)
        switch( ((sector.getRow()+sector.getColumn()) % 9) + 1  ) {
        case 1 :
            pm = QPixmap( IMAGE_PLANET_1 );
            labelCorner = QPoint( 18, 14 );
            break;
        case 2 :
            pm = QPixmap( IMAGE_PLANET_2 );
            labelCorner = QPoint( 2, 14 );
            break;
        case 3 :
            pm = QPixmap( IMAGE_PLANET_3 );
            labelCorner = QPoint( 2, 26 );
            break;
        case 4 :
            pm = QPixmap( IMAGE_PLANET_4 );
            labelCorner = QPoint( 18, 26 );
            break;
        case 5 :
            pm = QPixmap( IMAGE_PLANET_5 );
            labelCorner = QPoint( 18, 26 );
            break;
        case 6 :
            pm = QPixmap( IMAGE_PLANET_6 );
            labelCorner = QPoint( 18, 26 );
            break;
        case 7 :
            pm = QPixmap( IMAGE_PLANET_7 );
            labelCorner = QPoint( 18, 26 );
            break;
        case 8 :
            pm = QPixmap( IMAGE_PLANET_8 );
            labelCorner = QPoint( 18, 26 );
            break;
        case 9 :
            pm = QPixmap( IMAGE_PLANET_9 );
            labelCorner = QPoint( 18, 26 );
            break;
        }

        QPoint pos;

        pos.setX( ( SECTOR_HEIGHT / 2 ) - ( pm.height() / 2 ) );
        pos.setY( ( SECTOR_WIDTH / 2 ) - ( pm.width() / 2 ) );

        p->drawPixmap( pos, pm, QRect(0, 0, pm.height(), pm.width() ) );

        p->setFont( labelFont );
        p->setPen( labelColor );
	
        p->drawText( labelCorner, sector.getPlanet()->getName() );

        if( borderStrobe ) {
            QPen gridPen( sector.getPlanet()->getPlayer()->getColor() );
            p->setPen( gridPen );
        } else if( highlight ) {
            QPen gridPen( white );
            p->setPen( gridPen );
        } else {
            QPen gridPen( gridColor );
            p->setPen( gridPen );
        }

    } else {
        p->eraseRect( 0, 0, SECTOR_WIDTH, SECTOR_HEIGHT );

        QPen gridPen( gridColor );

        p->setPen( gridPen );
    }

    p->drawRect( 0, 0, SECTOR_HEIGHT, SECTOR_WIDTH );

}