Skip to content

Commit c107dea

Browse files
committed
the enroute GUI is DONE
Signed-off-by: Octol1ttle <[email protected]>
1 parent e6710ab commit c107dea

File tree

7 files changed

+107
-48
lines changed

7 files changed

+107
-48
lines changed

src/main/kotlin/ru/octol1ttle/flightassistant/api/util/extensions/CollectionExtensions.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,9 @@ fun List<AlertData>.getHighestPriority(): List<AlertData> {
3030
fun <T> List<T>.filterWorking(): List<T> {
3131
return this.filter { it !is Computer || !it.isDisabledOrFaulted() }
3232
}
33+
34+
fun <T> MutableList<T>.swap(index1: Int, index2: Int) {
35+
val entry1: T = this[index1]
36+
this[index1] = this[index2]
37+
this[index2] = entry1
38+
}

src/main/kotlin/ru/octol1ttle/flightassistant/api/util/extensions/GuiGraphicsExtensions.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,24 +78,24 @@ fun textWidth(text: String): Int {
7878
return font.width(text)
7979
}
8080

81-
fun GuiGraphics.drawString(text: String, x: Int, y: Int, color: Int) {
82-
drawString(font, text, x, y, color, false)
81+
fun GuiGraphics.drawString(text: String, x: Int, y: Int, color: Int, shadow: Boolean = false) {
82+
drawString(font, text, x, y, color, shadow)
8383
}
8484

85-
fun GuiGraphics.drawRightAlignedString(text: String, x: Int, y: Int, color: Int) {
86-
drawString(font, text, x - font.width(text), y, color, false)
85+
fun GuiGraphics.drawRightAlignedString(text: String, x: Int, y: Int, color: Int, shadow: Boolean = false) {
86+
drawString(font, text, x - font.width(text), y, color, shadow)
8787
}
8888

89-
fun GuiGraphics.drawMiddleAlignedString(text: String, x: Int, y: Int, color: Int) {
90-
drawString(font, text, x - font.width(text) / 2 + 1, y, color, false)
89+
fun GuiGraphics.drawMiddleAlignedString(text: String, x: Int, y: Int, color: Int, shadow: Boolean = false) {
90+
drawString(font, text, x - font.width(text) / 2 + 1, y, color, shadow)
9191
}
9292

9393
fun textWidth(formattedText: FormattedText): Int {
9494
return font.width(formattedText)
9595
}
9696

97-
fun GuiGraphics.drawString(text: Component, x: Int, y: Int, color: Int): Int {
98-
drawString(font, text, x, y, color, false)
97+
fun GuiGraphics.drawString(text: Component, x: Int, y: Int, color: Int, shadow: Boolean = false): Int {
98+
drawString(font, text, x, y, color, shadow)
9999
return 1
100100
}
101101

@@ -107,25 +107,25 @@ private fun getContrasting(original: Int): Int {
107107
return if (luma > 0.5) Color.BLACK.rgb else Color.WHITE.rgb
108108
}
109109

110-
fun GuiGraphics.drawRightAlignedString(text: Component, x: Int, y: Int, color: Int) {
111-
drawString(font, text, x - textWidth(text), y, color, false)
110+
fun GuiGraphics.drawRightAlignedString(text: Component, x: Int, y: Int, color: Int, shadow: Boolean = false) {
111+
drawString(font, text, x - textWidth(text), y, color, shadow)
112112
}
113113

114-
fun GuiGraphics.drawMiddleAlignedString(text: Component, x: Int, y: Int, color: Int) {
115-
drawString(font, text, x - textWidth(text) / 2 + 1, y, color, false)
114+
fun GuiGraphics.drawMiddleAlignedString(text: Component, x: Int, y: Int, color: Int, shadow: Boolean = false) {
115+
drawString(font, text, x - textWidth(text) / 2 + 1, y, color, shadow)
116116
}
117117

118-
fun GuiGraphics.drawHighlightedCenteredText(text: Component, x: Int, y: Int, color: Int, highlight: Boolean) {
118+
fun GuiGraphics.drawHighlightedCenteredText(text: Component, x: Int, y: Int, color: Int, highlight: Boolean, shadow: Boolean = false) {
119119
pose().push()
120120

121121
if (highlight) {
122122
val halfWidth: Int = textWidth(text) / 2
123123
fill(x - halfWidth - 1, y - 1, x + halfWidth + 2, y + 8, color)
124124
//? if <1.21.6
125125
pose().translate(0.0f, 0.0f, 100.0f)
126-
drawMiddleAlignedString(text, x, y, getContrasting(color))
126+
drawMiddleAlignedString(text, x, y, getContrasting(color), shadow)
127127
} else {
128-
drawMiddleAlignedString(text, x, y, color)
128+
drawMiddleAlignedString(text, x, y, color, shadow)
129129
}
130130

131131
pose().pop()

src/main/kotlin/ru/octol1ttle/flightassistant/screen/FlightAssistantSetupScreen.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@ class FlightAssistantSetupScreen : FABaseScreen(null, Component.translatable("me
3737

3838
this.addRenderableWidget(Button.builder(Component.translatable("menu.flightassistant.autoflight")) {
3939
this.minecraft!!.setScreen(AutoFlightScreen(this))
40-
}.pos(this.centerX - 75, this.centerY - 30).width(150).build())
40+
}.pos(this.centerX - 80, this.centerY - 30).width(160).build())
4141

4242
this.addRenderableWidget(StringWidget(0, this.centerY + 5, this.width, this.font.lineHeight, Component.translatable("menu.flightassistant.fms"), this.font))
4343
this.addRenderableWidget(Button.builder(Component.translatable("menu.flightassistant.fms.departure")) {
4444
this.minecraft!!.setScreen(DepartureScreen(this))
45-
}.pos(this.centerX - 160, this.centerY + 20).width(100).build())
45+
}.pos(this.centerX - 130, this.centerY + 20).width(80).build())
4646
this.addRenderableWidget(Button.builder(Component.translatable("menu.flightassistant.fms.enroute")) {
4747
this.minecraft!!.setScreen(EnrouteScreen(this))
48-
}.pos(this.centerX - 50, this.centerY + 20).width(100).build()).active = Platform.isDevelopmentEnvironment()
48+
}.pos(this.centerX - 40, this.centerY + 20).width(80).build()).active = Platform.isDevelopmentEnvironment()
4949
this.addRenderableWidget(Button.builder(Component.translatable("menu.flightassistant.fms.arrival")) {
5050
//this.minecraft!!.setScreen(FlightPlanScreen())
51-
}.pos(this.centerX + 60, this.centerY + 20).width(100).build()).active = Platform.isDevelopmentEnvironment()
51+
}.pos(this.centerX + 50, this.centerY + 20).width(80).build()).active = Platform.isDevelopmentEnvironment()
5252

5353
this.addRenderableWidget(Button.builder(Component.translatable("menu.flightassistant.config")) {
5454
this.minecraft!!.setScreen(FAConfigScreen.generate(this))
55-
}.pos(10, this.height - 30).width(100).build())
55+
}.pos(10, this.height - 30).width(120).build())
5656

57-
this.addRenderableWidget(Button.builder(CommonComponents.GUI_CANCEL) { _: Button? ->
57+
this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE) { _: Button? ->
5858
this.onClose()
5959
}.pos(this.width - 90, this.height - 30).width(80).build())
6060
}

