Skip to content

Commit

Permalink
Merge pull request #9535 from unknownbrackets/ui-background2
Browse files Browse the repository at this point in the history
UI: Allow setting the background image
  • Loading branch information
hrydgard committed Apr 2, 2017
2 parents 97058b4 + 6541413 commit 4ea01be
Show file tree
Hide file tree
Showing 14 changed files with 198 additions and 36 deletions.
3 changes: 0 additions & 3 deletions UI/EmuScreen.cpp
Expand Up @@ -77,9 +77,6 @@
#if !PPSSPP_PLATFORM(UWP)
#include "gfx/gl_common.h"
#endif
#if !PPSSPP_PLATFORM(UWP)
#include "gfx/gl_common.h"
#endif

#ifndef MOBILE_DEVICE
AVIDump avi;
Expand Down
8 changes: 5 additions & 3 deletions UI/GameInfoCache.cpp
Expand Up @@ -673,7 +673,7 @@ void GameInfoCache::FlushBGs() {
iter->second->sndFileData.clear();
iter->second->sndDataLoaded = false;
}
iter->second->wantFlags &= ~(GAMEINFO_WANTBG | GAMEINFO_WANTSND);
iter->second->wantFlags &= ~(GAMEINFO_WANTBG | GAMEINFO_WANTSND | GAMEINFO_WANTBGDATA);
}
}

Expand Down Expand Up @@ -758,7 +758,9 @@ void GameInfoCache::SetupTexture(GameInfo *info, Draw::DrawContext *thin3d, Game
icon.timeLoaded = time_now_d();
}
}
icon.data.clear();
icon.dataLoaded = false;
if ((info->wantFlags & GAMEINFO_WANTBGDATA) == 0) {
icon.data.clear();
icon.dataLoaded = false;
}
}
}
1 change: 1 addition & 0 deletions UI/GameInfoCache.h
Expand Up @@ -53,6 +53,7 @@ enum GameInfoWantFlags {
GAMEINFO_WANTBG = 0x01,
GAMEINFO_WANTSIZE = 0x02,
GAMEINFO_WANTSND = 0x04,
GAMEINFO_WANTBGDATA = 0x08, // Use with WANTBG.
};

class FileLoader;
Expand Down
123 changes: 99 additions & 24 deletions UI/GameScreen.cpp
Expand Up @@ -27,6 +27,10 @@
#include "ui/ui_context.h"
#include "ui/view.h"
#include "ui/viewgroup.h"
#include "Common/FileUtil.h"
#include "Core/Host.h"
#include "Core/Config.h"
#include "Core/System.h"
#include "UI/CwCheatScreen.h"
#include "UI/EmuScreen.h"
#include "UI/GameScreen.h"
Expand All @@ -36,9 +40,6 @@
#include "UI/MainScreen.h"
#include "UI/BackgroundAudio.h"

#include "Core/Host.h"
#include "Core/Config.h"

