Finished support for theme loading.

Fixed several bugs regarding theme loading.

svn-id: r33728
This commit is contained in:
Vicent Marti 2008-08-09 22:40:05 +00:00
parent 103a4f6681
commit 185dbbe84d
9 changed files with 90 additions and 85 deletions

View file

@ -511,6 +511,8 @@ void ListWidget::reflowLayout() {
_entriesPerPage >>= 16;
assert(_entriesPerPage > 0);
delete[] _textWidth;
_textWidth = new int[_entriesPerPage];

View file

@ -377,7 +377,7 @@ public:
}
void debugDraw(Graphics::Surface *screen, const Graphics::Font *font) {
_layouts["Dialog.Launcher"]->debugDraw(screen, font);
_layouts["Dialog.Browser"]->debugDraw(screen, font);
// _layouts["Dialog.GameOptions_Graphics"]->debugDraw(screen, font);
}

View file

@ -95,7 +95,7 @@ const ThemeRenderer::TextDataInfo ThemeRenderer::kTextDataDefaults[] = {
};
ThemeRenderer::ThemeRenderer(Common::String themeName, GraphicsMode mode) :
ThemeRenderer::ThemeRenderer(Common::String fileName, GraphicsMode mode) :
_vectorRenderer(0), _system(0), _graphicsMode(kGfxDisabled),
_screen(0), _backBuffer(0), _bytesPerPixel(0), _initOk(false),
_themeOk(false), _enabled(false), _buffering(false) {
@ -120,10 +120,8 @@ ThemeRenderer::ThemeRenderer(Common::String themeName, GraphicsMode mode) :
_font = FontMan.getFontByUsage(Graphics::FontManager::kGUIFont);
}
ImageMan.addArchive(themeName + ".zip");
_themeFileName = fileName;
_initOk = true;
_themeName = themeName;
}
ThemeRenderer::~ThemeRenderer() {
@ -134,12 +132,8 @@ ThemeRenderer::~ThemeRenderer() {
delete _parser;
delete _themeEval;
for (ImagesMap::iterator i = _bitmaps.begin(); i != _bitmaps.end(); ++i) {
// delete i->_value;
for (ImagesMap::iterator i = _bitmaps.begin(); i != _bitmaps.end(); ++i)
ImageMan.unregisterSurface(i->_key);
}
ImageMan.remArchive(_stylefile + ".zip");
}
bool ThemeRenderer::init() {
@ -154,7 +148,7 @@ bool ThemeRenderer::init() {
}
if (isThemeLoadingRequired() || !_themeOk) {
loadTheme(_themeName);
loadTheme(_themeFileName);
}
return true;
@ -170,6 +164,30 @@ void ThemeRenderer::deinit() {
}
}
void ThemeRenderer::unloadTheme() {
if (!_themeOk)
return;
for (int i = 0; i < kDrawDataMAX; ++i) {
delete _widgets[i];
_widgets[i] = 0;
}
for (int i = 0; i < kTextDataMAX; ++i) {
delete _texts[i];
_texts[i] = 0;
}
for (ImagesMap::iterator i = _bitmaps.begin(); i != _bitmaps.end(); ++i)
ImageMan.unregisterSurface(i->_key);
ImageMan.remArchive(_themeFileName + ".zip");
_themeName.clear();
_themeFileName.clear();
_themeOk = false;
}
void ThemeRenderer::clearAll() {
if (!_initOk)
return;
@ -281,7 +299,6 @@ bool ThemeRenderer::addFont(const Common::String &fontId, const Common::String &
bool ThemeRenderer::addBitmap(const Common::String &filename) {
if (_bitmaps.contains(filename)) {
delete _bitmaps[filename];
ImageMan.unregisterSurface(filename);
}
@ -309,14 +326,28 @@ bool ThemeRenderer::addDrawData(const Common::String &data, bool cached) {
return true;
}
bool ThemeRenderer::loadTheme(Common::String themeName) {
bool ThemeRenderer::loadTheme(Common::String fileName) {
unloadTheme();
if (themeName == "builtin" && !loadDefaultXML())
error("Could not load default embeded theme.");
if (fileName != "builtin") {
if (ConfMan.hasKey("themepath"))
Common::File::addDefaultDirectory(ConfMan.get("themepath"));
if (!loadThemeXML(themeName)) {
warning("Could not parse custom theme '%s'.\nFalling back to default theme", themeName.c_str());
#ifdef DATA_PATH
Common::File::addDefaultDirectoryRecursive(DATA_PATH);
#endif
if (ConfMan.hasKey("extrapath"))
Common::File::addDefaultDirectoryRecursive(ConfMan.get("extrapath"));
ImageMan.addArchive(fileName + ".zip");
}
if (fileName == "builtin") {
if (!loadDefaultXML())
error("Could not load default embeded theme");
}
else if (!loadThemeXML(fileName)) {
warning("Could not parse custom theme '%s'.\nFalling back to default theme", fileName.c_str());
if (!loadDefaultXML()) // if we can't load the embeded theme, this is a complete failure
error("Could not load default embeded theme");
@ -333,9 +364,7 @@ bool ThemeRenderer::loadTheme(Common::String themeName) {
}
}
// Debug print all the parsed variables. remove
_themeEval->debugPrint();
_themeName = "DEBUG - A Theme name";
_themeOk = true;
return true;
}
@ -346,6 +375,8 @@ bool ThemeRenderer::loadDefaultXML() {
// file inside the themes directory.
// Use the Python script "makedeftheme.py" to convert a normal XML theme
// into the "default.inc" file, which is ready to be included in the code.
#ifdef GUI_ENABLE_BUILTIN_THEME
const char *defaultXML =
#include "themes/default.inc"
;
@ -354,21 +385,15 @@ bool ThemeRenderer::loadDefaultXML() {
return false;
return parser()->parse();
#else
warning("The built-in theme is not enabled in the current build. Please load an external theme");
return false;
#endif
}
bool ThemeRenderer::loadThemeXML(Common::String themeName) {
assert(_parser);
if (ConfMan.hasKey("themepath"))
Common::File::addDefaultDirectory(ConfMan.get("themepath"));
#ifdef DATA_PATH
Common::File::addDefaultDirectoryRecursive(DATA_PATH);
#endif
if (ConfMan.hasKey("extrapath"))
Common::File::addDefaultDirectoryRecursive(ConfMan.get("extrapath"));
if (!parser()->loadFile(themeName + ".stx")){
#ifdef USE_ZLIB
unzFile zipFile = unzOpen((themeName + ".zip").c_str());
@ -821,9 +846,9 @@ void ThemeRenderer::updateScreen() {
renderDirtyScreen();
// _vectorRenderer->fillSurface();
// themeEval()->debugDraw(_screen, _font);
// _vectorRenderer->copyWholeFrame(_system);
// _vectorRenderer->fillSurface();
// themeEval()->debugDraw(_screen, _font);
// _vectorRenderer->copyWholeFrame(_system);
}
void ThemeRenderer::renderDirtyScreen() {

View file

@ -216,7 +216,7 @@ public:
};
/** Default constructor */
ThemeRenderer(Common::String themeName, GraphicsMode mode);
ThemeRenderer(Common::String fileName, GraphicsMode mode);
/** Default destructor */
~ThemeRenderer();
@ -471,10 +471,11 @@ public:
return 0;
}
const Common::String &getThemeName() { return _themeName; }
protected:
const Common::String &getThemeName() const { return _themeName; }
const Common::String &getThemeFileName() const { return _themeFileName; }
/**
* Initializes the drawing screen surfaces, _screen and _backBuffer.
* If the surfaces already exist, they are cleared and re-initialized.
@ -505,22 +506,7 @@ protected:
* Unloads the currently loaded theme so another one can
* be loaded.
*/
void unloadTheme() {
if (!_themeOk)
return;
for (int i = 0; i < kDrawDataMAX; ++i) {
delete _widgets[i];
_widgets[i] = 0;
}
for (int i = 0; i < kTextDataMAX; ++i) {
delete _texts[i];
_texts[i] = 0;
}
_themeOk = false;
}
void unloadTheme();
/**
* Not implemented yet.
@ -710,6 +696,7 @@ protected:
bool _enabled; /** Whether the Theme is currently shown on the overlay */
Common::String _themeName; /** Name of the currently loaded theme */
Common::String _themeFileName;
};
} // end of namespace GUI.

View file

@ -98,7 +98,7 @@ NewGui::NewGui() : _redrawStatus(kRedrawDisabled),
style = "builtin";
//DEBUG:
style = "scummodern";
// style = "scummodern";
loadNewTheme(style);
@ -110,11 +110,8 @@ NewGui::~NewGui() {
delete _theme;
}
bool NewGui::loadNewTheme(const Common::String &style) {
Common::String styleType;
Common::ConfigFile cfg;
Common::String oldTheme = (_theme != 0) ? _theme->getThemeName() : "";
bool NewGui::loadNewTheme(const Common::String &filename) {
Common::String oldTheme = (_theme != 0) ? _theme->getThemeFileName() : "";
if (_theme)
_theme->disable();
@ -127,7 +124,7 @@ bool NewGui::loadNewTheme(const Common::String &style) {
delete _theme;
_theme = 0;
_theme = new ThemeRenderer(style, GUI::ThemeRenderer::kGfxAntialias16bit);
_theme = new ThemeRenderer(filename, GUI::ThemeRenderer::kGfxAntialias16bit);
if (!_theme)
return (!oldTheme.empty() ? loadNewTheme(oldTheme) : false);

View file

@ -907,7 +907,7 @@ void GlobalOptionsDialog::handleCommand(CommandSender *sender, uint32 cmd, uint3
if (browser.runModal() > 0) {
// User made his choice...
const Common::String &theme = browser.selected();
if (0 != theme.compareToIgnoreCase(g_gui.theme()->getStylefileName()))
if (0 != theme.compareToIgnoreCase(g_gui.theme()->getThemeFileName()))
if (g_gui.loadNewTheme(theme)) {
_curTheme->setLabel(g_gui.theme()->getThemeName());
ConfMan.set("gui_theme", theme);

View file

@ -370,12 +370,8 @@ public:
static bool themeConfigUseable(const Common::String &file, const Common::String &style="", Common::String *cStyle=0, Common::ConfigFile *cfg=0);
const Common::String &getStylefileName() const { return _stylefile; }
virtual const Common::String &getThemeName() const { return _stylename; }
virtual bool isDynamic() {
return false;
}
virtual const Common::String &getThemeFileName() const = 0;
virtual const Common::String &getThemeName() const = 0;
/**
* Checks if the theme renderer supports drawing of images.

View file

@ -43,21 +43,21 @@ enum {
// but for now this simple browser works,
// also it will get its own theme config values
// and not use 'browser_' anymore
ThemeBrowser::ThemeBrowser() : Dialog("browser") {
ThemeBrowser::ThemeBrowser() : Dialog("Browser") {
_fileList = 0;
new StaticTextWidget(this, "browser_headline", "Select a Theme");
new StaticTextWidget(this, "Browser.Headline", "Select a Theme");
// Add file list
_fileList = new ListWidget(this, "browser_list");
_fileList = new ListWidget(this, "Browser.List");
_fileList->setNumberingMode(kListNumberingOff);
_fileList->setEditable(false);
_fileList->setHints(THEME_HINT_PLAIN_COLOR);
// Buttons
new ButtonWidget(this, "browser_cancel", "Cancel", kCloseCmd, 0);
new ButtonWidget(this, "browser_choose", "Choose", kChooseCmd, 0);
new ButtonWidget(this, "Browser.Cancel", "Cancel", kCloseCmd, 0);
new ButtonWidget(this, "Browser.Choose", "Choose", kChooseCmd, 0);
}
void ThemeBrowser::open() {
@ -91,9 +91,8 @@ void ThemeBrowser::updateListing() {
// classic is always build in
Entry th;
th.name = "Classic (Builtin)";
th.type = "Classic";
th.file = "Classic (Builtin)";
th.name = "Modern Development Theme (Builtin) - WIP";
th.file = "builtin";
_themes.push_back(th);
// we are using only the paths 'themepath', 'extrapath', DATA_PATH and '.'
@ -172,10 +171,11 @@ void ThemeBrowser::addDir(ThList &list, const Common::String &dir, int level) {
}
bool ThemeBrowser::isTheme(const FilesystemNode &node, Entry &out) {
Common::ConfigFile cfg;
Common::String type;
out.file = node.getName();
if (!out.file.hasSuffix(".zip") && !out.file.hasSuffix(".stx"))
return false;
for (int i = out.file.size()-1; out.file[i] != '.' && i > 0; --i) {
out.file.deleteLastChar();
}
@ -184,14 +184,13 @@ bool ThemeBrowser::isTheme(const FilesystemNode &node, Entry &out) {
if (out.file.empty())
return false;
if (!Theme::themeConfigUseable(out.file, "", &type, &cfg))
return false;
// TODO: Check if theme is usable.
// if (!Theme::themeConfigUseable(out.file, "", &type, &cfg))
// return false;
out.type = type;
if (cfg.hasKey("name", "theme"))
cfg.getKey("name", "theme", out.name);
else
// if (cfg.hasKey("name", "theme"))
// cfg.getKey("name", "theme", out.name);
// else
out.name = out.file;
return true;

View file

@ -46,7 +46,6 @@ public:
private:
struct Entry {
Common::String name;
Common::String type;
Common::String file;
};