ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

unknown-game-dialog.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "gui/unknown-game-dialog.h"
00024 
00025 #include "common/translation.h"
00026 #include "common/str-array.h"
00027 #include "common/system.h"
00028 
00029 #include "gui/gui-manager.h"
00030 #include "gui/message.h"
00031 #include "gui/ThemeEval.h"
00032 #include "gui/widgets/popup.h"
00033 #include "gui/widgets/scrollcontainer.h"
00034 
00035 namespace GUI {
00036 
00037 enum {
00038     kCopyToClipboard = 'cpcl',
00039     kOpenBugtrackerURL = 'ourl',
00040     kClose = 'clse',
00041     kAddAnyway = 'adda',
00042     kScrollContainerReflow = 'SCRf'
00043 };
00044 
00045 UnknownGameDialog::UnknownGameDialog(const DetectedGame &detectedGame) :
00046         Dialog(30, 20, 260, 124),
00047         _detectedGame(detectedGame) {
00048     // For now place the buttons with a default place and size. They will be resized and moved when rebuild() is called.
00049     _closeButton = new ButtonWidget(this, 0, 0, 0, 0, detectedGame.canBeAdded ? _("Cancel") : _("Close"), 0, kClose);
00050 
00051     if (detectedGame.canBeAdded) {
00052         _addAnywayButton = new ButtonWidget(this, 0, 0, 0, 0, _("Add anyway"), 0, kAddAnyway);
00053     } else {
00054         _addAnywayButton = nullptr;
00055     }
00056 
00057     //Check if we have clipboard functionality
00058     if (g_system->hasFeature(OSystem::kFeatureClipboardSupport)) {
00059         _copyToClipboardButton = new ButtonWidget(this, 0, 0, 0, 0, _("Copy to clipboard"), 0, kCopyToClipboard);
00060     } else
00061         _copyToClipboardButton = nullptr;
00062 
00063 #if 0 // ResidualVM specific
00064     //Check if we have support for opening URLs
00065     if (g_system->hasFeature(OSystem::kFeatureOpenUrl)) {
00066         _openBugTrackerUrlButton = new ButtonWidget(this, 0, 0, 0, 0, _("Report game"), 0, kOpenBugtrackerURL);
00067     } else
00068 #endif
00069         _openBugTrackerUrlButton = nullptr;
00070 
00071     // Use a ScrollContainer for the report in case we have a lot of lines.
00072     _textContainer = new ScrollContainerWidget(this, 0, 0, 0, 0, kScrollContainerReflow);
00073     _textContainer->setTarget(this);
00074 
00075     rebuild();
00076 }
00077 
00078 void UnknownGameDialog::handleMouseWheel(int x, int y, int direction) {
00079     _textContainer->handleMouseWheel(x, y, direction);
00080 }
00081 
00082 void UnknownGameDialog::reflowLayout() {
00083     rebuild();
00084     Dialog::reflowLayout();
00085 }
00086 
00087 void UnknownGameDialog::rebuild() {
00088     // TODO: Use a theme layout dialog definition
00089 
00090     // First remove the old text widgets
00091     for (uint i = 0; i < _textWidgets.size() ; i++) {
00092         _textContainer->removeWidget(_textWidgets[i]);
00093         delete _textWidgets[i];
00094     }
00095     _textWidgets.clear();
00096 
00097     // Work out dialog size and position of the various elements in the dialog.
00098     // Limit the width of the dialog to 600 - 2 * 10 pixels.
00099     const int screenW = MIN((int)g_system->getOverlayWidth(), 600);
00100     const int screenH = g_system->getOverlayHeight();
00101 
00102     int buttonHeight = g_gui.xmlEval()->getVar("Globals.Button.Height", 0);
00103     int buttonWidth = g_gui.xmlEval()->getVar("Globals.Button.Width", 0);
00104 
00105     Common::String reportTranslated = generateUnknownGameReport(_detectedGame, true, true);
00106 
00107     // Check if we have clipboard functionality and expand the reportTranslated message if needed...
00108     if (g_system->hasFeature(OSystem::kFeatureClipboardSupport)) {
00109         reportTranslated += "\n";
00110         reportTranslated += _("Use the button below to copy the required game information into your clipboard.");
00111     }
00112 
00113 #if 0 // ResidualVM specific
00114     // Check if we have support for opening URLs and expand the reportTranslated message if needed...
00115     if (g_system->hasFeature(OSystem::kFeatureOpenUrl)) {
00116         reportTranslated += "\n";
00117         reportTranslated += _("You can also directly report your game to the Bug Tracker.");
00118     }
00119 #endif
00120 
00121     // We use a ScrollContainer to display the text, with a 2 * 8 pixels margin to the dialog border,
00122     // the scrollbar, and 2 * 10 margin for the text in the container.
00123     // We also keep 2 * 10 pixels between the screen border and the dialog.
00124     int scrollbarWidth = g_gui.xmlEval()->getVar("Globals.Scrollbar.Width", 0);
00125     Common::Array<Common::String> lines;
00126     int maxlineWidth = g_gui.getFont().wordWrapText(reportTranslated, screenW - 2 * 20 - 16 - scrollbarWidth, lines);
00127 
00128     int lineCount = lines.size() + 1;
00129 
00130     _h = MIN(screenH - 20, lineCount * kLineHeight + kLineHeight + buttonHeight + 24);
00131 
00132     int closeButtonWidth = MAX(buttonWidth, g_gui.getFont().getStringWidth(_closeButton->getLabel()) + 10);
00133     int copyToClipboardButtonWidth = 0, openBugtrackerURLButtonWidth = 0, addAnywayButtonWidth = 0;
00134     int totalButtonWidth = closeButtonWidth;
00135 
00136     if (_copyToClipboardButton) {
00137         copyToClipboardButtonWidth = MAX(buttonWidth, g_gui.getFont().getStringWidth(_copyToClipboardButton->getLabel()) + 10);
00138         totalButtonWidth += copyToClipboardButtonWidth + 10;
00139     }
00140     if (_openBugTrackerUrlButton) {
00141         openBugtrackerURLButtonWidth = MAX(buttonWidth, g_gui.getFont().getStringWidth(_openBugTrackerUrlButton->getLabel()) + 10);
00142         totalButtonWidth += openBugtrackerURLButtonWidth + 10;
00143     }
00144     if (_addAnywayButton) {
00145         addAnywayButtonWidth = MAX(buttonWidth, g_gui.getFont().getStringWidth(_addAnywayButton->getLabel()) + 10);
00146         totalButtonWidth += addAnywayButtonWidth + 10;
00147     }
00148 
00149     _w = MAX(MAX(maxlineWidth, 0) + 16 + scrollbarWidth, totalButtonWidth) + 20;
00150 
00151     // Center the dialog on the screen
00152     _x = (g_system->getOverlayWidth() - _w) / 2;
00153     _y = (g_system->getOverlayHeight() - _h) / 2;
00154 
00155     // Now move the buttons and text container to their proper place
00156     int buttonPos = _w - 10;
00157     if (_addAnywayButton) {
00158         buttonPos -= addAnywayButtonWidth + 5;
00159         _addAnywayButton->resize(buttonPos, _h - buttonHeight - 8, addAnywayButtonWidth, buttonHeight);
00160     }
00161     buttonPos -= closeButtonWidth + 5;
00162     _closeButton->resize(buttonPos, _h - buttonHeight - 8, closeButtonWidth, buttonHeight);
00163     if (_copyToClipboardButton) {
00164         buttonPos -= copyToClipboardButtonWidth + 5;
00165         _copyToClipboardButton->resize(buttonPos, _h - buttonHeight - 8, copyToClipboardButtonWidth, buttonHeight);
00166     }
00167     if (_openBugTrackerUrlButton) {
00168         buttonPos -= openBugtrackerURLButtonWidth + 5;
00169         _openBugTrackerUrlButton->resize(buttonPos, _h - buttonHeight - 8, openBugtrackerURLButtonWidth, buttonHeight);
00170     }
00171 
00172     int containerHeight = _h - kLineHeight - buttonHeight - 16;
00173     _textContainer->resize(8, 8, _w - 16, containerHeight);
00174 
00175     // And create text widgets
00176     uint y = 8;
00177     for (uint i = 0; i < lines.size() ; i++) {
00178         StaticTextWidget *widget = new StaticTextWidget(_textContainer, 10, y, _w - 36 - scrollbarWidth, kLineHeight, lines[i], Graphics::kTextAlignLeft);
00179         _textWidgets.push_back(widget);
00180         y += kLineHeight;
00181     }
00182 }
00183 
00184 Common::String UnknownGameDialog::encodeUrlString(const Common::String &string) {
00185     Common::String encoded;
00186     for (uint i = 0 ; i < string.size() ; ++i) {
00187         char c = string[i];
00188         if ((c >= 'a' && c <= 'z') || (c >= 'A'  && c <= 'Z') || (c >= '0' && c <= '9') ||
00189             c == '~' || c == '-' || c == '.' || c == '_')
00190             encoded += c;
00191         else
00192             encoded += Common::String::format("%%%02X", c);
00193     }
00194     return encoded;
00195 }
00196 
00197 Common::String UnknownGameDialog::generateBugtrackerURL() {
00198     Common::String report = generateUnknownGameReport(_detectedGame, false, false);
00199     report = encodeUrlString(report);
00200 
00201     Common::String engineName = encodeUrlString(_detectedGame.engineName);
00202 
00203     return Common::String::format(
00204         "https://www.scummvm.org/unknowngame?"
00205         "engine=%s"
00206         "&description=%s",
00207         engineName.c_str(),
00208         report.c_str());
00209 }
00210 
00211 void UnknownGameDialog::handleCommand(CommandSender *sender, uint32 cmd, uint32 data) {
00212     switch(cmd) {
00213     case kCopyToClipboard: {
00214         Common::String report = generateUnknownGameReport(_detectedGame, false, false);
00215 
00216         if (g_system->setTextInClipboard(report)) {
00217             g_system->displayMessageOnOSD(
00218                     _("All necessary information about your game has been copied into the clipboard"));
00219         } else {
00220             g_system->displayMessageOnOSD(_("Copying the game information to the clipboard has failed!"));
00221         }
00222         break;
00223     }
00224     case kClose:
00225         // The user cancelled adding the game
00226         setResult(-1);
00227         close();
00228         break;
00229     case kAddAnyway:
00230         // When the detection entry comes from the fallback detector, the game can be added / launched anyways.
00231         close();
00232         break;
00233     case kOpenBugtrackerURL:
00234         g_system->openUrl(generateBugtrackerURL());
00235         break;
00236     case kScrollContainerReflow:
00237         for (uint i = 0; i < _textWidgets.size() ; i++)
00238             _textWidgets[i]->setVisible(true);
00239         break;
00240     }
00241 }
00242 
00243 } // End of namespace GUI


Generated on Sat Dec 7 2019 05:00:44 for ResidualVM by doxygen 1.7.1
curved edge   curved edge