Project

General

Profile

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

magiccube / src / main / java / org / distorted / control / RubikControl.java @ b7e5a0ac

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.library.main.DistortedNode;
23
import org.distorted.library.main.DistortedScreen;
24
import org.distorted.library.message.EffectListener;
25
import org.distorted.main.RubikActivity;
26

    
27
import java.lang.ref.WeakReference;
28

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30

    
31
public class RubikControl implements EffectListener
32
  {
33
  WeakReference<RubikActivity> mRefAct;
34
  boolean mWholeReturned, mRotateReturned;
35

    
36
  RubikControlWhole mWhole;
37
  RubikControlRotate mRotate;
38

    
39
///////////////////////////////////////////////////////////////////////////////////////////////////
40

    
41
  private DistortedScreen getScreen()
42
    {
43
    RubikActivity act = mRefAct.get();
44
    return act!=null ? act.getScreen() : null;
45
    }
46

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

    
49
  private void addWholeObjects()
50
    {
51
    DistortedScreen screen = getScreen();
52
    DistortedNode[] nodes = mWhole.getNodes();
53

    
54
    if( screen!=null && nodes!=null )
55
      {
56
      for (DistortedNode node : nodes) screen.attach(node);
57
      }
58
    }
59

    
60
///////////////////////////////////////////////////////////////////////////////////////////////////
61

    
62
  private void removeWholeObjects()
63
    {
64
    DistortedScreen screen = getScreen();
65
    DistortedNode[] nodes = mWhole.returnNodes();
66

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

    
73
///////////////////////////////////////////////////////////////////////////////////////////////////
74

    
75
  private void addRotateObjects()
76
    {
77
    DistortedScreen screen = getScreen();
78
    DistortedNode[] nodes = mRotate.getNodes();
79

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

    
86
///////////////////////////////////////////////////////////////////////////////////////////////////
87

    
88
  private void removeRotateObjects()
89
    {
90
    DistortedScreen screen = getScreen();
91
    DistortedNode[] nodes = mRotate.returnNodes();
92

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

    
99
///////////////////////////////////////////////////////////////////////////////////////////////////
100

    
101
  private void finishWhole()
102
    {
103
    removeWholeObjects();
104

    
105
    mWholeReturned = true;
106

    
107
    if( !mRotateReturned ) addRotateObjects();
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  private void finishRotate()
113
    {
114
    removeRotateObjects();
115

    
116
    mRotateReturned= true;
117

    
118
    RubikActivity act = mRefAct.get();
119
    if( act!=null ) act.unblockEverything();
120
    }
121

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

    
124
  RubikActivity getActivity()
125
    {
126
    return mRefAct.get();
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
// PUBLIC
131

    
132
  public RubikControl()
133
    {
134
    mWhole = new RubikControlWhole(this);
135
    mRotate= new RubikControlRotate(this);
136
    }
137

    
138
///////////////////////////////////////////////////////////////////////////////////////////////////
139

    
140
  public void effectFinished(long effectID)
141
    {
142
    if( effectID==mWhole.getEffectID()  )
143
      {
144
      android.util.Log.e("D", "whole finished");
145
      finishWhole();
146
      }
147
    if( effectID==mRotate.getEffectID() )
148
      {
149
      android.util.Log.e("D", "rotate finished");
150
      finishRotate();
151
      }
152
    }
153

    
154
///////////////////////////////////////////////////////////////////////////////////////////////////
155

    
156
  public void animateAll(RubikActivity act)
157
    {
158
    act.blockEverything();
159
    mRefAct = new WeakReference<>(act);
160

    
161
    mWholeReturned = false;
162
    mRotateReturned= false;
163

    
164
    addWholeObjects();
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  public void animateRotate(RubikActivity act)
170
    {
171
    act.blockEverything();
172
    mRefAct = new WeakReference<>(act);
173

    
174
    mWholeReturned = true;
175
    mRotateReturned= false;
176

    
177
    addRotateObjects();
178
    }
179
  }
(1-1/3)