guiCtrlArrayCtrl.cpp
Engine/source/gui/containers/guiCtrlArrayCtrl.cpp
Public Functions
Detailed Description
Public Functions
ConsoleDocClass(GuiControlArrayControl )
IMPLEMENT_CONOBJECT(GuiControlArrayControl )
1 2//----------------------------------------------------------------------------- 3// Copyright (c) 2012 GarageGames, LLC 4// 5// Permission is hereby granted, free of charge, to any person obtaining a copy 6// of this software and associated documentation files (the "Software"), to 7// deal in the Software without restriction, including without limitation the 8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 9// sell copies of the Software, and to permit persons to whom the Software is 10// furnished to do so, subject to the following conditions: 11// 12// The above copyright notice and this permission notice shall be included in 13// all copies or substantial portions of the Software. 14// 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21// IN THE SOFTWARE. 22//----------------------------------------------------------------------------- 23 24#include "gui/containers/guiCtrlArrayCtrl.h" 25 26#include "console/engineAPI.h" 27 28IMPLEMENT_CONOBJECT(GuiControlArrayControl); 29 30ConsoleDocClass( GuiControlArrayControl, 31 "@brief Brief Desc.\n\n" 32 33 "@tsexample\n" 34 "// Comment:\n" 35 "%okButton = new ClassObject()\n" 36 "instantiation\n" 37 "@endtsexample\n\n" 38 39 "@ingroup GuiContainers" 40); 41 42// One call back that the system isnt ready for yet in this class 43 44GuiControlArrayControl::GuiControlArrayControl() 45{ 46 mResizing = false; 47 48 mCols = 0; 49 mRowSize = 30; 50 mRowSpacing = 2; 51 mColSpacing = 0; 52 mIsContainer = true; 53} 54 55void GuiControlArrayControl::initPersistFields() 56{ 57 addGroup( "Array" ); 58 59 addField( "colCount", TypeS32, Offset(mCols, GuiControlArrayControl), 60 "Number of colums in the array." ); 61 addField( "colSizes", TypeS32Vector, Offset(mColumnSizes, GuiControlArrayControl), 62 "Size of each individual column." ); 63 addField( "rowSize", TypeS32, Offset(mRowSize, GuiControlArrayControl), 64 "Heigth of a row in the array." ); 65 addField( "rowSpacing", TypeS32, Offset(mRowSpacing, GuiControlArrayControl), 66 "Padding to put between rows." ); 67 addField( "colSpacing", TypeS32, Offset(mColSpacing, GuiControlArrayControl), 68 "Padding to put between columns." ); 69 70 endGroup( "Array" ); 71 72 Parent::initPersistFields(); 73} 74 75bool GuiControlArrayControl::onWake() 76{ 77 if ( !Parent::onWake() ) 78 return false; 79 80 return true; 81} 82 83void GuiControlArrayControl::onSleep() 84{ 85 Parent::onSleep(); 86} 87 88void GuiControlArrayControl::inspectPostApply() 89{ 90 Parent::inspectPostApply(); 91 92 updateArray(); 93} 94 95bool GuiControlArrayControl::resize(const Point2I &newPosition, const Point2I &newExtent) 96{ 97 if( !Parent::resize(newPosition, newExtent) ) 98 return false; 99 100 return updateArray(); 101} 102 103void GuiControlArrayControl::addObject(SimObject *obj) 104{ 105 Parent::addObject(obj); 106 107 updateArray(); 108} 109 110void GuiControlArrayControl::removeObject(SimObject *obj) 111{ 112 Parent::removeObject(obj); 113 114 updateArray(); 115} 116 117bool GuiControlArrayControl::reOrder(SimObject* obj, SimObject* target) 118{ 119 bool ret = Parent::reOrder(obj, target); 120 if (ret) 121 updateArray(); 122 123 return ret; 124} 125 126bool GuiControlArrayControl::updateArray() 127{ 128 // Prevent recursion 129 if(mResizing) 130 return false; 131 132 // Set Resizing. 133 mResizing = true; 134 135 if(mCols < 1 || size() < 1) 136 { 137 mResizing = false; 138 return false; 139 } 140 141 S32 *sizes = new S32[mCols]; 142 S32 *offsets = new S32[mCols]; 143 S32 totalSize = 0; 144 Point2I extent = getExtent(); 145 146 // Calculate the column sizes 147 for(S32 i=0; i<mCols; i++) 148 { 149 if( i >= mColumnSizes.size() ) 150 sizes[ i ] = 0; 151 else 152 sizes[i] = mColumnSizes[i]; 153 154 offsets[i] = totalSize; 155 156 // If it's an auto-size one, then... auto-size... 157 if(sizes[i] == -1) 158 { 159 sizes[i] = extent.x - totalSize; 160 break; 161 } 162 163 totalSize += sizes[i] + mColSpacing; 164 } 165 166 // Now iterate through the children and resize them to fit the grid... 167 for(S32 i=0; i<size(); i++) 168 { 169 GuiControl *gc = dynamic_cast<GuiControl*>(operator[](i)); 170 171 // Get the current column and row... 172 S32 curCol = i % mCols; 173 S32 curRow = i / mCols; 174 175 if(gc) 176 { 177 Point2I newPos(offsets[curCol], curRow * (mRowSize + mRowSpacing)); 178 Point2I newExtents(sizes[curCol], mRowSize); 179 180 gc->resize(newPos, newExtents); 181 } 182 } 183 184 // Clear Sizing Flag. 185 mResizing = false; 186 187 delete [] sizes; 188 delete [] offsets; 189 190 return true; 191} 192