Project

General

Profile

Download (7.58 KB) Statistics
| Branch: | Tag: | Revision:

magiccube / src / main / java / org / distorted / control / RubikControl.java @ 88a3e972

1 f5da732a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 88a3e972 Leszek Koltunski
import org.distorted.objectlib.helpers.BlockController;
23 51f51f83 Leszek Koltunski
import org.distorted.library.main.DistortedNode;
24 f8eece7d Leszek Koltunski
import org.distorted.library.main.DistortedScreen;
25 f5da732a Leszek Koltunski
import org.distorted.library.message.EffectListener;
26 7aa4c349 Leszek Koltunski
import org.distorted.library.type.Static4D;
27 3f7a4363 Leszek Koltunski
28
import org.distorted.objectlib.main.TwistyObject;
29
30 f5da732a Leszek Koltunski
import org.distorted.main.RubikActivity;
31 8ba7aeb1 Leszek Koltunski
import org.distorted.main.RubikSurfaceView;
32 f5da732a Leszek Koltunski
33
import java.lang.ref.WeakReference;
34
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36
37 51f51f83 Leszek Koltunski
public class RubikControl implements EffectListener
38 f5da732a Leszek Koltunski
  {
39 314bffaf Leszek Koltunski
  private static RubikControl mControl;
40
41 f5da732a Leszek Koltunski
  WeakReference<RubikActivity> mRefAct;
42
  boolean mWholeReturned, mRotateReturned;
43
44 51f51f83 Leszek Koltunski
  RubikControlWhole mWhole;
45
  RubikControlRotate mRotate;
46 f5da732a Leszek Koltunski
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48
49 f7f24f23 Leszek Koltunski
  DistortedScreen getScreen()
50 f5da732a Leszek Koltunski
    {
51 51f51f83 Leszek Koltunski
    RubikActivity act = mRefAct.get();
52 f8eece7d Leszek Koltunski
    return act!=null ? act.getScreen() : null;
53 f5da732a Leszek Koltunski
    }
54
55 b9d4aa3b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57
  TwistyObject getObject()
58
    {
59
    RubikActivity act = mRefAct.get();
60
    return act!=null ? act.getObject() : null;
61
    }
62
63 7aa4c349 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
64
65
  Static4D getCurrQuat()
66
    {
67
    RubikActivity act = mRefAct.get();
68
    return act!=null ? act.getCurrQuat() : null;
69
    }
70
71 8ba7aeb1 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
72
73
  RubikSurfaceView getSurfaceView()
74
    {
75
    RubikActivity act = mRefAct.get();
76
    return act!=null ? act.getSurfaceView() : null;
77
    }
78
79 f5da732a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
80
81 51f51f83 Leszek Koltunski
  private void addWholeObjects()
82 f5da732a Leszek Koltunski
    {
83 f8eece7d Leszek Koltunski
    DistortedScreen screen = getScreen();
84 51f51f83 Leszek Koltunski
    DistortedNode[] nodes = mWhole.getNodes();
85 f5da732a Leszek Koltunski
86 f8eece7d Leszek Koltunski
    if( screen!=null && nodes!=null )
87 51f51f83 Leszek Koltunski
      {
88 f8eece7d Leszek Koltunski
      for (DistortedNode node : nodes) screen.attach(node);
89 51f51f83 Leszek Koltunski
      }
90 f5da732a Leszek Koltunski
    }
91
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93
94
  private void removeWholeObjects()
95
    {
96 f8eece7d Leszek Koltunski
    DistortedScreen screen = getScreen();
97 51f51f83 Leszek Koltunski
    DistortedNode[] nodes = mWhole.returnNodes();
98 f5da732a Leszek Koltunski
99 f8eece7d Leszek Koltunski
    if( screen!=null && nodes!=null )
100 51f51f83 Leszek Koltunski
      {
101 f8eece7d Leszek Koltunski
      for (DistortedNode node : nodes) screen.detach(node);
102 51f51f83 Leszek Koltunski
      }
103 f5da732a Leszek Koltunski
    }
104
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106
107
  private void addRotateObjects()
108
    {
109 f8eece7d Leszek Koltunski
    DistortedScreen screen = getScreen();
110 b9d4aa3b Leszek Koltunski
    DistortedNode[] screenNodes = mRotate.getScreenNodes();
111 f5da732a Leszek Koltunski
112 b9d4aa3b Leszek Koltunski
    if( screen!=null && screenNodes!=null )
113 51f51f83 Leszek Koltunski
      {
114 b9d4aa3b Leszek Koltunski
      for (DistortedNode node : screenNodes) screen.attach(node);
115
      }
116
117
    DistortedNode object = getObject();
118
    DistortedNode[] objectNodes = mRotate.getObjectNodes();
119
120
    if( object!=null && objectNodes!=null )
121
      {
122
      for (DistortedNode node : objectNodes) object.attach(node);
123 51f51f83 Leszek Koltunski
      }
124 f5da732a Leszek Koltunski
    }
125
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127
128
  private void removeRotateObjects()
129
    {
130 f8eece7d Leszek Koltunski
    DistortedScreen screen = getScreen();
131 b9d4aa3b Leszek Koltunski
    DistortedNode[] screenNodes = mRotate.returnScreenNodes();
132 f5da732a Leszek Koltunski
133 b9d4aa3b Leszek Koltunski
    if( screen!=null && screenNodes!=null )
134 51f51f83 Leszek Koltunski
      {
135 b9d4aa3b Leszek Koltunski
      for (DistortedNode node : screenNodes) screen.detach(node);
136
      }
137
138
    DistortedNode object = getObject();
139
    DistortedNode[] objectNodes = mRotate.returnObjectNodes();
140
141
    if( object!=null && objectNodes!=null )
142
      {
143
      for (DistortedNode node : objectNodes) object.detach(node);
144 51f51f83 Leszek Koltunski
      }
145 f5da732a Leszek Koltunski
    }
146
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148
149
  private void finishWhole()
150
    {
151
    removeWholeObjects();
152
153
    mWholeReturned = true;
154
155 51f51f83 Leszek Koltunski
    if( !mRotateReturned ) addRotateObjects();
156 f5da732a Leszek Koltunski
    }
157
158
///////////////////////////////////////////////////////////////////////////////////////////////////
159
160
  private void finishRotate()
161
    {
162
    removeRotateObjects();
163
164
    mRotateReturned= true;
165
166
    RubikActivity act = mRefAct.get();
167
    if( act!=null ) act.unblockEverything();
168
    }
169
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171
172 51f51f83 Leszek Koltunski
  RubikActivity getActivity()
173 f5da732a Leszek Koltunski
    {
174 51f51f83 Leszek Koltunski
    return mRefAct.get();
175
    }
176 f5da732a Leszek Koltunski
177 51f51f83 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
178 f5da732a Leszek Koltunski
179 314bffaf Leszek Koltunski
  private RubikControl()
180 51f51f83 Leszek Koltunski
    {
181
    mWhole = new RubikControlWhole(this);
182
    mRotate= new RubikControlRotate(this);
183 f5da732a Leszek Koltunski
    }
184
185 314bffaf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
186
// PUBLIC
187
188
  public static RubikControl getInstance()
189
    {
190
    if( mControl==null ) mControl = new RubikControl();
191
192
    return mControl;
193
    }
194
195
///////////////////////////////////////////////////////////////////////////////////////////////////
196
197
  public void postDrawFrame(long time)
198
    {
199
    mWhole.postDrawFrame(time);
200
    }
201
202 f5da732a Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
203
204
  public void effectFinished(long effectID)
205
    {
206 f7f24f23 Leszek Koltunski
    if( effectID==mWhole.getEffectID()  ) finishWhole();
207
    if( effectID==mRotate.getEffectID() ) finishRotate();
208 f5da732a Leszek Koltunski
    }
209
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211
212
  public void animateAll(RubikActivity act)
213
    {
214 809c3432 Leszek Koltunski
    act.blockEverything(BlockController.CONTROL_PLACE_0);
215 f5da732a Leszek Koltunski
    mRefAct = new WeakReference<>(act);
216
217
    mWholeReturned = false;
218
    mRotateReturned= false;
219
220 51f51f83 Leszek Koltunski
    addWholeObjects();
221 f5da732a Leszek Koltunski
    }
222
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
225
  public void animateRotate(RubikActivity act)
226
    {
227 809c3432 Leszek Koltunski
    act.blockEverything(BlockController.CONTROL_PLACE_1);
228 f5da732a Leszek Koltunski
    mRefAct = new WeakReference<>(act);
229
230
    mWholeReturned = true;
231
    mRotateReturned= false;
232
233 51f51f83 Leszek Koltunski
    addRotateObjects();
234 f5da732a Leszek Koltunski
    }
235
  }