move minimap logic out of dialog

This commit is contained in:
2025-04-12 10:01:17 -05:00
parent 704e724fe9
commit 5d7f3088e9
3 changed files with 27 additions and 17 deletions

View File

@@ -43,8 +43,8 @@ const short cDialog::BG_DARK = 5, cDialog::BG_LIGHT = 16;
short cDialog::defaultBackground = cDialog::BG_DARK;
cDialog* cDialog::topWindow = nullptr;
void (*cDialog::redraw_everything)() = nullptr;
std::function<sf::RenderWindow&()> cDialog::get_mini_map;
bool* cDialog::map_visible_p = nullptr;
std::function<void(sf::RenderWindow& win)> cDialog::onLostFocus;
std::function<void(sf::RenderWindow& win)> cDialog::onGainedFocus;
extern std::map<std::string,sf::Color> colour_map;
@@ -743,23 +743,18 @@ void cDialog::handle_one_event(const sf::Event& currentEvent, cFramerateLimiter&
break;
case sf::Event::LostFocus:
has_focus = false;
if(get_mini_map){
setWindowFloating(get_mini_map(), false);
if(onLostFocus){
onLostFocus(win);
}
break;
case sf::Event::GainedFocus:
if(!has_focus){
has_focus = true;
if(get_mini_map){
setWindowFloating(get_mini_map(), true);
}
makeFrontWindow(mainPtr());
if(get_mini_map){
if(*map_visible_p){
makeFrontWindow(get_mini_map());
}
if(onGainedFocus){
onGainedFocus(win);
}
// Put all dialogs in correct z order:
std::vector<sf::RenderWindow*> dialog_stack;
cDialog* next = this;
while(next != nullptr){
@@ -769,7 +764,7 @@ void cDialog::handle_one_event(const sf::Event& currentEvent, cFramerateLimiter&
for(int i = dialog_stack.size() - 1; i >= 0; --i){
makeFrontWindow(*(dialog_stack[i]));
}
// that generates a LostFocus and a GainedFocus event
// that generates more LostFocus and GainedFocus event(s)
sf::Event evt;
while(pollEvent(win, evt));
}

View File

@@ -79,9 +79,9 @@ class cDialog : public iComponent, public iNameGiver {
bool doAnimations;
bool has_focus = false;
public:
static std::function<sf::RenderWindow&()> get_mini_map;
static bool* map_visible_p;
static void (*redraw_everything)();
static std::function<void(sf::RenderWindow& win)> onLostFocus;
static std::function<void(sf::RenderWindow& win)> onGainedFocus;
/// Performs essential startup initialization. Generally should not be called directly.
static void init();
static bool wasInitCalled() { return initCalled; };

View File

@@ -224,6 +224,21 @@ static void handleFatalError(std::string what) {
}
}
void dialog_lost_focus(sf::RenderWindow& win) {
setWindowFloating(mini_map(), false);
}
void dialog_gained_focus(sf::RenderWindow& win) {
setWindowFloating(mini_map(), true);
makeFrontWindow(mainPtr());
if(map_visible){
makeFrontWindow(mini_map());
}
// that generates more LostFocus and GainedFocus event(s)
sf::Event evt;
while(pollEvent(win, evt));
}
// Comment this line out for exact exception callstacks:
#define CATCH_ERRORS
@@ -236,8 +251,8 @@ int main(int argc, char* argv[]) {
try{
#endif
cDialog::redraw_everything = &redraw_everything;
cDialog::get_mini_map = &mini_map;
cDialog::map_visible_p = &map_visible;
cDialog::onLostFocus = &dialog_lost_focus;
cDialog::onGainedFocus = &dialog_gained_focus;
init_boe(argc, argv);