001 package com.croftsoft.apps.jogl; 002 003 import java.awt.event.*; 004 005 import com.croftsoft.apps.jogl.JoglMoveState.EnumDirection; 006 import com.croftsoft.core.gui.controller.NilController; 007 import com.croftsoft.core.lang.NullArgumentException; 008 import com.croftsoft.core.lang.lifecycle.Startable; 009 import com.croftsoft.core.util.mail.Mail; 010 011 /*********************************************************************** 012 * Jogl controller. 013 * 014 * Modifies the Model based on user input. 015 * 016 * @version 017 * $Id: JoglController.java,v 1.12 2008/05/17 00:18:02 croft Exp $ 018 * @since 019 * 2008-02-10 020 * @author 021 * <a href="https://www.croftsoft.com/">David Wallace Croft</a> 022 ***********************************************************************/ 023 024 public final class JoglController 025 extends NilController 026 implements Startable 027 //////////////////////////////////////////////////////////////////////// 028 //////////////////////////////////////////////////////////////////////// 029 { 030 031 private final Mail<JoglMessage> mail; 032 033 private final JoglMoveRequest joglMoveRequest; 034 035 // 036 037 private boolean 038 mouseClicked, 039 sendMoveRequest; 040 041 //////////////////////////////////////////////////////////////////////// 042 // constructor method 043 //////////////////////////////////////////////////////////////////////// 044 045 public JoglController ( final Mail<JoglMessage> mail ) 046 //////////////////////////////////////////////////////////////////////// 047 { 048 NullArgumentException.checkArgs ( this.mail = mail ); 049 050 joglMoveRequest = new JoglMoveRequest ( ); 051 } 052 053 //////////////////////////////////////////////////////////////////////// 054 // listener methods 055 //////////////////////////////////////////////////////////////////////// 056 057 @Override 058 public void keyPressed ( final KeyEvent keyEvent ) 059 //////////////////////////////////////////////////////////////////////// 060 { 061 processKeyEvent ( keyEvent, true ); 062 } 063 064 @Override 065 public void keyReleased ( final KeyEvent keyEvent ) 066 //////////////////////////////////////////////////////////////////////// 067 { 068 processKeyEvent ( keyEvent, false ); 069 } 070 071 @Override 072 public void mouseClicked ( final MouseEvent mouseEvent ) 073 //////////////////////////////////////////////////////////////////////// 074 { 075 mouseClicked = true; 076 } 077 078 @Override 079 public void mouseMoved ( final MouseEvent mouseEvent ) 080 //////////////////////////////////////////////////////////////////////// 081 { 082 //mouseMoved = true; 083 } 084 085 //////////////////////////////////////////////////////////////////////// 086 // lifecycle methods 087 //////////////////////////////////////////////////////////////////////// 088 089 public void start ( ) 090 //////////////////////////////////////////////////////////////////////// 091 { 092 mouseClicked = false; 093 } 094 095 @Override 096 public void update ( ) 097 //////////////////////////////////////////////////////////////////////// 098 { 099 if ( sendMoveRequest ) 100 { 101 sendMoveRequest = !mail.offer ( joglMoveRequest ); 102 } 103 104 if ( mouseClicked ) 105 { 106 mouseClicked = !mail.offer ( 107 JoglMessage.CHANGE_PERTURBATION_FACTOR_REQUEST_INSTANCE ); 108 } 109 } 110 111 //////////////////////////////////////////////////////////////////////// 112 // private methods 113 //////////////////////////////////////////////////////////////////////// 114 115 private EnumDirection getEnumDirection ( final KeyEvent keyEvent ) 116 //////////////////////////////////////////////////////////////////////// 117 { 118 final int keyCode = keyEvent.getKeyCode ( ); 119 120 final boolean shiftDown = keyEvent.isShiftDown ( ); 121 122 switch ( keyCode ) 123 { 124 case KeyEvent.VK_A: 125 126 return EnumDirection.LEFT; 127 128 case KeyEvent.VK_D: 129 130 return EnumDirection.RIGHT; 131 132 case KeyEvent.VK_W: 133 134 return EnumDirection.FORWARD; 135 136 case KeyEvent.VK_S: 137 138 return EnumDirection.BACKWARD; 139 140 case KeyEvent.VK_UP: 141 142 if ( shiftDown ) 143 { 144 return EnumDirection.PITCH_DOWN; 145 } 146 147 return EnumDirection.UP; 148 149 case KeyEvent.VK_DOWN: 150 151 if ( shiftDown ) 152 { 153 return EnumDirection.PITCH_UP; 154 } 155 156 return EnumDirection.DOWN; 157 158 case KeyEvent.VK_LEFT: 159 160 if ( shiftDown ) 161 { 162 return EnumDirection.ROLL_LEFT; 163 } 164 165 return EnumDirection.YAW_LEFT; 166 167 case KeyEvent.VK_RIGHT: 168 169 if ( shiftDown ) 170 { 171 return EnumDirection.ROLL_RIGHT; 172 } 173 174 return EnumDirection.YAW_RIGHT; 175 176 default: 177 178 return null; 179 } 180 } 181 182 private void processKeyEvent ( 183 final KeyEvent keyEvent, 184 final boolean keyPressed ) 185 //////////////////////////////////////////////////////////////////////// 186 { 187 final EnumDirection enumDirection = getEnumDirection ( keyEvent ); 188 189 if ( enumDirection != null ) 190 { 191 joglMoveRequest.set ( enumDirection, keyPressed ); 192 193 sendMoveRequest = true; 194 } 195 } 196 197 //////////////////////////////////////////////////////////////////////// 198 //////////////////////////////////////////////////////////////////////// 199 }