cloudhead / rx

👾 Modern and minimalist pixel editor
https://discord.gg/xHggPjfsS9
GNU General Public License v3.0
3.09k stars 109 forks source link

Implement selection flipping #85

Closed luciusmagn closed 4 years ago

luciusmagn commented 4 years ago

Hi, this is my humble attempt at implementing the functionality to flip a selection. It was once again a pleasant experience as I really like the structure of the code, but I'm not sure if I'm doing it "idiomatically".

Here is a demonstration of how it looks: flip.gif (yea, the colors are screwed because it's a gif, here's a webm for more pleasant viewing -> flip.webm)

This is what I did for these changes:

  1. create a command selection/flip which takes one argument of direction, valid values are h or horizontal and v or vertical.
  2. then I created a ViewOp for flip and went to gl/mod.rs to create an implementation. It's pretty much a yank, which also takes direction as an argument and then flips the pixels accordingly
  3. In session.rs, I added appropriate functions taking yank as example. To allow possibly doing a bunch of flips with a hotkey, I figured the easiest would be doing it in the place of selection, so I did this:
            Command::SelectionFlip(dir) => {
                self.flip_selection(dir);
                // I think its handy to flip in place for now, hence these commands
                // 1., 2. prevent overlap and paste
                // 3.     preserve location so you can flip repeatedly w/ hotkey
                self.command(Command::SelectionErase);
                self.command(Command::SelectionPaste);
                self.command(Command::Mode(Mode::Visual(VisualState::Selecting { dragging: false })));
            }

I'm really unsure if this is the correct way to do it, so I am sorry if I did something wrong, haha :sweat_smile:

cloudhead commented 4 years ago

Awesome, thanks! I think the approach is right overall, with a clever use of the paste buffer :ok_hand:

I'm going to comb :eyes: through it as soon as I have some time.

luciusmagn commented 4 years ago

thanks! I was considering doing rotations as well, but I'm unsure about changing the dimensions of a selection and have yet to figure out a eloquent solution, but I haven't had the need to rotate anything in my five days of using rx so it's not a priority.

Here are some other ideas that I'm considering trying, though:

if you think any of these is a particulary good idea, I can prioritize it, or if you have any tips what to watch out for, it would be awesome :D

cloudhead commented 4 years ago
  • own file format for rx, which would just be the entire state of the editor dumped into a file, so I can get back to work with the palette loaded in, undo/redo history, layers, views, frames etc. preserved and ready.

Yes, I'd love to have something like this. Actually, part of this is already implemented via the "archive" format (.rxa) which I had to implement for layers functionality.

The other part exists via the .rxrc support, which is loaded when you start rx from a directory with an .rxrc file. Combining those would involve having .rxrc files inside .rxa archives, that would be auto-loaded when you open an archive, plus a way to create a .rxrc from an rx session. Palette writing already exists via p/write, but in the archive case, you could just write the colors to the .rxrc..

cloudhead commented 4 years ago
  • color replace function for when I'm a dingus (probably allow to replace foreground w/ background or the other way around?)

I also want this. The way I was thinking is to just replace the current bg with the current fg. So you would do:

  1. Pick two colors with the sampler (or via a new pick command?)
  2. Call :colors/replace with no arguments to replace the first color selected (bg) with the second (fg).

The command could also take arguments, in case the color isn't in your session, then you would do :colors/replace <old> <new>

luciusmagn commented 4 years ago

Yes, I'd love to have something like this. Actually, part of this is already implemented via the "archive" format (.rxa) which I had to implement for layers functionality.

The other part exists via the .rxrc support, which is loaded when you start rx from a directory with an .rxrc file. Combining those would involve having .rxrc files inside .rxa archives, that would be auto-loaded when you open an archive, plus a way to create a .rxrc from an rx session. Palette writing already exists via p/write, but in the archive case, you could just write the colors to the .rxrc..

Oh, that's great, I hadn't known about ˙.rxa, I'll look into it! Using them in conjunction with.rxrc` sounds like a good idea, yea!

luciusmagn commented 4 years ago

I also want this. The way I was thinking is to just replace the current bg with the current fg. So you would do:

1. Pick two colors with the sampler (or via a new `pick` command?)

2. Call `:colors/replace` with no arguments to replace the first color selected (bg) with the second (fg).

The command could also take arguments, in case the color isn't in your session, then you would do :colors/replace <old> <new>

Yea, that's pretty much what I thought I'd try. The colorpicking command might a great addition to, especially in reference to #22 .

the :colors/replace <old> <new> could possibly work with not just arbitrary colors but indexes in palette too, although the utility might not be as high unless one can tell a color's number easily (perhaps with a optional index number?)

cloudhead commented 4 years ago

the :colors/replace <old> <new> could possibly work with not just arbitrary colors but indexes in palette too, although the utility might not be as high unless one can tell a color's number easily (perhaps with a optional index number?)

Note that a semi-hidden feature is that you can use the color sampler while in command mode, and the hex code is added to the command line. This is a lot easier if you're picking from the palette.

luciusmagn commented 4 years ago

Note that a semi-hidden feature is that you can use the color sampler while in command mode, and the hex code is added to the command line. This is a lot easier if you're picking from the palette.

Oh, I didn't know that! Yea, that invalidates my concern completely, haha

cloudhead commented 4 years ago

@luciusmagn do you want to finish this?

luciusmagn commented 4 years ago

Yea, I do. Sorry, I was graduating which kinda temporarily brought my programming endeavors to a screeching halt. I resolved most of the issues already, I will commit them asap!

cloudhead commented 4 years ago

Thanks for this contribution!