~legacy2345-developers/legacy2345/legacy2345

« back to all changes in this revision

Viewing changes to legacy/options_screen.cpp

  • Committer: Stephen M. Webb
  • Date: 2011-10-02 19:14:02 UTC
  • mfrom: (4.2.6)
  • Revision ID: git-v1:f302b2ce5902bf0e834f967fdcf86236759bd446
Merge branch 'release-0.1'

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 * @file legacy/options_screen.h
 
3
 * @brief Public interface for the Legacy options screen.
 
4
 */
 
5
 
 
6
/*
 
7
 * Copyright 2011 Stephen M. Webb <stephen.webb@bregmasoft.ca>
 
8
 *
 
9
 * This program is free software: you can redistribute it and/or modify
 
10
 * it under the terms of the GNU General Public License as published by
 
11
 * the Free Software Foundation, either version 3 of the License, or
 
12
 * (at your option) any later version.
 
13
 *
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 *
 
19
 * You should have received a copy of the GNU General Public License
 
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
21
 */
 
22
#include "legacy_config.h"
 
23
#include "legacy/options_screen.h"
 
24
 
 
25
#include <guichan.hpp>
 
26
#include "legacy/dialog.h"
 
27
#include "legacy/game_loop.h"
 
28
#include <memory>
 
29
 
 
30
 
 
31
namespace {
 
32
 
 
33
  static const std::string menu_audio = "Audio Settings";
 
34
  static const std::string menu_video = "Video Settings";
 
35
  static const std::string menu_back = "Back";
 
36
 
 
37
  /**
 
38
   * Menu model.
 
39
   */
 
40
  class Menu
 
41
  : public gcn::ListModel
 
42
  {
 
43
  public:
 
44
    Menu()
 
45
    {
 
46
      m_items.push_back(menu_audio);
 
47
      m_items.push_back(menu_video);
 
48
      m_items.push_back(menu_back);
 
49
    }
 
50
 
 
51
    std::string
 
52
    getElementAt(int i)
 
53
    {
 
54
      return m_items[i];
 
55
    }
 
56
 
 
57
    int
 
58
    getNumberOfElements()
 
59
    {
 
60
      return m_items.size();
 
61
    }
 
62
 
 
63
    int
 
64
    maxWidth(gcn::Font* font) const
 
65
    { 
 
66
      int maxWidth = 0;
 
67
      if (maxWidth == 0)
 
68
      {
 
69
        for (auto s = m_items.begin(); s != m_items.end(); ++s)
 
70
        {
 
71
          maxWidth = std::max(maxWidth, font->getWidth(*s));
 
72
        }
 
73
      }
 
74
      return maxWidth;
 
75
    }
 
76
 
 
77
  private:
 
78
    std::vector<std::string> m_items;
 
79
  };
 
80
} // anonymous namespace
 
81
 
 
82
 
 
83
Legacy::OptionsScreen::
 
84
OptionsScreen(GameLoop* gameLoop, Config& config LEGACY_UNUSED)
 
85
: m_gameLoop(gameLoop)
 
86
, m_menu(new gcn::ListBox(new Menu()))
 
87
{
 
88
  this->setBaseColor(gcn::Color(0x00, 0x00, 0x00, 0xff));
 
89
  this->add(m_menu.get());
 
90
 
 
91
  m_menu->setForegroundColor(gcn::Color(0xff, 0xe3, 0xab, 0xff));
 
92
  m_menu->setBackgroundColor(gcn::Color(0xff, 0xff, 0xff, 0x00));
 
93
  m_menu->setSelectionColor(gcn::Color(0x27, 0x40, 0x74, 0xff));
 
94
  m_menu->setActionEventId("selected");
 
95
  m_menu->addActionListener(this);
 
96
  m_menu->setSelected(0);
 
97
 
 
98
  addWidgetListener(this);
 
99
}
 
100
 
 
101
 
 
102
void Legacy::OptionsScreen::
 
103
action(const gcn::ActionEvent& event LEGACY_UNUSED)
 
104
{
 
105
  gcn::ListModel* model = m_menu->getListModel();
 
106
  int selected = m_menu->getSelected();
 
107
  std::string label = model->getElementAt(selected);
 
108
  if (label == menu_back)
 
109
  {
 
110
    m_gameLoop->popScreen();
 
111
  }
 
112
  else
 
113
  {
 
114
    new Dialog(this,
 
115
               "Unfinished business",
 
116
               "Sorry, this function is not yet available.");
 
117
  }
 
118
}
 
119
 
 
120
 
 
121
void Legacy::OptionsScreen::
 
122
requestFocus()
 
123
{
 
124
  m_menu->requestFocus();
 
125
}
 
126
 
 
127
 
 
128
/*
 
129
 * Menu placement is relative to the window size.
 
130
 */
 
131
void Legacy::OptionsScreen::
 
132
widgetResized(const gcn::Event &event LEGACY_UNUSED)
 
133
{
 
134
  gcn::Rectangle dimension = getDimension();
 
135
 
 
136
  Menu* listModel = static_cast<Menu*>(m_menu->getListModel());
 
137
  int menuWidth = listModel->maxWidth(getFont());
 
138
  m_menu->setPosition((dimension.width - menuWidth) / 2, dimension.height / 3);
 
139
  m_menu->setWidth(listModel->maxWidth(getFont()));
 
140
}
 
141
 
 
142
 
 
143
void Legacy::OptionsScreen::
 
144
_setParent(Widget* parent)
 
145
{
 
146
  this->setDimension(parent->getDimension());
 
147
  gcn::Container::_setParent(parent);
 
148
}
 
149
 
 
150