File: credits_menu.cpp

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 (165 lines) | stat: -rw-r--r-- 5,611 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/******************************************************************************
 *  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
 ******************************************************************************
 * Credits Menu
 *****************************************************************************/

#include "menu/credits_menu.h"
//-----------------------------------------------------------------------------
#include <sstream>
#include <iostream>
#include <algorithm>  //std::transform
#include "game/config.h"
#include "graphic/video.h"
#include "gui/label.h"
#include "gui/scroll_box.h"
#include "include/app.h"
#include "tool/xml_document.h"
//-----------------------------------------------------------------------------

class Author
{
public:
  std::string name;
  std::string nickname;
  std::string email;
  std::string country;
  std::string description;

  bool Feed (const xmlNode* node);
  std::string PrettyString(bool with_email) const;
};

//-----------------------------------------------------------------------------

bool Author::Feed (const xmlNode* node)
{
  if (!XmlReader::ReadString(node, "name", name))
    return false;
  if (!XmlReader::ReadString(node, "description", description))
    return false;
  XmlReader::ReadString(node, "nickname", nickname);
  XmlReader::ReadString(node, "email", email);
  XmlReader::ReadString(node, "country", country);
  return true;
}

//-----------------------------------------------------------------------------

std::string Author::PrettyString(bool with_email) const
{
  std::ostringstream ss;
  ss << "* " << name;
  if (with_email) {
    ss << " <" << email << ">";
  }
  if (!nickname.empty()) {
    ss << " " << _("aka") << " " << nickname;
  }
  if (!country.empty()) {
    ss << " " << _("from") << " " << country;
  }
  ss << ": " << description;
  return ss.str();
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------

CreditsMenu::CreditsMenu()  :
  Menu("credit/background", vOk)
{
  const Surface& window = GetMainWindow();
  lbox_authors = new ScrollBox(window.GetSize()*0.9f - Point2i(0, 30));
  lbox_authors->SetBackgroundColor(Color(0, 200));
  lbox_authors->SetPosition(window.GetSize()*0.05f);

  PrepareAuthorsList();

  widgets.AddWidget(lbox_authors);
  widgets.Pack();
  widgets.SetFocusOn(lbox_authors);
}

void CreditsMenu::AddItem(const std::string & label,
                          Font::font_size_t fsize,
                          Font::font_style_t fstyle,
                          const Color & color)
{
  lbox_authors->AddWidget(new Label(label, lbox_authors->GetSizeX() - 10,
                                    fsize, fstyle, color));
}

void CreditsMenu::PrepareAuthorsList()
{
  std::string filename = Config::GetInstance()->GetDataDir() + "authors.xml";
  XmlReader doc;
  if (!doc.Load(filename)) {
    // Error: do something ...
    return;
  }
  // Use an array for this is the best solution I think, but there is perhaps a better code...
  static const std::string teams[] = { "team", "contributors", "thanks" };

  for (uint i = 0; i < (sizeof teams / sizeof* teams); ++i) {
    xmlNodeArray team = XmlReader::GetNamedChildren(doc.GetRoot(), teams[i]);

    if (team.empty())
      continue;

    std::string team_title = teams[i];
    std::transform(team_title.begin(), team_title.end(), team_title.begin(),
                   static_cast<int (*)(int)>(toupper) );

    // I think this is ugly, but someone can use a better presentation
    std::cout << "       ===[ " << team_title << " ]===" << std::endl << std::endl;

    AddItem(" ", Font::FONT_BIG, Font::FONT_BOLD);
    AddItem(team_title,  Font::FONT_BIG, Font::FONT_BOLD, c_red);

    // We think there is ONLY ONE occurence of team section, so we use the first
    xmlNodeArray sections = XmlReader::GetNamedChildren(team.front(), "section");
    xmlNodeArray::const_iterator section     = sections.begin(),
                                 end_section = sections.end();

    for (; section != end_section; ++section) {
      xmlNodeArray authors = XmlReader::GetNamedChildren(*section, "author");
      xmlNodeArray::const_iterator node = authors.begin(),
                                   end  = authors.end();
      std::string title;

      if (!XmlReader::ReadStringAttr(*section, "title", title))
        continue;

      std::cout << "== " << title << " ==" << std::endl;

      AddItem(" ");
      AddItem("   "+title, Font::FONT_MEDIUM, Font::FONT_BOLD, c_yellow);

      for (; node != end; ++node) {
        Author author;
        if (author.Feed(*node)) {
          std::cout << author.PrettyString(false) << std::endl;
          AddItem(author.PrettyString(false));
        }
      }
      std::cout << std::endl;
      AddItem("");
    }
  }
}