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.main.DistortedScreen;
|
24
|
import org.distorted.library.message.EffectListener;
|
25
|
import org.distorted.main.RubikActivity;
|
26
|
import org.distorted.main.RubikSurfaceView;
|
27
|
|
28
|
import java.lang.ref.WeakReference;
|
29
|
|
30
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
31
|
|
32
|
public class RubikControl implements EffectListener
|
33
|
{
|
34
|
private static RubikControl mControl;
|
35
|
|
36
|
WeakReference<RubikActivity> mRefAct;
|
37
|
boolean mWholeReturned, mRotateReturned;
|
38
|
|
39
|
RubikControlWhole mWhole;
|
40
|
RubikControlRotate mRotate;
|
41
|
|
42
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
43
|
|
44
|
DistortedScreen getScreen()
|
45
|
{
|
46
|
RubikActivity act = mRefAct.get();
|
47
|
return act!=null ? act.getScreen() : null;
|
48
|
}
|
49
|
|
50
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
51
|
|
52
|
RubikSurfaceView getSurfaceView()
|
53
|
{
|
54
|
RubikActivity act = mRefAct.get();
|
55
|
return act!=null ? act.getSurfaceView() : null;
|
56
|
}
|
57
|
|
58
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
59
|
|
60
|
private void addWholeObjects()
|
61
|
{
|
62
|
DistortedScreen screen = getScreen();
|
63
|
DistortedNode[] nodes = mWhole.getNodes();
|
64
|
|
65
|
if( screen!=null && nodes!=null )
|
66
|
{
|
67
|
for (DistortedNode node : nodes) screen.attach(node);
|
68
|
}
|
69
|
}
|
70
|
|
71
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
72
|
|
73
|
private void removeWholeObjects()
|
74
|
{
|
75
|
DistortedScreen screen = getScreen();
|
76
|
DistortedNode[] nodes = mWhole.returnNodes();
|
77
|
|
78
|
if( screen!=null && nodes!=null )
|
79
|
{
|
80
|
for (DistortedNode node : nodes) screen.detach(node);
|
81
|
}
|
82
|
}
|
83
|
|
84
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
85
|
|
86
|
private void addRotateObjects()
|
87
|
{
|
88
|
DistortedScreen screen = getScreen();
|
89
|
DistortedNode[] nodes = mRotate.getNodes();
|
90
|
|
91
|
if( screen!=null && nodes!=null )
|
92
|
{
|
93
|
for (DistortedNode node : nodes) screen.attach(node);
|
94
|
}
|
95
|
}
|
96
|
|
97
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
98
|
|
99
|
private void removeRotateObjects()
|
100
|
{
|
101
|
DistortedScreen screen = getScreen();
|
102
|
DistortedNode[] nodes = mRotate.returnNodes();
|
103
|
|
104
|
if( screen!=null && nodes!=null )
|
105
|
{
|
106
|
for (DistortedNode node : nodes) screen.detach(node);
|
107
|
}
|
108
|
}
|
109
|
|
110
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
111
|
|
112
|
private void finishWhole()
|
113
|
{
|
114
|
removeWholeObjects();
|
115
|
|
116
|
mWholeReturned = true;
|
117
|
|
118
|
if( !mRotateReturned ) addRotateObjects();
|
119
|
}
|
120
|
|
121
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
122
|
|
123
|
private void finishRotate()
|
124
|
{
|
125
|
removeRotateObjects();
|
126
|
|
127
|
mRotateReturned= true;
|
128
|
|
129
|
RubikActivity act = mRefAct.get();
|
130
|
if( act!=null ) act.unblockEverything();
|
131
|
}
|
132
|
|
133
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
134
|
|
135
|
RubikActivity getActivity()
|
136
|
{
|
137
|
return mRefAct.get();
|
138
|
}
|
139
|
|
140
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
141
|
|
142
|
private RubikControl()
|
143
|
{
|
144
|
mWhole = new RubikControlWhole(this);
|
145
|
mRotate= new RubikControlRotate(this);
|
146
|
}
|
147
|
|
148
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
149
|
// PUBLIC
|
150
|
|
151
|
public static RubikControl getInstance()
|
152
|
{
|
153
|
if( mControl==null ) mControl = new RubikControl();
|
154
|
|
155
|
return mControl;
|
156
|
}
|
157
|
|
158
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
159
|
|
160
|
public void postDrawFrame(long time)
|
161
|
{
|
162
|
mWhole.postDrawFrame(time);
|
163
|
}
|
164
|
|
165
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
166
|
|
167
|
public void effectFinished(long effectID)
|
168
|
{
|
169
|
if( effectID==mWhole.getEffectID() ) finishWhole();
|
170
|
if( effectID==mRotate.getEffectID() ) finishRotate();
|
171
|
}
|
172
|
|
173
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
174
|
|
175
|
public void animateAll(RubikActivity act)
|
176
|
{
|
177
|
act.blockEverything();
|
178
|
mRefAct = new WeakReference<>(act);
|
179
|
|
180
|
mWholeReturned = false;
|
181
|
mRotateReturned= false;
|
182
|
|
183
|
addWholeObjects();
|
184
|
}
|
185
|
|
186
|
///////////////////////////////////////////////////////////////////////////////////////////////////
|
187
|
|
188
|
public void animateRotate(RubikActivity act)
|
189
|
{
|
190
|
act.blockEverything();
|
191
|
mRefAct = new WeakReference<>(act);
|
192
|
|
193
|
mWholeReturned = true;
|
194
|
mRotateReturned= false;
|
195
|
|
196
|
addRotateObjects();
|
197
|
}
|
198
|
}
|