1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
|
// -----------------------------------------------------------------------------
// File: carousel.ss
// Description: carousel UI component (Debug Mode)
// Author: Alexandre Martins <http://opensurge2d.org>
// License: MIT
// -----------------------------------------------------------------------------
/*
This UI component lets you pick items in a Carousel.
*/
using SurgeEngine.Video.Screen;
using SurgeEngine.Transform;
using SurgeEngine.Vector2;
using SurgeEngine;
object "Debug Mode - Carousel" is "debug-mode-ui-component", "iterable", "detached", "private", "entity"
{
public readonly transform = Transform();
itemContainers = [];
itemHeight = 32;
itemWidth = 32;
uiScroller = spawn("Debug Mode - UI Scroller");
zindex = 0;
state "main"
{
// update the position and the size of the scroller
uiScroller.setActiveArea(0, transform.position.y, Screen.width, itemHeight);
// scroll the items
if(Math.abs(uiScroller.dx) > 0.1) {
transform.translateBy(uiScroller.dx, 0);
// keep the scroll within the boundaries of the carousel
width = itemWidth * itemContainers.length - Screen.width;
if(transform.localPosition.x > 0 || width <= 0)
transform.translateBy(-transform.localPosition.x, 0);
else if(width > 0 && transform.localPosition.x < -width)
transform.translateBy(-transform.localPosition.x - width, 0);
}
}
fun add(itemBuilder)
{
itemContainer = spawn("Debug Mode - Carousel Item Container");
item = itemBuilder.build(itemContainer);
itemContainer.init(item, itemContainers.length, itemWidth, itemHeight, zindex, this);
itemContainers.push(itemContainer);
return this;
}
fun clear()
{
foreach(container in itemContainers)
container.destroy();
itemContainers.clear();
return this;
}
fun get_width()
{
return itemWidth * itemContainers.length;
}
fun get_height()
{
return itemHeight;
}
fun iterator()
{
return spawn("Debug Mode - Carousel Iterator").setItemContainers(itemContainers);
}
fun setItemSize(width, height)
{
itemWidth = width;
itemHeight = height;
return this;
}
fun onLoad(debugMode)
{
tapDetector = debugMode.plugin("Debug Mode - Tap Detector");
tapDetector.subscribe(this);
uiSettings = debugMode.plugin("Debug Mode - UI Settings");
zindex = uiSettings.zindex + 500;
}
fun onUnload(debugMode)
{
tapDetector = debugMode.plugin("Debug Mode - Tap Detector");
tapDetector.unsubscribe(this);
}
fun onTapScreen(position)
{
// pick an item
if(uiScroller.isInActiveArea(position)) {
itemIndex = Math.floor((position.x - transform.localPosition.x) / itemWidth);
if(itemIndex >= 0 && itemIndex < itemContainers.length) {
// get the picked item
pickedCarouselItem = itemContainers[itemIndex].item;
// dehighlight all items
foreach(item in this)
item.highlighted = false;
// highlight the picked item
pickedCarouselItem.highlighted = true;
// callback
parent.onPickCarouselItem(pickedCarouselItem);
}
}
}
}
object "Debug Mode - Carousel Item" is "debug-mode-carousel-item", "detached", "private", "entity"
{
public carousel = null;
public zindex = 0;
public highlighted = false;
public readonly naturalWidth = 1;
public readonly naturalHeight = 1;
}
object "Debug Mode - Carousel Item Builder" is "debug-mode-carousel-item-builder"
{
fun build(itemContainer)
{
return null;
//return itemContainer.spawn("...");
}
}
object "Debug Mode - Carousel Item Container" is "detached", "private", "entity"
{
transform = Transform();
item = null; // another carousel item
fun get_item()
{
return item;
}
fun init(itemReference, itemIndex, itemWidth, itemHeight, itemZindex, carousel)
{
item = itemReference;
item.highlighted = false;
item.zindex = itemZindex;
item.carousel = carousel;
sx = itemWidth / item.naturalWidth;
sy = itemHeight / item.naturalHeight;
scale = Math.min(sx, sy);
transform.localScale = Vector2(scale, scale);
transform.localPosition = Vector2(itemWidth * itemIndex, 0);
return this;
}
}
object "Debug Mode - Carousel Iterator" is "iterator"
{
itemContainers = null;
index = 0;
fun setItemContainers(array)
{
itemContainers = array;
return this;
}
fun hasNext()
{
return index < itemContainers.length;
}
fun next()
{
if(!hasNext())
return null;
return itemContainers[index++].item;
}
}
|