Tightening Screws
smee, build 6
Laundry List
I've started cleaning up and polishing things a little here and there.
- The checkbox for grid toggling was kinda ugly. I moved that to a new View checkbox menu item. Much prettier. I even gave it a hotkey,
CTRL+G. - I didn't like how the grid and zoom settings would revert to defaults after creating or loading maps, so I shifted the responsibility for those from
MapPanelintoMain. - Spiffed up the menus slightly and made sure everything had mnemonics.
- It was inconsistent how you couldn't
ESCout of theNew Mapdialog, so I fixed that. - I also didn't like how I was recreating most of the UI each time a new map was created or loaded. That is,
ViewportandTileSelector, as well as the slider for the zoom were all recreated each time. So, I refactored the two so that they didn't demand aMapimmediately. Then inMainI create the full UI and just call the new methodset_map()each time I create a new map or load one. This method takes care of making sure the viewport and tile selector receive the new map and are laid out and painted again. - I also want to eventually make the undo functionality encompass more than tile plots, so I preemptively shifted the
PlotHistoryinstance fromMapPanelintoMainas well. - That hackeriffic
ImageTransferHandlerwas an eyesore inTileSelectorso I moved it out into its own file and renamed it toTileSelectorClipboardTransferHandler. Kind of a mouthful, but it clearly indicates its association withTileSelectorand that it deals witth the clipboard. - The key listeners for
MapPanelandTileSelectorwere somewhat haphazard, asJPanelobjects don't accept focus by default. I moved these into the more proper input and action maps on theViewport. Now all of the hot keys for undo/redo, pasting a new tile from the clipboard, deleting a tile, and toggling the grid are all in one tidy location, and all handled the same way. - I made the mousewheel alter the zoom level, like all good map editors should!
Those are all the key points for this iteration, I think.
You'll notice Main is becoming kind of a master hub for all the broad operations. It also carries state for these operations. I think that was a point of some confusion for me in the past. I would try to tie a lot of these operations in with specific components, not always sure what to put where. I won't say I always know that even now, but I'm getting better at it!
MVC
Another source of confusion for me back in the day was using UI components as data sources. Before learning much about the Model/View/Controller pattern, I would often attempt to use components themselves as the data model for various settings.
For example, in I would've tried to use the JSlider component that does the zooming as the data source for the zoom level. I wouldn't have an actual variable anywhere that stored the zoom level. And if I did it still would often receive its value from the slider.
Of course, this is a bad idea because UI components are supposed to be "backed" by your internal data, merely providing a visual interpretation of your data. The main idea behind MVC is that you can provide numerous views for the same data.
At any rate, MVC is super great! Read up on it!
Alternatively, check back over the weekend when I cover MVC and event listeners!