pc/scenario editor: add a basic preferences's menu.

This commit is contained in:
ALONSO Laurent
2022-07-15 15:53:23 +02:00
committed by Celtic Minstrel
parent d83139eaa0
commit 26d2328fe7
11 changed files with 165 additions and 11 deletions

View File

@@ -10,6 +10,7 @@
#include "gfx/render_shapes.hpp"
#include "gfx/render_image.hpp"
#include "fileio/resmgr/res_image.hpp"
#include "dialogxml/widgets/led.hpp"
#include "dialogxml/widgets/message.hpp"
#include "mathutil.hpp"
#ifndef MSBUILD_GITREV

View File

@@ -52,6 +52,7 @@ short current_active_pc = 0;
/* Mac stuff globals */
bool All_Done = false;
bool changed_display_mode = false;
sf::RenderWindow mainPtr;
sf::View mainView;
fs::path file_in_mem;
@@ -65,10 +66,15 @@ void handle_one_event(const sf::Event&);
void redraw_everything();
void Handle_Activate();
void Mouse_Pressed(const sf::Event&);
void init_main_window(sf::RenderWindow&, sf::View&);
void adjust_window(sf::RenderWindow&, sf::View&);
sf::FloatRect compute_viewport(const sf::RenderWindow&, float ui_scale);
bool verify_restore_quit(std::string dlog);
void set_up_apple_events(int argc, char* argv[]);
void pick_preferences();
void save_prefs();
bool prefs_event_filter (cDialog& me, std::string id, eKeyMod);
extern bool cur_scen_is_mac;
extern fs::path progDir;
short specials_res_id;
@@ -79,8 +85,8 @@ int main(int argc, char* argv[]) {
try {
init_directories(argv[0]);
sync_prefs();
init_main_window(mainPtr, mainView);
init_menubar();
adjust_window(mainPtr, mainView);
//init_menubar();
init_fileio();
init_main_buttons();
Set_up_win();
@@ -137,7 +143,7 @@ sf::FloatRect compute_viewport(const sf::RenderWindow& mainPtr, float ui_scale)
return viewport;
}
void init_main_window (sf::RenderWindow& mainPtr, sf::View& mainView) {
void adjust_window (sf::RenderWindow& mainPtr, sf::View& mainView) {
float ui_scale = get_float_pref("UIScale", 1.0);
@@ -167,6 +173,8 @@ void init_main_window (sf::RenderWindow& mainPtr, sf::View& mainView) {
const ImageRsrc& icon = ResMgr::graphics.get("icon", true);
mainPtr.setIcon(icon->getSize().x, icon->getSize().y, icon->copyToImage().getPixelsPtr());
#endif
init_menubar();
}
void handle_events() {
@@ -174,6 +182,11 @@ void handle_events() {
cFramerateLimiter fps_limiter;
while(!All_Done) {
if(changed_display_mode) {
changed_display_mode = false;
adjust_window(mainPtr, mainView);
}
#ifdef __APPLE__
if (menuChoiceId>=0) {
handle_menu_choice(eMenu(menuChoiceId));
@@ -273,7 +286,10 @@ void handle_menu_choice(eMenu item_hit) {
if(verify_restore_quit("save-close"))
file_in_mem = "";
break;
case eMenu::QUIT:
case eMenu::PREFS:
pick_preferences();
break;
case eMenu::QUIT: // quit case eMenu::QUIT:
All_Done = verify_restore_quit("save-quit");
break;
case eMenu::EDIT_GOLD:
@@ -480,3 +496,49 @@ void display_skills(eSkill skill,cDialog* parent) {
skillDlog.run();
}
void save_prefs(){
bool success = sync_prefs();
if(!success){
showWarning("There was a problem writing to the preferences file. When the character editor is next run the preferences will revert to their previously set values.","Should you manage to resolve the problem without closing the program, simply open the preferences screen and click \"OK\" to try again.");
}
}
bool prefs_event_filter (cDialog& me, std::string id, eKeyMod) {
bool did_cancel = false;
if(id == "okay") {
me.toast(true);
} else if(id == "cancel") {
me.toast(false);
did_cancel = true;
}
if(!did_cancel) {
cLed& ui_scale = dynamic_cast<cLed&>(me["scaleui"]);
if(ui_scale.getState() == led_off)
set_pref("UIScale", 1.0);
else if(ui_scale.getState() == led_red)
set_pref("UIScale", 2.0);
set_pref("PlaySounds", dynamic_cast<cLed&>(me["nosound"]).getState() == led_off);
}
save_prefs();
return true;
}
void pick_preferences() {
set_cursor(sword_curs);
cDialog prefsDlog(*ResMgr::dialogs.get("pref-character"));
prefsDlog.attachClickHandlers(&prefs_event_filter, {"okay", "cancel"});
float ui_scale = get_float_pref("UIScale", 1.0);
dynamic_cast<cLed&>(prefsDlog["scaleui"]).setState(ui_scale == 1.0 ? led_off : (ui_scale == 2.0 ? led_red : led_green));
dynamic_cast<cLed&>(prefsDlog["nosound"]).setState(get_bool_pref("PlaySounds", true) ? led_off : led_red);
prefsDlog.run();
if(get_float_pref("UIScale") != ui_scale)
changed_display_mode = true;
}

View File

@@ -40,6 +40,7 @@ void OpenBoEPCEditMenu::add_persistent_menu_items(tgui::MenuBar::Ptr& menubar) c
{ { "File", "Save Game Ctrl-S" }, eMenu::FILE_SAVE },
{ { "File", "Save As... Ctrl-Shift-S" }, eMenu::FILE_SAVE_AS },
{ { "File", "Revert to Saved" }, eMenu::FILE_REVERT },
{ { "File", "Preferences" }, eMenu::PREFS },
{ { "File", "Quit Ctrl-Q" }, eMenu::QUIT },
{ { "Party", "Edit Gold" }, eMenu::EDIT_GOLD },

View File

@@ -17,7 +17,7 @@ bool menuBarProcessEvent(const sf::Event&);
void drawMenuBar();
enum class eMenu {
NONE, ABOUT, QUIT,
NONE, ABOUT, PREFS, QUIT,
FILE_OPEN, FILE_CLOSE, FILE_SAVE, FILE_SAVE_AS, FILE_REVERT, HELP_TOC,
// Edit Party menu
EDIT_GOLD, EDIT_FOOD, EDIT_ALCHEMY,

View File

@@ -65,6 +65,7 @@ void init_menubar() {
MenuHandler* handler = [[[MenuHandler alloc] init] retain];
setMenuCallback([apple_menu itemWithTitle: @"About BoE Character Editor"], handler, @selector(menuChoice:), int(eMenu::ABOUT));
setMenuCallback([apple_menu itemWithTitle: @"Preferences…"], handler, @selector(menuChoice:), int(eMenu::PREFS));
setMenuCallback([apple_menu itemWithTitle: @"Quit BoE Character Editor"], handler, @selector(menuChoice:), int(eMenu::QUIT));
setMenuCallback([[[menu_bar_handle itemWithTitle: @"Help"] submenu] itemAtIndex: 0], handler, @selector(menuChoice:), int(eMenu::HELP_TOC));

View File

@@ -18,12 +18,15 @@
#include "scen.keydlgs.hpp"
#include "mathutil.hpp"
#include "fileio/fileio.hpp"
#include "dialogxml/widgets/button.hpp"
#include "dialogxml/widgets/led.hpp"
#include "dialogxml/widgets/scrollbar.hpp"
#include "tools/winutil.hpp"
#include "tools/cursors.hpp"
#include "dialogxml/dialogs/strdlog.hpp"
#include "dialogxml/dialogs/choicedlog.hpp"
#include "scen.menus.hpp"
#include "fileio/resmgr/res_dialog.hpp"
#include "fileio/resmgr/res_image.hpp"
#include "tools/prefs.hpp"
#include "tools/framerate_limiter.hpp"
@@ -37,6 +40,7 @@ short menuChoiceId=-1;
/* Globals */
bool All_Done = false;
bool changed_display_mode = false;
sf::RenderWindow mainPtr;
sf::View mainView;
cTown* town = nullptr;
@@ -63,9 +67,12 @@ void redraw_everything();
void Mouse_Pressed(const sf::Event&);
void close_program();
void ding();
void init_main_window(sf::RenderWindow&, sf::View&);
void adjust_windows(sf::RenderWindow&, sf::View&);
sf::FloatRect compute_viewport(const sf::RenderWindow&, float ui_scale);
void pick_preferences();
void save_prefs();
cScenario scenario;
rectangle right_sbar_rect;
extern rectangle terrain_buttons_rect;
@@ -153,7 +160,7 @@ sf::FloatRect compute_viewport(const sf::RenderWindow & mainPtr, float ui_scale)
return viewport;
}
void init_main_window (sf::RenderWindow & mainPtr, sf::View & mainView) {
void adjust_windows (sf::RenderWindow & mainPtr, sf::View & mainView) {
// TODO: things might still be broken when upscaled.
// translate_mouse_coordinates has been applied in some places but more work might be needed.
@@ -186,13 +193,14 @@ void init_main_window (sf::RenderWindow & mainPtr, sf::View & mainView) {
const ImageRsrc& icon = ResMgr::graphics.get("icon", true);
mainPtr.setIcon(icon->getSize().x, icon->getSize().y, icon->copyToImage().getPixelsPtr());
#endif
init_menubar();
}
void init_scened(int argc, char* argv[]) {
init_directories(argv[0]);
sync_prefs();
init_main_window(mainPtr, mainView);
init_menubar();
adjust_windows(mainPtr, mainView);
//init_menubar();
init_shaders();
init_tiling();
init_snd_tool();
@@ -232,6 +240,11 @@ void handle_events() {
cFramerateLimiter fps_limiter;
while(!All_Done) {
if(changed_display_mode) {
changed_display_mode = false;
adjust_windows(mainPtr, mainView);
}
#ifdef __APPLE__
if (menuChoiceId>=0) {
handle_menu_choice(eMenu(menuChoiceId));
@@ -357,6 +370,9 @@ void handle_menu_choice(eMenu item_hit) {
undo_list.clear();
update_edit_menu();
break;
case eMenu::PREFS:
pick_preferences();
break;
case eMenu::QUIT: // quit
if(!save_check("save-before-quit"))
break;
@@ -641,6 +657,51 @@ void handle_menu_choice(eMenu item_hit) {
redraw_screen();
}
void save_prefs(){
bool success = sync_prefs();
if(!success){
showWarning("There was a problem writing to the preferences file. When the Scenerio Editor is next run the preferences will revert to their previously set values.","Should you manage to resolve the problem without closing the program, simply open the preferences screen and click \"OK\" to try again.");
}
}
static bool prefs_event_filter (cDialog& me, std::string id, eKeyMod) {
bool did_cancel = false;
if(id == "okay") {
me.toast(true);
} else if(id == "cancel") {
me.toast(false);
did_cancel = true;
}
if(!did_cancel) {
cLed& ui_scale = dynamic_cast<cLed&>(me["scaleui"]);
if(ui_scale.getState() == led_off)
set_pref("UIScale", 1.0);
else if(ui_scale.getState() == led_red)
set_pref("UIScale", 2.0);
set_pref("PlaySounds", dynamic_cast<cLed&>(me["nosound"]).getState() == led_off);
}
save_prefs();
return true;
}
void pick_preferences() {
set_cursor(sword_curs);
cDialog prefsDlog(*ResMgr::dialogs.get("pref-scenario"));
prefsDlog.attachClickHandlers(&prefs_event_filter, {"okay", "cancel"});
float ui_scale = get_float_pref("UIScale", 1.0);
dynamic_cast<cLed&>(prefsDlog["scaleui"]).setState(ui_scale == 1.0 ? led_off : (ui_scale == 2.0 ? led_red : led_green));
dynamic_cast<cLed&>(prefsDlog["nosound"]).setState(get_bool_pref("PlaySounds", true) ? led_off : led_red);
prefsDlog.run();
if(get_float_pref("UIScale") != ui_scale)
changed_display_mode = true;
}
void Mouse_Pressed(const sf::Event & event) {
location mousePos { translate_mouse_coordinates({event.mouseButton.x, event.mouseButton.y}) };
handle_action(mousePos,event);

View File

@@ -45,6 +45,7 @@ void OpenBoESceneditMenu::add_persistent_menu_items(tgui::MenuBar::Ptr& menubar)
{ { "File", "Save Scenario Ctrl-S" }, eMenu::FILE_SAVE },
{ { "File", "Save As... Ctrl-Shift-S" }, eMenu::FILE_SAVE_AS },
{ { "File", "Revert to Saved" }, eMenu::FILE_REVERT },
{ { "File", "Preferences" }, eMenu::PREFS },
{ { "File", "Quit Ctrl-Q" }, eMenu::QUIT },
{ { "Edit", "Undo Ctrl-Z" }, eMenu::EDIT_UNDO },

View File

@@ -14,7 +14,7 @@ void shut_down_menus(short mode);
void update_edit_menu();
enum class eMenu {
NONE, ABOUT, QUIT, FRILL, UNFRILL,
NONE, ABOUT, PREFS, QUIT, FRILL, UNFRILL,
FILE_NEW, FILE_OPEN, FILE_CLOSE, FILE_SAVE, FILE_SAVE_AS, FILE_REVERT,
EDIT_UNDO, EDIT_REDO, EDIT_CUT, EDIT_COPY, EDIT_PASTE, EDIT_DELETE, EDIT_SELECT_ALL,
HELP_TOC, HELP_START, HELP_TEST, HELP_DIST, HELP_CONTEST,

View File

@@ -79,6 +79,7 @@ void init_menubar() {
MenuHandler* handler = [[[MenuHandler alloc] init] retain];
setMenuCallback([app_menu itemWithTitle: @"About BoE Scenario Editor"], handler, @selector(menuChoice:), int(eMenu::ABOUT));
setMenuCallback([app_menu itemWithTitle: @"Preferences…"], handler, @selector(menuChoice:), int(eMenu::PREFS));
setMenuCallback([app_menu itemWithTitle: @"Quit BoE Scenario Editor"], handler, @selector(menuChoice:), int(eMenu::QUIT));
int i = 0;