Remove swing UI classes
This commit is contained in:
-164
@@ -1,164 +0,0 @@
|
||||
/*
|
||||
* This file is part of the Distant Horizons mod
|
||||
* licensed under the GNU LGPL v3 License.
|
||||
*
|
||||
* Copyright (C) 2020 James Seibel
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.seibel.distanthorizons.core.config.gui;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.KeyEvent;
|
||||
import java.awt.event.KeyListener;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
public class JavaScreenHandlerScreen extends AbstractScreen
|
||||
{
|
||||
public static Frame frame;
|
||||
public static boolean firstRun = true;
|
||||
public final Component jComponent;
|
||||
|
||||
|
||||
|
||||
static
|
||||
{
|
||||
// Note: this code can cause Mac
|
||||
// to lock up and refuse the load (there's a bug with Java.awt texture loading)
|
||||
|
||||
// Needs to be called before any Swing code is called, otherwise
|
||||
// Swing will get stuck thinking it's headless
|
||||
System.setProperty("java.awt.headless", "false");
|
||||
}
|
||||
|
||||
public JavaScreenHandlerScreen(@NotNull Component component)
|
||||
{
|
||||
this.jComponent = component;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init()
|
||||
{
|
||||
if (firstRun)
|
||||
{
|
||||
frame = EmbeddedFrameUtil.embeddedFrameCreate(this.minecraftWindow); // Don't call this multiple times
|
||||
}
|
||||
|
||||
frame.add(this.jComponent);
|
||||
frame.setBackground(new Color(0, 125, 155));
|
||||
|
||||
JavaScreenHandlerScreen thiss = this;
|
||||
|
||||
frame.addKeyListener(new KeyListener()
|
||||
{
|
||||
@Override
|
||||
public void keyPressed(KeyEvent keyEvent)
|
||||
{
|
||||
System.out.println("Key pressed code=" + keyEvent.getKeyCode() + ", char=" + keyEvent.getKeyChar());
|
||||
if (keyEvent.getKeyCode() == KeyEvent.VK_ESCAPE)
|
||||
{
|
||||
thiss.close = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void keyTyped(KeyEvent keyEvent) { }
|
||||
@Override
|
||||
public void keyReleased(KeyEvent keyEvent) { }
|
||||
});
|
||||
|
||||
if (firstRun)
|
||||
{
|
||||
EmbeddedFrameUtil.embeddedFrameSetBounds(frame, 0, 0, this.width, this.height);
|
||||
firstRun = false;
|
||||
}
|
||||
|
||||
EmbeddedFrameUtil.showFrame(frame);
|
||||
}
|
||||
|
||||
/** A testing/debug screen */
|
||||
public static class ExampleScreen extends JComponent
|
||||
{
|
||||
public ExampleScreen()
|
||||
{
|
||||
this.setLayout(new GridBagLayout());
|
||||
this.setBackground(new Color(255, 0, 0)); // doesn't appear to be used
|
||||
|
||||
GridBagConstraints helloWorldConstraints = new GridBagConstraints();
|
||||
helloWorldConstraints.weightx = 0.5;
|
||||
helloWorldConstraints.gridx = 0;
|
||||
helloWorldConstraints.gridy = 0;
|
||||
//helloWorldConstraints.fill = GridBagConstraints.BOTH;
|
||||
this.add(new JLabel("Hello World!"), helloWorldConstraints);
|
||||
|
||||
|
||||
GridBagConstraints buttonConstraints = new GridBagConstraints();
|
||||
buttonConstraints.weightx = 0.5;
|
||||
buttonConstraints.gridx = 0;
|
||||
buttonConstraints.gridy = 1;
|
||||
//buttonConstraints.fill = GridBagConstraints.BOTH;
|
||||
JButton button = new JButton();
|
||||
button.setBackground(Color.GREEN);
|
||||
button.setFocusable(false); // otherwise we can't use escape to leave
|
||||
button.setAction(new ExampleButtonEventHandler("Button text"));
|
||||
this.add(button, buttonConstraints);
|
||||
}
|
||||
|
||||
private class ExampleButtonEventHandler extends AbstractAction
|
||||
{
|
||||
public ExampleButtonEventHandler(String text)
|
||||
{
|
||||
super(text);
|
||||
//this.putValue(SHORT_DESCRIPTION, text);
|
||||
//this.putValue(MNEMONIC_KEY, text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e)
|
||||
{
|
||||
System.out.println("button pressed");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void render(float delta)
|
||||
{
|
||||
// TODO: Make screen only update on this being called
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onResize()
|
||||
{
|
||||
EmbeddedFrameUtil.embeddedFrameSetBounds(frame, 0, 0, this.width, this.height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose()
|
||||
{
|
||||
frame.remove(this.jComponent);
|
||||
EmbeddedFrameUtil.hideFrame(frame);
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,255 +0,0 @@
|
||||
/*
|
||||
* This file is part of the Distant Horizons mod
|
||||
* licensed under the GNU LGPL v3 License.
|
||||
*
|
||||
* Copyright (C) 2020 James Seibel
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.seibel.distanthorizons.core.jar.gui;
|
||||
|
||||
import com.seibel.distanthorizons.core.jar.JarUtils;
|
||||
import com.seibel.distanthorizons.core.dependencyInjection.SingletonInjector;
|
||||
import com.seibel.distanthorizons.core.wrapperInterfaces.config.ILangWrapper;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.InputStreamReader;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.*;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author coolGi
|
||||
*/
|
||||
// This will be removed later on to make a better ui
|
||||
// To get colors use https://alvinalexander.com/java/java-uimanager-color-keys-list/
|
||||
// TODO: Make the code less spaghetti later
|
||||
public class BaseJFrame extends JFrame
|
||||
{
|
||||
public BaseJFrame()
|
||||
{
|
||||
init();
|
||||
}
|
||||
public BaseJFrame(boolean show, boolean resizable)
|
||||
{
|
||||
init();
|
||||
setVisible(show);
|
||||
setResizable(resizable);
|
||||
}
|
||||
|
||||
public void init()
|
||||
{
|
||||
setTitle(SingletonInjector.INSTANCE.get(ILangWrapper.class).getLang("lod.title"));
|
||||
try
|
||||
{
|
||||
setIconImage(ImageIO.read(JarUtils.accessFile("assets/distanthorizons/icon.png")));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
setSize(720, 480);
|
||||
setLocationRelativeTo(null); // Puts the window at the middle of the screen
|
||||
setLayout(new BorderLayout());
|
||||
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
initLookAndFeel();
|
||||
}
|
||||
|
||||
/**
|
||||
* Buttons for language and theme changing
|
||||
*
|
||||
* @param themeOnBottom Puts the theme buttons below the language
|
||||
* @param rootPosOnLeft Where the start for the x is (on the left of the buttons or on the right)
|
||||
*/
|
||||
public void addExtraButtons(int x, int y, boolean themeOnBottom, boolean rootPosOnLeft)
|
||||
{
|
||||
// ========== LANGUAGE ==========
|
||||
int langBoxHeight = 25;
|
||||
int langBoxWidth = 100;
|
||||
|
||||
// Creates a list with all the options in it
|
||||
List<String> langsToChoose = new ArrayList<>();
|
||||
try (
|
||||
final InputStreamReader isr = new InputStreamReader(JarUtils.accessFile("assets/distanthorizons/lang"), StandardCharsets.UTF_8);
|
||||
final BufferedReader br = new BufferedReader(isr)
|
||||
)
|
||||
{
|
||||
List<Object> col = Collections.unmodifiableList(new ArrayList<>(Arrays.asList(br.lines().toArray())));
|
||||
for (Object obj : col)
|
||||
{
|
||||
langsToChoose.add(((String) obj).replaceAll("\\.json", ""));
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
// Creates the box
|
||||
JComboBox<String> languageBox = new JComboBox(new DefaultComboBoxModel(langsToChoose.toArray()));
|
||||
languageBox.setSelectedIndex(langsToChoose.indexOf(Locale.getDefault().toString().toLowerCase()));
|
||||
languageBox.addActionListener(e -> {
|
||||
Locale.setDefault(Locale.forLanguageTag(languageBox.getSelectedItem().toString())); // Change lang on update
|
||||
});
|
||||
// Set where it goes
|
||||
languageBox.setBounds(rootPosOnLeft ? x : x - langBoxWidth, themeOnBottom ? y : y + langBoxHeight, langBoxWidth, langBoxHeight);
|
||||
// And finally add it
|
||||
add(languageBox);
|
||||
|
||||
|
||||
// ========== THEMING ========== //
|
||||
/**
|
||||
// TODO: Change the theme to a toggle switch rather than having 2 buttons
|
||||
int themeButtonSize = 25;
|
||||
JButton lightMode = null;
|
||||
JButton darkMode = null;
|
||||
// Try to set the icons for them
|
||||
try
|
||||
{
|
||||
lightMode = new JButton(new ImageIcon(
|
||||
new FlatSVGIcon(JarUtils.accessFile("assets/distanthorizons/textures/jar/themeLight.svg")).getImage() // Get the image
|
||||
.getScaledInstance(themeButtonSize, themeButtonSize, Image.SCALE_DEFAULT) // Scale it to the correct size
|
||||
));
|
||||
darkMode = new JButton(new ImageIcon(
|
||||
new FlatSVGIcon(JarUtils.accessFile("assets/distanthorizons/textures/jar/themeDark.svg")).getImage() // Get the image
|
||||
.getScaledInstance(themeButtonSize, themeButtonSize, Image.SCALE_DEFAULT) // Scale it to the correct size
|
||||
));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
// Where do the buttons go
|
||||
lightMode.setBounds(rootPosOnLeft ? x : x - (themeButtonSize * 2), themeOnBottom ? y + langBoxHeight : y, themeButtonSize, themeButtonSize);
|
||||
darkMode.setBounds(rootPosOnLeft ? x + themeButtonSize : x - themeButtonSize, themeOnBottom ? y + langBoxHeight : y, themeButtonSize, themeButtonSize);
|
||||
// Tell buttons what to do
|
||||
lightMode.addActionListener(e -> {
|
||||
FlatLightLaf.setup();
|
||||
FlatLightLaf.updateUI();
|
||||
});
|
||||
darkMode.addActionListener(e -> {
|
||||
FlatDarkLaf.setup();
|
||||
FlatDarkLaf.updateUI();
|
||||
});
|
||||
// Finally add the buttons
|
||||
add(lightMode);
|
||||
add(darkMode);
|
||||
*/
|
||||
}
|
||||
|
||||
public BaseJFrame addLogo()
|
||||
{
|
||||
int logoHeight = 200;
|
||||
|
||||
JPanel logo = new JPanel()
|
||||
{
|
||||
@Override
|
||||
protected void paintComponent(Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
try
|
||||
{
|
||||
BufferedImage image = ImageIO.read(JarUtils.accessFile("assets/distanthorizons/logo.png"));
|
||||
int logoWidth = (int) ((double) logoHeight * ((double) image.getWidth() / (double) image.getHeight())); // Calculate the aspect ratio and set the height correctly to not stretch it
|
||||
g.drawImage(image, (getWidth() / 2) - (logoWidth / 2), 0, logoWidth, logoHeight, this); // Resize image and draw it
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
};
|
||||
logo.setBounds(logo.getX(), logo.getY(), logo.getWidth(), logo.getHeight());
|
||||
|
||||
add(logo);
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// This part of the code is taken from the official java docs at https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
|
||||
|
||||
// Specify the look and feel to use by defining the LOOKANDFEEL constant
|
||||
// Valid values are: null (use the default), "Metal", "System", "Motif", and "GTK"
|
||||
final static String LOOKANDFEEL = "GTK";
|
||||
private static void initLookAndFeel()
|
||||
{
|
||||
String lookAndFeel = null;
|
||||
|
||||
if (LOOKANDFEEL != null)
|
||||
{
|
||||
if (LOOKANDFEEL.equals("Metal"))
|
||||
{
|
||||
lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
|
||||
// an alternative way to set the Metal L&F is to replace the
|
||||
// previous line with:
|
||||
// lookAndFeel = "javax.swing.plaf.metal.MetalLookAndFeel";
|
||||
|
||||
}
|
||||
else if (LOOKANDFEEL.equals("System"))
|
||||
{
|
||||
lookAndFeel = UIManager.getSystemLookAndFeelClassName();
|
||||
}
|
||||
else if (LOOKANDFEEL.equals("Motif"))
|
||||
{
|
||||
lookAndFeel = "com.sun.java.swing.plaf.motif.MotifLookAndFeel";
|
||||
}
|
||||
else if (LOOKANDFEEL.equals("GTK"))
|
||||
{
|
||||
lookAndFeel = "com.sun.java.swing.plaf.gtk.GTKLookAndFeel";
|
||||
}
|
||||
else
|
||||
{
|
||||
System.err.println("Unexpected value of LOOKANDFEEL specified: "
|
||||
+ LOOKANDFEEL);
|
||||
lookAndFeel = UIManager.getCrossPlatformLookAndFeelClassName();
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
UIManager.setLookAndFeel(lookAndFeel);
|
||||
}
|
||||
catch (ClassNotFoundException e)
|
||||
{
|
||||
System.err.println("Couldn't find class for specified look and feel:"
|
||||
+ lookAndFeel);
|
||||
System.err.println("Did you include the L&F library in the class path?");
|
||||
System.err.println("Using the default look and feel.");
|
||||
}
|
||||
catch (UnsupportedLookAndFeelException e)
|
||||
{
|
||||
System.err.println("Can't use the specified look and feel ("
|
||||
+ lookAndFeel
|
||||
+ ") on this platform.");
|
||||
System.err.println("Using the default look and feel.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
System.err.println("Couldn't get specified look and feel ("
|
||||
+ lookAndFeel
|
||||
+ "), for some reason.");
|
||||
System.err.println("Using the default look and feel.");
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,179 +0,0 @@
|
||||
/*
|
||||
* This file is part of the Distant Horizons mod
|
||||
* licensed under the GNU LGPL v3 License.
|
||||
*
|
||||
* Copyright (C) 2020 James Seibel
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.seibel.distanthorizons.core.jar.gui;
|
||||
|
||||
import java.awt.*;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.beans.ConstructorProperties;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.plaf.ButtonUI;
|
||||
|
||||
/**
|
||||
* A switch button cus Java dosnt have one
|
||||
*
|
||||
* <p>
|
||||
* Ever wanted a switch like
|
||||
* <code><a href="https://www.overflowarchives.com/wp-content/uploads/2020/05/overflow-archives-unity-3d-switch-button-ui.jpg">this</a></code>
|
||||
* or
|
||||
* <code><a href="https://c8.alamy.com/comp/2E3PHHW/day-night-mode-switch-ui-button-light-dark-mode-slider-theme-2E3PHHW.jpg">this</a></code>?
|
||||
* Well now you can this this class!
|
||||
* </p>
|
||||
*
|
||||
* Based off Java's JButton
|
||||
*
|
||||
* @author coolGi
|
||||
*/
|
||||
// TODO: Make this for the theme (and finish the documentation once it is done)
|
||||
@SuppressWarnings("serial")
|
||||
public class JSwitch extends AbstractButton
|
||||
{
|
||||
private static final String uiClassID = "SwitchUI";
|
||||
|
||||
/** Creates a switch with no set text or icons */
|
||||
public JSwitch()
|
||||
{
|
||||
this(null, null, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a switch with an icon
|
||||
*
|
||||
* @param offIcon The deactivated icon image
|
||||
* @param onIcon The activated icon image
|
||||
*/
|
||||
public JSwitch(Icon offIcon, Icon onIcon)
|
||||
{
|
||||
this(null, null, offIcon, onIcon);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a switch with text
|
||||
*
|
||||
* @param offText the deactivated text of the button
|
||||
* @param onText the activated text of the button
|
||||
*/
|
||||
@ConstructorProperties({"text"})
|
||||
public JSwitch(String offText, String onText)
|
||||
{
|
||||
this(offText, onText, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a switch where properties are taken from the
|
||||
* <code>Action</code> supplied.
|
||||
*
|
||||
* @param a the <code>Action</code> used to specify the code that runs when pressing
|
||||
*/
|
||||
public JSwitch(Action a)
|
||||
{
|
||||
this();
|
||||
setAction(a);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a switch with initial text and an icon
|
||||
*
|
||||
* @param offText the deactivated text of the button
|
||||
* @param onText the activated text of the button
|
||||
* @param offIcon The deactivated icon image
|
||||
* @param onIcon The activated icon image
|
||||
*/
|
||||
public JSwitch(String offText, String onText, Icon offIcon, Icon onIcon)
|
||||
{
|
||||
// Create the model
|
||||
setModel(new DefaultButtonModel());
|
||||
|
||||
// this.trueLabel = trueLabel;
|
||||
// this.falseLabel = falseLabel;
|
||||
// double trueLenth = getFontMetrics( getFont() ).getStringBounds( trueLabel, getGraphics() ).getWidth();
|
||||
// double falseLenght = getFontMetrics( getFont() ).getStringBounds( falseLabel, getGraphics() ).getWidth();
|
||||
// max = (int)Math.max( trueLenth, falseLenght );
|
||||
// gap = Math.max( 5, 5+(int)Math.abs(trueLenth - falseLenght ) );
|
||||
// thumbBounds = new Dimension(max+gap*2,20);
|
||||
// globalWitdh = max + thumbBounds.width+gap*2;
|
||||
// setModel( new DefaultButtonModel() );
|
||||
// setSelected( false );
|
||||
// addMouseListener( new MouseAdapter() {
|
||||
// @Override
|
||||
// public void mouseReleased( MouseEvent e ) {
|
||||
// if(new Rectangle( getPreferredSize() ).contains( e.getPoint() )) {
|
||||
// setSelected( !isSelected() );
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the UI property to a value from the current look and feel
|
||||
*
|
||||
* @see JComponent#updateUI
|
||||
*/
|
||||
public void updateUI()
|
||||
{
|
||||
setUI((ButtonUI) UIManager.getUI(this));
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setSelected(boolean b)
|
||||
{
|
||||
// if(b){
|
||||
// setText( trueLabel );
|
||||
// setBackground( green );
|
||||
// } else {
|
||||
// setBackground( red );
|
||||
// setText( falseLabel );
|
||||
// }
|
||||
super.setSelected(b);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a string that specifies the name of the L&F class
|
||||
* that renders this component.
|
||||
*
|
||||
* @return the string "ButtonUI"
|
||||
* @see JComponent#getUIClassID
|
||||
* @see UIDefaults#getUI
|
||||
*/
|
||||
public String getUIClassID()
|
||||
{
|
||||
return uiClassID;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Overrides <code>JComponent.removeNotify</code> to check if
|
||||
* this button is currently set as the default button on the
|
||||
* <code>RootPane</code>, and if so, sets the <code>RootPane</code>'s
|
||||
* default button to <code>null</code> to ensure the
|
||||
* <code>RootPane</code> doesn't hold onto an invalid button reference.
|
||||
*/
|
||||
public void removeNotify()
|
||||
{
|
||||
JRootPane root = SwingUtilities.getRootPane(this);
|
||||
super.removeNotify();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,219 +0,0 @@
|
||||
/*
|
||||
* This file is part of the Distant Horizons mod
|
||||
* licensed under the GNU LGPL v3 License.
|
||||
*
|
||||
* Copyright (C) 2020 James Seibel
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.seibel.distanthorizons.core.jar.gui;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.Dimension;
|
||||
import java.awt.Font;
|
||||
import java.awt.FontMetrics;
|
||||
import java.awt.GradientPaint;
|
||||
import java.awt.Graphics;
|
||||
import java.awt.Graphics2D;
|
||||
import java.awt.Rectangle;
|
||||
import java.awt.RenderingHints;
|
||||
import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.awt.geom.Rectangle2D;
|
||||
|
||||
import javax.swing.AbstractButton;
|
||||
import javax.swing.DefaultButtonModel;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.UIManager;
|
||||
import javax.swing.border.Border;
|
||||
|
||||
/**
|
||||
* Taken from https://github.com/sshtools/ui/blob/master/src/main/java/com/sshtools/ui/swing/JSwitchBox.java
|
||||
*
|
||||
* @author sshtools
|
||||
*/
|
||||
// TODO: Merge this with my own JSwitch rather than use all their code
|
||||
// TODO: Make it work with the theme rather than do whatever it is doing now
|
||||
public class JSwitchBox extends AbstractButton
|
||||
{
|
||||
private Color shadow1 = UIManager.getColor("controlHighlight");
|
||||
private Color shadow2 = UIManager.getColor("control");
|
||||
private Color colorBright = UIManager.getColor("Button.light");
|
||||
private Color red = UIManager.getColor("controlShadow");
|
||||
private Color redf = UIManager.getColor("Button.foreground");
|
||||
private Color trackBackground = UIManager.getColor("textHighlight");
|
||||
private Color trackBackgroundText = UIManager.getColor("textHighlightText");
|
||||
private Border buttonBorder = UIManager.getBorder("Button.border");
|
||||
private Border trackBorder = UIManager.getBorder("Button.border");
|
||||
|
||||
private Font font = new JLabel().getFont();
|
||||
private int gap = 5;
|
||||
private int globalWitdh = 0;
|
||||
private final String trueLabel;
|
||||
private final String falseLabel;
|
||||
private Dimension thumbBounds;
|
||||
private Rectangle2D bounds;
|
||||
private int max;
|
||||
|
||||
public JSwitchBox(String trueLabel, String falseLabel)
|
||||
{
|
||||
setBackground(UIManager.getColor("Panel.background"));
|
||||
this.trueLabel = trueLabel;
|
||||
this.falseLabel = falseLabel;
|
||||
FontMetrics fontMetrics = getFontMetrics(getFont());
|
||||
double trueLenth = fontMetrics
|
||||
.getStringBounds(trueLabel, getGraphics()).getWidth();
|
||||
double falseLenght = fontMetrics.getStringBounds(falseLabel,
|
||||
getGraphics()).getWidth();
|
||||
max = (int) Math.max(trueLenth, falseLenght);
|
||||
gap = Math.max(5, 5 + (int) Math.abs(trueLenth - falseLenght));
|
||||
thumbBounds = new Dimension(max + gap * 2,
|
||||
(int) ((float) fontMetrics.getHeight() * 1.5));
|
||||
globalWitdh = max + thumbBounds.width + gap * 2;
|
||||
setModel(new DefaultButtonModel());
|
||||
setSelected(false);
|
||||
addMouseListener(new MouseAdapter()
|
||||
{
|
||||
@Override
|
||||
public void mouseReleased(MouseEvent e)
|
||||
{
|
||||
if (new Rectangle(getPreferredSize()).contains(e.getPoint()))
|
||||
{
|
||||
setSelected(!isSelected());
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dimension getPreferredSize()
|
||||
{
|
||||
return new Dimension(globalWitdh, thumbBounds.height);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setSelected(boolean b)
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
setText(trueLabel);
|
||||
setBackground(trackBackground);
|
||||
setForeground(trackBackgroundText);
|
||||
}
|
||||
else
|
||||
{
|
||||
setBackground(red);
|
||||
setForeground(redf);
|
||||
setText(falseLabel);
|
||||
}
|
||||
super.setSelected(b);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setText(String text)
|
||||
{
|
||||
super.setText(text);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getHeight()
|
||||
{
|
||||
|
||||
return getPreferredSize().height;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getWidth()
|
||||
{
|
||||
return getPreferredSize().width;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Font getFont()
|
||||
{
|
||||
return font;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void paintComponent(Graphics g)
|
||||
{
|
||||
g.setColor(getBackground());
|
||||
g.fillRect(0, 0, getWidth(), getHeight() - 4);
|
||||
Graphics2D g2 = (Graphics2D) g;
|
||||
|
||||
// g2.setColor(black);
|
||||
// g2.drawRoundRect(1, 1, getWidth() - 2 - 1, getHeight() - 2 - 1, 2,
|
||||
// 2);
|
||||
// g2.setColor(white);
|
||||
// g2.drawRoundRect(1 + 1, 1 + 1, getWidth() - 2 - 3, getHeight() - 2 -
|
||||
// 3,
|
||||
// 2, 2);
|
||||
|
||||
trackBorder.paintBorder(this, g2, 0, 2, getWidth(), getHeight() - 4);
|
||||
|
||||
int buttonX = 0;
|
||||
int textX = 0;
|
||||
if (isSelected())
|
||||
{
|
||||
textX = thumbBounds.width;
|
||||
}
|
||||
else
|
||||
{
|
||||
buttonX = thumbBounds.width;
|
||||
}
|
||||
int y = 0;
|
||||
int w = thumbBounds.width;
|
||||
int h = thumbBounds.height;
|
||||
|
||||
g2.setPaint(new GradientPaint(buttonX, (int) (y - 0.1 * h), shadow2,
|
||||
buttonX, (int) (y + 1.2 * h), shadow1));
|
||||
g2.fillRect(buttonX, y, w, h);
|
||||
g2.setPaint(new GradientPaint(buttonX, (int) (y + .65 * h), shadow1,
|
||||
buttonX, (int) (y + 1.3 * h), shadow2));
|
||||
g2.fillRect(buttonX, (int) (y + .65 * h), w, (int) (h - .65 * h));
|
||||
|
||||
if (w > 14)
|
||||
{
|
||||
int size = 10;
|
||||
g2.setColor(colorBright);
|
||||
g2.fillRect(buttonX + w / 2 - size / 2, y + h / 2 - size / 2, size,
|
||||
size);
|
||||
g2.setColor(colorBright.darker());
|
||||
g2.fillRect(buttonX + w / 2 - 4, h / 2 - 4, 2, 2);
|
||||
g2.fillRect(buttonX + w / 2 - 1, h / 2 - 4, 2, 2);
|
||||
g2.fillRect(buttonX + w / 2 + 2, h / 2 - 4, 2, 2);
|
||||
g2.setColor(colorBright.darker().darker());
|
||||
g2.fillRect(buttonX + w / 2 - 4, h / 2 - 2, 2, 6);
|
||||
g2.fillRect(buttonX + w / 2 - 1, h / 2 - 2, 2, 6);
|
||||
g2.fillRect(buttonX + w / 2 + 2, h / 2 - 2, 2, 6);
|
||||
g2.setColor(colorBright.darker());
|
||||
g2.fillRect(buttonX + w / 2 - 4, h / 2 + 2, 2, 2);
|
||||
g2.fillRect(buttonX + w / 2 - 1, h / 2 + 2, 2, 2);
|
||||
g2.fillRect(buttonX + w / 2 + 2, h / 2 + 2, 2, 2);
|
||||
}
|
||||
|
||||
buttonBorder.paintBorder(this, g2, buttonX, y, w, h);
|
||||
// g2.setColor(black);
|
||||
// g2.drawRoundRect(x, y, w - 1, h - 1, 2, 2);
|
||||
// g2.setColor(white);
|
||||
// g2.drawRoundRect(x + 1, y + 1, w - 3, h - 3, 2, 2);
|
||||
|
||||
g2.setColor(getForeground());
|
||||
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
|
||||
RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
||||
g2.setFont(getFont());
|
||||
g2.drawString(getText(), textX + gap, y + h / 2 + h / 4);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
/*
|
||||
* This file is part of the Distant Horizons mod
|
||||
* licensed under the GNU LGPL v3 License.
|
||||
*
|
||||
* Copyright (C) 2020 James Seibel
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, version 3.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.seibel.distanthorizons.core.jar.gui.cusomJObject;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
/**
|
||||
* A rectangular box that can be placed with java swing
|
||||
*
|
||||
* @author coolGi
|
||||
*/
|
||||
public class JBox extends JComponent
|
||||
{
|
||||
private static final String uiClassID = "BoxBarUI";
|
||||
|
||||
private Color color;
|
||||
|
||||
private int x;
|
||||
private int y;
|
||||
private int width;
|
||||
private int height;
|
||||
|
||||
public JBox()
|
||||
{
|
||||
this(null);
|
||||
}
|
||||
|
||||
public JBox(Color color)
|
||||
{
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
public JBox(Color color, Rectangle rectangle)
|
||||
{
|
||||
this(color, rectangle.x, rectangle.y, rectangle.width, rectangle.height);
|
||||
}
|
||||
|
||||
public JBox(Color color, int x, int y, int width, int height)
|
||||
{
|
||||
this.color = color;
|
||||
setBounds(x, y, width, height);
|
||||
}
|
||||
|
||||
public void setColor(Color color)
|
||||
{
|
||||
this.color = color;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void paintComponent(Graphics g)
|
||||
{
|
||||
super.paintComponent(g);
|
||||
g.setColor(color);
|
||||
g.fillRect(0, 0, getBounds().width, getBounds().height);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user