Skip to content

Commit

Permalink
Merge pull request calref#358 from NQNStudios/dialog-scale-2
Browse files Browse the repository at this point in the history
naively scale dialogs by ui scale
  • Loading branch information
CelticMinstrel authored May 31, 2024
2 parents b3cc666 + f8f5e05 commit e344542
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/dialogxml/dialogs/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,6 @@ void cDialog::loadFromFile(const DialogDefn& file){
dialogNotToast = true;
if(bg == BG_DARK) defTextClr = sf::Color::White;
// now calculate window rect
winRect = rectangle();
recalcRect();
currentFocus = "";
for(ctrlIter iter = controls.begin(); iter != controls.end(); iter++){
Expand All @@ -428,6 +427,7 @@ void cDialog::loadFromFile(const DialogDefn& file){

void cDialog::recalcRect(){
bool haveRel = false;
winRect = rectangle();
for(ctrlIter iter = controls.begin(); iter != controls.end(); iter++) {
using namespace std::placeholders;
if(auto container = dynamic_cast<cContainer*>(iter->second))
Expand All @@ -442,18 +442,22 @@ void cDialog::recalcRect(){
}
winRect.right += 6;
winRect.bottom += 6;
if(!haveRel) return;
// Resolve any remaining relative positions
// Controls placed relative to the dialog's edges can go off the edge of the dialog
for(ctrlIter iter = controls.begin(); iter != controls.end(); iter++) {
location pos = iter->second->getBounds().topLeft();
if(iter->second->horz == POS_REL_NEG)
pos.x = winRect.right - pos.x;
if(iter->second->vert == POS_REL_NEG)
pos.y = winRect.bottom - pos.y;
iter->second->horz = iter->second->vert = POS_ABS;
iter->second->relocate(pos);
if(haveRel) {
// Resolve any remaining relative positions
// Controls placed relative to the dialog's edges can go off the edge of the dialog
for(ctrlIter iter = controls.begin(); iter != controls.end(); iter++) {
location pos = iter->second->getBounds().topLeft();
if(iter->second->horz == POS_REL_NEG)
pos.x = winRect.right - pos.x;
if(iter->second->vert == POS_REL_NEG)
pos.y = winRect.bottom - pos.y;
iter->second->horz = iter->second->vert = POS_ABS;
iter->second->relocate(pos);
}
}

winRect.right *= ui_scale();
winRect.bottom *= ui_scale();
}

void cDialog::init(){
Expand Down Expand Up @@ -682,7 +686,7 @@ void cDialog::handle_one_event(const sf::Event& currentEvent) {
if(kb.isMetaPressed()) key.mod += mod_ctrl;
if(kb.isAltPressed()) key.mod += mod_alt;
if(kb.isShiftPressed()) 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
Expand Down Expand Up @@ -1043,6 +1047,11 @@ void cDialog::draw(){
animTimer.restart();
}

// Scale dialogs:
sf::View view = win.getDefaultView();
view.setViewport(sf::FloatRect(0, 0, ui_scale(), ui_scale()));
win.setView(view);

ctrlIter iter = controls.begin();
while(iter != controls.end()){
iter->second->draw();
Expand Down
2 changes: 2 additions & 0 deletions src/dialogxml/dialogs/dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "location.hpp"
#include <boost/any.hpp>
#include <boost/iterator/iterator_facade.hpp>
#include "tools/prefs.hpp"

class cControl;
class cTextField;
Expand Down Expand Up @@ -242,6 +243,7 @@ class cDialog {
cDialog& operator=(cDialog& other) = delete;
cDialog(cDialog& other) = delete;
private:
inline double ui_scale() { return get_float_pref("UIScale", 1.0); };
void draw();
void handle_events();
void handle_one_event(const sf::Event&);
Expand Down

0 comments on commit e344542

Please sign in to comment.