Skip to content

Commit 19f4727

Browse files
author
Gary Huang
committed
Add rounded rectangle support
1 parent e8a6387 commit 19f4727

File tree

7 files changed

+154
-39
lines changed

7 files changed

+154
-39
lines changed

.gitignore

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
ompiled Object files
1+
# Compiled Object files
22
*.slo
33
*.lo
44
*.o
@@ -28,14 +28,16 @@ ompiled Object files
2828
*.app
2929

3030
# QMake
31+
.qmake.stash
3132
src/*/Makefile
3233
src/Makefile
33-
.qmake.stash
34+
src/object_script.Processing
35+
moc_*
36+
*.moc
3437

3538
# Others
3639
*.log
3740
*.swp
3841
.nfs*
39-
40-
main.exe
41-
main
42+
include/*.h
43+
Makefile

src/GuiEngine/canvas.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ void Canvas::rect(float a, float b, float c, float d)
148148
draw_queue.push_back(new PRect(a, b, c, d));
149149
}
150150

151+
void Canvas::rect(float a, float b, float c, float d, float r)
152+
{
153+
draw_queue.push_back(new PRoundedRect(a, b, c, d, r));
154+
}
155+
156+
void Canvas::rect(float a, float b, float c, float d, float tl, float tr, float br, float bl)
157+
{
158+
draw_queue.push_back(new PRoundedRectC4(a, b, c, d, tl, tr, br, bl));
159+
}
160+
151161
void Canvas::triangle(float x1, float y1, float x2, float y2, float x3, float y3)
152162
{
153163
draw_queue.push_back(new PTriangle(x1, y1, x2, y2, x3, y3));

src/GuiEngine/canvas.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ class Canvas
4242
void point(float x, float y);
4343
void quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);
4444
void rect(float a, float b, float c, float d);
45+
void rect(float a, float b, float c, float d, float r);
46+
void rect(float a, float b, float c, float d, float tl, float tr, float br, float bl);
4547
void triangle(float x1, float y1, float x2, float y2, float x3, float y3);
4648

4749
void background(int rgb);

src/GuiEngine/pelement.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ class PElement
2222
Point,
2323
Quad,
2424
Rect,
25+
RoundedRect,
26+
RoundedRectC4,
2527
Triangle,
2628
Background,
2729
Fill,
@@ -173,6 +175,45 @@ class PRect : public PElement
173175
float m_a, m_b, m_c, m_d;
174176
};
175177

178+
class PRoundedRect : public PElement
179+
{
180+
public:
181+
PRoundedRect(float a, float b, float c, float d, float r)
182+
: m_a(a), m_b(b), m_c(c), m_d(d), m_r(r) {}
183+
184+
PElement::PElementType type() const { return PElement::RoundedRect; }
185+
PElement * clone() const { return new PRoundedRect(m_a, m_b, m_c, m_d, m_r); }
186+
float a() const { return m_a; }
187+
float b() const { return m_b; }
188+
float c() const { return m_c; }
189+
float d() const { return m_d; }
190+
float r() const { return m_r; }
191+
192+
private:
193+
float m_a, m_b, m_c, m_d, m_r;
194+
};
195+
196+
class PRoundedRectC4 : public PElement
197+
{
198+
public:
199+
PRoundedRectC4(float a, float b, float c, float d, float tl, float tr, float br, float bl)
200+
: m_a(a), m_b(b), m_c(c), m_d(d), m_tl(tl), m_tr(tr), m_br(br), m_bl(bl) {}
201+
202+
PElement::PElementType type() const { return PElement::RoundedRectC4; }
203+
PElement * clone() const { return new PRoundedRectC4(m_a, m_b, m_c, m_d, m_tl, m_tr, m_br, m_bl); }
204+
float a() const { return m_a; }
205+
float b() const { return m_b; }
206+
float c() const { return m_c; }
207+
float d() const { return m_d; }
208+
float tl() const { return m_tl; }
209+
float tr() const { return m_tr; }
210+
float br() const { return m_br; }
211+
float bl() const { return m_bl; }
212+
213+
private:
214+
float m_a, m_b, m_c, m_d, m_tl, m_tr, m_br, m_bl;
215+
};
216+
176217
class PTriangle : public PElement
177218
{
178219
public:

src/Processing/processing.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ class ProcessingPrivate
6868
inline void point(float x, float y) { canvas->point(x, y); }
6969
inline void quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4) { canvas->quad(x1, y1, x2, y2, x3, y3, x4, y4); }
7070
inline void rect(float a, float b, float c, float d) { canvas->rect(a, b, c, d); }
71+
inline void rect(float a, float b, float c, float d, float r) { canvas->rect(a, b, c, d, r); }
72+
inline void rect(float a, float b, float c, float d, float tl, float tr, float br, float bl) { canvas->rect(a, b, c, d, tl, tr, br, bl); }
7173
inline void triangle(float x1, float y1, float x2, float y2, float x3, float y3) { canvas->triangle(x1, y1, x2, y2, x3, y3); }
7274

7375
inline void background(int rgb) { canvas->background(rgb); }
@@ -236,6 +238,16 @@ void rect(float a, float b, float c, float d)
236238
ProcessingPrivate::getInstance()->rect(a, b, c, d);
237239
}
238240

241+
void rect(float a, float b, float c, float d, float r)
242+
{
243+
ProcessingPrivate::getInstance()->rect(a, b, c, d, r);
244+
}
245+
246+
void rect(float a, float b, float c, float d, float tl, float tr, float br, float bl)
247+
{
248+
ProcessingPrivate::getInstance()->rect(a, b, c, d, tl, tr, br, bl);
249+
}
250+
239251
void triangle(float x1, float y1, float x2, float y2, float x3, float y3)
240252
{
241253
ProcessingPrivate::getInstance()->triangle(x1, y1, x2, y2, x3, y3);
@@ -347,7 +359,7 @@ static int hex_to_int(char c)
347359
color::color(const char *hex)
348360
{
349361
if (hex[0] != '#')
350-
throw "bad hexadecimal notation: no '#'";
362+
throw "bad hex notation: no '#'";
351363
const char *tmp = hex + 1;
352364
int d[8];
353365
int i;
@@ -357,11 +369,11 @@ color::color(const char *hex)
357369
if (c == '\0')
358370
break;
359371
if (!is_digit(c))
360-
throw "bad hexadecimal notation: not digit";
372+
throw "bad hex notation: not digit";
361373
d[i] = hex_to_int(c);
362374
}
363375
if (tmp[i] != '\0')
364-
throw "bad hexadecimal notation";
376+
throw "bad hex notation";
365377

366378
int r, g, b, a = 0xFF;
367379
switch (i)
@@ -383,7 +395,7 @@ color::color(const char *hex)
383395
a = (d[6] << 4) | d[7];
384396
break;
385397
default:
386-
throw "bad hexadecimal notation: invalid format";
398+
throw "bad hex notation: only support format: #RGB, #RRGGBB, #RRGGBBAA";
387399
}
388400
store(r, g, b, a);
389401
}

src/Processing/processing.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ void line(float x1, float y1, float x2, float y2);
7272
void point(float x, float y);
7373
void quad(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4);
7474
void rect(float a, float b, float c, float d);
75+
void rect(float a, float b, float c, float d, float r);
76+
void rect(float a, float b, float c, float d, float tl, float tr, float br, float bl);
7577
void triangle(float x1, float y1, float x2, float y2, float x3, float y3);
7678

7779
// Color

src/QtEngine/qtcanvas.cpp

Lines changed: 76 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,42 @@ void QtCanvas::animate()
4343
update();
4444
}
4545

46+
static QRectF getRect(DrawMode mode, float a, float b, float c, float d)
47+
{
48+
QRectF bbox;
49+
switch (mode)
50+
{
51+
case RADIUS:
52+
{
53+
float x = a - c;
54+
float y = b - d;
55+
bbox.setRect(x, y, 2 * c, 2 * d);
56+
break;
57+
}
58+
case CENTER:
59+
{
60+
float x = a - 0.5 * c;
61+
float y = b - 0.5 * d;
62+
bbox.setRect(x, y, c, d);
63+
break;
64+
}
65+
case CORNER:
66+
{
67+
bbox.setRect(a, b, c, d);
68+
break;
69+
}
70+
case CORNERS:
71+
{
72+
QPointF tl(a, b);
73+
QPointF br(c, d);
74+
bbox.setTopLeft(tl);
75+
bbox.setBottomRight(br);
76+
break;
77+
}
78+
}
79+
return bbox;
80+
}
81+
4682
void QtCanvas::paint(QPainter *painter, QPaintEvent *event)
4783
{
4884
for (std::list<PElement *>::const_iterator it = draw_queue.cbegin();
@@ -158,36 +194,46 @@ void QtCanvas::paint(QPainter *painter, QPaintEvent *event)
158194
case PElement::Rect:
159195
{
160196
PRect *r = (PRect *) e;
161-
switch (style.rect_mode)
162-
{
163-
case RADIUS:
164-
{
165-
float x = r->a() - r->c();
166-
float y = r->b() - r->d();
167-
painter->drawRect(x, y, 2 * r->c(), 2 * r->d());
168-
break;
169-
}
170-
case CENTER:
171-
{
172-
float x = r->a() - 0.5 * r->c();
173-
float y = r->b() - 0.5 * r->d();
174-
painter->drawRect(x, y, r->c(), r->d());
175-
break;
176-
}
177-
case CORNER:
178-
{
179-
painter->drawRect(r->a(), r->b(), r->c(), r->d());
180-
break;
181-
}
182-
case CORNERS:
183-
{
184-
QPointF tl(r->a(), r->b());
185-
QPointF br(r->c(), r->d());
186-
QRectF bbox(tl, br);
187-
painter->drawRect(bbox);
188-
break;
189-
}
190-
}
197+
QRectF bbox = getRect(style.rect_mode, r->a(), r->b(), r->c(), r->d());
198+
painter->drawRect(bbox);
199+
break;
200+
}
201+
case PElement::RoundedRect:
202+
{
203+
PRoundedRect *r = (PRoundedRect *)e;
204+
QRectF bbox = getRect(style.rect_mode, r->a(), r->b(), r->c(), r->d());
205+
painter->drawRoundedRect(bbox, r->r(), r->r());
206+
break;
207+
}
208+
case PElement::RoundedRectC4:
209+
{
210+
PRoundedRectC4 *r = (PRoundedRectC4 *)e;
211+
QRectF bbox = getRect(style.rect_mode, r->a(), r->b(), r->c(), r->d());
212+
QPainterPath path;
213+
float x = bbox.x();
214+
float y = bbox.y();
215+
float w = bbox.width();
216+
float h = bbox.height();
217+
float hw = 0.5 * w;
218+
float hh = 0.5 * h;
219+
path.setFillRule(Qt::WindingFill);
220+
path.addRoundedRect(x, y , hw, hh, r->tl(), r->tl());
221+
path.addRoundedRect(x + hw, y , hw, hh, r->tr(), r->tr());
222+
path.addRoundedRect(x + hw, y + hw, hw, hh, r->br(), r->br());
223+
path.addRoundedRect(x, y + hw, hw, hh, r->bl(), r->bl());
224+
path.addRect(x + hw - r->tl(), y, r->tl(), r->tl());
225+
path.addRect(x, y + hh - r->tl(), r->tl(), r->tl());
226+
path.addRect(x + hw - r->tl(), y + hh - r->tl(), r->tl(), r->tl());
227+
path.addRect(x + hw, y, r->tr(), r->tr());
228+
path.addRect(x + hw, y + hh - r->tr(), r->tr(), r->tr());
229+
path.addRect(x + w - r->tr(), y + hh - r->tr(), r->tr(), r->tr());
230+
path.addRect(x + hw, y + hh, r->br(), r->br());
231+
path.addRect(x + h - r->br(), y + hh, r->br(), r->br());
232+
path.addRect(x + hw, y + h - r->br(), r->br(), r->br());
233+
path.addRect(x, y + hh, r->bl(), r->bl());
234+
path.addRect(x + hw - r->bl(), y + hh, r->bl(), r->bl());
235+
path.addRect(x + hw - r->bl(), y + h - r->bl(), r->bl(), r->bl());
236+
painter->drawPath(path.simplified());
191237
break;
192238
}
193239
case PElement::Triangle:

0 commit comments

Comments
 (0)