1
1
package ru.octol1ttle.flightassistant.screen.fms.enroute
2
2
3
- import kotlin.random.Random
4
- import kotlin.random.nextInt
5
3
import net.minecraft.ChatFormatting
6
4
import net.minecraft.client.gui.GuiGraphics
7
5
import net.minecraft.client.gui.components.Button
8
6
import net.minecraft.client.gui.components.ContainerObjectSelectionList
7
+ import net.minecraft.client.gui.components.Tooltip
9
8
import net.minecraft.client.gui.components.events.GuiEventListener
10
9
import net.minecraft.client.gui.narration.NarratableEntry
11
10
import net.minecraft.network.chat.Component
12
11
import ru.octol1ttle.flightassistant.api.util.extensions.drawString
13
12
import ru.octol1ttle.flightassistant.api.util.extensions.font
13
+ import ru.octol1ttle.flightassistant.api.util.extensions.swap
14
14
import ru.octol1ttle.flightassistant.api.util.extensions.toIntOrNullWithFallback
15
15
import ru.octol1ttle.flightassistant.screen.components.FABaseList
16
16
import ru.octol1ttle.flightassistant.screen.components.TypeStrictEditBox
17
17
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 ) {
19
19
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()
23
21
}
24
22
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>() {
26
24
private val columnWidth: Float = width / this .columns
27
25
28
26
private val xEditBox = TypeStrictEditBox (0 , 0 , columnWidth.toInt(), font.lineHeight, state.coordinatesX, { state.coordinatesX = it }, String ::toIntOrNullWithFallback)
29
27
private val zEditBox = TypeStrictEditBox (0 , 0 , columnWidth.toInt(), font.lineHeight, state.coordinatesZ, { state.coordinatesZ = it }, String ::toIntOrNullWithFallback)
30
28
private val altitudeEditBox = TypeStrictEditBox (0 , 0 , columnWidth.toInt(), font.lineHeight, state.altitude, { state.altitude = it }, String ::toIntOrNullWithFallback)
31
29
private val speedEditBox = TypeStrictEditBox (0 , 0 , columnWidth.toInt(), font.lineHeight, state.speed, { state.speed = it }, String ::toIntOrNullWithFallback) { it >= 0 }
32
30
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()
36
45
46
+ private var index: Int = 0
37
47
private var hovering: Boolean = false
38
48
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)
40
50
41
51
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
42
53
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
43
58
44
59
guiGraphics.drawString((index + 1 ).toString(), (width * (0.4f / this .columns)).toInt(), top, ChatFormatting .WHITE .color!! )
45
60
@@ -51,9 +66,11 @@ class EnrouteWaypointsList(y0: Int, y1: Int, width: Int, columns: Float) : FABas
51
66
editBox.render(guiGraphics, mouseX, mouseY, partialTick)
52
67
}
53
68
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
57
74
button.render(guiGraphics, mouseX, mouseY, partialTick)
58
75
}
59
76
}
@@ -67,7 +84,16 @@ class EnrouteWaypointsList(y0: Int, y1: Int, width: Int, columns: Float) : FABas
67
84
}
68
85
}
69
86
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
+
70
94
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" )
72
98
}
73
99
}
0 commit comments