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.AppSettings;
11 import com.jme3.system.JmeContext;
12 import com.jme3.system.JmeSystem;
13 import com.jme3.system.SystemListener;
14 import com.jme3.system.Timer;
15 import java.util.ArrayList;
17 public class AwtPanelsContext implements JmeContext {
19 protected JmeContext actualContext;
20 protected AppSettings settings = new AppSettings(true);
21 protected SystemListener listener;
22 protected ArrayList<AwtPanel> panels = new ArrayList<AwtPanel>();
23 protected AwtPanel inputSource;
25 protected AwtMouseInput mouseInput = new AwtMouseInput();
26 protected AwtKeyInput keyInput = new AwtKeyInput();
28 protected boolean lastThrottleState = false;
30 private class AwtPanelsListener implements SystemListener {
32 public void initialize() {
36 public void reshape(int width, int height) {
37 throw new IllegalStateException();
40 public void update() {
44 public void requestClose(boolean esc) {
46 throw new IllegalStateException();
49 public void gainFocus() {
51 throw new IllegalStateException();
54 public void loseFocus() {
56 throw new IllegalStateException();
59 public void handleError(String errorMsg, Throwable t) {
60 listener.handleError(errorMsg, t);
63 public void destroy() {
68 public void setInputSource(AwtPanel panel){
69 if (!panels.contains(panel))
70 throw new IllegalArgumentException();
73 mouseInput.setInputSource(panel);
74 keyInput.setInputSource(panel);
77 public Type getType() {
78 return Type.OffscreenSurface;
81 public void setSystemListener(SystemListener listener) {
82 this.listener = listener;
85 public AppSettings getSettings() {
89 public Renderer getRenderer() {
90 return actualContext.getRenderer();
93 public MouseInput getMouseInput() {
97 public KeyInput getKeyInput() {
101 public JoyInput getJoyInput() {
105 public TouchInput getTouchInput() {
109 public Timer getTimer() {
110 return actualContext.getTimer();
113 public boolean isCreated() {
114 return actualContext != null && actualContext.isCreated();
117 public boolean isRenderable() {
118 return actualContext != null && actualContext.isRenderable();
121 public AwtPanelsContext(){
124 public AwtPanel createPanel(PaintMode paintMode){
125 AwtPanel panel = new AwtPanel(paintMode);
130 private void initInThread(){
131 listener.initialize();
134 private void updateInThread(){
135 // Check if throttle required
136 boolean needThrottle = true;
138 for (AwtPanel panel : panels){
139 if (panel.isActiveDrawing()){
140 needThrottle = false;
145 if (lastThrottleState != needThrottle){
146 lastThrottleState = needThrottle;
147 if (lastThrottleState){
148 System.out.println("OGL: Throttling update loop.");
150 System.out.println("OGL: Ceased throttling update loop.");
157 } catch (InterruptedException ex) {
164 private void destroyInThread(){
168 public void setSettings(AppSettings settings) {
169 this.settings.copyFrom(settings);
170 this.settings.setRenderer(AppSettings.LWJGL_OPENGL2);
171 if (actualContext != null){
172 actualContext.setSettings(settings);
176 public void create(boolean waitFor) {
177 if (actualContext != null){
178 throw new IllegalStateException("Already created");
181 actualContext = JmeSystem.newContext(settings, Type.OffscreenSurface);
182 actualContext.setSystemListener(new AwtPanelsListener());
183 actualContext.create(waitFor);
186 public void destroy(boolean waitFor) {
187 if (actualContext == null)
188 throw new IllegalStateException("Not created");
190 // destroy parent context
191 actualContext.destroy(waitFor);
194 public void setTitle(String title) {
195 // not relevant, ignore
198 public void setAutoFlushFrames(boolean enabled) {
199 // not relevant, ignore
202 public void restart() {
203 // only relevant if changing pixel format.