| <component name="ChangeListManager"> | ||
| <list default="true" id="be1819c5-28d3-4984-9634-0b266734e13c" name="Default Changelist" comment=""> | ||
| + <change afterPath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/ImageComponent.java" afterDir="false" /> | ||
| + <change afterPath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/KeyState.java" afterDir="false" /> | ||
| <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> | ||
| + <change beforePath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/AppImage.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/AppImage.java" afterDir="false" /> | ||
| <change beforePath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/EventFrame.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/EventFrame.java" afterDir="false" /> | ||
| + <change beforePath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/KmCaster.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/KmCaster.java" afterDir="false" /> | ||
| + <change beforePath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/listeners/Key.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/listeners/Key.java" afterDir="false" /> | ||
| <change beforePath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/listeners/KeyboardListener.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/com/whitemagicsoftware/kmcaster/listeners/KeyboardListener.java" afterDir="false" /> | ||
| </list> | ||
| <option name="RECENT_TEMPLATES"> | ||
| <list> | ||
| - <option value="Class" /> | ||
| <option value="Enum" /> | ||
| + <option value="Class" /> | ||
| </list> | ||
| </option> | ||
| <component name="RecentsManager"> | ||
| <key name="MoveInnerDialog.RECENTS_KEY"> | ||
| - <recent name="com.whitemagicsoftware.kmcaster.listeners" /> | ||
| <recent name="com.whitemagicsoftware.kmcaster" /> | ||
| + <recent name="com.whitemagicsoftware.kmcaster.listeners" /> | ||
| </key> | ||
| <key name="CreateClassDialog.RecentsKey"> | ||
| </state> | ||
| <state x="813" y="683" width="218" height="250" key="#Notifications/0.28.2560.1529@0.28.2560.1529" timestamp="1594856235998" /> | ||
| - <state x="314" y="436" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1594912481218"> | ||
| + <state x="314" y="436" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1594963749524"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state x="314" y="436" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.28.2560.1529@0.28.2560.1529" timestamp="1594912481218" /> | ||
| + <state x="314" y="436" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.28.2560.1529@0.28.2560.1529" timestamp="1594963749524" /> | ||
| <state x="734" y="519" width="376" height="578" key="#com.intellij.ide.util.MemberChooser" timestamp="1594883540197"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| <state x="734" y="519" width="376" height="578" key="#com.intellij.ide.util.MemberChooser/0.28.2560.1529@0.28.2560.1529" timestamp="1594883540197" /> | ||
| - <state x="745" y="737" key="#com.intellij.refactoring.move.MoveHandler.SelectRefactoringDialog" timestamp="1594953388252"> | ||
| + <state x="745" y="737" key="#com.intellij.refactoring.move.MoveHandler.SelectRefactoringDialog" timestamp="1594958988910"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state x="745" y="737" key="#com.intellij.refactoring.move.MoveHandler.SelectRefactoringDialog/0.28.2560.1529@0.28.2560.1529" timestamp="1594953388252" /> | ||
| - <state x="707" y="657" width="431" height="302" key="#com.intellij.refactoring.move.moveInner.MoveInnerDialog" timestamp="1594953392347"> | ||
| + <state x="745" y="737" key="#com.intellij.refactoring.move.MoveHandler.SelectRefactoringDialog/0.28.2560.1529@0.28.2560.1529" timestamp="1594958988910" /> | ||
| + <state x="707" y="657" width="431" height="302" key="#com.intellij.refactoring.move.moveInner.MoveInnerDialog" timestamp="1594958994663"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state x="707" y="657" width="431" height="302" key="#com.intellij.refactoring.move.moveInner.MoveInnerDialog/0.28.2560.1529@0.28.2560.1529" timestamp="1594953392347" /> | ||
| + <state x="707" y="657" width="431" height="302" key="#com.intellij.refactoring.move.moveInner.MoveInnerDialog/0.28.2560.1529@0.28.2560.1529" timestamp="1594958994663" /> | ||
| <state x="567" y="28" width="711" height="1526" key="#com.intellij.refactoring.rename.AutomaticRenamingDialog" timestamp="1594954562308"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| <state x="710" y="704" width="425" height="207" key="DetectedRootsChooserDialog/0.28.2560.1529@0.28.2560.1529" timestamp="1594872764235" /> | ||
| - <state width="1479" height="330" key="GridCell.Tab.0.bottom" timestamp="1594955986749"> | ||
| + <state width="1479" height="330" key="GridCell.Tab.0.bottom" timestamp="1594963811114"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state width="1479" height="330" key="GridCell.Tab.0.bottom/0.28.2560.1529@0.28.2560.1529" timestamp="1594955986749" /> | ||
| - <state width="1479" height="330" key="GridCell.Tab.0.center" timestamp="1594955986748"> | ||
| + <state width="1479" height="330" key="GridCell.Tab.0.bottom/0.28.2560.1529@0.28.2560.1529" timestamp="1594963811114" /> | ||
| + <state width="1479" height="330" key="GridCell.Tab.0.center" timestamp="1594963811113"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state width="1479" height="330" key="GridCell.Tab.0.center/0.28.2560.1529@0.28.2560.1529" timestamp="1594955986748" /> | ||
| - <state width="1479" height="330" key="GridCell.Tab.0.left" timestamp="1594955986748"> | ||
| + <state width="1479" height="330" key="GridCell.Tab.0.center/0.28.2560.1529@0.28.2560.1529" timestamp="1594963811113" /> | ||
| + <state width="1479" height="330" key="GridCell.Tab.0.left" timestamp="1594963811113"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state width="1479" height="330" key="GridCell.Tab.0.left/0.28.2560.1529@0.28.2560.1529" timestamp="1594955986748" /> | ||
| - <state width="1479" height="330" key="GridCell.Tab.0.right" timestamp="1594955986749"> | ||
| + <state width="1479" height="330" key="GridCell.Tab.0.left/0.28.2560.1529@0.28.2560.1529" timestamp="1594963811113" /> | ||
| + <state width="1479" height="330" key="GridCell.Tab.0.right" timestamp="1594963811113"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state width="1479" height="330" key="GridCell.Tab.0.right/0.28.2560.1529@0.28.2560.1529" timestamp="1594955986749" /> | ||
| - <state width="1479" height="406" key="GridCell.Tab.1.bottom" timestamp="1594948352434"> | ||
| + <state width="1479" height="330" key="GridCell.Tab.0.right/0.28.2560.1529@0.28.2560.1529" timestamp="1594963811113" /> | ||
| + <state width="1479" height="406" key="GridCell.Tab.1.bottom" timestamp="1594959771008"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state width="1479" height="406" key="GridCell.Tab.1.bottom/0.28.2560.1529@0.28.2560.1529" timestamp="1594948352434" /> | ||
| - <state width="1479" height="406" key="GridCell.Tab.1.center" timestamp="1594948352433"> | ||
| + <state width="1479" height="406" key="GridCell.Tab.1.bottom/0.28.2560.1529@0.28.2560.1529" timestamp="1594959771008" /> | ||
| + <state width="1479" height="406" key="GridCell.Tab.1.center" timestamp="1594959771007"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state width="1479" height="406" key="GridCell.Tab.1.center/0.28.2560.1529@0.28.2560.1529" timestamp="1594948352433" /> | ||
| - <state width="1479" height="406" key="GridCell.Tab.1.left" timestamp="1594948352432"> | ||
| + <state width="1479" height="406" key="GridCell.Tab.1.center/0.28.2560.1529@0.28.2560.1529" timestamp="1594959771007" /> | ||
| + <state width="1479" height="406" key="GridCell.Tab.1.left" timestamp="1594959771006"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state width="1479" height="406" key="GridCell.Tab.1.left/0.28.2560.1529@0.28.2560.1529" timestamp="1594948352432" /> | ||
| - <state width="1479" height="406" key="GridCell.Tab.1.right" timestamp="1594948352433"> | ||
| + <state width="1479" height="406" key="GridCell.Tab.1.left/0.28.2560.1529@0.28.2560.1529" timestamp="1594959771006" /> | ||
| + <state width="1479" height="406" key="GridCell.Tab.1.right" timestamp="1594959771007"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state width="1479" height="406" key="GridCell.Tab.1.right/0.28.2560.1529@0.28.2560.1529" timestamp="1594948352433" /> | ||
| + <state width="1479" height="406" key="GridCell.Tab.1.right/0.28.2560.1529@0.28.2560.1529" timestamp="1594959771007" /> | ||
| <state x="423" y="444" width="1026" height="842" key="SettingsEditor" timestamp="1594876039235"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| <state x="423" y="444" width="1026" height="842" key="SettingsEditor/0.28.2560.1529@0.28.2560.1529" timestamp="1594876039235" /> | ||
| - <state width="761" height="601" key="XDebugger.FullValuePopup" timestamp="1594940367498"> | ||
| + <state width="761" height="601" key="XDebugger.FullValuePopup" timestamp="1594959421070"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state width="761" height="601" key="XDebugger.FullValuePopup/0.28.2560.1529@0.28.2560.1529" timestamp="1594940367498" /> | ||
| + <state width="761" height="601" key="XDebugger.FullValuePopup/0.28.2560.1529@0.28.2560.1529" timestamp="1594959421070" /> | ||
| <state x="1068" y="347" width="1323" height="1003" key="com.intellij.history.integration.ui.views.FileHistoryDialog" timestamp="1594955523088"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| <state x="589" y="382" width="672" height="677" key="run.anything.popup/0.28.2560.1529@0.28.2560.1529" timestamp="1594954728874" /> | ||
| - <state x="589" y="477" width="672" height="678" key="search.everywhere.popup" timestamp="1594954743947"> | ||
| + <state x="589" y="477" width="672" height="678" key="search.everywhere.popup" timestamp="1594956705885"> | ||
| <screen x="0" y="28" width="2560" height="1529" /> | ||
| </state> | ||
| - <state x="589" y="477" width="672" height="678" key="search.everywhere.popup/0.28.2560.1529@0.28.2560.1529" timestamp="1594954743947" /> | ||
| + <state x="589" y="477" width="672" height="678" key="search.everywhere.popup/0.28.2560.1529@0.28.2560.1529" timestamp="1594956705885" /> | ||
| </component> | ||
| </project> | ||
| } | ||
| - public JComponent toComponent( final Dimension dimension ) { | ||
| - final var image = toImage( dimension ); | ||
| - | ||
| - return new JComponent() { | ||
| - @Override | ||
| - public Dimension getPreferredSize() { | ||
| - return new Dimension( | ||
| - image.getWidth( null ), image.getHeight( null ) | ||
| - ); | ||
| - } | ||
| - | ||
| - @Override | ||
| - protected void paintComponent( final Graphics graphics ) { | ||
| - super.paintComponent( graphics ); | ||
| - | ||
| - final var g = (Graphics2D) graphics.create(); | ||
| - g.drawImage( image, 0, 0, this ); | ||
| - } | ||
| - }; | ||
| - } | ||
| - | ||
| public Image toImage( final Dimension dstDim ) { | ||
| final var diagram = loadDiagram(); |
| import com.whitemagicsoftware.kmcaster.listeners.FrameDragListener; | ||
| +import com.whitemagicsoftware.kmcaster.listeners.Key; | ||
| import javax.swing.*; | ||
| import java.awt.*; | ||
| import java.awt.geom.RoundRectangle2D; | ||
| +import java.util.HashMap; | ||
| +import java.util.Map; | ||
| import static com.whitemagicsoftware.kmcaster.AppImage.*; | ||
| +import static com.whitemagicsoftware.kmcaster.listeners.Key.*; | ||
| -class EventFrame extends JFrame { | ||
| +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 final Map<KeyState, Image> mKeyStates = new HashMap<>(); | ||
| + private final Map<Key, ImageComponent> mKeyViews = new HashMap<>(); | ||
| public EventFrame() { | ||
| + setDefaultCloseOperation( EXIT_ON_CLOSE ); | ||
| setLocationRelativeTo( null ); | ||
| setUndecorated( true ); | ||
| final var dimensions = new Dimension( getWidth(), getHeight() - 10 ); | ||
| - final var mouseView = MOUSE_LR.toComponent( dimensions ); | ||
| - final var shiftView = KEY_UP_SHIFT.toComponent( dimensions ); | ||
| - final var ctrlView = KEY_UP_CTRL.toComponent( dimensions ); | ||
| - final var altView = KEY_UP_ALT.toComponent( dimensions ); | ||
| - final var regularView = KEY_UP_REGULAR.toComponent( dimensions ); | ||
| + | ||
| + final var mouseImage = MOUSE_REST.toImage( dimensions ); | ||
| + final var mouseComponent = createImageComponent( mouseImage ); | ||
| + | ||
| + final var shiftUpImage = KEY_UP_SHIFT.toImage( dimensions ); | ||
| + final var ctrlUpImage = KEY_UP_CTRL.toImage( dimensions ); | ||
| + final var altUpImage = KEY_UP_ALT.toImage( dimensions ); | ||
| + final var regularUpImage = KEY_UP_REGULAR.toImage( dimensions ); | ||
| + | ||
| + final var shiftComponent = createImageComponent( shiftUpImage ); | ||
| + final var ctrlComponent = createImageComponent( ctrlUpImage ); | ||
| + final var altComponent = createImageComponent( altUpImage ); | ||
| + final var regularComponent = createImageComponent( regularUpImage ); | ||
| final var panel = new JPanel(); | ||
| panel.setAlignmentX( Component.CENTER_ALIGNMENT ); | ||
| panel.setBackground( TRANSLUCENT ); | ||
| - panel.add( mouseView ); | ||
| - panel.add( shiftView ); | ||
| - panel.add( ctrlView ); | ||
| - panel.add( altView ); | ||
| - panel.add( regularView ); | ||
| + panel.add( mouseComponent ); | ||
| + panel.add( shiftComponent ); | ||
| + panel.add( ctrlComponent ); | ||
| + panel.add( altComponent ); | ||
| + panel.add( regularComponent ); | ||
| final var content = getContentPane(); | ||
| final var layout = new BoxLayout( content, BoxLayout.Y_AXIS ); | ||
| content.setLayout( layout ); | ||
| content.add( panel ); | ||
| + | ||
| + final var shiftDnImage = KEY_DN_SHIFT.toImage( dimensions ); | ||
| + final var ctrlDnImage = KEY_DN_CTRL.toImage( dimensions ); | ||
| + final var altDnImage = KEY_DN_ALT.toImage( dimensions ); | ||
| + final var regularDnImage = KEY_DN_REGULAR.toImage( dimensions ); | ||
| + | ||
| + mKeyStates.put( new KeyState( KEY_SHIFT, false ), shiftUpImage ); | ||
| + mKeyStates.put( new KeyState( KEY_SHIFT, true ), shiftDnImage ); | ||
| + mKeyStates.put( new KeyState( KEY_CTRL, false ), ctrlUpImage ); | ||
| + mKeyStates.put( new KeyState( KEY_CTRL, true ), ctrlDnImage ); | ||
| + mKeyStates.put( new KeyState( KEY_ALT, false ), altUpImage ); | ||
| + mKeyStates.put( new KeyState( KEY_ALT, true ), altDnImage ); | ||
| + mKeyStates.put( new KeyState( KEY_REGULAR, false ), regularUpImage ); | ||
| + mKeyStates.put( new KeyState( KEY_REGULAR, true ), regularDnImage ); | ||
| + | ||
| + mKeyViews.put( KEY_SHIFT, shiftComponent ); | ||
| + mKeyViews.put( KEY_CTRL, ctrlComponent ); | ||
| + mKeyViews.put( KEY_ALT, altComponent ); | ||
| + mKeyViews.put( KEY_REGULAR, regularComponent ); | ||
| + } | ||
| + | ||
| + protected void updateKeys( final KeyState keyState ) { | ||
| + final var image = mKeyStates.get( keyState ); | ||
| + final var component = mKeyViews.get( keyState.getKey() ); | ||
| + | ||
| + component.repaint( image ); | ||
| + } | ||
| + | ||
| + private ImageComponent createImageComponent( final Image image ) { | ||
| + return new ImageComponent( image ); | ||
| } | ||
| +/* | ||
| + * Copyright 2020 White Magic Software, Ltd. | ||
| + * | ||
| + * All rights reserved. | ||
| + * | ||
| + * Redistribution and use in source and binary forms, with or without | ||
| + * modification, are permitted provided that the following conditions are met: | ||
| + * | ||
| + * o Redistributions of source code must retain the above copyright | ||
| + * notice, this list of conditions and the following disclaimer. | ||
| + * | ||
| + * o Redistributions in binary form must reproduce the above copyright | ||
| + * notice, this list of conditions and the following disclaimer in the | ||
| + * documentation and/or other materials provided with the distribution. | ||
| + * | ||
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| + */ | ||
| +package com.whitemagicsoftware.kmcaster; | ||
| + | ||
| +import javax.swing.*; | ||
| +import java.awt.*; | ||
| + | ||
| +/** | ||
| + * Responsible for drawing an image, which can be changed at any time. | ||
| + */ | ||
| +public class ImageComponent extends JComponent { | ||
| + /** | ||
| + * Mutable image. | ||
| + */ | ||
| + private Image mImage; | ||
| + | ||
| + ImageComponent( final Image image ) { | ||
| + mImage = image; | ||
| + } | ||
| + | ||
| + @Override | ||
| + public Dimension getPreferredSize() { | ||
| + final var image = mImage; | ||
| + | ||
| + return new Dimension( | ||
| + image.getWidth( null ), image.getHeight( null ) | ||
| + ); | ||
| + } | ||
| + | ||
| + @Override | ||
| + protected void paintComponent( final Graphics graphics ) { | ||
| + super.paintComponent( graphics ); | ||
| + | ||
| + final var g = (Graphics2D) graphics.create(); | ||
| + g.drawImage( mImage, 0, 0, this ); | ||
| + } | ||
| + | ||
| + /** | ||
| + * Repaints this component using the given image. This is a mutable | ||
| + * operation that changes the internal {@link Image} instance. | ||
| + * | ||
| + * @param image The new image to use for painting. | ||
| + */ | ||
| + public void repaint( final Image image ) { | ||
| + mImage = image; | ||
| + repaint(); | ||
| + } | ||
| +} | ||
| +/* | ||
| + * Copyright 2020 White Magic Software, Ltd. | ||
| + * | ||
| + * All rights reserved. | ||
| + * | ||
| + * Redistribution and use in source and binary forms, with or without | ||
| + * modification, are permitted provided that the following conditions are met: | ||
| + * | ||
| + * o Redistributions of source code must retain the above copyright | ||
| + * notice, this list of conditions and the following disclaimer. | ||
| + * | ||
| + * o Redistributions in binary form must reproduce the above copyright | ||
| + * notice, this list of conditions and the following disclaimer in the | ||
| + * documentation and/or other materials provided with the distribution. | ||
| + * | ||
| + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| + * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| + */ | ||
| +package com.whitemagicsoftware.kmcaster; | ||
| + | ||
| +import com.whitemagicsoftware.kmcaster.listeners.Key; | ||
| + | ||
| +import static java.lang.Boolean.parseBoolean; | ||
| + | ||
| +public class KeyState { | ||
| + private final Key mKey; | ||
| + private final String mState; | ||
| + | ||
| + public KeyState( final Key key, final boolean pressed ) { | ||
| + assert key != null; | ||
| + | ||
| + mKey = key; | ||
| + mState = Boolean.toString( pressed ); | ||
| + } | ||
| + | ||
| + public KeyState( final Key key, final String pressed ) { | ||
| + this( key, parseBoolean( pressed ) ); | ||
| + } | ||
| + | ||
| + public Key getKey() { | ||
| + return mKey; | ||
| + } | ||
| + | ||
| + @Override | ||
| + public boolean equals( final Object o ) { | ||
| + if( this == o ) { | ||
| + return true; | ||
| + } | ||
| + if( o == null || getClass() != o.getClass() ) { | ||
| + return false; | ||
| + } | ||
| + | ||
| + final KeyState keyState = (KeyState) o; | ||
| + | ||
| + if( mKey != keyState.mKey ) { | ||
| + return false; | ||
| + } | ||
| + return mState.equals( keyState.mState ); | ||
| + } | ||
| + | ||
| + @Override | ||
| + public int hashCode() { | ||
| + int result = mKey.hashCode(); | ||
| + result = 31 * result + mState.hashCode(); | ||
| + return result; | ||
| + } | ||
| +} | ||
| package com.whitemagicsoftware.kmcaster; | ||
| +import com.whitemagicsoftware.kmcaster.listeners.Key; | ||
| import com.whitemagicsoftware.kmcaster.listeners.KeyboardListener; | ||
| import com.whitemagicsoftware.kmcaster.listeners.MouseListener; | ||
| import org.jnativehook.GlobalScreen; | ||
| -import javax.swing.*; | ||
| import java.beans.PropertyChangeEvent; | ||
| import java.beans.PropertyChangeListener; | ||
| @Override | ||
| - public void propertyChange( final PropertyChangeEvent evt ) { | ||
| - System.out.println( evt ); | ||
| + public void propertyChange( final PropertyChangeEvent e ) { | ||
| + final var keyState = createKeyState( e.getPropertyName(), e.getNewValue() ); | ||
| + updateKeys( keyState ); | ||
| + } | ||
| + | ||
| + private KeyState createKeyState( final String name, final Object bool ) { | ||
| + final var key = Key.valueFrom( name ); | ||
| + final var state = bool.toString(); | ||
| + | ||
| + return new KeyState( key, state ); | ||
| } | ||
| } | ||
| + /** | ||
| + * Looks up the key that matches the given name, case-insensitively. | ||
| + * | ||
| + * @param name The name of the key to find in this enum. | ||
| + * @return The {@link Key} object that matches the name. | ||
| + */ | ||
| + public static Key valueFrom( final String name ) { | ||
| + for( final var b : Key.values() ) { | ||
| + if( b.isName( name ) ) { | ||
| + return b; | ||
| + } | ||
| + } | ||
| + | ||
| + return KEY_REGULAR; | ||
| + } | ||
| + | ||
| @Override | ||
| public String toString() { |
| @Override | ||
| public void nativeKeyPressed( final NativeKeyEvent e ) { | ||
| - final String regularHeld = getDisplayText( e ); | ||
| - boolean isModifier = false; | ||
| - | ||
| - // The key is regular iff its name does not match any modifier name. | ||
| - for( final var modifier : mModifiers ) { | ||
| - isModifier |= modifier.isKeyName( regularHeld ); | ||
| - } | ||
| - | ||
| - // If it's not a modifier key, broadcast the regular value. | ||
| - if( !isModifier ) { | ||
| - tryFire( KEY_REGULAR, mRegularHeld, regularHeld ); | ||
| - mRegularHeld = regularHeld; | ||
| - } | ||
| - | ||
| + updateRegular( mRegularHeld, getDisplayText( e ) ); | ||
| updateModifiers( e ); | ||
| } | ||
| @Override | ||
| public void nativeKeyReleased( final NativeKeyEvent e ) { | ||
| - final String oldValue = getDisplayText( e ); | ||
| - final String newValue = ""; | ||
| - | ||
| - tryFire( KEY_REGULAR, oldValue, newValue ); | ||
| - mRegularHeld = newValue; | ||
| - | ||
| + updateRegular( getDisplayText( e ), "" ); | ||
| updateModifiers( e ); | ||
| } | ||
| @Override | ||
| public void nativeKeyTyped( final NativeKeyEvent e ) { | ||
| + } | ||
| + | ||
| + /** | ||
| + * State for a regular (non-modifier) key has changed. | ||
| + * | ||
| + * @param o Previous key value. | ||
| + * @param n Current key value. | ||
| + */ | ||
| + private void updateRegular( final String o, final String n ) { | ||
| + assert o != null; | ||
| + assert n != null; | ||
| + | ||
| + boolean isModifier = false; | ||
| + | ||
| + // The key is regular iff its name does not match any modifier name. | ||
| + for( final var modifier : mModifiers ) { | ||
| + isModifier |= (modifier.isKeyName( n ) || modifier.isKeyName( o )); | ||
| + } | ||
| + | ||
| + // If it's not a modifier key, broadcast the regular value. | ||
| + if( !isModifier ) { | ||
| + tryFire( KEY_REGULAR, o, n ); | ||
| + mRegularHeld = n; | ||
| + } | ||
| } | ||
| Author | DaveJarvis <email> |
|---|---|
| Date | 2020-07-16 22:35:04 GMT-0700 |
| Commit | 620583d8dcc190ec93579c86abfc4abd16d5bc39 |
| Parent | e8a1560 |
| Delta | 291 lines added, 84 lines removed, 207-line increase |