2023-10-31 08:32:40 -04:00
|
|
|
import { timeout } from 'resource:///com/github/Aylur/ags/utils.js';
|
|
|
|
import { Box, EventBox, Overlay } from 'resource:///com/github/Aylur/ags/widget.js';
|
2023-10-02 12:06:35 -04:00
|
|
|
|
|
|
|
import Gtk from 'gi://Gtk';
|
2023-09-26 10:43:37 -04:00
|
|
|
|
|
|
|
const MAX_OFFSET = 200;
|
|
|
|
const OFFSCREEN = 500;
|
|
|
|
const TRANSITION = 'transition: margin 0.5s ease, opacity 3s ease;';
|
|
|
|
|
2023-10-02 12:06:35 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
export default ({
|
|
|
|
properties,
|
|
|
|
connections,
|
|
|
|
props,
|
|
|
|
} = {}) => {
|
|
|
|
const widget = EventBox({});
|
|
|
|
const gesture = Gtk.GestureDrag.new(widget);
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
widget.add(Overlay({
|
|
|
|
...props,
|
|
|
|
properties: [
|
|
|
|
...properties,
|
|
|
|
['dragging', false],
|
|
|
|
],
|
2023-11-01 14:33:06 -04:00
|
|
|
|
|
|
|
// Have empty PlayerBox to define the size of the widget
|
2023-10-20 23:11:21 -04:00
|
|
|
child: Box({ className: 'player' }),
|
|
|
|
connections: [
|
|
|
|
...connections,
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
[gesture, overlay => {
|
2023-11-01 14:33:06 -04:00
|
|
|
// Don't allow gesture when only one player
|
2023-10-20 23:11:21 -04:00
|
|
|
if (overlay.list().length <= 1)
|
|
|
|
return;
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
overlay._dragging = true;
|
2023-11-01 14:33:06 -04:00
|
|
|
var offset = gesture.get_offset()[1];
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
const playerBox = overlay.list().at(-1);
|
2023-10-12 22:59:04 -04:00
|
|
|
|
2023-11-01 15:29:47 -04:00
|
|
|
// Slide right
|
2023-10-20 23:11:21 -04:00
|
|
|
if (offset >= 0) {
|
2023-11-06 18:37:23 -05:00
|
|
|
playerBox.setCss(`
|
2023-11-01 13:12:43 -04:00
|
|
|
margin-left: ${offset}px;
|
|
|
|
margin-right: -${offset}px;
|
|
|
|
${playerBox._bgStyle}
|
|
|
|
`);
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
2023-11-01 14:33:06 -04:00
|
|
|
|
2023-11-01 15:29:47 -04:00
|
|
|
// Slide left
|
2023-10-20 23:11:21 -04:00
|
|
|
else {
|
2023-11-01 14:33:06 -04:00
|
|
|
offset = Math.abs(offset);
|
2023-11-06 18:37:23 -05:00
|
|
|
playerBox.setCss(`
|
2023-11-01 14:33:06 -04:00
|
|
|
margin-left: -${offset}px;
|
|
|
|
margin-right: ${offset}px;
|
2023-11-01 13:12:43 -04:00
|
|
|
${playerBox._bgStyle}
|
|
|
|
`);
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
|
|
|
}, 'drag-update'],
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
[gesture, overlay => {
|
2023-11-01 14:33:06 -04:00
|
|
|
// Don't allow gesture when only one player
|
2023-10-20 23:11:21 -04:00
|
|
|
if (overlay.list().length <= 1)
|
|
|
|
return;
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
overlay._dragging = false;
|
|
|
|
const offset = gesture.get_offset()[1];
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
const playerBox = overlay.list().at(-1);
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-11-01 14:33:06 -04:00
|
|
|
// If crosses threshold after letting go, slide away
|
2023-10-20 23:11:21 -04:00
|
|
|
if (Math.abs(offset) > MAX_OFFSET) {
|
2023-11-01 14:33:06 -04:00
|
|
|
// Disable inputs during animation
|
2023-11-01 13:12:43 -04:00
|
|
|
widget.sensitive = false;
|
2023-11-01 14:33:06 -04:00
|
|
|
|
|
|
|
// Slide away right
|
2023-10-20 23:11:21 -04:00
|
|
|
if (offset >= 0) {
|
2023-11-06 18:37:23 -05:00
|
|
|
playerBox.setCss(`
|
2023-11-01 13:12:43 -04:00
|
|
|
${TRANSITION}
|
|
|
|
margin-left: ${OFFSCREEN}px;
|
|
|
|
margin-right: -${OFFSCREEN}px;
|
|
|
|
opacity: 0; ${playerBox._bgStyle}
|
|
|
|
`);
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
2023-11-01 14:33:06 -04:00
|
|
|
|
|
|
|
// Slide away left
|
2023-10-20 23:11:21 -04:00
|
|
|
else {
|
2023-11-06 18:37:23 -05:00
|
|
|
playerBox.setCss(`
|
2023-11-01 13:12:43 -04:00
|
|
|
${TRANSITION}
|
|
|
|
margin-left: -${OFFSCREEN}px;
|
|
|
|
margin-right: ${OFFSCREEN}px;
|
|
|
|
opacity: 0; ${playerBox._bgStyle}`);
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
2023-11-01 14:33:06 -04:00
|
|
|
|
2023-10-31 08:32:40 -04:00
|
|
|
timeout(500, () => {
|
2023-11-01 14:33:06 -04:00
|
|
|
// Put the player in the back after anim
|
2023-10-20 23:11:21 -04:00
|
|
|
overlay.reorder_overlay(playerBox, 0);
|
2023-11-01 14:33:06 -04:00
|
|
|
// Recenter player
|
2023-11-06 18:37:23 -05:00
|
|
|
playerBox.setCss(playerBox._bgStyle);
|
2023-11-01 14:33:06 -04:00
|
|
|
|
2023-11-01 13:12:43 -04:00
|
|
|
widget.sensitive = true;
|
2023-10-20 23:11:21 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2023-11-01 14:33:06 -04:00
|
|
|
// Recenter with transition for animation
|
2023-11-06 18:37:23 -05:00
|
|
|
playerBox.setCss(`${TRANSITION} ${playerBox._bgStyle}`);
|
2023-10-20 23:11:21 -04:00
|
|
|
}
|
|
|
|
}, 'drag-end'],
|
|
|
|
],
|
|
|
|
}));
|
|
|
|
widget.child.list = () => widget.child.get_children().filter(ch => ch._bgStyle !== undefined);
|
2023-09-26 10:43:37 -04:00
|
|
|
|
2023-10-20 23:11:21 -04:00
|
|
|
return widget;
|
2023-09-26 10:43:37 -04:00
|
|
|
};
|