Skip to content

Commit

Permalink
implement command dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
sproctor committed Feb 10, 2025
1 parent 4014135 commit 8f236d5
Show file tree
Hide file tree
Showing 17 changed files with 349 additions and 49 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package warlockfe.warlock3.compose.icons

import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.PathFillType
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.graphics.StrokeCap
import androidx.compose.ui.graphics.StrokeJoin
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.graphics.vector.path
import androidx.compose.ui.unit.dp

public val Arrow_right: ImageVector
get() {
if (_Arrow_right != null) {
return _Arrow_right!!
}
_Arrow_right = ImageVector.Builder(
name = "Arrow_right",
defaultWidth = 24.dp,
defaultHeight = 24.dp,
viewportWidth = 960f,
viewportHeight = 960f
).apply {
path(
fill = SolidColor(Color.Black),
fillAlpha = 1.0f,
stroke = null,
strokeAlpha = 1.0f,
strokeLineWidth = 1.0f,
strokeLineCap = StrokeCap.Butt,
strokeLineJoin = StrokeJoin.Miter,
strokeLineMiter = 1.0f,
pathFillType = PathFillType.NonZero
) {
moveTo(400f, 680f)
verticalLineToRelative(-400f)
lineToRelative(200f, 200f)
close()
}
}.build()
return _Arrow_right!!
}