GameScreen::GameScreen(const std::string &gamePath) : UIDialogScreenWithGameBackground(gamePath) {
SetBackgroundAudioGame(gamePath);
}
Expand Down Expand Up @@ -107,27 +108,20 @@ void GameScreen::CreateViews() {

rightColumnItems->Add(new Choice(ga->T("Play")))->OnClick.Handle(this, &GameScreen::OnPlay);

if (info) {
btnGameSettings_ = rightColumnItems->Add(new Choice(ga->T("Game Settings")));
btnGameSettings_->OnClick.Handle(this, &GameScreen::OnGameSettings);
btnDeleteGameConfig_ = rightColumnItems->Add(new Choice(ga->T("Delete Game Config")));
btnDeleteGameConfig_->OnClick.Handle(this, &GameScreen::OnDeleteConfig);
btnCreateGameConfig_ = rightColumnItems->Add(new Choice(ga->T("Create Game Config")));
btnCreateGameConfig_->OnClick.Handle(this, &GameScreen::OnCreateConfig);

btnGameSettings_->SetVisibility(V_GONE);
btnDeleteGameConfig_->SetVisibility(V_GONE);
btnCreateGameConfig_->SetVisibility(V_GONE);

btnDeleteSaveData_ = new Choice(ga->T("Delete Save Data"));
rightColumnItems->Add(btnDeleteSaveData_)->OnClick.Handle(this, &GameScreen::OnDeleteSaveData);
btnDeleteSaveData_->SetVisibility(V_GONE);
} else {
btnGameSettings_ = nullptr;
btnCreateGameConfig_ = nullptr;
btnDeleteGameConfig_ = nullptr;
btnDeleteSaveData_ = nullptr;
}
btnGameSettings_ = rightColumnItems->Add(new Choice(ga->T("Game Settings")));
btnGameSettings_->OnClick.Handle(this, &GameScreen::OnGameSettings);
btnDeleteGameConfig_ = rightColumnItems->Add(new Choice(ga->T("Delete Game Config")));
btnDeleteGameConfig_->OnClick.Handle(this, &GameScreen::OnDeleteConfig);
btnCreateGameConfig_ = rightColumnItems->Add(new Choice(ga->T("Create Game Config")));
btnCreateGameConfig_->OnClick.Handle(this, &GameScreen::OnCreateConfig);

btnGameSettings_->SetVisibility(V_GONE);
btnDeleteGameConfig_->SetVisibility(V_GONE);
btnCreateGameConfig_->SetVisibility(V_GONE);

btnDeleteSaveData_ = new Choice(ga->T("Delete Save Data"));
rightColumnItems->Add(btnDeleteSaveData_)->OnClick.Handle(this, &GameScreen::OnDeleteSaveData);
btnDeleteSaveData_->SetVisibility(V_GONE);

if (info && !info->IsPending()) {
otherChoices_.clear();
Expand All @@ -146,6 +140,10 @@ void GameScreen::CreateViews() {
if (g_Config.bEnableCheats) {
rightColumnItems->Add(AddOtherChoice(new Choice(pa->T("Cheats"))))->OnClick.Handle(this, &GameScreen::OnCwCheat);
}

btnSetBackground_ = rightColumnItems->Add(new Choice(ga->T("Use UI background")));
btnSetBackground_->OnClick.Handle(this, &GameScreen::OnSetBackground);
btnSetBackground_->SetVisibility(V_GONE);
}

UI::Choice *GameScreen::AddOtherChoice(UI::Choice *choice) {
Expand Down Expand Up @@ -248,6 +246,9 @@ void GameScreen::update() {
if (saveDirs.size()) {
btnDeleteSaveData_->SetVisibility(UI::V_VISIBLE);
}
if (info->pic0.texture || info->pic1.texture) {
btnSetBackground_->SetVisibility(UI::V_VISIBLE);
}
}
if (!info->IsPending()) {
// At this point, the above buttons won't become visible. We can show these now.
Expand Down Expand Up @@ -376,3 +377,77 @@ UI::EventReturn GameScreen::OnRemoveFromRecent(UI::EventParams &e) {
}
return UI::EVENT_DONE;
}

class SetBackgroundPopupScreen : public PopupScreen {
public:
SetBackgroundPopupScreen(const std::string &title, const std::string &gamePath);

protected:
bool FillVertical() const override { return false; }
bool ShowButtons() const override { return false; }
void CreatePopupContents(UI::ViewGroup *parent) override;
void update() override;

private:
std::string gamePath_;
double timeStart_;
double timeDone_ = 0.0;

enum class Status {
PENDING,
DELAY,
DONE,
};
Status status_ = Status::PENDING;
};

SetBackgroundPopupScreen::SetBackgroundPopupScreen(const std::string &title, const std::string &gamePath)
: PopupScreen(title), gamePath_(gamePath) {
timeStart_ = time_now_d();
}

void SetBackgroundPopupScreen::CreatePopupContents(UI::ViewGroup *parent) {
I18NCategory *ga = GetI18NCategory("Game");
parent->Add(new UI::TextView(ga->T("One moment please..."), ALIGN_LEFT | ALIGN_VCENTER, false, new UI::LinearLayoutParams(UI::Margins(10, 0, 10, 10))));
}

void SetBackgroundPopupScreen::update() {
PopupScreen::update();

GameInfo *info = g_gameInfoCache->GetInfo(nullptr, gamePath_, GAMEINFO_WANTBG | GAMEINFO_WANTBGDATA);
if (status_ == Status::PENDING && info && !info->IsPending()) {
GameInfoTex *pic = nullptr;
if (info->pic1.dataLoaded && info->pic1.data.size()) {
pic = &info->pic1;
} else if (info->pic0.dataLoaded && info->pic0.data.size()) {
pic = &info->pic0;
}

if (pic) {
const std::string bgPng = GetSysDirectory(DIRECTORY_SYSTEM) + "background.png";
writeStringToFile(false, pic->data, bgPng.c_str());
}

NativeMessageReceived("bgImage_updated", "");

// It's worse if it flickers, stay open for at least 1s.
timeDone_ = timeStart_ + 1.0;
status_ = Status::DELAY;
}

if (status_ == Status::DELAY && timeDone_ <= time_now_d()) {
TriggerFinish(DR_OK);
status_ = Status::DONE;
}
}

UI::EventReturn GameScreen::OnSetBackground(UI::EventParams &e) {
I18NCategory *ga = GetI18NCategory("Game");
// This popup is used to prevent any race condition:
// g_gameInfoCache may take time to load the data, and a crash could happen if they exit before then.
SetBackgroundPopupScreen *pop = new SetBackgroundPopupScreen(ga->T("Setting Background"), gamePath_);
if (e.v)
pop->SetPopupOrigin(e.v);
screenManager()->push(pop);
return UI::EVENT_DONE;
}
2 changes: 2 additions & 0 deletions UI/GameScreen.h
Expand Up @@ -59,6 +59,7 @@ class GameScreen : public UIDialogScreenWithGameBackground {
UI::EventReturn OnCreateConfig(UI::EventParams &e);
UI::EventReturn OnDeleteConfig(UI::EventParams &e);
UI::EventReturn OnCwCheat(UI::EventParams &e);
UI::EventReturn OnSetBackground(UI::EventParams &e);

// As we load metadata in the background, we need to be able to update these after the fact.
UI::TextureView *texvGameIcon_;
Expand All @@ -72,6 +73,7 @@ class GameScreen : public UIDialogScreenWithGameBackground {
UI::Choice *btnCreateGameConfig_;
UI::Choice *btnDeleteGameConfig_;
UI::Choice *btnDeleteSaveData_;
UI::Choice *btnSetBackground_;
std::vector<UI::Choice *> otherChoices_;
std::vector<std::string> saveDirs;
};
37 changes: 37 additions & 0 deletions UI/GameSettingsScreen.cpp
Expand Up @@ -667,6 +667,20 @@ void GameSettingsScreen::CreateViews() {
systemSettings->Add(new CheckBox(&g_Config.bCheckForNewVersion, sy->T("VersionCheck", "Check for new versions of PPSSPP")));
if (g_Config.iMaxRecent > 0)
systemSettings->Add(new Choice(sy->T("Clear Recent Games List")))->OnClick.Handle(this, &GameSettingsScreen::OnClearRecents);

const std::string bgPng = GetSysDirectory(DIRECTORY_SYSTEM) + "background.png";
const std::string bgJpg = GetSysDirectory(DIRECTORY_SYSTEM) + "background.jpg";
if (File::Exists(bgPng) || File::Exists(bgJpg)) {
backgroundChoice_ = systemSettings->Add(new Choice(sy->T("Clear UI background")));
} else if (System_GetPropertyInt(SYSPROP_HAS_IMAGE_BROWSER)) {
backgroundChoice_ = systemSettings->Add(new Choice(sy->T("Set UI background...")));
} else {
backgroundChoice_ = nullptr;
}
if (backgroundChoice_ != nullptr) {
backgroundChoice_->OnClick.Handle(this, &GameSettingsScreen::OnChangeBackground);
}

systemSettings->Add(new Choice(sy->T("Restore Default Settings")))->OnClick.Handle(this, &GameSettingsScreen::OnRestoreDefaultSettings);
systemSettings->Add(new CheckBox(&g_Config.bEnableAutoLoad, sy->T("Auto Load Newest Savestate")));

Expand Down Expand Up @@ -908,6 +922,29 @@ UI::EventReturn GameSettingsScreen::OnClearRecents(UI::EventParams &e) {
return UI::EVENT_DONE;
}

UI::EventReturn GameSettingsScreen::OnChangeBackground(UI::EventParams &e) {
const std::string bgPng = GetSysDirectory(DIRECTORY_SYSTEM) + "background.png";
const std::string bgJpg = GetSysDirectory(DIRECTORY_SYSTEM) + "background.jpg";
if (File::Exists(bgPng) || File::Exists(bgJpg)) {
if (File::Exists(bgPng)) {
File::Delete(bgPng);
}
if (File::Exists(bgJpg)) {
File::Delete(bgJpg);
}

NativeMessageReceived("bgImage_updated", "");
} else {
if (System_GetPropertyInt(SYSPROP_HAS_IMAGE_BROWSER)) {
System_SendMessage("bgImage_browse", "");
}
}

// Change to a browse or clear button.
RecreateViews();
return UI::EVENT_DONE;
}

UI::EventReturn GameSettingsScreen::OnReloadCheats(UI::EventParams &e) {
// Hmm, strange mechanism.
g_Config.bReloadCheats = true;
Expand Down
2 changes: 2 additions & 0 deletions UI/GameSettingsScreen.h
Expand Up @@ -47,6 +47,7 @@ class GameSettingsScreen : public UIDialogScreenWithGameBackground {
UI::Choice *layoutEditorChoice_;
UI::Choice *postProcChoice_;
UI::Choice *displayEditor_;
UI::Choice *backgroundChoice_ = nullptr;
UI::PopupMultiChoice *resolutionChoice_;
UI::CheckBox *frameSkipAuto_;
SettingInfoMessage *settingInfo_;
Expand Down Expand Up @@ -79,6 +80,7 @@ class GameSettingsScreen : public UIDialogScreenWithGameBackground {
UI::EventReturn OnChangeproAdhocServerAddress(UI::EventParams &e);
UI::EventReturn OnChangeMacAddress(UI::EventParams &e);
UI::EventReturn OnClearRecents(UI::EventParams &e);
UI::EventReturn OnChangeBackground(UI::EventParams &e);
UI::EventReturn OnFullscreenChange(UI::EventParams &e);
UI::EventReturn OnDisplayLayoutEditor(UI::EventParams &e);
UI::EventReturn OnResolutionChange(UI::EventParams &e);
Expand Down
8 changes: 4 additions & 4 deletions UI/MainScreen.cpp
Expand Up @@ -977,11 +977,11 @@ UI::EventReturn MainScreen::OnLoadFile(UI::EventParams &e) {
g_Config.Save();
screenManager()->switchScreen(new EmuScreen(fileName.toStdString()));
}
#elif PPSSPP_PLATFORM(UWP)
System_SendMessage("browse_file", "");
#elif defined(USING_WIN_UI)
MainWindow::BrowseAndBoot("");
#endif

if (System_GetPropertyInt(SYSPROP_HAS_FILE_BROWSER)) {
System_SendMessage("browse_file", "");
}
return UI::EVENT_DONE;
}

Expand Down
4 changes: 4 additions & 0 deletions Windows/MainWindow.cpp
Expand Up @@ -875,6 +875,10 @@ namespace MainWindow
BrowseAndBootDone();
break;

case WM_USER_BROWSE_BG_DONE:
BrowseBackgroundDone();
break;

case WM_MENUSELECT:
// Unfortunately, accelerate keys (hotkeys) shares the same enabled/disabled states
// with corresponding menu items.
Expand Down
3 changes: 2 additions & 1 deletion Windows/MainWindow.h
Expand Up @@ -16,6 +16,7 @@ namespace MainWindow
enum {
WM_USER_SAVESTATE_FINISH = WM_USER + 100,
WM_USER_UPDATE_UI = WM_USER + 101,
WM_USER_BROWSE_BG_DONE = WM_USER + 102,
WM_USER_WINDOW_TITLE_CHANGED = WM_USER + 103,
WM_USER_BROWSE_BOOT_DONE = WM_USER + 104,
WM_USER_TOGGLE_FULLSCREEN = WM_USER + 105,
Expand Down Expand Up @@ -74,4 +75,4 @@ namespace MainWindow
void SetWindowSize(int zoom);
}

#endif
#endif
32 changes: 32 additions & 0 deletions Windows/MainWindowMenu.cpp
Expand Up @@ -41,6 +41,7 @@ namespace MainWindow {
static const int numCPUs = 1; // what?
extern bool noFocusPause;
static W32Util::AsyncBrowseDialog *browseDialog;
static W32Util::AsyncBrowseDialog *browseImageDialog;
static bool browsePauseAfter;

static std::map<int, std::string> initialMenuKeys;
Expand Down Expand Up @@ -399,6 +400,37 @@ namespace MainWindow {
browseDialog = 0;
}

void BrowseBackground() {
static std::wstring filter = L"All supported images (*.jpg *.png)|*.jpg;*.png|All files (*.*)|*.*||";
for (size_t i = 0; i < filter.length(); i++) {
if (filter[i] == '|')
filter[i] = '\0';
}

W32Util::MakeTopMost(GetHWND(), false);
browseImageDialog = new W32Util::AsyncBrowseDialog(W32Util::AsyncBrowseDialog::OPEN, GetHWND(), WM_USER_BROWSE_BG_DONE, L"LoadFile", L"", filter, L"*.jpg;*.png;");
}

void BrowseBackgroundDone() {
std::string filename;
if (browseImageDialog->GetResult(filename)) {
std::wstring src = ConvertUTF8ToWString(filename);
std::wstring dest;
if (filename.size() >= 4 && filename.substr(filename.size() - 4) == ".jpg") {
dest = ConvertUTF8ToWString(GetSysDirectory(DIRECTORY_SYSTEM) + "background.jpg");
} else {
dest = ConvertUTF8ToWString(GetSysDirectory(DIRECTORY_SYSTEM) + "background.png");
}

CopyFileW(src.c_str(), dest.c_str(), FALSE);
NativeMessageReceived("bgImage_updated", "");
}

W32Util::MakeTopMost(GetHWND(), g_Config.bTopMost);

delete browseImageDialog;
browseImageDialog = nullptr;
}

static void UmdSwitchAction() {
std::string fn;
Expand Down
2 changes: 2 additions & 0 deletions Windows/MainWindowMenu.h
Expand Up @@ -9,5 +9,7 @@ namespace MainWindow {
void TranslateMenus(HWND hWnd, HMENU menu);
void BrowseAndBoot(std::string defaultPath, bool browseDirectory = false);
void BrowseAndBootDone();
void BrowseBackground();
void BrowseBackgroundDone();
void setTexScalingMultiplier(int level);
}

0 comments on commit 4ea01be

Please sign in to comment.