Project

General

Profile

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

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

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
    ObjectLibInterface inter = mObjectControl.getInterface();
54
    return inter.getFramebuffer();
55
    }
56

    
57
///////////////////////////////////////////////////////////////////////////////////////////////////
58

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

    
64
///////////////////////////////////////////////////////////////////////////////////////////////////
65

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

    
71
///////////////////////////////////////////////////////////////////////////////////////////////////
72

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

    
78
///////////////////////////////////////////////////////////////////////////////////////////////////
79

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

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

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

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

    
104
///////////////////////////////////////////////////////////////////////////////////////////////////
105

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

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

    
116
    DistortedNode object = mObjectControl.getObject();
117
    DistortedNode[] objectNodes = mRotate.getObjectNodes();
118

    
119
    if( object!=null && objectNodes!=null )
120
      {
121
      for (DistortedNode node : objectNodes) object.attach(node);
122
      }
123
    }
124

    
125
///////////////////////////////////////////////////////////////////////////////////////////////////
126

    
127
  private void removeRotateObjects()
128
    {
129
    DistortedFramebuffer frame = getFramebuffer();
130
    DistortedNode[] screenNodes = mRotate.returnScreenNodes();
131

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

    
137
    DistortedNode object = mObjectControl.getObject();
138
    DistortedNode[] objectNodes = mRotate.returnObjectNodes();
139

    
140
    if( object!=null && objectNodes!=null )
141
      {
142
      for (DistortedNode node : objectNodes) object.detach(node);
143
      }
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  private void finishWhole()
149
    {
150
    removeWholeObjects();
151
    mWholeReturned = true;
152
    if( !mRotateReturned ) addRotateObjects();
153
    }
154

    
155
///////////////////////////////////////////////////////////////////////////////////////////////////
156

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

    
164
///////////////////////////////////////////////////////////////////////////////////////////////////
165

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

    
171
///////////////////////////////////////////////////////////////////////////////////////////////////
172

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

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

    
181
///////////////////////////////////////////////////////////////////////////////////////////////////
182
// PUBLIC
183

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

    
188
    return mControl;
189
    }
190

    
191
///////////////////////////////////////////////////////////////////////////////////////////////////
192

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

    
198
///////////////////////////////////////////////////////////////////////////////////////////////////
199

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

    
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207

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

    
213
    mWholeReturned = false;
214
    mRotateReturned= false;
215

    
216
    addWholeObjects();
217
    }
218

    
219
///////////////////////////////////////////////////////////////////////////////////////////////////
220

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

    
226
    mWholeReturned = true;
227
    mRotateReturned= false;
228

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