gdx-studio

#libgdx#java#desktop

git clone https://git.pyrossh.dev/gdx-studio

An IDE for creating Games using libgdx and Java supported on all platforms Android, iOS, Desktop


src/gdxstudio/StatusBar.java
a9b2a64 1
package gdxstudio;
a62d533 2
import java.awt.Color;
a62d533 3
import java.awt.Graphics;
a62d533 4
import java.awt.Graphics2D;
a62d533 5
import java.awt.Shape;
a62d533 6
import java.awt.event.ActionEvent;
a62d533 7
import java.awt.event.ActionListener;
a62d533 8
import java.awt.event.FocusAdapter;
a62d533 9
import java.awt.event.FocusEvent;
a62d533 10
import java.awt.event.MouseAdapter;
a62d533 11
import java.awt.event.MouseEvent;
a62d533 12
import java.awt.geom.RoundRectangle2D;
a62d533 13
import java.lang.management.ManagementFactory;
a62d533 14
import java.lang.management.MemoryUsage;
a62d533 15
import java.text.DecimalFormat;
a62d533 16
7c84877 17
import javax.swing.BorderFactory;
a62d533 18
import javax.swing.JButton;
a62d533 19
import javax.swing.JLabel;
a62d533 20
import javax.swing.JPanel;
a62d533 21
import javax.swing.SwingUtilities;
a62d533 22
a62d533 23
import web.laf.lite.layout.ToolbarLayout;
a62d533 24
import web.laf.lite.utils.LafUtils;
a62d533 25
import web.laf.lite.utils.UIUtils;
a62d533 26
import web.laf.lite.utils.UpdateTimer;
a62d533 27
a62d533 28
a62d533 29
final public class StatusBar extends JPanel {
a62d533 30
	private static final long serialVersionUID = 1L;
a62d533 31
	public static JLabel caret, xy, selected, error, warning;
a62d533 32
a62d533 33
	public StatusBar(){
a62d533 34
		 super(new ToolbarLayout());
a62d533 35
		 UIUtils.setUndecorated(this, true);
a62d533 36
	     xy = new JLabel("<html><b>x: &nbsp</b>0<b>&nbsp y: &nbsp</b>0</html>");
a62d533 37
	     selected = new JLabel("<html><b>Selected: </b> &nbsp None</html>");
a62d533 38
	     caret = new JLabel("<html><b>Line</b> 15 <b>Column</b> 42</html>");
a62d533 39
         add(caret, ToolbarLayout.START);
a62d533 40
         addSpace();
a62d533 41
         addSeparator();
a9b2a64 42
         //addSpace();
a9b2a64 43
         //add(selected, ToolbarLayout.START);
a9b2a64 44
         //addSpace();
a9b2a64 45
         //addSeparator();
a62d533 46
         
a62d533 47
         addSpace();
a62d533 48
         add(xy, ToolbarLayout.START);
a62d533 49
         addSpace();
a62d533 50
         
a62d533 51
         addSeparator();
a62d533 52
     
a62d533 53
         addSpace();
a62d533 54
         initError();
a62d533 55
         addSpace();
a62d533 56
         
a62d533 57
         addSeparator();
a62d533 58
         
a62d533 59
         addSpace();
a62d533 60
         initWarning();
a62d533 61
         addSpace();
a62d533 62
         
a62d533 63
         addSeparator();
a62d533 64
         
a62d533 65
         addSpace();
a62d533 66
         initConsole();
a62d533 67
         addSpace();
a62d533 68
         
a62d533 69
         addSeparator();
a62d533 70
         
a62d533 71
         addSpace();
a62d533 72
         initAsset();
a62d533 73
         addSpace();
a62d533 74
         
a9b2a64 75
         addSeparator();
a9b2a64 76
         
a9b2a64 77
         addSpace();
a9b2a64 78
         initWidget();
a9b2a64 79
         addSpace();
a9b2a64 80
         
a62d533 81
         addSeparator();
a62d533 82
        
a62d533 83
         //initUpdate();
a62d533 84
         //initNuke(); //Reduce nuke size
a62d533 85
         add(new MemoryBar(), ToolbarLayout.END);
7c84877 86
         setBorder(BorderFactory.createMatteBorder(1, 0, 0, 0, Style.border));
a62d533 87
	}
a62d533 88
	
a62d533 89
	public void addSpace(){
a62d533 90
		add(new JLabel("       "), ToolbarLayout.START);
a62d533 91
	}
a62d533 92
	
a62d533 93
	public void addSeparator(){
7c84877 94
		JLabel comp = new JLabel();
7c84877 95
		comp.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Style.border));
