From e9284e21587670f8971a651bc2f7114698f5c245 Mon Sep 17 00:00:00 2001
From: dalyjame <dalyjame@msu.edu>
Date: Mon, 1 Jul 2019 08:52:09 -0400
Subject: [PATCH] Can build animations with multiple frames and play the
 animation.  Still need to be able to save/load animations and to pick the
 images

---
 .../src/fmon/builder/AnimationBuilder.fxml    | 13 +++--
 .../src/fmon/builder/AnimationBuilder.scala   | 56 ++++++++++++++-----
 FakeMon/src/fmon/draw/Palette.scala           |  6 ++
 3 files changed, 58 insertions(+), 17 deletions(-)

diff --git a/Builder/src/fmon/builder/AnimationBuilder.fxml b/Builder/src/fmon/builder/AnimationBuilder.fxml
index cce780a..b09e010 100644
--- a/Builder/src/fmon/builder/AnimationBuilder.fxml
+++ b/Builder/src/fmon/builder/AnimationBuilder.fxml
@@ -10,7 +10,7 @@
 <?import javafx.scene.layout.RowConstraints?>
 <?import javafx.scene.layout.TilePane?>
 
-<GridPane xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fmon.builder.AnimationBuilder">
+<GridPane xmlns="http://javafx.com/javafx/11.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="fmon.builder.AnimationBuilder">
    <columnConstraints>
       <ColumnConstraints hgrow="SOMETIMES" maxWidth="234.0" minWidth="10.0" prefWidth="127.0" />
       <ColumnConstraints hgrow="SOMETIMES" maxWidth="308.0" minWidth="10.0" prefWidth="308.0" />
@@ -23,10 +23,15 @@
    </rowConstraints>
    <children>
       <ListView fx:id="animationList" prefWidth="150.0" />
-      <Button mnemonicParsing="false" onAction="#addAnimation" text="+" GridPane.rowIndex="1" />
+      <HBox alignment="CENTER_LEFT" GridPane.rowIndex="1">
+         <children>
+            <Button mnemonicParsing="false" onAction="#addAnimation" text="+" />
+            <Button mnemonicParsing="false" onAction="#quickBuildAnimation" text="Build" />
+         </children>
+      </HBox>
       <ImageView fx:id="viewpane" fitHeight="269.0" fitWidth="308.0" pickOnBounds="true" preserveRatio="true" GridPane.columnIndex="1" />
       <ListView fx:id="frameList" prefWidth="150.0" GridPane.columnIndex="2" />
-      <HBox prefHeight="100.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
+      <HBox alignment="CENTER_LEFT" prefHeight="100.0" prefWidth="200.0" GridPane.columnIndex="2" GridPane.rowIndex="1">
          <children>
             <Button mnemonicParsing="false" onAction="#playAnimation" text="Play" />
             <Button mnemonicParsing="false" onAction="#addFrame" text="+" />
@@ -35,7 +40,7 @@
       </HBox>
       <ScrollPane GridPane.columnSpan="3" GridPane.rowIndex="2">
          <content>
-            <TilePane fx:id="tiles" hgap="5.0" prefHeight="121.0" prefWidth="600.0" vgap="5.0" />
+            <TilePane fx:id="tiles" hgap="5.0" vgap="5.0" />
          </content>
       </ScrollPane>
    </children>
diff --git a/Builder/src/fmon/builder/AnimationBuilder.scala b/Builder/src/fmon/builder/AnimationBuilder.scala
index 2750709..f2fd1ed 100644
--- a/Builder/src/fmon/builder/AnimationBuilder.scala
+++ b/Builder/src/fmon/builder/AnimationBuilder.scala
@@ -14,10 +14,12 @@ import scalafx.beans.property._
 import scalafx.collections.ObservableBuffer
 import scalafx.scene.control._
 import scalafx.scene.image.ImageView
+import scalafx.scene.input.MouseEvent
 import scalafx.scene.paint.Color
 import scalafx.scene.layout._
+import scalafx.util.Duration
 
