Skip to content

Commit

Permalink
Dialog refactoring #15
Browse files Browse the repository at this point in the history
  • Loading branch information
Tim De Mey committed Dec 30, 2020
1 parent be44301 commit fcd7ffb
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import gent.timdemey.cards.services.interfaces.IFrameService;
import gent.timdemey.cards.services.panels.PanelButtonType;
import gent.timdemey.cards.services.panels.PanelOutData;
import gent.timdemey.cards.services.panels.mp.LobbyPanelManager;

public class D_ShowLobby extends DialogCommandBase
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package gent.timdemey.cards.model.entities.commands;

import gent.timdemey.cards.Services;
import gent.timdemey.cards.model.entities.commands.contract.CanExecuteResponse;
import gent.timdemey.cards.model.state.State;
import gent.timdemey.cards.services.context.Context;
import gent.timdemey.cards.services.context.ContextType;
import gent.timdemey.cards.services.interfaces.IDialogService;

public abstract class DialogCommandBase extends CommandBase
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,8 @@

public class DataPanelDescriptor<IN, OUT> extends PanelDescriptor
{
public DataPanelDescriptor(String id, int layer)
public DataPanelDescriptor(String id, PanelType panelType)
{
super(id, layer);
}

@Override
public String toString()
{
return String.format("DataPanelDescriptor [id=%s, layer=%s]", id, layer);
super(id, panelType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
public class PanelDescriptor
{
public final String id;
public final int layer;
public final PanelType panelType;

public PanelDescriptor(String id, int layer)
public PanelDescriptor(String id, PanelType panelType)
{
this.id = id;
this.layer = layer;
this.panelType = panelType;
}

@Override
public String toString()
public final String toString()
{
return String.format("PanelDescriptor [id=%s, layer=%s]", id, layer);
return String.format("%s [id=%s, PanelType=%s]", getClass().getSimpleName(), id, panelType);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

public final class PanelDescriptors
{
public static final PanelDescriptor GAME = new PanelDescriptor("Game", 500);
public static final PanelDescriptor LOAD = new PanelDescriptor("Load", 600);
public static final PanelDescriptor MENU = new PanelDescriptor("Menu", 700);
public static final DataPanelDescriptor<String, Void> MESSAGE = new DataPanelDescriptor<>("Message", 900);
public static final DataPanelDescriptor<Void, JoinMPGamePanelData> CONNECT = new DataPanelDescriptor<>("Connect", 800);
public static final PanelDescriptor JOINMP = new PanelDescriptor("JoinMP", 800);
public static final DataPanelDescriptor<Void, Void> LOBBY = new DataPanelDescriptor<>("Lobby", 900);
public static final DataPanelDescriptor<String, StartServerPanelData> START_SERVER = new DataPanelDescriptor<>("StartServer", 900);
public static final PanelDescriptor GAME = new PanelDescriptor("Game", PanelType.Root);
public static final PanelDescriptor LOAD = new PanelDescriptor("Load", PanelType.Overlay);
public static final PanelDescriptor MENU = new PanelDescriptor("Menu", PanelType.Root);
public static final DataPanelDescriptor<String, Void> MESSAGE = new DataPanelDescriptor<>("Message", PanelType.Dialog);
public static final DataPanelDescriptor<Void, JoinMPGamePanelData> CONNECT = new DataPanelDescriptor<>("Connect", PanelType.Dialog);
public static final PanelDescriptor JOINMP = new PanelDescriptor("JoinMP", PanelType.Dialog);
public static final DataPanelDescriptor<Void, Void> LOBBY = new DataPanelDescriptor<>("Lobby", PanelType.Dialog);
public static final DataPanelDescriptor<String, StartServerPanelData> START_SERVER = new DataPanelDescriptor<>("StartServer", PanelType.Dialog);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package gent.timdemey.cards.services.contract.descriptors;

public enum PanelType
{
/**
* Root panel, there is always exactly 1 showing in the frame. Cannot
* be closed or hidden except when showing a different root panel.
*/
Root,

/**
* Dialog panel, can be stacked on top of each other. When closing one,
* we return to the topmost open dialog or the root panel. A dialog must
* be explicitly closed, and blocks input to any panels below it.
*/
Dialog,

/**
* Standalone panel that can always be shown e.g. a breadcrumb or a HUD.
* Doesn't block input to other panels. Can be shown permanently.
*/
Overlay
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import gent.timdemey.cards.services.contract.descriptors.DataPanelDescriptor;
import gent.timdemey.cards.services.contract.descriptors.PanelDescriptor;
import gent.timdemey.cards.services.contract.descriptors.PanelDescriptors;
import gent.timdemey.cards.services.contract.descriptors.PanelType;
import gent.timdemey.cards.services.contract.res.FontResource;
import gent.timdemey.cards.services.contract.res.ImageResource;
import gent.timdemey.cards.services.interfaces.IFrameService;
Expand Down Expand Up @@ -81,7 +82,9 @@ private OpenPanel(PanelDescriptor panelDesc, PanelBase panel)
}
}

private Stack<OpenPanel> openPanels = new Stack<>();
private OpenPanel currPanel = null;
private Stack<OpenPanel> dialogs = new Stack<>();
private Stack<OpenPanel> overlays = new Stack<>();

private JButton createFrameButton(ActionDescriptor desc)
{
Expand Down Expand Up @@ -216,31 +219,15 @@ public void showPanel(PanelDescriptor desc)

showInternal(desc, pb, add);
}

@Override
public PanelDescriptor getCurrentPanel()
{
if (openPanels.size() == 0)
{
return null;
}

return openPanels.peek().panelDesc;
}

@Override
public <IN, OUT> void showPanel(DataPanelDescriptor<IN, OUT> desc, IN data, Consumer<PanelOutData<OUT>> onClose)
{
PanelBase pb = createDialog(desc, data, onClose);
openPanels.push(new OpenPanel(desc, pb));
dialogs.push(new OpenPanel(desc, pb));
showInternal(desc, pb, true);
}

private boolean isOverlay (PanelDescriptor desc)
{
return desc instanceof DataPanelDescriptor<?,?>;
}


private void showInternal(PanelDescriptor desc, PanelBase pb, boolean add)
{
IPanelService panelServ = Services.get(IPanelService.class);
Expand All @@ -255,31 +242,18 @@ private void showInternal(PanelDescriptor desc, PanelBase pb, boolean add)
cardPanel.validate();
}

// hide other panels if this panel is not just a (transparent) overlay
if (!isOverlay(desc))
// in case of a root panel, all open dialogs and overlays are hidden
if (desc.panelType == PanelType.Root)
{
List<PanelDescriptor> panelDescs = panelServ.getPanelDescriptors();
for (PanelDescriptor pd : panelDescs)
while (!dialogs.empty())
{
// don't hide the panel that needs to show
if (pd == desc)
{
continue;
}

IPanelManager pMan = panelServ.getPanelManager(pd);

// if the panel doesn't exist, it was never added to the card panel
if (!pMan.isCreated())
{
continue;
}

if (pMan.get().isVisible())
{
// hide the component
hidePanel(pd);
}
OpenPanel op = dialogs.peek();
hidePanel(op.panelDesc);
}
while (!overlays.empty())
{
OpenPanel op = dialogs.peek();
hidePanel(op.panelDesc);
}
}

Expand All @@ -289,7 +263,25 @@ private void showInternal(PanelDescriptor desc, PanelBase pb, boolean add)
}

@Override
public void hidePanel(PanelDescriptor desc)
public void closePanel()
{
closePanelInternal();

cardPanel.invalidate();
cardPanel.validate();
cardPanel.repaint();
}

public void closePanel(PanelType)
{
closePanelInternal();

cardPanel.invalidate();
cardPanel.validate();
cardPanel.repaint();
}

private void closePanelInternal()
{
IPanelService panelServ = Services.get(IPanelService.class);

Expand All @@ -300,11 +292,16 @@ public void hidePanel(PanelDescriptor desc)
throw new IllegalArgumentException("Attempted to hide a panel which has not been created yet");
}

boolean overlay = isOverlay(desc);
PanelBase pb;
if (overlay)
if (desc.panelType == PanelType.Dialog)
{
OpenPanel curr = dialogs.pop();
pb = curr.panel;
cardPanel.remove(pb);
}
else if (desc.panelType == PanelType.Overlay)
{
OpenPanel curr = openPanels.pop();
OpenPanel curr = overlays.pop();
pb = curr.panel;
cardPanel.remove(pb);
}
Expand All @@ -317,13 +314,8 @@ public void hidePanel(PanelDescriptor desc)
{
throw new IllegalStateException("Attempted to hide a panel which is not visible");
}

cardPanel.invalidate();
cardPanel.validate();
cardPanel.repaint();

// hide the panel and notify its manager

// hide the panel and notify its manager
pb.setVisible(false);
panelMgr.onHidden();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

import javax.swing.JPanel;


public class RootPanel extends JPanel
{
private final BufferedImage tile;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ public interface IFrameService
public <IN, OUT> void showPanel(DataPanelDescriptor<IN, OUT> desc, IN data, Consumer<PanelOutData<OUT>> onClose);
public void showMessage(String title, String message);
public void showInternalError();
public void hidePanel(PanelDescriptor desc);
public void removePanel(PanelDescriptor desc);
public PanelDescriptor getCurrentPanel();
public void closePanel();
public void removePanel(PanelDescriptor desc);

public void maximize();
public void minimize();
Expand Down

0 comments on commit fcd7ffb

Please sign in to comment.