001        package com.croftsoft.apps.jogl.imp;
002        
003        import java.util.*;
004         
005        import com.croftsoft.core.lang.NullArgumentException;
006        import com.croftsoft.core.lang.lifecycle.Updatable;
007        import com.croftsoft.core.media.jogl.camera.JoglCamera;
008        import com.croftsoft.core.media.jogl.camera.JoglCameraImp;
009        import com.croftsoft.core.media.jogl.camera.JoglCameraMut;
010        import com.croftsoft.core.media.jogl.render.JoglSpinCube;
011        import com.croftsoft.core.util.mail.Mail;
012    
013        import com.croftsoft.apps.jogl.JoglConfig;
014        import com.croftsoft.apps.jogl.JoglMessage;
015        import com.croftsoft.apps.jogl.JoglModel;
016        import com.croftsoft.apps.jogl.JoglMoveRequest;
017        import com.croftsoft.apps.jogl.JoglMoveState;
018        import com.croftsoft.apps.jogl.JoglMoveState.EnumDirection;
019    
020        /***********************************************************************
021        * Model.
022        * 
023        * Maintains program state.
024        * 
025        * @version
026        *   $Id: JoglModelImp.java,v 1.19 2008/05/17 01:32:31 croft Exp $
027        * @since
028        *   2008-02-10
029        * @author
030        *   <a href="https://www.croftsoft.com/">David Wallace Croft</a>
031        ***********************************************************************/
032    
033        public final class  JoglModelImp
034          implements JoglModel, Updatable
035        ////////////////////////////////////////////////////////////////////////
036        ////////////////////////////////////////////////////////////////////////
037        {
038          
039        private static final double [ ]
040          PERTURBATION_FACTORS = { 0.01, 0.10, 1.00, 0 };
041        
042        private static final double
043          ROTATE_DELTA    = 1,
044          TRANSLATE_DELTA = 0.1;
045           
046        // private final instance variables
047        
048        private final Mail<JoglMessage>      mail;
049        
050        private final JoglMoveState          joglMoveState;
051        
052        private final JoglCameraMut          joglCameraMut;
053        
054        private final JoglSpinCube.ModelImp  joglSpinCubeModelImp;
055           
056        // model state instance variables
057         
058        private int  perturbationFactorIndex;
059        
060        private final Map<EnumDouble, Double>  doubleMap;
061        
062        ////////////////////////////////////////////////////////////////////////
063        ////////////////////////////////////////////////////////////////////////
064         
065        public  JoglModelImp (
066          final JoglConfig         joglConfig,
067          final Mail<JoglMessage>  mail )
068        ////////////////////////////////////////////////////////////////////////
069        {
070          NullArgumentException.checkArgs (
071            joglConfig,
072            this.mail = mail );
073          
074          doubleMap = new HashMap<EnumDouble, Double> ( );
075          
076          joglMoveState = new JoglMoveState ( );
077          
078          joglCameraMut = new JoglCameraImp ( );
079          
080          joglSpinCubeModelImp = new JoglSpinCube.ModelImp ( );
081                
082          joglSpinCubeModelImp.setXYZ ( 0, 0, -10 );
083          
084          joglSpinCubeModelImp.setPerturbationFactor (
085            PERTURBATION_FACTORS [ perturbationFactorIndex ] );
086        }
087         
088        ////////////////////////////////////////////////////////////////////////
089        // interface Accessor methods
090        ////////////////////////////////////////////////////////////////////////
091         
092        public double  get ( final EnumDouble  enumDouble )
093        ////////////////////////////////////////////////////////////////////////
094        {
095          final Double  d = doubleMap.get ( enumDouble );
096          
097          return d == null ? 0 : d.doubleValue ( );
098        }
099        
100        public JoglCamera  getJoglCamera ( )
101        ////////////////////////////////////////////////////////////////////////
102        {
103          return joglCameraMut;
104        }
105        
106        public JoglSpinCube.Model  getJoglSpinCubeModel ( )
107        ////////////////////////////////////////////////////////////////////////
108        {
109          return joglSpinCubeModelImp;
110        }
111        
112        ////////////////////////////////////////////////////////////////////////
113        // lifecycle methods
114        ////////////////////////////////////////////////////////////////////////
115         
116        public void  update ( )
117        ////////////////////////////////////////////////////////////////////////
118        {
119          final int  size = mail.size ( );
120          
121          for ( int  i = 0; i < size; i++ )
122          {
123            final JoglMessage  joglMessage = mail.get ( i );
124            
125            final JoglMessage.Type  type = joglMessage.getType ( );
126             
127            switch ( type )
128            {
129              case CHANGE_PERTURBATION_FACTOR_REQUEST:
130                 
131                perturbationFactorIndex = ( perturbationFactorIndex + 1 )
132                  % PERTURBATION_FACTORS.length;
133                
134                joglSpinCubeModelImp.setPerturbationFactor (
135                  PERTURBATION_FACTORS [ perturbationFactorIndex ] );
136                
137                mail.offer (
138                  JoglMessage.CHANGE_PERTURBATION_FACTOR_EVENT_INSTANCE );
139                 
140                break;
141                
142              case MOVE_REQUEST:
143                
144                processMoveRequest ( ( JoglMoveRequest ) joglMessage );
145                
146                break;
147                 
148              default:
149                
150                // ignore
151            }
152          }
153          
154          joglSpinCubeModelImp.update ( );
155          
156          updateMove ( );
157        }
158        
159        ////////////////////////////////////////////////////////////////////////
160        // private methods
161        ////////////////////////////////////////////////////////////////////////
162        
163        private void  processMoveRequest (
164          final JoglMoveRequest  joglMoveRequest )
165        ////////////////////////////////////////////////////////////////////////
166        {
167          final EnumDirection [ ]  enumDirections = EnumDirection.values ( );
168          
169          for ( final EnumDirection  enumDirection : enumDirections )
170          {
171            joglMoveState.set (
172              enumDirection,
173              joglMoveRequest.get ( enumDirection ) );
174          }
175        }
176    /*    
177        private void  set (
178          final EnumDouble  enumDouble,
179          final double      value )
180        ////////////////////////////////////////////////////////////////////////
181        {
182          doubleMap.put ( enumDouble, new Double ( value ) );
183        }
184    */    
185        private void  updateMove ( )
186        ////////////////////////////////////////////////////////////////////////
187        {
188          int
189            stepX    = 0,
190            stepY    = 0,
191            stepZ    = 0,
192            stepRotX = 0,
193            stepRotY = 0,
194            stepRotZ = 0;
195          
196          final EnumDirection [ ]  enumDirections = EnumDirection.values ( );
197          
198          for ( final EnumDirection  enumDirection : enumDirections )
199          {
200            if ( joglMoveState.get ( enumDirection ) )
201            {
202              switch ( enumDirection )
203              {
204                case BACKWARD:
205                  
206                  stepZ++;
207              
208                  break;
209                  
210                case DOWN:
211                  
212                  stepY--;
213              
214                  break;
215                  
216                case FORWARD:
217              
218                  stepZ--;
219              
220                  break;
221              
222                case LEFT:
223                  
224                  stepX--;
225                  
226                  break;
227                  
228                case PITCH_DOWN:
229                  
230                  stepRotX--;
231                  
232                  break;             
233              
234                case PITCH_UP:
235                  
236                  stepRotX++;
237                  
238                  break;             
239              
240                case RIGHT:
241                  
242                  stepX++;
243                  
244                  break;
245              
246                case ROLL_LEFT:
247                  
248                  stepRotZ++;
249                  
250                  break;             
251              
252                case ROLL_RIGHT:
253                  
254                  stepRotZ--;
255                  
256                  break;             
257              
258                case UP:
259                  
260                  stepY++;
261              
262                  break;
263                  
264                case YAW_LEFT:
265                  
266                  stepRotY++;
267                  
268                  break;             
269              
270                case YAW_RIGHT:
271                  
272                  stepRotY--;
273                  
274                  break;             
275              
276                default:
277               
278                  throw new RuntimeException ( );
279              }
280            }
281          }
282          
283          if ( ( stepX != 0 )
284            || ( stepY != 0 )
285            || ( stepZ != 0 ) )
286          {
287            if ( stepX != 0 )
288            {
289              joglCameraMut.translate (
290                JoglCamera.Axis.X,
291                TRANSLATE_DELTA * stepX );
292            }      
293    
294            if ( stepY != 0 )
295            {
296              joglCameraMut.translate (
297                JoglCamera.Axis.Y,
298                TRANSLATE_DELTA * stepY );
299            }      
300    
301            if ( stepZ != 0 )
302            {
303              joglCameraMut.translate (
304                JoglCamera.Axis.Z,
305                TRANSLATE_DELTA * stepZ );
306            }      
307          }
308          
309          if ( ( stepRotX != 0 )
310            || ( stepRotY != 0 )
311            || ( stepRotZ != 0 ) )
312          {
313            if ( stepRotX != 0 )
314            {
315              joglCameraMut.rotate (
316                JoglCamera.Axis.X,
317                ROTATE_DELTA * stepRotX );
318            }
319    
320            if ( stepRotY != 0 )
321            {
322              joglCameraMut.rotate (
323                JoglCamera.Axis.Y,
324                ROTATE_DELTA * stepRotY );
325            }
326    
327            if ( stepRotZ != 0 )
328            {
329              joglCameraMut.rotate (
330                JoglCamera.Axis.Z,
331                ROTATE_DELTA * stepRotZ );
332            }
333          }
334        }
335         
336        ////////////////////////////////////////////////////////////////////////
337        ////////////////////////////////////////////////////////////////////////
338        }