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 "agg_image.h"
24 #include "cursor.h"
25 #include "dialog.h"
26 #include "game.h"
27 #include "icn.h"
28 #include "localevent.h"
29 #include "payment.h"
30 #include "settings.h"
31 #include "text.h"
32 #include "translations.h"
33 #include "ui_button.h"
34 
BuyBoat(bool enable)35 int Dialog::BuyBoat( bool enable )
36 {
37     fheroes2::Display & display = fheroes2::Display::instance();
38 
39     const int system = Settings::Get().ExtGameEvilInterface() ? ICN::SYSTEME : ICN::SYSTEM;
40 
41     // setup cursor
42     const CursorRestorer cursorRestorer( true, Cursor::POINTER );
43 
44     Resource::BoxSprite rbs( PaymentConditions::BuyBoat(), BOXAREA_WIDTH );
45 
46     const fheroes2::Sprite & sprite = fheroes2::AGG::GetICN( ICN::BOATWIND, 0 );
47     Text text( _( "Build a new ship:" ), Font::BIG );
48     const int spacer = 10;
49 
50     Dialog::FrameBox box( text.h() + spacer + sprite.height() + spacer + text.h() + spacer + rbs.GetArea().height - 20, true );
51 
52     const fheroes2::Rect & box_rt = box.GetArea();
53     fheroes2::Point dst_pt( box_rt.x + ( box_rt.width - text.w() ) / 2, box_rt.y );
54     text.Blit( dst_pt.x, dst_pt.y );
55 
56     dst_pt.x = box_rt.x + ( box_rt.width - sprite.width() ) / 2;
57     dst_pt.y = box_rt.y + text.h() + spacer;
58     fheroes2::Blit( sprite, display, dst_pt.x, dst_pt.y );
59 
60     text.Set( _( "Resource cost:" ), Font::BIG );
61     dst_pt.x = box_rt.x + ( box_rt.width - text.w() ) / 2;
62     dst_pt.y = dst_pt.y + sprite.height() + spacer;
63     text.Blit( dst_pt.x, dst_pt.y );
64 
65     rbs.SetPos( box_rt.x, dst_pt.y + spacer );
66     rbs.Redraw();
67 
68     // buttons
69     dst_pt.x = box_rt.x;
70     dst_pt.y = box_rt.y + box_rt.height - fheroes2::AGG::GetICN( system, 1 ).height();
71     fheroes2::Button button1( dst_pt.x, dst_pt.y, system, 1, 2 );
72 
73     dst_pt.x = box_rt.x + box_rt.width - fheroes2::AGG::GetICN( system, 3 ).width();
74     dst_pt.y = box_rt.y + box_rt.height - fheroes2::AGG::GetICN( system, 3 ).height();
75     fheroes2::Button button2( dst_pt.x, dst_pt.y, system, 3, 4 );
76 
77     if ( !enable ) {
78         button1.press();
79         button1.disable();
80     }
81 
82     button1.draw();
83     button2.draw();
84 
85     display.render();
86 
87     LocalEvent & le = LocalEvent::Get();
88 
89     // message loop
90     while ( le.HandleEvents() ) {
91         if ( button1.isEnabled() )
92             le.MousePressLeft( button1.area() ) ? button1.drawOnPress() : button1.drawOnRelease();
93         le.MousePressLeft( button2.area() ) ? button2.drawOnPress() : button2.drawOnRelease();
94 
95         if ( button1.isEnabled() && ( Game::HotKeyPressEvent( Game::EVENT_DEFAULT_READY ) || le.MouseClickLeft( button1.area() ) ) )
96             return Dialog::OK;
97 
98         if ( Game::HotKeyPressEvent( Game::EVENT_DEFAULT_EXIT ) || le.MouseClickLeft( button2.area() ) )
99             return Dialog::CANCEL;
100     }
101 
102     return Dialog::ZERO;
103 }
104