Skip to content

Commit

Permalink
Merge pull request #16 from rh-robotics/teo-old-compass
Browse files Browse the repository at this point in the history
Adds `.setCompassImage(image: Image)` and `.setCompassImage(compassImage: CompassImage)` Functions
  • Loading branch information
DragonDev07 authored Nov 4, 2024
2 parents 34e5195 + 1186a6f commit c9bb1bf
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
83 changes: 83 additions & 0 deletions src/main/kotlin/org/rowlandhall/meepmeep/MeepMeep.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import java.awt.event.KeyListener
import java.awt.event.MouseEvent
import java.awt.event.MouseListener
import java.awt.event.MouseMotionListener
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
import javax.swing.UIManager

Expand Down Expand Up @@ -467,6 +468,52 @@ class MeepMeep @JvmOverloads constructor(
return this
}

/**
* Sets the compass image for the MeepMeep application.
*
* @param compassImage The [CompassImage] enum representing the wanted compass image.
* @return The [MeepMeep] instance for method chaining.
*/
fun setCompassImage(compassImage: CompassImage): MeepMeep {
val classLoader = Thread.currentThread().contextClassLoader

val compassImageMap = CompassImage.values().associateWith { img ->
val path = when (img) {
CompassImage.SIMPLE -> if (colorManager.isDarkMode) "misc/simple-compass-white.png" else "misc/simple-compass-black.png"
CompassImage.SIMPLE_BLACK -> "misc/simple-compass-black.png"
CompassImage.SIMPLE_WHITE -> "misc/simple-compass-white.png"
CompassImage.COMPASS_ROSE -> if (colorManager.isDarkMode) "misc/compass-rose-white-text.png" else "misc/compass-rose-black-text.png"
CompassImage.COMPASS_ROSE_WHITE -> "misc/compass-rose-white-text.png"
CompassImage.COMPASS_ROSE_BLACK -> "misc/compass-rose-black-text.png"
}

path
}

// Get the path and dark mode boolean from the compass image map
val path = compassImageMap[compassImage]!!
val newImage = ImageIO.read(classLoader.getResourceAsStream(path))
DEFAULT_COMPASS_ENTITY.setCompassImage(newImage, colorManager.isDarkMode)

return this
}

/**
* Sets the compass image for the MeepMeep application.
*
* This method scales the provided [Image] and sets it as the compass image, allowing for custom compass images.
*
* @param image The [Image] to be set as the compass image.
* @return The [MeepMeep] instance for method chaining.
*/
fun setCompassImage(image: Image): MeepMeep {
// Read the image into a buffered image
val bufferedImage = image as BufferedImage
DEFAULT_COMPASS_ENTITY.setCompassImage(bufferedImage, colorManager.isDarkMode)

return this
}

/**
* Sets the display position for the mouse coordinates on the canvas.
*
Expand Down Expand Up @@ -791,4 +838,40 @@ class MeepMeep @JvmOverloads constructor(
FIELD_INTOTHEDEEP_JUICE_GREYSCALE,
FIELD_INTOTHEDEEP_JUICE_BLACK
}

/**
* Enum class representing various compass image options for the MeepMeep
* application.
*
* Each enum constant corresponds to a specific compass image that can be
* set using the [MeepMeep.setCompassImage] method.
*
* @see [MeepMeep.setCompassImage]
* @see [MeepMeep.setCompassImage]
*/
enum class CompassImage {
/**
* Simple Compass type, automatically selects black or white text based on
* color scheme ([ColorManager.isDarkMode]).
*/
SIMPLE,

/** Simple Compass type with black text. */
SIMPLE_BLACK,

/** Simple Compass type with white text. */
SIMPLE_WHITE,

/**
* Compass Rose type, automatically selects white or black text based on
* color scheme ([ColorManager.isDarkMode]).
*/
COMPASS_ROSE,

/** Compass Rose type with white text. */
COMPASS_ROSE_WHITE,

/** Compass Rose type with black text. */
COMPASS_ROSE_BLACK
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,18 @@ class CompassEntity(
}
}

/**
* Switches the image used by the compass entity. This function is to be
* used through [MeepMeep.setCompassImage], not directly.
*
* @param image The new image.
*/
fun setCompassImage(image: BufferedImage, isDark: Boolean) {
bgDark = image
bgLight = image
redraw()
}

/**
* Handles mouse moved events to animate the compass opacity.
*
Expand Down

0 comments on commit c9bb1bf

Please sign in to comment.