Skip to content

Commit

Permalink
Fix new datatag styles not being shown in select boxes
Browse files Browse the repository at this point in the history
  • Loading branch information
Bombbird2001 committed Aug 12, 2023
1 parent 0cdfc73 commit 3264be9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ configure(subprojects - project(':android')) {
subprojects {
version = '2.0.0-beta'
ext.appName = 'Terminal Control 2'
ext.buildVersion = 7
ext.buildVersion = 8
repositories {
mavenCentral()
maven { url 'https://s01.oss.sonatype.org' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.bombbird.terminalcontrol2.files.deleteDatatagLayout
import com.bombbird.terminalcontrol2.files.saveDatatagLayout
import com.bombbird.terminalcontrol2.global.*
import com.bombbird.terminalcontrol2.screens.BasicUIScreen
import com.bombbird.terminalcontrol2.ui.CustomDialog
import com.bombbird.terminalcontrol2.ui.addChangeListener
import com.bombbird.terminalcontrol2.ui.datatag.DatatagConfig
import com.bombbird.terminalcontrol2.ui.defaultSettingsSelectBox
Expand All @@ -31,6 +32,7 @@ class DatatagLayoutSettings: BasicUIScreen() {
private val showWhenChangedCheckbox: CheckBox
private val previewLabel: Label
private val pageButton: KTextButton
private val configSelectBox: KSelectBox<String>
private var showingMainPage = true
private var currPreviewLayout: DatatagConfig = DatatagConfig("Empty")
private var miniArrangementFirstEmpty = false
Expand All @@ -40,6 +42,7 @@ class DatatagLayoutSettings: BasicUIScreen() {
companion object {
private const val NONE = "(None)"
private const val NEW_LAYOUT = "+ New layout"
private const val NEW_LAYOUT_PLACEHOLDER = "New layout"

private val FIELD_POSITIONS = GdxArrayMap<String, Int>(17).apply {
put(DatatagConfig.CALLSIGN, 0)
Expand Down Expand Up @@ -89,7 +92,7 @@ class DatatagLayoutSettings: BasicUIScreen() {
setSize(UI_WIDTH, UI_HEIGHT)
table {
table {
defaultSettingsSelectBox<String>().apply {
configSelectBox = defaultSettingsSelectBox<String>().apply {
val availableLayouts = GdxArray<String>()
availableLayouts.add(NONE)
DATATAG_LAYOUTS.keys.filter { it != DatatagConfig.DEFAULT && it != DatatagConfig.COMPACT }.forEach { availableLayouts.add(it) }
Expand Down Expand Up @@ -232,6 +235,11 @@ class DatatagLayoutSettings: BasicUIScreen() {
}
textButton("Save", "Menu").cell(width = BUTTON_WIDTH_BIG / 1.5f, height = BUTTON_HEIGHT_BIG, padBottom = BOTTOM_BUTTON_MARGIN, align = Align.bottom).addChangeListener { _, _ ->
val newName = layoutNameField.text
if (!validateLayoutName(newName)) {
CustomDialog("Invalid name", "Names can only contain letters, digits, spaces," +
" underscores and hyphens with maximum length of 20 characters", "", "Ok").show(stage)
return@addChangeListener
}
val newLayout = getLayoutObjectFromSelectBoxes(newName)
if (saveDatatagLayout(newLayout)) {
if (currName != newName) {
Expand All @@ -240,9 +248,11 @@ class DatatagLayoutSettings: BasicUIScreen() {
DATATAG_LAYOUTS.remove(it)
if (DATATAG_STYLE_NAME == it) DATATAG_STYLE_NAME = DatatagConfig.DEFAULT
}
} else {
DATATAG_LAYOUTS[newName] = newLayout
}
DATATAG_LAYOUTS[newName] = newLayout
updateDatatagConfigChoices()
GAME.getScreen<DatatagSettings>().updateDatatagConfigChoices()
configSelectBox.selected = newName
}
GAME.setScreen<DatatagSettings>()
}
Expand All @@ -265,6 +275,7 @@ class DatatagLayoutSettings: BasicUIScreen() {
*/
private fun setSelectBoxesToLayout(layoutName: String) {
currName = layoutName
if (currName == NEW_LAYOUT) currName = NEW_LAYOUT_PLACEHOLDER
if (layoutName == NONE) {
for (i in 0 until arrangementSelectBoxes.size) arrangementSelectBoxes[i].isVisible = false
for (i in 0 until miniArrangementFirstSelectBoxes.size) miniArrangementFirstSelectBoxes[i].isVisible = false
Expand All @@ -283,7 +294,7 @@ class DatatagLayoutSettings: BasicUIScreen() {

modificationInProgress = true
val layout = DATATAG_LAYOUTS[layoutName]
layoutNameField.text = if (layoutName == NEW_LAYOUT) "New layout" else layoutName
layoutNameField.text = if (layoutName == NEW_LAYOUT) NEW_LAYOUT_PLACEHOLDER else layoutName
updateSelectBoxOptionsForArrangement(arrangementSelectBoxes)
updateSelectBoxOptionsForArrangement(miniArrangementFirstSelectBoxes)
updateSelectBoxOptionsForArrangement(miniArrangementSecondSelectBoxes)
Expand Down Expand Up @@ -425,6 +436,29 @@ class DatatagLayoutSettings: BasicUIScreen() {
previewLabel.setText(currPreviewLayout.generateTagText(PREVIEW_DATA, !showingMainPage))
}

/** Update the choices available in the datatag config select box */
private fun updateDatatagConfigChoices() {
val availableLayouts = GdxArray<String>()
availableLayouts.add(NONE)
DATATAG_LAYOUTS.keys.filter { it != DatatagConfig.DEFAULT && it != DatatagConfig.COMPACT }.forEach { availableLayouts.add(it) }
availableLayouts.add(NEW_LAYOUT)
configSelectBox.items = availableLayouts
}

/**
* Validates the layout name to contain only letters, digits, spaces, underscores and hyphens, max length 20
* characters
* @param name the name to validate
*/
private fun validateLayoutName(name: String): Boolean {
if (name == NEW_LAYOUT) return false
if (name.length > 20) return false
for (c in name) {
if (!c.isLetterOrDigit() && c != ' ' && c != '_' && c != '-') return false
}
return true
}

override fun render(delta: Float) {
super.render(delta)
// We'll call update preview here if minimised page is active, and both first and second mini arrangement are
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ class DatatagSettings: BaseSettings() {
rowSpacingLabel.setText("${newSpacingPx.roundToInt()}px")
}

/** Updates all the datatag choices available */
fun updateDatatagConfigChoices() {
styleSelectBox.setItems(*DATATAG_LAYOUTS.keys.toTypedArray(), MANAGE_LAYOUTS)
}

/**
* Overrides the base [BaseSettings.setToCurrentClientSettings] function; will take the relevant datatag settings
* and set the select box choices based on them
Expand Down

0 comments on commit 3264be9

Please sign in to comment.