Project

General

Profile

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

magiccube / src / main / java / org / distorted / control / RubikControl.java @ 967b79dc

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.main.RubikActivity;
27
import org.distorted.main.RubikSurfaceView;
28

    
29
import java.lang.ref.WeakReference;
30

    
31
///////////////////////////////////////////////////////////////////////////////////////////////////
32

    
33
public class RubikControl implements EffectListener
34
  {
35
  private static RubikControl mControl;
36

    
37
  WeakReference<RubikActivity> mRefAct;
38
  boolean mWholeReturned, mRotateReturned;
39

    
40
  RubikControlWhole mWhole;
41
  RubikControlRotate mRotate;
42

    
43
///////////////////////////////////////////////////////////////////////////////////////////////////
44

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

    
51
///////////////////////////////////////////////////////////////////////////////////////////////////
52

    
53
  RubikSurfaceView getSurfaceView()
54
    {
55
    RubikActivity act = mRefAct.get();
56
    return act!=null ? act.getSurfaceView() : null;
57
    }
58

    
59
///////////////////////////////////////////////////////////////////////////////////////////////////
60

    
61
  private void addWholeObjects()
62
    {
63
    DistortedScreen screen = getScreen();
64
    DistortedNode[] nodes = mWhole.getNodes();
65

    
66
    if( screen!=null && nodes!=null )
67
      {
68
      for (DistortedNode node : nodes) screen.attach(node);
69
      }
70
    }
71

    
72
///////////////////////////////////////////////////////////////////////////////////////////////////
73

    
74
  private void removeWholeObjects()
75
    {
76
    DistortedScreen screen = getScreen();
77
    DistortedNode[] nodes = mWhole.returnNodes();
78

    
79
    if( screen!=null && nodes!=null )
80
      {
81
      for (DistortedNode node : nodes) screen.detach(node);
82
      }
83
    }
84

    
85
///////////////////////////////////////////////////////////////////////////////////////////////////
86

    
87
  private void addRotateObjects()
88
    {
89
    DistortedScreen screen = getScreen();
90
    DistortedNode[] nodes = mRotate.getNodes();
91

    
92
    if( screen!=null && nodes!=null )
93
      {
94
      for (DistortedNode node : nodes) screen.attach(node);
95
      }
96
    }
97

    
98
///////////////////////////////////////////////////////////////////////////////////////////////////
99

    
100
  private void removeRotateObjects()
101
    {
102
    DistortedScreen screen = getScreen();
103
    DistortedNode[] nodes = mRotate.returnNodes();
104

    
105
    if( screen!=null && nodes!=null )
106
      {
107
      for (DistortedNode node : nodes) screen.detach(node);
108
      }
109
    }
110

    
111
///////////////////////////////////////////////////////////////////////////////////////////////////
112

    
113
  private void finishWhole()
114
    {
115
    removeWholeObjects();
116

    
117
    mWholeReturned = true;
118

    
119
    if( !mRotateReturned ) addRotateObjects();
120
    }
121

    
122
///////////////////////////////////////////////////////////////////////////////////////////////////
123

    
124
  private void finishRotate()
125
    {
126
    removeRotateObjects();
127

    
128
    mRotateReturned= true;
129

    
130
    RubikActivity act = mRefAct.get();
131
    if( act!=null ) act.unblockEverything();
132
    }
133

    
134
///////////////////////////////////////////////////////////////////////////////////////////////////
135

    
136
  RubikActivity getActivity()
137
    {
138
    return mRefAct.get();
139
    }
140

    
141
///////////////////////////////////////////////////////////////////////////////////////////////////
142

    
143
  private RubikControl()
144
    {
145
    mWhole = new RubikControlWhole(this);
146
    mRotate= new RubikControlRotate(this);
147
    }
148

    
149
///////////////////////////////////////////////////////////////////////////////////////////////////
150
// PUBLIC
151

    
152
  public static RubikControl getInstance()
153
    {
154
    if( mControl==null ) mControl = new RubikControl();
155

    
156
    return mControl;
157
    }
158

    
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160

    
161
  public void postDrawFrame(long time)
162
    {
163
    mWhole.postDrawFrame(time);
164
    }
165

    
166
///////////////////////////////////////////////////////////////////////////////////////////////////
167

    
168
  public void effectFinished(long effectID)
169
    {
170
    if( effectID==mWhole.getEffectID()  ) finishWhole();
171
    if( effectID==mRotate.getEffectID() ) finishRotate();
172
    }
173

    
174
///////////////////////////////////////////////////////////////////////////////////////////////////
175

    
176
  public void animateAll(RubikActivity act)
177
    {
178
    act.blockEverything(BlockController.CONTROL_PLACE_0);
179
    mRefAct = new WeakReference<>(act);
180

    
181
    mWholeReturned = false;
182
    mRotateReturned= false;
183

    
184
    addWholeObjects();
185
    }
186

    
187
///////////////////////////////////////////////////////////////////////////////////////////////////
188

    
189
  public void animateRotate(RubikActivity act)
190
    {
191
    act.blockEverything(BlockController.CONTROL_PLACE_1);
192
    mRefAct = new WeakReference<>(act);
193

    
194
    mWholeReturned = true;
195
    mRotateReturned= false;
196

    
197
    addRotateObjects();
198
    }
199
  }
(1-1/3)