List of usage examples for com.badlogic.gdx.scenes.scene2d.utils DragListener setTapSquareSize
public void setTapSquareSize(float halfTapSquareSize)
From source file:com.kotcrab.vis.editor.util.scene2d.VisDragAndDrop.java
License:Apache License
public void addSource(final Source source) { DragListener listener = new DragListener() { public void dragStart(InputEvent event, float x, float y, int pointer) { if (activePointer != -1) { event.stop();//from w ww .ja v a2 s . com return; } activePointer = pointer; dragStartTime = System.currentTimeMillis(); payload = source.dragStart(event, getTouchDownX(), getTouchDownY(), pointer); event.stop(); if (cancelTouchFocus && payload != null) source.getActor().getStage().cancelTouchFocusExcept(this, source.getActor()); } public void drag(InputEvent event, float x, float y, int pointer) { if (payload == null) return; if (pointer != activePointer) return; Stage stage = event.getStage(); Touchable dragActorTouchable = null; if (dragActor != null) { dragActorTouchable = dragActor.getTouchable(); dragActor.setTouchable(Touchable.disabled); } // Find target. Target newTarget = null; isValidTarget = false; float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY; Actor hit = event.getStage().hit(stageX, stageY, true); // Prefer touchable actors. if (hit == null) hit = event.getStage().hit(stageX, stageY, false); if (hit != null) { for (int i = 0, n = targets.size; i < n; i++) { Target target = targets.get(i); if (!target.getActor().isAscendantOf(hit)) continue; newTarget = target; target.getActor().stageToLocalCoordinates(tmpVector.set(stageX, stageY)); break; } } //if over a new target, notify the former target that it's being left behind. if (newTarget != target) { if (target != null) target.reset(source, payload); target = newTarget; } //with any reset out of the way, notify new targets of drag. if (newTarget != null) { isValidTarget = newTarget.drag(source, payload, tmpVector.x, tmpVector.y, pointer); } if (dragActor != null) dragActor.setTouchable(dragActorTouchable); // Add/remove and position the drag actor. Actor actor = null; if (target != null) actor = isValidTarget ? payload.getValidDragActor() : payload.getInvalidDragActor(); if (actor == null) actor = payload.getDragActor(); if (actor == null) return; if (dragActor != actor) { if (dragActor != null) dragActor.remove(); dragActor = actor; stage.addActor(actor); } float actorX = event.getStageX() + dragActorX; float actorY = event.getStageY() + dragActorY - actor.getHeight(); if (keepWithinStage) { if (actorX < 0) actorX = 0; if (actorY < 0) actorY = 0; if (actorX + actor.getWidth() > stage.getWidth()) actorX = stage.getWidth() - actor.getWidth(); if (actorY + actor.getHeight() > stage.getHeight()) actorY = stage.getHeight() - actor.getHeight(); } actor.setPosition(actorX, actorY); if (currentSceneCamera != null && editingSettings.isSnapEnabledOrKeyPressed()) { float gridSize = gridSettings.config.gridSize; actor.setPosition(MathUtils.floor(currentSceneCamera.getInputX() / gridSize) * gridSize, MathUtils.floor(currentSceneCamera.getInputY() / gridSize) * gridSize); Vector3 v = currentSceneCamera.project(tmpVector3.set(actor.getX(), actor.getY(), 0)); actor.setPosition(v.x, v.y); } } public void dragStop(InputEvent event, float x, float y, int pointer) { if (pointer != activePointer) return; activePointer = -1; if (payload == null) return; if (System.currentTimeMillis() - dragStartTime < dragTime) isValidTarget = false; if (dragActor != null) dragActor.remove(); if (isValidTarget) { float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY; target.getActor().stageToLocalCoordinates(tmpVector.set(stageX, stageY)); target.drop(source, payload, tmpVector.x, tmpVector.y, pointer); } source.dragStop(event, x, y, pointer, payload, isValidTarget ? target : null); if (target != null) target.reset(source, payload); payload = null; target = null; isValidTarget = false; dragActor = null; } }; listener.setTapSquareSize(tapSquareSize); listener.setButton(button); source.getActor().addCaptureListener(listener); sourceListeners.put(source, listener); }
From source file:com.ray3k.skincomposer.dialog.DialogColorPicker.java
License:Open Source License
public void populate() { content.clear();/*from ww w .j ava 2 s .c o m*/ content.defaults().padLeft(10.0f); Image cursor = new Image(skin.getDrawable("color-picker")); cursor.setTouchable(Touchable.enabled); Image hueKnob = new Image(skin, "color-scale"); hueKnob.setTouchable(Touchable.enabled); Image hueKnob2 = new Image(skin, "color-scale-flipped"); hueKnob2.setTouchable(Touchable.enabled); Image alphaKnob = new Image(skin, "color-scale"); alphaKnob.setTouchable(Touchable.enabled); Image alphaKnob2 = new Image(skin, "color-scale-flipped"); alphaKnob2.setTouchable(Touchable.enabled); Container selectedColorCont = new Container(); selectedColorCont.setBackground(skin.getDrawable("white")); selectedColorCont.setColor(selectedColor); Vector3 v = rgbToHsb(selectedColor.r, selectedColor.g, selectedColor.b); Spinner greenSpinner, blueSpinner, alphaSpinner, hueSpinner, saturationSpinner, brightnessSpinner; hueSpinner = new Spinner(v.x * 359.0f, 1.0, true, Orientation.HORIZONTAL, spinnerStyle); hueSpinner.setMinimum(0.0); hueSpinner.setMaximum(359.0); hueSpinner.getTextField().addListener(main.getIbeamListener()); hueSpinner.getButtonMinus().addListener(main.getHandListener()); hueSpinner.getButtonPlus().addListener(main.getHandListener()); saturationSpinner = new Spinner(v.y * 100.0f, 1.0, true, Orientation.HORIZONTAL, spinnerStyle); saturationSpinner.setMinimum(0.0); saturationSpinner.setMaximum(100.0); saturationSpinner.getTextField().addListener(main.getIbeamListener()); saturationSpinner.getButtonMinus().addListener(main.getHandListener()); saturationSpinner.getButtonPlus().addListener(main.getHandListener()); brightnessSpinner = new Spinner(v.z * 100.0f, 1.0, true, Orientation.HORIZONTAL, spinnerStyle); brightnessSpinner.setMinimum(0.0); brightnessSpinner.setMaximum(100.0); brightnessSpinner.getTextField().addListener(main.getIbeamListener()); brightnessSpinner.getButtonMinus().addListener(main.getHandListener()); brightnessSpinner.getButtonPlus().addListener(main.getHandListener()); redSpinner = new Spinner(selectedColor.r * 255.0f, 1.0, true, Orientation.HORIZONTAL, spinnerStyle); redSpinner.setMinimum(0.0); redSpinner.setMaximum(255.0); redSpinner.getTextField().addListener(main.getIbeamListener()); redSpinner.getButtonMinus().addListener(main.getHandListener()); redSpinner.getButtonPlus().addListener(main.getHandListener()); greenSpinner = new Spinner(selectedColor.g * 255.0f, 1.0, true, Orientation.HORIZONTAL, spinnerStyle); greenSpinner.setMinimum(0.0); greenSpinner.setMaximum(255.0); greenSpinner.getTextField().addListener(main.getIbeamListener()); greenSpinner.getButtonMinus().addListener(main.getHandListener()); greenSpinner.getButtonPlus().addListener(main.getHandListener()); blueSpinner = new Spinner(selectedColor.b * 255.0f, 1.0, true, Orientation.HORIZONTAL, spinnerStyle); blueSpinner.setMinimum(0.0); blueSpinner.setMaximum(255.0); blueSpinner.getTextField().addListener(main.getIbeamListener()); blueSpinner.getButtonMinus().addListener(main.getHandListener()); blueSpinner.getButtonPlus().addListener(main.getHandListener()); alphaSpinner = new Spinner(selectedColor.a * 255.0f, 1.0, true, Orientation.HORIZONTAL, spinnerStyle); alphaSpinner.setMinimum(0.0); alphaSpinner.setMaximum(255.0); alphaSpinner.getTextField().addListener(main.getIbeamListener()); alphaSpinner.getButtonMinus().addListener(main.getHandListener()); alphaSpinner.getButtonPlus().addListener(main.getHandListener()); redSpinner.setTransversalNext(greenSpinner.getTextField()); greenSpinner.setTransversalNext(blueSpinner.getTextField()); blueSpinner.setTransversalNext(alphaSpinner.getTextField()); alphaSpinner.setTransversalNext(hueSpinner.getTextField()); hueSpinner.setTransversalNext(saturationSpinner.getTextField()); saturationSpinner.setTransversalNext(brightnessSpinner.getTextField()); brightnessSpinner.setTransversalNext(redSpinner.getTextField()); redSpinner.setTransversalPrevious(brightnessSpinner.getTextField()); greenSpinner.setTransversalPrevious(redSpinner.getTextField()); blueSpinner.setTransversalPrevious(greenSpinner.getTextField()); alphaSpinner.setTransversalPrevious(blueSpinner.getTextField()); hueSpinner.setTransversalPrevious(alphaSpinner.getTextField()); saturationSpinner.setTransversalPrevious(hueSpinner.getTextField()); brightnessSpinner.setTransversalPrevious(saturationSpinner.getTextField()); ChangeListener rgbListener = new ChangeListener() { @Override public void changed(ChangeListener.ChangeEvent event, Actor actor) { selectedColor.set((float) redSpinner.getValue() / 255.0f, (float) greenSpinner.getValue() / 255.0f, (float) blueSpinner.getValue() / 255.0f, (float) alphaSpinner.getValue() / 255.0f); Vector3 v = rgbToHsb(selectedColor.r, selectedColor.g, selectedColor.b); hueSpinner.setValue(v.x * 359.0f); saturationSpinner.setValue(v.y * 100.0f); brightnessSpinner.setValue(v.z * 100.0f); selectedColorCont.setColor(selectedColor); Color color = hsbToRgb((float) hueSpinner.getValue(), 1.0f, 1.0f); gradientS.setCol2(color); gradientS.setCol3(color); gradientAlpha.setCol3(color); gradientAlpha.setCol4(color); color = new Color(color); color.a = 0.0f; gradientAlpha.setCol1(color); gradientAlpha.setCol2(color); cursor.setX(v.y * SIZE - cursor.getWidth() / 2.0f); cursor.setY(v.z * SIZE - cursor.getHeight() / 2.0f); hueKnob.setY(v.x * SIZE - hueKnob.getHeight() / 2.0f); hueKnob2.setY(hueKnob.getY()); } }; redSpinner.addListener(rgbListener); greenSpinner.addListener(rgbListener); blueSpinner.addListener(rgbListener); ChangeListener hsbListener = new ChangeListener() { @Override public void changed(ChangeListener.ChangeEvent event, Actor actor) { Color color = hsbToRgb((float) hueSpinner.getValue(), (float) saturationSpinner.getValue() / 100.0f, (float) brightnessSpinner.getValue() / 100.0f); color.a = (float) alphaSpinner.getValue() / 255.0f; redSpinner.setValue(color.r * 255.0f); greenSpinner.setValue(color.g * 255.0f); blueSpinner.setValue(color.b * 255.0f); selectedColor.set(color); selectedColorCont.setColor(selectedColor); color = hsbToRgb((float) hueSpinner.getValue(), 1.0f, 1.0f); gradientS.setCol2(color); gradientS.setCol3(color); gradientAlpha.setCol3(color); gradientAlpha.setCol4(color); color = new Color(color); color.a = 0.0f; gradientAlpha.setCol1(color); gradientAlpha.setCol2(color); cursor.setX((float) saturationSpinner.getValue() / 100.0f * SIZE - cursor.getWidth() / 2.0f); cursor.setY((float) brightnessSpinner.getValue() / 100.0f * SIZE - cursor.getHeight() / 2.0f); hueKnob.setY((float) hueSpinner.getValue() / 359.0f * SIZE - hueKnob.getHeight() / 2.0f); hueKnob2.setY(hueKnob.getY()); } }; hueSpinner.addListener(hsbListener); saturationSpinner.addListener(hsbListener); brightnessSpinner.addListener(hsbListener); alphaSpinner.addListener(new ChangeListener() { @Override public void changed(ChangeListener.ChangeEvent event, Actor actor) { selectedColor.set((float) redSpinner.getValue() / 255.0f, (float) greenSpinner.getValue() / 255.0f, (float) blueSpinner.getValue() / 255.0f, (float) alphaSpinner.getValue() / 255.0f); selectedColorCont.setColor(selectedColor); alphaKnob.setY(selectedColor.a * SIZE - alphaKnob.getHeight() / 2.0f); alphaKnob2.setY(alphaKnob.getY()); } }); Table panel = new Table(skin); panel.setBackground("color-box"); Table t = new Table(skin); t.setClip(true); t.setBackground(gradientSB); t.setTouchable(Touchable.enabled); cursor.setPosition(v.y * SIZE - cursor.getWidth() / 2.0f, v.z * SIZE - cursor.getHeight() / 2.0f); t.addActor(cursor); DragListener dragListener = new DragListener() { @Override public void drag(InputEvent event, float x, float y, int pointer) { saturationSpinner.setValue(MathUtils.clamp(x / SIZE * 100.0f, 0, 100)); brightnessSpinner.setValue(MathUtils.clamp(y / SIZE * 100.0f, 0, 100)); saturationSpinner.fire(new ChangeListener.ChangeEvent()); } }; dragListener.setTapSquareSize(1.0f); t.addListener(dragListener); t.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { saturationSpinner.setValue(MathUtils.clamp(x / SIZE * 100.0f, 0, 100)); brightnessSpinner.setValue(MathUtils.clamp(y / SIZE * 100.0f, 0, 100)); saturationSpinner.fire(new ChangeListener.ChangeEvent()); return false; } }); panel.add(t).size(SIZE, SIZE); content.add(panel); panel = new Table(skin); panel.setBackground("color-box"); t = new Table(skin); t.setTouchable(Touchable.enabled); t.setClip(true); for (GradientDrawable gradient : hueGradient) { Container container = new Container(); container.background(gradient); t.add(container).growX().height(50.0f); t.row(); } t.addActor(hueKnob); t.addActor(hueKnob2); hueKnob.setY(v.x * SIZE - hueKnob.getHeight() / 2.0f); hueKnob2.setX(30.0f - hueKnob2.getWidth()); hueKnob2.setY(v.x * SIZE - hueKnob2.getHeight() / 2.0f); dragListener = new DragListener() { @Override public void drag(InputEvent event, float x, float y, int pointer) { hueSpinner.setValue(MathUtils.clamp(y / SIZE * 359.0f, 0.0f, 359.0f)); hueSpinner.fire(new ChangeListener.ChangeEvent()); } }; dragListener.setTapSquareSize(1.0f); t.addListener(dragListener); t.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { hueSpinner.setValue(MathUtils.clamp(y / SIZE * 359.0f, 0.0f, 359.0f)); hueSpinner.fire(new ChangeListener.ChangeEvent()); return false; } }); panel.add(t).minWidth(30.0f).height(SIZE); content.add(panel); panel = new Table(skin); panel.setBackground("color-box"); t = new Table(); t.setTouchable(Touchable.enabled); t.setBackground(alphaStack); t.setClip(true); t.addActor(alphaKnob); t.addActor(alphaKnob2); alphaKnob.setY(selectedColor.a * SIZE - alphaKnob.getHeight() / 2.0f); alphaKnob2.setX(30.0f - alphaKnob2.getWidth()); alphaKnob2.setY(selectedColor.a * SIZE - alphaKnob2.getHeight() / 2.0f); dragListener = new DragListener() { @Override public void drag(InputEvent event, float x, float y, int pointer) { alphaSpinner.setValue(MathUtils.clamp(y / SIZE * 255.0f, 0.0f, 255.0f)); alphaSpinner.fire(new ChangeListener.ChangeEvent()); } }; dragListener.setTapSquareSize(1.0f); t.addListener(dragListener); t.addListener(new InputListener() { @Override public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { alphaSpinner.setValue(MathUtils.clamp(y / SIZE * 255.0f, 0.0f, 255.0f)); alphaSpinner.fire(new ChangeListener.ChangeEvent()); return false; } }); panel.add(t).minWidth(30.0f).height(SIZE); content.add(panel); t = new Table(); t.defaults().pad(10.0f); Table table = new Table(skin); Label label = new Label("new", skin); label.setAlignment(Align.center); table.add(label).growX(); table.row(); Container bg = new Container(); bg.setBackground(checker); Stack stack = new Stack(bg, selectedColorCont); panel = new Table(skin); panel.setBackground("color-box"); panel.add(stack).grow(); table.add(panel).grow(); if (previousColor != null) { Container cont = new Container(); cont.setBackground(skin.getDrawable("white")); cont.setColor(previousColor); bg = new Container(); bg.setBackground(checker); stack = new Stack(bg, cont); panel.row(); panel.add(stack).grow(); table.row(); label = new Label("current", skin); label.setAlignment(Align.center); table.add(label).growX(); t.add(table).minWidth(80.0f).minHeight(150.0f); } else { t.add(table).minWidth(80.0f).minHeight(100.0f); } table = new Table(); TextButton textButton = new TextButton("OK", skin); textButton.addListener(main.getHandListener()); textButton.addListener(new ChangeListener() { @Override public void changed(ChangeListener.ChangeEvent event, Actor actor) { if (listener != null) { listener.handle(new ColorListener.ColorEvent(selectedColor)); } hide(); } }); table.add(textButton).growX(); table.row(); textButton = new TextButton("Cancel", skin); textButton.addListener(main.getHandListener()); textButton.addListener(new ChangeListener() { @Override public void changed(ChangeListener.ChangeEvent event, Actor actor) { if (listener != null) { listener.handle(new ColorListener.ColorEvent(null)); } hide(); } }); table.add(textButton).growX().padTop(10.0f); t.add(table); t.row(); table = new Table(); label = new Label("R", skin, "required"); table.add(label); table.add(redSpinner).padLeft(10.0f).minWidth(90.0f); t.add(table); table = new Table(); label = new Label("H", skin, "required"); table.add(label); table.add(hueSpinner).padLeft(10.0f).minWidth(90.0f); t.add(table); t.row(); table = new Table(); label = new Label("G", skin, "required"); table.add(label); table.add(greenSpinner).padLeft(10.0f).minWidth(90.0f); t.add(table); table = new Table(); label = new Label("S", skin, "required"); table.add(label); table.add(saturationSpinner).padLeft(10.0f).minWidth(90.0f); t.add(table); t.row(); table = new Table(); label = new Label("B", skin, "required"); table.add(label); table.add(blueSpinner).padLeft(10.0f).minWidth(90.0f); t.add(table); table = new Table(); label = new Label("B", skin, "required"); table.add(label); table.add(brightnessSpinner).padLeft(10.0f).minWidth(90.0f); t.add(table); t.row(); table = new Table(); label = new Label("A", skin, "required"); table.add(label); t.add(table); table.add(alphaSpinner).padLeft(10.0f).minWidth(90.0f); content.add(t).growY(); }
From source file:com.uwsoft.editor.gdx.actors.CustomDragAndDrop.java
License:Apache License
public void addSource(final Source source) { DragListener listener = new DragListener() { public void dragStart(InputEvent event, float x, float y, int pointer) { if (activePointer != -1) { event.stop();//from w ww. j a v a 2 s . com return; } activePointer = pointer; dragStartTime = System.currentTimeMillis(); payload = source.dragStart(event, getTouchDownX(), getTouchDownY(), pointer); event.stop(); } public void drag(InputEvent event, float x, float y, int pointer) { if (payload == null) return; if (pointer != activePointer) return; Stage stage = event.getStage(); Touchable dragActorTouchable = null; if (dragActor != null) { dragActorTouchable = dragActor.getTouchable(); dragActor.setTouchable(Touchable.disabled); } // Find target. Target newTarget = null; isValidTarget = false; float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY; Actor hit = event.getStage().hit(stageX, stageY, true); // Prefer touchable actors. if (hit == null) hit = event.getStage().hit(stageX, stageY, false); if (hit != null) { for (int i = 0, n = targets.size; i < n; i++) { Target target = targets.get(i); if (!target.actor.isAscendantOf(hit)) continue; newTarget = target; target.actor.stageToLocalCoordinates(tmpVector.set(stageX, stageY)); isValidTarget = target.drag(source, payload, tmpVector.x, tmpVector.y, pointer); break; } } if (newTarget != target) { if (target != null) target.reset(source, payload); target = newTarget; } if (dragActor != null) dragActor.setTouchable(dragActorTouchable); // Add/remove and position the drag actor. Actor actor = null; if (target != null) actor = isValidTarget ? payload.validDragActor : payload.invalidDragActor; if (actor == null) actor = payload.dragActor; if (actor == null) return; if (dragActor != actor) { if (dragActor != null) dragActor.remove(); dragActor = actor; stage.addActor(actor); } float actorX = event.getStageX() + dragActorX; float actorY = event.getStageY() + dragActorY - actor.getHeight(); // if (actorX < 0) actorX = 0; // if (actorY < 0) actorY = 0; // if (actorX + actor.getWidth() > stage.getWidth()) actorX = stage.getWidth() - actor.getWidth(); // if (actorY + actor.getHeight() > stage.getHeight()) actorY = stage.getHeight() - actor.getHeight(); actor.setPosition(actorX, actorY); } public void dragStop(InputEvent event, float x, float y, int pointer) { if (pointer != activePointer) return; activePointer = -1; if (payload == null) return; if (System.currentTimeMillis() - dragStartTime < dragTime) isValidTarget = false; if (dragActor != null) dragActor.remove(); if (isValidTarget) { float stageX = event.getStageX() + touchOffsetX, stageY = event.getStageY() + touchOffsetY; target.actor.stageToLocalCoordinates(tmpVector.set(stageX, stageY)); target.drop(source, payload, tmpVector.x, tmpVector.y, pointer); } source.dragStop(event, x, y, pointer, payload, isValidTarget ? target : null); if (target != null) target.reset(source, payload); payload = null; target = null; isValidTarget = false; dragActor = null; } }; listener.setTapSquareSize(tapSquareSize); listener.setButton(button); source.actor.addCaptureListener(listener); sourceListeners.put(source, listener); }