Skip to content

Commit 6b81659

Browse files
committed
Graphics: Constrain aspect ratio when dragging corners
Has a bug that now placing a new graphic will keep it w/ a 1:1 ratio while placing. This should be fixed in a future patch.
1 parent afdd697 commit 6b81659

9 files changed

+52
-53
lines changed

Diff for: checkmark.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ void Checkmark::Serialize(pdfsketchproto::Graphic* out) const {
99
out->set_type(pdfsketchproto::Graphic::CHECKMARK);
1010
}
1111

12-
void Checkmark::Place(int page, const Point& location, bool constrain) {
12+
void Checkmark::Place(int page, const Point& location) {
1313
page_ = page;
1414
frame_.size_ = Size(9.0, 9.0);
15-
PlaceUpdate(location, constrain);
15+
PlaceUpdate(location);
1616
}
1717

18-
void Checkmark::PlaceUpdate(const Point& location, bool constrain) {
18+
void Checkmark::PlaceUpdate(const Point& location) {
1919
SetNeedsDisplay(false);
2020
frame_.SetCenter(location);
2121
SetNeedsDisplay(false);

Diff for: checkmark.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ class Checkmark : public Graphic {
1414
: Graphic(msg) {}
1515
~Checkmark() {}
1616
virtual void Serialize(pdfsketchproto::Graphic* out) const;
17-
virtual void Place(int page, const Point& location, bool constrain);
18-
virtual void PlaceUpdate(const Point& location, bool constrain);
17+
virtual void Place(int page, const Point& location);
18+
virtual void PlaceUpdate(const Point& location);
1919
virtual bool PlaceComplete() { return false; }
2020
virtual void Draw(cairo_t* cr, bool selected);
2121
};

Diff for: document_view.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ View* DocumentView::OnMouseDown(const MouseInputEvent& event) {
380380
if ((knob = gr->PointInKnob(pos)) != kKnobNone) {
381381
resize_graphic_original_frame_ = gr->Frame();
382382
resizing_graphic_ = gr;
383-
gr->BeginResize(pos, knob, false);
383+
gr->BeginResize(pos, knob);
384384
return this;
385385
}
386386
}
@@ -438,7 +438,7 @@ View* DocumentView::OnMouseDown(const MouseInputEvent& event) {
438438
int page = PageForPoint(event.position());
439439
Point page_pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
440440
page);
441-
gr->Place(page, page_pos, false);
441+
gr->Place(page, page_pos);
442442
placing_graphic_ = gr.get();
443443
return this;
444444
}
@@ -447,14 +447,14 @@ void DocumentView::OnMouseDrag(const MouseInputEvent& event) {
447447
if (resizing_graphic_) {
448448
Point pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
449449
resizing_graphic_->Page());
450-
resizing_graphic_->UpdateResize(pos, false);
450+
resizing_graphic_->UpdateResize(pos);
451451
return;
452452
}
453453

454454
if (placing_graphic_) {
455455
Point page_pos = ConvertPointToPage(event.position().TranslatedBy(0.5, 0.5),
456456
placing_graphic_->Page());
457-
placing_graphic_->PlaceUpdate(page_pos, false);
457+
placing_graphic_->PlaceUpdate(page_pos);
458458
return;
459459
}
460460

Diff for: graphic.cc

+19-24
Original file line numberDiff line numberDiff line change
@@ -106,32 +106,34 @@ void Graphic::SetNeedsDisplay(bool withKnobs) const {
106106
DrawingFrame());
107107
}
108108

109-
void Graphic::Place(int page, const Point& location, bool constrain) {
109+
void Graphic::Place(int page, const Point& location) {
110110
page_ = page;
111111
frame_ = Rect(location);
112112
resizing_knob_ = kKnobLowerRight;
113113
}
114-
void Graphic::PlaceUpdate(const Point& location, bool constrain) {
115-
UpdateResize(location, constrain);
114+
void Graphic::PlaceUpdate(const Point& location) {
115+
UpdateResize(location);
116116
}
117117
bool Graphic::PlaceComplete() {
118118
resizing_knob_ = kKnobNone;
119119
return frame_.size_ == Size();
120120
}
121121

122-
void Graphic::BeginResize(const Point& location, int knob, bool constrain) {
122+
void Graphic::BeginResize(const Point& location, int knob) {
123123
resizing_knob_ = knob;
124-
UpdateResize(location, constrain);
124+
UpdateResize(location);
125125
}
126-
void Graphic::UpdateResize(const Point& location, bool constrain) {
126+
void Graphic::UpdateResize(const Point& location) {
127127
// Structure from https://github.com/adlr/formulatepro/blob/master/FPGraphic.m
128128

129-
//double shift_slope = 0.0;
130-
// if (frame_.size_.width_ > 0.0 &&
131-
// frame_.size_.height_ > 0.0)
132-
// shift_slope = frame_.size_.height_ / frame_.size_.width_;
133-
// else
134-
// shift_slope = natural_size_.height_ / natural_size_.width_;
129+
bool constrain = KnobIsCorner(resizing_knob_);
130+
131+
double shift_slope = 0.0;
132+
if (frame_.size_.width_ > 0.0 &&
133+
frame_.size_.height_ > 0.0)
134+
shift_slope = frame_.size_.height_ / frame_.size_.width_;
135+
else
136+
shift_slope = natural_size_.height_ / natural_size_.width_;
135137

136138
if (delegate_) {
137139
delegate_->SetNeedsDisplayInPageRect(Page(), DrawingFrameWithKnobs());
@@ -178,25 +180,18 @@ void Graphic::UpdateResize(const Point& location, bool constrain) {
178180
}
179181
}
180182

181-
if (constrain) {
182-
/*
183+
if (constrain && fabsf(shift_slope) > 0.00001) {
184+
float new_width = frame_.size_.height_ / shift_slope;
183185
switch (resizing_knob_) {
184186
case kKnobUpperRight:
185187
case kKnobLowerRight:
186-
didFlip = FPRectSetRightAbs(&_bounds,
187-
_bounds.origin.x +
188-
(_bounds.size.height /
189-
shiftSlope));
188+
frame_.size_.width_ = new_width;
190189
break;
191190
case kKnobLowerLeft:
192191
case kKnobUpperLeft:
193-
didFlip = FPRectSetLeftAbs(&_bounds,
194-
_bounds.origin.x +
195-
_bounds.size.width -
196-
(_bounds.size.height /
197-
shiftSlope));
192+
frame_.SetLeftAbs(frame_.Right() - new_width);
198193
break;
199-
}*/
194+
}
200195
}
201196
if (delegate_) {
202197
delegate_->SetNeedsDisplayInPageRect(Page(), DrawingFrameWithKnobs());

Diff for: graphic.h

+15-7
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ const int kKnobLowerMiddle = 1 << 6;
4545
const int kKnobLowerRight = 1 << 7;
4646
const int kAllKnobs = 0xff;
4747

48+
inline bool KnobIsCorner(int knob) {
49+
switch (knob) {
50+
case kKnobUpperLeft: // fallthough
51+
case kKnobUpperRight: // fallthough
52+
case kKnobLowerLeft: // fallthough
53+
case kKnobLowerRight:
54+
return true;
55+
}
56+
return false;
57+
}
58+
4859
class GraphicDelegate {
4960
public:
5061
virtual void SetNeedsDisplayInPageRect(int page, const Rect& rect) = 0;
@@ -89,12 +100,9 @@ class Graphic {
89100
delegate_ = delegate;
90101
}
91102

92-
// For these, constrain means shift key is held (so aspect ratio should
93-
// be constrained)
94-
95103
// Placement
96-
virtual void Place(int page, const Point& location, bool constrain);
97-
virtual void PlaceUpdate(const Point& location, bool constrain);
104+
virtual void Place(int page, const Point& location);
105+
virtual void PlaceUpdate(const Point& location);
98106
// Returns true if this graphic should be deleted.
99107
// By default, graphics with 0 size are deleted.
100108
virtual bool PlaceComplete();
@@ -111,8 +119,8 @@ class Graphic {
111119
// Return true if handled
112120
virtual bool OnPaste(const std::string& str) { return false; }
113121

114-
virtual void BeginResize(const Point& location, int knob, bool constrain);
115-
virtual void UpdateResize(const Point& location, bool constrain);
122+
virtual void BeginResize(const Point& location, int knob);
123+
virtual void UpdateResize(const Point& location);
116124
virtual void EndResize();
117125

118126
virtual void Draw(cairo_t* cr, bool selected) {}

Diff for: squiggle.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ void Squiggle::Serialize(pdfsketchproto::Graphic* out) const {
2727
}
2828
}
2929

30-
void Squiggle::Place(int page, const Point& location, bool constrain) {
30+
void Squiggle::Place(int page, const Point& location) {
3131
page_ = page;
3232
points_.push_back(location);
3333
frame_ = Rect(location);
3434
original_origin_ = location;
3535
}
3636

37-
void Squiggle::PlaceUpdate(const Point& location, bool constrain) {
37+
void Squiggle::PlaceUpdate(const Point& location) {
3838
if (points_.back() == location)
3939
return;
4040
points_.push_back(location);

Diff for: squiggle.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ class Squiggle : public Graphic {
1212
Squiggle() {}
1313
explicit Squiggle(const pdfsketchproto::Graphic& msg);
1414
virtual void Serialize(pdfsketchproto::Graphic* out) const;
15-
virtual void Place(int page, const Point& location, bool constrain);
16-
virtual void PlaceUpdate(const Point& location, bool constrain);
15+
virtual void Place(int page, const Point& location);
16+
virtual void PlaceUpdate(const Point& location);
1717
virtual bool PlaceComplete();
1818
virtual void Draw(cairo_t* cr, bool selected);
1919

Diff for: text_area.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ void TextArea::Serialize(pdfsketchproto::Graphic* out) const {
1818
*msg->mutable_text() = text_;
1919
}
2020

21-
void TextArea::Place(int page, const Point& location, bool constrain) {
21+
void TextArea::Place(int page, const Point& location) {
2222
page_ = page;
23-
PlaceUpdate(location, constrain);
23+
PlaceUpdate(location);
2424
}
25-
void TextArea::PlaceUpdate(const Point& location, bool constrain) {
25+
void TextArea::PlaceUpdate(const Point& location) {
2626
frame_ = Rect(location, Size(150.0, 72.0));
2727
}
2828
bool TextArea::PlaceComplete() {

Diff for: text_area.h

+2-6
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ class TextArea : public Graphic {
5151
frame_.size_.width_ = 150.0;
5252
}
5353
virtual void Serialize(pdfsketchproto::Graphic* out) const;
54-
virtual void Place(int page, const Point& location, bool constrain);
55-
virtual void PlaceUpdate(const Point& location, bool constrain);
54+
virtual void Place(int page, const Point& location);
55+
virtual void PlaceUpdate(const Point& location);
5656
virtual bool PlaceComplete();
5757

5858
virtual bool Editable() const { return true; }
@@ -68,10 +68,6 @@ class TextArea : public Graphic {
6868
text_ = str;
6969
}
7070

71-
// virtual void BeginResize(const Point& location, int knob, bool constrain);
72-
// virtual void UpdateResize(const Point& location, bool constrain);
73-
// virtual void EndResize();
74-
7571
virtual void Draw(cairo_t* cr, bool selected);
7672

7773
protected:

0 commit comments

Comments
 (0)