7c84877 96
		add(comp, ToolbarLayout.START);
a62d533 97
	}
a62d533 98
a62d533 99
	public static void updateCaret(int row, int col){
a62d533 100
		caret.setText("<html><b>Line</b> "+row+" <b>Column</b> "+col+"</html>");
a62d533 101
	}
a62d533 102
	
a62d533 103
	public static void updateXY(float x, float y){
a62d533 104
		xy.setText("<html><b>x:&nbsp</b>"+(int)x+"<b>&nbsp y:&nbsp</b>"+(int)y+"</html>");
a62d533 105
	}
a62d533 106
	
a62d533 107
	public static void updateSelected(String text){
a62d533 108
		selected.setText("<html><b>Selected:</b> &nbsp "+text+"</html>");
a62d533 109
	}
a62d533 110
	
a62d533 111
	void initError(){
a62d533 112
		error = new JLabel("Errors");
a62d533 113
		UIUtils.setBoldFont(error);
a62d533 114
        add(error, ToolbarLayout.START);
a62d533 115
	}
a62d533 116
	
a62d533 117
	void initWarning(){
a62d533 118
		warning = new JLabel("Warnings");
a62d533 119
		UIUtils.setBoldFont(warning);
a62d533 120
        add(warning, ToolbarLayout.START);
a62d533 121
	}
a62d533 122
	
a62d533 123
	void initConsole(){
a62d533 124
		final JButton console = Style.createMenuButton("<html><b>Console</b>");
a62d533 125
		console.addActionListener(new ActionListener(){
a62d533 126
			@Override
a62d533 127
			public void actionPerformed(ActionEvent arg0) {
a62d533 128
				Content.toggleConsole();
a62d533 129
			}
a62d533 130
		});
a62d533 131
        add(console, ToolbarLayout.START);
a62d533 132
	}
a62d533 133
	
a62d533 134
	void initAsset(){
a62d533 135
		final JButton asset = Style.createMenuButton("<html><b>Assets</b>");
a62d533 136
		asset.addActionListener(new ActionListener(){
a62d533 137
			@Override
a62d533 138
			public void actionPerformed(ActionEvent arg0) {
a62d533 139
				Content.toggleAsset();
a62d533 140
			}
a62d533 141
		});
a62d533 142
		add(asset);
a62d533 143
	}
a62d533 144
	
a9b2a64 145
	void initWidget(){
a9b2a64 146
		final JButton widget = Style.createMenuButton("<html><b>Widgets</b>");
a9b2a64 147
		widget.addActionListener(new ActionListener(){
a9b2a64 148
			@Override
a9b2a64 149
			public void actionPerformed(ActionEvent arg0) {
a9b2a64 150
				Content.toggleWidget();
a9b2a64 151
			}
a9b2a64 152
		});
a9b2a64 153
		add(widget);
a9b2a64 154
	}
a9b2a64 155
	
a62d533 156
	void initZoomIn(){
7c84877 157
		add(Style.createToolPanel("ZoomIn", "szoomin", new ActionListener(){
a62d533 158
			@Override
a62d533 159
			public void actionPerformed(ActionEvent arg0) {
a62d533 160
				Content.editor.zoomin();
a62d533 161
			}
7c84877 162
		}));
a62d533 163
	}
a62d533 164
	
a62d533 165
	void initZoomOut(){
7c84877 166
		add(Style.createToolPanel("ZoomOut", "szoomout", new ActionListener(){
a62d533 167
			@Override
a62d533 168
			public void actionPerformed(ActionEvent arg0) {
a62d533 169
				Content.editor.zoomout();
a62d533 170
			}
7c84877 171
		}));
a62d533 172
	}
a62d533 173
	
