mirror of
https://github.com/scratchfoundation/scratch-flash.git
synced 2024-12-04 21:21:06 -05:00
Merge pull request #222 from nathan/list-delete-button
List delete button
This commit is contained in:
commit
8c0a53054a
5 changed files with 48 additions and 3 deletions
|
@ -104,6 +104,8 @@ public class Resources {
|
|||
[Embed(source='UI/buttons/checkboxOn.gif')] private static const checkboxOn:Class;
|
||||
[Embed(source='UI/buttons/closeOff.gif')] private static const closeOff:Class;
|
||||
[Embed(source='UI/buttons/closeOn.gif')] private static const closeOn:Class;
|
||||
[Embed(source='UI/buttons/deleteItemOff.png')] private static const deleteItemOff:Class;
|
||||
[Embed(source='UI/buttons/deleteItemOn.png')] private static const deleteItemOn:Class;
|
||||
[Embed(source='UI/buttons/extensionHelpOff.png')] private static const extensionHelpOff:Class;
|
||||
[Embed(source='UI/buttons/extensionHelpOn.png')] private static const extensionHelpOn:Class;
|
||||
[Embed(source='UI/buttons/flipOff.png')] private static const flipOff:Class;
|
||||
|
|
BIN
src/assets/UI/buttons/deleteItemOff.png
Normal file
BIN
src/assets/UI/buttons/deleteItemOff.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 404 B |
BIN
src/assets/UI/buttons/deleteItemOn.png
Normal file
BIN
src/assets/UI/buttons/deleteItemOn.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 411 B |
|
@ -20,8 +20,9 @@
|
|||
package watchers {
|
||||
import flash.display.Sprite;
|
||||
import flash.events.*;
|
||||
import flash.utils.*;
|
||||
import flash.text.*;
|
||||
import uiwidgets.ResizeableFrame;
|
||||
import uiwidgets.*;
|
||||
import util.Color;
|
||||
|
||||
public class ListCell extends Sprite {
|
||||
|
@ -32,12 +33,15 @@ public class ListCell extends Sprite {
|
|||
|
||||
public var tf:TextField;
|
||||
private var frame:ResizeableFrame;
|
||||
private var deleteButton:IconButton;
|
||||
private var deleteItem:Function;
|
||||
|
||||
public function ListCell(s:String, width:int, whenChanged:Function, keyPress:Function) {
|
||||
public function ListCell(s:String, width:int, whenChanged:Function, keyPress:Function, deleteItem:Function) {
|
||||
frame = new ResizeableFrame(0xFFFFFF, normalColor, 6, true);
|
||||
addChild(frame);
|
||||
addTextField(whenChanged, keyPress);
|
||||
tf.text = s;
|
||||
deleteButton = new IconButton(deleteItem, 'deleteItem');
|
||||
setWidth(width);
|
||||
}
|
||||
|
||||
|
@ -45,6 +49,7 @@ public class ListCell extends Sprite {
|
|||
// Set the text and, optionally, the width.
|
||||
tf.text = s;
|
||||
setWidth((w > 0) ? w : frame.w);
|
||||
removeDeleteButton();
|
||||
}
|
||||
|
||||
public function setEditable(isEditable:Boolean):void {
|
||||
|
@ -55,6 +60,8 @@ public class ListCell extends Sprite {
|
|||
tf.width = Math.max(w, 15); // forces line wrapping, possibly changing tf.height
|
||||
var frameH:int = Math.max(tf.textHeight + 7, 20);
|
||||
frame.setWidthHeight(tf.width, frameH);
|
||||
deleteButton.x = tf.width - deleteButton.width - 3;
|
||||
deleteButton.y = (frameH - deleteButton.height) / 2;
|
||||
}
|
||||
|
||||
private function addTextField(whenChanged:Function, keyPress:Function):void {
|
||||
|
@ -83,5 +90,15 @@ public class ListCell extends Sprite {
|
|||
var hasFocus:Boolean = (e.type == FocusEvent.FOCUS_IN);
|
||||
frame.setColor(hasFocus ? focusedColor : normalColor);
|
||||
tf.textColor = (hasFocus ? 0 : 0xFFFFFF);
|
||||
setTimeout(hasFocus && tf.type == 'input' ? addDeleteButton : removeDeleteButton, 1);
|
||||
}
|
||||
|
||||
private function removeDeleteButton():void {
|
||||
if (deleteButton.parent) removeChild(deleteButton);
|
||||
}
|
||||
|
||||
private function addDeleteButton():void {
|
||||
addChild(deleteButton);
|
||||
deleteButton.turnOff();
|
||||
}
|
||||
}}
|
||||
|
|
|
@ -339,6 +339,32 @@ public class ListWatcher extends Sprite {
|
|||
if (e.relatedObject != null) insertionIndex = -1;
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Delete Item Button Support
|
||||
//------------------------------
|
||||
|
||||
private function deleteItem(b:IconButton):void {
|
||||
var cell:ListCell = b.lastEvent.target.parent as ListCell;
|
||||
if (cell == null) return;
|
||||
for (var i:int = 0; i < visibleCells.length; i++) {
|
||||
var c:ListCell = visibleCells[i];
|
||||
if (c == cell) {
|
||||
var j:int = firstVisibleIndex + i;
|
||||
contents.splice(j, 1);
|
||||
if (j == contents.length && visibleCells.length == 1) {
|
||||
scrollToIndex(j - 1);
|
||||
} else {
|
||||
updateContents();
|
||||
updateScrollbar();
|
||||
}
|
||||
if (visibleCells.length) {
|
||||
selectCell(Math.min(j, contents.length - 1));
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------
|
||||
// Layout
|
||||
//------------------------------
|
||||
|
@ -470,7 +496,7 @@ public class ListWatcher extends Sprite {
|
|||
private function allocateCell(s:String, width:int):ListCell {
|
||||
// Allocate a ListCell with the given contents and width.
|
||||
// Recycle one from the cell pool if possible.
|
||||
if (cellPool.length == 0) return new ListCell(s, width, textChanged, keyPress);
|
||||
if (cellPool.length == 0) return new ListCell(s, width, textChanged, keyPress, deleteItem);
|
||||
var result:ListCell = cellPool.pop();
|
||||
result.setText(s, width);
|
||||
return result;
|
||||
|
|
Loading…
Reference in a new issue