Extend multiple image resource selection to all BoxItemResource items

This commit is contained in:
fgnm
2021-07-11 09:35:19 +02:00
parent 619f64e773
commit 99cf8fa1e0
10 changed files with 100 additions and 82 deletions
+1 -1
View File
@@ -11,4 +11,4 @@ damios (@crykn) - https://github.com/crykn
Raymond Buckley (@raeleus) - https://github.com/raeleus
alyrow (@alyrow) - https://github.com/alyrow
6money (@6money) - https://github.com/6money
jantwegener (@jantwegener) - https://github.com/jantwegener
Jan-Thierry Wegener (@jantwegener) - https://github.com/jantwegener
@@ -28,7 +28,7 @@ import games.rednblack.editor.view.menu.HyperLap2DMenuBarMediator;
import games.rednblack.editor.view.stage.SandboxMediator;
import games.rednblack.editor.view.stage.UIStageMediator;
import games.rednblack.editor.view.ui.FollowersUIMediator;
import games.rednblack.editor.view.ui.ImageResourceSelectionUIMediator;
import games.rednblack.editor.view.ui.BoxItemResourceSelectionUIMediator;
import games.rednblack.editor.view.ui.RulersUIMediator;
import games.rednblack.editor.view.ui.StickyNotesUIMediator;
import games.rednblack.editor.view.ui.UIDropDownMenuMediator;
@@ -93,6 +93,9 @@ public class BootstrapViewCommand extends SimpleCommand {
facade.registerMediator(new FollowersUIMediator());
facade.registerMediator(new StickyNotesUIMediator());
// Multiple selection behavior for box resources
facade.registerMediator(new BoxItemResourceSelectionUIMediator());
facade.registerMediator(new UIAlignBoxMediator());
facade.registerMediator(new UIItemsTreeBoxMediator());
facade.registerMediator(new UIMultiPropertyBoxMediator());
@@ -101,9 +104,6 @@ public class BootstrapViewCommand extends SimpleCommand {
facade.registerMediator(new UIStageMediator());
facade.registerMediator(new SandboxMediator());
facade.registerMediator(new UIDropDownMenuMediator());
// improved selection behavior for the image panel
facade.registerMediator(new ImageResourceSelectionUIMediator());
//Panels
facade.registerMediator(new ImportPanelMediator());
@@ -6,6 +6,8 @@ import java.util.SortedSet;
import java.util.TreeSet;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.ObjectSet;
import games.rednblack.editor.view.ui.box.resourcespanel.draggable.box.BoxItemResource;
import org.puremvc.java.interfaces.INotification;
import org.puremvc.java.patterns.mediator.Mediator;
@@ -18,7 +20,6 @@ import com.kotcrab.vis.ui.widget.VisTable;
import games.rednblack.editor.HyperLap2DFacade;
import games.rednblack.editor.view.ui.box.UIResourcesBoxMediator;
import games.rednblack.editor.view.ui.box.resourcespanel.UIImagesTab;
import games.rednblack.editor.view.ui.box.resourcespanel.draggable.box.ImageResource;
import games.rednblack.h2d.common.MsgAPI;
/**
@@ -26,20 +27,20 @@ import games.rednblack.h2d.common.MsgAPI;
*
* @author Jan-Thierry Wegener
*/
public class ImageResourceSelectionUIMediator extends Mediator<ImageResource> {
public class BoxItemResourceSelectionUIMediator extends Mediator<BoxItemResource> {
public static final String NAME = ImageResourceSelectionUIMediator.class.getCanonicalName();
public static final String NAME = BoxItemResourceSelectionUIMediator.class.getCanonicalName();
private final SortedSet<String> imageResourceSelectedSet = new TreeSet<>();
private final SortedSet<String> boxResourceSelectedSet = new TreeSet<>();
/**
* The image table from {@link UIImagesTab}. This is the table we add our selection behavior to.
*/
private VisTable imagesTable;
private ObjectSet<VisTable> boxesTableSet = new ObjectSet<>();
private ImageResource imageResourcePreviousClick;
private BoxItemResource boxResourcePreviousClick;
public ImageResourceSelectionUIMediator() {
public BoxItemResourceSelectionUIMediator() {
super(NAME);
}
@@ -51,9 +52,9 @@ public class ImageResourceSelectionUIMediator extends Mediator<ImageResource> {
@Override
public String[] listNotificationInterests() {
return new String[]{
UIResourcesBoxMediator.IMAGE_LEFT_CLICK,
UIResourcesBoxMediator.RESOURCE_BOX_LEFT_CLICK,
MsgAPI.IMAGE_BUNDLE_DROP,
UIResourcesBoxMediator.IMAGE_TABLE_UPDATED,
UIResourcesBoxMediator.ADD_RESOURCES_BOX_TABLE_SELECTION_MANAGEMENT,
UIResourcesBoxMediator.SANDBOX_DRAG_IMAGE_ENTER,
UIResourcesBoxMediator.SANDBOX_DRAG_IMAGE_EXIT
};
@@ -64,8 +65,8 @@ public class ImageResourceSelectionUIMediator extends Mediator<ImageResource> {
super.handleNotification(notification);
switch (notification.getName()) {
case UIResourcesBoxMediator.IMAGE_LEFT_CLICK:
ImageResource imageResource = notification.getBody();
case UIResourcesBoxMediator.RESOURCE_BOX_LEFT_CLICK:
BoxItemResource imageResource = notification.getBody();
switch (notification.getType()) {
case UIResourcesBoxMediator.NORMAL_CLICK_EVENT_TYPE:
handleNormalClick(imageResource);
@@ -81,10 +82,10 @@ public class ImageResourceSelectionUIMediator extends Mediator<ImageResource> {
break;
}
imageResourcePreviousClick = imageResource;
boxResourcePreviousClick = imageResource;
break;
case MsgAPI.IMAGE_BUNDLE_DROP:
Set<String> nameSet = new HashSet<>(imageResourceSelectedSet);
Set<String> nameSet = new HashSet<>(boxResourceSelectedSet);
// remove the dropped one, so that it is not added twice
Object[] payloadBody = notification.getBody();
nameSet.remove(payloadBody[0]);
@@ -95,8 +96,8 @@ public class ImageResourceSelectionUIMediator extends Mediator<ImageResource> {
HyperLap2DFacade.getInstance().sendNotification(MsgAPI.IMAGE_BUNDLE_DROP_SINGLE, new Object[] {name, payloadBody[1]});
}
break;
case UIResourcesBoxMediator.IMAGE_TABLE_UPDATED:
imagesTable = notification.getBody();
case UIResourcesBoxMediator.ADD_RESOURCES_BOX_TABLE_SELECTION_MANAGEMENT:
boxesTableSet.add(notification.getBody());
break;
case UIResourcesBoxMediator.SANDBOX_DRAG_IMAGE_ENTER:
Source sourceEnter = notification.getBody();
@@ -114,14 +115,16 @@ public class ImageResourceSelectionUIMediator extends Mediator<ImageResource> {
/**
* Darkens all the resources except the given one.
*
* @param imageResource The dragged image resource.
* @param boxResource The dragged image resource.
* @param color The color to set to all other resources.
*/
private void setColorExcept(Color color, Actor imageResource) {
for (Cell<ImageResource> cell : imagesTable.getCells()) {
Actor imgResource = cell.getActor();
if (!imgResource.equals(imageResource)) {
imgResource.setColor(color);
private void setColorExcept(Color color, Actor boxResource) {
for (VisTable boxesTable : boxesTableSet) {
for (Cell<BoxItemResource> cell : boxesTable.getCells()) {
Actor imgResource = cell.getActor();
if (!imgResource.equals(boxResource)) {
imgResource.setColor(color);
}
}
}
}
@@ -130,17 +133,17 @@ public class ImageResourceSelectionUIMediator extends Mediator<ImageResource> {
* Handles a shift-click on the panel. This means that the selection is cleared, and all resources between the
* previously clicked and currently clicked resources are selected / unselected.
*
* @param imageResource The clicked image resource.
* @param boxResource The clicked image resource.
*/
private void handleShiftClick(ImageResource imageResource) {
boolean removed = imageResourceSelectedSet.remove(imageResource.getPayloadData().name);
private void handleShiftClick(BoxItemResource boxResource) {
boolean removed = boxResourceSelectedSet.remove(boxResource.getPayloadData().name);
clearSelection();
if (removed) {
setSelectionBetween(imageResourcePreviousClick, imageResource, false);
setSelectionBetween(boxResourcePreviousClick, boxResource, false);
} else {
setSelectionBetween(imageResourcePreviousClick, imageResource, true);
setSelectionBetween(boxResourcePreviousClick, boxResource, true);
}
}
@@ -148,58 +151,58 @@ public class ImageResourceSelectionUIMediator extends Mediator<ImageResource> {
* Handles a shift-ctrl-click on the panel. This means that the selection is kept, and all resources between the
* previously clicked and currently clicked resources are selected / unselected.
*
* @param imageResource The clicked image resource.
* @param boxResource The clicked image resource.
*/
private void handleShiftCtrlClick(ImageResource imageResource) {
if (imageResourceSelectedSet.remove(imageResource.getPayloadData().name)) {
setSelectionBetween(imageResourcePreviousClick, imageResource, false);
private void handleShiftCtrlClick(BoxItemResource boxResource) {
if (boxResourceSelectedSet.remove(boxResource.getPayloadData().name)) {
setSelectionBetween(boxResourcePreviousClick, boxResource, false);
} else {
setSelectionBetween(imageResourcePreviousClick, imageResource, true);
setSelectionBetween(boxResourcePreviousClick, boxResource, true);
}
}
/**
* Handles a ctrl-click on the panel. This means that the selection is kept, and the currently clicked resource is selected / unselected.
*
* @param imageResource The clicked image resource.
* @param boxResource The clicked image resource.
*/
private void handleCtrlClick(ImageResource imageResource) {
if (imageResourceSelectedSet.remove(imageResource.getPayloadData().name)) {
private void handleCtrlClick(BoxItemResource boxResource) {
if (boxResourceSelectedSet.remove(boxResource.getPayloadData().name)) {
// we unselected the cell
setSelected(imageResource, false);
setSelected(boxResource, false);
} else {
// we selected the cell
setSelected(imageResource, true);
setSelected(boxResource, true);
}
}
/**
* Handles a click on the panel. This means that the selection is cleared, and the resource currently clicked resources are selected.
*
* @param imageResource The clicked image resource.
* @param boxResource The clicked image resource.
*/
private void handleNormalClick(ImageResource imageResource) {
private void handleNormalClick(BoxItemResource boxResource) {
clearSelection();
// we selected the cell
setSelected(imageResource, true);
setSelected(boxResource, true);
}
/**
* Selects or unselets the given image resource.
*
* @param imageResource The clicked image resource.
* @param boxResource The clicked image resource.
* @param isSelected Whether to select (true) or unselect (false) the given resource.
*/
private void setSelected(ImageResource imageResource, boolean isSelected) {
private void setSelected(BoxItemResource boxResource, boolean isSelected) {
if (isSelected) {
imageResource.switchToMouseOverColor();
imageResource.setHighlightWhenMouseOver(false);
imageResourceSelectedSet.add(imageResource.getPayloadData().name);
boxResource.switchToMouseOverColor();
boxResource.setHighlightWhenMouseOver(false);
boxResourceSelectedSet.add(boxResource.getPayloadData().name);
} else {
imageResource.switchToStandardColor();
imageResource.setHighlightWhenMouseOver(true);
imageResourceSelectedSet.remove(imageResource.getPayloadData().name);
boxResource.switchToStandardColor();
boxResource.setHighlightWhenMouseOver(true);
boxResourceSelectedSet.remove(boxResource.getPayloadData().name);
}
}
@@ -207,38 +210,52 @@ public class ImageResourceSelectionUIMediator extends Mediator<ImageResource> {
* Clears the selection.
*/
private void clearSelection() {
for (Cell<ImageResource> cell : imagesTable.getCells()) {
ImageResource imgResource = cell.getActor();
setSelected(imgResource, false);
for (VisTable boxesTable : boxesTableSet) {
for (Cell<BoxItemResource> cell : boxesTable.getCells()) {
BoxItemResource imgResource = cell.getActor();
setSelected(imgResource, false);
}
}
}
private int getCellIndex(ImageResource imageResource, int defaultIndex) {
Cell<ImageResource> cell = imagesTable.getCell(imageResource);
private int getCellIndex(BoxItemResource boxResource, int defaultIndex, VisTable boxesTable) {
Cell<BoxItemResource> cell = boxesTable.getCell(boxResource);
int index = defaultIndex;
if (cell != null) {
// compute the indixes, should be faster than iterating over the array of cells
index = cell.getRow() * imagesTable.getColumns() + cell.getColumn();
index = cell.getRow() * boxesTable.getColumns() + cell.getColumn();
}
return index;
}
private void setSelectionBetween(ImageResource imageResourceStart, ImageResource imageResourceEnd, boolean selected) {
int startIndex = getCellIndex(imageResourceStart, 0);
int endIndex = getCellIndex(imageResourceEnd, imagesTable.getCells().size - 1);
private VisTable getBoxResourceTable(BoxItemResource boxResource) {
for (VisTable boxesTable : boxesTableSet) {
Cell<BoxItemResource> cell = boxesTable.getCell(boxResource);
if (cell != null)
return boxesTable;
}
return null;
}
// we want to start with start :)
if (endIndex < startIndex) {
int tmp = startIndex;
startIndex = endIndex;
endIndex = tmp;
}
private void setSelectionBetween(BoxItemResource boxResourceStart, BoxItemResource boxResourceEnd, boolean selected) {
VisTable boxesTable = getBoxResourceTable(boxResourceStart);
if (boxesTable == null) return;
for (int i = startIndex; i <= endIndex; i++) {
Cell<ImageResource> cell = imagesTable.getCells().get(i);
ImageResource imageResource = cell.getActor();
setSelected(imageResource, selected);
}
int startIndex = getCellIndex(boxResourceStart, 0, boxesTable);
int endIndex = getCellIndex(boxResourceEnd, boxesTable.getCells().size - 1, boxesTable);
// we want to start with start :)
if (endIndex < startIndex) {
int tmp = startIndex;
startIndex = endIndex;
endIndex = tmp;
}
for (int i = startIndex; i <= endIndex; i++) {
Cell<BoxItemResource> cell = boxesTable.getCells().get(i);
BoxItemResource boxResource = cell.getActor();
setSelected(boxResource, selected);
}
}
}
@@ -45,14 +45,14 @@ public class UIResourcesBoxMediator extends PanelMediator<UIResourcesBox> {
private static final String PREFIX = "games.rednblack.editor.view.ui.box.UIResourcesBoxMediator";
public static final String IMAGE_LEFT_CLICK = PREFIX + ".IMAGE_LEFT_CLICK";
public static final String RESOURCE_BOX_LEFT_CLICK = PREFIX + ".RESOURCE_BOX_LEFT_CLICK";
public static final String SHIFT_CTRL_EVENT_TYPE = PREFIX + ".SHIFT_CTRL_EVENT_TYPE";
public static final String SHIFT_EVENT_TYPE = PREFIX + ".SHIFT_EVENT_TYPE";
public static final String CTRL_EVENT_TYPE = PREFIX + ".CTRL_EVENT_TYPE";
public static final String NORMAL_CLICK_EVENT_TYPE = PREFIX + ".NORMAL_CLICK_EVENT_TYPE";
public static final String IMAGE_TABLE_UPDATED = PREFIX + ".IMAGE_TABLE_UPDATED";
public static final String ADD_RESOURCES_BOX_TABLE_SELECTION_MANAGEMENT = PREFIX + ".ADD_RESOURCES_BOX_TABLE_SELECTION_MANAGEMENT";
public static final String IMAGE_RIGHT_CLICK = PREFIX + ".IMAGE_RIGHT_CLICK";
public static final String SPINE_ANIMATION_RIGHT_CLICK = PREFIX + ".SPINE_ANIMATION_RIGHT_CLICK";
@@ -19,10 +19,11 @@
package games.rednblack.editor.view.ui.box.resourcespanel;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Array;
import com.kotcrab.vis.ui.widget.VisScrollPane;
import com.kotcrab.vis.ui.widget.VisTable;
import games.rednblack.editor.HyperLap2DFacade;
import games.rednblack.editor.view.ui.box.UIResourcesBoxMediator;
import games.rednblack.h2d.common.view.ui.StandardWidgetsFactory;
import games.rednblack.editor.view.ui.box.resourcespanel.draggable.DraggableResource;
@@ -56,6 +57,7 @@ public class UIAnimationsTab extends UIResourcesTab {
@Override
protected VisScrollPane crateScrollPane() {
animationsTable = new VisTable();
HyperLap2DFacade.getInstance().sendNotification(UIResourcesBoxMediator.ADD_RESOURCES_BOX_TABLE_SELECTION_MANAGEMENT, animationsTable);
VisScrollPane scrollPane = StandardWidgetsFactory.createScrollPane(animationsTable);
scrollPane.setScrollingDisabled(true, false);
return scrollPane;
@@ -42,6 +42,7 @@ public class UIImagesTab extends UIResourcesTab {
@Override
protected VisScrollPane crateScrollPane() {
imagesTable = new VisTable();
HyperLap2DFacade.getInstance().sendNotification(UIResourcesBoxMediator.ADD_RESOURCES_BOX_TABLE_SELECTION_MANAGEMENT, imagesTable);
return StandardWidgetsFactory.createScrollPane(imagesTable);
}
@@ -65,6 +66,5 @@ public class UIImagesTab extends UIResourcesTab {
imagesTable.row();
}
}
HyperLap2DFacade.getInstance().sendNotification(UIResourcesBoxMediator.IMAGE_TABLE_UPDATED, imagesTable);
}
}
@@ -68,8 +68,7 @@ public abstract class BoxItemResource extends Group implements DraggableResource
/**
* Creates a new box item resource with the given colors.
*
* @param region The atlas region for the image resource.
*
* @param fillColor The color to fill the background of the image.
* @param borderColor The standard color of the border. Also used when the mouse is not hovering over the image.
* @param fillMouseOverColor The color to fill the background of the image when the mouse hovers over the image. Only used if the the parameter <code>highlightWhenMouseOver</code> is set to <code>true</code>.
@@ -93,7 +92,7 @@ public abstract class BoxItemResource extends Group implements DraggableResource
}
/**
* Sets the right-click event. Should not be used with {@link #setClickEvent(String, String, String)}.
* Sets the right-click event. Should not be used with {@link #setClickEvent(String, String, Object, Object)}.
*
* @param eventName The event name in case of a right-click.
* @param payload The payload for the right-click.
@@ -73,7 +73,7 @@ public class ImageResource extends BoxItemResource {
addActor(img);
setClickEvent(UIResourcesBoxMediator.IMAGE_LEFT_CLICK, UIResourcesBoxMediator.IMAGE_RIGHT_CLICK, this, region.name);
setClickEvent(UIResourcesBoxMediator.RESOURCE_BOX_LEFT_CLICK, UIResourcesBoxMediator.IMAGE_RIGHT_CLICK, this, region.name);
payloadImg = new Image(region);
payload = new ResourcePayloadObject();
@@ -115,7 +115,7 @@ public class SpineResource extends BoxItemResource {
super.act(1f);
super.act(Gdx.graphics.getDeltaTime());
setRightClickEvent(UIResourcesBoxMediator.SPINE_ANIMATION_RIGHT_CLICK, payload.name);
setClickEvent(UIResourcesBoxMediator.RESOURCE_BOX_LEFT_CLICK, UIResourcesBoxMediator.SPINE_ANIMATION_RIGHT_CLICK, this, payload.name);
}
@Override
@@ -107,7 +107,7 @@ public class SpriteResource extends BoxItemResource {
payload.className = getClass().getName();
setHeight(thumbnailSize);
setRightClickEvent(UIResourcesBoxMediator.SPRITE_ANIMATION_RIGHT_CLICK, payload.name);
setClickEvent(UIResourcesBoxMediator.RESOURCE_BOX_LEFT_CLICK, UIResourcesBoxMediator.SPRITE_ANIMATION_RIGHT_CLICK, this, payload.name);
}