UI scaling: try to also scale the dialogs(simplify code)...
This commit is contained in:
@@ -167,12 +167,13 @@ cKey cControl::parseKey(string what){
|
|||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
cDialog::cDialog(cDialog* p) : parent(p) {}
|
cDialog::cDialog(cDialog* p) : parent(p), ui_scale(-1) {
|
||||||
|
|
||||||
cDialog::cDialog(std::string path, cDialog* p) : parent(p) {
|
|
||||||
loadFromFile(path + ".xml");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cDialog::cDialog(std::string path, cDialog* p) : parent(p), ui_scale(-1) {
|
||||||
|
loadFromFile(path + ".xml");
|
||||||
|
}
|
||||||
|
|
||||||
extern fs::path progDir;
|
extern fs::path progDir;
|
||||||
void cDialog::loadFromFile(std::string path){
|
void cDialog::loadFromFile(std::string path){
|
||||||
static const cKey enterKey = {true, key_enter};
|
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.create(sf::VideoMode(1,1),"");
|
||||||
win.close();
|
win.close();
|
||||||
#endif
|
#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
|
// 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();
|
draw();
|
||||||
makeFrontWindow(parent ? parent-> win : mainPtr);
|
makeFrontWindow(parent ? parent-> win : mainPtr);
|
||||||
makeFrontWindow(win);
|
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::RAlt)) key.mod += mod_alt;
|
||||||
if(kb::isKeyPressed(kb::LShift)) key.mod += mod_shift;
|
if(kb::isKeyPressed(kb::LShift)) key.mod += mod_shift;
|
||||||
if(kb::isKeyPressed(kb::RShift)) 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);
|
process_click(where, key.mod);
|
||||||
break;
|
break;
|
||||||
default: // To silence warning of unhandled enum values
|
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:
|
case sf::Event::MouseMoved:
|
||||||
bool inField = false;
|
bool inField = false;
|
||||||
for(auto& ctrl : controls) {
|
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);
|
set_cursor(text_curs);
|
||||||
inField = true;
|
inField = true;
|
||||||
break;
|
break;
|
||||||
|
@@ -38,6 +38,7 @@ enum eLabelPos {
|
|||||||
class cDialog {
|
class cDialog {
|
||||||
typedef std::map<std::string,cControl*>::iterator ctrlIter;
|
typedef std::map<std::string,cControl*>::iterator ctrlIter;
|
||||||
std::map<std::string,cControl*> controls;
|
std::map<std::string,cControl*> controls;
|
||||||
|
float ui_scale;
|
||||||
short bg;
|
short bg;
|
||||||
sf::Color defTextClr;
|
sf::Color defTextClr;
|
||||||
sf::RenderWindow win;
|
sf::RenderWindow win;
|
||||||
|
Reference in New Issue
Block a user