Project

General

Profile

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

magiccube / src / main / java / org / distorted / control / RubikControl.java @ 588ace55

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