1
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
2
|
// Copyright 2021 Leszek Koltunski //
|
3
|
// //
|
4
|
// This file is part of Magic Cube. //
|
5
|
// //
|
6
|
// Magic Cube is free software: you can redistribute it and/or modify //
|
7
|
// it under the terms of the GNU General Public License as published by //
|
8
|
// the Free Software Foundation, either version 2 of the License, or //
|
9
|
// (at your option) any later version. //
|
10
|
// //
|
11
|
// Magic Cube is distributed in the hope that it will be useful, //
|
12
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
|
13
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
|
14
|
// GNU General Public License for more details. //
|
15
|
// //
|
16
|
// You should have received a copy of the GNU General Public License //
|
17
|
// along with Magic Cube. If not, see <http://www.gnu.org/licenses/>. //
|
18
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
19
|
|
20
|
package org.distorted.control;
|
21
|
|
22
|
import org.distorted.library.main.DistortedNode;
|
23
|
import org.distorted.library.message.EffectListener;
|
24
|
import org.distorted.main.RubikActivity;
|
25
|
import org.distorted.main.RubikPreRender;
|
26
|
import org.distorted.objects.TwistyObject;
|
27
|
|
28
|
import java.lang.ref.WeakReference;
|
29
|
|
30
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31
|
|
32
|
public class RubikControl implements EffectListener
|
33
|
{
|
34
|
WeakReference<RubikActivity> mRefAct;
|
35
|
boolean mWholeReturned, mRotateReturned;
|
36
|
|
37
|
RubikControlWhole mWhole;
|
38
|
RubikControlRotate mRotate;
|
39
|
|
40
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
41
|
|
42
|
private TwistyObject getObject()
|
43
|
{
|
44
|
RubikActivity act = mRefAct.get();
|
45
|
|
46
|
if( act!=null )
|
47
|
{
|
48
|
RubikPreRender pre = act.getPreRender();
|
49
|
return pre!=null ? pre.getObject() : null;
|
50
|
}
|
51
|
|
52
|
return null;
|
53
|
}
|
54
|
|
55
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
56
|
|
57
|
private void addWholeObjects()
|
58
|
{
|
59
|
TwistyObject object = getObject();
|
60
|
DistortedNode[] nodes = mWhole.getNodes();
|
61
|
|
62
|
if( object!=null && nodes!=null )
|
63
|
{
|
64
|
for (DistortedNode node : nodes) object.attach(node);
|
65
|
}
|
66
|
}
|
67
|
|
68
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
69
|
|
70
|
private void removeWholeObjects()
|
71
|
{
|
72
|
TwistyObject object = getObject();
|
73
|
DistortedNode[] nodes = mWhole.returnNodes();
|
74
|
|
75
|
if( object!=null && nodes!=null )
|
76
|
{
|
77
|
for (DistortedNode node : nodes) object.detach(node);
|
78
|
}
|
79
|
}
|
80
|
|
81
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
82
|
|
83
|
private void addRotateObjects()
|
84
|
{
|
85
|
TwistyObject object = getObject();
|
86
|
DistortedNode[] nodes = mRotate.getNodes();
|
87
|
|
88
|
if( object!=null && nodes!=null )
|
89
|
{
|
90
|
for (DistortedNode node : nodes) object.attach(node);
|
91
|
}
|
92
|
}
|
93
|
|
94
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
95
|
|
96
|
private void removeRotateObjects()
|
97
|
{
|
98
|
TwistyObject object = getObject();
|
99
|
DistortedNode[] nodes = mRotate.returnNodes();
|
100
|
|
101
|
if( object!=null && nodes!=null )
|
102
|
{
|
103
|
for (DistortedNode node : nodes) object.detach(node);
|
104
|
}
|
105
|
}
|
106
|
|
107
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
108
|
|
109
|
private void finishWhole()
|
110
|
{
|
111
|
removeWholeObjects();
|
112
|
|
113
|
mWholeReturned = true;
|
114
|
|
115
|
if( !mRotateReturned ) addRotateObjects();
|
116
|
}
|
117
|
|
118
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
119
|
|
120
|
private void finishRotate()
|
121
|
{
|
122
|
removeRotateObjects();
|
123
|
|
124
|
mRotateReturned= true;
|
125
|
|
126
|
RubikActivity act = mRefAct.get();
|
127
|
if( act!=null ) act.unblockEverything();
|
128
|
}
|
129
|
|
130
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
131
|
|
132
|
RubikActivity getActivity()
|
133
|
{
|
134
|
return mRefAct.get();
|
135
|
}
|
136
|
|
137
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
138
|
// PUBLIC
|
139
|
|
140
|
public RubikControl()
|
141
|
{
|
142
|
mWhole = new RubikControlWhole(this);
|
143
|
mRotate= new RubikControlRotate(this);
|
144
|
}
|
145
|
|
146
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
147
|
|
148
|
public void effectFinished(long effectID)
|
149
|
{
|
150
|
if( effectID==mWhole.getEffectID() ) finishWhole();
|
151
|
if( effectID==mRotate.getEffectID() ) finishRotate();
|
152
|
}
|
153
|
|
154
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
155
|
|
156
|
public void animateAll(RubikActivity act)
|
157
|
{
|
158
|
act.blockEverything();
|
159
|
mRefAct = new WeakReference<>(act);
|
160
|
|
161
|
mWholeReturned = false;
|
162
|
mRotateReturned= false;
|
163
|
|
164
|
addWholeObjects();
|
165
|
}
|
166
|
|
167
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
168
|
|
169
|
public void animateRotate(RubikActivity act)
|
170
|
{
|
171
|
act.blockEverything();
|
172
|
mRefAct = new WeakReference<>(act);
|
173
|
|
174
|
mWholeReturned = true;
|
175
|
mRotateReturned= false;
|
176
|
|
177
|
addRotateObjects();
|
178
|
}
|
179
|
}
|