Skip to content

Commit

Permalink
Fix VisSplitPane and MultiSplitPane custom cursor handling, #255
Browse files Browse the repository at this point in the history
  • Loading branch information
kotcrab committed Apr 14, 2017
1 parent 59ad84f commit 47d40fb
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 100 deletions.
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -15,3 +15,4 @@ StQuote https://github.com/StQuote
Favorlock https://github.com/Favorlock
strubelz https://github.com/strubelz
Snehks https://github.com/Snehks
ericnondahl https://github.com/ericnondahl
3 changes: 2 additions & 1 deletion ui/CHANGES.md
@@ -1,4 +1,5 @@
#### Version: 1.3.1 (LibGDX 1.9.6)
#### Version: 1.4.0 (LibGDX 1.9.6)
- **Fixed**: [#255](https://github.com/kotcrab/vis-editor/issues/255) Custom cursor handling in `VisSplitPane` and `MultiSplitPane`
- `Spinner` now implements `Disableable`

#### Version: 1.3.0 (LibGDX 1.9.6)
Expand Down
59 changes: 8 additions & 51 deletions ui/src/main/java/com/kotcrab/vis/ui/widget/MultiSplitPane.java
Expand Up @@ -16,9 +16,7 @@

package com.kotcrab.vis.ui.widget;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Cursor.SystemCursor;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Rectangle;
Expand All @@ -28,7 +26,6 @@
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Touchable;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.Layout;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
Expand All @@ -37,7 +34,7 @@
import com.badlogic.gdx.utils.SnapshotArray;
import com.kotcrab.vis.ui.FocusManager;
import com.kotcrab.vis.ui.VisUI;
import com.kotcrab.vis.ui.util.CursorManager;
import com.kotcrab.vis.ui.widget.internal.SplitPaneCursorManager;

import java.util.Arrays;

Expand Down Expand Up @@ -80,58 +77,18 @@ public MultiSplitPane (boolean vertical, MultiSplitPaneStyle style) {
}

private void initialize () {
addListener(new ClickListener() {
SystemCursor currentCursor;
SystemCursor targetCursor;

@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
if (getHandleContaining(x, y) != null) {
return true;
}

return false;
}

@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
CursorManager.restoreDefaultCursor();
currentCursor = null;
}

addListener(new SplitPaneCursorManager(this, vertical) {
@Override
public boolean mouseMoved (InputEvent event, float x, float y) {
if (getHandleContaining(x, y) != null) {
if (vertical) {
targetCursor = SystemCursor.VerticalResize;
} else {
targetCursor = SystemCursor.HorizontalResize;
}

if (currentCursor != targetCursor) {
Gdx.graphics.setSystemCursor(targetCursor);
currentCursor = targetCursor;
}
} else {
clearCustomCursor();
}

return false;
protected boolean handleBoundsContains (float x, float y) {
return getHandleContaining(x, y) != null;
}

@Override
public void exit (InputEvent event, float x, float y, int pointer, Actor toActor) {
super.exit(event, x, y, pointer, toActor);
if (pointer == -1) {
clearCustomCursor();
}
}

private void clearCustomCursor () {
if (currentCursor != null) {
CursorManager.restoreDefaultCursor();
currentCursor = null;
protected boolean contains (float x, float y) {
for (Rectangle bound : widgetBounds) {
if (bound.contains(x, y)) return true;
}
return getHandleContaining(x, y) != null;
}
});

Expand Down
54 changes: 6 additions & 48 deletions ui/src/main/java/com/kotcrab/vis/ui/widget/VisSplitPane.java
Expand Up @@ -16,9 +16,7 @@

package com.kotcrab.vis.ui.widget;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Cursor.SystemCursor;
import com.badlogic.gdx.graphics.g2d.Batch;
import com.badlogic.gdx.math.Rectangle;
import com.badlogic.gdx.math.Vector2;
Expand All @@ -29,14 +27,13 @@
import com.badlogic.gdx.scenes.scene2d.ui.SplitPane;
import com.badlogic.gdx.scenes.scene2d.ui.SplitPane.SplitPaneStyle;
import com.badlogic.gdx.scenes.scene2d.ui.WidgetGroup;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.scenes.scene2d.utils.Layout;
import com.badlogic.gdx.scenes.scene2d.utils.ScissorStack;
import com.badlogic.gdx.utils.GdxRuntimeException;
import com.kotcrab.vis.ui.FocusManager;
import com.kotcrab.vis.ui.VisUI;
import com.kotcrab.vis.ui.util.CursorManager;
import com.kotcrab.vis.ui.widget.internal.SplitPaneCursorManager;

/**
* Extends functionality of standard {@link SplitPane}. Style supports handle over {@link Drawable}. Due to scope of
Expand Down Expand Up @@ -96,54 +93,15 @@ public VisSplitPane (Actor firstWidget, Actor secondWidget, boolean vertical, Vi
}

private void initialize () {
addListener(new ClickListener() {
SystemCursor currentCursor;
SystemCursor targetCursor;

@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return true;
}

addListener(new SplitPaneCursorManager(this, vertical) {
@Override
public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
CursorManager.restoreDefaultCursor();
currentCursor = null;
protected boolean handleBoundsContains (float x, float y) {
return handleBounds.contains(x, y);
}

@Override
public boolean mouseMoved (InputEvent event, float x, float y) {
if (handleBounds.contains(x, y)) {
if (vertical) {
targetCursor = SystemCursor.VerticalResize;
} else {
targetCursor = SystemCursor.HorizontalResize;
}

if (currentCursor != targetCursor) {
Gdx.graphics.setSystemCursor(targetCursor);
currentCursor = targetCursor;
}
} else {
clearCustomCursor();
}

return false;
}

@Override
public void exit (InputEvent event, float x, float y, int pointer, Actor toActor) {
super.exit(event, x, y, pointer, toActor);
if (pointer == -1) {
clearCustomCursor();
}
}

private void clearCustomCursor () {
if (currentCursor != null) {
CursorManager.restoreDefaultCursor();
currentCursor = null;
}
protected boolean contains (float x, float y) {
return firstWidgetBounds.contains(x, y) || secondWidgetBounds.contains(x, y) || handleBounds.contains(x, y);
}
});

Expand Down
@@ -0,0 +1,102 @@
/*
* Copyright 2014-2016 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.kotcrab.vis.ui.widget.internal;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Cursor;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.kotcrab.vis.ui.util.CursorManager;

/**
* Manages setting custom cursor for split panes.
* This is VisUI internal class
* @author Kotcrab
* @since 1.4.0
*/
public abstract class SplitPaneCursorManager extends ClickListener {
private Actor owner;
private boolean vertical;

private Cursor.SystemCursor currentCursor;

public SplitPaneCursorManager (Actor owner, boolean vertical) {
this.owner = owner;
this.vertical = vertical;
}

@Override
public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
return handleBoundsContains(x, y);
}

@Override
public void touchDragged (InputEvent event, float x, float y, int pointer) {
super.touchDragged(event, x, y, pointer); //handles setting cursor when mouse returned to widget after exiting it while dragged
if (contains(x, y)) {
setCustomCursor();
} else {
clearCustomCursor();
}
}

@Override
public boolean mouseMoved (InputEvent event, float x, float y) {
super.mouseMoved(event, x, y);
if (handleBoundsContains(x, y)) {
setCustomCursor();
} else {
clearCustomCursor();
}

return false;
}

@Override
public void exit (InputEvent event, float x, float y, int pointer, Actor toActor) {
super.exit(event, x, y, pointer, toActor);
if (pointer == -1 && (toActor == null || toActor.isDescendantOf(owner) == false)) {
clearCustomCursor();
}
}

private void setCustomCursor () {
Cursor.SystemCursor targetCursor;
if (vertical) {
targetCursor = Cursor.SystemCursor.VerticalResize;
} else {
targetCursor = Cursor.SystemCursor.HorizontalResize;
}

if (currentCursor != targetCursor) {
Gdx.graphics.setSystemCursor(targetCursor);
currentCursor = targetCursor;
}
}

private void clearCustomCursor () {
if (currentCursor != null) {
CursorManager.restoreDefaultCursor();
currentCursor = null;
}
}

protected abstract boolean handleBoundsContains (float x, float y);

protected abstract boolean contains (float x, float y);
}

0 comments on commit 47d40fb

Please sign in to comment.