Skip to content
This repository has been archived by the owner on Feb 4, 2024. It is now read-only.

Rework UI building #24

Merged
merged 9 commits into from
Apr 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ repositories {
eclipse {
classpath {
file {
whenMerged {
whenMerged {
def lib = entries.find { it.path.contains 'starfarer.api.jar' }
lib.javadocPath = fileReference(file('libs/starfarer.api.zip'))
lib.sourcePath = fileReference(file('libs/starfarer.api.zip'))
Expand Down Expand Up @@ -45,6 +45,10 @@ dependencies {
implementation group: 'net.java.jinput', name: 'jinput', version: '2.0.7'
implementation group: 'org.codehaus.janino', name: 'janino', version: '3.0.7'

// lombok
compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.20'
annotationProcessor 'org.projectlombok:lombok:1.18.20'

// test only
testImplementation 'junit:junit:4.12'
}
Expand Down
50 changes: 39 additions & 11 deletions src/main/java/stelnet/BaseBoard.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,55 @@
package stelnet;

import java.util.List;

import com.fs.starfarer.api.impl.campaign.intel.BaseIntelPlugin;
import com.fs.starfarer.api.ui.CustomPanelAPI;
import com.fs.starfarer.api.ui.IntelUIAPI;
import com.fs.starfarer.api.ui.TooltipMakerAPI;

import stelnet.helper.LogHelper;
import stelnet.ui.Callable;
import stelnet.ui.ButtonHandler;
import stelnet.ui.RenderableView;
import stelnet.ui.Size;

public abstract class BaseBoard extends BaseIntelPlugin {

@Override
public void buttonPressCancelled(Object buttonId, IntelUIAPI ui) {
LogHelper.debug("Calling cancel()");
ButtonHandler handler = (ButtonHandler) buttonId;
handler.onCancel(ui);
redraw(ui);
}

@Override
public void buttonPressConfirmed(Object buttonId, IntelUIAPI ui) {
LogHelper.debug("Calling callback.confirm()");
Callable callable = (Callable) buttonId;
callable.confirm(ui);
ui.recreateIntelUI();
ui.updateUIForItem(this);
LogHelper.debug("Calling confirm()");
ButtonHandler handler = (ButtonHandler) buttonId;
handler.onConfirm(ui);
redraw(ui);
}

@Override
public void createConfirmationPrompt(Object buttonId, TooltipMakerAPI prompt) {
LogHelper.debug("Calling callback.prompt()");
Callable callable = (Callable) buttonId;
callable.prompt(prompt);
LogHelper.debug("Calling prompt()");
ButtonHandler handler = (ButtonHandler) buttonId;
handler.onPrompt(prompt);
}

@Override
public void createLargeDescription(CustomPanelAPI panel, float width, float height) {
Size size = new Size(width, height);
for (RenderableView view : getRenderableViews()) {
view.render(panel, size);
}
}

@Override
public boolean doesButtonHaveConfirmDialog(Object buttonId) {
Callable callable = (Callable) buttonId;
return callable.hasPrompt();
LogHelper.debug("Calling hasPrompt()");
ButtonHandler handler = (ButtonHandler) buttonId;
return handler.hasPrompt();
}

@Override
Expand All @@ -45,4 +66,11 @@ public boolean hasSmallDescription() {
public IntelSortTier getSortTier() {
return IntelSortTier.TIER_0;
}

protected abstract List<RenderableView> getRenderableViews();

private void redraw(IntelUIAPI ui) {
ui.recreateIntelUI();
ui.updateUIForItem(this);
}
}
33 changes: 15 additions & 18 deletions src/main/java/stelnet/commodity/CommodityBoard.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package stelnet.commodity;

import java.util.List;
import java.util.Set;

import com.fs.starfarer.api.campaign.comm.IntelInfoPlugin;
Expand All @@ -9,17 +10,16 @@
import com.fs.starfarer.api.ui.SectorMapAPI;
import com.fs.starfarer.api.ui.TooltipMakerAPI;

import lombok.Setter;
import stelnet.BaseBoard;
import stelnet.commodity.view.ButtonViewFactory;
import stelnet.commodity.view.CommodityViewFactory;
import stelnet.commodity.view.DeleteButton;
import stelnet.commodity.view.DeleteViewFactory;
import stelnet.commodity.view.IntelSelectionFactory;
import stelnet.commodity.view.PurgeButton;
import stelnet.helper.IntelHelper;
import stelnet.helper.SettingHelper;
import stelnet.ui.GridRenderer;
import stelnet.ui.RenderableView;
import stelnet.ui.Size;
import stelnet.ui.Stack;

public class CommodityBoard extends BaseBoard {

Expand All @@ -33,7 +33,9 @@ private CommodityTab(String title) {
}
}

@Setter
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For future, if you end up with a class where you have 3-4 setters and maybe some instance variables you are not necessarily exposing already then it is ok practice to just add @Data on the class itself not micromanaging individual properties with Getter/Setter functionality.

If there are some properties that you do not want to be made accessible through getters/setters then you can use

@Getter(value = AccessLevel.NONE)
@Setter(value = AccessLevel.NONE)
private Datatype myVariableName;

As the code is generated then having lombok generate a method that is not used vs human is wasting time on reading dead code - there is a difference. I would rather read less and have some dead functions in compile time. Most of the time it will not hurt anybody.

There are exceptions to everything of course. When you are writing a library for example then obviously the exposed api has to be thought through.

private String activeId;
@Setter
private CommodityTab activeTab;
private ButtonViewFactory buttonViewFactory;
private CommodityViewFactory commodityViewFactory;
Expand Down Expand Up @@ -61,14 +63,11 @@ public void createIntelInfo(TooltipMakerAPI info, ListInfoMode mode) {

@Override
public void createLargeDescription(CustomPanelAPI panel, float width, float height) {
float commodityViewWidth = width - 210;
float commodityViewHeight = height - 35;
GridRenderer renderer = new GridRenderer(new Size(width, height));
renderer.setTopLeft(commodityViewFactory.get(activeId, activeTab, commodityViewWidth, commodityViewHeight));
renderer.setTopRight(buttonViewFactory.get(activeId));
renderer.setBottomLeft(intelSelectionFactory.get(activeId, activeTab, commodityViewWidth));
renderer.setBottomRight(new Stack(true, new PurgeButton(), new DeleteButton(activeId)));
renderer.render(panel);
Size size = new Size(width, height);
commodityViewFactory.get(activeId, activeTab, size).render(panel);
intelSelectionFactory.get(activeId, activeTab, size).render(panel);
buttonViewFactory.get(activeId, size).render(panel);
new DeleteViewFactory().get(activeId, size).render(panel);
}

@Override
Expand All @@ -83,12 +82,10 @@ public Set<String> getIntelTags(SectorMapAPI map) {
return tags;
}

public void setActiveId(String activeId) {
this.activeId = activeId;
}

public void setActiveTab(CommodityTab activeTab) {
this.activeTab = activeTab;
@Override
protected List<RenderableView> getRenderableViews() {
// TODO Auto-generated method stub, rework createLargeDescription
return null;
}

protected Object readResolve() {
Expand Down
11 changes: 3 additions & 8 deletions src/main/java/stelnet/commodity/CommodityIntel.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@
import com.fs.starfarer.api.ui.TooltipMakerAPI;
import com.fs.starfarer.api.util.Misc;

import lombok.Getter;
import stelnet.commodity.data.Price;
import stelnet.helper.StarSystemHelper;

public class CommodityIntel extends BaseIntelPlugin {

public final static String TAG = "stelnetCommodity";

@Getter
private String action;
private CommoditySpecAPI commodity;
@Getter
private MarketAPI market;
private IntelTracker tracker;
private Price priceProvider;
Expand Down Expand Up @@ -132,18 +135,10 @@ public void delete() {
tracker.remove(this);
}

public String getAction() {
return action;
}

public String getCommodityId() {
return commodity.getId();
}

public MarketAPI getMarket() {
return market;
}

private String getTitle() {
return String.format("%s %s for %s", action, commodity.getName(), Misc.getDGSCredits(price));
}
Expand Down
11 changes: 7 additions & 4 deletions src/main/java/stelnet/commodity/view/ButtonViewFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@
import stelnet.helper.GlobalHelper;
import stelnet.ui.Button;
import stelnet.ui.Renderable;
import stelnet.ui.Size;
import stelnet.ui.Stack;

public class ButtonViewFactory {

public Stack get(String activeId) {
public Renderable get(String activeId, Size size) {
EconomyAPI economy = GlobalHelper.getEconomy();
List<Renderable> buttons = new LinkedList<>();
List<String> commodityIds = economy.getAllCommodityIds();
Expand All @@ -26,9 +27,12 @@ public Stack get(String activeId) {
buttons.add(get(commodity, activeId));
}
}
return new Stack(buttons);
Renderable stack = new Stack(buttons);
stack.setSize(size);
return stack;
}

// TODO: Replace this with a Filter
private boolean canInclude(CommoditySpecAPI commodity) {
if (commodity.hasTag("nonecon")) {
return false;
Expand All @@ -41,8 +45,7 @@ private boolean canInclude(CommoditySpecAPI commodity) {

private Button get(CommoditySpecAPI commodity, String activeId) {
boolean isOn = commodity.getId().equals(activeId);
Button button = new CommodityButton(commodity, isOn);
return button;
return new CommodityButton(commodity, isOn);
}

private void sortCommodities(List<String> commodityIds) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/stelnet/commodity/view/CommodityButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import com.fs.starfarer.api.util.Misc;

import stelnet.commodity.CommodityBoard;
import stelnet.ui.SimpleCallback;
import stelnet.ui.Location;
import stelnet.ui.SimpleHandler;
import stelnet.ui.Size;
import stelnet.ui.ToggleButton;

Expand All @@ -14,11 +15,11 @@ public class CommodityButton extends ToggleButton {
public CommodityButton(final CommoditySpecAPI commodity, boolean isOn) {
super(new Size(200, 24), commodity.getName(), commodity.getName(), true, Misc.getHighlightColor(),
Misc.getGrayColor(), isOn);

setCallback(new SimpleCallback() {
setLocation(Location.TOP_RIGHT);
setHandler(new SimpleHandler() {

@Override
public void confirm(IntelUIAPI ui) {
public void onConfirm(IntelUIAPI ui) {
String commodityId = commodity.getId();
CommodityBoard board = CommodityBoard.getInstance();
board.setActiveId(commodityId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import stelnet.commodity.data.SellTableContent;
import stelnet.ui.Renderable;
import stelnet.ui.Row;
import stelnet.ui.Size;
import stelnet.ui.Stack;
import stelnet.ui.Table;
import stelnet.ui.TableContent;
Expand All @@ -27,7 +28,9 @@ public CommodityViewFactory(IntelSelectionFactory intelSelectionFactory) {
this.intelSelectionFactory = intelSelectionFactory;
}

public Renderable get(String commodityId, CommodityTab activeTab, float width, float height) {
public Renderable get(String commodityId, CommodityTab activeTab, Size size) {
float width = size.getWidth() - 210;
float height = size.getHeight() - 35;
float tabsHeight = 15f;
float tableHeight = height - tabsHeight;
TableContent tableContent = getTableContent(commodityId, activeTab);
Expand Down
25 changes: 11 additions & 14 deletions src/main/java/stelnet/commodity/view/DeleteButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,35 @@
import stelnet.commodity.CommodityIntel;
import stelnet.helper.IntelHelper;
import stelnet.ui.Button;
import stelnet.ui.SimpleCallback;
import stelnet.ui.Location;
import stelnet.ui.SimpleHandler;
import stelnet.ui.Size;

public class DeleteButton extends Button {

public DeleteButton(final String commodityId) {
super(new Size(200, 24), "Delete This", true, Misc.getButtonTextColor());
setCallback(new SimpleCallback() {
setLocation(Location.BOTTOM_RIGHT);
setHandler(new SimpleHandler() {

@Override
public void confirm(IntelUIAPI ui) {
boolean needRefresh = false;
public boolean hasPrompt() {
return true;
}

@Override
public void onConfirm(IntelUIAPI ui) {
List<IntelInfoPlugin> intels = IntelHelper.getAll(CommodityIntel.class);
for (int i = intels.size(); i > 0; i--) {
CommodityIntel commodityIntel = (CommodityIntel) intels.get(i - 1);
if (commodityIntel.getCommodityId().equals(commodityId)) {
commodityIntel.delete();
needRefresh = true;
}
}
if (needRefresh) {
ui.recreateIntelUI();
}
}

@Override
public boolean hasPrompt() {
return true;
}

@Override
public void prompt(TooltipMakerAPI tooltipMaker) {
public void onPrompt(TooltipMakerAPI tooltipMaker) {
tooltipMaker.addPara("Are you sure you want to delete all intel for this commodity?",
Misc.getTextColor(), 0f);
}
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/stelnet/commodity/view/DeleteViewFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package stelnet.commodity.view;

import stelnet.ui.Renderable;
import stelnet.ui.Size;
import stelnet.ui.Stack;

public class DeleteViewFactory {

public Renderable get(String activeId, Size size) {
Renderable stack = new Stack(new PurgeButton(), new DeleteButton(activeId));
stack.setSize(size);
return stack;
}
}
9 changes: 5 additions & 4 deletions src/main/java/stelnet/commodity/view/IntelButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import stelnet.commodity.CommodityBoard.CommodityTab;
import stelnet.commodity.IntelTracker;
import stelnet.ui.SimpleCallback;
import stelnet.ui.Location;
import stelnet.ui.SimpleHandler;
import stelnet.ui.Size;
import stelnet.ui.ToggleButton;

Expand All @@ -18,12 +19,12 @@ public IntelButton(int i, final CommodityTab commodityTab, final String commodit
super(new Size(28f, 24f), String.valueOf(i), String.valueOf(i), true, Misc.getTextColor(), Misc.getGrayColor(),
tracker.has(commodityTab.title, commodityId, market));
setCutStyle(CutStyle.TL_BR);
setCallback(new SimpleCallback() {
setLocation(Location.BOTTOM_LEFT);
setHandler(new SimpleHandler() {

@Override
public void confirm(IntelUIAPI ui) {
public void onConfirm(IntelUIAPI ui) {
tracker.toggle(commodityId, commodityTab, market);
ui.recreateIntelUI();
}
});
}
Expand Down
Loading