| Author | DaveJarvis <email> |
|---|---|
| Date | 2020-08-01 18:38:28 GMT-0700 |
| Commit | d3ccdda5c86d2754816df74c0d1da4f7cf6e3125 |
| Parent | b06e2c5 |
| import static com.whitemagicsoftware.kmcaster.HardwareState.SWITCH_PRESSED; | ||
| import static com.whitemagicsoftware.kmcaster.HardwareState.SWITCH_RELEASED; | ||
| -import static com.whitemagicsoftware.kmcaster.HardwareSwitch.KEY_REGULAR; | ||
| +import static com.whitemagicsoftware.kmcaster.HardwareSwitch.*; | ||
| import static com.whitemagicsoftware.kmcaster.LabelConfig.*; | ||
| import static com.whitemagicsoftware.kmcaster.ui.Constants.*; | ||
| hwSwitch, hwState, switchValue ); | ||
| + // Get the mouse timer, modifier key timer, or non-modifier key timer. | ||
| final var timer = getTimer( hwSwitch ); | ||
| if( hwState == SWITCH_RELEASED ) { | ||
| timer.addActionListener( | ||
| - ( event ) -> updateSwitchState( switchState ) | ||
| + ( event ) -> updateMouseLabel( switchState ) | ||
| ); | ||
| } | ||
| else { | ||
| timer.stop(); | ||
| - updateSwitchState( switchState ); | ||
| + updateMouseLabel( switchState ); | ||
| } | ||
| } | ||
| final var tally = getLabel( LABEL_REGULAR_COUNTER ); | ||
| - tally.setVisible( false ); | ||
| main.setVisible( false ); | ||
| sup.setVisible( false ); | ||
| + tally.setVisible( false ); | ||
| if( hwState == SWITCH_PRESSED ) { | ||
| } | ||
| } | ||
| + } | ||
| + } | ||
| + | ||
| + private void updateMouseLabel( final HardwareSwitchState state ) { | ||
| + updateSwitchState( state ); | ||
| + | ||
| + final var button = getLabel( LABEL_MOUSE_UNDEFINED ); | ||
| + button.setVisible( false ); | ||
| + | ||
| + if( state.getHardwareSwitch() == MOUSE_UNDEFINED && | ||
| + state.getHardwareState() == SWITCH_PRESSED ) { | ||
| + button.setText( state.getValue() ); | ||
| + button.transform(); | ||
| + button.setVisible( true ); | ||
| } | ||
| } | ||
| KEY_CTRL, new Insets( 10, 11, 12, 11 ), | ||
| KEY_SHIFT, new Insets( 10, 50, 12, 11 ), | ||
| - KEY_REGULAR, new Insets( 3, 7, 6, 7 ) | ||
| + KEY_REGULAR, new Insets( 3, 7, 6, 7 ), | ||
| + MOUSE_UNDEFINED, new Insets( 27, 5, 11, 5 ) | ||
| ); | ||
| mAppDimensions = userSettings.createAppDimensions(); | ||
| - final var mouseStates = createHardwareComponent(); | ||
| final var mouseReleased = mouseImage( "0" ); | ||
| + final var mouseScale = mouseReleased.getValue(); | ||
| + final var mouseStates = | ||
| + createHardwareComponent( MOUSE_UNDEFINED, mouseScale ); | ||
| for( final var key : HardwareSwitch.mouseSwitches() ) { | ||
| final var imageUp = keyUpImage( FILE_NAME_PREFIXES.get( key ) ); | ||
| final var scale = imageDn.getValue(); | ||
| - final var insets = new PaddedInsets( SWITCH_INSETS.get( key ) ); | ||
| - final var scaledInsets = insets.scale( scale ); | ||
| - | ||
| - final var component = createHardwareComponent( scaledInsets ); | ||
| + final var keyStates = createHardwareComponent( key, scale ); | ||
| - component.put( stateOn, imageDn.getKey() ); | ||
| - component.put( stateOff, imageUp.getKey() ); | ||
| + keyStates.put( stateOn, imageDn.getKey() ); | ||
| + keyStates.put( stateOff, imageUp.getKey() ); | ||
| - mSwitches.put( key, component ); | ||
| + mSwitches.put( key, keyStates ); | ||
| } | ||
| } | ||
| - private HardwareComponent<HardwareSwitchState, Image> createHardwareComponent() { | ||
| - return new HardwareComponent<>(); | ||
| + private PaddedInsets createInsets( final HardwareSwitch hwSwitch ) { | ||
| + return new PaddedInsets( SWITCH_INSETS.get( hwSwitch ) ); | ||
| } | ||
| private HardwareComponent<HardwareSwitchState, Image> createHardwareComponent( | ||
| - final Insets insets ) { | ||
| - return new HardwareComponent<>( insets ); | ||
| + final HardwareSwitch hwSwitch, | ||
| + final DimensionTuple scale ) { | ||
| + final var insets = createInsets( hwSwitch ); | ||
| + final var scaledInsets = insets.scale( scale ); | ||
| + | ||
| + return new HardwareComponent<>( scaledInsets ); | ||
| } | ||
| MOUSE_MIDDLE( "2" ), | ||
| MOUSE_RIGHT( "3" ), | ||
| - MOUSE_REGULAR( "regular" ), | ||
| + MOUSE_UNDEFINED( "undefined" ), | ||
| KEY_SHIFT( "shift", SHIFT_MASK ), | ||
| KEY_CTRL( "ctrl", CTRL_MASK ), | ||
| /** | ||
| - * Returns a list of all mouse buttons. | ||
| + * Returns a list of mouse buttons. | ||
| * | ||
| * @return The complete list of mouse buttons. | ||
| */ | ||
| public static HardwareSwitch[] mouseSwitches() { | ||
| return new HardwareSwitch[]{ | ||
| - MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT, MOUSE_REGULAR | ||
| + MOUSE_LEFT, MOUSE_MIDDLE, MOUSE_RIGHT, MOUSE_UNDEFINED | ||
| }; | ||
| } | ||
| /** | ||
| - * Used for initializing the {@link AutofitLabel} instances. | ||
| + * Used for initializing the {@link AutofitLabel} instances. Labels are | ||
| + * centre-aligned vertically and horizontally by default. See also | ||
| + * the hardware switch insets from {@link HardwareImages}, which defines | ||
| + * the safe drawing area for each label. | ||
| */ | ||
| public enum LabelConfig { | ||
| - LABEL_SHIFT( KEY_SHIFT, CENTER, CENTER ), | ||
| - LABEL_CTRL( KEY_CTRL, CENTER, CENTER ), | ||
| - LABEL_ALT( KEY_ALT, CENTER, CENTER ), | ||
| - LABEL_REGULAR( KEY_REGULAR, CENTER, CENTER ), | ||
| - LABEL_REGULAR_NUM_MAIN( CENTER, CENTER ), | ||
| + LABEL_SHIFT( KEY_SHIFT ), | ||
| + LABEL_CTRL( KEY_CTRL ), | ||
| + LABEL_ALT( KEY_ALT ), | ||
| + LABEL_REGULAR( KEY_REGULAR ), | ||
| + LABEL_REGULAR_NUM_MAIN(), | ||
| LABEL_REGULAR_NUM_SUPERSCRIPT( TOP, LEFT ), | ||
| - LABEL_REGULAR_COUNTER( TOP, RIGHT ); | ||
| + LABEL_REGULAR_COUNTER( TOP, RIGHT ), | ||
| + LABEL_MOUSE_UNDEFINED( MOUSE_UNDEFINED ); | ||
| /** | ||
| * A value of {@code null} indicates multiple labels adorn the switch. | ||
| */ | ||
| private final HardwareSwitch mHardwareSwitch; | ||
| private final int mHorizontalAlign; | ||
| private final int mVerticalAlign; | ||
| + | ||
| + /** | ||
| + * Centres the label vertically and horizontally. | ||
| + */ | ||
| + LabelConfig() { | ||
| + this( null ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Centres the label vertically and horizontally. | ||
| + * | ||
| + * @param hwSwitch The switch to associate with the single label. | ||
| + */ | ||
| + LabelConfig( final HardwareSwitch hwSwitch ) { | ||
| + this( hwSwitch, CENTER, CENTER ); | ||
| + } | ||
| /** |
| "Delay for releasing mouse buttons (${DEFAULT-VALUE} milliseconds)", | ||
| paramLabel = "delay", | ||
| - defaultValue = "200" | ||
| + defaultValue = "100" | ||
| ) | ||
| - private int mDelayMouseButton = 200; | ||
| + private int mDelayMouseButton = 100; | ||
| public UserSettings( final KmCaster kmCaster ) { |
| if( modifierKey == null ) { | ||
| - updateRegular( mRegularHeld, getDisplayText( e ) ); | ||
| + dispatchRegular( mRegularHeld, getDisplayText( e ) ); | ||
| } | ||
| else { | ||
| - updateModifier( modifierKey, 1 ); | ||
| + dispatchModifier( modifierKey, 1 ); | ||
| } | ||
| } | ||
| @Override | ||
| public void nativeKeyReleased( final NativeKeyEvent e ) { | ||
| final var modifierKey = getModifierKey( e ); | ||
| if( modifierKey == null ) { | ||
| - updateRegular( getDisplayText( e ), "" ); | ||
| + dispatchRegular( getDisplayText( e ), "" ); | ||
| } | ||
| else { | ||
| - updateModifier( modifierKey, -1 ); | ||
| + dispatchModifier( modifierKey, -1 ); | ||
| } | ||
| } | ||
| * @param increment {@code -1} means released, {@code 1} means pressed. | ||
| */ | ||
| - private void updateModifier( final HardwareSwitch key, final int increment ) { | ||
| + private void dispatchModifier( final HardwareSwitch key, final int increment ) { | ||
| final var oldCount = mModifiers.get( key ); | ||
| final var newCount = oldCount + increment; | ||
| * @param n Current key value. | ||
| */ | ||
| - private void updateRegular( final String o, final String n ) { | ||
| + private void dispatchRegular( final String o, final String n ) { | ||
| assert o != null; | ||
| assert n != null; | ||
| import java.util.Map; | ||
| -import static com.whitemagicsoftware.kmcaster.HardwareSwitch.*; | ||
| +import static com.whitemagicsoftware.kmcaster.HardwareSwitch.MOUSE_UNDEFINED; | ||
| +import static com.whitemagicsoftware.kmcaster.HardwareSwitch.mouseSwitches; | ||
| +import static java.util.Map.entry; | ||
| /** | ||
| * Listens for all mouse events: clicks and mouse wheel scrolls. | ||
| */ | ||
| public final class MouseListener | ||
| extends PropertyDispatcher<HardwareSwitch> | ||
| implements NativeMouseInputListener, NativeMouseWheelListener { | ||
| + | ||
| + private final static Map<Integer, String> SCROLL_CODES = | ||
| + Map.ofEntries( | ||
| + entry( 1, "↑" ), | ||
| + entry( -1, "↓" ) | ||
| + ); | ||
| /** | ||
| * Stores the state of button presses. The contents of the map reflect the | ||
| * state of each switch, so the reference can be final but not its contents. | ||
| */ | ||
| private final Map<HardwareSwitch, Boolean> mSwitches = new HashMap<>(); | ||
| public MouseListener() { | ||
| - mSwitches.put( MOUSE_LEFT, false ); | ||
| - mSwitches.put( MOUSE_MIDDLE, false ); | ||
| - mSwitches.put( MOUSE_RIGHT, false ); | ||
| - mSwitches.put( MOUSE_REGULAR, false ); | ||
| + for( final var key : mouseSwitches() ) { | ||
| + mSwitches.put( key, false ); | ||
| + } | ||
| } | ||
| private void dispatchMouseEvent( | ||
| final NativeMouseEvent e, final boolean pressed ) { | ||
| - try { | ||
| - final var id = Integer.toString( e.getButton() ); | ||
| - final var hwSwitch = HardwareSwitch.valueFrom( id ); | ||
| + final var hwSwitch = getMouseSwitch( e ); | ||
| + | ||
| + // Percolate the button number as a string for any undefined (unmapped) | ||
| + // mouse buttons that are clicked. This enables additional mouse | ||
| + // buttons beyond two to appear, without an image representation. | ||
| + if( hwSwitch == MOUSE_UNDEFINED ) { | ||
| + final var button = Integer.toString( e.getButton() ); | ||
| + final var n = pressed ? button : ""; | ||
| + final var o = pressed ? "" : button; | ||
| + fire( hwSwitch, o, n ); | ||
| + } | ||
| + else { | ||
| tryFire( hwSwitch, mSwitches.get( hwSwitch ), pressed ); | ||
| - mSwitches.put( hwSwitch, pressed ); | ||
| - } catch( final Exception ex ) { | ||
| - // The mouse button wasn't found. This means that there is no visual | ||
| - // representation for the button, so pass up the generic brand instead. | ||
| - //tryFire( MOUSE_REGULAR, mSwitches.get( MOUSE_REGULAR ), pressed ); | ||
| } | ||
| + | ||
| + mSwitches.put( hwSwitch, pressed ); | ||
| + } | ||
| + | ||
| + private HardwareSwitch getMouseSwitch( final NativeMouseEvent e ) { | ||
| + final var button = e.getButton(); | ||
| + | ||
| + return switch( button ) { | ||
| + case 1, 2, 3 -> HardwareSwitch.valueFrom( Integer.toString( button ) ); | ||
| + default -> MOUSE_UNDEFINED; | ||
| + }; | ||
| } | ||
| -<svg viewBox="0 0 35 60" xmlns="http://www.w3.org/2000/svg"><path d="m17.502 59.388c-14.667 0-16.886-11.917-16.733-15.693.2258-3.5611.06598-20.108.06598-20.108s-.03406-.98114 16.665-.98698c16.626 0 16.665.98698 16.665.98698s-.15981 16.547.06598 20.107c.15174 3.7756-2.0658 15.693-16.733 15.693" fill="#feff00" fill-opacity=".25098" fill-rule="evenodd"/></svg> | ||
| + |
| - | ||
| +<svg viewBox="0 0 35 60" xmlns="http://www.w3.org/2000/svg"><path d="m17.502 59.388c-14.667 0-16.886-11.917-16.733-15.693.2258-3.5611.06598-20.108.06598-20.108s-.03406-.98114 16.665-.98698c16.626 0 16.665.98698 16.665.98698s-.15981 16.547.06598 20.107c.15174 3.7756-2.0658 15.693-16.733 15.693" fill="#feff00" fill-opacity=".25098" fill-rule="evenodd"/></svg> |
| Delta | 111 lines added, 50 lines removed, 61-line increase |
|---|