mirror of
https://github.com/tobspr/shapez.io.git
synced 2025-06-13 13:04:03 +00:00
undo / redo - HUD info
This commit is contained in:
parent
24c1f42747
commit
69acb28707
@ -4,12 +4,12 @@ import { KEYMAPPINGS } from "./key_action_mapper";
|
||||
|
||||
class LiFoQueue {
|
||||
constructor(size = 20) {
|
||||
this.size = size;
|
||||
this._size = size;
|
||||
this.items = [];
|
||||
}
|
||||
|
||||
enqueue(element) {
|
||||
if (this.size < this.items.length + 1) {
|
||||
if (this._size < this.items.length + 1) {
|
||||
this.items.shift();
|
||||
}
|
||||
this.items.push(element);
|
||||
@ -22,6 +22,10 @@ class LiFoQueue {
|
||||
clear() {
|
||||
this.items = [];
|
||||
}
|
||||
|
||||
isEmpty() {
|
||||
return !this.items.length;
|
||||
}
|
||||
}
|
||||
|
||||
const ActionType = {
|
||||
@ -32,7 +36,7 @@ const ActionType = {
|
||||
export class HistoryManager {
|
||||
constructor(root) {
|
||||
this.root = root;
|
||||
this._entities = new LiFoQueue();
|
||||
this._forUndo = new LiFoQueue();
|
||||
this._forRedo = new LiFoQueue();
|
||||
|
||||
this.initializeBindings();
|
||||
@ -43,21 +47,29 @@ export class HistoryManager {
|
||||
this.root.keyMapper.getBinding(KEYMAPPINGS.placement.redo).add(this._redo, this);
|
||||
}
|
||||
|
||||
get canUndo() {
|
||||
return !this._forUndo.isEmpty();
|
||||
}
|
||||
|
||||
get canRedo() {
|
||||
return !this._forRedo.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Entity} entity
|
||||
*/
|
||||
addAction(entity) {
|
||||
this._forRedo.clear();
|
||||
this._entities.enqueue({ type: ActionType.add, entity });
|
||||
this._forUndo.enqueue({ type: ActionType.add, entity });
|
||||
}
|
||||
|
||||
removeAction(entity) {
|
||||
this._forRedo.clear();
|
||||
this._entities.enqueue({ type: ActionType.remove, entity });
|
||||
this._forUndo.enqueue({ type: ActionType.remove, entity });
|
||||
}
|
||||
|
||||
_undo() {
|
||||
const { type, entity } = this._entities.dequeue() || {};
|
||||
const { type, entity } = this._forUndo.dequeue() || {};
|
||||
if (!entity) {
|
||||
return;
|
||||
}
|
||||
@ -78,10 +90,10 @@ export class HistoryManager {
|
||||
}
|
||||
if (type === ActionType.remove && this.root.logic.checkCanPlaceEntity(entity)) {
|
||||
this._placeEntity(entity);
|
||||
this._entities.enqueue({ type: ActionType.add, entity });
|
||||
this._forUndo.enqueue({ type: ActionType.add, entity });
|
||||
}
|
||||
if (type === ActionType.add && this.root.logic.canDeleteBuilding(entity)) {
|
||||
this._entities.enqueue({ type: ActionType.remove, entity: entity.clone() });
|
||||
this._forUndo.enqueue({ type: ActionType.remove, entity: entity.clone() });
|
||||
this._removeEntity(entity);
|
||||
}
|
||||
}
|
||||
|
@ -120,6 +120,14 @@ export class HUDKeybindingOverlay extends BaseHUDPart {
|
||||
return this.root.camera.getIsMapOverlayActive();
|
||||
}
|
||||
|
||||
get undoActive() {
|
||||
return !this.mapOverviewActive && !this.blueprintPlacementActive && this.root.historyMgr.canUndo;
|
||||
}
|
||||
|
||||
get redoActive() {
|
||||
return !this.mapOverviewActive && !this.blueprintPlacementActive && this.root.historyMgr.canRedo;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the element
|
||||
* @param {HTMLElement} parent
|
||||
@ -185,6 +193,20 @@ export class HUDKeybindingOverlay extends BaseHUDPart {
|
||||
condition: () => !this.mapOverviewActive && !this.blueprintPlacementActive,
|
||||
},
|
||||
|
||||
{
|
||||
// Undo
|
||||
label: T.ingame.keybindingsOverlay.undo,
|
||||
keys: [k.placement.undo],
|
||||
condition: () => this.undoActive,
|
||||
},
|
||||
|
||||
{
|
||||
// Redo
|
||||
label: T.ingame.keybindingsOverlay.redo,
|
||||
keys: [k.placement.redo],
|
||||
condition: () => this.redoActive,
|
||||
},
|
||||
|
||||
{
|
||||
// Area select
|
||||
label: T.ingame.keybindingsOverlay.selectBuildings,
|
||||
@ -297,10 +319,18 @@ export class HUDKeybindingOverlay extends BaseHUDPart {
|
||||
case ADDER_TOKEN:
|
||||
html += `+`;
|
||||
break;
|
||||
default:
|
||||
html += `<code class="keybinding">${getStringForKeyCode(
|
||||
mapper.getBinding(/** @type {KeyCode} */ (key)).keyCode
|
||||
)}</code>`;
|
||||
default: {
|
||||
const binding = mapper.getBinding(/** @type {KeyCode} */ (key));
|
||||
if (binding.shift) {
|
||||
html += `<code class="keybinding">${getStringForKeyCode(16)}</code>`;
|
||||
html += `+`;
|
||||
}
|
||||
if (binding.ctrl) {
|
||||
html += `<code class="keybinding">${getStringForKeyCode(17)}</code>`;
|
||||
html += `+`;
|
||||
}
|
||||
html += `<code class="keybinding">${getStringForKeyCode(binding.keyCode)}</code>`;
|
||||
}
|
||||
}
|
||||
}
|
||||
html += `<label>${handle.label}</label>`;
|
||||
|
Loading…
Reference in New Issue
Block a user