Skip to content

Commit

Permalink
add search to room sounds table; sort room sounds by name;
Browse files Browse the repository at this point in the history
  • Loading branch information
4rg0n committed Sep 18, 2024
1 parent 07c2d16 commit 26c0b43
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,15 @@
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import snake2d.SPRITE_RENDERER;
import snake2d.util.color.COLOR;
import snake2d.util.datatypes.COORDINATE;
import snake2d.util.gui.GuiSection;
import snake2d.util.gui.renderable.RENDEROBJ;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand All @@ -30,7 +28,8 @@
public class ColumnRow<Value> extends Section implements
Searchable<String, Boolean>,
Valuable<Value>,
Toggleable<String>
Toggleable<String>,
Comparable<ColumnRow<Value>>
{
/**
* Used to identify the row
Expand Down Expand Up @@ -133,6 +132,14 @@ public class ColumnRow<Value> extends Section implements
@Accessors(fluent = true, chain = false)
private Consumer<Value> valueConsumer;

/**
* For sorting rows in a list via their values by a defined behavior
*/
@Setter
@Builder.Default
@Accessors(fluent = true, chain = false)
private Comparator<Value> comparator = (value1, value2) -> 0;

/**
* What shall happen when you doubleclick the row.
*/
Expand Down Expand Up @@ -326,6 +333,11 @@ public ColumnRowBuilder<Value> column(RENDEROBJ column) {
}
}

@Override
public int compareTo(@NotNull ColumnRow<Value> row) {
return comparator.compare(this.value, row.getValue());
}

@Override
public String toString() {
return "ColumnRow{" +
Expand Down
2 changes: 1 addition & 1 deletion mod/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<version>local</version>
</parent>

<version>3.0.3</version>
<version>3.1.0</version>
<artifactId>sos-mod-more-options</artifactId>

<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,31 +106,35 @@ public static Map<String, List<BoostersTab.Entry>> toBoosterPanelEntriesCategori
.collect(groupingBy(entry -> entry.getCat().name.toString()));
}

public <Value, Element extends RENDEROBJ> List<ColumnRow<Value>> toRoomSoundLabeledColumnRows(Map<String, Element> elements) {
public <Element extends RENDEROBJ> List<ColumnRow<String>> toRoomSoundLabeledColumnRows(Map<String, Element> elements) {
return elements.entrySet().stream()
.sorted(Map.Entry.comparingByKey())
.map(entry -> {
String key = entry.getKey();
Element element = entry.getValue();

String name = key;
SPRITE sprite = SPRITES.icons().m.clear_structure;
Optional<RoomBlueprintImp> bySound = gameApis.rooms().getBySound(key);
Optional<RoomBlueprintImp> room = gameApis.rooms().getBySound(key);

if (bySound.isPresent()) {
name = bySound.get().info.name.toString();
sprite = bySound.get().iconBig().medium;
if (room.isPresent()) {
name = room.get().info.name.toString();
sprite = room.get().iconBig().medium;
}

// label with element
return ColumnRow.<Value>builder()
return ColumnRow.<String>builder()
.key(key)
.value(name)
.column(Label.builder()
.name(name)
.build())
.column(UiUtil.toRender(sprite))
.column(element)
.searchTerm(name)
.comparator(String::compareTo)
.build();
})
.sorted()
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class SoundsTab extends AbstractConfigTab<SoundsConfig, SoundsTab> {
private static final Logger log = Loggers.getLogger(SoundsTab.class);
private final static I18nTranslator i18n = ModModule.i18n().get(SoundsTab.class);

private final Map<String, Slider> ambienceSoundSliders;
private final Map<String, Slider> soundSliders;
public SoundsTab(
String title,
SoundsConfig soundsConfig,
Expand All @@ -34,19 +34,19 @@ public SoundsTab(
int availableHeight
) {
super(title, defaultConfig, availableWidth, availableHeight);
this.ambienceSoundSliders = UiMapper.toSliders(soundsConfig.getAmbience());
this.soundSliders = UiMapper.toSliders(soundsConfig.getAmbience());

GHeader ambienceSoundsHeader = new GHeader(i18n.t("SoundsTab.header.ambienceSounds.name"));
ambienceSoundsHeader.hoverInfoSet(i18n.d("SoundsTab.header.ambienceSounds.desc"));

GHeader roomSoundsHeader = new GHeader(i18n.t("SoundsTab.header.roomSounds.name"));
roomSoundsHeader.hoverInfoSet(i18n.d("SoundsTab.header.roomSounds.desc"));

Map<String, Slider> ambience = ambienceSoundSliders.entrySet().stream()
Map<String, Slider> ambience = soundSliders.entrySet().stream()
.filter(stringSliderEntry -> !stringSliderEntry.getKey().contains("ROOM_"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Map<String, Slider> room = ambienceSoundSliders.entrySet().stream()
Map<String, Slider> room = soundSliders.entrySet().stream()
.filter(stringSliderEntry -> stringSliderEntry.getKey().contains("ROOM_"))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Expand All @@ -69,12 +69,13 @@ public SoundsTab(

Layout.vertical(tableHeight)
.addDownC(20, roomSoundsHeader)
.addDownC(5, new VerticalLayout.Scalable(150, height -> Table.<Integer>builder()
.addDownC(5, new VerticalLayout.Scalable(150, height -> Table.<String>builder()
.rows(ModModule.uiMapper().toRoomSoundLabeledColumnRows(room))
.rowPadding(5)
.columnMargin(5)
.highlight(true)
.scrollable(true)
.displaySearch(true)
.backgroundColor(COLOR.WHITE10)
.displayHeight(height)
.build()))
Expand All @@ -89,7 +90,7 @@ public SoundsConfig getValue() {
}

public Map<String, Range> getSoundsAmbienceConfig() {
return ambienceSoundSliders.entrySet().stream()
return soundSliders.entrySet().stream()
.collect(Collectors.toMap(
Map.Entry::getKey,
tab -> Range.fromSlider(tab.getValue())));
Expand All @@ -100,8 +101,8 @@ public void setValue(SoundsConfig soundsConfig) {
log.trace("Applying UI sounds config %s", soundsConfig);

soundsConfig.getAmbience().forEach((key, range) -> {
if (ambienceSoundSliders.containsKey(key)) {
ambienceSoundSliders.get(key).setValue(range.getValue());
if (soundSliders.containsKey(key)) {
soundSliders.get(key).setValue(range.getValue());
} else {
log.warn("No slider with key %s found in UI", key);
}
Expand Down

0 comments on commit 26c0b43

Please sign in to comment.