Skip to content

Commit

Permalink
Start recreating map using nodes/scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
ultraq committed Feb 28, 2024
1 parent b4afd20 commit b2bce64
Show file tree
Hide file tree
Showing 13 changed files with 485 additions and 159 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -419,19 +419,19 @@ class MapRA extends Node<MapRA> implements GraphicsElement {
}
var unitImages = resourceManager.loadFile("${unitLine.type}.shp", ShpFile)

var unit = new Vehicle(unitConfig, unitImages, palette)

// TODO: Country to faction map
unit.faction = switch (unitLine.faction) {
case "Greece" -> Faction.BLUE
case "USSR" -> Faction.RED
default -> Faction.GOLD
}

unit.heading = unitLine.heading
unit.translate(unitLine.coords.asWorldCoords())

elements << unit
// var unit = new Vehicle(unitConfig, unitImages, palette)
//
// // TODO: Country to faction map
// unit.faction = switch (unitLine.faction) {
// case "Greece" -> Faction.BLUE
// case "USSR" -> Faction.RED
// default -> Faction.GOLD
// }
//
// unit.heading = unitLine.heading
// unit.translate(unitLine.coords.asWorldCoords())
//
// elements << unit
}
catch (IllegalArgumentException ignored) {
// Ignore unknown units
Expand Down Expand Up @@ -462,21 +462,21 @@ class MapRA extends Node<MapRA> implements GraphicsElement {
var unitConfig = jsonSlurper.parseText(unitConfigJson) as UnitData
var infantryImages = resourceManager.loadFile("${infantryLine.type}.shp", ShpFile)

var infantry = new Infantry(unitConfig, infantryImages, palette).tap { it ->

// TODO: Country to faction map
it.faction = switch (infantryLine.faction) {
case "Greece" -> Faction.BLUE
case "USSR" -> Faction.RED
default -> Faction.GOLD
}
it.heading = infantryLine.heading

// TODO: Sub positions within cells
it.translate(infantryLine.coords.asWorldCoords())
}

elements << infantry
// var infantry = new Infantry(unitConfig, infantryImages, palette).tap { it ->
//
// // TODO: Country to faction map
// it.faction = switch (infantryLine.faction) {
// case "Greece" -> Faction.BLUE
// case "USSR" -> Faction.RED
// default -> Faction.GOLD
// }
// it.heading = infantryLine.heading
//
// // TODO: Sub positions within cells
// it.translate(infantryLine.coords.asWorldCoords())
// }
//
// elements << infantry
}
catch (IllegalArgumentException ignored) {
// Ignore unknown units
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class SandboxCli implements Callable<Integer> {
Thread.currentThread().name = 'Sandbox [main]'
logger.info('Red Horizon Sandbox {}', commandSpec.parent().version()[0])

new Sandbox(touchpadInput).start()
new Sandbox(touchpadInput)

return 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ import imgui.ImGui
import imgui.type.ImBoolean
import static imgui.flag.ImGuiWindowFlags.*

import java.util.concurrent.CopyOnWriteArrayList

/**
* Small overlay for displaying registered controls in the application.
*
* @author Emanuel Rabina
*/
class ControlsOverlayRenderPass implements OverlayRenderPass {

private List<String> controls = []
private List<String> controls = new CopyOnWriteArrayList<>()
private int controlsWindowSizeX = 350
private int controlsWindowSizeY = 200

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
/*
* Copyright 2022, 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.
Expand All @@ -19,20 +19,37 @@ package nz.net.ultraq.redhorizon.engine.input
import nz.net.ultraq.redhorizon.events.Event
import nz.net.ultraq.redhorizon.events.EventListener

import org.lwjgl.glfw.GLFW

import groovy.transform.TupleConstructor
import java.lang.reflect.Field
import java.lang.reflect.Modifier

/**
* An input binding with a description so that what it does can be displayed.
*
*
* @author Emanuel Rabina
*/
@TupleConstructor
@TupleConstructor(defaults = false)
abstract class Control<E extends Event> implements EventListener<E> {

protected static final Field[] glfwFields = GLFW.getDeclaredFields()

final Class<E> event
final String name
final String binding

/**
* Return a string representing the name of a modifier key.
*/
protected static String determineModifierName(int modifier) {

var modifierField = glfwFields.find { field ->
return Modifier.isStatic(field.modifiers) && field.name.startsWith("GLFW_MOD_") && field.getInt(null) == modifier
}
return modifierField ? "${modifierField.name.substring(9).toLowerCase().capitalize()} + " : ""
}

@Override
String toString() {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
/*
* Copyright 2022, 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.
Expand All @@ -16,8 +16,6 @@

package nz.net.ultraq.redhorizon.engine.input


import org.lwjgl.glfw.GLFW
import static org.lwjgl.glfw.GLFW.GLFW_PRESS
import static org.lwjgl.glfw.GLFW.GLFW_REPEAT

Expand All @@ -30,38 +28,35 @@ import java.lang.reflect.Modifier
*/
class KeyControl extends Control<KeyEvent> {

private final int modifier
private final int key
private Closure handler

/**
* Constructor, build a key-handling control around the given key.
*
* @param key
* @param name
* @param handler
*/
KeyControl(int key, String name, Closure handler) {

super(KeyEvent, name, determineKeyBindingName(key))
this(-1, key, name, handler)
}

KeyControl(int modifier, int key, String name, Closure handler) {

super(KeyEvent, name, determineBindingName(modifier, key))
this.modifier = modifier
this.key = key
this.handler = handler
}

/**
* Return a string representing the name of the key binding.
*
* @param key
* @return
*/
private static String determineKeyBindingName(int key) {
private static String determineBindingName(int modifier, int key) {

var field = GLFW.getDeclaredFields().find { field ->
var modifierName = determineModifierName(modifier)
var buttonField = glfwFields.find { field ->
return Modifier.isStatic(field.modifiers) && field.name.startsWith("GLFW_KEY_") && field.getInt(null) == key
}
if (field) {
return field.name.substring(9).toLowerCase().capitalize()
}
return key.toString()
return buttonField ?
modifierName + buttonField.name.substring(9).toLowerCase().capitalize() :
key.toString()
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* 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.engine.input

import java.lang.reflect.Modifier

/**
* Mouse-specific control class.
*
* @author Emanuel Rabina
*/
class MouseControl extends Control<MouseButtonEvent> {

private final int modifier
private final int button
private Closure handler

MouseControl(int button, String name, Closure handler) {

this(-1, button, name, handler)
}

MouseControl(int modifier, int button, String name, Closure handler) {

super(MouseButtonEvent, name, determineBindingName(modifier, button))
this.modifier = modifier
this.button = button
this.handler = handler
}

/**
* Return a string representing the name of the key binding.
*/
private static String determineBindingName(int modifier, int button) {

var modifierName = determineModifierName(modifier)
var buttonField = glfwFields.find { field ->
return Modifier.isStatic(field.modifiers) && field.name.startsWith("GLFW_MOUSE_BUTTON_") && field.getInt(null) == button
}
return buttonField ?
"${modifierName}Mouse ${buttonField.name.substring(18).toLowerCase().capitalize()}" :
button.toString()
}

@Override
void handleEvent(MouseButtonEvent event) {

if (event.button == button && event.mods == modifier) {
handler()
}
}
}

This file was deleted.

Loading

0 comments on commit b2bce64

Please sign in to comment.