// ----------------------------------------------------------------------------- // Called when the mouse pointer is moved within the canvas // ----------------------------------------------------------------------------- void GfxCanvas::onMouseMovement(wxMouseEvent& e) { bool refresh = false; // Check if the mouse is over the image int x = e.GetPosition().x; int y = e.GetPosition().y - 2; bool on_image = onImage(x, y); cursor_pos_ = imageCoords(x, y); if (on_image && editing_mode_ != EditMode::None) { if (cursor_pos_ != prev_pos_) generateBrushShadow(); prev_pos_ = cursor_pos_; } if (on_image != image_hilight_) { image_hilight_ = on_image; refresh = true; // Update cursor if drag allowed if (on_image) { if (editing_mode_ != EditMode::None) SetCursor(wxCursor(wxCURSOR_PENCIL)); else if (allow_drag_) SetCursor(wxCursor(wxCURSOR_SIZING)); } else if (allow_drag_ && !e.LeftIsDown()) SetCursor(wxNullCursor); } // Drag if (e.LeftIsDown()) { if (editing_mode_ != EditMode::None) { brushCanvas(x, y); } else { drag_pos_.set(e.GetPosition().x, e.GetPosition().y); refresh = true; } } else if (e.MiddleIsDown()) { offset_ = offset_ + Vec2d(e.GetPosition().x - mouse_prev_.x, e.GetPosition().y - mouse_prev_.y); refresh = true; } // Right mouse down if (e.RightIsDown() && on_image) pickColour(x, y); if (refresh) Refresh(); mouse_prev_.set(e.GetPosition().x, e.GetPosition().y); }
/* GfxCanvas::brushCanvas * Finds all the pixels under the brush, and paints them. *******************************************************************/ void GfxCanvas::brushCanvas(int x, int y) { if (brush == nullptr) return; point2_t coord = imageCoords(x, y); for (int i = -4; i < 5; ++i) for (int j = -4; j < 5; ++j) if (brush->getPixel(i, j)) paintPixel(coord.x + i, coord.y + j); }
// ----------------------------------------------------------------------------- // Returns true if the given coordinates are 'on' top of the image // ----------------------------------------------------------------------------- bool GfxCanvas::onImage(int x, int y) { // Don't disable in editing mode; it can be quite useful // to have a live preview of how a graphic will tile. if (view_type_ == View::Tiled && editing_mode_ == EditMode::None) return false; // No need to duplicate the imageCoords code. return imageCoords(x, y) != Vec2i{ -1, -1 }; }
/* GfxCanvas::onImage * Returns true if the given coordinates are 'on' top of the image *******************************************************************/ bool GfxCanvas::onImage(int x, int y) { // Don't disable in editing mode; it can be quite useful // to have a live preview of how a graphic will tile. if (view_type == GFXVIEW_TILED && editing_mode == 0) return false; // No need to duplicate the imageCoords code. return imageCoords(x, y) != POINT_OUTSIDE; }
// ----------------------------------------------------------------------------- // Finds the pixel under the cursor, and picks its colour. // ----------------------------------------------------------------------------- void GfxCanvas::pickColour(int x, int y) { // Get the pixel auto coord = imageCoords(x, y); // Pick its colour paint_colour_ = image_.pixelAt(coord.x, coord.y, &palette_); // Announce it triumphantly to the world wxNotifyEvent e(wxEVT_GFXCANVAS_COLOUR_PICKED, GetId()); e.SetEventObject(this); GetEventHandler()->ProcessEvent(e); }