KDEGames

kgameproperty.cpp
1/*
2 This file is part of the KDE games library
3 SPDX-FileCopyrightText: 2001 Andreas Beckermann <b_mann@gmx.de>
4 SPDX-FileCopyrightText: 2001 Martin Heni <kde at heni-online.de>
5
6 SPDX-License-Identifier: LGPL-2.0-only
7*/
8
9#include "kgameproperty.h"
10
11// own
12#include "kgame.h"
13#include "kgamemessage.h"
14#include "kgamepropertyhandler.h"
15#include "kplayer.h"
16#include <kdegamesprivate_kgame_logging.h>
17
18#define KPLAYERHANDLER_LOAD_COOKIE 6239
19
21{
22 init();
23 registerData(id, parent);
24}
25
27{
28 init();
29 registerData(id, parent);
30}
31
33{
34 init();
35 registerData(id, owner);
36}
37
42
43KGamePropertyBase::~KGamePropertyBase()
44{
45 unregisterData();
46}
47
48void KGamePropertyBase::init()
49{
50 mOwner = nullptr;
51 setDirty(false);
52
53 // this is very useful and used by e.g. KGameDialog so
54 // it is activated by default. Big games may profit by deactivating it to get
55 // a better performance.
57
58 setOptimized(false);
59
60 // setReadOnly(false);
61 mFlags.bits.locked = false; // setLocked(false); is NOT possible as it checks whether isLocked() allows to change the status
62
63 // local is default
64 setPolicy(PolicyLocal);
65}
66
67int KGamePropertyBase::registerData(int id, KGame *owner, const QString &name)
68{
69 return registerData(id, owner->dataHandler(), name);
70}
71
72int KGamePropertyBase::registerData(int id, KPlayer *owner, const QString &name)
73{
74 return registerData(id, owner->dataHandler(), name);
75}
76
78{
79 return registerData(-1, owner, p, name);
80}
81
83{
84 return registerData(id, owner, PolicyUndefined, name);
85}
86
88{
89 // we don't support changing the id
90 if (!owner) {
91 qCWarning(KDEGAMESPRIVATE_KGAME_LOG) << "Resetting owner=0. Sure you want to do this?";
92 mOwner = nullptr;
93 return -1;
94 }
95 if (!mOwner) {
96 if (id == -1) {
97 id = owner->uniquePropertyId();
98 }
99 mId = id;
100 mOwner = owner;
101 mOwner->addProperty(this, name);
102 if (p != PolicyUndefined) {
103 setPolicy(p);
104 } else {
105 setPolicy(mOwner->policy());
106 }
107 }
108 return mId;
109}
110
111void KGamePropertyBase::unregisterData()
112{
113 if (!mOwner) {
114 return;
115 }
116 mOwner->removeProperty(this);
117 mOwner = nullptr;
118}
119
121{
122 QByteArray b;
125 save(s);
126 if (mOwner) {
127 return mOwner->sendProperty(s);
128 } else {
129 qCCritical(KDEGAMESPRIVATE_KGAME_LOG) << "Cannot send because there is no receiver defined";
130 return false;
131 }
132}
133
135{
136 QByteArray b;
139 s.writeRawData(data.data(), data.size());
140 if (mOwner) {
141 return mOwner->sendProperty(s);
142 } else {
143 qCCritical(KDEGAMESPRIVATE_KGAME_LOG) << ": Cannot send because there is no receiver defined";
144 return false;
145 }
146}
147
149{
150 if (isLocked()) {
151 return false;
152 }
153 setLock(true);
154 return true;
155}
156
158{
159 if (isLocked() && !force) {
160 return false;
161 }
162 setLock(false);
163 return true;
164}
165
167{
168 QByteArray b;
170 KGameMessage::createPropertyCommand(s, IdCommand, id(), CmdLock);
171
172 s << (qint8)l;
173 if (mOwner) {
174 mOwner->sendProperty(s);
175 } else {
176 qCCritical(KDEGAMESPRIVATE_KGAME_LOG) << ": Cannot send because there is no receiver defined";
177 return;
178 }
179}
180
182{
183 // qCDebug(KDEGAMESPRIVATE_KGAME_LOG) << ": mOwnerP="<< mOwner << "id=" << id();
184 if (mOwner) {
185 mOwner->emitSignal(this);
186 } else {
187 qCCritical(KDEGAMESPRIVATE_KGAME_LOG) << ":id=" << id() << " Cannot emitSignal because there is no handler set";
188 }
189}
190
191void KGamePropertyBase::command(QDataStream &s, int cmd, bool isSender)
192{
193 switch (cmd) {
194 case CmdLock: {
195 if (!isSender) {
196 qint8 locked;
197 s >> locked;
198 mFlags.bits.locked = (bool)locked;
199 break;
200 }
201 }
202 default: // probably in derived classes
203 break;
204 }
205}
static void createPropertyHeader(QDataStream &msg, int id)
Creates a property header given the property id.
static void createPropertyCommand(QDataStream &msg, int cmdid, int pid, int cmd)
Creates a property header given the property id.
KGamePropertyBase()
Creates a KGamePropertyBase object without an owner.
void setOptimized(bool p)
Sets this property to try to optimize signal and network handling by not sending it out when the prop...
void setEmittingSignal(bool p)
Sets this property to emit a signal on value changed.
PropertyPolicy
The policy of the property.
virtual void save(QDataStream &s)=0
Write the value into a stream.
void setLock(bool l)
A locked property can only be changed by the player who has set the lock.
void setDirty(bool d)
Sets the "dirty" flag of the property.
void setPolicy(PropertyPolicy p)
Changes the consistency policy of a property.
int registerData(int id, KGamePropertyHandler *owner, PropertyPolicy p, const QString &name=QString())
You have to register a KGamePropertyBase before you can use it.
bool sendProperty()
Forward the data to the owner of this property which then sends it over network.
bool lock()
A locked property can only be changed by the player who has set the lock.
bool unlock(bool force=false)
A locked property can only be changed by the player who has set the lock.
void emitSignal()
Causes the parent object to emit a signal on value change.
virtual void command(QDataStream &stream, int msgid, bool isSender=false)
send a command to advanced properties like arrays
bool isLocked() const
A locked property can only be changed by the player who has set the lock.
A collection class for KGameProperty objects.
bool sendProperty(QDataStream &s)
called by a property to send itself into the datastream.
int uniquePropertyId()
returns a unique property ID starting called usually with a base of KGamePropertyBase::IdAutomatic.
bool removeProperty(KGamePropertyBase *data)
Removes a property from the handler.
bool addProperty(KGamePropertyBase *data, const QString &name=QString())
Adds a KGameProperty property to the handler.
void emitSignal(KGamePropertyBase *data)
called by a property to emit a signal This call is simply forwarded to the parent object
KGamePropertyBase::PropertyPolicy policy()
Returns the default policy for this property handler.
The main KDE game object.
Definition kgame.h:47
KGamePropertyHandler * dataHandler() const
Returns a pointer to the KGame property handler.
Definition kgame.cpp:627
Base class for a game player.
Definition kplayer.h:60
KGamePropertyHandler * dataHandler()
Definition kplayer.cpp:232
char * data()
qsizetype size() const const
int writeRawData(const char *s, int len)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:49:34 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.