ManiaLib 1.0b1 API Reference

Source for file GuiTools.class.php

Documentation is available at GuiTools.class.php

  1. <?php
  2. /**
  3.  * @author Maxime Raoust
  4.  * @copyright 2009-2010 NADEO
  5.  * @package ManiaLib
  6.  */
  7.  
  8. /**
  9.  * GUI Tools
  10.  * Misc helpers for the GUI toolkit
  11.  * @package ManiaLib
  12.  * @subpackage GUIToolkit
  13.  */
  14. abstract class GuiTools
  15. {
  16.     /**
  17.      * Returns the X position of an element in relation to another element and
  18.      * according to their respective alignments
  19.      * 
  20.      * @param int X position of the parent element
  21.      * @param int Width of the parent element
  22.      * @param string Horizontal alignement of the parent element
  23.      * @param string Horizontal alignement of the element you want to place
  24.      * @return int Calculated X position of the element you want to place
  25.      */
  26.     final public static function getAlignedPosX($posX$sizeX$halign$newAlign)
  27.     {
  28.         if(!$halign)
  29.         {
  30.             $halign 'left';
  31.         }
  32.         $alignmentString $halign.'|'.$newAlign;
  33.         switch($alignmentString)
  34.         {
  35.             case 'center|center':
  36.             case 'left|left':
  37.             case 'right|right':
  38.                 $factor 0;
  39.                 break;
  40.                 
  41.             case 'center|left':
  42.             case 'right|center':
  43.                 $factor = -0.5;
  44.                 break;
  45.                 
  46.             case 'center|right':
  47.             case 'left|center':
  48.                 $factor 0.5;
  49.                 break;
  50.  
  51.             case 'left|right':
  52.                 $factor 1;
  53.                 break;
  54.  
  55.             case 'right|left':
  56.                 $factor = -1;
  57.                 break;        
  58.             
  59.             default:
  60.                 throw new Exception('GUITools: Unsupported positions');
  61.         }
  62.         return $posX $factor $sizeX;
  63.     }
  64.  
  65.     /**
  66.      * Returns the Y position of an element in relation to another element and
  67.      * according to their respective alignments
  68.      * 
  69.      * @param int Y position of the parent element
  70.      * @param int Height of the parent element
  71.      * @param string Vertical alignement of the parent element
  72.      * @param string Vertical alignement of the element you want to place
  73.      * @return int Calculated Y position of the element you want to place
  74.      */
  75.     final public static function getAlignedPosY($posY$sizeY$valign$newAlign)
  76.     {
  77.         switch($valign)
  78.         {
  79.             case 'top'
  80.             case null
  81.                 $valign 'right';
  82.                 break;
  83.                 
  84.             case 'bottom':
  85.                 $valign 'left';   
  86.                 break;
  87.         }
  88.         switch($newAlign)
  89.         {
  90.             case 'top':
  91.                 $newAlign 'right';
  92.                 break;
  93.                 
  94.             case 'bottom'
  95.                 $newAlign 'left';
  96.                 break;
  97.         }
  98.         return self::getAlignedPosX($posY$sizeY$valign$newAlign);
  99.     }
  100.  
  101.     /**
  102.      * Returns the position of an element in relation to another element and
  103.      * according to their respective alignments
  104.      * 
  105.      * @param GuiElement Parent element
  106.      * @param string Horizontal alignement of the element you want to place
  107.      * @param string Vertical alignement of the element you want to place
  108.      * @return array Calculated position of the element you want to place. The
  109.      *  array contains 2 elements with "x" and "y" indexes
  110.      */
  111.     final public static function getAlignedPos(GuiElement $object$newHalign$newValign)
  112.     {
  113.         $newPosX self::getAlignedPosX(
  114.             $object->getPosX()
  115.             $object->getSizeX()
  116.             $object->getHalign()
  117.             $newHalign);
  118.         $newPosY self::getAlignedPosY(
  119.             $object->getPosY()
  120.             $object->getSizeY()
  121.             $object->getValign()
  122.             $newValign);
  123.         return array('x' => $newPosX'y' => $newPosY);
  124.     }
  125. }
  126.  
  127. ?>