| Author | DaveJarvis <email> |
|---|---|
| Date | 2020-07-17 16:58:11 GMT-0700 |
| Commit | 8657077e48dce4d29612a098c5dfc7da6a245f68 |
| Parent | cc2f14b |
| import static com.whitemagicsoftware.kmcaster.HardwareImages.state; | ||
| import static com.whitemagicsoftware.kmcaster.HardwareSwitch.*; | ||
| +import static java.lang.Boolean.parseBoolean; | ||
| public class EventFrame extends JFrame { | ||
| private static final float ARC = 8; | ||
| private static final Dimension FRAME_DIMENSIONS = new Dimension( 484, 70 ); | ||
| private static final Color TRANSLUCENT = new Color( .2f, .2f, .2f, 0.5f ); | ||
| private static final Color TRANSPARENT = new Color( 0, 0, 0, 0 ); | ||
| + private static final Color COLOUR_LABEL = new Color( 33, 33, 33 ); | ||
| private final HardwareImages mSwitches; | ||
| - private final Map<HardwareSwitch, ImageComponent> mSwitchViews = new HashMap<>(); | ||
| + private final Map<HardwareSwitch, ImageComponent> mSwitchViews = | ||
| + new HashMap<>(); | ||
| public EventFrame() { | ||
| addMouseMotionListener( frameDragListener ); | ||
| - final var dimensions = new Dimension( getWidth(), getHeight()- 10 ); | ||
| + final var dimensions = new Dimension( getWidth(), getHeight() - 10 ); | ||
| mSwitches = new HardwareImages( dimensions ); | ||
| protected void updateSwitchState( final HardwareState keyState ) { | ||
| final var image = mSwitches.get( keyState ); | ||
| - final var component = mSwitchViews.get( keyState.getKey() ); | ||
| + final var component = mSwitchViews.get( keyState.getHardwareSwitch() ); | ||
| component.redraw( image ); | ||
| } | ||
| protected void updateSwitchLabel( | ||
| - final HardwareSwitch name, final String label ) { | ||
| - System.out.println( "Switch Label: " + label ); | ||
| + final HardwareState state, final String value ) { | ||
| + if( state.isModifier() ) { | ||
| + final var pressed = parseBoolean( value ); | ||
| + System.out.println( "Modifier pressed: " + pressed ); | ||
| + } | ||
| + else { | ||
| + final var component = mSwitchViews.get( state.getHardwareSwitch() ); | ||
| + component.removeAll(); | ||
| + | ||
| + if( !"false".equals( value ) ) { | ||
| + System.out.println( "Regular pressed: " + value ); | ||
| + | ||
| + final var label = labelFor( value ); | ||
| + label.setSize( component.getSize() ); | ||
| + | ||
| + component.add( label ); | ||
| + component.repaint(); | ||
| + } | ||
| + } | ||
| + } | ||
| + | ||
| + private JLabel labelFor( final String value ) { | ||
| + final var label = new JLabel( value ); | ||
| + label.setForeground( COLOUR_LABEL ); | ||
| + | ||
| + return label; | ||
| } | ||
| private ImageComponent createImageComponent( final Image image ) { | ||
| return new ImageComponent( image ); | ||
| } | ||
| + /** | ||
| + * Returns the shape for the application's window frame. | ||
| + * | ||
| + * @return A rounded rectangle. | ||
| + */ | ||
| private Shape createShape() { | ||
| return new RoundRectangle2D.Double( | ||
| * Responsible for loading vector graphics representations of application | ||
| * images. The images provide an on-screen interface that indicate to the user | ||
| - * what key or mouse events are being triggered. | ||
| + * what key or mouse events have been triggered. | ||
| */ | ||
| public class HardwareImages { |
| public final static String ANY_KEY = "*"; | ||
| - private final HardwareSwitch mName; | ||
| - private final String mState; | ||
| + private final HardwareSwitch mHardwareSwitch; | ||
| + private final String mHardwareStatus; | ||
| /** | ||
| * Constructs a new instance that represents whether a key or mouse button | ||
| * was pressed. | ||
| * | ||
| - * @param name The {@link HardwareSwitch} representing the type of switch | ||
| - * state to represent. | ||
| - * @param state A value of "*' means a regular key was pressed; otherwise, | ||
| - * "true" or "false" indicate pressed or released, respectively. | ||
| + * @param hardwareSwitch The {@link HardwareSwitch} representing the type | ||
| + * of switch state to represent. | ||
| + * @param hardwareStatus A value of {@link #ANY_KEY} means a regular key was | ||
| + * pressed; otherwise, "true" or "false" indicate | ||
| + * pressed or released, respectively. | ||
| */ | ||
| - public HardwareState( final HardwareSwitch name, final String state ) { | ||
| - assert name != null; | ||
| - assert valid( state ); | ||
| + public HardwareState( | ||
| + final HardwareSwitch hardwareSwitch, final String hardwareStatus ) { | ||
| + assert hardwareSwitch != null; | ||
| + assert valid( hardwareStatus ); | ||
| - mName = name; | ||
| - mState = state; | ||
| + mHardwareSwitch = hardwareSwitch; | ||
| + mHardwareStatus = hardwareStatus; | ||
| } | ||
| - public HardwareSwitch getKey() { | ||
| - return mName; | ||
| + /** | ||
| + * Returns the physical switch containing its name. | ||
| + * | ||
| + * @return The {@link HardwareSwitch} having a switch-dependent state. | ||
| + */ | ||
| + public HardwareSwitch getHardwareSwitch() { | ||
| + return mHardwareSwitch; | ||
| + } | ||
| + | ||
| + /** | ||
| + * Answers whether this is a modifier key. | ||
| + * | ||
| + * @return {@code true} when this is a modifier key. | ||
| + */ | ||
| + public boolean isModifier() { | ||
| + return mHardwareSwitch.isModifier(); | ||
| } | ||
| final HardwareState that = (HardwareState) o; | ||
| - if( mName != that.mName ) { | ||
| + if( mHardwareSwitch != that.mHardwareSwitch ) { | ||
| return false; | ||
| } | ||
| - return mState.equals( that.mState ); | ||
| + return mHardwareStatus.equals( that.mHardwareStatus ); | ||
| } | ||
| @Override | ||
| public int hashCode() { | ||
| - int result = mName.hashCode(); | ||
| - result = 31 * result + mState.hashCode(); | ||
| + int result = mHardwareSwitch.hashCode(); | ||
| + result = 31 * result + mHardwareStatus.hashCode(); | ||
| return result; | ||
| - } | ||
| - | ||
| - @Override | ||
| - public String toString() { | ||
| - return getClass().getName() + "{" + | ||
| - "mKey=" + mName + | ||
| - ", mState='" + mState + '\'' + | ||
| - '}'; | ||
| } | ||
| } | ||
| final var switchState = createState( e.getPropertyName(), context ); | ||
| updateSwitchState( switchState ); | ||
| - updateSwitchLabel( switchState.getKey(), switchValue ); | ||
| + updateSwitchLabel( switchState, switchValue ); | ||
| } | ||
| Delta | 72 lines added, 32 lines removed, 40-line increase |
|---|