Commit 4af83bbb authored by Waldo Bastian's avatar Waldo Bastian
Browse files

Use KConfigSkeleton based settings.

svn path=/trunk/kdegames/ktron/; revision=255734
parent 889fa7a5
......@@ -2,7 +2,7 @@
bin_PROGRAMS = ktron
INCLUDES = -I$(top_srcdir)/libkdegames $(all_includes)
ktron_SOURCES = ai.ui appearance.ui general.ui tron.cpp player.cpp ktron.cpp main.cpp
ktron_SOURCES = ai.ui appearance.ui general.ui tron.cpp player.cpp ktron.cpp main.cpp settings.cpp
ktron_LDFLAGS = $(all_libraries) $(KDE_RPATH)
ktron_LDADD = $(LIB_KIO) $(LIB_KDEGAMES)
......@@ -19,3 +19,9 @@ rc_DATA = ktronui.rc
messages: rc.cpp
$(XGETTEXT) *.cpp -o $(podir)/ktron.pot
# avoid running the below command in parallel
settings.cpp: settings.h
settings.cpp settings.h: $(srcdir)/ktron.kcfg $(srcdir)/settings.kcfg-codegen
kconfig_compiler $(srcdir)/ktron.kcfg $(srcdir)/settings.kcfg-codegen
......@@ -52,18 +52,15 @@
</property>
<widget class="QCheckBox">
<property name="name">
<cstring>Computerplayer1</cstring>
<cstring>kcfg_Computerplayer1</cstring>
</property>
<property name="text">
<string>Player &amp;1</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
<widget class="QCheckBox">
<property name="name">
<cstring>Computerplayer2</cstring>
<cstring>kcfg_Computerplayer2</cstring>
</property>
<property name="text">
<string>Player &amp;2</string>
......@@ -113,7 +110,7 @@
</property>
</item>
<property name="name">
<cstring>Skill</cstring>
<cstring>kcfg_Skill</cstring>
</property>
<property name="currentItem">
<number>1</number>
......
......@@ -69,7 +69,7 @@
</property>
</item>
<property name="name">
<cstring>Style</cstring>
<cstring>kcfg_Style</cstring>
</property>
</widget>
<widget class="QGroupBox" row="1" column="0" rowspan="1" colspan="3">
......@@ -118,7 +118,7 @@
</widget>
<widget class="QSlider" row="0" column="0" rowspan="1" colspan="3">
<property name="name">
<cstring>RectSize</cstring>
<cstring>kcfg_RectSize</cstring>
</property>
<property name="minValue">
<number>4</number>
......@@ -149,7 +149,7 @@
</widget>
<widget class="QButtonGroup" row="2" column="0" rowspan="1" colspan="3">
<property name="name">
<cstring>BackgroundGroup</cstring>
<cstring>kcfg_BackgroundImageChoice</cstring>
</property>
<property name="title">
<string>Background</string>
......@@ -171,7 +171,7 @@
</widget>
<widget class="KURLRequester" row="1" column="1">
<property name="name">
<cstring>BackgroundImage</cstring>
<cstring>kcfg_BackgroundImage</cstring>
</property>
<property name="enabled">
<bool>false</bool>
......@@ -184,13 +184,10 @@
<property name="text">
<string>Image:</string>
</property>
<property name="checked">
<bool>false</bool>
</property>
</widget>
<widget class="KColorButton" row="0" column="1">
<property name="name">
<cstring>Color_Background</cstring>
<cstring>kcfg_Color_Background</cstring>
</property>
<property name="enabled">
<bool>true</bool>
......@@ -218,7 +215,7 @@
</widget>
<widget class="KColorButton" row="3" column="1" rowspan="1" colspan="2">
<property name="name">
<cstring>Color_Player1</cstring>
<cstring>kcfg_Color_Player1</cstring>
</property>
<property name="text">
<string></string>
......@@ -241,7 +238,7 @@
</widget>
<widget class="KColorButton" row="4" column="1" rowspan="1" colspan="2">
<property name="name">
<cstring>Color_Player2</cstring>
<cstring>kcfg_Color_Player2</cstring>
</property>
<property name="text">
<string></string>
......@@ -279,21 +276,21 @@
<connection>
<sender>BackgroundColorChoice</sender>
<signal>toggled(bool)</signal>
<receiver>Color_Background</receiver>
<receiver>kcfg_Color_Background</receiver>
<slot>setEnabled(bool)</slot>
</connection>
<connection>
<sender>BackgroundImageChoice</sender>
<signal>toggled(bool)</signal>
<receiver>BackgroundImage</receiver>
<receiver>kcfg_BackgroundImage</receiver>
<slot>setEnabled(bool)</slot>
</connection>
</connections>
<tabstops>
<tabstop>Style</tabstop>
<tabstop>RectSize</tabstop>
<tabstop>Color_Player1</tabstop>
<tabstop>Color_Player2</tabstop>
<tabstop>kcfg_Style</tabstop>
<tabstop>kcfg_RectSize</tabstop>
<tabstop>kcfg_Color_Player1</tabstop>
<tabstop>kcfg_Color_Player2</tabstop>
</tabstops>
<layoutdefaults spacing="6" margin="11"/>
<includehints>
......@@ -301,7 +298,5 @@
<includehint>klineedit.h</includehint>
<includehint>kpushbutton.h</includehint>
<includehint>kcolorbutton.h</includehint>
<includehint>kcolorbutton.h</includehint>
<includehint>kcolorbutton.h</includehint>
</includehints>
</UI>
......@@ -52,18 +52,15 @@
</property>
<widget class="QCheckBox">
<property name="name">
<cstring>ChangeWinnerColor</cstring>
<cstring>kcfg_ChangeWinnerColor</cstring>
</property>
<property name="text">
<string>&amp;Show winner by changing color</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
<widget class="QCheckBox">
<property name="name">
<cstring>AcceleratorBlocked</cstring>
<cstring>kcfg_AcceleratorBlocked</cstring>
</property>
<property name="text">
<string>&amp;Disable acceleration</string>
......@@ -71,7 +68,7 @@
</widget>
<widget class="QCheckBox">
<property name="name">
<cstring>OppositeDirCrashes</cstring>
<cstring>kcfg_OppositeDirCrashes</cstring>
</property>
<property name="text">
<string>&amp;Crash when moving in the opposite direction</string>
......@@ -92,12 +89,12 @@
</property>
<widget class="QLineEdit" row="0" column="1">
<property name="name">
<cstring>Name_Pl1</cstring>
<cstring>kcfg_NamePlayer1</cstring>
</property>
</widget>
<widget class="QLineEdit" row="1" column="1">
<property name="name">
<cstring>Name_Pl2</cstring>
<cstring>kcfg_NamePlayer2</cstring>
</property>
</widget>
<widget class="QLabel" row="0" column="0">
......@@ -131,7 +128,7 @@
</property>
<widget class="QSlider">
<property name="name">
<cstring>Velocity</cstring>
<cstring>kcfg_Velocity</cstring>
</property>
<property name="minValue">
<number>1</number>
......
......@@ -21,9 +21,9 @@
#include "ktron.h"
#include <kconfigdialog.h>
#include <klocale.h>
#include <kkeydialog.h>
#include <kconfig.h>
#include <kmessagebox.h>
#include <kaction.h>
#include <kstdgameaction.h>
......@@ -31,10 +31,10 @@
#include <kstatusbar.h>
// Settings
#include "settings.h"
#include "general.h"
#include "ai.h"
#include "appearance.h"
#include <kautoconfigdialog.h>
#define ID_STATUS_BASE 40
#define MESSAGE_TIME 2000
......@@ -88,12 +88,10 @@ KTron::KTron(QWidget *parent, const char *name) : KMainWindow(parent, name) {
}
void KTron::loadSettings() {
KConfig *config=kapp->config();
config->setGroup("Game");
playerName[0]=config->readEntry("Name_Pl1");
playerName[0]=Settings::namePlayer1();
if ( playerName[0].isEmpty() )
playerName[0] = i18n("Player 1");
playerName[1]=config->readEntry("Name_Pl2");
playerName[1]=Settings::namePlayer2();
if ( playerName[1].isEmpty() )
playerName[1] = i18n("Player 2");
......@@ -185,13 +183,13 @@ void KTron::configureKeys(){
* Show Settings dialog.
*/
void KTron::showSettings(){
if(KAutoConfigDialog::showDialog("settings"))
if(KConfigDialog::showDialog("settings"))
return;
KAutoConfigDialog *dialog = new KAutoConfigDialog(this, "settings");
dialog->addPage(new General(0, "General"), i18n("General"), "General", "package_settings");
dialog->addPage(new Ai(0, "Ai"), i18n("Ai"), "Game", "package_system");
dialog->addPage(new Appearance(0, "Appearance"), i18n("Appearance"), "Game", "style");
KConfigDialog *dialog = new KConfigDialog(this, "settings", Settings::self());
dialog->addPage(new General(0, "General"), i18n("General"), "package_settings");
dialog->addPage(new Ai(0, "Ai"), i18n("Ai"), "package_system");
dialog->addPage(new Appearance(0, "Appearance"), i18n("Appearance"), "style");
connect(dialog, SIGNAL(settingsChanged()), tron, SLOT(loadSettings()));
connect(dialog, SIGNAL(settingsChanged()), this, SLOT(loadSettings()));
dialog->show();
......
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE kcfg SYSTEM "http://www.kde.org/standards/kcfg/1.0/kcfg.dtd">
<kcfg>
<kcfgfile name="ktronrc"/>
<group name="Game">
<entry name="Color_Background" type="QColor">
<desc>The background color of the game.</desc>
<default>black</default>
</entry>
<entry name="Color_Player1" type="QColor">
<desc>The color of player 1</desc>
<default>red</default>
</entry>
<entry name="Color_Player2" type="QColor">
<desc>The color of player 2</desc>
<default>blue</default>
</entry>
<entry name="ChangeWinnerColor" type="bool">
<desc>Whether to show the winner by changing color.</desc>
<default>true</default>
</entry>
<entry name="AcceleratorBlocked" type="bool">
<desc>Whether to disable acceleration.</desc>
<default>false</default>
</entry>
<entry name="OppositeDirCrashes" type="bool">
<desc>Whether changing in the opposite direction causes a crash.</desc>
<default>false</default>
</entry>
<entry name="RectSize" type="int">
<desc>The width of the line.</desc>
<default>10</default>
</entry>
<entry name="Velocity" type="int">
<desc>The speed of the line.</desc>
<default>5</default>
</entry>
<entry name="Skill" type="Enum">
<desc>The skill of the computer player.</desc>
<default>Medium</default>
<values>
<value>Easy</value>
<value>Medium</value>
<value>Hard</value>
</values>
</entry>
<entry name="Style" type="Enum">
<desc>The line style.</desc>
<default>OLine</default>
<values>
<value>OLine</value>
<value>ORect</value>
<value>Line</value>
<value>Circle</value>
</values>
</entry>
<entry name="BackgroundImageChoice" type="bool">
<desc>Whether to use a custom background image.</desc>
<default>false</default>
</entry>
<entry name="BackgroundImage" type="Path">
<desc>Custom background image to use.</desc>
</entry>
<entry name="Computerplayer1" type="bool">
<desc>Whether player 1 is a computer player.</desc>
<default>true</default>
</entry>
<entry name="Computerplayer2" type="bool">
<desc>Whether player 2 is a computer player.</desc>
<default>false</default>
</entry>
<entry name="NamePlayer1" type="QString" key="Name_Pl1">
<desc>The name of player 1.</desc>
</entry>
<entry name="NamePlayer2" type="QString" key="Name_Pl2">
<desc>The name of player 2.</desc>
</entry>
</group>
</kcfg>
\ No newline at end of file
# Code generation options for kconfig_compiler
ClassName=Settings
Singleton=true
Mutators=false
......@@ -32,6 +32,7 @@
#include <kcolordialog.h>
#include <kaction.h>
#include "settings.h"
#include "tron.h"
#define TRON_FRAMESIZE 2
......@@ -63,40 +64,21 @@ Tron::Tron(QWidget *parent,const char *name)
}
void Tron::loadSettings(){
KConfig *config = kapp->config();
config->setGroup("Game");
// Colors
QColor bg("black");
QColor pl1("red");
QColor pl2("blue");
colors[0] = config->readColorEntry("Color_Background",&bg);
colors[1] = config->readColorEntry("Color_Player1",&pl1);
colors[2] = config->readColorEntry("Color_Player2",&pl2);
reset();
setPalette(colors[0]);
changeWinnerColor = config->readBoolEntry("ChangeWinnerColor",true);
blockAccelerator = config->readBoolEntry("AcceleratorBlocked",false);
crashOnOppositeDir=config->readBoolEntry("OppositeDirCrashes",false);
setPalette(Settings::color_Background());
// Size
int newSize = config->readNumEntry("RectSize",10);
int newSize = Settings::rectSize();
if(newSize!=rectSize){
rectSize=newSize;
createNewPlayfield();
reset();
}
// Velocity
int velocity=config->readNumEntry("Velocity",5);
setVelocity(velocity);
reset();
// Skill
_skill=(Skill)config->readNumEntry("Skill",(int)Medium);
// Velocity
setVelocity( Settings::velocity() );
// Style
style=(TronStyle)config->readNumEntry("Style",(int) OLine);
if(pixmap){
updatePixmap();
repaint();
......@@ -104,8 +86,8 @@ void Tron::loadSettings(){
// Backgroundimage
setBackgroundPix(NULL);
if(config->readBoolEntry("BackgroundImageChoice", false)){
KURL url = config->readPathEntry("BackgroundImage");
if(Settings::backgroundImageChoice()){
KURL url = Settings::backgroundImage();
if(!url.isEmpty()){
QString tmpFile;
KIO::NetAccess::download(url, tmpFile, this);
......@@ -121,8 +103,8 @@ void Tron::loadSettings(){
}
else setBackgroundPix(NULL);
}
setComputerplayer(One,config->readBoolEntry("Computerplayer1",true));
setComputerplayer(Two,config->readBoolEntry("Computerplayer2",false));
setComputerplayer(One, Settings::computerplayer1());
setComputerplayer(Two, Settings::computerplayer2());
}
Tron::~Tron()
......@@ -155,7 +137,7 @@ void Tron::createNewPlayfield()
playfield[i].resize(fieldHeight);
pixmap=new QPixmap(size());
pixmap->fill(colors[0]);
pixmap->fill(Settings::color_Background());
//int min=(fieldWidth<fieldHeight) ? fieldWidth : fieldHeight;
//lookForward=min/4;
......@@ -257,7 +239,7 @@ void Tron::showWinner(Player player)
{
int i,j;
if(player != Both && changeWinnerColor)
if(player != Both && Settings::changeWinnerColor())
{
int winner;
int loser;
......@@ -314,7 +296,7 @@ void Tron::updatePixmap()
}
else
{
pixmap->fill(colors[0]);
pixmap->fill(Settings::color_Background());
}
QPainter p;
......@@ -372,12 +354,12 @@ void Tron::drawRect(QPainter & p, int x, int y)
int player;
if(type&PLAYER1) // check player bit
{
toDraw=colors[1];
toDraw=Settings::color_Player1();
player=0;
}
else if(type&PLAYER2)
{
toDraw=colors[2];
toDraw=Settings::color_Player2();
player=1;
}
else
......@@ -386,14 +368,14 @@ void Tron::drawRect(QPainter & p, int x, int y)
return;
}
switch(style)
switch(Settings::style())
{
case Line:
case Settings::EnumStyle::Line:
p.setBrush(toDraw);
p.setPen(toDraw);
p.drawRect(xOffset,yOffset,rectSize,rectSize);
break;
case OLine:
case Settings::EnumStyle::OLine:
{
p.setBrush(toDraw);
p.setPen(toDraw);
......@@ -419,12 +401,12 @@ void Tron::drawRect(QPainter & p, int x, int y)
break;
}
case Circle:
case Settings::EnumStyle::Circle:
p.setBrush(toDraw);
p.setPen(toDraw);
p.drawEllipse(xOffset ,yOffset ,rectSize,rectSize);
break;
case ORect:
case Settings::EnumStyle::ORect:
p.setBrush(toDraw);
p.setPen(toDraw.light());
p.drawRect(xOffset,yOffset,rectSize,rectSize);
......@@ -456,37 +438,6 @@ void Tron::setBackgroundPix(QPixmap pix)
}
}
// configure colors
// id = 0 = background, 1, and 2
bool Tron::changeColor(int id)
{
QColor color;
int result=KColorDialog::getColor(color);
bool flag=false;
if(result) // if button 'ok' pressed
{
// backgroundcolor changed
if(id==0)
{
colors[0]=color;
setPalette(color);
bgPix.resize(0,0);
}
else // set other color
{
colors[id]=color;
}
updatePixmap();
repaint();
flag=true;
}
return flag;
}
void Tron::setVelocity(int newVel) // set new velocity
{
velocity=(10-newVel)*15;
......@@ -495,11 +446,6 @@ void Tron::setVelocity(int newVel) // set new velocity
timer->changeInterval(velocity);
}
int Tron::getVelocity() const
{
return 10-velocity/15;
}
void Tron::setComputerplayer(Player player, bool flag) {
if(player==One)
players[0].setComputer(flag);
......@@ -552,7 +498,7 @@ void Tron::switchDir(int playerNr,Direction newDirection)
return;
}
if (crashOnOppositeDir==false)
if (Settings::oppositeDirCrashes()==false)
{
if (newDirection==::Up && players[playerNr].last_dir==::Down)
return;
......@@ -732,7 +678,7 @@ void Tron::keyPressEvent(QKeyEvent *e)
}
else if(actionCollection->action("Pl2Ac")->shortcut().contains(key))
{
if(!blockAccelerator)
if(!Settings::acceleratorBlocked())
players[1].accelerated=true;
}
......@@ -762,7 +708,7 @@ void Tron::keyPressEvent(QKeyEvent *e)
}
else if(actionCollection->action("Pl1Ac")->shortcut().contains(key))
{
if(!blockAccelerator)
if(!Settings::acceleratorBlocked())
players[0].accelerated=true;
}
}
......@@ -1139,7 +1085,7 @@ void Tron::doMove()
// xtron-1.1 by Rhett D. Jacobs <rhett@hotel.canberra.edu.au>
void Tron::think(int playerNr)
{
if(_skill != Easy)
if(Settings::skill() != Settings::EnumSkill::Easy)
{
int opponent=(playerNr==1)? 0 : 1;
......@@ -1310,11 +1256,21 @@ if(_skill != Easy)
}
int doPercentage=100;
if(_skill==Medium)
int doPercentage = 100;
switch(Settings::skill())
{
case Settings::EnumSkill::Easy:
// Never reached
break;
case Settings::EnumSkill::Medium:
doPercentage=5;
else if(_skill==Hard)
break;
case Settings::EnumSkill::Hard:
doPercentage=90;
break;
}
// if opponent moves the opposite direction as we
if(opMovesOppositeDir)
......@@ -1533,7 +1489,7 @@ if(_skill != Easy)
}
// This part is completely ported from
// xtron-1.1 by Rhett D. Jacobs <rhett@hotel.canberra.edu.au>
else //_skill==Easy
else // Settings::skill() == Settings::EnumSkill::Easy
{
Direction sides[2];
int flags[6] = {0,0,0,0,0,0};
......
......@@ -37,13 +37,10 @@ class KActionCollection;
#include "player.h"
enum TronStyle{OLine=0,ORect=1 , Line=2 , Circle=3};
enum Player{One,Two,Both,Nobody};
// Bits that defines the rect and which sides to draw
enum {BACKGROUND=0, PLAYER1=1,PLAYER2=2,TOP=4,BOTTOM=8,LEFT=16,RIGHT=32};
enum Skill{Easy=0,Medium=1,Hard=2};
/**
* @short The playingfield
*/
......@@ -57,13 +54,10 @@ public:
void setActionCollection(KActionCollection*);
void updatePixmap();
void setBackgroundPix(QPixmap);
bool changeColor(int player);
void setComputerplayer(Player player, bool);
bool isComputer(Player player);
void setVelocity(int);
int getVelocity() const;
void setRectSize(int newSize);
int getRectSize() const;
public slots:
/** Starts a new game. The difference to reset is, that the players
......@@ -116,30 +110,19 @@ private:
int fieldWidth;
QTimer *timer;