Home > aen's Blog

Tightening Screws

smee, build 6

Laundry List

I've started cleaning up and polishing things a little here and there.

  1. 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.
  2. 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 MapPanel into Main.
  3. Spiffed up the menus slightly and made sure everything had mnemonics.
  4. It was inconsistent how you couldn't ESC out of the New Map dialog, so I fixed that.
  5. I also didn't like how I was recreating most of the UI each time a new map was created or loaded. That is, Viewport and TileSelector, as well as the slider for the zoom were all recreated each time. So, I refactored the two so that they didn't demand a Map immediately. Then in Main I create the full UI and just call the new method set_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.
  6. I also want to eventually make the undo functionality encompass more than tile plots, so I preemptively shifted the PlotHistory instance from MapPanel into Main as well.
  7. That hackeriffic ImageTransferHandler was an eyesore in TileSelector so I moved it out into its own file and renamed it to TileSelectorClipboardTransferHandler. Kind of a mouthful, but it clearly indicates its association with TileSelector and that it deals witth the clipboard.
  8. The key listeners for MapPanel and TileSelector were somewhat haphazard, as JPanel objects don't accept focus by default. I moved these into the more proper input and action maps on the Viewport. 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.
  9. 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!