Witaj!
tBane dodał nowy post w wątku: Schowek - Logika wycinania, wklejania i przesuwania obrazu
Cześć. Mam problem, którego nie potrafię samodzielnie rozwiąza, a mianowicie wklejanie i wycinanie obrazu. Na początek przedstawię funkcje kopiowania i wycinania.
Kopiuj
void copyImage(sf::Image& image) { sf::Vector2i s(std::min(start_px.x, end_px.x), std::min(start_px.y, end_px.y)); sf::Vector2i e(std::max(start_px.x, end_px.x), std::max(start_px.y, end_px.y)); // copy the image this->img = sf::Image(); this->img.create(e.x - s.x, e.y - s.y, sf::Color::Transparent); this->img.copy(image, 0, 0, sf::IntRect(s.x, s.y, e.x - s.x, e.y - s.y), false); } void cutImage(sf::Image& image) { sf::Vector2i s(std::min(start_px.x, end_px.x), std::min(start_px.y, end_px.y)); sf::Vector2i e(std::max(start_px.x, end_px.x), std::max(start_px.y, end_px.y)); // cut the image sf::Image background; background.create(e.x - s.x, e.y - s.y, sf::Color::Transparent); image.copy(background, s.x, s.y, sf::IntRect(0, 0, e.x - s.x, e.y - s.y), false); } void pasteImage(sf::Image& image) { sf::Vector2i s(std::min(start_px.x, end_px.x), std::min(start_px.y, end_px.y)); sf::Vector2i e(std::max(start_px.x, end_px.x), std::max(start_px.y, end_px.y)); image.copy(this->img, s.x, s.y, sf::IntRect(0, 0, this->img.getSize().x, this->img.getSize().y), true); this->img = sf::Image(); }
Teraz próbuję opakować te funkcje w Eventy i robię to w następujący sposób:
Kopiuj
if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { ElementGUI_pressed = this; if (tools->toolType == ToolType::Brush || tools->toolType == ToolType::Eraser) drawPixels(colors_dialog->current_color); if (tools->toolType == ToolType::Eraser) drawPixels(sf::Color::Transparent); if (tools->toolType == ToolType::Selector) { if (selection->clickOnSelection(worldToTile(worldMousePosition, position, size, zoom, zoom_delta))) { if (!selection->isMoved) { selection->isMoved = true; selection->setOffset(worldToTile(worldMousePosition, position, size, zoom, zoom_delta)); selection->copyImage(layers_dialog->getCurrentLayer()->image); selection->cutImage(layers_dialog->getCurrentLayer()->image); } } else { selection->start_px = worldToTile(worldMousePosition, position, size, zoom, zoom_delta); selection->end_px = selection->start_px; selection->isMoved = false; } selecting = true; } } if (event.type == sf::Event::MouseMoved && sf::Mouse::isButtonPressed(sf::Mouse::Left)) { if (tools->toolType == ToolType::Brush) { drawPixels(colors_dialog->getCurrentColor()); } if (tools->toolType == ToolType::Eraser) { drawPixels(sf::Color::Transparent); } if (tools->toolType == ToolType::Selector) { if (!selection->isMoved) { selection->end_px = worldToTile(worldMousePosition, position, size, zoom, zoom_delta); } else { selection->move( worldToTile(worldMousePosition, position, size, zoom, zoom_delta), layers_dialog->getCurrentLayer()->image.getSize() ); selecting = true; // <<< utrzymuj widoczność podczas przenoszenia } } } if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left) { if (tools->toolType == ToolType::Selector) { if (selection->isMoved) { selection->isMoved = false; selection->pasteImage(layers_dialog->getCurrentLayer()->image); } selecting = (std::abs(selection->start_px.x - selection->end_px.x) >= 1) || (std::abs(selection->start_px.y - selection->end_px.y) >= 1); } }
No i niestety nie działa. Za każdym razem gdy przesunę zaznaczony fragment obrazu to nadpisuje mi on oryginalny obraz a nie powinien ponieważ nadal jest przesuwany. Obraz powinien być wklejany dopiero gdy zaznaczenie przestaje być aktywne. Chciałbym zrobić poprawnie przenoszenie grafiki ale nie wiem jak to zrobić.. Pomożecie ? Kodu jest dużo więc wkleiłem istotne fragmenty, ale jeżeli uważasz, że dobrze by było uruchomić projekt u siebie to dorzucam link do repozytorium. [a href="https://github.com/tBane1995/Anim-Paint.git"] Powyższy kod znajduje się w pliku "Canvas.hpp" oraz "Selection.hpp"
Z poważaniem, 4programmers.net
|