1 /***************************************************************************
2 * Copyright (C) 2009 by Andrey Afletdinov <fheroes2@gmail.com> *
3 * *
4 * Part of the Free Heroes2 Engine: *
5 * http://sourceforge.net/projects/fheroes2 *
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * This program is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with this program; if not, write to the *
19 * Free Software Foundation, Inc., *
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21 ***************************************************************************/
22
23 #include "interface_buttons.h"
24 #include "dialog.h"
25 #include "game_interface.h"
26 #include "heroes.h"
27 #include "icn.h"
28 #include "settings.h"
29 #include "text.h"
30 #include "translations.h"
31 #include "world.h"
32
ButtonsArea(Basic & basic)33 Interface::ButtonsArea::ButtonsArea( Basic & basic )
34 : BorderWindow( fheroes2::Rect( 0, 0, 144, 72 ) )
35 , interface( basic )
36 {}
37
SavePosition(void)38 void Interface::ButtonsArea::SavePosition( void )
39 {
40 Settings::Get().SetPosButtons( GetRect().getPosition() );
41 }
42
SetRedraw(void) const43 void Interface::ButtonsArea::SetRedraw( void ) const
44 {
45 interface.SetRedraw( REDRAW_BUTTONS );
46 }
47
SetPos(s32 ox,s32 oy)48 void Interface::ButtonsArea::SetPos( s32 ox, s32 oy )
49 {
50 BorderWindow::SetPosition( ox, oy );
51
52 const int icnbtn = Settings::Get().ExtGameEvilInterface() ? ICN::ADVEBTNS : ICN::ADVBTNS;
53
54 buttonNextHero.setICNInfo( icnbtn, 0, 1 );
55 buttonMovement.setICNInfo( icnbtn, 2, 3 );
56 buttonKingdom.setICNInfo( icnbtn, 4, 5 );
57 buttonSpell.setICNInfo( icnbtn, 6, 7 );
58 buttonEndTurn.setICNInfo( icnbtn, 8, 9 );
59 buttonAdventure.setICNInfo( icnbtn, 10, 11 );
60 buttonFile.setICNInfo( icnbtn, 12, 13 );
61 buttonSystem.setICNInfo( icnbtn, 14, 15 );
62
63 SetButtonStatus();
64
65 ox = GetArea().x;
66 oy = GetArea().y;
67
68 // Top row
69 buttonNextHero.setPosition( ox, oy );
70 nextHeroRect = buttonNextHero.area();
71
72 buttonMovement.setPosition( nextHeroRect.x + nextHeroRect.width, oy );
73 movementRect = buttonMovement.area();
74
75 buttonKingdom.setPosition( movementRect.x + movementRect.width, oy );
76 kingdomRect = buttonKingdom.area();
77
78 buttonSpell.setPosition( kingdomRect.x + kingdomRect.width, oy );
79 spellRect = buttonSpell.area();
80
81 // Bottom row
82 oy = nextHeroRect.y + nextHeroRect.height;
83
84 buttonEndTurn.setPosition( ox, oy );
85 endTurnRect = buttonEndTurn.area();
86
87 buttonAdventure.setPosition( endTurnRect.x + endTurnRect.width, oy );
88 adventureRect = buttonAdventure.area();
89
90 buttonFile.setPosition( adventureRect.x + adventureRect.width, oy );
91 fileRect = buttonFile.area();
92
93 buttonSystem.setPosition( fileRect.x + fileRect.width, oy );
94 systemRect = buttonSystem.area();
95 }
96
Redraw(void)97 void Interface::ButtonsArea::Redraw( void )
98 {
99 const Settings & conf = Settings::Get();
100
101 if ( !conf.ExtGameHideInterface() || conf.ShowButtons() ) {
102 if ( conf.ExtGameHideInterface() )
103 BorderWindow::Redraw();
104
105 SetButtonStatus();
106
107 buttonNextHero.draw();
108 buttonMovement.draw();
109 buttonKingdom.draw();
110 buttonSpell.draw();
111 buttonEndTurn.draw();
112 buttonAdventure.draw();
113 buttonFile.draw();
114 buttonSystem.draw();
115 }
116 }
117
ResetButtons(void)118 void Interface::ButtonsArea::ResetButtons( void )
119 {
120 if ( buttonNextHero.isEnabled() ) {
121 buttonNextHero.drawOnRelease();
122 }
123
124 buttonMovement.drawOnRelease();
125 buttonKingdom.drawOnRelease();
126
127 if ( buttonSpell.isEnabled() ) {
128 buttonSpell.drawOnRelease();
129 }
130
131 buttonEndTurn.drawOnRelease();
132 buttonAdventure.drawOnRelease();
133 buttonFile.drawOnRelease();
134 buttonSystem.drawOnRelease();
135
136 LocalEvent::Get().ResetPressLeft();
137 }
138
QueueEventProcessing(void)139 fheroes2::GameMode Interface::ButtonsArea::QueueEventProcessing( void )
140 {
141 LocalEvent & le = LocalEvent::Get();
142 fheroes2::GameMode res = fheroes2::GameMode::CANCEL;
143
144 if ( buttonNextHero.isEnabled() )
145 le.MousePressLeft( nextHeroRect ) ? buttonNextHero.drawOnPress() : buttonNextHero.drawOnRelease();
146 le.MousePressLeft( movementRect ) ? buttonMovement.drawOnPress() : buttonMovement.drawOnRelease();
147 le.MousePressLeft( kingdomRect ) ? buttonKingdom.drawOnPress() : buttonKingdom.drawOnRelease();
148 if ( buttonSpell.isEnabled() )
149 le.MousePressLeft( spellRect ) ? buttonSpell.drawOnPress() : buttonSpell.drawOnRelease();
150 le.MousePressLeft( endTurnRect ) ? buttonEndTurn.drawOnPress() : buttonEndTurn.drawOnRelease();
151 le.MousePressLeft( adventureRect ) ? buttonAdventure.drawOnPress() : buttonAdventure.drawOnRelease();
152 le.MousePressLeft( fileRect ) ? buttonFile.drawOnPress() : buttonFile.drawOnRelease();
153 le.MousePressLeft( systemRect ) ? buttonSystem.drawOnPress() : buttonSystem.drawOnRelease();
154
155 if ( Settings::Get().ShowButtons() && BorderWindow::QueueEventProcessing() ) {
156 // Move border window. No other action is required.
157 }
158 else if ( buttonNextHero.isEnabled() && le.MouseClickLeft( nextHeroRect ) ) {
159 interface.EventNextHero();
160 }
161 else if ( le.MouseClickLeft( movementRect ) ) {
162 interface.EventContinueMovement();
163 }
164 else if ( le.MouseClickLeft( kingdomRect ) ) {
165 interface.EventKingdomInfo();
166 }
167 else if ( buttonSpell.isEnabled() && le.MouseClickLeft( spellRect ) ) {
168 interface.EventCastSpell();
169 }
170 else if ( le.MouseClickLeft( endTurnRect ) ) {
171 res = interface.EventEndTurn();
172 }
173 else if ( le.MouseClickLeft( adventureRect ) ) {
174 res = interface.EventAdventureDialog();
175 }
176 else if ( le.MouseClickLeft( fileRect ) ) {
177 res = interface.EventFileDialog();
178 }
179 else if ( le.MouseClickLeft( systemRect ) ) {
180 interface.EventSystemDialog();
181 }
182
183 if ( le.MousePressRight( nextHeroRect ) )
184 Dialog::Message( _( "Next Hero" ), _( "Select the next Hero." ), Font::BIG );
185 else if ( le.MousePressRight( movementRect ) )
186 Dialog::Message( _( "Continue Movement" ), _( "Continue the Hero's movement along the current path." ), Font::BIG );
187 else if ( le.MousePressRight( kingdomRect ) )
188 Dialog::Message( _( "Kingdom Summary" ), _( "View a Summary of your Kingdom." ), Font::BIG );
189 else if ( le.MousePressRight( spellRect ) )
190 Dialog::Message( _( "Cast Spell" ), _( "Cast an adventure spell." ), Font::BIG );
191 else if ( le.MousePressRight( endTurnRect ) )
192 Dialog::Message( _( "End Turn" ), _( "End your turn and left the computer take its turn." ), Font::BIG );
193 else if ( le.MousePressRight( adventureRect ) )
194 Dialog::Message( _( "Adventure Options" ), _( "Bring up the adventure options menu." ), Font::BIG );
195 else if ( le.MousePressRight( fileRect ) )
196 Dialog::Message( _( "File Options" ), _( "Bring up the file options menu, alloving you to load menu, save etc." ), Font::BIG );
197 else if ( le.MousePressRight( systemRect ) )
198 Dialog::Message( _( "System Options" ), _( "Bring up the system options menu, alloving you to customize your game." ), Font::BIG );
199
200 return res;
201 }
202
SetButtonStatus()203 void Interface::ButtonsArea::SetButtonStatus()
204 {
205 Heroes * currentHero = GetFocusHeroes();
206 if ( currentHero == nullptr || !currentHero->GetPath().isValid() || !currentHero->MayStillMove( false, true ) )
207 buttonMovement.disable();
208 else
209 buttonMovement.enable();
210
211 if ( currentHero == nullptr || !currentHero->HaveSpellBook() || !currentHero->MayCastAdventureSpells() )
212 buttonSpell.disable();
213 else
214 buttonSpell.enable();
215
216 const Kingdom & kingdom = world.GetKingdom( Settings::Get().CurrentColor() );
217 const KingdomHeroes & heroes = kingdom.GetHeroes();
218
219 bool isMovableHeroPresent = false;
220 for ( size_t i = 0; i < heroes.size(); ++i ) {
221 if ( heroes[i]->MayStillMove( false, false ) ) {
222 isMovableHeroPresent = true;
223 break;
224 }
225 }
226
227 if ( isMovableHeroPresent ) {
228 buttonNextHero.enable();
229 }
230 else {
231 buttonNextHero.disable();
232 }
233 }
234