Skip to content

Instantly share code, notes, and snippets.

@jessefreeman
Created January 16, 2016 18:36
Show Gist options
  • Select an option

  • Save jessefreeman/203c2d5f555af54dc300 to your computer and use it in GitHub Desktop.

Select an option

Save jessefreeman/203c2d5f555af54dc300 to your computer and use it in GitHub Desktop.
using UnityEngine;
using System.Collections;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using PixelVision.Framework.Utils;
using PixelVision.Engine;
namespace PixelVision.App.UI{
public class GridPosition
{
public int cellWidth = 0;
public int cellHeight = 0;
public int column = 0;
public int row = 0;
public int x
{
get { return column * cellWidth; }
}
public int y
{
get { return row*cellWidth; }
}
public GridPosition(int column = 0, int row = 0)
{
this.column = column;
this.row = row;
}
}
public class TileSelector : Selectable {
public delegate void OnSelectionCallback();
public OnSelectionCallback selectionCallback;
protected Vector2 _tileSize = Vector2.one;
public Vector2 tileSize
{
set { _tileSize = value; }
}
public int tileSizeWidth
{
get { return (int)_tileSize.x * zoomScale; }
}
public int tileSizeHeight
{
get { return (int)_tileSize.y * zoomScale; }
}
public RectTransform rectTransform;
public int zoomScale
{
get { return _zoomScale;}
set
{
_zoomScale = (value <= 0) ? 1 : value;
if(originalTexture != null)
texture = originalTexture;
}
}
protected int _zoomScale;
protected Rect sRect = new Rect();
protected RawImage rawImage;
protected Texture2D originalTexture;
virtual public Texture2D texture
{
get { return rawImage.texture as Texture2D; }
set
{
originalTexture = ProcessImage(value);
rawImage.texture = Texture2DUtil.CloneTexture2D(originalTexture, zoomScale);
// Resize the component to fit the new texture
rectTransform.sizeDelta = new Vector2(texture.width, texture.height);
}
}
public void Refresh()
{
}
virtual public int selectionIndex
{
get
{
var x = sRect.x / tileSizeWidth;
var y = sRect.y / tileSizeHeight;
var width = texture.width / tileSizeWidth;
var index = x + (y * width);
return (int)index;
}
}
public Texture2D rawTexture
{
get { return rawImage.texture as Texture2D;}
}
virtual public void Awake(){
rectTransform = GetComponent<RectTransform> ();
rawImage = GetComponent<RawImage> ();
}
public override void OnPointerDown(PointerEventData eventData)
{
var gridPos = CalculateGridPosition(eventData);
if (gridPos != null)
OnSelection(gridPos.column, gridPos.row);
}
virtual protected GridPosition CalculateGridPosition(PointerEventData eventData)
{
// Convert the mouse position to the local coordinate space of the GameObject
Vector2 localCursor;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(rectTransform, eventData.position, eventData.pressEventCamera, out localCursor))
return null;
// Adjust the coordinates based on the bottom left 0,0 registration
localCursor.x += rectTransform.rect.width / 2;
localCursor.y = (rectTransform.rect.height / 2 + localCursor.y);
// Make sure we are not out of bounds
if (localCursor.x > rectTransform.rect.width || localCursor.x < 0)
return null;
if (localCursor.y > rectTransform.rect.height || localCursor.y < 0)
return null;
// Find the correct column and row
var column = Mathf.CeilToInt(localCursor.x / tileSizeWidth) - 1;
var row = Mathf.CeilToInt(localCursor.y / tileSizeHeight) - 1;
// Return the new grid position
return new GridPosition(column, row);
}
virtual protected void UpdateSelection(int column, int row){
//Debug.Log ("Select Column " + column + " row " + row);
sRect.x = (int)(column * tileSizeWidth);
sRect.y = (int)(row * tileSizeHeight);
sRect.width = (int)tileSizeWidth;
sRect.height = (int)tileSizeHeight;
}
virtual protected void SelectionAction(){
}
virtual protected void TriggerSelectionCallBack(){
if (selectionCallback != null)
selectionCallback ();
}
virtual public void OnSelection(int column, int row){
UpdateSelection (column, row);
SelectionAction ();
TriggerSelectionCallBack ();
}
virtual public void ResetSelection(){
sRect.x = 0;
sRect.y = 0;
sRect.width = tileSizeWidth;
sRect.height = tileSizeHeight;
}
virtual protected Texture2D ProcessImage(Texture2D texture)
{
return texture;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment