Skip to content

Commit

Permalink
Merge pull request #67 from scardwell15/subsystem-matching-bar-length
Browse files Browse the repository at this point in the history
extends bar widths to account for any subsystem name
  • Loading branch information
wispborne authored Feb 22, 2024
2 parents 97be21d + f84f294 commit cd7be54
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 20 deletions.
1 change: 1 addition & 0 deletions data/strings/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@
"ml_mp_appliedRefit":"Applied: %s",

"subsystemTitleText": "SUBSYSTEMS",
"subsystemNameWithKeyText": "%s [%s]",
"subsystemMoreInfoText": "PRESS %s FOR MORE INFO",
"subsystemLessInfoText": "PRESS %s FOR LESS INFO",
"subsystemHotkeyText": "HOTKEY: %s",
Expand Down
Binary file modified jars/MagicLib.jar
Binary file not shown.
28 changes: 15 additions & 13 deletions src/org/magiclib/subsystems/CombatUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,19 @@ public static boolean getHasRenderedSpatial() {
* method) then they will not have an effect on the output location. This must be predetermined by the input
* guiBarCount parameter to make room for them.
*
* @param ship Player ship
* @param fill Value 0 to 1, how full the bar is from left to right
* @param name Name of subsystem
* @param extraText Info string opportunity. Appears to the right of the status bar.
* @param extraTextColor color of extraText. if null, uses standard text color
* @param stateText Subsystem activity status. Appears to left of status bar.
* @param hotkey Hotkey string of key used to activate subsystem
* @param briefText A brief description of what the subsystem does
* @param showInfoText If the subsystem is in show info mode
* @param guiBarCount The number of gui bars this subsystem will use
* @param inputLoc the Input location (top left) of the subsystem GUI element
* @param rootLoc the Root location of subsystem GUI elements
* @param ship Player ship
* @param fill Value 0 to 1, how full the bar is from left to right
* @param name Name of subsystem
* @param extraText Info string opportunity. Appears to the right of the status bar.
* @param extraTextColor color of extraText. if null, uses standard text color
* @param stateText Subsystem activity status. Appears to left of status bar.
* @param hotkey Hotkey string of key used to activate subsystem
* @param briefText A brief description of what the subsystem does
* @param showInfoText If the subsystem is in show info mode
* @param guiBarCount The number of gui bars this subsystem will use
* @param inputLoc the Input location (top left) of the subsystem GUI element
* @param extraBarPadding
* @param rootLoc the Root location of subsystem GUI elements
* @return The output location (bottom left) of GUI element
* @author tomatopaste
*/
Expand All @@ -112,6 +113,7 @@ public static Vector2f drawSubsystemStatus(
boolean showInfoText,
int guiBarCount,
Vector2f inputLoc,
float extraBarPadding,
Vector2f rootLoc) {
CombatEngineAPI engine = Global.getCombatEngine();
Color colour = (ship.isAlive()) ? MagicUI.GREENCOLOR : MagicUI.BLUCOLOR;
Expand Down Expand Up @@ -147,7 +149,7 @@ public static Vector2f drawSubsystemStatus(
}

Vector2f boxLoc = new Vector2f(loc);
boxLoc.x += MagicUI.scale(STATUS_BAR_PADDING);
boxLoc.x += MagicUI.scale(STATUS_BAR_PADDING) + MagicUI.scale(extraBarPadding);

final float boxHeight = MagicUI.scale(STATUS_BAR_HEIGHT);
final float boxEndWidth = MagicUI.scale(STATUS_BAR_WIDTH);
Expand Down
10 changes: 7 additions & 3 deletions src/org/magiclib/subsystems/MagicSubsystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -969,13 +969,14 @@ public Vector2f getBarLocationForBarNum(Vector2f baseBarLoc, int barNum) {
* @param rootLoc root location of subsystem info
* @param barLoc location to draw (top left)
* @param displayAdditionalInfo display additional subsystem info (hotkey, brief)
* @param longestNameWidth longest name width of all subsystems on the ship, for making the UI look uniform
*/
public void drawHUDBar(ViewportAPI viewport, Vector2f rootLoc, Vector2f barLoc, boolean displayAdditionalInfo) {
public void drawHUDBar(ViewportAPI viewport, Vector2f rootLoc, Vector2f barLoc, boolean displayAdditionalInfo, float longestNameWidth) {
String nameText = getDisplayText();
String keyText = getKeyText();

if (!displayAdditionalInfo && !keyText.equals(BLANK_KEY)) {
nameText = String.format("%s [%s]", nameText, keyText);
nameText = MagicTxt.getString("subsystemNameWithKeyText", nameText, keyText);
}

boolean displayStateText = true;
Expand Down Expand Up @@ -1006,6 +1007,7 @@ public void drawHUDBar(ViewportAPI viewport, Vector2f rootLoc, Vector2f barLoc,
stateText = null;
}

float additionalBarPadding = Math.max(0f, longestNameWidth - CombatUI.STATUS_BAR_PADDING);
CombatUI.drawSubsystemStatus(
ship,
getBarFill(),
Expand All @@ -1018,14 +1020,16 @@ public void drawHUDBar(ViewportAPI viewport, Vector2f rootLoc, Vector2f barLoc,
displayAdditionalInfo,
getNumHUDBars(),
barLoc,
additionalBarPadding,
rootLoc
);

if (hasCharges()) {
CombatUI.renderAuxiliaryStatusBar(
ship,
CombatUI.INFO_TEXT_PADDING,
false, CombatUI.STATUS_BAR_PADDING - CombatUI.INFO_TEXT_PADDING,
false,
CombatUI.STATUS_BAR_PADDING - CombatUI.INFO_TEXT_PADDING + additionalBarPadding,
CombatUI.STATUS_BAR_WIDTH,
chargeInterval.getElapsed() / chargeInterval.getIntervalDuration(),
getAmmoText(),
Expand Down
19 changes: 18 additions & 1 deletion src/org/magiclib/subsystems/MagicSubsystemsCombatPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import com.fs.starfarer.api.Global
import com.fs.starfarer.api.combat.*
import com.fs.starfarer.api.input.InputEventAPI
import org.lwjgl.util.vector.Vector2f
import org.magiclib.util.MagicTxt
import org.magiclib.util.MagicUI
import java.util.*
import kotlin.math.roundToInt

class MagicSubsystemsCombatPlugin : BaseEveryFrameCombatPlugin() {
companion object {
Expand Down Expand Up @@ -87,12 +90,26 @@ class MagicSubsystemsCombatPlugin : BaseEveryFrameCombatPlugin() {
totalBars += subsystems.size
}

val longestNameLength = MagicUI.getTextWidthUnscaled(subsystems
.map {
if (displayAdditionalInfo)
it.displayText
else
MagicTxt.getString(
"subsystemNameWithKeyText",
it.displayText,
it.keyText
)
}
.maxByOrNull { it.length }!!
) + MagicUI.getTextWidthUnscaled(MagicTxt.getString("subsystemState_Active"))

val rootVec = CombatUI.getSubsystemsRootLocation(ship, totalBars, barHeight)
var lastVec = Vector2f(rootVec)
MagicSubsystemsManager.sortSubsystems(subsystems)
.forEach { subsystem ->
val numBars = subsystem.numHUDBars + if (displayAdditionalInfo) 1 else 0
subsystem.drawHUDBar(viewport, rootVec, lastVec, displayAdditionalInfo)
subsystem.drawHUDBar(viewport, rootVec, lastVec, displayAdditionalInfo, longestNameLength.roundToInt().toFloat())
lastVec = Vector2f.add(lastVec, Vector2f(0f, -barHeight * numBars), null)
}
CombatUI.drawSubsystemsTitle(ship, true, rootVec, displayAdditionalInfo)
Expand Down
8 changes: 5 additions & 3 deletions src/org/magiclib/subsystems/drones/MagicDroneSubsystem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -368,9 +368,10 @@ abstract class MagicDroneSubsystem(ship: ShipAPI) : MagicSubsystem(ship) {
viewport: ViewportAPI,
rootLoc: Vector2f,
barLoc: Vector2f,
displayAdditionalInfo: Boolean
displayAdditionalInfo: Boolean,
longestNameWidth: Float
) {
super.drawHUDBar(viewport, rootLoc, barLoc, displayAdditionalInfo)
super.drawHUDBar(viewport, rootLoc, barLoc, displayAdditionalInfo, longestNameWidth)

val colour = if (ship.isAlive) hudColor else MagicUI.BLUCOLOR

Expand Down Expand Up @@ -406,11 +407,12 @@ abstract class MagicDroneSubsystem(ship: ShipAPI) : MagicSubsystem(ship) {
val chevronRow = if (hasCharges()) 2 else 1
val chevronRowPos = getBarLocationForBarNum(barLoc, chevronRow)

val additionalBarPadding = (longestNameWidth - CombatUI.STATUS_BAR_PADDING).coerceAtLeast(0f)
CombatUI.renderAuxiliaryStatusBar(
ship,
CombatUI.INFO_TEXT_PADDING,
false,
CombatUI.STATUS_BAR_PADDING - CombatUI.INFO_TEXT_PADDING,
CombatUI.STATUS_BAR_PADDING - CombatUI.INFO_TEXT_PADDING + additionalBarPadding,
CombatUI.STATUS_BAR_WIDTH,
forgeCooldown,
forgeText,
Expand Down
16 changes: 16 additions & 0 deletions src/org/magiclib/util/MagicUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ public static float getTextWidth(String text) {
return width;
}

/**
* Gets unscaled text width by unscaling the Victor14 font and then rescaling it.
* @param text text to measure
* @return unscaled width
*/
public static float getTextWidthUnscaled(String text) {
float fontSize = TODRAW14.getFontSize();
String oldText = TODRAW14.getText();
TODRAW14.setFontSize(14);
TODRAW14.setText(text);
float width = TODRAW14.getWidth();
TODRAW14.setFontSize(fontSize);
TODRAW14.setText(oldText);
return width;
}

public static void setTextAligned(LazyFont.TextAlignment alignment) {
TODRAW14.setAlignment(alignment);
}
Expand Down

0 comments on commit cd7be54

Please sign in to comment.