v1.2.0: improved offset logic, and stacked windows now share offsets

This commit is contained in:
Evan Debenham 2022-02-02 20:29:13 -05:00
parent 15e4ff15e6
commit c80f89133a
3 changed files with 67 additions and 22 deletions

View File

@ -39,7 +39,6 @@ import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.Mob;
import com.shatteredpixel.shatteredpixeldungeon.actors.mobs.Snake;
import com.shatteredpixel.shatteredpixeldungeon.effects.BannerSprites;
import com.shatteredpixel.shatteredpixeldungeon.effects.BlobEmitter;
import com.shatteredpixel.shatteredpixeldungeon.effects.CircleArc;
import com.shatteredpixel.shatteredpixeldungeon.effects.EmoIcon;
import com.shatteredpixel.shatteredpixeldungeon.effects.Flare;
import com.shatteredpixel.shatteredpixeldungeon.effects.FloatingText;
@ -121,6 +120,7 @@ import com.watabou.noosa.audio.Sample;
import com.watabou.noosa.particles.Emitter;
import com.watabou.utils.DeviceCompat;
import com.watabou.utils.GameMath;
import com.watabou.utils.Point;
import com.watabou.utils.Random;
import com.watabou.utils.RectF;
@ -667,6 +667,8 @@ public class GameScene extends PixelScene {
@Override
public synchronized void update() {
lastOffset = null;
if (Dungeon.hero == null || scene == null) {
return;
}
@ -734,6 +736,17 @@ public class GameScene extends PixelScene {
toDestroy.clear();
}
private Point lastOffset = null;
@Override
public synchronized Gizmo erase (Gizmo g) {
Gizmo result = super.erase(g);
if (result instanceof Window){
lastOffset = ((Window) result).getOffset();
}
return result;
}
private boolean tagAttack = false;
private boolean tagLoot = false;
private boolean tagAction = false;
@ -1050,6 +1063,23 @@ public class GameScene extends PixelScene {
public static void show( Window wnd ) {
if (scene != null) {
cancelCellSelector();
//If a window is already present (or was just present)
// then inherit the offset it had
if (scene.inventory != null && scene.inventory.visible){
Point offsetToInherit = null;
for (Gizmo g : scene.members){
if (g instanceof Window) offsetToInherit = ((Window) g).getOffset();
}
if (scene.lastOffset != null && offsetToInherit == null) {
offsetToInherit = scene.lastOffset;
}
if (offsetToInherit != null) {
wnd.offset(offsetToInherit);
wnd.boundOffsetWithMargin(3);
}
}
scene.addToFront(wnd);
}
}

View File

@ -21,35 +21,26 @@
package com.shatteredpixel.shatteredpixeldungeon.ui;
import com.shatteredpixel.shatteredpixeldungeon.Assets;
import com.shatteredpixel.shatteredpixeldungeon.Chrome;
import com.shatteredpixel.shatteredpixeldungeon.Dungeon;
import com.shatteredpixel.shatteredpixeldungeon.actors.buffs.LostInventory;
import com.shatteredpixel.shatteredpixeldungeon.actors.hero.Belongings;
import com.shatteredpixel.shatteredpixeldungeon.items.EquipableItem;
import com.shatteredpixel.shatteredpixeldungeon.items.Gold;
import com.shatteredpixel.shatteredpixeldungeon.items.Item;
import com.shatteredpixel.shatteredpixeldungeon.items.bags.Bag;
import com.shatteredpixel.shatteredpixeldungeon.items.bags.MagicalHolster;
import com.shatteredpixel.shatteredpixeldungeon.items.bags.PotionBandolier;
import com.shatteredpixel.shatteredpixeldungeon.items.bags.ScrollHolder;
import com.shatteredpixel.shatteredpixeldungeon.items.bags.VelvetPouch;
import com.shatteredpixel.shatteredpixeldungeon.items.wands.Wand;
import com.shatteredpixel.shatteredpixeldungeon.scenes.PixelScene;
import com.shatteredpixel.shatteredpixeldungeon.sprites.ItemSpriteSheet;
import com.shatteredpixel.shatteredpixeldungeon.utils.GLog;
import com.shatteredpixel.shatteredpixeldungeon.windows.WndBag;
import com.shatteredpixel.shatteredpixeldungeon.windows.WndUseItem;
import com.watabou.gltextures.TextureCache;
import com.watabou.input.PointerEvent;
import com.watabou.noosa.BitmapText;
import com.watabou.noosa.ColorBlock;
import com.watabou.noosa.Game;
import com.watabou.noosa.Gizmo;
import com.watabou.noosa.Image;
import com.watabou.noosa.NinePatch;
import com.watabou.noosa.PointerArea;
import com.watabou.noosa.audio.Sample;
import com.watabou.noosa.ui.Component;
import java.util.ArrayList;
@ -113,18 +104,9 @@ public class InventoryPane extends Component {
updateInventory();
} else {
Window w = new WndUseItem( null, item );
int xOfs, yOfs;
if (w.height > InventoryPane.this.height - 15){
yOfs = (camera.height - w.height)/2 - 9;
} else {
yOfs = (int)(InventoryPane.this.y)/2;
}
if (w.width > InventoryPane.this.width - 15){
xOfs = (camera.width - w.width)/2 - 9;
} else {
xOfs = (int)(InventoryPane.this.x)/2;
}
w.offset(xOfs, yOfs);
w.offset( (int)InventoryPane.this.centerX() - camera.width/2,
(int)InventoryPane.this.centerY() - camera.height/2 );
w.boundOffsetWithMargin(3);
Game.scene().addToFront(w);
}
}

View File

@ -33,6 +33,7 @@ import com.watabou.noosa.Game;
import com.watabou.noosa.Group;
import com.watabou.noosa.NinePatch;
import com.watabou.noosa.PointerArea;
import com.watabou.utils.Point;
import com.watabou.utils.Signal;
public class Window extends Group implements Signal.Listener<KeyEvent> {
@ -131,6 +132,14 @@ public class Window extends Group implements Signal.Listener<KeyEvent> {
shadow.boxRect( camera.x / camera.zoom, camera.y / camera.zoom, chrome.width(), chrome.height );
}
public Point getOffset(){
return new Point(xOffset, yOffset);
}
public final void offset( Point offset ){
offset(offset.x, offset.y);
}
//windows with scroll panes will likely need to override this and refresh them when offset changes
public void offset( int xOffset, int yOffset ){
camera.x -= this.xOffset * camera.zoom;
@ -143,6 +152,30 @@ public class Window extends Group implements Signal.Listener<KeyEvent> {
shadow.boxRect( camera.x / camera.zoom, camera.y / camera.zoom, chrome.width(), chrome.height );
}
//ensures the window, with offset, does not go beyond a given margin
public void boundOffsetWithMargin( int margin ){
float x = camera.x / camera.zoom;
float y = camera.y / camera.zoom;
Camera sceneCam = PixelScene.uiCamera.visible ? PixelScene.uiCamera : Camera.main;
int newXOfs = xOffset;
if (x < margin){
newXOfs += margin - x;
} else if (x + camera.width > sceneCam.width - margin){
newXOfs += (sceneCam.width - margin) - (x + camera.width);
}
int newYOfs = yOffset;
if (y < margin){
newYOfs += margin - y;
} else if (y + camera.height > sceneCam.height - margin){
newYOfs += (sceneCam.height - margin) - (y + camera.height);
}
offset(newXOfs, newYOfs);
}
public void hide() {
if (parent != null) {