-import fmon.draw.Palette
+import fmon.draw._
 
 class ObsAnimation(name_ : String) {
   val name = new StringProperty(this, "name", name_)
@@ -26,7 +28,7 @@ class ObsAnimation(name_ : String) {
   override def toString = name()
 }
 
-case class FrameToken() {
+case class FrameToken(img: StillImg) {
 }
 
 class AnimationBuilder extends Savable {
@@ -37,33 +39,47 @@ class AnimationBuilder extends Savable {
   
   val animations = ObservableBuffer[ObsAnimation]()
   
+  val palette = new Palette(new FileInputStream(raw"C:\Users\dalyj\Documents\Design\Images\TimmahLexusX reduced\Reduced Animations\Fireball.png"), 5, 3)
+  
   @FXML
   def initialize(): Unit = {
     animationList.items = animations
     animationList.selectionModel().selectedIndex.onChange(selectionChange)
-    val palette = new Palette(new FileInputStream(raw"C:\Users\James\Documents\Design\Images\TimmahLexusX\Standalone Animations\Fireball.png"), 5, 3)
+    frameList.selectionModel().selectedIndex.onChange(selectFrame)
     //for (x <- 0 until palette.numAcross; y <- 0 until palette.numDown) {
-    val children = for (y <- 0 until palette.numDown; x <- 0 until palette.numAcross) yield {
-      val img = palette(x, y)
+    val images = palette.images
+    val children = images.map(img => { 
       val imgView = new ImageView() {
         scaleX = 0.5
         scaleY = 0.5
         image = img.croppedImage()
+        
+      }
+      imgView.handleEvent(MouseEvent.Any) {
+        e: MouseEvent => if (e.clickCount == 2 && e.eventType == MouseEvent.MouseClicked) {
+          addFrame(img)
+        }
       }
       imgView.delegate
-      //imgView.autosize()
-      //println(imgView.getBoundsInParent)
-      //img.draw(imgView)
-      //imgView.resize(100, 100)
-    }
+    })
     
     tiles.children ++= children
     tiles.prefTileWidth = 96 // TODO : I think this has to do with how the image is scaled
     tiles.prefTileHeight = 96
+    
+    addAnimation()
+    animationList.selectionModel().selectFirst()
   }
   
   def addAnimation(): Unit = {
     animations += new ObsAnimation("Animation")
+    animationList.selectionModel().selectLast()
+  }
+  
+  def quickBuildAnimation(): Unit = {
+    animations += new ObsAnimation("Animation")
+    animationList.selectionModel().selectLast()
+    palette.images.foreach(addFrame(_))
   }
   
   def selectionChange(): Unit = {
@@ -71,18 +87,32 @@ class AnimationBuilder extends Savable {
   }
   
   def playAnimation(): Unit = {
-    
+    val images = frameList.items().map(f => f.img).toIndexedSeq
+    val animated = new AnimatedImage(images)
+    val duration = new Duration(50 * images.size)
+    val animator = new SpriteAnimation(viewpane, animated, duration)
+    animator.play()
   }
   
   def addFrame(): Unit = {
-    println("New frame")
-    selected.frames += new FrameToken
+    //println("New frame")
+    //selected.frames += new FrameToken
+  }
+  
+  def addFrame(img: StillImg): Unit = {
+    println("Image")
+    selected.frames += new FrameToken(img)
   }
   
   def removeFrame(): Unit = {
     frameList.items() --= frameList.selectionModel().getSelectedItems
   }
   
+  def selectFrame(): Unit = {
+    val frame = frameList.selectionModel().getSelectedItem
+    frame.img.draw(viewpane, 0)
+  }
+  
   override def saveTo(file: File): Unit = {
     
   }
diff --git a/FakeMon/src/fmon/draw/Palette.scala b/FakeMon/src/fmon/draw/Palette.scala
index 6a9c33a..63ceb1e 100644
--- a/FakeMon/src/fmon/draw/Palette.scala
+++ b/FakeMon/src/fmon/draw/Palette.scala
@@ -41,4 +41,10 @@ class Palette(val image: Image, val numAcross: Int, val numDown: Int) {
   def apply(cells : Box): StillImg = {
     this(cells.x, cells.y, cells.width, cells.height)
   }
+  
+  def images = {
+    for (y <- 0 until numDown; x <- 0 until numAcross) yield {
+      this(x, y)
+    }
+  }
 }
\ No newline at end of file