UI scaling: try to also scale the dialogs(simplify code)...

This commit is contained in:
Laurent Alonso(fr)
2020-06-10 19:59:55 +02:00
committed by Celtic Minstrel
parent 34d3431e49
commit 5896188094
2 changed files with 33 additions and 8 deletions

View File

@@ -167,12 +167,13 @@ cKey cControl::parseKey(string what){
return key;
}
cDialog::cDialog(cDialog* p) : parent(p) {}
cDialog::cDialog(std::string path, cDialog* p) : parent(p) {
loadFromFile(path + ".xml");
cDialog::cDialog(cDialog* p) : parent(p), ui_scale(-1) {
}
cDialog::cDialog(std::string path, cDialog* p) : parent(p), ui_scale(-1) {
loadFromFile(path + ".xml");
}
extern fs::path progDir;
void cDialog::loadFromFile(std::string path){
static const cKey enterKey = {true, key_enter};
@@ -551,9 +552,32 @@ void cDialog::run(std::function<void(cDialog&)> onopen){
win.create(sf::VideoMode(1,1),"");
win.close();
#endif
win.create(sf::VideoMode(winRect.width(), winRect.height()), "Dialog", sf::Style::Titlebar);
if (ui_scale<=0) {
ui_scale = get_float_pref("UIScale", 1.0);
if (ui_scale < 0.1) ui_scale = 1.0;
// check that the dialog can be displayed on the screen
sf::VideoMode desktop = sf::VideoMode::getDesktopMode();
if (desktop.width< ui_scale*winRect.width())
ui_scale=float(desktop.width)/winRect.width();
if ((desktop.height-30)< ui_scale*winRect.height())
ui_scale=float(desktop.height-30)/winRect.height();
}
int wWidth=int(ui_scale*winRect.width()), wHeight=int(ui_scale*winRect.height());
win.create(sf::VideoMode(wWidth, wHeight), "Dialog", sf::Style::Titlebar);
sf::FloatRect viewport;
viewport.top = 0;
viewport.left = 0;
viewport.width = ui_scale;
viewport.height = ui_scale;
sf::View view;
view.reset(sf::FloatRect(0, 0, wWidth, wHeight));
view.setViewport(viewport);
win.setView(view);
// ASAN overflow
win.setPosition({parentPos.x + (int(parentSz.x) - winRect.width()) / 2, parentPos.y + (int(parentSz.y) - winRect.height()) / 2});
win.setPosition({parentPos.x + (int(parentSz.x) - wWidth) / 2, parentPos.y + (int(parentSz.y) - wHeight) / 2});
draw();
makeFrontWindow(parent ? parent-> win : mainPtr);
makeFrontWindow(win);
@@ -719,7 +743,7 @@ void cDialog::handle_one_event(const sf::Event& currentEvent) {
if(kb::isKeyPressed(kb::RAlt)) key.mod += mod_alt;
if(kb::isKeyPressed(kb::LShift)) key.mod += mod_shift;
if(kb::isKeyPressed(kb::RShift)) key.mod += mod_shift;
where = {currentEvent.mouseButton.x, currentEvent.mouseButton.y};
where = {int(currentEvent.mouseButton.x/ui_scale), int(currentEvent.mouseButton.y/ui_scale)};
process_click(where, key.mod);
break;
default: // To silence warning of unhandled enum values
@@ -728,7 +752,7 @@ void cDialog::handle_one_event(const sf::Event& currentEvent) {
case sf::Event::MouseMoved:
bool inField = false;
for(auto& ctrl : controls) {
if(ctrl.second->getType() == CTRL_FIELD && ctrl.second->getBounds().contains(currentEvent.mouseMove.x, currentEvent.mouseMove.y)) {
if(ctrl.second->getType() == CTRL_FIELD && ctrl.second->getBounds().contains(currentEvent.mouseMove.x/ui_scale, currentEvent.mouseMove.y/ui_scale)) {
set_cursor(text_curs);
inField = true;
break;

View File

@@ -38,6 +38,7 @@ enum eLabelPos {
class cDialog {
typedef std::map<std::string,cControl*>::iterator ctrlIter;
std::map<std::string,cControl*> controls;
float ui_scale;
short bg;
sf::Color defTextClr;
sf::RenderWindow win;