private var _Arrow_right: ImageVector? = null
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ private val keyMappings = mapOf(
Key.Zero to "0",
Key.Minus to "Minus",
Key.Equals to "Equals",
Key.Backspace to "Backspace",
Key.A to "A",
Key.B to "B",
Key.C to "C",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,38 @@ import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.DropdownMenu
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.FilledTonalButton
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.contentColorFor
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import warlockfe.warlock3.compose.components.CompassView
import warlockfe.warlock3.compose.components.ResizablePanel
import warlockfe.warlock3.compose.components.ResizablePanelState
import warlockfe.warlock3.compose.icons.Arrow_right
import warlockfe.warlock3.compose.ui.components.HandsView
import warlockfe.warlock3.compose.ui.components.IndicatorView
import warlockfe.warlock3.compose.ui.components.VitalBars
import warlockfe.warlock3.compose.ui.window.ScrollEvent
import warlockfe.warlock3.compose.ui.window.WindowUiState
import warlockfe.warlock3.compose.ui.window.WindowView
import warlockfe.warlock3.compose.util.LocalLogger
import warlockfe.warlock3.compose.util.toColor
import warlockfe.warlock3.core.client.WarlockAction
import warlockfe.warlock3.core.client.WarlockMenuData
import warlockfe.warlock3.core.prefs.defaultStyles
import warlockfe.warlock3.core.text.StyleDefinition
import warlockfe.warlock3.core.window.WindowLocation
Expand Down Expand Up @@ -62,6 +71,26 @@ fun GameView(

val subWindows = viewModel.windowUiStates.collectAsState()
val mainWindow = viewModel.mainWindowUiState.collectAsState()

var openMenuId: Int? by remember { mutableStateOf(null) }
var openMenu: WarlockMenuData? by remember { mutableStateOf(null) }

LaunchedEffect(openMenuId) {
if (openMenuId != null) {
viewModel.menuData.collect { menuData ->
openMenu = menuData
}
} else {
openMenu = null
}
}

ActionContextMenu(
menuData = openMenu,
expectedMenuId = openMenuId,
onDismiss = { openMenuId = null },
)

GameTextWindows(
modifier = Modifier.fillMaxWidth().weight(1f),
subWindowUiStates = subWindows.value,
Expand All @@ -70,8 +99,20 @@ fun GameView(
topHeight = viewModel.topHeight.collectAsState(null).value,
leftWidth = viewModel.leftWidth.collectAsState(null).value,
rightWidth = viewModel.rightWidth.collectAsState(null).value,
onActionClicked = {
viewModel.sendCommand(it)
onActionClicked = { action ->
when (action) {
is WarlockAction.SendCommand -> {
viewModel.sendCommand(action.command)
}

is WarlockAction.OpenMenu -> {
openMenuId = action.onClick()
}

else -> {
// Not our concern
}
}
},
onMoveClicked = { name, location ->
viewModel.moveWindow(name = name, location = location)
Expand All @@ -96,6 +137,91 @@ fun GameView(
}
}

@Composable
fun ActionContextMenu(
menuData: WarlockMenuData?,
expectedMenuId: Int?,
onDismiss: () -> Unit,
) {
val logger = LocalLogger.current
if (menuData != null) {
if (expectedMenuId == menuData.id) {
DropdownMenu(
expanded = true,
onDismissRequest = onDismiss,
) {
val groups = menuData.items.groupBy { it.category.split('-').first() }
val categories = groups.keys.sorted()
categories.forEach { category ->
val items = groups[category]!!
if (!category.contains('_')) {
items.forEach { item ->
logger.debug { "Menu item: $item" }
DropdownMenuItem(
text = {
Text(item.label)
},
onClick = item.action
)
}
} else {
var expanded by remember(category) { mutableStateOf(false) }
DropdownMenuItem(
text = {
Text(category.split('_').getOrNull(1) ?: "Unknown")
},
onClick = { expanded = true },
trailingIcon = {
Icon(Arrow_right, contentDescription = "expandable")
}
)
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
val subgroups = items.groupBy { it.category.split('-').getOrNull(1) }
subgroups[null]?.forEach { item ->
DropdownMenuItem(
text = {
Text(item.label)
},
onClick = item.action
)
}
val subcatories = subgroups.keys.filterNotNull().sorted()
subcatories.forEach { category ->
var expanded by remember(category) { mutableStateOf(false) }
DropdownMenuItem(
text = {
Text(category)
},
onClick = { expanded = true },
trailingIcon = {
Icon(Arrow_right, contentDescription = "expandable")
}
)
DropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
subgroups[category]?.forEach { item ->
DropdownMenuItem(
text = {
Text(item.label)
},
onClick = item.action
)
}
}
}
}
}
}
}
}
}
}

@Composable
fun GameTextWindows(
modifier: Modifier,
Expand All @@ -105,7 +231,7 @@ fun GameTextWindows(
topHeight: Int?,
leftWidth: Int?,
rightWidth: Int?,
onActionClicked: (String) -> Unit,
onActionClicked: (WarlockAction) -> Unit,
onMoveClicked: (name: String, WindowLocation) -> Unit,
onHeightChanged: (String, Int) -> Unit,
onWidthChanged: (String, Int) -> Unit,
Expand Down Expand Up @@ -295,7 +421,7 @@ fun WindowViews(
windowStates: List<WindowUiState>,
selectedWindow: String,
isHorizontal: Boolean,
onActionClicked: (String) -> Unit,
onActionClicked: (WarlockAction) -> Unit,
onMoveClicked: (String, WindowLocation) -> Unit,
onWidthChanged: (String, Int) -> Unit,
onHeightChanged: (String, Int) -> Unit,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import warlockfe.warlock3.core.client.ClientProgressBarEvent
import warlockfe.warlock3.core.client.GameCharacter
import warlockfe.warlock3.core.client.ProgressBarData
import warlockfe.warlock3.core.client.SendCommandType
import warlockfe.warlock3.core.client.WarlockAction
import warlockfe.warlock3.core.client.WarlockClient
import warlockfe.warlock3.core.macro.MacroToken
import warlockfe.warlock3.core.prefs.AliasRepository
Expand Down Expand Up @@ -351,6 +352,8 @@ class GameViewModel(

val disconnected = client.disconnected

val menuData = client.menuData

init {
client.eventFlow
.onEach { event ->
Expand Down Expand Up @@ -394,19 +397,19 @@ class GameViewModel(
when (info.status) {
ScriptStatus.Running -> text += StyledString(
"pause",
WarlockStyle.Link("action" to "/pause ${it.key}")
WarlockStyle.Link(WarlockAction.SendCommand("/pause ${it.key}"))
)

ScriptStatus.Suspended -> text += StyledString(
"resume",
WarlockStyle.Link("action" to "/resume ${it.key}")
WarlockStyle.Link(WarlockAction.SendCommand("/resume ${it.key}"))
)

else -> {
// do nothing
}
}
text += StyledString(" ") + StyledString("stop", WarlockStyle.Link("action" to "/kill ${it.key}"))
text += StyledString(" ") + StyledString("stop", WarlockStyle.Link(WarlockAction.SendCommand("/kill ${it.key}")))
scriptStream.appendLine(text, false)
}
}
Expand Down Expand Up @@ -793,7 +796,7 @@ fun StreamLine.toWindowLine(
highlights: List<ViewHighlight>,
presets: Map<String, StyleDefinition>,
components: Map<String, StyledString>,
actionHandler: (String) -> Unit,
actionHandler: (WarlockAction) -> Unit,
): WindowLine? {
val textWithComponents = text.toAnnotatedString(
variables = components,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ import kotlinx.coroutines.DelicateCoroutinesApi
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import warlockfe.warlock3.compose.components.ColorPickerDialog
import warlockfe.warlock3.compose.components.FontPickerDialog
import warlockfe.warlock3.compose.components.FontUpdate
import warlockfe.warlock3.compose.components.LocalScrollbarStyle
import warlockfe.warlock3.compose.components.ScrollableColumn
import warlockfe.warlock3.compose.components.ScrollbarStyle
import warlockfe.warlock3.compose.components.FontPickerDialog
import warlockfe.warlock3.compose.components.FontUpdate
import warlockfe.warlock3.compose.util.getEntireLineStyles
import warlockfe.warlock3.compose.util.toAnnotatedString
import warlockfe.warlock3.compose.util.toColor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import warlockfe.warlock3.compose.ui.settings.WindowSettingsDialog
import warlockfe.warlock3.compose.util.LocalLogger
import warlockfe.warlock3.compose.util.createFontFamily
import warlockfe.warlock3.compose.util.toColor
import warlockfe.warlock3.core.client.WarlockAction
import warlockfe.warlock3.core.text.StyleDefinition
import warlockfe.warlock3.core.text.specifiedOrNull
import warlockfe.warlock3.core.window.Window
Expand All @@ -63,7 +64,7 @@ fun WindowView(
modifier: Modifier,
uiState: WindowUiState,
isSelected: Boolean,
onActionClicked: (String) -> Unit,
onActionClicked: (WarlockAction) -> Unit,
onMoveClicked: (WindowLocation) -> Unit,
onMoveTowardsStart: (() -> Unit)?,
onMoveTowardsEnd: (() -> Unit)?,
Expand Down Expand Up @@ -280,7 +281,7 @@ private fun WindowViewContent(
highlights: List<ViewHighlight>,
presets: Map<String, StyleDefinition>,
defaultStyle: StyleDefinition,
onActionClicked: (String) -> Unit
onActionClicked: (WarlockAction) -> Unit
) {
val logger = LocalLogger.current

Expand Down
Loading

0 comments on commit 8f236d5

Please sign in to comment.