Project

General

Profile

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

magiccube / src / main / java / org / distorted / control / RubikControl.java @ 51f51f83

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 51f51f83 Leszek Koltunski
import org.distorted.library.main.DistortedNode;
23 f5da732a Leszek Koltunski
import org.distorted.library.message.EffectListener;
24
import org.distorted.main.RubikActivity;
25 51f51f83 Leszek Koltunski
import org.distorted.main.RubikPreRender;
26
import org.distorted.objects.TwistyObject;
27 f5da732a Leszek Koltunski
28
import java.lang.ref.WeakReference;
29
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32 51f51f83 Leszek Koltunski
public class RubikControl implements EffectListener
33 f5da732a Leszek Koltunski
  {
34
  WeakReference<RubikActivity> mRefAct;
35
  boolean mWholeReturned, mRotateReturned;
36
37 51f51f83 Leszek Koltunski
  RubikControlWhole mWhole;
38
  RubikControlRotate mRotate;
39 f5da732a Leszek Koltunski
40
///////////////////////////////////////////////////////////////////////////////////////////////////
41
42 51f51f83 Leszek Koltunski
  private TwistyObject getObject()
43 f5da732a Leszek Koltunski
    {
44 51f51f83 Leszek Koltunski
    RubikActivity act = mRefAct.get();
45 f5da732a Leszek Koltunski
46 51f51f83 Leszek Koltunski
    if( act!=null )
47
      {
48
      RubikPreRender pre = act.getPreRender();
49
      return pre!=null ? pre.getObject() : null;
50
      }
51 f5da732a Leszek Koltunski
52 51f51f83 Leszek Koltunski
    return null;
53 f5da732a Leszek Koltunski
    }
54
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56
57 51f51f83 Leszek Koltunski
  private void addWholeObjects()
58 f5da732a Leszek Koltunski
    {
59 51f51f83 Leszek Koltunski
    TwistyObject object = getObject();
60
    DistortedNode[] nodes = mWhole.getNodes();
61 f5da732a Leszek Koltunski
62 51f51f83 Leszek Koltunski
    if( object!=null && nodes!=null )
63
      {
64
      for (DistortedNode node : nodes) object.attach(node);
65
      }
66 f5da732a Leszek Koltunski
    }
67
68
///////////////////////////////////////////////////////////////////////////////////////////////////
69
70
  private void removeWholeObjects()
71
    {
72 51f51f83 Leszek Koltunski
    TwistyObject object = getObject();
73
    DistortedNode[] nodes = mWhole.returnNodes();
74 f5da732a Leszek Koltunski
75 51f51f83 Leszek Koltunski
    if( object!=null && nodes!=null )
76
      {
77
      for (DistortedNode node : nodes) object.detach(node);
78
      }
79 f5da732a Leszek Koltunski
    }
80
81
///////////////////////////////////////////////////////////////////////////////////////////////////
82
83
  private void addRotateObjects()
84
    {
85 51f51f83 Leszek Koltunski
    TwistyObject object = getObject();
86
    DistortedNode[] nodes = mRotate.getNodes();
87 f5da732a Leszek Koltunski
88 51f51f83 Leszek Koltunski
    if( object!=null && nodes!=null )
89
      {
90
      for (DistortedNode node : nodes) object.attach(node);
91
      }
92 f5da732a Leszek Koltunski
    }
93
94
///////////////////////////////////////////////////////////////////////////////////////////////////
95
96
  private void removeRotateObjects()
97
    {
98 51f51f83 Leszek Koltunski
    TwistyObject object = getObject();
99
    DistortedNode[] nodes = mRotate.returnNodes();
100 f5da732a Leszek Koltunski
101 51f51f83 Leszek Koltunski
    if( object!=null && nodes!=null )
102
      {
103
      for (DistortedNode node : nodes) object.detach(node);
104
      }
105 f5da732a Leszek Koltunski
    }
106
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108
109
  private void finishWhole()
110
    {
111
    removeWholeObjects();
112
113
    mWholeReturned = true;
114
115 51f51f83 Leszek Koltunski
    if( !mRotateReturned ) addRotateObjects();
116 f5da732a Leszek Koltunski
    }
117
118
///////////////////////////////////////////////////////////////////////////////////////////////////
119
120
  private void finishRotate()
121
    {
122
    removeRotateObjects();
123
124
    mRotateReturned= true;
125
126
    RubikActivity act = mRefAct.get();
127
    if( act!=null ) act.unblockEverything();
128
    }
129
130
///////////////////////////////////////////////////////////////////////////////////////////////////
131
132 51f51f83 Leszek Koltunski
  RubikActivity getActivity()
133 f5da732a Leszek Koltunski
    {
134 51f51f83 Leszek Koltunski
    return mRefAct.get();
135
    }
136 f5da732a Leszek Koltunski
137 51f51f83 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
138
// PUBLIC
139 f5da732a Leszek Koltunski
140 51f51f83 Leszek Koltunski
  public RubikControl()
141
    {
142
    mWhole = new RubikControlWhole(this);
143
    mRotate= new RubikControlRotate(this);
144 f5da732a Leszek Koltunski
    }
145
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147
148
  public void effectFinished(long effectID)
149
    {
150 51f51f83 Leszek Koltunski
    if( effectID==mWhole.getEffectID()  ) finishWhole();
151
    if( effectID==mRotate.getEffectID() ) finishRotate();
152 f5da732a Leszek Koltunski
    }
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 51f51f83 Leszek Koltunski
    addWholeObjects();
165 f5da732a Leszek Koltunski
    }
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 51f51f83 Leszek Koltunski
    addRotateObjects();
178 f5da732a Leszek Koltunski
    }
179
  }