diff --git a/checkmark.cc b/checkmark.cc index 288dd5b..beac1d8 100644 --- a/checkmark.cc +++ b/checkmark.cc @@ -9,13 +9,13 @@ void Checkmark::Serialize(pdfsketchproto::Graphic* out) const { out->set_type(pdfsketchproto::Graphic::CHECKMARK); } -void Checkmark::Place(int page, const Point& location, bool constrain) { +void Checkmark::Place(int page, const Point& location) { page_ = page; frame_.size_ = Size(9.0, 9.0); - PlaceUpdate(location, constrain); + PlaceUpdate(location); } -void Checkmark::PlaceUpdate(const Point& location, bool constrain) { +void Checkmark::PlaceUpdate(const Point& location) { SetNeedsDisplay(false); frame_.SetCenter(location); SetNeedsDisplay(false); diff --git a/checkmark.h b/checkmark.h index ed37670..90c1b44 100644 --- a/checkmark.h +++ b/checkmark.h @@ -14,8 +14,8 @@ class Checkmark : public Graphic { : Graphic(msg) {} ~Checkmark() {} virtual void Serialize(pdfsketchproto::Graphic* out) const; - virtual void Place(int page, const Point& location, bool constrain); - virtual void PlaceUpdate(const Point& location, bool constrain); + virtual void Place(int page, const Point& location); + virtual void PlaceUpdate(const Point& location); virtual bool PlaceComplete() { return false; } virtual void Draw(cairo_t* cr, bool selected); }; diff --git a/document_view.cc b/document_view.cc index 873875e..844ca45 100644 --- a/document_view.cc +++ b/document_view.cc @@ -380,7 +380,7 @@ View* DocumentView::OnMouseDown(const MouseInputEvent& event) { if ((knob = gr->PointInKnob(pos)) != kKnobNone) { resize_graphic_original_frame_ = gr->Frame(); resizing_graphic_ = gr; - gr->BeginResize(pos, knob, false); + gr->BeginResize(pos, knob); return this; } } @@ -438,7 +438,7 @@ View* DocumentView::OnMouseDown(const MouseInputEvent& event) { int page = PageForPoint(event.position()); Point page_pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5), page); - gr->Place(page, page_pos, false); + gr->Place(page, page_pos); placing_graphic_ = gr.get(); return this; } @@ -447,14 +447,14 @@ void DocumentView::OnMouseDrag(const MouseInputEvent& event) { if (resizing_graphic_) { Point pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5), resizing_graphic_->Page()); - resizing_graphic_->UpdateResize(pos, false); + resizing_graphic_->UpdateResize(pos); return; } if (placing_graphic_) { Point page_pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5), placing_graphic_->Page()); - placing_graphic_->PlaceUpdate(page_pos, false); + placing_graphic_->PlaceUpdate(page_pos); return; } diff --git a/graphic.cc b/graphic.cc index 98de251..89ce782 100644 --- a/graphic.cc +++ b/graphic.cc @@ -106,32 +106,34 @@ void Graphic::SetNeedsDisplay(bool withKnobs) const { DrawingFrame()); } -void Graphic::Place(int page, const Point& location, bool constrain) { +void Graphic::Place(int page, const Point& location) { page_ = page; frame_ = Rect(location); resizing_knob_ = kKnobLowerRight; } -void Graphic::PlaceUpdate(const Point& location, bool constrain) { - UpdateResize(location, constrain); +void Graphic::PlaceUpdate(const Point& location) { + UpdateResize(location); } bool Graphic::PlaceComplete() { resizing_knob_ = kKnobNone; return frame_.size_ == Size(); } -void Graphic::BeginResize(const Point& location, int knob, bool constrain) { +void Graphic::BeginResize(const Point& location, int knob) { resizing_knob_ = knob; - UpdateResize(location, constrain); + UpdateResize(location); } -void Graphic::UpdateResize(const Point& location, bool constrain) { +void Graphic::UpdateResize(const Point& location) { // Structure from https://github.com/adlr/formulatepro/blob/master/FPGraphic.m - //double shift_slope = 0.0; - // if (frame_.size_.width_ > 0.0 && - // frame_.size_.height_ > 0.0) - // shift_slope = frame_.size_.height_ / frame_.size_.width_; - // else - // shift_slope = natural_size_.height_ / natural_size_.width_; + bool constrain = KnobIsCorner(resizing_knob_); + + double shift_slope = 0.0; + if (frame_.size_.width_ > 0.0 && + frame_.size_.height_ > 0.0) + shift_slope = frame_.size_.height_ / frame_.size_.width_; + else + shift_slope = natural_size_.height_ / natural_size_.width_; if (delegate_) { delegate_->SetNeedsDisplayInPageRect(Page(), DrawingFrameWithKnobs()); @@ -178,25 +180,18 @@ void Graphic::UpdateResize(const Point& location, bool constrain) { } } - if (constrain) { - /* + if (constrain && fabsf(shift_slope) > 0.00001) { + float new_width = frame_.size_.height_ / shift_slope; switch (resizing_knob_) { case kKnobUpperRight: case kKnobLowerRight: - didFlip = FPRectSetRightAbs(&_bounds, - _bounds.origin.x + - (_bounds.size.height / - shiftSlope)); + frame_.size_.width_ = new_width; break; case kKnobLowerLeft: case kKnobUpperLeft: - didFlip = FPRectSetLeftAbs(&_bounds, - _bounds.origin.x + - _bounds.size.width - - (_bounds.size.height / - shiftSlope)); + frame_.SetLeftAbs(frame_.Right() - new_width); break; - }*/ + } } if (delegate_) { delegate_->SetNeedsDisplayInPageRect(Page(), DrawingFrameWithKnobs()); diff --git a/graphic.h b/graphic.h index 3fb22fa..4d6d592 100644 --- a/graphic.h +++ b/graphic.h @@ -45,6 +45,17 @@ const int kKnobLowerMiddle = 1 << 6; const int kKnobLowerRight = 1 << 7; const int kAllKnobs = 0xff; +inline bool KnobIsCorner(int knob) { + switch (knob) { + case kKnobUpperLeft: // fallthough + case kKnobUpperRight: // fallthough + case kKnobLowerLeft: // fallthough + case kKnobLowerRight: + return true; + } + return false; +} + class GraphicDelegate { public: virtual void SetNeedsDisplayInPageRect(int page, const Rect& rect) = 0; @@ -89,12 +100,9 @@ class Graphic { delegate_ = delegate; } - // For these, constrain means shift key is held (so aspect ratio should - // be constrained) - // Placement - virtual void Place(int page, const Point& location, bool constrain); - virtual void PlaceUpdate(const Point& location, bool constrain); + virtual void Place(int page, const Point& location); + virtual void PlaceUpdate(const Point& location); // Returns true if this graphic should be deleted. // By default, graphics with 0 size are deleted. virtual bool PlaceComplete(); @@ -111,8 +119,8 @@ class Graphic { // Return true if handled virtual bool OnPaste(const std::string& str) { return false; } - virtual void BeginResize(const Point& location, int knob, bool constrain); - virtual void UpdateResize(const Point& location, bool constrain); + virtual void BeginResize(const Point& location, int knob); + virtual void UpdateResize(const Point& location); virtual void EndResize(); virtual void Draw(cairo_t* cr, bool selected) {} diff --git a/squiggle.cc b/squiggle.cc index 1520325..1b851e6 100644 --- a/squiggle.cc +++ b/squiggle.cc @@ -27,14 +27,14 @@ void Squiggle::Serialize(pdfsketchproto::Graphic* out) const { } } -void Squiggle::Place(int page, const Point& location, bool constrain) { +void Squiggle::Place(int page, const Point& location) { page_ = page; points_.push_back(location); frame_ = Rect(location); original_origin_ = location; } -void Squiggle::PlaceUpdate(const Point& location, bool constrain) { +void Squiggle::PlaceUpdate(const Point& location) { if (points_.back() == location) return; points_.push_back(location); diff --git a/squiggle.h b/squiggle.h index b6b4c7c..165fd6b 100644 --- a/squiggle.h +++ b/squiggle.h @@ -12,8 +12,8 @@ class Squiggle : public Graphic { Squiggle() {} explicit Squiggle(const pdfsketchproto::Graphic& msg); virtual void Serialize(pdfsketchproto::Graphic* out) const; - virtual void Place(int page, const Point& location, bool constrain); - virtual void PlaceUpdate(const Point& location, bool constrain); + virtual void Place(int page, const Point& location); + virtual void PlaceUpdate(const Point& location); virtual bool PlaceComplete(); virtual void Draw(cairo_t* cr, bool selected); diff --git a/text_area.cc b/text_area.cc index cbe2979..779b92f 100644 --- a/text_area.cc +++ b/text_area.cc @@ -18,11 +18,11 @@ void TextArea::Serialize(pdfsketchproto::Graphic* out) const { *msg->mutable_text() = text_; } -void TextArea::Place(int page, const Point& location, bool constrain) { +void TextArea::Place(int page, const Point& location) { page_ = page; - PlaceUpdate(location, constrain); + PlaceUpdate(location); } -void TextArea::PlaceUpdate(const Point& location, bool constrain) { +void TextArea::PlaceUpdate(const Point& location) { frame_ = Rect(location, Size(150.0, 72.0)); } bool TextArea::PlaceComplete() { diff --git a/text_area.h b/text_area.h index 60808a0..d4b90b3 100644 --- a/text_area.h +++ b/text_area.h @@ -51,8 +51,8 @@ class TextArea : public Graphic { frame_.size_.width_ = 150.0; } virtual void Serialize(pdfsketchproto::Graphic* out) const; - virtual void Place(int page, const Point& location, bool constrain); - virtual void PlaceUpdate(const Point& location, bool constrain); + virtual void Place(int page, const Point& location); + virtual void PlaceUpdate(const Point& location); virtual bool PlaceComplete(); virtual bool Editable() const { return true; } @@ -68,10 +68,6 @@ class TextArea : public Graphic { text_ = str; } - // virtual void BeginResize(const Point& location, int knob, bool constrain); - // virtual void UpdateResize(const Point& location, bool constrain); - // virtual void EndResize(); - virtual void Draw(cairo_t* cr, bool selected); protected: