Project

General

Profile

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

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

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.objectlib.helpers.BlockController;
23
import org.distorted.library.main.DistortedNode;
24
import org.distorted.library.main.DistortedScreen;
25
import org.distorted.library.message.EffectListener;
26
import org.distorted.library.type.Static4D;
27

    
28
import org.distorted.objectlib.main.TwistyObject;
29

    
30
import org.distorted.main.RubikActivity;
31
import org.distorted.main.RubikSurfaceView;
32

    
33
import java.lang.ref.WeakReference;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

    
37
public class RubikControl implements EffectListener
38
  {
39
  private static RubikControl mControl;
40

    
41
  WeakReference<RubikActivity> mRefAct;
42
  boolean mWholeReturned, mRotateReturned;
43

    
44
  RubikControlWhole mWhole;
45
  RubikControlRotate mRotate;
46

    
47
///////////////////////////////////////////////////////////////////////////////////////////////////
48

    
49
  DistortedScreen getScreen()
50
    {
51
    RubikActivity act = mRefAct.get();
52
    return act!=null ? act.getScreen() : null;
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

    
57
  TwistyObject getObject()
58
    {
59
    RubikActivity act = mRefAct.get();
60
    return act!=null ? act.getObject() : null;
61
    }
62

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

    
65
  Static4D getCurrQuat()
66
    {
67
    RubikActivity act = mRefAct.get();
68
    return act!=null ? act.getCurrQuat() : null;
69
    }
70

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

    
73
  RubikSurfaceView getSurfaceView()
74
    {
75
    RubikActivity act = mRefAct.get();
76
    return act!=null ? act.getSurfaceView() : null;
77
    }
78

    
79
///////////////////////////////////////////////////////////////////////////////////////////////////
80

    
81
  private void addWholeObjects()
82
    {
83
    DistortedScreen screen = getScreen();
84
    DistortedNode[] nodes = mWhole.getNodes();
85

    
86
    if( screen!=null && nodes!=null )
87
      {
88
      for (DistortedNode node : nodes) screen.attach(node);
89
      }
90
    }
91

    
92
///////////////////////////////////////////////////////////////////////////////////////////////////
93

    
94
  private void removeWholeObjects()
95
    {
96
    DistortedScreen screen = getScreen();
97
    DistortedNode[] nodes = mWhole.returnNodes();
98

    
99
    if( screen!=null && nodes!=null )
100
      {
101
      for (DistortedNode node : nodes) screen.detach(node);
102
      }
103
    }
104

    
105
///////////////////////////////////////////////////////////////////////////////////////////////////
106

    
107
  private void addRotateObjects()
108
    {
109
    DistortedScreen screen = getScreen();
110
    DistortedNode[] screenNodes = mRotate.getScreenNodes();
111

    
112
    if( screen!=null && screenNodes!=null )
113
      {
114
      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
      }
124
    }
125

    
126
///////////////////////////////////////////////////////////////////////////////////////////////////
127

    
128
  private void removeRotateObjects()
129
    {
130
    DistortedScreen screen = getScreen();
131
    DistortedNode[] screenNodes = mRotate.returnScreenNodes();
132

    
133
    if( screen!=null && screenNodes!=null )
134
      {
135
      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
      }
145
    }
146

    
147
///////////////////////////////////////////////////////////////////////////////////////////////////
148

    
149
  private void finishWhole()
150
    {
151
    removeWholeObjects();
152

    
153
    mWholeReturned = true;
154

    
155
    if( !mRotateReturned ) addRotateObjects();
156
    }
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
  RubikActivity getActivity()
173
    {
174
    return mRefAct.get();
175
    }
176

    
177
///////////////////////////////////////////////////////////////////////////////////////////////////
178

    
179
  private RubikControl()
180
    {
181
    mWhole = new RubikControlWhole(this);
182
    mRotate= new RubikControlRotate(this);
183
    }
184

    
185
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
203

    
204
  public void effectFinished(long effectID)
205
    {
206
    if( effectID==mWhole.getEffectID()  ) finishWhole();
207
    if( effectID==mRotate.getEffectID() ) finishRotate();
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  public void animateAll(RubikActivity act)
213
    {
214
    act.blockEverything(BlockController.CONTROL_PLACE_0);
215
    mRefAct = new WeakReference<>(act);
216

    
217
    mWholeReturned = false;
218
    mRotateReturned= false;
219

    
220
    addWholeObjects();
221
    }
222

    
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224

    
225
  public void animateRotate(RubikActivity act)
226
    {
227
    act.blockEverything(BlockController.CONTROL_PLACE_1);
228
    mRefAct = new WeakReference<>(act);
229

    
230
    mWholeReturned = true;
231
    mRotateReturned= false;
232

    
233
    addRotateObjects();
234
    }
235
  }
(1-1/3)