• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • libtdegames
 

libtdegames

  • libtdegames
  • kgame
kgameproperty.cpp
1 /*
2  This file is part of the TDE games library
3  Copyright (C) 2001 Andreas Beckermann (b_mann@gmx.de)
4  Copyright (C) 2001 Martin Heni (martin@heni-online.de)
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Library General Public
8  License version 2 as published by the Free Software Foundation.
9 
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Library General Public License for more details.
14 
15  You should have received a copy of the GNU Library General Public License
16  along with this library; see the file COPYING.LIB. If not, write to
17  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  Boston, MA 02110-1301, USA.
19 */
20 /*
21  $Id$
22 */
23 
24 #include "kgameproperty.h"
25 #include "kgamepropertyhandler.h"
26 #include "kgamemessage.h"
27 #include "kplayer.h"
28 #include "kgame.h"
29 
30 #define KPLAYERHANDLER_LOAD_COOKIE 6239
31 
32 KGamePropertyBase::KGamePropertyBase(int id, KGame* parent)
33 {
34  init();
35  registerData(id, parent);
36 }
37 
38 KGamePropertyBase::KGamePropertyBase(int id, KPlayer* parent)
39 {
40  init();
41  registerData(id, parent);
42 }
43 
44 KGamePropertyBase::KGamePropertyBase(int id, KGamePropertyHandler* owner)
45 {
46  init();
47  registerData(id, owner);
48 }
49 
50 KGamePropertyBase::KGamePropertyBase()
51 {
52  init();
53 }
54 
55 KGamePropertyBase::~KGamePropertyBase()
56 {
57  unregisterData();
58 }
59 
60 void KGamePropertyBase::init()
61 {
62  mOwner = 0;
63  setDirty(false);
64 
65  // this is very useful and used by e.g. KGameDialog so
66  // it is activated by default. Big games may profit by deactivating it to get
67  // a better performance.
68  setEmittingSignal(true);
69 
70  setOptimized(false);
71 
72  //setReadOnly(false);
73  mFlags.bits.locked = false ; // setLocked(false); is NOT possible as it checks whether isLocked() allows to change the status
74 
75  // local is default
76  setPolicy(PolicyLocal);
77 }
78 
79 int KGamePropertyBase::registerData(int id, KGame* owner, TQString name)
80 { return registerData(id, owner->dataHandler(), name); }
81 
82 int KGamePropertyBase::registerData(int id, KPlayer* owner, TQString name)
83 { return registerData(id, owner->dataHandler(), name); }
84 
85 int KGamePropertyBase::registerData( KGamePropertyHandler* owner,PropertyPolicy p, TQString name)
86 { return registerData(-1, owner,p, name); }
87 
88 int KGamePropertyBase::registerData(int id, KGamePropertyHandler* owner, TQString name)
89 { return registerData(id, owner,PolicyUndefined, name); }
90 
91 int KGamePropertyBase::registerData(int id, KGamePropertyHandler* owner,PropertyPolicy p, TQString name)
92 {
93 // we don't support changing the id
94  if (!owner) {
95  kdWarning(11001) << k_funcinfo << "Resetting owner=0. Sure you want to do this?" << endl;
96  mOwner=0;
97  return -1;
98  }
99  if (!mOwner) {
100  if (id==-1) {
101  id=owner->uniquePropertyId();
102  }
103  mId = id;
104  mOwner = owner;
105  mOwner->addProperty(this, name);
106  if (p!=PolicyUndefined) {
107  setPolicy(p);
108  } else {
109  setPolicy(mOwner->policy());
110  }
111  }
112  return mId;
113 }
114 
115 void KGamePropertyBase::unregisterData()
116 {
117  if (!mOwner) {
118  return;
119  }
120  mOwner->removeProperty(this);
121  mOwner = 0;
122 }
123 
124 bool KGamePropertyBase::sendProperty()
125 {
126  TQByteArray b;
127  TQDataStream s(b, IO_WriteOnly);
128  KGameMessage::createPropertyHeader(s, id());
129  save(s);
130  if (mOwner) {
131  return mOwner->sendProperty(s);
132  } else {
133  kdError(11001) << k_funcinfo << "Cannot send because there is no receiver defined" << endl;
134  return false;
135  }
136 }
137 
138 bool KGamePropertyBase::sendProperty(const TQByteArray& data)
139 {
140  TQByteArray b;
141  TQDataStream s(b, IO_WriteOnly);
142  KGameMessage::createPropertyHeader(s, id());
143  s.writeRawBytes(data.data(), data.size());
144  if (mOwner) {
145  return mOwner->sendProperty(s);
146  } else {
147  kdError(11001) << k_funcinfo << ": Cannot send because there is no receiver defined" << endl;
148  return false;
149  }
150 }
151 
152 bool KGamePropertyBase::lock()
153 {
154  if (isLocked()) {
155  return false;
156  }
157  setLock(true);
158  return true;
159 }
160 
161 bool KGamePropertyBase::unlock(bool force)
162 {
163  if (isLocked() && !force) {
164  return false;
165  }
166  setLock(false);
167  return true;
168 }
169 
170 void KGamePropertyBase::setLock(bool l)
171 {
172  TQByteArray b;
173  TQDataStream s(b, IO_WriteOnly);
174  KGameMessage::createPropertyCommand(s, IdCommand, id(), CmdLock);
175 
176  s << (TQ_INT8)l;
177  if (mOwner) {
178  mOwner->sendProperty(s);
179  } else {
180  kdError(11001) << k_funcinfo << ": Cannot send because there is no receiver defined" << endl;
181  return ;
182  }
183 }
184 
185 void KGamePropertyBase::emitSignal()
186 {
187  //kdDebug(11001) << k_funcinfo << ": mOwnerP="<< mOwner << " id=" << id() << endl;
188  if (mOwner ) {
189  mOwner->emitSignal(this);
190  } else {
191  kdError(11001) << k_funcinfo << ":id="<<id()<<" Cannot emitSignal because there is no handler set" << endl;
192  }
193 }
194 
195 void KGamePropertyBase::command(TQDataStream& s, int cmd, bool isSender)
196 {
197  switch (cmd) {
198  case CmdLock:
199  {
200  if (!isSender) {
201  TQ_INT8 locked;
202  s >> locked;
203  mFlags.bits.locked = (bool)locked ;
204  break;
205  }
206  }
207  default: // probably in derived classes
208  break;
209  }
210 }
211 
KGamePropertyBase::setEmittingSignal
void setEmittingSignal(bool p)
Sets this property to emit a signal on value changed.
Definition: kgameproperty.h:157
KGamePropertyHandler::sendProperty
bool sendProperty(TQDataStream &s)
called by a property to send itself into the datastream.
Definition: kgamepropertyhandler.cpp:322
KGamePropertyBase::save
virtual void save(TQDataStream &s)=0
Write the value into a stream.
KGamePropertyBase::sendProperty
bool sendProperty()
Forward the data to the owner of this property which then sends it over network.
Definition: kgameproperty.cpp:124
KGamePropertyHandler
A collection class for KGameProperty objects.
Definition: kgamepropertyhandler.h:72
KGamePropertyBase::setOptimized
void setOptimized(bool p)
Sets this property to try to optimize signal and network handling by not sending it out when the prop...
Definition: kgameproperty.h:169
KGamePropertyBase::setDirty
void setDirty(bool d)
Sets the "dirty" flag of the property.
Definition: kgameproperty.h:316
KGamePropertyBase::id
int id() const
Definition: kgameproperty.h:238
KGamePropertyHandler::removeProperty
bool removeProperty(KGamePropertyBase *data)
Removes a property from the handler.
Definition: kgamepropertyhandler.cpp:142
KGamePropertyBase::PropertyPolicy
PropertyPolicy
The policy of the property.
Definition: kgameproperty.h:109
KGamePropertyBase::emitSignal
void emitSignal()
Causes the parent object to emit a signal on value change.
Definition: kgameproperty.cpp:185
KGamePropertyBase::setLock
void setLock(bool l)
A locked property can only be changed by the player who has set the lock.
Definition: kgameproperty.cpp:170
KGamePropertyBase::command
virtual void command(TQDataStream &stream, int msgid, bool isSender=false)
send a command to advanced properties like arrays
Definition: kgameproperty.cpp:195
KGamePropertyHandler::emitSignal
void emitSignal(KGamePropertyBase *data)
called by a property to emit a signal This call is simply forwarded to the parent object ...
Definition: kgamepropertyhandler.cpp:303
KGamePropertyBase::unlock
bool unlock(bool force=false)
A locked property can only be changed by the player who has set the lock.
Definition: kgameproperty.cpp:161
KGamePropertyBase::setPolicy
void setPolicy(PropertyPolicy p)
Changes the consistency policy of a property.
Definition: kgameproperty.h:145
KGamePropertyBase::isLocked
bool isLocked() const
A locked property can only be changed by the player who has set the lock.
Definition: kgameproperty.h:187
KGamePropertyHandler::uniquePropertyId
int uniquePropertyId()
returns a unique property ID starting called usually with a base of KGamePropertyBase::IdAutomatic.
Definition: kgamepropertyhandler.cpp:264
KPlayer
Base class for a game player.
Definition: kplayer.h:69
KGamePropertyBase::KGamePropertyBase
KGamePropertyBase()
Creates a KGamePropertyBase object without an owner.
Definition: kgameproperty.cpp:50
KGamePropertyBase::lock
bool lock()
A locked property can only be changed by the player who has set the lock.
Definition: kgameproperty.cpp:152
KGamePropertyBase::registerData
int registerData(int id, KGamePropertyHandler *owner, PropertyPolicy p, TQString name=0)
You have to register a KGamePropertyBase before you can use it.
Definition: kgameproperty.cpp:91
KGamePropertyHandler::addProperty
bool addProperty(KGamePropertyBase *data, TQString name=0)
Adds a KGameProperty property to the handler.
Definition: kgamepropertyhandler.cpp:151
KGame
The main KDE game object.
Definition: kgame.h:62
KPlayer::dataHandler
KGamePropertyHandler * dataHandler()
Definition: kplayer.cpp:198
KGamePropertyHandler::policy
KGamePropertyBase::PropertyPolicy policy()
Returns the default policy for this property handler.
Definition: kgamepropertyhandler.cpp:227
KGame::dataHandler
KGamePropertyHandler * dataHandler() const
Returns a pointer to the KGame property handler.
Definition: kgame.cpp:689

libtdegames

Skip menu "libtdegames"
  • Main Page
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

libtdegames

Skip menu "libtdegames"
  • libtdegames
Generated for libtdegames by doxygen 1.8.13
This website is maintained by Timothy Pearson.