11package com .thecoderscorner .menu .editorui .gfxui .imgedit ;
22
3+ import com .thecoderscorner .embedcontrol .core .service .GlobalSettings ;
34import com .thecoderscorner .menu .domain .util .PortablePalette ;
45import com .thecoderscorner .menu .editorui .dialog .BaseDialogSupport ;
56import com .thecoderscorner .menu .editorui .gfxui .pixmgr .BmpDataManager ;
1415import javafx .scene .control .Button ;
1516import javafx .scene .control .ComboBox ;
1617import javafx .scene .control .Label ;
18+ import javafx .scene .input .*;
1719import javafx .scene .layout .BorderPane ;
1820import javafx .scene .layout .HBox ;
1921import javafx .stage .Stage ;
2325import java .nio .file .Paths ;
2426import java .util .Optional ;
2527
26- import static com .thecoderscorner .menu .editorui .gfxui .imgedit .ImageDrawingGrid .DrawingMode ;
28+ import static com .thecoderscorner .menu .editorui .gfxui .imgedit .ImageDrawingGrid .DrawingMode .* ;
2729import static com .thecoderscorner .menu .editorui .gfxui .imgedit .SimpleImagePane .shortFmtText ;
2830
2931public class SimpleImageEditor {
3032 private final BmpDataManager bitmap ;
3133 private final PortablePalette palette ;
3234 private final NativePixelFormat format ;
35+ private CurrentProjectEditorUI editorUI ;
36+ private ComboBox <TextDrawingMode > modeComboBox ;
3337
3438 public SimpleImageEditor (BmpDataManager bitmap , NativePixelFormat format , PortablePalette palette ) {
3539 this .bitmap = bitmap ;
@@ -40,38 +44,36 @@ public SimpleImageEditor(BmpDataManager bitmap, NativePixelFormat format, Portab
4044 public boolean presentUI (CurrentProjectEditorUI editorUI ) {
4145 BorderPane pane = new BorderPane ();
4246 pane .setOpaqueInsets (new Insets (10 ));
47+ this .editorUI = editorUI ;
4348
4449 HBox hbox = new HBox (4 );
4550 hbox .setAlignment (Pos .CENTER_LEFT );
4651 hbox .getChildren ().add (new Label ("Function" ));
4752 pane .setTop (hbox );
4853 ImageDrawingGrid canvas = new ImageDrawingGrid (bitmap , palette , true );
49- var modeComboBox = new ComboBox <>(FXCollections .observableArrayList (
50- DrawingMode .LINE , DrawingMode .OUTLINE_RECT , DrawingMode .FILLED_RECT
54+ modeComboBox = new ComboBox <>(FXCollections .observableArrayList (
55+ new TextDrawingMode ("Pixel - D" , DOT ),
56+ new TextDrawingMode ("Line - L" , LINE ),
57+ new TextDrawingMode ("Box Outline - R" , OUTLINE_RECT ),
58+ new TextDrawingMode ("Box Filled - B" , FILLED_RECT ),
59+ new TextDrawingMode ("Circle - I" , OUTLINE_CIRCLE ),
60+ new TextDrawingMode ("Flood Fill - F" , FLOOD_FILL )
5161 ));
5262 modeComboBox .getSelectionModel ().select (0 );
53- modeComboBox .setOnAction (_ -> canvas .setCurrentShape (modeComboBox .getValue ()));
63+ modeComboBox .setOnAction (_ -> canvas .setCurrentShape (modeComboBox .getValue (). mode () ));
5464 hbox .getChildren ().add (modeComboBox );
5565
5666 hbox .getChildren ().add (new Label ("Palette" ));
5767 UIColorPaletteControl paletteControl = new UIColorPaletteControl ();
5868 hbox .getChildren ().add (paletteControl .swatchControl (palette , canvas ::setCurrentColor ));
5969
70+ var copyButton = new Button ("Copy" );
71+ copyButton .setOnAction (_ -> copyContents ());
6072 var saveButton = new Button ("Save" );
61- saveButton .setOnAction (_ -> {
62- var file = editorUI .findFileNameFromUser (Optional .empty (), false , "*.png" );
63- if (file .isEmpty ()) return ;
64- try {
65- var img = bitmap .createImageFromBitmap (palette );
66- ImageIO .write (SwingFXUtils .fromFXImage (img , null ), "png" , Paths .get (file .get ()).toFile ());
67- } catch (IOException e ) {
68- editorUI .alertOnError ("Error Saving Image" , "An error occurred while saving the image." );
69- }
70- });
71- hbox .getChildren ().add (saveButton );
73+ saveButton .setOnAction (_ -> saveContents ());
7274 var xyLabel = new Label ("" );
73- canvas .onPositionUpdate ((x , y ) -> xyLabel .setText (STR ."X=\{x }, Y=\{y }" ));
74- hbox .getChildren ().add ( xyLabel );
75+ canvas .setPositionUpdateListener ((x , y ) -> xyLabel .setText (STR ."X=\{x }, Y=\{y }" ));
76+ hbox .getChildren ().addAll ( copyButton , saveButton , xyLabel );
7577
7678 canvas .widthProperty ().bind (pane .widthProperty ());
7779 canvas .heightProperty ().bind (pane .heightProperty ().multiply (0.9 ));
@@ -81,8 +83,32 @@ public boolean presentUI(CurrentProjectEditorUI editorUI) {
8183
8284 pane .setCenter (canvas );
8385 pane .getStyleClass ().add ("background" );
86+ pane .setStyle (STR ."-fx-font-size: \{GlobalSettings .defaultFontSize ()}" );
8487
8588 Scene scene = new Scene (pane );
89+
90+ KeyCombination copyPressed = new KeyCodeCombination (KeyCode .C , KeyCombination .SHORTCUT_DOWN );
91+ KeyCombination savePressed = new KeyCodeCombination (KeyCode .S , KeyCombination .SHORTCUT_DOWN );
92+ KeyCombination dotPressed = new KeyCodeCombination (KeyCode .D );
93+ KeyCombination linePressed = new KeyCodeCombination (KeyCode .L );
94+ KeyCombination circlePressed = new KeyCodeCombination (KeyCode .I );
95+ KeyCombination boxPressed = new KeyCodeCombination (KeyCode .B );
96+ KeyCombination rectPressed = new KeyCodeCombination (KeyCode .R );
97+ KeyCombination fillPressed = new KeyCodeCombination (KeyCode .F );
98+ scene .addEventFilter (KeyEvent .KEY_PRESSED , ke -> {
99+ if (copyPressed .match (ke )) {
100+ copyContents ();
101+ ke .consume (); // <-- stops passing the event to next node
102+ } else if (savePressed .match (ke )) {
103+ saveContents ();
104+ ke .consume (); // <-- stops passing the event to next node
105+ } else if (dotPressed .match (ke )) changeShape (DOT );
106+ else if (linePressed .match (ke )) changeShape (LINE );
107+ else if (circlePressed .match (ke )) changeShape (OUTLINE_CIRCLE );
108+ else if (boxPressed .match (ke )) changeShape (FILLED_RECT );
109+ else if (rectPressed .match (ke )) changeShape (OUTLINE_RECT );
110+ else if (fillPressed .match (ke )) changeShape (FLOOD_FILL );
111+ });
86112 Stage stage = new Stage ();
87113 stage .setMaximized (true );
88114 stage .setWidth (800 );
@@ -93,4 +119,38 @@ public boolean presentUI(CurrentProjectEditorUI editorUI) {
93119 stage .showAndWait ();
94120 return canvas .isModified ();
95121 }
122+
123+ private void changeShape (ImageDrawingGrid .DrawingMode drawingMode ) {
124+ for (int i =0 ;i <modeComboBox .getItems ().size (); i ++) {
125+ if (modeComboBox .getItems ().get (i ).mode () == drawingMode ) {
126+ modeComboBox .getSelectionModel ().select (i );
127+ break ;
128+ }
129+ }
130+ }
131+
132+ private void saveContents () {
133+ var file = editorUI .findFileNameFromUser (Optional .empty (), false , "*.png" );
134+ if (file .isEmpty ()) return ;
135+ try {
136+ var img = bitmap .createImageFromBitmap (palette );
137+ ImageIO .write (SwingFXUtils .fromFXImage (img , null ), "png" , Paths .get (file .get ()).toFile ());
138+ } catch (IOException e ) {
139+ editorUI .alertOnError ("Error Saving Image" , "An error occurred while saving the image." );
140+ }
141+ }
142+
143+ private void copyContents () {
144+ Clipboard clipboard = Clipboard .getSystemClipboard ();
145+ ClipboardContent content = new ClipboardContent ();
146+ content .putImage (bitmap .createImageFromBitmap (palette ));
147+ clipboard .setContent (content );
148+ }
149+
150+ record TextDrawingMode (String name , ImageDrawingGrid .DrawingMode mode ) {
151+ @ Override
152+ public String toString () {
153+ return name ;
154+ }
155+ }
96156}
0 commit comments