1 /*
2 * Copyright 2010-2014 OpenXcom Developers.
3 *
4 * This file is part of OpenXcom.
5 *
6 * OpenXcom is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * OpenXcom is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with OpenXcom. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "MiniBaseView.h"
20 #include <cmath>
21 #include "../Engine/SurfaceSet.h"
22 #include "../Engine/Action.h"
23 #include "../Savegame/Base.h"
24 #include "../Savegame/BaseFacility.h"
25 #include "../Ruleset/RuleBaseFacility.h"
26 #include "../Savegame/Craft.h"
27 #include "../Engine/Palette.h"
28
29 namespace OpenXcom
30 {
31
32 /**
33 * Sets up a mini base view with the specified size and position.
34 * @param width Width in pixels.
35 * @param height Height in pixels.
36 * @param x X position in pixels.
37 * @param y Y position in pixels.
38 */
MiniBaseView(int width,int height,int x,int y)39 MiniBaseView::MiniBaseView(int width, int height, int x, int y) : InteractiveSurface(width, height, x, y), _bases(), _texture(0), _base(0), _hoverBase(0)
40 {
41 }
42
43 /**
44 *
45 */
~MiniBaseView()46 MiniBaseView::~MiniBaseView()
47 {
48 }
49
50 /**
51 * Changes the current list of bases to display.
52 * @param bases Pointer to base list to display.
53 */
setBases(std::vector<Base * > * bases)54 void MiniBaseView::setBases(std::vector<Base*> *bases)
55 {
56 _bases = bases;
57 _redraw = true;
58 }
59
60 /**
61 * Changes the texture to use for drawing
62 * the various base elements.
63 * @param texture Pointer to SurfaceSet to use.
64 */
setTexture(SurfaceSet * texture)65 void MiniBaseView::setTexture(SurfaceSet *texture)
66 {
67 _texture = texture;
68 }
69
70 /**
71 * Returns the base the mouse cursor is currently over.
72 * @return ID of the base.
73 */
getHoveredBase() const74 size_t MiniBaseView::getHoveredBase() const
75 {
76 return _hoverBase;
77 }
78
79 /**
80 * Changes the base that is currently selected on
81 * the mini base view.
82 * @param base ID of base.
83 */
setSelectedBase(size_t base)84 void MiniBaseView::setSelectedBase(size_t base)
85 {
86 _base = base;
87 _redraw = true;
88 }
89
90 /**
91 * Draws the view of all the bases with facilities
92 * in varying colors.
93 */
draw()94 void MiniBaseView::draw()
95 {
96 Surface::draw();
97 for (size_t i = 0; i < MAX_BASES; ++i)
98 {
99 // Draw base squares
100 if (i == _base)
101 {
102 SDL_Rect r;
103 r.x = i * (MINI_SIZE + 2);
104 r.y = 0;
105 r.w = MINI_SIZE + 2;
106 r.h = MINI_SIZE + 2;
107 drawRect(&r, 1);
108 }
109 _texture->getFrame(41)->setX(i * (MINI_SIZE + 2));
110 _texture->getFrame(41)->setY(0);
111 _texture->getFrame(41)->blit(this);
112
113 // Draw facilities
114 if (i < _bases->size())
115 {
116 SDL_Rect r;
117 lock();
118 for (std::vector<BaseFacility*>::iterator f = _bases->at(i)->getFacilities()->begin(); f != _bases->at(i)->getFacilities()->end(); ++f)
119 {
120 int pal;
121 if ((*f)->getBuildTime() == 0)
122 pal = 3;
123 else
124 pal = 2;
125
126 r.x = i * (MINI_SIZE + 2) + 2 + (*f)->getX() * 2;
127 r.y = 2 + (*f)->getY() * 2;
128 r.w = (*f)->getRules()->getSize() * 2;
129 r.h = (*f)->getRules()->getSize() * 2;
130 drawRect(&r, Palette::blockOffset(pal)+3);
131 r.x++;
132 r.y++;
133 r.w--;
134 r.h--;
135 drawRect(&r, Palette::blockOffset(pal)+5);
136 r.x--;
137 r.y--;
138 drawRect(&r, Palette::blockOffset(pal)+2);
139 r.x++;
140 r.y++;
141 r.w--;
142 r.h--;
143 drawRect(&r, Palette::blockOffset(pal)+3);
144 r.x--;
145 r.y--;
146 setPixel(r.x, r.y, Palette::blockOffset(pal)+1);
147 }
148 unlock();
149 }
150 }
151 }
152
153 /**
154 * Selects the base the mouse is over.
155 * @param action Pointer to an action.
156 * @param state State that the action handlers belong to.
157 */
mouseOver(Action * action,State * state)158 void MiniBaseView::mouseOver(Action *action, State *state)
159 {
160 _hoverBase = (int)floor(action->getRelativeXMouse() / ((MINI_SIZE + 2) * action->getXScale()));
161 InteractiveSurface::mouseOver(action, state);
162 }
163
164 }
165