src/main/kotlin/ru/octol1ttle/flightassistant/screen/fms/enroute/EnrouteScreen.kt

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ import net.minecraft.network.chat.CommonComponents
1010
import net.minecraft.network.chat.Component
1111
import net.minecraft.network.chat.Component.literal
1212
import net.minecraft.network.chat.Component.translatable
13+
import ru.octol1ttle.flightassistant.api.util.extensions.drawMiddleAlignedString
1314
import ru.octol1ttle.flightassistant.screen.FABaseScreen
1415
import ru.octol1ttle.flightassistant.screen.components.SmartStringWidget
1516

1617
class EnrouteScreen(parent: Screen) : FABaseScreen(parent, Component.translatable("menu.flightassistant.fms.enroute")) {
1718
private lateinit var discardChanges: Button
19+
private lateinit var save: Button
20+
private lateinit var done: Button
1821

1922
override fun init() {
2023
super.init()
@@ -27,28 +30,38 @@ class EnrouteScreen(parent: Screen) : FABaseScreen(parent, Component.translatabl
2730
this.addRenderableWidget(SmartStringWidget((this.width * (max(0.4f, i.toFloat()) / optimumColumnsSize)).toInt(), Y0, component).setColor(ChatFormatting.GRAY.color!!))
2831
}
2932

30-
this.addRenderableWidget(EnrouteWaypointsList(Y0 + 10, this.height - Y0 * 2, this.width, optimumColumnsSize))
33+
val list: EnrouteWaypointsList = this.addRenderableWidget(EnrouteWaypointsList(Y0 + 10, this.height - Y0 * 2, this.width, optimumColumnsSize, state) { !state.equals(lastState) })
34+
35+
this.addRenderableWidget(Button.builder(Component.translatable("menu.flightassistant.fms.enroute.add_waypoint")) {
36+
state.waypoints.add(EnrouteScreenState.Waypoint())
37+
list.rebuildEntries()
38+
}.bounds(this.centerX - 50, this.height - Y0 * 2 + 5, 100, 20).build())
3139

3240
discardChanges = this.addRenderableWidget(Button.builder(Component.translatable("menu.flightassistant.fms.discard_changes")) { _: Button? ->
3341
state = lastState.copy()
3442
this.rebuildWidgets()
35-
}.pos(this.width - 200, this.height - 30).width(100).build())
43+
}.pos(this.width - 290, this.height - 30).width(100).build())
44+
45+
save = this.addRenderableWidget(Button.builder(Component.translatable("menu.flightassistant.fms.save")) { _: Button? ->
46+
lastState = state.copy()
47+
}.pos(this.width - 180, this.height - 30).width(80).build())
3648

