Project

General

Profile

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

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

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.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
import org.distorted.main.RubikActivity;
28
import org.distorted.main.RubikSurfaceView;
29
import org.distorted.objectlib.TwistyObject;
30

    
31
import java.lang.ref.WeakReference;
32

    
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34

    
35
public class RubikControl implements EffectListener
36
  {
37
  private static RubikControl mControl;
38

    
39
  WeakReference<RubikActivity> mRefAct;
40
  boolean mWholeReturned, mRotateReturned;
41

    
42
  RubikControlWhole mWhole;
43
  RubikControlRotate mRotate;
44

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
53
///////////////////////////////////////////////////////////////////////////////////////////////////
54

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

    
61
///////////////////////////////////////////////////////////////////////////////////////////////////
62

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

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

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

    
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78

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

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

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

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

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

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

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

    
110
    if( screen!=null && screenNodes!=null )
111
      {
112
      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
      }
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

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

    
131
    if( screen!=null && screenNodes!=null )
132
      {
133
      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
      }
143
    }
144

    
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146

    
147
  private void finishWhole()
148
    {
149
    removeWholeObjects();
150

    
151
    mWholeReturned = true;
152

    
153
    if( !mRotateReturned ) addRotateObjects();
154
    }
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
  RubikActivity getActivity()
171
    {
172
    return mRefAct.get();
173
    }
174

    
175
///////////////////////////////////////////////////////////////////////////////////////////////////
176

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

    
183
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
201

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

    
208
///////////////////////////////////////////////////////////////////////////////////////////////////
209

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

    
215
    mWholeReturned = false;
216
    mRotateReturned= false;
217

    
218
    addWholeObjects();
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

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

    
228
    mWholeReturned = true;
229
    mRotateReturned= false;
230

    
231
    addRotateObjects();
232
    }
233
  }
(1-1/3)