Project

General

Profile

Download (7.64 KB) Statistics
| Branch: | Revision:

distorted-objectlib / src / main / java / org / distorted / objectlib / automator / ObjectAutomator.java @ 7ba38dd4

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.objectlib.automator;
21

    
22
import java.lang.ref.WeakReference;
23

    
24
import android.app.Activity;
25

    
26
import org.distorted.library.main.DistortedFramebuffer;
27
import org.distorted.library.main.DistortedNode;
28
import org.distorted.library.message.EffectListener;
29
import org.distorted.library.type.Static4D;
30

    
31
import org.distorted.objectlib.helpers.ObjectLibInterface;
32
import org.distorted.objectlib.main.ObjectControl;
33
import org.distorted.objectlib.helpers.BlockController;
34
import org.distorted.objectlib.main.TwistyObject;
35

    
36
///////////////////////////////////////////////////////////////////////////////////////////////////
37

    
38
public class ObjectAutomator implements EffectListener
39
  {
40
  private static ObjectAutomator mControl;
41
  private final ObjectControl mObjectControl;
42

    
43
  WeakReference<Activity> mRefAct;
44
  boolean mWholeReturned, mRotateReturned;
45

    
46
  ObjectAutomatorWhole mWhole;
47
  ObjectAutomatorRotate mRotate;
48

    
49
///////////////////////////////////////////////////////////////////////////////////////////////////
50

    
51
  DistortedFramebuffer getFramebuffer()
52
    {
53
    return mObjectControl.getNode().getFramebuffer();
54
    }
55

    
56
///////////////////////////////////////////////////////////////////////////////////////////////////
57

    
58
  ObjectControl getObjectControl()
59
    {
60
    return mObjectControl;
61
    }
62

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

    
65
  Static4D getQuat()
66
    {
67
    return mObjectControl.getQuat();
68
    }
69

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

    
72
  TwistyObject getObject()
73
    {
74
    return mObjectControl.getObject();
75
    }
76

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

    
79
  private void addWholeObjects()
80
    {
81
    DistortedFramebuffer frame = getFramebuffer();
82
    DistortedNode[] nodes = mWhole.getNodes();
83

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

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

    
92
  private void removeWholeObjects()
93
    {
94
    DistortedFramebuffer frame = getFramebuffer();
95
    DistortedNode[] nodes = mWhole.returnNodes();
96

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

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

    
105
  private void addRotateObjects()
106
    {
107
    DistortedFramebuffer frame = getFramebuffer();
108
    DistortedNode[] screenNodes = mRotate.getScreenNodes();
109

    
110
    if( frame!=null && screenNodes!=null )
111
      {
112
      for (DistortedNode node : screenNodes) frame.attach(node);
113
      }
114

    
115
    DistortedNode object = mObjectControl.getObject().getNode();
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
    DistortedFramebuffer frame = getFramebuffer();
129
    DistortedNode[] screenNodes = mRotate.returnScreenNodes();
130

    
131
    if( frame!=null && screenNodes!=null )
132
      {
133
      for (DistortedNode node : screenNodes) frame.detach(node);
134
      }
135

    
136
    DistortedNode object = mObjectControl.getObject().getNode();
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
    mWholeReturned = true;
151
    if( !mRotateReturned ) addRotateObjects();
152
    }
153

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

    
156
  private void finishRotate()
157
    {
158
    removeRotateObjects();
159
    mRotateReturned= true;
160
    mObjectControl.unblockEverything();
161
    }
162

    
163
///////////////////////////////////////////////////////////////////////////////////////////////////
164

    
165
  Activity getActivity()
166
    {
167
    return mRefAct.get();
168
    }
169

    
170
///////////////////////////////////////////////////////////////////////////////////////////////////
171

    
172
  private ObjectAutomator(ObjectControl control)
173
    {
174
    mObjectControl = control;
175

    
176
    mWhole = new ObjectAutomatorWhole(this);
177
    mRotate= new ObjectAutomatorRotate(this);
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181
// PUBLIC
182

    
183
  public static ObjectAutomator getInstance(ObjectControl control)
184
    {
185
    if( mControl==null ) mControl = new ObjectAutomator(control);
186

    
187
    return mControl;
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  public void postDrawFrame(long time)
193
    {
194
    mWhole.postDrawFrame(time);
195
    }
196

    
197
///////////////////////////////////////////////////////////////////////////////////////////////////
198

    
199
  public void effectFinished(long effectID)
200
    {
201
    if( effectID==mWhole.getEffectID()  ) finishWhole();
202
    if( effectID==mRotate.getEffectID() ) finishRotate();
203
    }
204

    
205
///////////////////////////////////////////////////////////////////////////////////////////////////
206

    
207
  public void animateAll(Activity act)
208
    {
209
    mObjectControl.blockEverything(BlockController.CONTROL_PLACE_0);
210
    mRefAct = new WeakReference<>(act);
211

    
212
    mWholeReturned = false;
213
    mRotateReturned= false;
214

    
215
    addWholeObjects();
216
    }
217

    
218
///////////////////////////////////////////////////////////////////////////////////////////////////
219

    
220
  public void animateRotate(Activity act)
221
    {
222
    mObjectControl.blockEverything(BlockController.CONTROL_PLACE_1);
223
    mRefAct = new WeakReference<>(act);
224

    
225
    mWholeReturned = true;
226
    mRotateReturned= false;
227

    
228
    addRotateObjects();
229
    }
230
  }
(1-1/3)