Project

General

Profile

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

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

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

    
26
import org.distorted.library.type.Static4D;
27
import org.distorted.objectlib.main.ObjectControl;
28
import org.distorted.objectlib.main.ObjectPreRender;
29
import org.distorted.objectlib.helpers.BlockController;
30
import org.distorted.objectlib.helpers.TwistyActivity;
31
import org.distorted.objectlib.main.TwistyObject;
32

    
33
import java.lang.ref.WeakReference;
34

    
35
///////////////////////////////////////////////////////////////////////////////////////////////////
36

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

    
42
  WeakReference<TwistyActivity> mRefAct;
43
  boolean mWholeReturned, mRotateReturned;
44

    
45
  ObjectAutomatorWhole mWhole;
46
  ObjectAutomatorRotate mRotate;
47

    
48
///////////////////////////////////////////////////////////////////////////////////////////////////
49

    
50
  DistortedScreen getScreen()
51
    {
52
    TwistyActivity act = mRefAct.get();
53
    return act!=null ? act.getScreen() : null;
54
    }
55

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

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

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

    
65
  Static4D getQuat()
66
    {
67
    ObjectControl control = getObjectControl();
68
    return control.getQuat();
69
    }
70

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

    
73
  TwistyObject getObject()
74
    {
75
    ObjectControl control = getObjectControl();
76
    ObjectPreRender pre =  control.getPreRender();
77
    return pre.getObject();
78
    }
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  private void addWholeObjects()
83
    {
84
    DistortedScreen screen = getScreen();
85
    DistortedNode[] nodes = mWhole.getNodes();
86

    
87
    if( screen!=null && nodes!=null )
88
      {
89
      for (DistortedNode node : nodes) screen.attach(node);
90
      }
91
    }
92

    
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94

    
95
  private void removeWholeObjects()
96
    {
97
    DistortedScreen screen = getScreen();
98
    DistortedNode[] nodes = mWhole.returnNodes();
99

    
100
    if( screen!=null && nodes!=null )
101
      {
102
      for (DistortedNode node : nodes) screen.detach(node);
103
      }
104
    }
105

    
106
///////////////////////////////////////////////////////////////////////////////////////////////////
107

    
108
  private void addRotateObjects()
109
    {
110
    DistortedScreen screen = getScreen();
111
    DistortedNode[] screenNodes = mRotate.getScreenNodes();
112

    
113
    if( screen!=null && screenNodes!=null )
114
      {
115
      for (DistortedNode node : screenNodes) screen.attach(node);
116
      }
117

    
118
    ObjectControl control = getObjectControl();
119
    ObjectPreRender pre =  control.getPreRender();
120
    DistortedNode object = pre.getObject();
121
    DistortedNode[] objectNodes = mRotate.getObjectNodes();
122

    
123
    if( object!=null && objectNodes!=null )
124
      {
125
      for (DistortedNode node : objectNodes) object.attach(node);
126
      }
127
    }
128

    
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130

    
131
  private void removeRotateObjects()
132
    {
133
    DistortedScreen screen = getScreen();
134
    DistortedNode[] screenNodes = mRotate.returnScreenNodes();
135

    
136
    if( screen!=null && screenNodes!=null )
137
      {
138
      for (DistortedNode node : screenNodes) screen.detach(node);
139
      }
140

    
141
    ObjectControl control = getObjectControl();
142
    ObjectPreRender pre =  control.getPreRender();
143
    DistortedNode object = pre.getObject();
144
    DistortedNode[] objectNodes = mRotate.returnObjectNodes();
145

    
146
    if( object!=null && objectNodes!=null )
147
      {
148
      for (DistortedNode node : objectNodes) object.detach(node);
149
      }
150
    }
151

    
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153

    
154
  private void finishWhole()
155
    {
156
    removeWholeObjects();
157

    
158
    mWholeReturned = true;
159

    
160
    if( !mRotateReturned ) addRotateObjects();
161
    }
162

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

    
165
  private void finishRotate()
166
    {
167
    removeRotateObjects();
168
    mRotateReturned= true;
169
    mObjectControl.getPreRender().unblockEverything();
170
    }
171

    
172
///////////////////////////////////////////////////////////////////////////////////////////////////
173

    
174
  TwistyActivity getActivity()
175
    {
176
    return mRefAct.get();
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  private ObjectAutomator(ObjectControl control)
182
    {
183
    mObjectControl = control;
184

    
185
    mWhole = new ObjectAutomatorWhole(this);
186
    mRotate= new ObjectAutomatorRotate(this);
187
    }
188

    
189
///////////////////////////////////////////////////////////////////////////////////////////////////
190
// PUBLIC
191

    
192
  public static ObjectAutomator getInstance(ObjectControl control)
193
    {
194
    if( mControl==null ) mControl = new ObjectAutomator(control);
195

    
196
    return mControl;
197
    }
198

    
199
///////////////////////////////////////////////////////////////////////////////////////////////////
200

    
201
  public void postDrawFrame(long time)
202
    {
203
    mWhole.postDrawFrame(time);
204
    }
205

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

    
208
  public void effectFinished(long effectID)
209
    {
210
    if( effectID==mWhole.getEffectID()  ) finishWhole();
211
    if( effectID==mRotate.getEffectID() ) finishRotate();
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  public void animateAll(TwistyActivity act)
217
    {
218
    mObjectControl.getPreRender().blockEverything(BlockController.CONTROL_PLACE_0);
219
    mRefAct = new WeakReference<>(act);
220

    
221
    mWholeReturned = false;
222
    mRotateReturned= false;
223

    
224
    addWholeObjects();
225
    }
226

    
227
///////////////////////////////////////////////////////////////////////////////////////////////////
228

    
229
  public void animateRotate(TwistyActivity act)
230
    {
231
    mObjectControl.getPreRender().blockEverything(BlockController.CONTROL_PLACE_1);
232
    mRefAct = new WeakReference<>(act);
233

    
234
    mWholeReturned = true;
235
    mRotateReturned= false;
236

    
237
    addRotateObjects();
238
    }
239
  }
(1-1/3)