From: Thomas Geymayer Date: Fri, 31 May 2013 17:16:04 +0000 (+0200) Subject: Canvas: add chainable helpers to Path for adding segments. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=2c879095d9ef09561adc6284989e02ce75ed4307;p=simgear.git Canvas: add chainable helpers to Path for adding segments. --- diff --git a/simgear/canvas/elements/CMakeLists.txt b/simgear/canvas/elements/CMakeLists.txt index 6e5c2e93..bd21c13d 100644 --- a/simgear/canvas/elements/CMakeLists.txt +++ b/simgear/canvas/elements/CMakeLists.txt @@ -6,7 +6,11 @@ set(HEADERS CanvasImage.hxx CanvasMap.hxx CanvasPath.hxx - CanvasText.hxx + CanvasText.hxx +) + +set(DETAIL_HEADERS + detail/add_segment_variadic.hxx ) set(SOURCES @@ -18,4 +22,5 @@ set(SOURCES CanvasText.cxx ) -simgear_scene_component(canvas-elements canvas/elements "${SOURCES}" "${HEADERS}") \ No newline at end of file +simgear_scene_component(canvas-elements canvas/elements "${SOURCES}" "${HEADERS}") +simgear_component(canvas-elements/detail canvas/elements/detail "" "${DETAIL_HEADERS}") \ No newline at end of file diff --git a/simgear/canvas/elements/CanvasPath.cxx b/simgear/canvas/elements/CanvasPath.cxx index a62ab080..5e9efa08 100644 --- a/simgear/canvas/elements/CanvasPath.cxx +++ b/simgear/canvas/elements/CanvasPath.cxx @@ -524,6 +524,60 @@ namespace canvas return _path->getTransformedBounds(m); } + //---------------------------------------------------------------------------- + Path& Path::moveTo(float x_abs, float y_abs) + { + return addSegment(VG_MOVE_TO_ABS, x_abs, y_abs); + } + + //---------------------------------------------------------------------------- + Path& Path::move(float x_rel, float y_rel) + { + return addSegment(VG_MOVE_TO_REL, x_rel, y_rel); + } + + //---------------------------------------------------------------------------- + Path& Path::lineTo(float x_abs, float y_abs) + { + return addSegment(VG_LINE_TO_ABS, x_abs, y_abs); + } + + //---------------------------------------------------------------------------- + Path& Path::line(float x_rel, float y_rel) + { + return addSegment(VG_LINE_TO_REL, x_rel, y_rel); + } + + //---------------------------------------------------------------------------- + Path& Path::horizTo(float x_abs) + { + return addSegment(VG_HLINE_TO_ABS, x_abs); + } + + //---------------------------------------------------------------------------- + Path& Path::horiz(float x_rel) + { + return addSegment(VG_HLINE_TO_REL, x_rel); + } + + //---------------------------------------------------------------------------- + Path& Path::vertTo(float y_abs) + { + return addSegment(VG_VLINE_TO_ABS, y_abs); + } + + //---------------------------------------------------------------------------- + Path& Path::vert(float y_rel) + { + return addSegment(VG_VLINE_TO_REL, y_rel); + } + + //---------------------------------------------------------------------------- + Path& Path::close() + { + return addSegment(VG_CLOSE_PATH); + } + //---------------------------------------------------------------------------- void Path::childRemoved(SGPropertyNode* child) { diff --git a/simgear/canvas/elements/CanvasPath.hxx b/simgear/canvas/elements/CanvasPath.hxx index 706f8cd7..de7c6524 100644 --- a/simgear/canvas/elements/CanvasPath.hxx +++ b/simgear/canvas/elements/CanvasPath.hxx @@ -20,6 +20,7 @@ #define CANVAS_PATH_HXX_ #include "CanvasElement.hxx" +#include namespace simgear { @@ -39,6 +40,30 @@ namespace canvas virtual osg::BoundingBox getTransformedBounds(const osg::Matrix& m) const; +#define BOOST_PP_ITERATION_LIMITS (0, 6) +#define BOOST_PP_FILENAME_1 \ + +#include BOOST_PP_ITERATE() + + /** Move path cursor */ + Path& moveTo(float x_abs, float y_abs); + Path& move(float x_rel, float y_rel); + + /** Add a line */ + Path& lineTo(float x_abs, float y_abs); + Path& line(float x_rel, float y_rel); + + /** Add a horizontal line */ + Path& horizTo(float x_abs); + Path& horiz(float x_rel); + + /** Add a vertical line */ + Path& vertTo(float y_abs); + Path& vert(float y_rel); + + /** Close the path (implicit lineTo to first point of path) */ + Path& close(); + protected: enum PathAttributes diff --git a/simgear/canvas/elements/detail/add_segment_variadic.hxx b/simgear/canvas/elements/detail/add_segment_variadic.hxx new file mode 100644 index 00000000..cb4f434e --- /dev/null +++ b/simgear/canvas/elements/detail/add_segment_variadic.hxx @@ -0,0 +1,21 @@ +#ifndef CANVAS_PATH_HXX_ +# error Canvas - do not include this file! +#endif + +#define n BOOST_PP_ITERATION() + +Path& addSegment( uint8_t cmd + BOOST_PP_COMMA_IF(n) + BOOST_PP_ENUM_PARAMS(n, float coord) ) +{ + _node->addChild("cmd")->setIntValue(cmd); + +#define SG_CANVAS_PATH_SET_COORD(z, n, dummy)\ + _node->addChild("coord")->setFloatValue(coord##n); + + BOOST_PP_REPEAT(n, SG_CANVAS_PATH_SET_COORD, 0) +#undef SG_CANVAS_PATH_SET_COORD + return *this; +} + +#undef n