From d5cbd2d6a5dda7fdfea90484dabb363b9ab62b0c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 28 Feb 2011 15:25:26 +0000 Subject: [PATCH] Add various constructors to Sides (semantics inspired by CSS margins) Reduce code duplication in Alignment::apply --- source/geometry.cpp | 46 ++++++++++++++++++++++++++++++++------------- source/geometry.h | 6 +++++- 2 files changed, 38 insertions(+), 14 deletions(-) diff --git a/source/geometry.cpp b/source/geometry.cpp index 5c304d3..91d7c5a 100644 --- a/source/geometry.cpp +++ b/source/geometry.cpp @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007-2008, 2010-2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -28,6 +28,34 @@ Sides::Sides(): left(0) { } +Sides::Sides(unsigned s): + top(s), + right(s), + bottom(s), + left(s) +{ } + +Sides::Sides(unsigned v, unsigned h): + top(v), + right(h), + bottom(v), + left(h) +{ } + +Sides::Sides(unsigned t, unsigned h, unsigned b): + top(t), + right(h), + bottom(b), + left(h) +{ } + +Sides::Sides(unsigned t, unsigned r, unsigned b, unsigned l): + top(t), + right(r), + bottom(b), + left(l) +{ } + Sides::Loader::Loader(Sides &s): DataFile::ObjectLoader(s) @@ -55,22 +83,14 @@ void Alignment::apply(Geometry &geom, const Geometry &parent) const void Alignment::apply(Geometry &geom, const Geometry &parent, const Sides &margin) const { - unsigned pw = parent.w-margin.left-margin.right; - unsigned ph = parent.h-margin.bottom-margin.top; + Geometry content = parent; + content.w -= margin.left+margin.right; + content.h -= margin.bottom+margin.top; geom.x += margin.left; geom.y += margin.bottom; - if(pw>geom.w) - { - geom.w += static_cast((pw-geom.w)*w); - geom.x += static_cast((pw-geom.w)*x); - } - if(ph>geom.h) - { - geom.h += static_cast((ph-geom.h)*h); - geom.y += static_cast((ph-geom.h)*y); - } + apply(geom, content); } } // namespace GLtk diff --git a/source/geometry.h b/source/geometry.h index 4fe6a75..ed18e54 100644 --- a/source/geometry.h +++ b/source/geometry.h @@ -1,7 +1,7 @@ /* $Id$ This file is part of libmspgltk -Copyright © 2007 Mikko Rasa, Mikkosoft Productions +Copyright © 2007, 2011 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ @@ -45,6 +45,10 @@ struct Sides unsigned left; Sides(); + Sides(unsigned); + Sides(unsigned, unsigned); + Sides(unsigned, unsigned, unsigned); + Sides(unsigned, unsigned, unsigned, unsigned); }; -- 2.43.0