Skip to content

Commit

Permalink
finished region manip strats
Browse files Browse the repository at this point in the history
  • Loading branch information
pier-bezuhoff committed Feb 4, 2025
1 parent 8d80bd7 commit 6619a91
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@
<string name="attributes_category_name">Удаление/дублирование</string>
<string name="transform_category_name">Преобразования</string>
<string name="create_category_name">Создание</string>
<!-- region manipulation strategies -->
<string name="region_manipulation_strategy_replace">&#120491; Замена</string>
<string name="region_manipulation_strategy_add">+ Добавление</string>
<string name="region_manipulation_strategy_erase">- Стирание</string>
<!-- color picker dialog -->
<string name="color_picker_title">Выберите цвет</string>
<string name="hex_name">hex</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@
<string name="attributes_category_name">Delete/Duplicate</string>
<string name="transform_category_name">Transform</string>
<string name="create_category_name">Create</string>

<!-- region manipulation strategies -->
<string name="region_manipulation_strategy_replace">&#120491; Replace</string>
<string name="region_manipulation_strategy_add">+ Add</string>
<string name="region_manipulation_strategy_erase">- Erase</string>

<!-- color picker dialog -->
<string name="color_picker_title">Pick a color</string>
<string name="hex_name">hex</string>
Expand Down Expand Up @@ -192,7 +198,6 @@
<string name="failed_open_notice">Failed to load</string>
<string name="successful_save_notice">Saved as</string>
<string name="failed_save_notice">Failed to save</string>
<!-- <string name="_notice"></string>-->

<!-- test -->
<string name="hello">😊 Hello world!</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,37 @@ import androidx.compose.animation.core.FastOutLinearInEasing
import androidx.compose.animation.core.LinearEasing
import androidx.compose.animation.core.tween
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.BoxScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowColumn
import androidx.compose.foundation.layout.IntrinsicSize
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.selection.selectable
import androidx.compose.foundation.selection.selectableGroup
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.RadioButton
import androidx.compose.material3.RadioButtonDefaults
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.drawBehind
import androidx.compose.ui.draw.drawWithCache
Expand Down Expand Up @@ -48,6 +67,7 @@ import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.graphics.rememberGraphicsLayer
import androidx.compose.ui.layout.onSizeChanged
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.IntSize
Expand All @@ -63,6 +83,7 @@ import data.geometry.Point
import data.geometry.fromCorners
import dodeclusters.composeapp.generated.resources.Res
import dodeclusters.composeapp.generated.resources.rotate_counterclockwise
import dodeclusters.composeapp.generated.resources.stub
import dodeclusters.composeapp.generated.resources.zoom_in
import domain.Arg
import domain.ChessboardPattern
Expand All @@ -74,6 +95,7 @@ import getPlatform
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.launch
import org.jetbrains.compose.resources.painterResource
import org.jetbrains.compose.resources.stringResource
import ui.SimpleToolButton
import ui.circle2path
import ui.reactiveCanvas
Expand Down Expand Up @@ -131,8 +153,6 @@ fun BoxScope.EditClusterCanvas(
val animations: MutableMap<ColoredContourAnimation, Animatable<Float, AnimationVector1D>> =
remember { mutableStateMapOf() }
val coroutineScope = rememberCoroutineScope()
val screenshotableGraphicsLayer = rememberGraphicsLayer()
screenshotableGraphicsLayer.compositingStrategy = androidx.compose.ui.graphics.layer.CompositingStrategy.Offscreen
coroutineScope.launch { // listen to circle animations
viewModel.animations.collect { event ->
when (event) {
Expand Down Expand Up @@ -210,6 +230,12 @@ fun BoxScope.EditClusterCanvas(
) {
ArcPathContextActions(viewModel.canvasSize, viewModel::toolAction)
}
if (viewModel.mode == SelectionMode.Region) {
RegionManipulationStrategySelector(
currentStrategy = viewModel.regionManipulationStrategy,
setStrategy = viewModel::setRegionsManipulationStrategy
)
}
}
}

Expand Down Expand Up @@ -1035,6 +1061,56 @@ fun PointSelectionContextActions(
}
}

@Composable
fun BoxScope.RegionManipulationStrategySelector(
currentStrategy: RegionManipulationStrategy,
setStrategy: (RegionManipulationStrategy) -> Unit,
modifier: Modifier = Modifier,
) {
Surface(
modifier = modifier.align(Alignment.CenterEnd),
shape = RoundedCornerShape(16.dp),
color = MaterialTheme.colorScheme.secondaryContainer.copy(alpha = 0.4f),
contentColor = MaterialTheme.colorScheme.onSecondaryContainer,
) {
Column(Modifier
.width(IntrinsicSize.Max)
.selectableGroup()
) {
RegionManipulationStrategy.entries.forEach { strategy ->
Row(
Modifier
.selectable(
selected = (strategy == currentStrategy),
onClick = { setStrategy(strategy) },
role = Role.RadioButton
)
.height(56.dp)
.padding(horizontal = 16.dp)
.fillMaxWidth()
,
verticalAlignment = Alignment.CenterVertically
) {
RadioButton(
selected = (strategy == currentStrategy),
onClick = null, // null recommended for accessibility with screen readers
colors = RadioButtonDefaults.colors().copy(
selectedColor = MaterialTheme.colorScheme.secondary,
)
)
Text(
text = stringResource(strategy.stringResource),
modifier = Modifier
.padding(start = 16.dp)
.weight(1f),
style = MaterialTheme.typography.bodyLarge,
)
}
}
}
}
}

fun DrawScope.drawSelectionControls(
canvasSize: IntSize,
selectionIsLocked: Boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
package ui.edit_cluster

import androidx.compose.runtime.Immutable
import dodeclusters.composeapp.generated.resources.Res
import dodeclusters.composeapp.generated.resources.region_manipulation_strategy_add
import dodeclusters.composeapp.generated.resources.region_manipulation_strategy_erase
import dodeclusters.composeapp.generated.resources.region_manipulation_strategy_replace
import kotlinx.serialization.Serializable
import org.jetbrains.compose.resources.StringResource

@Immutable
@Serializable
enum class RegionManipulationStrategy {
enum class RegionManipulationStrategy(
val stringResource: StringResource
) {
/** When clicking into an existing region, intelligently prefer XOR-style replacing */
REPLACE,
REPLACE(Res.string.region_manipulation_strategy_replace),
/** Only & always add new regions within set of delimiters */
ADD,
ADD(Res.string.region_manipulation_strategy_add),
/** Only & always erase minimal existing outer regions */
ERASE,
ERASE(Res.string.region_manipulation_strategy_erase),
}

0 comments on commit 6619a91

Please sign in to comment.