Skip to content

Commit

Permalink
Create mechanism to remember dialog window positions
Browse files Browse the repository at this point in the history
  • Loading branch information
AntumDeluge committed Nov 28, 2023
1 parent 8ff35d5 commit 2ad8ee6
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 19 deletions.
2 changes: 1 addition & 1 deletion srcjs/stendhal/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ export class Client {

var menubutton = document.getElementById("menubutton")!;
menubutton.addEventListener("click", () => {
const dialogState = stendhal.config.dialogstates["menu"];
const dialogState = stendhal.config.windowstates["menu"];
const menuContent = new ApplicationMenuDialog();
const menuFrame = ui.createSingletonFloatingWindow(
"Menu", menuContent, dialogState.x, dialogState.y);
Expand Down
2 changes: 1 addition & 1 deletion srcjs/stendhal/action/SettingsAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class SettingsAction extends SlashAction {
readonly maxParams = 0;

execute(_type: string, _params: string[], _remainder: string): boolean {
const wstate = stendhal.config.dialogstates["settings"];
const wstate = stendhal.config.windowstates["settings"];
const offset = stendhal.ui.getPageOffset();

const content = new SettingsDialog();
Expand Down
2 changes: 1 addition & 1 deletion srcjs/stendhal/entity/Chest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export class Chest extends PopupInventory {

openInventoryWindow() {
if (!this.inventory || !this.inventory.isOpen()) {
const dstate = stendhal.config.dialogstates["chest"];
const dstate = stendhal.config.windowstates["chest"];
const invComponent = new ItemInventoryComponent(this,
"content", 5, 6, stendhal.config.getBoolean("action.chest.quickpickup"), undefined);
invComponent.setConfigId("chest");
Expand Down
2 changes: 1 addition & 1 deletion srcjs/stendhal/entity/Corpse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class Corpse extends PopupInventory {
}
}

const dstate = stendhal.config.dialogstates["corpse"];
const dstate = stendhal.config.windowstates["corpse"];
const invComponent = new ItemInventoryComponent(this,
"content", content_row, content_col, true, undefined);
invComponent.setConfigId("corpse");
Expand Down
2 changes: 1 addition & 1 deletion srcjs/stendhal/entity/User.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export class User extends Player {
action: function(_entity: any) {
let outfitDialog = ui.get(UIComponentEnum.OutfitDialog);
if (!outfitDialog) {
const dstate = stendhal.config.dialogstates["outfit"];
const dstate = stendhal.config.windowstates["outfit"];
outfitDialog = new OutfitDialog();
new FloatingWindow("Choose outfit", outfitDialog, dstate.x, dstate.y);
}
Expand Down
2 changes: 1 addition & 1 deletion srcjs/stendhal/event/ProgressStatusEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class ProgressStatusEvent extends RPEvent {
let travelLogDialog = ui.get(UIComponentEnum.TravelLogDialog) as TravelLogDialog;
if (!this["progress_type"]) {
if (!travelLogDialog) {
const dstate = stendhal.config.dialogstates["travellog"];
const dstate = stendhal.config.windowstates["travellog"];
travelLogDialog = new TravelLogDialog(dataItems);
new FloatingWindow("Travel Log", travelLogDialog, dstate.x, dstate.y);
}
Expand Down
2 changes: 1 addition & 1 deletion srcjs/stendhal/event/TradeEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export class TradeEvent extends RPEvent {
let dialog = ui.get(UIComponentEnum.TradeDialog) as TradeDialog;

if (this["user_trade_state"] !== "NO_ACTIVE_TRADE" && !dialog) {
const dstate = stendhal.config.dialogstates["trade"];
const dstate = stendhal.config.windowstates["trade"];
dialog = new TradeDialog();
new FloatingWindow("Trade", dialog, dstate.x, dstate.y);
}
Expand Down
1 change: 1 addition & 0 deletions srcjs/stendhal/ui/toolkit/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export abstract class Component {
*
* @param cid
* The string identifier.
* @deprecated
*/
public setConfigId(cid: string) {
this.cid = cid;
Expand Down
7 changes: 5 additions & 2 deletions srcjs/stendhal/ui/toolkit/DialogContentComponent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ export abstract class DialogContentComponent extends ThemedComponent {
this.componentElement.classList.add("dialogcontents");
}

/**
* @deprecated
*/
public updateConfig(newX: number, newY: number) {
const cid = this.getConfigId();
if (stendhal.config.dialogstates[cid]) {
stendhal.config.dialogstates[cid] = {x: newX, y: newY};
if (stendhal.config.windowstates[cid]) {
stendhal.config.windowstates[cid] = {x: newX, y: newY};
}
}

Expand Down
14 changes: 12 additions & 2 deletions srcjs/stendhal/ui/toolkit/FloatingWindow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ export class FloatingWindow extends Component {

private content: Component;

private windowId?: string;


constructor(title: string, protected contentComponent: Component, x: number, y: number) {
super("window-template");

Expand Down Expand Up @@ -163,7 +166,7 @@ export class FloatingWindow extends Component {
window.removeEventListener("touchend", this.onMouseUpDuringDragListener, true);
}

private checkPos() {
private checkPos(): any {
if (this.content) {
this.content.onMoved();
}
Expand Down Expand Up @@ -201,6 +204,13 @@ export class FloatingWindow extends Component {
}

public override onMoved() {
this.checkPos();
const pos = this.checkPos();
if (typeof(this.windowId) !== "undefined") {
stendhal.config.setWindowState(this.windowId, pos.x, pos.y);
}
}

public setId(id: string|undefined) {
this.windowId = id;
}
}
55 changes: 47 additions & 8 deletions srcjs/stendhal/util/ConfigManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class ConfigManager {
} as {[name: string]: string};

private storage = window.localStorage;
private dialogstates: any = {};
private windowstates: any = {};
private initialized = false;

/** Singleton instance. */
Expand Down Expand Up @@ -122,13 +122,13 @@ export class ConfigManager {

// store window information for this session
// TODO: move this into "session" file
this.dialogstates["chest"] = {x: 160, y: 370};
this.dialogstates["corpse"] = {x: 160, y: 370};
this.dialogstates["menu"] = {x: 150, y: 20};
this.dialogstates["outfit"] = {x: 300, y: 50};
this.dialogstates["settings"] = {x: 20, y: 20};
this.dialogstates["trade"] = {x: 200, y: 100};
this.dialogstates["travellog"] = {x: 160, y: 50};
this.windowstates["chest"] = {x: 160, y: 370};
this.windowstates["corpse"] = {x: 160, y: 370};
this.windowstates["menu"] = {x: 150, y: 20};
this.windowstates["outfit"] = {x: 300, y: 50};
this.windowstates["settings"] = {x: 20, y: 20};
this.windowstates["trade"] = {x: 200, y: 100};
this.windowstates["travellog"] = {x: 160, y: 50};
this.initialized = true;
}

Expand Down Expand Up @@ -258,6 +258,45 @@ export class ConfigManager {
this.storage.clear();
}

/**
* Sets attributes for a dialog window.
*
* TODO: move into session manager
*
* @param id
* Dialog identifier.
* @param x
* Horizontal position.
* @param y
* Vertical position.
*/
setWindowState(id: string, x: number, y: number) {
this.windowstates[id] = {x: x, y: y};
this.set("ui.window." + id, x + "," + y);
}

/**
* Retrieves attributes for a dialog window.
*
* TODO: move into session manager
*
* @param id
* Dialog identifier.
* @return
* Object containing X/Y positioning of dialog.
*/
getWindowState(id: string): {[index: string]: number} {
let state: {[index: string]: number} = {};
if (this.windowstates.hasOwnProperty(id)) {
state = this.windowstates[id];
} else {
const tmp: string[] = (this.get("ui.window." + id) || "0,0").split(",");
state.x = parseInt(tmp[0], 10);
state.y = parseInt(tmp[1], 10);
}
return state;
}

/**
* Sets the UI theme.
*
Expand Down

0 comments on commit 2ad8ee6

Please sign in to comment.