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 #ifndef H2TEXT_H
24 #define H2TEXT_H
25 
26 #include <list>
27 #include <string>
28 
29 #include "screen.h"
30 #include "types.h"
31 
32 namespace Font
33 {
34     enum
35     {
36         SMALL = 0x01,
37         BIG = 0x02,
38         YELLOW_BIG = 0x04,
39         YELLOW_SMALL = 0x08,
40         GRAY_BIG = 0x10,
41         GRAY_SMALL = 0x20,
42         WHITE_LARGE = 0x40
43     };
44 }
45 enum
46 {
47     ALIGN_NONE,
48     ALIGN_LEFT,
49     ALIGN_CENTER,
50     ALIGN_RIGHT
51 };
52 
53 class TextAscii;
54 
55 class Text
56 {
57 public:
58     Text();
59     Text( const std::string &, int ft = Font::BIG );
60     ~Text();
61 
62     Text & operator=( const Text & ) = delete;
63 
64     void Set( const std::string &, int );
65     void Set( const std::string & );
66     void Set( int );
67 
68     void Clear( void );
69     size_t Size( void ) const;
70 
w(void)71     int w( void ) const
72     {
73         return static_cast<int>( gw );
74     }
h(void)75     int h( void ) const
76     {
77         return static_cast<int>( gh );
78     }
79 
80     void Blit( s32, s32, fheroes2::Image & sf = fheroes2::Display::instance() ) const;
81     void Blit( s32, s32, int maxw, fheroes2::Image & sf = fheroes2::Display::instance() ) const;
82     void Blit( const fheroes2::Point &, fheroes2::Image & sf = fheroes2::Display::instance() ) const;
83 
84     static int32_t getCharacterWidth( const uint8_t character, const int fontType );
85 
86     // Use this method when you need to find the maximum width of of a string to be fit within given width
87     static int32_t getFitWidth( const std::string & text, const int fontId, const int32_t width_ );
88 
89 protected:
90     TextAscii * message;
91     u32 gw;
92     u32 gh;
93 };
94 
95 class TextSprite : protected Text
96 {
97 public:
98     TextSprite();
99     TextSprite( const std::string &, int ft, s32, s32 );
100 
101     void SetPos( s32, s32 );
102     void SetText( const std::string & );
103     void SetText( const std::string &, int );
104     void SetFont( int );
105 
106     void Show( void );
107     void Hide( void );
108 
109     bool isShow( void ) const;
110 
111     int w() const;
112     int h() const;
113 
114     fheroes2::Rect GetRect( void ) const;
115 
116 private:
117     fheroes2::ImageRestorer _restorer;
118     bool hide;
119 };
120 
121 class TextBox : protected fheroes2::Rect
122 {
123 public:
124     TextBox();
125     TextBox( const std::string &, int, uint32_t width_ );
126     TextBox( const std::string &, int, const fheroes2::Rect & );
127 
128     void Set( const std::string &, int, uint32_t width_ );
129     void SetAlign( int type );
130 
w()131     int32_t w() const
132     {
133         return fheroes2::Rect::width;
134     }
135 
h()136     int32_t h() const
137     {
138         return fheroes2::Rect::height;
139     }
140 
row()141     size_t row() const
142     {
143         return messages.size();
144     }
145 
146     void Blit( s32, s32, fheroes2::Image & sf = fheroes2::Display::instance() );
147 
148 private:
149     void Append( const std::string &, int, u32 );
150 
151     std::list<Text> messages;
152     int align;
153 };
154 
155 #endif
156