Skip to content

Commit

Permalink
Move user preference handling to CLI project for CLI flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Aug 3, 2024
1 parent 354678f commit 235063f
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,6 @@ class Map extends Node<Map> {
}
return newSpriteSheet
}
// TODO: This can be some batching request method
.thenApplyAsync { spriteSheet ->
List<Vector2f> allVertices = []
List<Vector2f> allTextureUVs = []
Expand Down
1 change: 1 addition & 0 deletions redhorizon-cli/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ dependencies {
implementation platform("org.lwjgl:lwjgl-bom:${lwjglVersion}")
implementation "ch.qos.logback:logback-core:${logbackVersion}"
implementation "info.picocli:picocli:${picocliVersion}"
implementation 'nz.net.ultraq.preferences:preferences:3.1.0'
implementation "org.apache.groovy:groovy-json:${groovyVersion}"
implementation "org.joml:joml:${jomlVersion}"
implementation "org.joml:joml-primitives:${jomlPrimitivesVersion}"
Expand Down
51 changes: 49 additions & 2 deletions redhorizon-cli/source/nz/net/ultraq/redhorizon/cli/Explorer.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,15 @@

package nz.net.ultraq.redhorizon.cli

import nz.net.ultraq.preferences.Preferences
import nz.net.ultraq.redhorizon.explorer.ExplorerOptions

import picocli.CommandLine.Command
import picocli.CommandLine.IDefaultValueProvider
import picocli.CommandLine.Model.ArgSpec
import picocli.CommandLine.Model.CommandSpec
import picocli.CommandLine.Model.OptionSpec
import picocli.CommandLine.Option
import picocli.CommandLine.Parameters
import picocli.CommandLine.Spec

Expand All @@ -28,23 +35,63 @@ import java.util.concurrent.Callable
*
* @author Emanuel Rabina
*/
@Command(name = 'explorer')
@Command(name = 'explorer', defaultValueProvider = DefaultOptionsProvider)
class Explorer implements Callable<Integer> {

static {
System.setProperty('logback.configurationFile', 'logback-application.xml')
}

private static final Preferences userPreferences = new Preferences()

@Spec
CommandSpec commandSpec

@Parameters(index = '0', defaultValue = '', description = 'Path to a file to open on launch')
File file

@Option(names = '--maximized', description = 'Start the application maximized. Remembers your last usage.')
boolean maximized

@Option(names = '--touchpad-input', description = 'Start the application using touchpad controls. Remembers your last usage.')
boolean touchpadInput

@Override
Integer call() {

new nz.net.ultraq.redhorizon.explorer.Explorer(commandSpec.parent().version()[0], file)
// Load options from command line or preferences
var explorerOptions = new ExplorerOptions(
maximized: maximized,
touchpadInput: touchpadInput
)

new nz.net.ultraq.redhorizon.explorer.Explorer(commandSpec.parent().version()[0], explorerOptions, file)

// Save preferences for next time
userPreferences.set(ExplorerPreferences.WINDOW_MAXIMIZED, explorerOptions.maximized)
userPreferences.set(ExplorerPreferences.TOUCHPAD_INPUT, explorerOptions.touchpadInput)

return 0
}

/**
* Provide default options for the user-remembered options.
*/
static class DefaultOptionsProvider implements IDefaultValueProvider {

@Override
String defaultValue(ArgSpec argSpec) {

if (argSpec.option) {
var option = (OptionSpec)argSpec
if (option.longestName() == '--maximized') {
return userPreferences.get(ExplorerPreferences.WINDOW_MAXIMIZED)
}
if (option.longestName() == '--touchpad-input') {
return userPreferences.get(ExplorerPreferences.TOUCHPAD_INPUT).toString()
}
}
return null
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package nz.net.ultraq.redhorizon.explorer
package nz.net.ultraq.redhorizon.cli

import nz.net.ultraq.preferences.UserPreference

Expand Down
1 change: 0 additions & 1 deletion redhorizon-explorer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ dependencies {
implementation platform("org.lwjgl:lwjgl-bom:${lwjglVersion}")
implementation "ch.qos.logback:logback-classic:${logbackVersion}"
implementation files("../libraries/imgui-java-binding-with-tablesortspecs-${imguiVersion}.jar")
implementation 'nz.net.ultraq.preferences:preferences:3.1.0'
implementation "org.apache.groovy:groovy-json:${groovyVersion}"
implementation "org.joml:joml:${jomlVersion}"
implementation "org.joml:joml-primitives:${jomlPrimitivesVersion}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package nz.net.ultraq.redhorizon.explorer

import nz.net.ultraq.preferences.Preferences
import nz.net.ultraq.redhorizon.classic.filetypes.IniFile
import nz.net.ultraq.redhorizon.classic.filetypes.MapFile
import nz.net.ultraq.redhorizon.classic.filetypes.MixFile
Expand Down Expand Up @@ -79,10 +78,10 @@ import java.util.concurrent.CopyOnWriteArrayList
class Explorer {

private static final Logger logger = LoggerFactory.getLogger(Explorer)
private static final Preferences userPreferences = new Preferences()
private static final Dimension renderResolution = new Dimension(1280, 800)
private static final float volume = 0.5f

private final ExplorerOptions options
private final List<Entry> entries = new CopyOnWriteArrayList<>()
private final EntryList entryList = new EntryList(entries)
private final MixDatabase mixDatabase = new MixDatabase()
Expand All @@ -100,7 +99,6 @@ class Explorer {
private File currentDirectory
private InputStream selectedFileInputStream
private PaletteType currentPalette
private boolean touchpadInput
private List<RemoveEventFunction> removeEventFunctions = []

/**
Expand All @@ -110,15 +108,15 @@ class Explorer {
* @param openOnLaunch
* Optional, a file to open on launch of the explorer.
*/
Explorer(String version, File openOnLaunch) {
Explorer(String version, ExplorerOptions options, File openOnLaunch) {

touchpadInput = userPreferences.get(ExplorerPreferences.TOUCHPAD_INPUT)
this.options = options

new Application('Explorer', version)
.addAudioSystem()
.addGraphicsSystem(new GraphicsConfiguration(
clearColour: Colour.GREY,
maximized: userPreferences.get(ExplorerPreferences.WINDOW_MAXIMIZED),
maximized: options.maximized,
renderResolution: renderResolution,
startWithChrome: true
), new LogPanel(true), entryList, nodeList)
Expand All @@ -137,7 +135,7 @@ class Explorer {
this.scene = scene

application.on(WindowMaximizedEvent) { event ->
userPreferences.set(ExplorerPreferences.WINDOW_MAXIMIZED, event.maximized)
options.maximized = event.maximized
}

buildList(new File(System.getProperty("user.dir")))
Expand Down Expand Up @@ -175,12 +173,11 @@ class Explorer {
scene.gameMenu.optionsMenu << new MenuItem() {
@Override
void render() {
if (ImGui.menuItem('Touchpad input', null, touchpadInput)) {
touchpadInput = !touchpadInput
userPreferences.set(ExplorerPreferences.TOUCHPAD_INPUT, touchpadInput)
if (ImGui.menuItem('Touchpad input', null, options.touchpadInput)) {
options.touchpadInput = !options.touchpadInput
var mapNode = (Map)scene.findDescendent { node -> node instanceof Map }
if (mapNode) {
((MapViewerScript)mapNode.script).touchpadInput = touchpadInput
((MapViewerScript)mapNode.script).touchpadInput = options.touchpadInput
}
}
}
Expand Down Expand Up @@ -509,7 +506,7 @@ class Explorer {
*/
private void preview(MapFile mapFile, String objectId) {

var mapViewerScript = new MapViewerScript(camera, nodeList, touchpadInput)
var mapViewerScript = new MapViewerScript(camera, nodeList, options.touchpadInput)
time("Loading map ${objectId}", logger) { ->
resourceManager.withDirectory(currentDirectory) { ->
var map = new Map(mapFile, resourceManager).attachScript(mapViewerScript)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* Copyright 2024, Emanuel Rabina (http://www.ultraq.net.nz/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package nz.net.ultraq.redhorizon.explorer

/**
* Options to pass to, and retrieve from, the Explorer application.
*
* @author Emanuel Rabina
*/
class ExplorerOptions {

boolean maximized
boolean touchpadInput
}

0 comments on commit 235063f

Please sign in to comment.