mirror of
https://github.com/scratchfoundation/scratch-flash.git
synced 2024-12-04 21:21:06 -05:00
Added a delete button to list cells
This button can only be pressed when the cell is focused. There was an annoying bug in Scratch 1.4 where you could delete cells just by clicking on the empty space on the right side of the cell.
This commit is contained in:
parent
464401fada
commit
9ad9ffc910
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,9 +20,10 @@
|
|||
package watchers {
|
||||
import flash.display.Sprite;
|
||||
import flash.events.*;
|
||||
import flash.utils.*;
|
||||
import flash.geom.ColorTransform;
|
||||
import flash.text.*;
|
||||
import uiwidgets.ResizeableFrame;
|
||||
import uiwidgets.*;
|
||||
|
||||
public class ListCell extends Sprite {
|
||||
|
||||
|
@ -30,12 +31,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, Specs.listColor, 6, true);
|
||||
addChild(frame);
|
||||
addTextField(whenChanged, keyPress);
|
||||
tf.text = s;
|
||||
deleteButton = new IconButton(deleteItem, 'deleteItem');
|
||||
setWidth(width);
|
||||
}
|
||||
|
||||
|
@ -43,6 +47,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 {
|
||||
|
@ -53,6 +58,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 {
|
||||
|
@ -81,5 +88,15 @@ public class ListCell extends Sprite {
|
|||
var hasFocus:Boolean = (e.type == FocusEvent.FOCUS_IN);
|
||||
frame.transform.colorTransform = (hasFocus ? focused : normal);
|
||||
tf.textColor = (hasFocus ? 0 : 0xFFFFFF);
|
||||
setTimeout(hasFocus ? 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