37-
this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE) { _: Button? ->
49+
done = this.addRenderableWidget(Button.builder(CommonComponents.GUI_DONE) { _: Button? ->
3850
this.onClose()
3951
}.pos(this.width - 90, this.height - 30).width(80).build())
4052
}
4153

42-
override fun onClose() {
43-
lastState = state.copy()
44-
state.save(computers.plan)
45-
super.onClose()
46-
}
47-
4854
override fun render(guiGraphics: GuiGraphics, mouseX: Int, mouseY: Int, delta: Float) {
49-
discardChanges.active = !state.equals(lastState)
55+
save.active = !state.equals(lastState)
56+
discardChanges.active = save.active
57+
done.active = !save.active
5058

5159
super.render(guiGraphics, mouseX, mouseY, delta)
60+
61+
if (save.active) {
62+
val text: Component = Component.translatable("menu.flightassistant.fms.enroute.unsaved_changes")
63+
guiGraphics.drawMiddleAlignedString(text, this.width / 4, 7, ChatFormatting.YELLOW.color!!, true)
64+
}
5265
}
5366

5467
companion object {
Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,60 @@
11
package ru.octol1ttle.flightassistant.screen.fms.enroute
22

3-
import kotlin.random.Random
4-
import kotlin.random.nextInt
53
import net.minecraft.ChatFormatting
64
import net.minecraft.client.gui.GuiGraphics
75
import net.minecraft.client.gui.components.Button
86
import net.minecraft.client.gui.components.ContainerObjectSelectionList
7+
import net.minecraft.client.gui.components.Tooltip
98
import net.minecraft.client.gui.components.events.GuiEventListener
109
import net.minecraft.client.gui.narration.NarratableEntry
1110
import net.minecraft.network.chat.Component
1211
import ru.octol1ttle.flightassistant.api.util.extensions.drawString
1312
import ru.octol1ttle.flightassistant.api.util.extensions.font
13+
import ru.octol1ttle.flightassistant.api.util.extensions.swap
1414
import ru.octol1ttle.flightassistant.api.util.extensions.toIntOrNullWithFallback
1515
import ru.octol1ttle.flightassistant.screen.components.FABaseList
1616
import ru.octol1ttle.flightassistant.screen.components.TypeStrictEditBox
1717

18-
class EnrouteWaypointsList(y0: Int, y1: Int, width: Int, columns: Float) : FABaseList<EnrouteWaypointsList.Entry>(y0, y1, width, ITEM_HEIGHT) {
18+
class EnrouteWaypointsList(y0: Int, y1: Int, width: Int, val columns: Float, val state: EnrouteScreenState, val hasUnsavedChanges: () -> Boolean) : FABaseList<EnrouteWaypointsList.Entry>(y0, y1, width, ITEM_HEIGHT) {
1919
init {
20-
repeat(Random.nextInt(1..69)) {
21-
addEntry(Entry(width, columns, EnrouteScreenState.Waypoint(Random.nextInt(-30_000_000..30_000_000), Random.nextInt(-30_000_000..30_000_000), Random.nextInt(62..620), Random.nextInt(0..420))))
22-
}
20+
rebuildEntries()
2321
}
2422

25-
class Entry(val width: Int, val columns: Float, val state: EnrouteScreenState.Waypoint) : ContainerObjectSelectionList.Entry<Entry>() {
23+
class Entry(val width: Int, val columns: Float, val state: EnrouteScreenState.Waypoint, val list: EnrouteWaypointsList) : ContainerObjectSelectionList.Entry<Entry>() {
2624
private val columnWidth: Float = width / this.columns
2725

2826
private val xEditBox = TypeStrictEditBox(0, 0, columnWidth.toInt(), font.lineHeight, state.coordinatesX, { state.coordinatesX = it }, String::toIntOrNullWithFallback)
2927
private val zEditBox = TypeStrictEditBox(0, 0, columnWidth.toInt(), font.lineHeight, state.coordinatesZ, { state.coordinatesZ = it }, String::toIntOrNullWithFallback)
3028
private val altitudeEditBox = TypeStrictEditBox(0, 0, columnWidth.toInt(), font.lineHeight, state.altitude, { state.altitude = it }, String::toIntOrNullWithFallback)
3129
private val speedEditBox = TypeStrictEditBox(0, 0, columnWidth.toInt(), font.lineHeight, state.speed, { state.speed = it }, String::toIntOrNullWithFallback) { it >= 0 }
3230

33-
private val moveUpButton = Button.builder(Component.literal("")) {}.size(12, 12).build()
34-
private val moveDownButton = Button.builder(Component.literal("")) {}.size(12, 12).build()
35-
private val deleteButton = Button.builder(Component.literal("X")) {}.size(12, 12).build()
31+
private val directToButton = Button.builder(Component.literal("")) {
32+
}.size(12, 12).build()
33+
private val moveUpButton = Button.builder(Component.literal("")) {
34+
list.state.waypoints.swap(index, index - 1)
35+
list.rebuildEntries()
36+
}.size(12, 12).build()
37+
private val moveDownButton = Button.builder(Component.literal("")) {
38+
list.state.waypoints.swap(index, index + 1)
39+
list.rebuildEntries()
40+
}.size(12, 12).build()
41+
private val deleteButton = Button.builder(Component.literal("X")) {
42+
list.state.waypoints.remove(this.state)
43+
list.rebuildEntries()
44+
}.size(12, 12).build()
3645

46+
private var index: Int = 0
3747
private var hovering: Boolean = false
3848
val children = listOf(xEditBox, zEditBox, altitudeEditBox, speedEditBox)
39-
val childrenWhenHovering = listOf(xEditBox, zEditBox, altitudeEditBox, speedEditBox, moveUpButton, moveDownButton, deleteButton)
49+
val childrenWhenHovering = listOf(xEditBox, zEditBox, altitudeEditBox, speedEditBox, directToButton, moveUpButton, moveDownButton, deleteButton)
4050

4151
override fun render(guiGraphics: GuiGraphics, index: Int, top: Int, left: Int, width: Int, height: Int, mouseX: Int, mouseY: Int, hovering: Boolean, partialTick: Float) {
52+
this.index = index
4253
this.hovering = hovering
54+
this.directToButton.active = !list.hasUnsavedChanges()
55+
this.directToButton.tooltip = Tooltip.create(if (this.directToButton.active) DIRECT_TO_TOOLTIP_TEXT else UNSAVED_CHANGES_TOOLTIP_TEXT)
56+
if (index == 0) this.moveUpButton.active = false
57+
if (index == list.children().size - 1) this.moveDownButton.active = false
4358

4459
guiGraphics.drawString((index + 1).toString(), (width * (0.4f / this.columns)).toInt(), top, ChatFormatting.WHITE.color!!)
4560

@@ -51,9 +66,11 @@ class EnrouteWaypointsList(y0: Int, y1: Int, width: Int, columns: Float) : FABas
5166
editBox.render(guiGraphics, mouseX, mouseY, partialTick)
5267
}
5368

54-
children().filterIsInstance<Button>().forEachIndexed { i, button ->
55-
button.x = (columnWidth * (if (columns <= 7) columns - 1 else 7.0f) + i * 15).toInt()
56-
button.y = top - button.height / 4
69+
var buttonX: Int = (columnWidth * (if (columns <= 7) columns - 1.25f else 6.75f)).toInt()
70+
children().filterIsInstance<Button>().forEach { button ->
71+
button.x = buttonX
72+
buttonX += button.width + 3
73+
button.y = top
5774
button.render(guiGraphics, mouseX, mouseY, partialTick)
5875
}
5976
}
@@ -67,7 +84,16 @@ class EnrouteWaypointsList(y0: Int, y1: Int, width: Int, columns: Float) : FABas
6784
}
6885
}
6986

87+
fun rebuildEntries() {
88+
this.clearEntries()
89+
for (waypoint: EnrouteScreenState.Waypoint in state.waypoints) {
90+
addEntry(Entry(this.width, this.columns, waypoint, this))
91+
}
92+
}
93+
7094
companion object {
71-
private const val ITEM_HEIGHT: Int = 10
95+
private const val ITEM_HEIGHT: Int = 12
96+
private val DIRECT_TO_TOOLTIP_TEXT: Component = Component.translatable("menu.flightassistant.fms.enroute.direct_to")
97+
private val UNSAVED_CHANGES_TOOLTIP_TEXT: Component = Component.translatable("menu.flightassistant.fms.enroute.direct_to.unsaved_changes")
7298
}
7399
}

src/main/resources/assets/flightassistant/lang/en_us.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,8 +286,15 @@ menu.flightassistant:
286286
.: Departure
287287
takeoff_thrust: 'Takeoff thrust %: '
288288
minimum_climb_speed: 'Minimum climb speed: '
289-
enroute: Enroute
289+
enroute:
290+
.: Enroute
291+
add_waypoint: Add waypoint
292+
direct_to:
293+
.: Direct-to
294+
unsaved_changes: Cannot use Direct-to because there are unsaved changes!
295+
unsaved_changes: UNSAVED CHANGES
290296
arrival: Arrival
297+
save: Save
291298
discard_changes: Discard changes
292299
coordinates: 'Coordinates: '
293300
elevation: 'Elevation: '

src/main/resources/assets/flightassistant/lang/ru_ru.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,15 @@ menu.flightassistant:
290290
.: Вылет
291291
takeoff_thrust: 'Взлётная тяга (%): '
292292
minimum_climb_speed: 'Минимальная скорость набора: '
293-
enroute: Путь
293+
enroute:
294+
.: Маршрут
295+
add_waypoint: Добавить точку
296+
direct_to:
297+
.: Прямо-на
298+
unsaved_changes: Невозможно использовать Прямо-на, так как есть несохранённые изменения!
299+
unsaved_changes: НЕСОХРАНЕН ИЗМЕНЕНИЯ
294300
arrival: Прибытие
301+
save: Сохранить
295302
discard_changes: Отменить изменения
296303
coordinates: 'Координаты: '
297304
elevation: 'Высота: '

0 commit comments

Comments
 (0)