Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from lonewolfwilliams/master
UIGridLayout and RadioButton elements
- Loading branch information
Showing
16 changed files
with
450 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
|
||
public class GridManager : MonoBehaviour | ||
{ | ||
|
||
// Use this for initialization | ||
void Start () | ||
{ | ||
var grid = new UIGridLayout(5, 5, 2); | ||
grid.edgeInsets = new UIEdgeInsets(2); | ||
|
||
//add buttons automagically | ||
UISprite[] buttons = new UISprite[9]; | ||
for (var i=0; i<9; i++) | ||
{ | ||
var depth = - (i+1); | ||
var button = UIButton.create( UI.firstToolkit, "emptyUp.png", "emptyDown.png", 0, 0, depth); | ||
buttons[i] = button; | ||
|
||
} | ||
grid.addChild(buttons); | ||
|
||
//add a button in a specific place with fixed width and height | ||
var anotherButton = UIButton.create( UI.firstToolkit, "emptyUp.png", "emptyDown.png", 0, 0, 10); | ||
//columns and rows are zero based | ||
grid.AddChildAt(anotherButton, 2, 0, 2, 2); | ||
|
||
//add buttons with snapping | ||
for (var i=0; i<2; i++) | ||
{ | ||
for (var j=0; j<2; j++) | ||
{ | ||
var button = UIButton.create( UI.firstToolkit, "emptyUp.png", "emptyDown.png", 0, 0, 11); | ||
grid.AddChildAt(button, 2+i, 2+j, true); | ||
} | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
|
||
public class RadioButtonManager : MonoBehaviour | ||
{ | ||
|
||
UIScrollableHorizontalLayout m_scrollable; | ||
|
||
// Use this for initialization | ||
void Start () | ||
{ | ||
CreateScrollableMenuWithPips(); | ||
} | ||
|
||
#region create ui helpers | ||
void CreateScrollableMenuWithPips() | ||
{ | ||
m_scrollable = new UIScrollableHorizontalLayout( 20 ); | ||
|
||
// we wrap the addition of all the sprites with a begin updates so it only lays out once when complete | ||
m_scrollable.beginUpdates(); | ||
|
||
// if you plan on making the scrollable wider than the item width you need to set your edgeInsets so that the | ||
// left + right inset is equal to the extra width you set | ||
float itemWidth = 250f; | ||
float leftInset; | ||
float rightInset; | ||
leftInset = rightInset = (Screen.width - itemWidth) / 2; | ||
|
||
m_scrollable.edgeInsets = new UIEdgeInsets( 0, Mathf.RoundToInt(leftInset), 0, Mathf.RoundToInt(rightInset) ); | ||
|
||
var scrollerHeight = UI.scaleFactor * itemWidth; | ||
var scrollerWidth = UI.scaleFactor * ( itemWidth + leftInset + rightInset ); // item width + 150 extra width | ||
m_scrollable.setSize( scrollerWidth, scrollerHeight ); | ||
|
||
// paging will snap to the nearest page when scrolling | ||
m_scrollable.pagingEnabled = true; | ||
m_scrollable.pageWidth = itemWidth * UI.scaleFactor; | ||
|
||
// center the scrollable horizontally | ||
m_scrollable.position = new Vector3( ( Screen.width - scrollerWidth ) / 2, -Screen.height + scrollerHeight, 0 ); | ||
|
||
for( var i = 0; i < 20; i++ ) | ||
{ | ||
var button = UIButton.create( "marioPanel.png", "marioPanel.png", 0, 0 ); | ||
m_scrollable.addChild( button ); | ||
} | ||
|
||
m_scrollable.endUpdates(); | ||
m_scrollable.endUpdates(); // this is a bug. it shouldnt need to be called twice | ||
|
||
//pips | ||
var pips = new UIRadioButtonGroup(0, UIAbstractContainer.UILayoutType.Horizontal); | ||
|
||
int pageCount = Mathf.CeilToInt(20 * itemWidth / scrollerWidth); | ||
for (int i = 0; i < pageCount; i++) | ||
{ | ||
var toggle = UIToggleButton.create("emptyUp.png", "emptyDown.png", "emptyUp.png", 0, 0); | ||
|
||
pips.addChild(toggle); | ||
} | ||
|
||
pips.beginUpdates(); | ||
float screenUnits = 1.0f / Screen.width; | ||
float halfWidth = 140 * pageCount / 2; | ||
float offset = halfWidth * screenUnits; | ||
pips.positionFromCenter(-50 * screenUnits, -offset); | ||
pips.endUpdates(); | ||
|
||
pips.OnSelect += HandlePipsOnSelect; | ||
pips.IndexOfCurrentlySelected = 1; | ||
} | ||
#endregion | ||
|
||
#region handlers | ||
void HandlePipsOnSelect (UIRadioButtonGroup sender, UIToggleButton selected) | ||
{ | ||
print ("pips selection changed"); | ||
|
||
int pageIndex = Mathf.RoundToInt( 20f / sender.NumberOfChildren * sender.IndexOfCurrentlySelected ); | ||
m_scrollable.scrollToPage(pageIndex); | ||
} | ||
#endregion handlers | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Binary file not shown.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
using UnityEngine; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
|
||
/* | ||
* gareth williams | ||
* | ||
* compose layouts by attaching uisprites to a grid of m_clipss | ||
* | ||
* overloaded functionality for | ||
* row and column spans | ||
* snapping objects to grid cell dimensions | ||
* | ||
* overriden functionality for | ||
* adding an array of children to be automagically attached to the grid | ||
*/ | ||
|
||
public class UIGridLayout : UIAbstractContainer | ||
{ | ||
public int columns; | ||
public int rows; | ||
|
||
UIAnchorInfo m_gridAnchor; | ||
|
||
public UIGridLayout( int columns, int rows, int cellPadding ) | ||
: base( UIAbstractContainer.UILayoutType.AbsoluteLayout ) //don't manage layout for us | ||
{ | ||
//padding | ||
_spacing = cellPadding; | ||
|
||
this.columns = columns; | ||
this.rows = rows; | ||
|
||
//top left | ||
m_gridAnchor = UIAnchorInfo.DefaultAnchorInfo(); | ||
m_gridAnchor.ParentUIObject = this; | ||
m_gridAnchor.UIPrecision = UIPrecision.Pixel; | ||
} | ||
|
||
public void AddChildAt(UISprite child, int column, int row) | ||
{ | ||
var anchorInfo = GetCellAnchorFor(column, row); | ||
|
||
// dont overwrite the sprites origin anchor! | ||
anchorInfo.OriginUIxAnchor = child.anchorInfo.OriginUIxAnchor; | ||
anchorInfo.OriginUIyAnchor = child.anchorInfo.OriginUIyAnchor; | ||
|
||
child.beginUpdates(); | ||
child.anchorInfo = anchorInfo; | ||
child.refreshPosition(); | ||
child.endUpdates(); | ||
|
||
base.addChild(child); | ||
} | ||
|
||
#region overloads | ||
public void AddChildAt(UISprite child, int column, int row, int colSpan, int rowSpan) | ||
{ | ||
float cellWidth = Screen.width / columns; | ||
float cellHeight = Screen.height / rows; | ||
|
||
Vector3 scale = new Vector3(1, 1, 1); | ||
if(colSpan > 0) | ||
{ | ||
scale.x = 1.0f / child.width * (colSpan * cellWidth); | ||
} | ||
if(rowSpan > 0) | ||
{ | ||
scale.y = 1.0f / child.height * (rowSpan * cellHeight); | ||
} | ||
|
||
child.scale = scale; | ||
AddChildAt(child, column, row); | ||
} | ||
|
||
public void AddChildAt(UISprite child, int column, int row, bool snapToGrid) | ||
{ | ||
Vector3 scale = new Vector3(1, 1, 1); | ||
if(snapToGrid) | ||
{ | ||
scale.x = 1.0f / child.width * GetSnappedWidthToGrid(child); | ||
scale.y = 1.0f / child.height * GetSnappedHeightToGrid(child); | ||
} | ||
|
||
child.scale = scale; | ||
AddChildAt(child, column, row); | ||
} | ||
#endregion | ||
|
||
public override void addChild (params UISprite[] children) | ||
{ | ||
for (int i=0; i<children.Length; i++) | ||
{ | ||
var toCol = Mathf.FloorToInt(i / columns); | ||
var toRow = i - toCol * columns; | ||
|
||
var anchorInfo = GetCellAnchorFor(toCol, toRow); | ||
|
||
// dont overwrite the sprites origin anchor! | ||
anchorInfo.OriginUIxAnchor = children[i].anchorInfo.OriginUIxAnchor; | ||
anchorInfo.OriginUIyAnchor = children[i].anchorInfo.OriginUIyAnchor; | ||
|
||
children[i].beginUpdates(); | ||
children[i].anchorInfo = anchorInfo; | ||
children[i].refreshPosition(); | ||
children[i].endUpdates(); | ||
} | ||
|
||
base.addChild (children); | ||
} | ||
|
||
//helper functions | ||
UIAnchorInfo GetCellAnchorFor(int column, int row) | ||
{ | ||
float cellWidth = Screen.width / columns; | ||
float cellHeight = Screen.height / rows; | ||
|
||
var cellAnchor = UIAnchorInfo.DefaultAnchorInfo(); | ||
cellAnchor.ParentUIObject = this; | ||
cellAnchor.ParentUIxAnchor = m_gridAnchor.OriginUIxAnchor; | ||
cellAnchor.ParentUIyAnchor = m_gridAnchor.OriginUIyAnchor; | ||
cellAnchor.OffsetX = UI.scaleFactor * cellWidth * column +_edgeInsets.left; | ||
cellAnchor.OffsetY = UI.scaleFactor * cellHeight * row + _edgeInsets.right; | ||
|
||
return cellAnchor; | ||
} | ||
|
||
float GetSnappedWidthToGrid (UISprite child) | ||
{ | ||
float cellWidth = Screen.width / columns; | ||
int requiredColumns = Mathf.RoundToInt(child.width / cellWidth); | ||
float snappedWidth = cellWidth * requiredColumns; | ||
|
||
if(snappedWidth <= 0) | ||
{ | ||
snappedWidth = cellWidth; | ||
} | ||
|
||
return snappedWidth; | ||
} | ||
|
||
float GetSnappedHeightToGrid (UISprite child) | ||
{ | ||
float cellHeight = Screen.height / rows; | ||
int requiredRows = Mathf.RoundToInt(child.height / cellHeight); | ||
float snappedHeight = cellHeight * requiredRows; | ||
|
||
if(snappedHeight <= 0) | ||
{ | ||
snappedHeight = cellHeight; | ||
} | ||
|
||
return snappedHeight; | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.