Last active
January 3, 2016 12:49
-
-
Save dkozar/8465389 to your computer and use it in GitHub Desktop.
This is the source of LoadData demo for eDriven.Gui framework. You can get eDriven.Gui framework at: http://edrivengui.com/
Free version is available at: http://edrivengui.com/free/
This file contains hidden or 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
/* | |
Copyright (c) 2010-2014 Danko Kozar | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
Note: | |
This is the source of the main frame of my Isometric experiment (from 2003) | |
The working demo is available at: http://dankokozar.com/flash/izometrija.html | |
I wrote this code BEFORE I ever knew about the existence of Object Oriented Programming :) | |
However, this is a very personal project that fired me into the (programming) orbit. ^^ | |
*/ | |
using System.Collections; | |
using Assets.eDriven.Demo._shared.Code.Tweens; | |
using Assets.eDriven.Skins; | |
using eDriven.Core.Geom; | |
using eDriven.Core.Managers; | |
using eDriven.Gui.Components; | |
using eDriven.Gui.Containers; | |
using eDriven.Gui.Cursor; | |
using eDriven.Gui.Events; | |
using eDriven.Gui.Managers; | |
using eDriven.Gui.Util; | |
using eDriven.Networking.Rpc; | |
using eDriven.Networking.Rpc.Loaders; | |
using Assets.eDriven.Demo.Tweens; | |
using UnityEngine; | |
namespace Assets.eDriven.Demo.Gui.Code.LoadData | |
{ | |
public class ImageViewer : Dialog | |
{ | |
private static readonly TextureLoader ImageLoader = new TextureLoader | |
{ | |
Cache = false, | |
}; // loads textures | |
private static bool _imageLoaderSet; | |
private static readonly Hashtable ButtonStyles = new Hashtable { { "cursor", "pointer" } }; | |
private Image _image; | |
private bool _imageUrlChanged; | |
private string _imageUrl; | |
public string ImageUrl | |
{ | |
get { | |
return _imageUrl; | |
} | |
set | |
{ | |
if (value == _imageUrl) | |
return; | |
_imageUrl = value; | |
_imageUrlChanged = true; | |
InvalidateProperties(); | |
} | |
} | |
public ImageViewer() | |
{ | |
Id = "viewer"; | |
//SetStyle("contentBackgroundColor", Color.green); // TODO: propagate style to skin | |
if (!_imageLoaderSet) | |
{ | |
_imageLoaderSet = true; | |
ImageLoader.Connector.MaxConcurrentRequests = 1; | |
ImageLoader.Connector.ConcurencyMode = ConcurencyMode.Multiple; | |
ImageLoader.Connector.ProcessingMode = ProcessingMode.Sync; | |
} | |
// minimum size (for window resizing) | |
MinWidth = 200; | |
MinHeight = 150; | |
// default size | |
Width = 400; | |
Height = 300; | |
// close the top window on Esc key | |
CloseOnEsc = true; | |
} | |
/* TEMP override - TODO: StyleProxy! */ | |
protected override void CreationComplete() | |
{ | |
base.CreationComplete(); | |
Skin.SetStyle("contentBackgroundColor", Color.grey); //RgbColor.FromHex(0x004CFF).ToColor()); | |
} | |
override protected void CreateChildren() | |
{ | |
base.CreateChildren(); | |
#region Tools | |
Button button = new Button | |
{ | |
SkinClass = typeof (ImageButtonSkin), | |
Icon = global::eDriven.Core.Caching.ImageLoader.Instance.Load("Icons/cancel"), | |
FocusEnabled = false, | |
Styles = ButtonStyles | |
}; | |
button.Click += delegate | |
{ | |
DispatchEvent(new CloseEvent(CloseEvent.CLOSE)); | |
}; | |
ToolGroup.AddChild(button); | |
#endregion | |
#region Image | |
_image = new Image { Id="image", PercentWidth = 100, PercentHeight = 100, ScaleMode = ImageScaleMode.ScaleToFit }; | |
AddContentChild(_image); | |
#endregion | |
#region Control bar | |
ControlBarGroup.AddChild(new Spacer {PercentWidth = 100}); | |
button = new Button | |
{ | |
Text = "Close preview", | |
SkinClass = typeof (ImageButtonSkin), | |
Icon = global::eDriven.Core.Caching.ImageLoader.Instance.Load("Icons/color_swatch"), | |
FocusEnabled = false, | |
Styles = ButtonStyles | |
}; | |
button.Click += delegate | |
{ | |
DispatchEvent(new CloseEvent(CloseEvent.CLOSE)); | |
}; | |
ControlBarGroup.AddChild(button); | |
#endregion | |
} | |
private LoadingMask _mask; | |
protected override void CommitProperties() | |
{ | |
base.CommitProperties(); | |
if (_imageUrlChanged) | |
{ | |
_imageUrlChanged = false; | |
_mask = new LoadingMask(ContentGroup, "Loading image..."); | |
var cursor = CursorManager.Instance.SetCursor(CursorType.Wait); | |
var oldBounds = Rectangle.FromWidthAndHeight(GetExplicitOrMeasuredWidth(), GetExplicitOrMeasuredHeight()); | |
oldBounds = oldBounds.CenterInside(Rectangle.FromSize(SystemManager.Instance.ScreenSize)); | |
ImageLoader.Load(_imageUrl, delegate(Texture texture) | |
{ | |
//Debug.Log("texture size: " + texture.width + ", " + texture.height); | |
_image.Texture = texture; | |
_image.Visible = false; | |
// reset explicit size to allow measurement | |
ExplicitWidth = null; | |
ExplicitHeight = null; | |
ValidateSize(true); | |
var newBounds = Rectangle.FromWidthAndHeight(GetExplicitOrMeasuredWidth(), GetExplicitOrMeasuredHeight()); | |
// center on screen | |
newBounds = newBounds.CenterInside(Rectangle.FromSize(SystemManager.Instance.ScreenSize)); | |
//Debug.Log("newBounds: " + newBounds); | |
// return to the old size immediatelly, because of the flicker | |
Width = oldBounds.Width; | |
Height = oldBounds.Height; | |
#region Alternative | |
//SetActualSize(GetExplicitOrMeasuredWidth(), GetExplicitOrMeasuredHeight()); | |
//PopupManager.Instance.CenterPopUp(this); | |
#endregion | |
Defer(delegate | |
{ | |
// tween the window size | |
//var newBounds = Bounds.CenterInside(Rectangle.FromSize(SystemManager.Instance.ScreenSize)); | |
ScaleBoundsTween tween = new ScaleBoundsTween(this, oldBounds, newBounds) | |
{ | |
Callback = delegate | |
{ | |
_image.Visible = true; // show image | |
//_image.SkipRender(2); // avoid flicker | |
_image.Alpha = 0; | |
// fade in the image | |
FadeIn fadeIn = new FadeIn | |
{ | |
Duration = 1 | |
}; | |
fadeIn.Play(_image); | |
} | |
}; | |
tween.Play(); | |
}, 1); | |
//GlobalLoadingMask.Hide(); | |
_mask.Unmask(); | |
CursorManager.Instance.RemoveCursor(cursor); | |
}); | |
} | |
} | |
} | |
} |
This file contains hidden or 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
/* | |
Copyright (c) 2010-2014 Danko Kozar | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
Note: | |
This is the source of the main frame of my Isometric experiment (from 2003) | |
The working demo is available at: http://dankokozar.com/flash/izometrija.html | |
I wrote this code BEFORE I ever knew about the existence of Object Oriented Programming :) | |
However, this is a very personal project that fired me into the (programming) orbit. ^^ | |
*/ | |
using System; | |
using System.Collections.Generic; | |
using Assets.eDriven.Demo.Gui.Code.Flickr; | |
using Assets.eDriven.Demo._shared.Code.Util; | |
using Assets.eDriven.Skins; | |
using eDriven.Animation; | |
using eDriven.Audio; | |
using eDriven.Core.Caching; | |
using eDriven.Core.Events; | |
using eDriven.Demo.Gui.Code.LoadData; | |
using eDriven.Gui; | |
using eDriven.Gui.Animation; | |
using eDriven.Gui.Components; | |
using eDriven.Gui.Cursor; | |
using eDriven.Gui.Data; | |
using eDriven.Gui.Events; | |
using eDriven.Gui.Layout; | |
using eDriven.Gui.Managers; | |
using eDriven.Gui.Plugins; | |
using eDriven.Networking.Rpc; | |
using eDriven.Networking.Rpc.Loaders; | |
using Assets.eDriven.Demo.Components; | |
using Assets.eDriven.Demo.Tweens; | |
using JsonFx.Json; | |
using UnityEngine; | |
using Action = eDriven.Animation.Action; | |
using Event = eDriven.Core.Events.Event; | |
using Random = System.Random; | |
namespace Assets.eDriven.Demo.Gui.Code.LoadData | |
{ | |
/// <summary> | |
/// </summary> | |
// ReSharper disable UnusedMember.Global | |
public class LoadData : global::eDriven.Gui.Gui | |
// ReSharper restore UnusedMember.Global | |
{ | |
#region Properties exposed to editor | |
public string FlickrAppKey = "19a0493934d366a8d534c578e9385b49"; // please use this API key for this demo only! | |
public string FlickrSearchUrl = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key={0}&text={1}&page={2}&per_page={3}&format=json&nojsoncallback=1"; | |
public bool CacheThumbnails; | |
public string ThumbnailSize = "q"; | |
public string ImageSize = "z"; | |
#endregion | |
#region Loaders | |
private readonly HttpConnector _httpConnector = new HttpConnector { ResponseMode = ResponseMode.WWW }; // loads data | |
private readonly TextureLoader _thumbnailLoader = new TextureLoader { Cache = false }; // loads textures | |
#endregion | |
#region Members | |
private ArrayList _dataProvider; | |
private readonly Random _random = new Random(); | |
private TextField _txtSearch; | |
private bool _newSearch; | |
private Scroller _scroller; | |
private List _list; | |
private readonly TweenFactory _windowEffect = new TweenFactory( | |
new Sequence( | |
new Action(delegate { AudioPlayerMapper.GetDefault().PlaySound("dialog_open"); }), | |
new FadeIn { Duration = 0.3f } | |
) | |
); | |
private readonly TweenFactory _scrollerShow = new TweenFactory( | |
new Sequence( | |
new ZeroFadeIn() | |
) | |
); | |
#endregion | |
protected override void OnInitialize() | |
{ | |
base.OnInitialize(); | |
Layout = new VerticalLayout | |
{ | |
HorizontalAlign = HorizontalAlign.Left, | |
Gap = 0 | |
}; | |
_thumbnailLoader.Connector.MaxConcurrentRequests = 3; | |
_thumbnailLoader.Connector.ConcurencyMode = ConcurencyMode.Multiple; | |
_thumbnailLoader.Cache = true; // cache thumbnails | |
} | |
override protected void CreateChildren() | |
{ | |
base.CreateChildren(); | |
#region Controls | |
Toolbar toolbar = new Toolbar(); | |
toolbar.Plugins.Add(new TabManager { ArrowsEnabled = true }); | |
AddChild(toolbar); | |
#region Text field | |
_txtSearch = new TextField | |
{ | |
StyleName = "search", | |
Text = "croatian coast", | |
FocusEnabled = true, | |
Width = 400 | |
}; | |
_txtSearch.SetFocus(); | |
_txtSearch.KeyUp += delegate(Event e) | |
{ | |
KeyboardEvent ke = (KeyboardEvent)e; | |
if (ke.KeyCode == KeyCode.Return) | |
{ | |
_newSearch = true; | |
Search(); | |
} | |
}; | |
toolbar.AddContentChild(_txtSearch); | |
#endregion | |
Button btnLoad = new Button | |
{ | |
Text = "Load data", | |
SkinClass = typeof(ImageButtonSkin), | |
Icon = ImageLoader.Instance.Load("Icons/arrow_refresh"), | |
AutoRepeat = true | |
}; | |
btnLoad.ButtonDown += delegate | |
{ | |
Search(); | |
btnLoad.Text = "Load more..."; | |
}; | |
toolbar.AddContentChild(btnLoad); | |
Button btnClear = new Button | |
{ | |
Text = "Remove all", | |
SkinClass = typeof(ImageButtonSkin), | |
Icon = ImageLoader.Instance.Load("Icons/cancel"), | |
AutoRepeat = true | |
}; | |
btnClear.ButtonDown += delegate | |
{ | |
_scroller.Visible = false; | |
_dataProvider.RemoveAll(); | |
btnLoad.Text = "Load data"; | |
}; | |
toolbar.AddContentChild(btnClear); | |
#endregion | |
#region Lower group | |
Group group = new Group | |
{ | |
PercentWidth = 100, | |
PercentHeight = 100 | |
}; | |
AddChild(group); | |
#endregion | |
#region Title label | |
Label label = new TitleLabel { HorizontalCenter = 0, VerticalCenter = 0, StyleName = "title" }; | |
group.AddChild(label); | |
new TextRotator | |
{ | |
Delay = 5, // 5 seconds delay | |
Lines = new[] | |
{ | |
"Load Data Demo", | |
"Created with eDriven.Gui", | |
"Loads images from Flickr"/*, | |
"Author: Danko Kozar"*/ | |
}, | |
Callback = delegate(string line) { label.Text = line; } | |
} | |
.Start(); | |
#endregion | |
#region Scroller / viewport | |
_scroller = new Scroller | |
{ | |
SkinClass = typeof (ScrollerSkin2), | |
PercentWidth = 100, PercentHeight = 100, | |
Visible = false | |
}; | |
_scroller.SetStyle("showEffect", _scrollerShow); | |
group.AddChild(_scroller); | |
Group viewport = new Group | |
{ | |
/*MouseEnabled = true, | |
Layout = new VerticalLayout | |
{ | |
HorizontalAlign = HorizontalAlign.Left, | |
PaddingLeft = 10, | |
PaddingRight = 10, | |
PaddingTop = 10, | |
PaddingBottom = 10, | |
Gap = 10 | |
}*/ | |
}; | |
_scroller.Viewport = viewport; | |
#endregion | |
#region Data controls | |
List<object> source = new List<object>(); | |
_dataProvider = new ArrayList(source); | |
/* LISTS */ | |
#region HGroup | |
#endregion | |
#region List | |
_list = new List | |
{ | |
Left = 10, Right = 10, Top = 10, Bottom = 10, | |
Layout = new TileLayout { HorizontalGap = 0, VerticalGap = 0/*, RequestedRowCount = 4, RequestedColumnCount = 3*/ }, | |
DataProvider = _dataProvider, | |
SkinClass = typeof(ListSkin2), | |
ItemRenderer = new ItemRendererFactory<ThumbnailItemRenderer>(), | |
LabelFunction = LabelFunction // extracting the title | |
}; | |
viewport.AddChild(_list); | |
#endregion | |
#endregion | |
// bubbling event listener | |
AddEventListener("showImage", ShowImageHandler); | |
} | |
#region Loading data | |
/// <summary> | |
/// Used by renderers to extract data to display | |
/// </summary> | |
/// <param name="item"></param> | |
/// <returns></returns> | |
private string LabelFunction(object item) | |
{ | |
return ((PhotoData)item).Title; | |
} | |
private void Search() | |
{ | |
if (_newSearch) | |
{ | |
_newSearch = false; | |
} | |
string url = string.Format( | |
FlickrSearchUrl, FlickrAppKey, | |
_txtSearch.Text.Replace(" ", "+"), | |
_random.Next(1, 20), /*1*/ // just a random page | |
20 | |
); | |
// make a Flickr request | |
GlobalLoadingMask.Show("Searching..."); | |
var cursor = CursorManager.Instance.SetCursor(CursorType.Wait); | |
_httpConnector.Send( | |
url, | |
delegate(object data) | |
{ | |
GlobalLoadingMask.Hide(); | |
CursorManager.Instance.RemoveCursor(cursor); | |
_scroller.Visible = true; | |
_list.SetFocus(); | |
DisplayData(data); | |
} | |
); | |
} | |
private void DisplayData(object result) | |
{ | |
//Debug.Log("Displaying data"); | |
string json = ((WWW)result).text; //Debug.Log(json); | |
try // deserialize | |
{ | |
PhotosSearchResponse response = (PhotosSearchResponse)JsonReader.Deserialize(json, typeof(PhotosSearchResponse)); | |
if (response.Stat != "ok") | |
{ | |
Alert.Show("Error", response.Message, AlertButtonFlag.Ok); | |
return; | |
} | |
var count = 0; | |
// load images | |
response.Photos.Photo.ForEach(delegate(Photo photo) | |
{ | |
var data = new PhotoData(photo.Title) | |
{ | |
ImageUrl = photo.GetUrl(ImageSize) | |
}; | |
_dataProvider.AddItemAt(data, count); // add it to the beginning off the list | |
// load thumbnails asynchronously, then update each item | |
_thumbnailLoader.Load(photo.GetUrl(ThumbnailSize), delegate(Texture texture) | |
{ | |
data.Thumbnail = texture; | |
_dataProvider.ItemUpdated(data, "Thumbnail", null, texture); | |
}); | |
count++; | |
}); | |
} | |
catch (Exception) | |
{ | |
Alert.Show("Error", "No images found", AlertButtonFlag.Ok); | |
} | |
} | |
private void ShowImageHandler(Event e) | |
{ | |
var data = (PhotoData)((IDataRenderer)e.Target).Data; | |
var window = new ImageViewer | |
{ | |
Title = data.Title, | |
ImageUrl = data.ImageUrl, | |
SkinClass = typeof(WindowSkin2), | |
Icon = ImageLoader.Instance.Load("Icons/star") | |
}; | |
window.SetStyle("addedEffect", _windowEffect); | |
window.Plugins.Add(new Resizable { ShowOverlay = false }); | |
window.AddEventListener(CloseEvent.CLOSE, delegate | |
{ | |
PopupManager.Instance.RemovePopup(window); | |
}); | |
PopupManager.Instance.AddPopup(window, false); | |
PopupManager.Instance.CenterPopUp(window); | |
} | |
#endregion | |
} | |
} |
This file contains hidden or 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
/* | |
Copyright (c) 2010-2014 Danko Kozar | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
Note: | |
This is the source of the main frame of my Isometric experiment (from 2003) | |
The working demo is available at: http://dankokozar.com/flash/izometrija.html | |
I wrote this code BEFORE I ever knew about the existence of Object Oriented Programming :) | |
However, this is a very personal project that fired me into the (programming) orbit. ^^ | |
*/ | |
using UnityEngine; | |
namespace Assets.eDriven.Demo.Gui.Code.LoadData | |
{ | |
internal class PhotoData | |
{ | |
public string Title; | |
public Texture Thumbnail; | |
// the URL of the actual (big) image, loaded on request | |
public string ImageUrl; | |
public PhotoData() | |
{ | |
} | |
public PhotoData(string title) | |
{ | |
Title = title; | |
} | |
} | |
} |
This file contains hidden or 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
/* | |
Copyright (c) 2010-2014 Danko Kozar | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
Note: | |
This is the source of the main frame of my Isometric experiment (from 2003) | |
The working demo is available at: http://dankokozar.com/flash/izometrija.html | |
I wrote this code BEFORE I ever knew about the existence of Object Oriented Programming :) | |
However, this is a very personal project that fired me into the (programming) orbit. ^^ | |
*/ | |
using System.Collections; | |
using Assets.eDriven.Demo.Gui.Code.LoadData; | |
using Assets.eDriven.Skins; | |
using eDriven.Core.Caching; | |
using eDriven.Core.Events; | |
using eDriven.Gui.Components; | |
using eDriven.Gui.Data; | |
using eDriven.Gui.Layout; | |
using eDriven.Gui.Shapes; | |
using eDriven.Gui.Styles; | |
using UnityEngine; | |
using Component = eDriven.Gui.Components.Component; | |
using Event = eDriven.Core.Events.Event; | |
namespace eDriven.Demo.Gui.Code.LoadData | |
{ | |
[Style(Name = "textColor", Type = typeof(Color), Default = 0x222222)] | |
[Style(Name = "backgroundColor", Type = typeof(Color), Default = 0xffffff)] | |
[Style(Name = "rollOverColor", Type = typeof(Color), Default = 0xdeeeff)] | |
[Style(Name = "caretColor", Type = typeof(Color), Default = 0xdeeeff)] | |
[Style(Name = "selectionColor", Type = typeof(Color), Default = 0xaaccee)] | |
public class ThumbnailItemRenderer : Component, IItemRenderer | |
{ | |
private static readonly Hashtable ButtonStyles = new Hashtable { { "cursor", "pointer" } }; | |
public ThumbnailItemRenderer() | |
{ | |
AddEventListener(MouseEvent.ROLL_OVER, ItemRendererRollOverHandler, EventPhase.CaptureAndTarget); | |
AddEventListener(MouseEvent.ROLL_OUT, ItemRendererRollOutHandler, EventPhase.CaptureAndTarget); | |
MinWidth = 255; | |
} | |
#region Implementation of IItemRenderer | |
private int _itemIndex; | |
///<summary> | |
/// Item index | |
///</summary> | |
public int ItemIndex | |
{ | |
get { return _itemIndex; } | |
set | |
{ | |
if (value == _itemIndex) | |
return; | |
_itemIndex = value; | |
InvalidateDisplayList(); | |
} | |
} | |
private bool _dragging; | |
///<summary> | |
///</summary> | |
public bool Dragging | |
{ | |
get { return _dragging; } | |
set { _dragging = value; } | |
} | |
private string _text = string.Empty; | |
///<summary> | |
///</summary> | |
public string Text | |
{ | |
get { return _text; } | |
set | |
{ | |
if (value == _text) | |
return; | |
_text = value; | |
if (null != LabelDisplay) | |
LabelDisplay.Text = _text; | |
} | |
} | |
private HGroup _hGroup; | |
//---------------------------------- | |
// labelDisplay | |
//---------------------------------- | |
public TextBase LabelDisplay; | |
private Button _buttonShow; | |
private Button _buttonRemove; | |
private bool _selected; | |
///<summary> | |
///</summary> | |
public bool Selected | |
{ | |
get { return _selected; } | |
set | |
{ | |
if (value != _selected) | |
{ | |
_selected = value; | |
InvalidateDisplayList(); | |
} | |
} | |
} | |
private bool _showsCaret; | |
///<summary> | |
///</summary> | |
public bool ShowsCaret | |
{ | |
get { return _showsCaret; } | |
set | |
{ | |
if (value == _showsCaret) | |
return; | |
_showsCaret = value; | |
InvalidateDisplayList(); | |
} | |
} | |
private bool _hovered; | |
#endregion | |
private object _data; | |
public override object Data | |
{ | |
get { | |
return _data; | |
} | |
set | |
{ | |
_data = value; | |
DispatchEvent(new FrameworkEvent(FrameworkEvent.DATA_CHANGE)); | |
if (null != _data) { | |
LabelDisplay.Text = ((PhotoData)_data).Title; | |
_image.Texture = ((PhotoData)_data).Thumbnail; | |
} | |
} | |
} | |
private RectShape _rect; | |
private Image _image; | |
protected override void CreateChildren() | |
{ | |
base.CreateChildren(); | |
_rect = new RectShape(); //{ Left = 0, Right = 0, Top = 0, Bottom = 0 //}; | |
AddChild(_rect); | |
_hGroup = new HGroup | |
{ | |
PaddingLeft = 10, PaddingRight = 10, PaddingTop = 10, PaddingBottom = 10, Gap = 10 | |
}; | |
AddChild(_hGroup); | |
// left group | |
VGroup vGroup = new VGroup | |
{ | |
VerticalAlign = VerticalAlign.Middle, | |
Gap = 10, | |
PercentWidth = 100, | |
PercentHeight = 100 | |
}; | |
_hGroup.AddChild(vGroup); | |
_image = new Image | |
{ | |
Styles = ButtonStyles | |
}; | |
_image.MouseDown += delegate | |
{ | |
DispatchEvent(new Event("showImage", true)); // bubbling event | |
}; | |
vGroup.AddChild(_image); | |
LabelDisplay = new Label | |
{ | |
Width = 150 | |
}; | |
//vGroup.AddChild(LabelDisplay); | |
if (_text != string.Empty) | |
LabelDisplay.Text = _text; | |
// right group | |
vGroup = new VGroup | |
{ | |
VerticalAlign = VerticalAlign.Middle, Gap = 10, PercentWidth = 100, PercentHeight = 100 | |
}; | |
_hGroup.AddChild(vGroup); | |
if (null == _buttonShow) | |
{ | |
_buttonShow = new Button { | |
Text = "Show", | |
PercentWidth = 100, | |
FocusEnabled = false, | |
SkinClass = typeof(ImageButtonSkin), | |
Styles = ButtonStyles, | |
Icon = ImageLoader.Instance.Load("Icons/accept") | |
}; | |
_buttonShow.ButtonDown += delegate | |
{ | |
DispatchEvent(new Event("showImage", true)); // bubbling! | |
}; | |
vGroup.AddChild(_buttonShow); | |
} | |
if (null == _buttonRemove) | |
{ | |
_buttonRemove = new Button | |
{ | |
Text = "Remove", | |
PercentWidth = 100, | |
FocusEnabled = false, | |
SkinClass = typeof(ImageButtonSkin), | |
Styles = ButtonStyles, | |
Icon = ImageLoader.Instance.Load("Icons/cancel") | |
}; | |
_buttonRemove.ButtonDown += delegate | |
{ | |
var parentList = Owner as List; | |
if (null != parentList) | |
{ | |
//Debug.Log("Removing at " + parentList.DataProvider.GetItemIndex(Data)); | |
parentList.DataProvider.RemoveItemAt(parentList.DataProvider.GetItemIndex(Data)); | |
} | |
else | |
Debug.LogError("Owner of item renderer is not a list"); | |
}; | |
vGroup.AddChild(_buttonRemove); | |
} | |
} | |
protected override void Measure() | |
{ | |
base.Measure(); | |
// label has padding of 3 on left and right and padding of 5 on top and bottom. | |
MeasuredWidth = _hGroup.GetExplicitOrMeasuredWidth(); // +6; | |
MeasuredHeight = _hGroup.GetExplicitOrMeasuredHeight(); // +10; | |
MeasuredMinWidth = LayoutUtil.GetMinBoundsWidth(_hGroup); // + 6; | |
MeasuredMinHeight = LayoutUtil.GetMinBoundsHeight(_hGroup); // + 10; | |
} | |
protected override void UpdateDisplayList(float width, float height) | |
{ | |
base.UpdateDisplayList(width, height); | |
LabelDisplay.Color = (Color) GetStyle("textColor"); | |
if (_selected) | |
_rect.BackgroundColor = (Color)GetStyle("selectionColor"); | |
else if (ShowsCaret) | |
_rect.BackgroundColor = (Color)GetStyle("caretColor"); | |
else if (_hovered) | |
_rect.BackgroundColor = (Color)GetStyle("rollOverColor"); | |
else | |
_rect.BackgroundColor = (Color)GetStyle("backgroundColor"); | |
_rect.SetActualSize(width, height); // (this is a component - no layout, so...) | |
_hGroup.SetActualSize(width, height); // (this is a component - no layout, so...) | |
} | |
/** | |
* @private | |
* Mouse rollOver event handler. | |
*/ | |
// ReSharper disable MemberCanBePrivate.Global | |
protected void ItemRendererRollOverHandler(Event e) | |
// ReSharper restore MemberCanBePrivate.Global | |
{ | |
//Debug.Log("Roll over"); | |
_hovered = true; | |
InvalidateDisplayList(); | |
} | |
/** | |
* @private | |
* Mouse rollOut event handler. | |
*/ | |
// ReSharper disable MemberCanBePrivate.Global | |
protected void ItemRendererRollOutHandler(Event e) | |
// ReSharper restore MemberCanBePrivate.Global | |
{ | |
_hovered = false; | |
InvalidateDisplayList(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment