Last active
November 21, 2023 14:33
-
-
Save ansisec/23683494e42076d8a44cf22b7015b538 to your computer and use it in GitHub Desktop.
Sketches base v_final
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Ficheiros auxiliares para a construção da versão final a ser desenvolvida na aula |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed class Sketch(val id: Int) : Serializable { | |
class SketchSolidColor(id : Int,val color : Int) : Sketch(id) | |
class SketchFigure(id : Int,val imagePath : String) : Sketch(id) | |
data class Point(val x: Int, val y: Int) : Serializable | |
data class Line(val color : Int, val points: List<Point>) : Serializable | |
private val date: Date = Date() | |
val lines = mutableListOf<Line>() | |
var title : String = "none" | |
set(value) { | |
field = value.ifBlank { "none" } | |
} | |
val strDate : String | |
@SuppressLint("SimpleDateFormat") | |
get() { | |
val df = SimpleDateFormat("yyyy.MM.dd HH:mm") | |
return df.format(date) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SketchesData(private val sketchesFile: File? = null) { | |
private var _sketches = sketchesFile?.let { load(it) } ?: mutableListOf() | |
val sketches: List<Sketch> | |
get() = _sketches | |
fun getNewSketchId() = | |
(_sketches.maxOfOrNull { it.id } ?: 0) + 1 | |
fun add(sketch: Sketch) { | |
_sketches.removeIf { | |
it.id == sketch.id | |
} | |
_sketches.add(sketch) | |
save() | |
} | |
fun findSketchById(id: Int): Sketch? { | |
return _sketches.find { it.id == id } | |
} | |
fun save(file: File? = sketchesFile) { | |
ObjectOutputStream(FileOutputStream(file)).use { | |
it.writeObject(_sketches) | |
} | |
} | |
@Suppress("UNCHECKED_CAST") | |
private fun load(file: File): MutableList<Sketch>? { | |
try { | |
ObjectInputStream(FileInputStream(file)).use { | |
return it.readObject() as MutableList<Sketch> | |
} | |
} catch (_: Exception) { } | |
return null | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
private var sketchId = -1 | |
var color : Int | |
get() = Color(colorR.intValue,colorG.intValue,colorB.intValue).toArgb() | |
set(value) { | |
colorR.intValue = AColor.red(value) | |
colorG.intValue = AColor.green(value) | |
colorB.intValue = AColor.blue(value) | |
} | |
fun saveSketch() { | |
if (sketchId<1) | |
sketchId = sketchesData.getNewSketchId() | |
val newSketch = if (imagePath.value != null) | |
Sketch.SketchFigure(sketchId,imagePath.value!!) | |
else | |
Sketch.SketchSolidColor(sketchId,color) | |
newSketch.let {sketch -> | |
sketch.title = sketchTitle.value | |
lines.forEach {line -> | |
sketch.lines.add( | |
Sketch.Line(line.color.toArgb(),line.points.map { offset -> | |
Sketch.Point(offset.x.toInt(),offset.y.toInt()) | |
}) | |
) | |
} | |
} | |
sketchesData.add(newSketch) | |
} | |
fun initSketch(id : Int = -1) { | |
sketchTitle.value = "" | |
colorR.intValue = 255 | |
colorG.intValue = 255 | |
colorB.intValue = 255 | |
imagePath.value = null | |
_lines.clear() | |
sketchId = id | |
if (id<1) { | |
return | |
} | |
sketchesData.findSketchById(id)?.let { sketch -> | |
sketchTitle.value = sketch.title | |
when (sketch) { | |
is Sketch.SketchSolidColor -> { | |
color = sketch.color | |
} | |
is Sketch.SketchFigure -> { | |
imagePath.value = sketch.imagePath | |
} | |
} | |
sketch.lines.forEach {line -> | |
_lines.add( | |
DrawingLine( | |
color = Color(AColor.red(line.color),AColor.green(line.color),AColor.blue(line.color)), | |
points = line.points.map {point -> | |
Offset(point.x.toFloat(),point.y.toFloat()) | |
} | |
) | |
) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
... | |
Column( | |
verticalArrangement = Arrangement.Center, | |
modifier = Modifier | |
.fillMaxSize() | |
.padding(8.dp) | |
) { | |
Text( | |
text = "Title: ${it.title}", | |
fontWeight = FontWeight.Bold, | |
fontSize = 20.sp | |
) | |
Spacer(modifier = Modifier.height(16.dp)) | |
Text(text = "Last modification: ${it.strDate}", fontSize = 12.sp) | |
} | |
... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment