File: text.h

package info (click to toggle)
warmux 1%3A11.04.1%2Brepack2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid
  • size: 126,388 kB
  • sloc: cpp: 186,040; xml: 8,909; sh: 3,358; makefile: 1,052; ansic: 713
file content (146 lines) | stat: -rw-r--r-- 4,652 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/******************************************************************************
 *  Warmux is a convivial mass murder game.
 *  Copyright (C) 2001-2011 Warmux Team.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
 ******************************************************************************/

#ifndef TEXT_H
#define TEXT_H

#include <string>
#include "colors.h"
#include "font.h"
#include "surface.h"

// Forward declaration
class XmlReader;
typedef struct _xmlNode xmlNode;

class Text
{
public:
  typedef enum
  {
    ALIGN_CENTER,
    ALIGN_LEFT_TOP,
    ALIGN_RIGHT_TOP,
    ALIGN_LEFT_CENTER,
    ALIGN_RIGHT_CENTER,
    ALIGN_CENTER_TOP,
    ALIGN_CENTER_BOTTOM
  } Alignment;

protected:
  Surface surf;
  Surface background; //shadow or outline or nothing;
  std::string txt;
  Color color;
  bool shadowed;
  bool dummy;
  uint bg_offset;
  uint max_width;
  Color shadowColor;
  Font::font_size_t font_size;
  Font::font_style_t font_style;
  int offset;

  virtual void Render();
  static int GetLineHeight(const Font *f) { return (7*f->GetHeight())>>3; }
  void RenderMultiLines();

public:
  Text(const std::string & text,
       const Color & fontColor = white_color,
       uint fontSize = (uint)Font::FONT_SMALL,
       Font::font_style_t fontStyle = Font::FONT_BOLD,
       bool shadowed = true,
       const Color & shadowColor = black_color,
       bool dummy = false);
  Text(void);

  virtual ~Text() { }

  void Init();
  void LoadXMLConfiguration(XmlReader * xmlFile,
                            const xmlNode * textNode);
  Font::font_style_t DetectFontStyle(const std::string & fontStyle);

  //Draw method using windows coordinates
  void DrawCenter(const Point2i & position) const { DrawLeftTop(position - surf.GetSize() / 2); }
  void DrawLeftTop(const Point2i & position) const;
  void DrawRightTop(const Point2i & position) const { DrawLeftTop(position - Point2i(surf.GetWidth(), 0)); }
  void DrawCenterTop(const Point2i & position) const { DrawLeftTop(position - Point2i(surf.GetWidth()/2, 0)); }
  void DrawLeftCenter(const Point2i & position) const { DrawLeftTop(position - Point2i(0, surf.GetHeight()/2)); }
  void DrawRightCenter(const Point2i & position) const { DrawLeftTop(position - Point2i(surf.GetWidth(), surf.GetHeight()/2)); }
  void DrawCenterBottom(const Point2i & position) const { DrawLeftTop(position - surf.GetSize()); }

  //Draw text cursor only (text_pos = position for DrawTopLeft)
  void DrawCursor(const Point2i & text_pos, std::string::size_type cursor_pos, int real_width);

  //Draw method using map coordinates
  void DrawCenterTopOnMap(const Point2i & position) const;

  void SetText(const std::string & new_txt)
  {
    if (txt == new_txt)
      return;

    txt = new_txt;

    Render();
  }
  const std::string & GetText() const { return txt; }
  void SetColor(const Color & new_color)
  {
    if (color == new_color)
      return;

    color = new_color;

    Render();
  }
  void SetMaxWidth(uint max_w)
  {
    if (max_width == max_w)
      return;

    max_width = max_w;

    Render();
  }
  int GetWidth() const { return (txt=="" && !dummy) ? 0 : surf.GetWidth(); }
  int GetHeight() const;

  const Color & GetFontColor() const { return color; };
  Font::font_size_t GetFontSize() const { return font_size; };
  Font::font_style_t GetFontStyle() const { return font_style; };
  bool IsFontShadowed() const { return shadowed; };
  const Color & GetShadowColor(void) const { return this->shadowColor; };

  void SetFont(const Color & font_color,
               const Font::font_size_t font_size,
               const Font::font_style_t font_style,
               bool font_shadowed,
               const Color & shadowColor = black_color);
};

void DrawTmpBoxText(Font& font,
                    Point2i pos,
                    const std::string& txt, uint space=3,
                    const Color& boxColor = defaultColorBox,
                    const Color& rectColor = defaultColorRect);

#endif