Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
STARK: First rough implementation of the SaveDataWidget
  • Loading branch information
DouglasLiuDev committed May 29, 2018
1 parent 340b55f commit 5559fee
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
69 changes: 69 additions & 0 deletions engines/stark/ui/menu/saveloadmenu.cpp
Expand Up @@ -23,6 +23,19 @@
#include "engines/stark/ui/menu/saveloadmenu.h"
#include "engines/stark/services/services.h"
#include "engines/stark/services/userinterface.h"
#include "engines/stark/services/stateprovider.h"

#include "engines/stark/gfx/driver.h"
#include "engines/stark/gfx/texture.h"
#include "engines/stark/gfx/surfacerenderer.h"

#include "engines/stark/stark.h"
#include "engines/stark/savemetadata.h"

#include "common/config-manager.h"
#include "common/savefile.h"

#include "gui/message.h"

namespace Stark {

Expand Down Expand Up @@ -77,12 +90,21 @@ void SaveLoadMenuScreen::open() {
_widgets.back()->setupSounds(0, 1);
_widgets.back()->setTextColor(_textColorBlack);

_widgets.push_back(new SaveDataWidget(
0, _gfx, this));
}

void SaveLoadMenuScreen::backHandler() {
StarkUserInterface->backPrevScreen();
}

void SaveLoadMenuScreen::checkError(Common::Error error) {
if (error.getCode() != Common::kNoError) {
GUI::MessageDialog dialog(error.getDesc());
dialog.runModal();
}
}

void SaveMenuScreen::open() {
SaveLoadMenuScreen::open();
_widgets[kWidgetLoadText]->setVisible(false);
Expand All @@ -93,4 +115,51 @@ void LoadMenuScreen::open() {
_widgets[kWidgetSaveText]->setVisible(false);
}

SaveDataWidget::SaveDataWidget(int slot, Gfx::Driver *gfx, SaveLoadMenuScreen *screen) :
StaticLocationWidget(nullptr, nullptr, nullptr),
_slot(slot),
_screen(screen) {
_texture = gfx->createTexture();
_surfaceRenderer = gfx->createSurfaceRenderer();

Common::String filename = StarkEngine::formatSaveName(ConfMan.getActiveDomainName().c_str(), _slot);
Common::InSaveFile *save = g_system->getSavefileManager()->openForLoading(filename);

if (save) {
// Obtain the thumbnail
SaveMetadata metadata;
StateReadStream stream(save);
Common::ErrorCode metadataErrorCode = metadata.read(&stream, filename);
if (metadataErrorCode != Common::kNoError) {
error("Unable to read save metadata with error code %d.", metadataErrorCode);
}

Graphics::Surface *thumb = metadata.readGameScreenThumbnail(&stream);
_texture->update(thumb);
}
}

SaveDataWidget::~SaveDataWidget() {
delete _texture;
delete _surfaceRenderer;
}

void SaveDataWidget::render() {
_surfaceRenderer->render(_texture, Common::Point(200, 200));
}

bool SaveDataWidget::isMouseInside(const Common::Point &mousePos) const {
return mousePos.x >= 200 && mousePos.x <= 200 + _texture->width() &&
mousePos.y >= 200 && mousePos.y <= 200 + _texture->height();
}

void SaveDataWidget::onClick() {
StaticLocationWidget::onClick();
_screen->clickHandler(_slot);
}

void SaveDataWidget::onMouseMove(const Common::Point &mousePos) {
StaticLocationWidget::onMouseMove(mousePos);
}

} // End of namespace Stark
48 changes: 48 additions & 0 deletions engines/stark/ui/menu/saveloadmenu.h
Expand Up @@ -25,8 +25,17 @@

#include "engines/stark/ui/menu/locationscreen.h"

#include "common/error.h"
#include "engines/engine.h"

namespace Stark {

namespace Gfx {
class Texture;
class SurfaceRenderer;
}


/**
* The base class of the save and load menu of the game
*/
Expand All @@ -38,12 +47,20 @@ class SaveLoadMenuScreen : public StaticLocationScreen {
// StaticLocationScreen API
void open() override;

void clickHandler(int slot) {
clickHandlerImpl(slot);
}

protected:
static void checkError(Common::Error error);

enum WidgetIndex {
kWidgetSaveText = 3,
kWidgetLoadText = 4,
};

virtual void clickHandlerImpl(int slot) = 0;

private:
static const uint32 _textColorBlack = 0xFF000000;

Expand All @@ -61,6 +78,11 @@ class SaveMenuScreen : public SaveLoadMenuScreen {

// SaveLoadMenuScreen API
void open() override;

protected:
void clickHandlerImpl(int slot) override {
checkError(g_engine->saveGameState(slot, "TestSave"));
}
};

/**
Expand All @@ -74,6 +96,32 @@ class LoadMenuScreen : public SaveLoadMenuScreen {

// SaveLoadMenuScreen API
void open() override;

protected:
void clickHandlerImpl(int slot) override {
checkError(g_engine->loadGameState(slot));
}
};

/**
* The widget of save data
*/
class SaveDataWidget : public StaticLocationWidget {
public:
SaveDataWidget(int slot, Gfx::Driver *gfx, SaveLoadMenuScreen *screen);
~SaveDataWidget();

// StaticLocationWidget API
void render() override;
bool isMouseInside(const Common::Point &mousePos) const override;
void onClick() override;
void onMouseMove(const Common::Point &mousePos) override;

private:
int _slot;
SaveLoadMenuScreen *_screen;
Gfx::Texture *_texture;
Gfx::SurfaceRenderer *_surfaceRenderer;
};

} // End of namespace Stark
Expand Down

0 comments on commit 5559fee

Please sign in to comment.