Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/kmcaster.git

Minor optimizations for bounds calculations

Author DaveJarvis <email>
Date 2020-07-25 21:05:30 GMT-0700
Commit 1d3c9fbf88fe9413e2a16481bdccdcf31ed74ac9
Parent eb09649
src/main/com/whitemagicsoftware/kmcaster/EventHandler.java
*/
protected void updateSwitchLabel( final HardwareSwitchState state ) {
- final var component = getHardwareComponent( state );
- final var keyValue = state.getValue();
final var keyColour = KEY_COLOURS.get( state.getHardwareState() );
final var pressed = state.isHardwareState( SWITCH_PRESSED );
}
}
- else if( pressed ) {
- // A non-modifier key has been pressed.
+ else {
+ final var component = getHardwareComponent( state );
+ final var keyValue = state.getValue();
- // Determine whether there are separate parts for the key label.
- final var index = keyValue.indexOf( ' ' );
+ if( pressed ) {
+ // A non-modifier key has been pressed.
+ System.out.println( "KEY PRESSED: " + keyValue );
- final var calculator = new BoundsCalculator( component );
- final var compDimen = new ScalableDimension( calculator.computeSize() );
+ // Determine whether there are separate parts for the key label.
+ final var index = keyValue.indexOf( ' ' );
- // If there's a space in the name, the text before the space is
- // positioned in the upper-left while the text afterwards takes up
- // the remainder. This is used for number pad keys, backspace, enter,
- // tab, and a few others.
- if( index > 0 ) {
- final var supSize = compDimen.scale( .6f );
- final var mainSize = compDimen.scale( .9f );
+ final var calculator = new BoundsCalculator( component );
+ final var bounds = calculator.getBounds();
+ final var compDimen = new ScalableDimension( bounds.width, bounds.height );
- final var s = new String[]{
- keyValue.substring( 0, index ),
- keyValue.substring( index + 1 )
- };
+ // If there's a space in the name, the text before the space is
+ // positioned in the upper-left while the text afterwards takes up
+ // the remainder. This is used for number pad keys, backspace, enter,
+ // tab, and a few others.
+ if( index > 0 ) {
+ final var supSize = compDimen.scale( .6f );
+ final var mainSize = compDimen.scale( .9f );
- // Label for "Num", "Back", "Tab", and other dual-labelled keys.
- final var sup = new AutofitLabel( s[ 0 ], LABEL_FONT );
- sup.setVisible( false );
- sup.setForeground( keyColour );
- sup.setVerticalAlignment( TOP );
+ final var s = new String[]{
+ keyValue.substring( 0, index ),
+ keyValue.substring( index + 1 )
+ };
- // Label for number pad keys or icon glyphs.
- final var main = new AutofitLabel( s[ 1 ], LABEL_FONT );
- main.setVisible( false );
- main.setForeground( keyColour );
- main.setHorizontalAlignment( CENTER );
- main.setVerticalAlignment( CENTER );
+ // Label for "Num", "Back", "Tab", and other dual-labelled keys.
+ final var sup = new AutofitLabel( s[ 0 ], LABEL_FONT );
+ sup.setVisible( false );
+ sup.setForeground( keyColour );
+ sup.setVerticalAlignment( TOP );
- // Keep removeAll/add operations close together to minimize flicker.
- component.removeAll();
- component.add( main );
- component.add( sup );
- main.setSize( mainSize );
- sup.setSize( supSize );
+ // Label for number pad keys or icon glyphs.
+ final var main = new AutofitLabel( s[ 1 ], LABEL_FONT );
+ main.setVisible( false );
+ main.setForeground( keyColour );
+ main.setHorizontalAlignment( CENTER );
+ main.setVerticalAlignment( CENTER );
- // Center-align the main text with respect to the container.
- final var location = main.getLocation();
- final var dx = (compDimen.getWidth() - main.getWidth()) / 2;
- final var dy = (compDimen.getHeight() - main.getHeight()) / 2;
+ // Keep removeAll/add operations close together to minimize flicker.
+ component.removeAll();
+ component.add( main );
+ component.add( sup );
+ main.setSize( mainSize );
+ sup.setSize( supSize );
- // Shift the main text down a smidgen, relative to the superscript.
- final var my = (int) (location.getY() + dy) + sup.getHeight() / 4;
- final var mx = (int) (location.getX() + dx);
+ // Center-align the main text with respect to the container.
+ final var location = main.getLocation();
+ final var dx = (compDimen.getWidth() - main.getWidth()) / 2;
+ final var dy = (compDimen.getHeight() - main.getHeight()) / 2;
- main.setLocation( mx, my );
- main.setVisible( true );
- sup.setVisible( true );
- }
- else {
- component.removeAll();
- updateLabel( state, keyColour );
- }
+ // Shift the main text down a smidgen, relative to the superscript.
+ final var my = (int) (location.getY() + dy) + sup.getHeight() / 4;
+ final var mx = (int) (location.getX() + dx);
- // Track the consecutive key presses for this value.
- if( mKeyCounter.apply( keyValue ) ) {
- final var count = mKeyCounter.toString();
- final var tallySize = compDimen.scale( .25f );
+ main.setLocation( mx, my );
+ main.setVisible( true );
+ sup.setVisible( true );
+ }
+ else {
+ component.removeAll();
+ updateLabel( state, keyColour );
+ }
- final var tally = new AutofitLabel( count, LABEL_FONT );
- tally.setVisible( false );
- component.add( tally );
+ // Track the consecutive key presses for this value.
+ if( mKeyCounter.apply( keyValue ) ) {
+ final var count = mKeyCounter.toString();
+ final var tallySize = compDimen.scale( .25f );
- tally.setSize( tallySize );
- tally.setVerticalAlignment( TOP );
- tally.setHorizontalAlignment( RIGHT );
+ final var tally = new AutofitLabel( count, LABEL_FONT );
+ tally.setVisible( false );
+ component.add( tally );
- // Get the upper-left point, accounting for padding and insets.
- final var ul = calculator.getLocation();
- final var tx = (int) (ul.x + compDimen.getWidth() - tally.getWidth());
- final var ty = ul.y;
+ tally.setSize( tallySize );
+ tally.setVerticalAlignment( TOP );
+ tally.setHorizontalAlignment( RIGHT );
- tally.setLocation( tx, ty );
- tally.setVisible( true );
+ // Get the upper-left point, accounting for padding and insets.
+ final var tx = bounds.x + compDimen.getWidth() - tally.getWidth();
+ final var ty = bounds.y;
+
+ tally.setLocation( (int)tx, ty );
+ tally.setVisible( true );
+ }
}
- }
- else {
- component.removeAll();
+ else {
+ component.removeAll();
+ }
}
}
final var container = getHardwareComponent( state );
final var value = state.getValue();
- final AutofitLabel label;
if( container.getComponentCount() == 0 ) {
// Regular keys will have labels recreated each time to auto-fit the text.
- label = new AutofitLabel( value, LABEL_FONT );
+ final var label = new AutofitLabel( value, LABEL_FONT );
+ label.setVisible( false );
label.setHorizontalAlignment( CENTER );
- label.setVerticalAlignment( CENTER );
label.setForeground( keyColour );
container.add( label );
+ label.setVisible( true );
}
else {
// Modifier keys can reuse labels.
- label = (AutofitLabel) container.getComponent( 0 );
+ final var label = (AutofitLabel) container.getComponent( 0 );
label.setForeground( keyColour );
label.setText( value );
src/main/com/whitemagicsoftware/kmcaster/HardwareComponent.java
public void setState( final S state ) {
assert state != null;
- mState = state;
- repaint();
+ if( !state.equals( mState ) ) {
+ mState = state;
+ repaint();
+ }
}
src/main/com/whitemagicsoftware/kmcaster/ui/AutofitLabel.java
(e.getChangedParent() == parent) ) {
final var calculator = new BoundsCalculator( parent );
+ final var bounds = calculator.getBounds();
- setSize( calculator.computeSize() );
- setLocation( calculator.getLocation() );
+ setSize( bounds.width, bounds.height );
+ setLocation( bounds.x, bounds.y );
}
} );
var minSizePt = 1f;
- var maxSizePt = 200f;
+ var maxSizePt = 100f;
var scaledFont = getFont();
float scaledPt = scaledFont.getSize();
src/main/com/whitemagicsoftware/kmcaster/ui/BoundsCalculator.java
* writing on the {@link Container} parameter provided during construction.
*
- * @return The width and height of the {@link Container}'s safe area, based
- * on the {@link Container}'s bounded dimensions and insets.
+ * @return The {@link Container}'s safe area, based on the
+ * {@link Container}'s bounded dimensions and insets.
*/
- public Dimension computeSize() {
+ public Rectangle getBounds() {
final var container = getContainer();
final var insets = getInsets();
- return new Dimension(
+ return new Rectangle(
+ insets.left, insets.top,
container.getWidth() - (insets.left + insets.right),
container.getHeight() - (insets.top + insets.bottom)
);
}
public Insets getInsets() {
return mContainer.getInsets();
- }
-
- public Point getLocation() {
- final var insets = getInsets();
- return new Point( insets.left, insets.top );
}
src/main/com/whitemagicsoftware/kmcaster/ui/ScalableDimension.java
/**
- * Delegates construction to this class.
- *
- * @param d Contains the width and height to use for this instance.
- */
- public ScalableDimension( final Dimension d ) {
- this( d.getWidth(), d.getHeight() );
- }
-
- /**
* Scales the given source {@link Dimension} to the destination
* {@link Dimension}, maintaining the aspect ratio with respect to
Delta 91 lines added, 97 lines removed, 6-line decrease