a62d533 174
	void initUpdate(){
7c84877 175
		/*final JButton update = Style.createExplorerToolButton("Update", "supdate", null);
a62d533 176
		ButtonPopup menu = new ButtonPopup(update, PopupWay.upCenter);
a62d533 177
        JPanel popupContent = new JPanel (new VerticalFlowLayout(5, 10));
a62d533 178
        popupContent.add(UIUtils.setBoldFont(new JLabel("SkyCode v0.39")));
a62d533 179
        popupContent.add(new JSeparator(SwingConstants.HORIZONTAL));
a62d533 180
        popupContent.add(new JLabel("Added Stuff"));
a62d533 181
        popupContent.add(new JLabel("Bug Fixes"));
a62d533 182
        popupContent.add(new JLabel("Bug Fixes"));
a62d533 183
        popupContent.add(new JLabel("Bug Fixes"));
a62d533 184
        popupContent.add(new JLabel("Bug Fixes"));
a62d533 185
        popupContent.setOpaque(false);
a62d533 186
        menu.setContent (popupContent);
7c84877 187
		add(update, ToolbarLayout.END);*/
a62d533 188
	}
a62d533 189
	
a62d533 190
	void initNuke(){
7c84877 191
		add(Style.createToolPanel("Free Memory", "snuke", new ActionListener(){
a62d533 192
			@Override
a62d533 193
			public void actionPerformed(ActionEvent arg0) {
a62d533 194
				System.gc();
a62d533 195
			}
7c84877 196
		}));
7c84877 197
	}
