-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathTr2DynamicBinding.cpp
More file actions
264 lines (238 loc) · 5.56 KB
/
Copy pathTr2DynamicBinding.cpp
File metadata and controls
264 lines (238 loc) · 5.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// Copyright © 2019 CCP ehf.
#include "StdAfx.h"
#include "Tr2DynamicBinding.h"
#include "ITr2DynamicBindingOwner.h"
#include "TriValueBinding.h"
#include <regex>
namespace
{
std::pair<const Be::VarEntry*, Be::Var*> FindEntry( IRoot* object, const char* name )
{
if( !object )
{
return std::make_pair( nullptr, nullptr );
}
auto type = object->ClassType();
ssize_t offs = 0;
// Loop over all entries - this double loop covers chaining
for( ; type; offs += type->mOffsetToParent, type = type->mParentClassInfo )
{
for( const Be::VarEntry* entry = type->mMemberTable; entry->mName; entry++ )
{
if( !entry->mGetProperty && strcmp( entry->mName, name ) == 0 )
{
return std::make_pair( entry, BLUEMAPMEMBEROFFSET( object, entry, type, offs ) );
}
}
}
return std::make_pair( nullptr, nullptr );
}
IRoot* GetIRootAttribute( IRoot* object, const std::string& name )
{
auto entry = FindEntry( object, name.c_str() );
if( !entry.second )
{
return nullptr;
}
if( entry.first->mSize && ( entry.first->mType == Be::IROOT || entry.first->mType == Be::IROOTPTR ) )
{
if( entry.first->mType == Be::IROOTPTR )
{
return entry.second->mIRootPtr;
}
else
{
return reinterpret_cast<IRoot*>( entry.second );
}
}
return nullptr;
}
bool GetStringAttribute( IRoot* object, const std::string& name, std::string& value )
{
auto entry = FindEntry( object, name.c_str() );
if( !entry.second )
{
return false;
}
switch( entry.first->mType )
{
case Be::CHARARRAY:
value = reinterpret_cast<const char*>( entry.second );
return true;
case Be::CSTRING:
value = reinterpret_cast<const char*>( entry.second->mCharPtr );
return true;
case Be::STDSTRING:
value = *reinterpret_cast<const std::string*>( entry.second );
return true;
case Be::SHAREDSTRING:
value = reinterpret_cast<BlueSharedString*>( entry.second )->c_str();
return true;
default:
return false;
}
}
IRoot* GetListElement( IRoot* object, ssize_t index )
{
if( IListPtr list = BlueCastPtr( object ) )
{
if( index < 0 )
{
index += int( list->GetSize() );
}
if( index >= 0 && index < list->GetSize() )
{
return list->GetAt( index );
}
}
return nullptr;
}
IRoot* GetListElement( IRoot* object, const std::string& name )
{
if( IListPtr list = BlueCastPtr( object ) )
{
auto size = list->GetSize();
for( ssize_t i = 0; i < size; ++i )
{
auto element = list->GetAt( i );
std::string elementName;
if( GetStringAttribute( element, "name", elementName ) && elementName == name )
{
return element;
}
}
}
return nullptr;
}
IRoot* ResolveReference( const std::string& reference, const std::unordered_map<std::string, IRoot*>& roots )
{
if( reference.empty() )
{
return nullptr;
}
static const std::regex tokens( "((\\.[a-zA-Z_][a-zA-Z_0-9]*)|(\\[-?[0-9]+\\])|(\\[\"[^\"]*\"\\])).*" );
static const std::regex root( "([a-zA-Z_][a-zA-Z_0-9]*).*" );
auto start = begin( reference );
auto finish = end( reference );
std::smatch match;
if( !std::regex_match( start, finish, match, root ) )
{
return nullptr;
}
auto found = roots.find( match[1].str() );
if( found == roots.end() )
{
return nullptr;
}
auto object = found->second;
start += match[1].length();
while( object && start != finish )
{
if( !std::regex_match( start, finish, match, tokens ) )
{
return nullptr;
}
if( match[2].length() )
{
auto attrName = match[2].str().substr( 1 );
object = GetIRootAttribute( object, attrName );
}
else if( match[3].length() )
{
auto index = std::atoi( match[3].str().c_str() + 1 );
object = GetListElement( object, ssize_t( index ) );
}
else if( match[4].length() )
{
auto name = match[4].str();
name = name.substr( 2, name.length() - 4 );
object = GetListElement( object, name );
}
else
{
object = nullptr;
}
start += match[1].length();
}
return object;
}
}
Tr2DynamicBinding::Tr2DynamicBinding( IRoot* lockobj ) :
m_destination( nullptr ),
m_source( nullptr ),
m_owner( nullptr ),
m_scale( 1.0f ),
m_bindingDelay( 0L ),
m_bindingTime( 0 )
{
m_binding = nullptr;
BeOS->RegisterForSimTimeRebase( this );
}
Tr2DynamicBinding::~Tr2DynamicBinding()
{
BeOS->UnregisterForSimTimeRebase( this );
m_owner = nullptr;
Unlink();
}
bool Tr2DynamicBinding::OnModified( Be::Var* value )
{
if( m_owner != nullptr )
{
Link();
}
else
{
Unlink();
}
return true;
}
void Tr2DynamicBinding::OnSimClockRebase( Be::Time oldTime, Be::Time newTime )
{
m_bindingTime += ( newTime - oldTime );
}
bool Tr2DynamicBinding::IsSourceValid() const
{
return m_source != nullptr;
}
bool Tr2DynamicBinding::IsDestinationValid() const
{
return m_destination != nullptr;
}
void Tr2DynamicBinding::Update( Be::Time time )
{
if( m_binding != nullptr && m_bindingTime <= time )
{
m_binding->CopyValue();
}
}
void Tr2DynamicBinding::Link()
{
Unlink();
if( m_owner == nullptr )
{
return;
}
auto parameters = m_owner->GetParameterMap();
m_destination = ResolveReference( m_destinationObjectPath.c_str(), parameters );
m_source = ResolveReference( m_sourceObjectPath.c_str(), parameters );
if( IsSourceValid() && IsDestinationValid() )
{
m_binding.CreateInstance();
m_binding->CreateWeakBinding( m_source, m_sourceObjectAttribute.c_str(), m_destination, m_destinationObjectAttribute.c_str(), m_scale );
if( m_bindingTime == 0 )
{
m_bindingTime = BeOS->GetCurrentFrameTime() + TimeFromMS( m_bindingDelay );
}
}
}
void Tr2DynamicBinding::Unlink()
{
m_binding = nullptr;
m_source = nullptr;
m_destination = nullptr;
m_bindingTime = 0;
}
void Tr2DynamicBinding::SetOwner( ITr2DynamicBindingOwner* owner )
{
m_owner = owner;
}