File indexing completed on 2022-09-27 13:19:23

0001 /*
0002     SPDX-FileCopyrightText: 1998-2001 Andreas Zehender <az@azweb.de>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "dialogs.h"
0008 #include "options.h"
0009 #include "ui_general.h"
0010 
0011 #include <KLocalizedString>
0012 
0013 #include <QGridLayout>
0014 #include <QLCDNumber>
0015 #include <QSlider>
0016 #include <QTabWidget>
0017 
0018 class General : public QWidget, public Ui::General
0019 {
0020 public:
0021     General(QWidget* parent = nullptr)
0022         : QWidget(parent)
0023         {
0024             setupUi(this);
0025         }
0026 };
0027 
0028 bool operator!=(const SConfig &s1, const SConfig &s2)
0029 {
0030    return
0031 (s1.gamespeed != s2.gamespeed) ||
0032 (s1.gravity != s2.gravity) ||
0033 (s1.acc != s2.acc) ||
0034 (s1.energyNeed != s2.energyNeed) ||
0035 (s1.sunEnergy != s2.sunEnergy) ||
0036 (s1.rotationSpeed != s2.rotationSpeed) ||
0037 (s1.mineActivateTime != s2.mineActivateTime) ||
0038 (s1.mineFuel != s2.mineFuel) ||
0039 (s1.shotSpeed != s2.shotSpeed) ||
0040 (s1.shotEnergyNeed != s2.shotEnergyNeed) ||
0041 (s1.mineEnergyNeed != s2.mineEnergyNeed) ||
0042 (s1.rotationEnergyNeed != s2.rotationEnergyNeed) ||
0043 (s1.startPosX != s2.startPosX) ||
0044 (s1.startPosY != s2.startPosY) ||
0045 (s1.startVelX != s2.startVelX) ||
0046 (s1.startVelY != s2.startVelY) ||
0047 (s1.bulletLifeTime != s2.bulletLifeTime) ||
0048 (s1.mineReloadTime != s2.mineReloadTime) ||
0049 (s1.bulletReloadTime != s2.bulletReloadTime) ||
0050 (s1.bulletDamage != s2.bulletDamage) ||
0051 (s1.shipDamage != s2.shipDamage) ||
0052 (s1.mineDamage != s2.mineDamage) ||
0053 (s1.maxBullets != s2.maxBullets) ||
0054 (s1.maxMines != s2.maxMines) ||
0055 (s1.powerupLifeTime != s2.powerupLifeTime) ||
0056 (s1.powerupRefreshTime != s2.powerupRefreshTime) ||
0057 (s1.powerupEnergyAmount != s2.powerupEnergyAmount) ||
0058 (s1.powerupShieldAmount != s2.powerupShieldAmount);
0059 }
0060 
0061 KLazyLocalizedString ConfigSetup::LabelName[EditNum] =
0062 {
0063    kli18n("Game speed:"),   kli18n("Shot speed:"),
0064    kli18n("Energy need:"),  kli18n("Max number:"),
0065    kli18n("Damage:"),   kli18n("Life time:"),
0066    kli18n("Reload time:"),  kli18n("Mine fuel:"),
0067    kli18n("Energy need:"),  kli18n("Activate time:"),
0068    kli18n("Damage:"),   kli18n("Max number:"),
0069    kli18n("Reload time:"),  kli18n("Acceleration:"),
0070    kli18n("Energy need:"),  kli18n("Rotation speed:"),
0071    kli18n("Energy need:"),  kli18n("Crash damage:"),
0072    kli18n("Sun energy:"),   kli18n("Gravity:"),
0073    kli18n("Position X:"),   kli18n("Position Y:"),
0074    kli18n("Velocity X:"),   kli18n("Velocity Y:"),
0075    kli18n("Appearance time:"),kli18n("Life time:"),
0076    kli18n("Energy amount:"),    kli18n("Shield amount:")
0077 };
0078 
0079 enum ConfigSetup::Type ConfigSetup::VarType[EditNum]=
0080 {VarFloat,
0081  VarFloat,VarFloat,VarInt,VarInt,VarFloat,VarFloat,
0082  VarFloat,VarFloat,VarFloat,VarInt,VarInt,VarFloat,
0083  VarFloat,VarFloat,VarFloat,
0084  VarFloat,VarInt,
0085  VarFloat,VarFloat,
0086  VarFloat,VarFloat,VarFloat,VarFloat,
0087  VarFloat,VarFloat,VarFloat,VarInt};
0088 
0089 double ConfigSetup::EditVal[EditNum][3]=
0090 {{0.2,4.0,1},
0091  {0.1,10,3},{0,99,10},{0,10,5},{1,100,20},{50,2000,100},{0,400,10},
0092  {5,200,65},{0,99,5},{5,100,15},{1,100,30},{0,10,3},{0,400,10},
0093  {0,1.0,0.2},{0,10,1.0},{0.1,4,1},
0094  {0,10,0.2},{0,100,50},
0095  {1000,30000,9000},{0,10000,2200},
0096  {-250,250,-180},{-180,180,-130},{-10,10,3},{-10,10,-1.7},
0097  {200,4000,800},{200,4000,400},{0,99,50},{0,99,30}};
0098 
0099 int ConfigSetup::EditDiv[EditNum]=
0100 {100,
0101  10,1,1,1,1,10,
0102  1,1,10,1,1,10,
0103  100,10,10,
0104  10,1,
0105  1,1,
0106  10,10,100,100,
0107  1,1,1,1};
0108 
0109 int ConfigSetup::Parent[EditNum]=
0110 {TabGeneral,
0111  TabBullet,TabBullet,TabBullet,TabBullet,TabBullet,TabBullet,
0112  TabMine,TabMine,TabMine,TabMine,TabMine,TabMine,
0113  TabShip,TabShip,TabShip,TabShip,TabShip,
0114  TabSun,TabSun,
0115  TabStart,TabStart,TabStart,TabStart,
0116  TabPowerups,TabPowerups,TabPowerups,TabPowerups};
0117 
0118 int ConfigSetup::Position[EditNum]=
0119 {0,
0120  0,1,2,3,4,5,
0121  0,1,2,3,4,5,
0122  0,1,2,3,4,
0123  0,1,
0124  0,1,2,3,
0125  0,1,2,3};
0126 
0127 const int LCDLen=6;
0128 
0129 ConfigSetup::ConfigSetup(SConfig *custom,QWidget *parent)
0130       :QWidget( parent )
0131 {
0132    QLabel *label[EditNum];
0133    QGridLayout *stacklayout[TabNum];
0134    QWidget *configWidgets[TabNum];
0135    //QGroupBox *box;
0136 
0137    int i;
0138 
0139    resize(500,400);
0140 
0141    QVBoxLayout *boxlayout = new QVBoxLayout( this );
0142    boxlayout->setContentsMargins(6, 6, 6, 6);
0143 
0144    tabs=new QTabWidget(this);
0145    for(i=0;i<TabNum;++i)
0146    {
0147       configWidgets[i]=new QWidget(tabs);
0148       stacklayout[i]=new QGridLayout(configWidgets[i]);
0149       stacklayout[i]->setContentsMargins(10, 10, 10, 10);
0150    }
0151 
0152    for(i=0;i<EditNum;++i)
0153    {
0154        label[i]=new QLabel(LabelName[i].toString(),configWidgets[Parent[i]]);
0155       slider[i]=new QSlider(Qt::Horizontal,configWidgets[Parent[i]]);
0156       slider[i]->setRange((int)(EditVal[i][0]*EditDiv[i]), (int)(EditVal[i][1]*EditDiv[i]));
0157       slider[i]->setPageStep((int)((EditVal[i][1]-EditVal[i][0])/10));
0158       slider[i]->setValue((int)(EditVal[i][2]*EditDiv[i]));
0159       connect(slider[i],&QSlider::valueChanged, this, &ConfigSetup::sliderChanged);
0160       value[i]=new QLCDNumber(LCDLen,configWidgets[Parent[i]]);
0161       value[i]->setFrameStyle(QFrame::NoFrame);
0162    }
0163 
0164    configCombo=new QComboBox(this);
0165    configCombo->setEditable(false);
0166    connect(configCombo, static_cast<void(QComboBox::*)(int)>(&QComboBox::activated), this, &ConfigSetup::configSelected);
0167    for(i=0;i<predefinedConfigNum;++i)
0168        configCombo->addItem(predefinedConfigName[i].toString());
0169    configCombo->addItem(i18nc("custom values","Custom"));
0170 
0171    boxlayout->addSpacing( 2 * 6 );
0172    boxlayout->addWidget(configCombo);
0173    boxlayout->addWidget(tabs);
0174 
0175    for(i=0;i<EditNum;++i)
0176    {
0177       stacklayout[Parent[i]]->addWidget(label[i],Position[i],0);
0178       stacklayout[Parent[i]]->addWidget(slider[i],Position[i],1);
0179       stacklayout[Parent[i]]->addWidget(value[i],Position[i],2);
0180    }
0181 
0182    for(i=0;i<TabNum;++i)
0183       stacklayout[i]->activate();
0184 
0185    tabs->addTab(configWidgets[0],i18nc("general game settings","General"));
0186    tabs->addTab(configWidgets[1],i18n("Bullet"));
0187    tabs->addTab(configWidgets[2],i18nc("Name","Mine"));
0188    tabs->addTab(configWidgets[3],i18n("Ship"));
0189    tabs->addTab(configWidgets[4],i18nc("the star, Sol","Sun"));
0190    tabs->addTab(configWidgets[5],i18nc("initial position and velocities of players","Start"));
0191    tabs->addTab(configWidgets[6],i18n("Powerups"));
0192 
0193    customConfig=custom;
0194 
0195    updateWidgets();
0196 }
0197 
0198 void ConfigSetup::updateWidgets()
0199 {
0200    config=*customConfig;
0201    selectedConfig = -1;
0202    configCombo->setCurrentIndex(Options::lastConfig());
0203    configSelected(Options::lastConfig());
0204 }
0205 
0206 void ConfigSetup::valueChanged(int ednum,int value)
0207 {
0208    if(selectedConfig==predefinedConfigNum)
0209    {
0210       switch(ednum)
0211       {
0212          case EditMaxBullets:config.maxBullets=value;break;
0213          case EditBulletDamage:config.bulletDamage=value;break;
0214          case EditMaxMines:config.maxMines=value;break;
0215          case EditMineDamage:config.mineDamage=value;break;
0216          case EditShipDamage:config.shipDamage=value;break;
0217          case EditPowerupShieldAmount:config.powerupShieldAmount=value;break;
0218       }
0219       Q_EMIT changed();
0220    }
0221 }
0222 
0223 void ConfigSetup::valueChanged(int ednum,double value)
0224 {
0225    if(selectedConfig==predefinedConfigNum)
0226    {
0227       switch(ednum)
0228       {
0229          case EditSunEnergy:config.sunEnergy=value;break;
0230          case EditGravity:config.gravity=value;break;
0231          case EditShotSpeed:config.shotSpeed=value;break;
0232          case EditShotEnergyNeed:config.shotEnergyNeed=value;break;
0233          case EditBulletLifeTime:config.bulletLifeTime=value;break;
0234          case EditBulletReloadTime:config.bulletReloadTime=value;break;
0235          case EditMineFuel:config.mineFuel=value;break;
0236          case EditMineEnergyNeed:config.mineEnergyNeed=value;break;
0237          case EditMineActivateTime:config.mineActivateTime=value;break;
0238          case EditMineReloadTime:config.mineReloadTime=value;break;
0239          case EditGamespeed:config.gamespeed=value;break;
0240          case EditPosX:config.startPosX=value;break;
0241          case EditPosY:config.startPosY=value;break;
0242          case EditVelX:config.startVelX=value;break;
0243          case EditVelY:config.startVelY=value;break;
0244          case EditAcc:config.acc=value;break;
0245          case EditEnergyNeed:config.energyNeed=value;break;
0246          case EditRotationSpeed:config.rotationSpeed=value;break;
0247          case EditRotationEnergyNeed:config.rotationEnergyNeed=value;break;
0248          case EditPowerupRefreshTime:config.powerupRefreshTime=value;break;
0249          case EditPowerupLifeTime:config.powerupLifeTime=value;break;
0250          case EditPowerupEnergyAmount:config.powerupEnergyAmount=value;break;
0251       }
0252       Q_EMIT changed();
0253    }
0254 }
0255 
0256 void ConfigSetup::updateSettings()
0257 {
0258    *customConfig=config;
0259 
0260    Options::setLastConfig(selectedConfig);
0261    Options::self()->save();
0262 }
0263 
0264 bool ConfigSetup::hasChanged()
0265 {
0266    if (configCombo->currentIndex() != Options::lastConfig())
0267       return true;
0268 
0269    if (configCombo->currentIndex() != predefinedConfigNum)
0270       return false;
0271 
0272    return ((*customConfig) != config);
0273 }
0274 
0275 void ConfigSetup::updateWidgetsDefault()
0276 {
0277    configCombo->setCurrentIndex(0);
0278    configSelected(0);
0279 }
0280 
0281 bool ConfigSetup::isDefault()
0282 {
0283    return configCombo->currentIndex() == 0;
0284 }
0285 
0286 void ConfigSetup::configSelected(int num)
0287 {
0288    int i;
0289    if(!((selectedConfig==predefinedConfigNum)&&(num==predefinedConfigNum)))
0290    {
0291       selectedConfig = num;
0292       for(i=0;i<EditNum;i++)
0293          slider[i]->setEnabled(num==predefinedConfigNum);
0294       //enableButton( Default, num==predefinedConfigNum );
0295       if(num<predefinedConfigNum)
0296          displayConfig(predefinedConfig[num]);
0297       else
0298          displayConfig(config);
0299       Q_EMIT changed();
0300    }
0301 }
0302 
0303 void ConfigSetup::displayConfig(const SConfig &cfg)
0304 {
0305    setValue(EditSunEnergy,cfg.sunEnergy);
0306    setValue(EditGravity,cfg.gravity);
0307 
0308    setValue(EditShotSpeed,cfg.shotSpeed);
0309    setValue(EditShotEnergyNeed,cfg.shotEnergyNeed);
0310    setValue(EditMaxBullets,cfg.maxBullets);
0311    setValue(EditBulletDamage,cfg.bulletDamage);
0312    setValue(EditBulletLifeTime,cfg.bulletLifeTime);
0313    setValue(EditBulletReloadTime,cfg.bulletReloadTime);
0314 
0315    setValue(EditMineFuel,cfg.mineFuel);
0316    setValue(EditMineEnergyNeed,cfg.mineEnergyNeed);
0317    setValue(EditMineActivateTime,cfg.mineActivateTime);
0318    setValue(EditMineDamage,cfg.mineDamage);
0319    setValue(EditMaxMines,cfg.maxMines);
0320    setValue(EditMineReloadTime,cfg.mineReloadTime);
0321 
0322    setValue(EditGamespeed,cfg.gamespeed);
0323 
0324    setValue(EditPosX,cfg.startPosX);
0325    setValue(EditPosY,cfg.startPosY);
0326    setValue(EditVelX,cfg.startVelX);
0327    setValue(EditVelY,cfg.startVelY);
0328 
0329    setValue(EditAcc,cfg.acc);
0330    setValue(EditEnergyNeed,cfg.energyNeed);
0331    setValue(EditRotationSpeed,cfg.rotationSpeed);
0332    setValue(EditRotationEnergyNeed,cfg.rotationEnergyNeed);
0333    setValue(EditShipDamage,cfg.shipDamage);
0334 
0335    setValue(EditPowerupLifeTime,cfg.powerupLifeTime);
0336    setValue(EditPowerupRefreshTime,cfg.powerupRefreshTime);
0337    setValue(EditPowerupShieldAmount,cfg.powerupShieldAmount);
0338    setValue(EditPowerupEnergyAmount,cfg.powerupEnergyAmount);
0339 }
0340 
0341 void ConfigSetup::setValue(int ednum,int val)
0342 {
0343    const QString str = QString::asprintf("%*i",LCDLen,val);
0344    value[ednum]->display(str);
0345    slider[ednum]->setValue(val);
0346 }
0347 
0348 void ConfigSetup::setValue(int ednum,double val)
0349 {
0350    QString str;
0351    int hval=(int)(val*EditDiv[ednum]+0.5);
0352    int n,h;
0353 
0354    if(EditDiv[ednum]==1)
0355       str = QString::asprintf("%*i",LCDLen,hval);
0356    else
0357    {
0358       h=1;
0359       for(n=0;h<EditDiv[ednum];++n)
0360          h*=10;
0361       str = QString::asprintf("%*.*f",LCDLen,n,val);
0362    }
0363    value[ednum]->display(str);
0364    slider[ednum]->setValue(hval);
0365 }
0366 
0367 void ConfigSetup::setValue(int ednum,unsigned val)
0368 {
0369    const QString str = QString::asprintf("%*i",LCDLen,(int)val);
0370    value[ednum]->display(str);
0371    slider[ednum]->setValue((int)val);
0372 }
0373 
0374 void ConfigSetup::sliderChanged(int val)
0375 {
0376    int i,n,h;
0377    QString str;
0378 
0379    for(i=0;(i<EditNum) && (slider[i]!=sender());i++);
0380    if(i<EditNum)
0381    {
0382       if(EditDiv[i]==1)
0383          str = QString::asprintf("%*i",LCDLen,val);
0384       else
0385       {
0386          h=1;
0387          for(n=0;h<EditDiv[i];++n)
0388             h*=10;
0389          str = QString::asprintf("%*.*f",LCDLen,n,(double)val/EditDiv[i]);
0390       }
0391       value[i]->display(str);
0392       if(VarType[i]==VarFloat)
0393          valueChanged(i,(double)val/EditDiv[i]);
0394       else
0395          valueChanged(i,val);
0396    }
0397 
0398 }
0399 
0400 SettingsDialog::SettingsDialog(SConfig *customConfig, QWidget *parent, const char *name)
0401   : KConfigDialog( parent,QLatin1String(  name ), Options::self())
0402 {
0403   General *general = new General();
0404   addPage(general, i18nc("general settings","General"), QStringLiteral( "games-config-options" ), i18n("General Settings"));
0405 
0406   cs = new ConfigSetup(customConfig);
0407   addPage(cs, i18nc("game settings","Game"),QStringLiteral(  "games-config-custom" ), i18n("Game Settings"));
0408   connect(cs, &ConfigSetup::changed, this, &SettingsDialog::updateButtons);
0409   setHelp(QStringLiteral( "options-configurations" ), QStringLiteral( "kspaceduel" ));
0410 //  resize(600,400);
0411 }
0412 
0413 SettingsDialog::~SettingsDialog()
0414 {
0415 }
0416 
0417 void SettingsDialog::updateWidgetsDefault()
0418 {
0419    cs->updateWidgetsDefault();
0420 }
0421 
0422 void SettingsDialog::updateWidgets()
0423 {
0424   cs->updateWidgets();
0425 }
0426 
0427 void SettingsDialog::updateSettings()
0428 {
0429    cs->updateSettings();
0430    Q_EMIT settingsUpdated();
0431 }
0432 
0433 bool SettingsDialog::hasChanged()
0434 {
0435    return cs->hasChanged();
0436 }
0437 
0438 bool SettingsDialog::isDefault()
0439 {
0440    return cs->isDefault();
0441 }
0442 
0443