1: <?php
2: /**
3: * ManiaLib - Lightweight PHP framework for Manialinks
4: *
5: * @see http://code.google.com/p/manialib/
6: * @copyright Copyright (c) 2009-2011 NADEO (http://www.nadeo.com)
7: * @license http://www.gnu.org/licenses/lgpl.html LGPL License 3
8: * @version $Revision: 732 $:
9: * @author $Author: maximeraoust $:
10: * @date $Date: 2012-07-09 16:55:18 +0200 (lun., 09 juil. 2012) $:
11: */
12:
13: namespace ManiaLib\Gui;
14:
15: abstract class Tools
16: {
17:
18: /**
19: * Returns the X position of an element in relation to another element and
20: * according to their respective alignments
21: *
22: * @param int X position of the parent element
23: * @param int Width of the parent element
24: * @param string Horizontal alignement of the parent element
25: * @param string Horizontal alignement of the element you want to place
26: * @return int Calculated X position of the element you want to place
27: */
28: final public static function getAlignedPosX($posX, $sizeX, $halign, $newAlign)
29: {
30: if(!$halign)
31: {
32: $halign = 'left';
33: }
34: $alignmentString = $halign.'|'.$newAlign;
35: switch($alignmentString)
36: {
37: case 'center|center':
38: case 'center|center2':
39: case 'left|left':
40: case 'right|right':
41: $factor = 0;
42: break;
43:
44: case 'center|left':
45: case 'right|center':
46: case 'right|center2':
47: $factor = -0.5;
48: break;
49:
50: case 'center|right':
51: case 'left|center':
52: case 'left|center2':
53: $factor = 0.5;
54: break;
55:
56: case 'left|right':
57: $factor = 1;
58: break;
59:
60: case 'right|left':
61: $factor = -1;
62: break;
63:
64: default:
65: throw new \Exception('GUITools: Unsupported positions: '.$alignmentString);
66: }
67: return $posX + $factor * $sizeX;
68: }
69:
70: /**
71: * Returns the Y position of an element in relation to another element and
72: * according to their respective alignments
73: *
74: * @param int Y position of the parent element
75: * @param int Height of the parent element
76: * @param string Vertical alignement of the parent element
77: * @param string Vertical alignement of the element you want to place
78: * @return int Calculated Y position of the element you want to place
79: */
80: final public static function getAlignedPosY($posY, $sizeY, $valign, $newAlign)
81: {
82: switch($valign)
83: {
84: case 'top':
85: case null:
86: $valign = 'right';
87: break;
88:
89: case 'bottom':
90: $valign = 'left';
91: break;
92: }
93: switch($newAlign)
94: {
95: case 'top':
96: $newAlign = 'right';
97: break;
98:
99: case 'bottom':
100: $newAlign = 'left';
101: break;
102: }
103: return self::getAlignedPosX($posY, $sizeY, $valign, $newAlign);
104: }
105:
106: /**
107: * Returns the position of an element in relation to another element and
108: * according to their respective alignments
109: *
110: * @param \ManiaLib\Gui\Element Parent element
111: * @param string Horizontal alignement of the element you want to place
112: * @param string Vertical alignement of the element you want to place
113: * @return array Calculated position of the element you want to place. The
114: * array contains 2 elements with "x" and "y" indexes
115: */
116: final public static function getAlignedPos(\ManiaLib\Gui\Element $object,
117: $newHalign, $newValign)
118: {
119: $newPosX = self::getAlignedPosX(
120: $object->getPosX(), $object->getRealSizeX(), $object->getHalign(),
121: $newHalign);
122: $newPosY = self::getAlignedPosY(
123: $object->getPosY(), $object->getRealSizeY(), $object->getValign(),
124: $newValign);
125: return array('x' => $newPosX, 'y' => $newPosY);
126: }
127:
128: }
129:
130: ?>