summaryrefslogtreecommitdiff
path: root/src/gui/setup_video.cpp
blob: 143f86edc74442532bf7aa43fb97da685bb68cf3 (plain) (blame)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 *  The Mana World
 *  Copyright 2004 The Mana World Development Team
 *
 *  This file is part of The Mana World.
 *
 *  The Mana World 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
 *  any later version.
 *
 *  The Mana World 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 The Mana World; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *  $Id$
 */

#include "setup_video.h"

#include <sstream>
#include <string>
#include <vector>
#include <SDL.h>

#include <guichan/listmodel.hpp>

#include <guichan/widgets/label.hpp>

#include "checkbox.h"
#include "listbox.h"
#include "ok_dialog.h"
#include "scrollarea.h"
#include "slider.h"

#include "../configuration.h"
#include "../graphics.h"
#include "../log.h"

extern Graphics *graphics;

/**
 * The list model for mode list.
 *
 * \ingroup Interface
 */
class ModeListModel : public gcn::ListModel
{
    public:
        /**
         * Constructor.
         */
        ModeListModel();

        /**
         * Destructor.
         */
        virtual ~ModeListModel() { }

        /**
         * Returns the number of elements in container.
         */
        int getNumberOfElements() { return mVideoModes.size(); }

        /**
         * Returns element from container.
         */
        std::string getElementAt(int i) { return mVideoModes[i]; }

    private:
        std::vector<std::string> mVideoModes;
};

ModeListModel::ModeListModel()
{
    /* Get available fullscreen/hardware modes */
    SDL_Rect **modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE);

    /* Check which modes are available */
    if (modes == (SDL_Rect **)0) {
        logger->log("No modes available");
    } else if (modes == (SDL_Rect **)-1) {
        logger->log("All resolutions available");
    } else {
        //logger->log("Available Modes");
        for (int i = 0; modes[i]; ++i) {
            std::stringstream ss;
            ss << (int)modes[i]->w << "x" << (int)modes[i]->h;
            //logger->log(ss.str().c_str());
            mVideoModes.push_back(ss.str());
        }
    }
}

Setup_Video::Setup_Video():
    mFullScreenEnabled(config.getValue("screen", 0)),
    mOpenGLEnabled(config.getValue("opengl", 0)),
    mCustomCursorEnabled(config.getValue("customcursor", 1)),
    mOpacity(config.getValue("guialpha", 0.8)),
    mModeListModel(new ModeListModel()),
    mModeList(new ListBox(mModeListModel)),
    mFsCheckBox(new CheckBox("Full screen", mFullScreenEnabled)),
    mOpenGLCheckBox(new CheckBox("OpenGL", mOpenGLEnabled)),
    mCustomCursorCheckBox(new CheckBox("Custom cursor", mCustomCursorEnabled)),
    mAlphaSlider(new Slider(0.2, 1.0))
{
    setOpaque(false);

    ScrollArea *scrollArea = new ScrollArea(mModeList);
    gcn::Label *alphaLabel = new gcn::Label("Gui opacity");

    mModeList->setEnabled(false);
#ifndef USE_OPENGL
    mOpenGLCheckBox->setEnabled(false);
#endif

    mModeList->setDimension(gcn::Rectangle(0, 0, 60, 50));
    scrollArea->setDimension(gcn::Rectangle(10, 10, 90, 50));
    mFsCheckBox->setPosition(110, 10);
    mOpenGLCheckBox->setPosition(110, 30);
    mCustomCursorCheckBox->setPosition(110, 50);
    mAlphaSlider->setDimension(gcn::Rectangle(10, 80, 100, 10));
    alphaLabel->setPosition(20 + mAlphaSlider->getWidth(), mAlphaSlider->getY());

    mModeList->setSelected(-1);
    mAlphaSlider->setValue(mOpacity);

    mCustomCursorCheckBox->setEventId("customcursor");
    mAlphaSlider->setEventId("guialpha");

    mCustomCursorCheckBox->addActionListener(this);
    mAlphaSlider->addActionListener(this);

    add(scrollArea);
    add(mFsCheckBox);
    add(mOpenGLCheckBox);
    add(mCustomCursorCheckBox);
    add(mAlphaSlider);
    add(alphaLabel);
}

Setup_Video::~Setup_Video()
{
    delete mModeListModel;
}

void Setup_Video::apply()
{
    // Full screen changes
    bool fullscreen = mFsCheckBox->isMarked();
    if (fullscreen != (config.getValue("screen", 0) == 1))
    {
        // checks for opengl usage
        if (!(config.getValue("opengl", 0) == 1))
        {
            if (!graphics->setFullscreen(fullscreen))
            {
                fullscreen = !fullscreen;
                if (!graphics->setFullscreen(fullscreen))
                {
                    std::stringstream error;
                    error << "Failed to switch to " <<
                        (fullscreen ? "windowed" : "fullscreen") <<
                        "mode and restoration of old mode also failed!" <<
                        std::endl;
                    logger->error(error.str());
                }
            }
        } else {
            new OkDialog("Switching to FullScreen",
                    "Restart needed for changes to take effect.");
        }
        config.setValue("screen", fullscreen ? 1 : 0);
    }

    // OpenGL change
    if (mOpenGLCheckBox->isMarked() != mOpenGLEnabled)
    {
        config.setValue("opengl", mOpenGLCheckBox->isMarked() ? 1 : 0);

        // OpenGL can currently only be changed by restarting, notify user.
        new OkDialog("Changing OpenGL",
                "Applying change to OpenGL requires restart.");
    }

    // We sync old and new values at apply time
    mFullScreenEnabled = config.getValue("screen", 0);
    mCustomCursorEnabled = config.getValue("customcursor", 1);
    mOpacity = config.getValue("guialpha", 0.8);
    mOpenGLEnabled = config.getValue("opengl", 0);
}

void Setup_Video::cancel()
{
    mFsCheckBox->setMarked(mFullScreenEnabled);
    mOpenGLCheckBox->setMarked(mOpenGLEnabled);
    mCustomCursorCheckBox->setMarked(mCustomCursorEnabled);
    mAlphaSlider->setValue(mOpacity);

    config.setValue("screen", mFullScreenEnabled ? 1 : 0);
    config.setValue("customcursor", mCustomCursorEnabled ? 1 : 0);
    config.setValue("guialpha", mOpacity);
    config.setValue("opengl", mOpenGLEnabled ? 1 : 0);
}

void Setup_Video::action(const std::string &event)
{
    if (event == "guialpha")
    {
        config.setValue("guialpha", mAlphaSlider->getValue());
    }
    else if (event == "customcursor")
    {
        config.setValue("customcursor",
                mCustomCursorCheckBox->isMarked() ? 1 : 0);
    }
}