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 "PrimeGrenadeState.h"
20 #include <sstream>
21 #include <cmath>
22 #include "../Engine/Game.h"
23 #include "../Resource/ResourcePack.h"
24 #include "../Engine/Language.h"
25 #include "../Engine/Palette.h"
26 #include "../Engine/Action.h"
27 #include "../Interface/Text.h"
28 #include "../Interface/Frame.h"
29 #include "../Engine/InteractiveSurface.h"
30 #include "../Savegame/BattleItem.h"
31 #include "../Savegame/SavedGame.h"
32 #include "../Savegame/SavedBattleGame.h"
33 
34 namespace OpenXcom
35 {
36 
37 /**
38  * Initializes all the elements in the Prime Grenade window.
39  * @param game Pointer to the core game.
40  * @param action Pointer to the action.
41  * @param inInventoryView Called from inventory?
42  * @param grenadeInInventory Pointer to associated grenade.
43  */
PrimeGrenadeState(Game * game,BattleAction * action,bool inInventoryView,BattleItem * grenadeInInventory)44 PrimeGrenadeState::PrimeGrenadeState(Game *game, BattleAction *action, bool inInventoryView, BattleItem *grenadeInInventory) : State(game), _action(action), _inInventoryView(inInventoryView), _grenadeInInventory(grenadeInInventory)
45 {
46 	_screen = false;
47 
48 	// Create objects
49 	_title = new Text(192, 24, 65, 44);
50 	_frame = new Frame(192, 27, 65, 37);
51 	_bg = new Surface(192, 93, 65, 45);
52 
53 	int x = 67;
54 	int y = 68;
55 	for (int i = 0; i < 24; ++i)
56 	{
57 		_button[i] = new InteractiveSurface(22, 22, x-1+((i%8)*24), y-4+((i/8)*25));
58 		_number[i] = new Text(20, 20, x+((i%8)*24), y-1+((i/8)*25));
59 	}
60 
61 	// Set palette
62 	setPalette("PAL_BATTLESCAPE");
63 
64 	// Set up objects
65 	add(_bg);
66 	_bg->drawRect(0, 0, _bg->getWidth(), _bg->getHeight(), Palette::blockOffset(6)+9);
67 
68 	add(_frame);
69 	_frame->setColor(Palette::blockOffset(6)+3);
70 	_frame->setBackground(Palette::blockOffset(6)+12);
71 	_frame->setThickness(3);
72 	_frame->setHighContrast(true);
73 
74 	add(_title);
75 	_title->setAlign(ALIGN_CENTER);
76 	_title->setBig();
77 	_title->setText(tr("STR_SET_TIMER"));
78 	_title->setColor(Palette::blockOffset(1)-1);
79 	_title->setHighContrast(true);
80 
81 	for (int i = 0; i < 24; ++i)
82 	{
83 		SDL_Rect square;
84 		add(_button[i]);
85 		_button[i]->onMouseClick((ActionHandler)&PrimeGrenadeState::btnClick);
86 		square.x = 0;
87 		square.y = 0;
88 		square.w = _button[i]->getWidth();
89 		square.h = _button[i]->getHeight();
90 		_button[i]->drawRect(&square, Palette::blockOffset(0)+15);
91 		square.x++;
92 		square.y++;
93 		square.w -= 2;
94 		square.h -= 2;
95 		_button[i]->drawRect(&square, Palette::blockOffset(6)+12);
96 
97 		std::wostringstream ss;
98 		ss << i;
99 		add(_number[i]);
100 		_number[i]->setBig();
101 		_number[i]->setText(ss.str());
102 		_number[i]->setColor(Palette::blockOffset(1)-1);
103 		_number[i]->setHighContrast(true);
104 		_number[i]->setAlign(ALIGN_CENTER);
105 		_number[i]->setVerticalAlign(ALIGN_MIDDLE);
106 	}
107 
108 	centerAllSurfaces();
109 	lowerAllSurfaces();
110 }
111 
112 /**
113  *
114  */
~PrimeGrenadeState()115 PrimeGrenadeState::~PrimeGrenadeState()
116 {
117 
118 }
119 
120 /**
121  * Closes the window on right-click.
122  * @param action Pointer to an action.
123  */
handle(Action * action)124 void PrimeGrenadeState::handle(Action *action)
125 {
126 	State::handle(action);
127 	if (action->getDetails()->type == SDL_MOUSEBUTTONDOWN && action->getDetails()->button.button == SDL_BUTTON_RIGHT)
128 	{
129 		if (!_inInventoryView) _action->value = -1;
130 		_game->popState();
131 	}
132 }
133 
134 
135 /**
136  * Executes the action corresponding to this action menu item.
137  * @param action Pointer to an action.
138  */
btnClick(Action * action)139 void PrimeGrenadeState::btnClick(Action *action)
140 {
141 	int btnID = -1;
142 
143 	if (action->getDetails()->button.button == SDL_BUTTON_RIGHT)
144 	{
145 		if (!_inInventoryView) _action->value = btnID;
146 		_game->popState();
147 		return;
148 	}
149 
150 	// got to find out which button was pressed
151 	for (int i = 0; i < 24 && btnID == -1; ++i)
152 	{
153 		if (action->getSender() == _button[i])
154 		{
155 			btnID = i;
156 		}
157 	}
158 
159 	if (btnID != -1)
160 	{
161 		if (_inInventoryView) _grenadeInInventory->setFuseTimer(0 + btnID);
162 		else _action->value = btnID;
163 		_game->popState();
164 		if (!_inInventoryView) _game->popState();
165 	}
166 }
167 
168 }
169