001package com.hfg.graphics.units;
002
003
004//------------------------------------------------------------------------------
005/**
006 * Container for unit-independent 2D width and height.
007 *
008 * @author J. Alex Taylor, hairyfatguy.com
009 */
010//------------------------------------------------------------------------------
011// com.hfg XML/HTML Coding Library
012//
013// This library is free software; you can redistribute it and/or
014// modify it under the terms of the GNU Lesser General Public
015// License as published by the Free Software Foundation; either
016// version 2.1 of the License, or (at your option) any later version.
017//
018// This library is distributed in the hope that it will be useful,
019// but WITHOUT ANY WARRANTY; without even the implied warranty of
020// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
021// Lesser General Public License for more details.
022//
023// You should have received a copy of the GNU Lesser General Public
024// License along with this library; if not, write to the Free Software
025// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026//
027// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
028// jataylor@hairyfatguy.com
029//------------------------------------------------------------------------------
030
031public class GfxSize2D
032{
033   private GfxSize mWidth;
034   private GfxSize mHeight;
035
036
037   //##########################################################################
038   // CONSTRUCTORS
039   //##########################################################################
040
041   //--------------------------------------------------------------------------
042   public GfxSize2D()
043   {
044
045   }
046
047   //##########################################################################
048   // PUBLIC METHODS
049   //##########################################################################
050
051
052   //--------------------------------------------------------------------------
053   public GfxSize2D setWidth(GfxSize inValue)
054   {
055      mWidth = inValue;
056      return this;
057   }
058
059   //--------------------------------------------------------------------------
060   public GfxSize getWidth()
061   {
062      return mWidth;
063   }
064
065   //--------------------------------------------------------------------------
066   public GfxSize2D setHeight(GfxSize inValue)
067   {
068      mHeight = inValue;
069      return this;
070   }
071
072   //--------------------------------------------------------------------------
073   public GfxSize getHeight()
074   {
075      return mHeight;
076   }
077
078   //--------------------------------------------------------------------------
079   public void scale(float inScalingFactor)
080   {
081      getHeight().scale(inScalingFactor);
082      getWidth().scale(inScalingFactor);
083   }
084
085   //---------------------------------------------------------------------------
086   public GfxSize2D applyMaxDimensionsPreservingAspectRatio(GfxSize2D inMaxDimensions)
087   {
088      float widthPx  = getWidth().to(GfxUnits.pixels);
089      float heightPx = getHeight().to(GfxUnits.pixels);
090
091      // Are we exceeding the maximum dimensions?
092      float maxWidthPx  = inMaxDimensions.getWidth().to(GfxUnits.pixels);
093      float maxHeightPx = inMaxDimensions.getHeight().to(GfxUnits.pixels);
094
095
096      if (widthPx > maxWidthPx)
097      {
098         float scalingFactor = maxWidthPx / widthPx;
099
100         widthPx = maxWidthPx;
101         heightPx = heightPx * scalingFactor;
102      }
103
104      if (heightPx > maxHeightPx)
105      {
106         float scalingFactor = maxHeightPx / heightPx;
107
108         widthPx = widthPx * scalingFactor;
109         heightPx = maxHeightPx;
110      }
111
112      return new GfxSize2D().setWidth(new Pixels(widthPx)).setHeight(new Pixels(heightPx));
113   }
114
115}