1 package com.jme3.system.awt;
3 import com.jme3.input.JoyInput;
4 import com.jme3.input.KeyInput;
5 import com.jme3.input.MouseInput;
6 import com.jme3.input.TouchInput;
7 import com.jme3.input.awt.AwtKeyInput;
8 import com.jme3.input.awt.AwtMouseInput;
9 import com.jme3.renderer.Renderer;
10 import com.jme3.system.*;
12 import java.util.ArrayList;
14 public class AwtPanelsContext implements JmeContext {
16 protected JmeContext actualContext;
17 protected AppSettings settings = new AppSettings(true);
18 protected SystemListener listener;
19 protected ArrayList<AwtPanel> panels = new ArrayList<AwtPanel>();
20 protected AwtPanel inputSource;
22 protected AwtMouseInput mouseInput = new AwtMouseInput();
23 protected AwtKeyInput keyInput = new AwtKeyInput();
25 protected boolean lastThrottleState = false;
27 private class AwtPanelsListener implements SystemListener {
29 public void initialize() {
33 public void reshape(int width, int height) {
34 throw new IllegalStateException();
37 public void update() {
41 public void requestClose(boolean esc) {
43 throw new IllegalStateException();
46 public void gainFocus() {
48 throw new IllegalStateException();
51 public void loseFocus() {
53 throw new IllegalStateException();
56 public void handleError(String errorMsg, Throwable t) {
57 listener.handleError(errorMsg, t);
60 public void destroy() {
65 public void setInputSource(AwtPanel panel){
66 if (!panels.contains(panel))
67 throw new IllegalArgumentException();
70 mouseInput.setInputSource(panel);
71 keyInput.setInputSource(panel);
74 public Type getType() {
75 return Type.OffscreenSurface;
78 public void setSystemListener(SystemListener listener) {
79 this.listener = listener;
82 public AppSettings getSettings() {
86 public Renderer getRenderer() {
87 return actualContext.getRenderer();
90 public MouseInput getMouseInput() {
94 public KeyInput getKeyInput() {
98 public JoyInput getJoyInput() {
102 public TouchInput getTouchInput() {
106 public Timer getTimer() {
107 return actualContext.getTimer();
110 public boolean isCreated() {
111 return actualContext != null && actualContext.isCreated();
114 public boolean isRenderable() {
115 return actualContext != null && actualContext.isRenderable();
118 public AwtPanelsContext(){
121 public AwtPanel createPanel(PaintMode paintMode){
122 AwtPanel panel = new AwtPanel(paintMode);
127 private void initInThread(){
128 listener.initialize();
131 private void updateInThread(){
132 // Check if throttle required
133 boolean needThrottle = true;
135 for (AwtPanel panel : panels){
136 if (panel.isActiveDrawing()){
137 needThrottle = false;
142 if (lastThrottleState != needThrottle){
143 lastThrottleState = needThrottle;
144 if (lastThrottleState){
145 System.out.println("OGL: Throttling update loop.");
147 System.out.println("OGL: Ceased throttling update loop.");
154 } catch (InterruptedException ex) {
161 private void destroyInThread(){
165 public void setSettings(AppSettings settings) {
166 this.settings.copyFrom(settings);
167 this.settings.setRenderer(AppSettings.LWJGL_OPENGL2);
168 if (actualContext != null){
169 actualContext.setSettings(settings);
173 public void create(boolean waitFor) {
174 if (actualContext != null){
175 throw new IllegalStateException("Already created");
178 actualContext = JmeSystem.newContext(settings, Type.OffscreenSurface);
179 actualContext.setSystemListener(new AwtPanelsListener());
180 actualContext.create(waitFor);
183 public void destroy(boolean waitFor) {
184 if (actualContext == null)
185 throw new IllegalStateException("Not created");
187 // destroy parent context
188 actualContext.destroy(waitFor);
191 public void setTitle(String title) {
192 // not relevant, ignore
195 public void setAutoFlushFrames(boolean enabled) {
196 // not relevant, ignore
199 public void restart() {
200 // only relevant if changing pixel format.