a62d533 198
}
a62d533 199
a62d533 200
class MemoryBar extends JLabel{
a62d533 201
	private static final long serialVersionUID = 1L;
a62d533 202
	public static final String THREAD_NAME = "MemoryBar.updater";
a62d533 203
a62d533 204
    private Color usedBorderColor = new Color ( 130, 130, 183 );
a62d533 205
    private Color usedFillColor = new Color ( 0, 0, 255, 50 );
a62d533 206
a62d533 207
    private long usedMemory = 0;
a62d533 208
    private long allocatedMemory = 0;
a62d533 209
a62d533 210
    private int refreshRate = 1000;
a62d533 211
    
a62d533 212
    UpdateTimer updater = null;
a62d533 213
a62d533 214
    private boolean pressed = false;
a62d533 215
a62d533 216
    public MemoryBar ()
a62d533 217
    {
a62d533 218
        super ();
a62d533 219
a62d533 220
        setOpaque ( false );
a62d533 221
        setFocusable ( true );
a62d533 222
        setHorizontalAlignment ( JLabel.CENTER );
a62d533 223
a62d533 224
        updateMemory ();
a62d533 225
       
a62d533 226
        addMouseListener ( new MouseAdapter ()
a62d533 227
        {
a62d533 228
            @Override
a62d533 229
            public void mousePressed ( MouseEvent e )
a62d533 230
            {
a62d533 231
                if (SwingUtilities.isLeftMouseButton ( e ) )
a62d533 232
                {
a62d533 233
                    pressed = true;
a62d533 234
                    requestFocusInWindow ();
a62d533 235
                    doGC ();
a62d533 236
                }
a62d533 237
            }
a62d533 238
a62d533 239
            @Override
a62d533 240
            public void mouseReleased ( MouseEvent e )
a62d533 241
            {
a62d533 242
                if ( pressed && SwingUtilities.isLeftMouseButton ( e ) )
a62d533 243
                {
a62d533 244
                    pressed = false;
a62d533 245
                    repaint ();
a62d533 246
                }
a62d533 247
            }
a62d533 248
        } );
a62d533 249
a62d533 250
        addFocusListener ( new FocusAdapter ()
a62d533 251
        {
a62d533 252
            @Override
a62d533 253
            public void focusGained ( FocusEvent e )
a62d533 254
            {
a62d533 255
                repaint ();
a62d533 256
            }
a62d533 257
a62d533 258
            @Override
a62d533 259
            public void focusLost ( FocusEvent e )
a62d533 260
            {
a62d533 261
                repaint ();
a62d533 262
            }
a62d533 263
        } );
a62d533 264
a62d533 265
        // Values updater
a62d533 266
        updater = UpdateTimer.install ( this, THREAD_NAME, refreshRate, new ActionListener ()
a62d533 267
        {
a62d533 268
            @Override
a62d533 269
            public void actionPerformed ( ActionEvent e )
a62d533 270
            {
a62d533 271
                updateMemory ();
a62d533 272
            }
a62d533 273
        } );
a62d533 274
    }
a62d533 275
a62d533 276
    public void doGC ()
a62d533 277
    {
a62d533 278
        System.gc ();
a62d533 279
        updateMemory ();
a62d533 280
    }
a62d533 281
a62d533 282
    protected void updateMemory ()
a62d533 283
    {
a62d533 284
        // Determining current memory usage state
a62d533 285
        MemoryUsage mu = ManagementFactory.getMemoryMXBean ().getHeapMemoryUsage ();
a62d533 286
        usedMemory = mu.getUsed ();
a62d533 287
        allocatedMemory = mu.getCommitted ();
a62d533 288
a62d533 289
        // Updating bar text
a62d533 290
        setText ( getMemoryBarText () );
a62d533 291
a62d533 292
        // Updating view
a62d533 293
        repaint ();
a62d533 294
    }
a62d533 295
    
a62d533 296
    private static String getDigits ( int digits )
a62d533 297
    {
a62d533 298
        StringBuilder stringBuilder = new StringBuilder ( digits );
a62d533 299
        for ( int i = 0; i < digits; i++ )
a62d533 300
        {
a62d533 301
            stringBuilder.append ( "#" );
a62d533 302
        }
a62d533 303
        return stringBuilder.toString ();
a62d533 304
    }
a62d533 305
    
a62d533 306
a62d533 307
    public static final long KB = 1024;
a62d533 308
    public static final long MB = 1024 * KB;
a62d533 309
    public static final long GB = 1024 * MB;
a62d533 310
    public static final long PB = 1024 * GB;
a62d533 311
    
a62d533 312
    public static String getFileSizeString ( long size, int digits )
a62d533 313
    {
a62d533 314
        DecimalFormat df = new DecimalFormat ( digits == 0 ? "#" : "#." + getDigits ( digits ) );
a62d533 315
        if ( size < KB )
a62d533 316
        {
a62d533 317
            return df.format ( size ) + " " + "KB";
a62d533 318
        }
a62d533 319
        else if ( size >= KB && size < MB )
a62d533 320
        {
a62d533 321
            return df.format ( ( float ) size / KB ) + " " + "KB";
a62d533 322
        }
a62d533 323
        else if ( size >= MB && size < GB )
a62d533 324
        {
a62d533 325
            return df.format ( ( float ) size / MB ) + " " + "MB";
a62d533 326
        }
a62d533 327
        else
a62d533 328
        {
a62d533 329
            return df.format ( ( float ) size / GB ) + " " + "GB";
a62d533 330
        }
a62d533 331
    }
a62d533 332
a62d533 333
a62d533 334
    protected String getMemoryBarText (){
a62d533 335
        long total = allocatedMemory;
a62d533 336
        return " " +getFileSizeString(usedMemory, getDigits(usedMemory)) + " " + " " + "of" + " " + " "+
a62d533 337
        		getFileSizeString ( total, getDigits ( total ) );
a62d533 338
    }
a62d533 339
a62d533 340
    private int getDigits ( long size )
a62d533 341
    {
a62d533 342
        return size < GB ? 0 : 2;
a62d533 343
    }
a62d533 344
a62d533 345
    @Override
a62d533 346
    protected void paintComponent ( Graphics g )
a62d533 347
    {
a62d533 348
        Graphics2D g2d = ( Graphics2D ) g;
a62d533 349
        Object old = LafUtils.setupAntialias ( g2d );
a62d533 350
a62d533 351
        // Used memory background
a62d533 352
        g2d.setPaint ( usedFillColor );
a62d533 353
        g2d.fill ( getProgressShape ( usedMemory, true ) );
a62d533 354
a62d533 355
        // Used memory border
a62d533 356
        g2d.setPaint ( usedBorderColor );
a62d533 357
        g2d.draw ( getProgressShape ( usedMemory, false ) );
a62d533 358
a62d533 359
        LafUtils.restoreAntialias ( g2d, old );
a62d533 360
a62d533 361
        super.paintComponent ( g2d );
a62d533 362
    }
a62d533 363
a62d533 364
    private Shape getProgressShape ( long progress, boolean fill )
a62d533 365
    {
a62d533 366
       return new RoundRectangle2D.Double ( 1, 2, getProgressWidth ( progress, fill ), getHeight () - 3 - ( fill ? 0 : 1 ), 2, 2);
a62d533 367
    }
a62d533 368
a62d533 369
    private int getProgressWidth ( long progress, boolean fill )
a62d533 370
    {
a62d533 371
        return Math.round ( ( float ) ( getWidth () - ( 2 ) -
a62d533 372
                ( fill ? 0 : 1 ) ) * progress / ( allocatedMemory ) );
a62d533 373
    }
a62d533 374
}