Home | History | Annotate | Download | only in awt
      1 package com.jme3.system.awt;
      2 
      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.*;
     11 import java.util.ArrayList;
     12 
     13 public class AwtPanelsContext implements JmeContext {
     14 
     15     protected JmeContext actualContext;
     16     protected AppSettings settings = new AppSettings(true);
     17     protected SystemListener listener;
     18     protected ArrayList<AwtPanel> panels = new ArrayList<AwtPanel>();
     19     protected AwtPanel inputSource;
     20 
     21     protected AwtMouseInput mouseInput = new AwtMouseInput();
     22     protected AwtKeyInput keyInput = new AwtKeyInput();
     23 
     24     protected boolean lastThrottleState = false;
     25 
     26     private class AwtPanelsListener implements SystemListener {
     27 
     28         public void initialize() {
     29             initInThread();
     30         }
     31 
     32         public void reshape(int width, int height) {
     33             throw new IllegalStateException();
     34         }
     35 
     36         public void update() {
     37             updateInThread();
     38         }
     39 
     40         public void requestClose(boolean esc) {
     41             // shouldn't happen
     42             throw new IllegalStateException();
     43         }
     44 
     45         public void gainFocus() {
     46             // shouldn't happen
     47             throw new IllegalStateException();
     48         }
     49 
     50         public void loseFocus() {
     51             // shouldn't happen
     52             throw new IllegalStateException();
     53         }
     54 
     55         public void handleError(String errorMsg, Throwable t) {
     56             listener.handleError(errorMsg, t);
     57         }
     58 
     59         public void destroy() {
     60             destroyInThread();
     61         }
     62     }
     63 
     64     public void setInputSource(AwtPanel panel){
     65         if (!panels.contains(panel))
     66             throw new IllegalArgumentException();
     67 
     68         inputSource = panel;
     69         mouseInput.setInputSource(panel);
     70         keyInput.setInputSource(panel);
     71     }
     72 
     73     public Type getType() {
     74         return Type.OffscreenSurface;
     75     }
     76 
     77     public void setSystemListener(SystemListener listener) {
     78         this.listener = listener;
     79     }
     80 
     81     public AppSettings getSettings() {
     82         return settings;
     83     }
     84 
     85     public Renderer getRenderer() {
     86         return actualContext.getRenderer();
     87     }
     88 
     89     public MouseInput getMouseInput() {
     90         return mouseInput;
     91     }
     92 
     93     public KeyInput getKeyInput() {
     94         return keyInput;
     95     }
     96 
     97     public JoyInput getJoyInput() {
     98         return null;
     99     }
    100 
    101     public TouchInput getTouchInput() {
    102         return null;
    103     }
    104 
    105     public Timer getTimer() {
    106         return actualContext.getTimer();
    107     }
    108 
    109     public boolean isCreated() {
    110         return actualContext != null && actualContext.isCreated();
    111     }
    112 
    113     public boolean isRenderable() {
    114         return actualContext != null && actualContext.isRenderable();
    115     }
    116 
    117     public AwtPanelsContext(){
    118     }
    119 
    120     public AwtPanel createPanel(PaintMode paintMode){
    121         AwtPanel panel = new AwtPanel(paintMode);
    122         panels.add(panel);
    123         return panel;
    124     }
    125 
    126     private void initInThread(){
    127         listener.initialize();
    128     }
    129 
    130     private void updateInThread(){
    131         // Check if throttle required
    132         boolean needThrottle = true;
    133 
    134         for (AwtPanel panel : panels){
    135             if (panel.isActiveDrawing()){
    136                 needThrottle = false;
    137                 break;
    138             }
    139         }
    140 
    141         if (lastThrottleState != needThrottle){
    142             lastThrottleState = needThrottle;
    143             if (lastThrottleState){
    144                 System.out.println("OGL: Throttling update loop.");
    145             }else{
    146                 System.out.println("OGL: Ceased throttling update loop.");
    147             }
    148         }
    149 
    150         if (needThrottle){
    151             try {
    152                 Thread.sleep(100);
    153             } catch (InterruptedException ex) {
    154             }
    155         }
    156 
    157         listener.update();
    158     }
    159 
    160     private void destroyInThread(){
    161         listener.destroy();
    162     }
    163 
    164     public void setSettings(AppSettings settings) {
    165         this.settings.copyFrom(settings);
    166         this.settings.setRenderer(AppSettings.LWJGL_OPENGL2);
    167         if (actualContext != null){
    168             actualContext.setSettings(settings);
    169         }
    170     }
    171 
    172     public void create(boolean waitFor) {
    173         if (actualContext != null){
    174             throw new IllegalStateException("Already created");
    175         }
    176 
    177         actualContext = JmeSystem.newContext(settings, Type.OffscreenSurface);
    178         actualContext.setSystemListener(new AwtPanelsListener());
    179         actualContext.create(waitFor);
    180     }
    181 
    182     public void destroy(boolean waitFor) {
    183         if (actualContext == null)
    184             throw new IllegalStateException("Not created");
    185 
    186         // destroy parent context
    187         actualContext.destroy(waitFor);
    188     }
    189 
    190     public void setTitle(String title) {
    191         // not relevant, ignore
    192     }
    193 
    194     public void setAutoFlushFrames(boolean enabled) {
    195         // not relevant, ignore
    196     }
    197 
    198     public void restart() {
    199         // only relevant if changing pixel format.
    200     }
    201 
    202 }
    203