Skip to content

Commit

Permalink
- Improve paper size handling (for printing)
Browse files Browse the repository at this point in the history
- Allow setting canvas size.
  • Loading branch information
litan committed Oct 28, 2019
1 parent c0d9f18 commit a9fc243
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 34 deletions.
16 changes: 15 additions & 1 deletion src/main/scala/net/kogics/kojo/lite/Builtins.scala
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,22 @@ Here's a partial list of the available commands:
dch.setResizeRequest(new Dimension(math.round(newWidth).toInt, b.height), true)
}

def setDrawingCanvasSize(width: Int, height: Int): Unit = {
val dch = Main.drawingCanvasHolder
import java.awt.Dimension
dch.setResizeRequest(new Dimension(width, height), true)
}

def setDrawingCanvasToA4(): Unit = {
setDrawingCanvasAspectRatio(210.0 / 297)
setDrawingCanvasAspectRatio(210.0 / 297.0)
}

def setDrawingCanvasToA4Landscape(): Unit = {
setDrawingCanvasAspectRatio(297.0 / 210.0)
}

def setDrawingCanvasToA3(): Unit = {
setDrawingCanvasAspectRatio(297.0 / 420.0)
}

val hueMod = Utils.hueMod _
Expand Down
52 changes: 26 additions & 26 deletions src/main/scala/net/kogics/kojo/lite/SettingsWindow.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ class SettingsWindow(owner: JFrame) extends JDialog(owner) {
val dpiTf = TextField(dpi)
dpiTf.setColumns(3)

def handleInchesDdSelection(item: String): Unit = {
if (item.toLowerCase.trim == "a4") {
aspectTf.setText("A4")
def handleInchesDdSelection(item: String): Unit = PaperSize.fromString(item) match {
case Some(ps) =>
aspectTf.setText(ps.name)
dimensionDd.setSelectedItem("height")
dimensionDd.setEnabled(false)
adjustCanvasBtn.requestFocusInWindow()
}
case None =>
}

val inchesDd = DropDown("A4")
val inchesDd = DropDown(PaperSize.allSizes: _*)
inchesDd.setEditable(true)
val inchesDdEditor = {
try {
Expand Down Expand Up @@ -118,15 +118,20 @@ class SettingsWindow(owner: JFrame) extends JDialog(owner) {
adjustCanvasBtn.addActionListener(new ActionListener {
def actionPerformed(e: ActionEvent): Unit = {
changeModality(false)
aspectTf.value.toLowerCase.trim match {
case "a4" =>
Builtins.instance.setDrawingCanvasToA4()
PaperSize.fromString(aspectTf.value) match {
case Some(ps) =>
ps match {
case A4 =>
Builtins.instance.setDrawingCanvasToA4()
case A4Landscape =>
Builtins.instance.setDrawingCanvasToA4Landscape()
}
Utils.runLaterInSwingThread {
setCurrentAspectRatio()
}
case s =>
case None =>
try {
val r = s.toDouble
val r = aspectTf.value.toDouble
Builtins.instance.setDrawingCanvasAspectRatio(r)
Utils.runLaterInSwingThread {
setCurrentAspectRatio()
Expand All @@ -139,13 +144,7 @@ class SettingsWindow(owner: JFrame) extends JDialog(owner) {
}
})
val aspectTf = TextField("")
// aspectTf.addKeyListener(new KeyAdapter {
// override def keyTyped(e: KeyEvent): Unit = {
// if (inchesDd.value.toLowerCase.trim == "a4")
// inchesDdEditor.setText("")
// dimensionDd.setEnabled(true)
// }
// })
aspectTf.setColumns(8)
handleInchesDdSelection(inchesDd.value)
val currentAspectTf = Label("")
setCurrentAspectRatio()
Expand All @@ -161,15 +160,16 @@ class SettingsWindow(owner: JFrame) extends JDialog(owner) {
val newTheme = themeDd.value
var newInches = {
val v = inchesDd.value
if (v.toLowerCase.trim == "a4") v
else {
try {
v.toDouble
v
}
catch {
case throwable: Throwable => ""
}
PaperSize.fromString(v) match {
case Some(ps) => ps.name
case None =>
try {
v.toDouble
v
}
catch {
case throwable: Throwable => ""
}
}
}
val newDpi = if (newInches.trim == "") "" else {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/net/kogics/kojo/lite/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package net.kogics.kojo.lite
object Versions {
val KojoMajorVersion = "2.7"
val KojoVersion = "2.7.08"
val KojoRevision = "r15"
val KojoBuildDate = "24 October 2019"
val KojoRevision = "r16"
val KojoBuildDate = "28 October 2019"
val JavaVersion = {
val jrv = System.getProperty("java.runtime.version")
val arch = System.getProperty("os.arch")
Expand Down
9 changes: 4 additions & 5 deletions src/main/scala/net/kogics/kojo/lite/canvas/SpriteCanvas.scala
Original file line number Diff line number Diff line change
Expand Up @@ -957,19 +957,18 @@ class SpriteCanvas(val kojoCtx: core.KojoCtx) extends PSwingCanvas with SCanvas
val fchooser = new FileChooser(kojoCtx)
override def actionPerformed(e: ActionEvent) {
val file = fchooser.chooseFile(Utils.stripDots(Utils.loadString("S_SaveAs")), "PNG Image File", "png")
val a4_dims = Map("height" -> 11.693, "width" -> 8.268)
if (file != null) {
Utils.appProperty("export.image.dpi") match {
case Some(sDpi) if sDpi.length > 0 =>
val dpi = sDpi.toInt
val dim = Utils.appProperty("export.image.dimension").getOrElse("height")
val inches = Utils.appProperty("export.image.inches") match {
case Some(inchVal) =>
inchVal.toLowerCase.trim match {
case "a4" => a4_dims.getOrElse(dim, 0.0)
case sInch => sInch.toDouble
PaperSize.fromString(inchVal) match {
case Some(ps) => if (dim == "height") ps.height else ps.width
case None => inchVal.toDouble
}
case None => a4_dims.getOrElse(dim, 11.0)
case None => 1.0
}

if (dim == "height") {
Expand Down
38 changes: 38 additions & 0 deletions src/main/scala/net/kogics/kojo/lite/papersizes.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package net.kogics.kojo.lite

trait PaperSize {
def name: String
def width: Double
def height: Double
}

case object A4 extends PaperSize {
val name = "A4"
val width = 8.268
val height = 11.693
}

case object A4Landscape extends PaperSize {
val name = "A4 Landscape"
val width = 11.693
val height = 8.268
}

object PaperSize {
def fromString(s: String): Option[PaperSize] = {
if (s == null) {
None
}
else {
s.toLowerCase.trim match {
case "a4" => Some(A4)
case "a4 landscape" => Some(A4Landscape)
case _ => None
}
}
}

def allSizes: Seq[String] = {
List(A4, A4Landscape).map(_.name)
}
}

0 comments on commit a9fc243

Please sign in to comment.