📄 percentagesizeutil.as
字号:
spaceToDistribute -= unused; } // Continue as long as there are some remaining flexible children. // The "done" flag isn't strictly necessary, except that it catches // cases where round-off error causes totalPercent to not exactly // equal zero. do { flexConsumed = 0; // space consumed by flexible compontents done = true; // we are optimistic // Space for flexible children is the total amount of space // available minus the amount of space consumed by non-flexible // components.Divide that space in proportion to the percent // of the child var spacePerPercent:Number = spaceToDistribute / totalPercent; // Attempt to divide out the space using our percent amounts, // if we hit its limit then that control becomes 'non-flexible' // and we run the whole space to distribute calculation again. for(var i:int = 0; i < numChildren; i++) { var childInfo:ChildInfo = ChildInfo(childInfoArray[i]); // Set its size in proportion to its percent. var size:Number = childInfo.percent * spacePerPercent; // If our flexiblity calc say grow/shrink more than we are // allowed, then we grow/shrink whatever we can, remove // ourselves from the array for the next pass, and start // the loop over again so that the space that we weren't // able to consume / release can be re-used by others. if(size < childInfo.min) { var min:Number = childInfo.min; childInfo.size = min; // Move this object to the end of the array // and decrement the length of the array. // This is slightly expensive, but we don't expect // to hit these min/max limits very often. childInfoArray[i] = childInfoArray[--numChildren]; childInfoArray[numChildren] = childInfo; totalPercent -= childInfo.percent; spaceToDistribute -= min; done = false; break; } else if(size > childInfo.max) { var max:Number = childInfo.max; childInfo.size = max; childInfoArray[i] = childInfoArray[--numChildren]; childInfoArray[numChildren] = childInfo; totalPercent -= childInfo.percent; spaceToDistribute -= max; done = false; break; } else { // All is well, let's carry on... childInfo.size = size; flexConsumed += size; } } } while(!done); return Math.max(0, Math.floor(spaceToDistribute - flexConsumed)) } /** * This function distributes excess space among the flexible children * because of rounding errors where we want to keep children's dimensions * full pixel amounts. This only distributes the extra space * if there was some rounding down and there are still * flexible children. * * @param parent The parent container of the children. * * @param spaceForChildren The total space for all children */ public static function distributeExtraHeight(children:Array, configurations:Array, spaceForChildren:Number):void { // We should only get here after distributing the majority of the // space already. This is done in flexChildHeightsProportionally. // Strategy here is to keep adding 1 pixel at a time to each // component that's flexible (percentHeight defined and hasn't // reached maxHeight yet). We could use another approach where // we add more than a pixel at a time, but we'd have to first // calculate exactly how many flexible components we have first // and see how much space we can add to them without hitting // their maxHeight. Since we're just dealing with rounding // issues, we should only make one pass here (if we hit maxHeight // problems, we might make more than one, but not many more). // We just distribute from the top-down and don't care about // who was "rounded down the most" // First check if we should distribute any extra space. To do // this, we check to see if someone suffers from rounding error. var wantToGrow:Boolean = false; var percentHeight:Number; var spaceToDistribute:Number = spaceForChildren; var spaceUsed:Number = 0; var childHeight:Number; var wantSpace:Number; var childCount:int = children.length; for(var i:int = 0; i < childCount; i++) { var child:DisplayObject = DisplayObject(children[i]); var config:Object = configurations[i]; if(!config.includeInLayout) { continue; } childHeight = child.height; percentHeight = config.percentHeight; spaceUsed += childHeight; if(!isNaN(percentHeight)) { wantSpace = Math.ceil(percentHeight/100 * spaceForChildren); if(wantSpace > childHeight) { wantToGrow = true; } } } // No need to distribute extra size if(!wantToGrow) { return; } // Start distributing... spaceToDistribute -= spaceUsed; // If we still have components that will let us // distribute to them var stillFlexibleComponents:Boolean = true; while(stillFlexibleComponents && spaceToDistribute > 0) { // Start optimistically stillFlexibleComponents = false; for(i = 0; i < childCount; i++) { child = DisplayObject(children[i]); config = configurations[i]; childHeight = child.height; percentHeight = config.percentHeight; // if they have a percentHeight, and we won't reach their // maxHeight by giving them one more pixel, then // give them a pixel if(!isNaN(percentHeight) && config.includeInLayout && childHeight < config.maxHeight) { wantSpace = Math.ceil(percentHeight/100 * spaceForChildren); if(wantSpace > childHeight) { child.height = childHeight + 1; spaceToDistribute--; stillFlexibleComponents = true; if(spaceToDistribute == 0) { return; } } } } } } /** * This function distributes excess space among the flexible children * because of rounding errors where we want to keep children's dimensions * full pixel amounts. This only distributes the extra space * if there was some rounding down and there are still * flexible children. * * @param parent The parent container of the children. * * @param spaceForChildren The total space for all children */ public static function distributeExtraWidth(children:Array, configurations:Array, spaceForChildren:Number):void { // We should only get here after distributing the majority of the // space already. This is done in flexChildWidthsProportionally. // Strategy here is to keep adding 1 pixel at a time to each // component that's flexible (percentWidth defined and hasn't // reached maxWidth yet). We could use another approach where // we add more than a pixel at a time, but we'd have to first // calculate exactly how many flexible components we have first // and see how much space we can add to them without hitting // their maxWidth. Since we're just dealing with rounding // issues, we should only make one pass here (if we hit maxWidth // problems, we might make more than one, but not many more). // We just distribute from the top-down and don't care about // who was "rounded down the most" // First check if we should distribute any extra space. To do // this, we check to see if someone suffers from rounding error. var childCount:int = children.length; var wantToGrow:Boolean = false; var percentWidth:Number; var spaceToDistribute:Number = spaceForChildren; var spaceUsed:Number = 0; var childWidth:Number; var wantSpace:Number; for(var i:int = 0; i < childCount; i++) { var child:DisplayObject = DisplayObject(children[i]); var config:Object = configurations[i]; if(!config.includeInLayout) { continue; } childWidth = child.width; percentWidth = config.percentWidth; spaceUsed += childWidth; if(!isNaN(percentWidth)) { wantSpace = Math.ceil(percentWidth / 100 * spaceForChildren); if(wantSpace > childWidth) { wantToGrow = true; } } } // No need to distribute extra size if(!wantToGrow) { return; } // Start distributing... spaceToDistribute -= spaceUsed; // If we still have components that will let us // distribute to them var stillFlexibleComponents:Boolean = true; while(stillFlexibleComponents && spaceToDistribute > 0) { // Start optimistically stillFlexibleComponents = false; for(i = 0; i < childCount; i++) { child = DisplayObject(children[i]); config = configurations[i]; childWidth = child.width; percentWidth = config.percentWidth; // if they have a percentWidth, and we won't reach their // maxWidth by giving them one more pixel, then // give them a pixel if(!isNaN(percentWidth) && config.includeInLayout && childWidth < config.maxWidth) { wantSpace = Math.ceil(percentWidth / 100 * spaceForChildren); if(wantSpace > childWidth) { child.width = childWidth + 1; spaceToDistribute--; stillFlexibleComponents = true; if(spaceToDistribute == 0) { return; } } } } } } }} import flash.display.DisplayObject; class ChildInfo{ public var max:Number; public var min:Number; public var child:DisplayObject; public var percent:Number; public var size:Number; public var opposite:Number;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -