You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pivot.apache.org by tv...@apache.org on 2009/04/30 22:59:15 UTC

svn commit: r770443 - in /incubator/pivot/trunk/wtk/src/pivot/wtk: MovieView.java MovieViewListener.java Theme.java media/Movie.java media/MovieListener.java skin/MovieViewSkin.java

Author: tvolkert
Date: Thu Apr 30 20:59:15 2009
New Revision: 770443

URL: http://svn.apache.org/viewvc?rev=770443&view=rev
Log:
Initial MovieView and MovieViewSkin implementations

Added:
    incubator/pivot/trunk/wtk/src/pivot/wtk/MovieView.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/MovieViewListener.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MovieViewSkin.java
Modified:
    incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/media/Movie.java
    incubator/pivot/trunk/wtk/src/pivot/wtk/media/MovieListener.java

Added: incubator/pivot/trunk/wtk/src/pivot/wtk/MovieView.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/MovieView.java?rev=770443&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/MovieView.java (added)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/MovieView.java Thu Apr 30 20:59:15 2009
@@ -0,0 +1,94 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.wtk;
+
+import pivot.util.ListenerList;
+import pivot.wtk.media.Movie;
+
+/**
+ * Component that plays a movie.
+ *
+ * @author tvolkert
+ */
+public class MovieView extends Component {
+    private static class MovieViewListenerList extends ListenerList<MovieViewListener>
+        implements MovieViewListener {
+        public void movieChanged(MovieView movieView, Movie previousMovie) {
+            for (MovieViewListener listener : this) {
+                listener.movieChanged(movieView, previousMovie);
+            }
+        }
+    }
+
+    private Movie movie = null;
+
+    private MovieViewListenerList movieViewListeners = new MovieViewListenerList();
+
+    /**
+     * Creates an empty movie view.
+     */
+    public MovieView() {
+        this(null);
+    }
+
+    /**
+     * Creates an movie view with the given movie.
+     *
+     * @param movie
+     * The initial movie to set, or <tt>null</tt> for no movie.
+     */
+    public MovieView(Movie movie) {
+        setMovie(movie);
+
+        installSkin(MovieView.class);
+    }
+
+    /**
+     * Returns the movie view's current movie.
+     *
+     * @return
+     * The current movie, or <tt>null</tt> if no movie is set.
+     */
+    public Movie getMovie() {
+        return movie;
+    }
+
+    /**
+     * Sets the movie view's current movie.
+     *
+     * @param movie
+     * The movie to set, or <tt>null</tt> for no movie.
+     */
+    public void setMovie(Movie movie) {
+        Movie previousMovie = this.movie;
+
+        if (previousMovie != movie) {
+            this.movie = movie;
+            movieViewListeners.movieChanged(this, previousMovie);
+        }
+    }
+
+    /**
+     * Returns the movie view listener list.
+     *
+     * @return
+     * The movie view listener list.
+     */
+    public ListenerList<MovieViewListener> getMovieViewListeners() {
+        return movieViewListeners;
+    }
+}

Added: incubator/pivot/trunk/wtk/src/pivot/wtk/MovieViewListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/MovieViewListener.java?rev=770443&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/MovieViewListener.java (added)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/MovieViewListener.java Thu Apr 30 20:59:15 2009
@@ -0,0 +1,34 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.wtk;
+
+import pivot.wtk.media.Movie;
+
+/**
+ * Movie view listener interface.
+ *
+ * @author tvolkert
+ */
+public interface MovieViewListener {
+    /**
+     * Called when an movie view's movie has changed.
+     *
+     * @param movieView
+     * @param previousMovie
+     */
+    public void movieChanged(MovieView movieView, Movie previousMovie);
+}

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java?rev=770443&r1=770442&r2=770443&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/Theme.java Thu Apr 30 20:59:15 2009
@@ -28,6 +28,7 @@
 import pivot.wtk.skin.FlowPaneSkin;
 import pivot.wtk.skin.ImageViewSkin;
 import pivot.wtk.skin.LabelSkin;
+import pivot.wtk.skin.MovieViewSkin;
 import pivot.wtk.skin.ScrollPaneSkin;
 import pivot.wtk.skin.SeparatorSkin;
 import pivot.wtk.skin.StackPaneSkin;
@@ -71,6 +72,7 @@
         componentSkinMap.put(FlowPane.class, FlowPaneSkin.class);
         componentSkinMap.put(ImageView.class, ImageViewSkin.class);
         componentSkinMap.put(Label.class, LabelSkin.class);
+        componentSkinMap.put(MovieView.class, MovieViewSkin.class);
         componentSkinMap.put(ScrollPane.class, ScrollPaneSkin.class);
         componentSkinMap.put(Separator.class, SeparatorSkin.class);
         componentSkinMap.put(StackPane.class, StackPaneSkin.class);

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/media/Movie.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/media/Movie.java?rev=770443&r1=770442&r2=770443&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/media/Movie.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/media/Movie.java Thu Apr 30 20:59:15 2009
@@ -36,6 +36,12 @@
             }
         }
 
+        public void loopingChanged(Movie movie) {
+            for (MovieListener listener : this) {
+                listener.loopingChanged(movie);
+            }
+        }
+
         public void movieStarted(Movie movie) {
             for (MovieListener listener : this) {
                 listener.movieStarted(movie);
@@ -57,6 +63,7 @@
 
     private int currentFrame = -1;
     private int frameRate = 26;
+    private boolean looping = false;
 
     private ApplicationContext.ScheduledCallback scheduledCallback = null;
 
@@ -65,7 +72,11 @@
     private final Runnable nextFrameCallback = new Runnable() {
         public void run() {
             if (currentFrame == getTotalFrames() - 1) {
-                stop();
+                if (looping) {
+                    setCurrentFrame(0);
+                } else {
+                    stop();
+                }
             } else {
                 setCurrentFrame(currentFrame + 1);
             }
@@ -78,14 +89,6 @@
 
     public abstract int getTotalFrames();
 
-    private int getFrameRate() {
-        return frameRate;
-    }
-
-    private void setFrameRate(int frameRate) {
-        this.frameRate = frameRate;
-    }
-
     public int getCurrentFrame() {
         return currentFrame;
     }
@@ -99,6 +102,25 @@
         }
     }
 
+    public int getFrameRate() {
+        return frameRate;
+    }
+
+    public void setFrameRate(int frameRate) {
+        this.frameRate = frameRate;
+    }
+
+    public boolean isLooping() {
+        return looping;
+    }
+
+    public void setLooping(boolean looping) {
+        if (this.looping != looping) {
+            this.looping = looping;
+            movieListeners.loopingChanged(this);
+        }
+    }
+
     public void play() {
         if (scheduledCallback != null) {
             throw new IllegalStateException("Movie is already playing.");
@@ -123,4 +145,8 @@
     public boolean isPlaying() {
         return (scheduledCallback != null);
     }
+
+    public ListenerList<MovieListener> getMovieListeners() {
+        return movieListeners;
+    }
 }

Modified: incubator/pivot/trunk/wtk/src/pivot/wtk/media/MovieListener.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/media/MovieListener.java?rev=770443&r1=770442&r2=770443&view=diff
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/media/MovieListener.java (original)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/media/MovieListener.java Thu Apr 30 20:59:15 2009
@@ -23,6 +23,28 @@
  */
 public interface MovieListener {
     /**
+     * Adapts the <tt>MovieListener</tt> interface.
+     *
+     * @author tvolkert
+     */
+    public static class Adapter implements MovieListener {
+        public void currentFrameChanged(Movie movie, int previousFrame) {
+        }
+
+        public void loopingChanged(Movie movie) {
+        }
+
+        public void movieStarted(Movie movie) {
+        }
+
+        public void movieStopped(Movie movie) {
+        }
+
+        public void regionUpdated(Movie movie, int x, int y, int width, int height) {
+        }
+    }
+
+    /**
      * Called when the movie's current frame changed.
      *
      * @param movie
@@ -31,6 +53,13 @@
     public void currentFrameChanged(Movie movie, int previousFrame);
 
     /**
+     * Called when the movie's looping property changed.
+     *
+     * @param movie
+     */
+    public void loopingChanged(Movie movie);
+
+    /**
      * Called when the movie begins playing. The frame at which the movie is
      * starting can be obtained via <tt>getCurrentFrame()</tt> (it is not
      * guaranteed to be positioned before the first frame when it is started).

Added: incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MovieViewSkin.java
URL: http://svn.apache.org/viewvc/incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MovieViewSkin.java?rev=770443&view=auto
==============================================================================
--- incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MovieViewSkin.java (added)
+++ incubator/pivot/trunk/wtk/src/pivot/wtk/skin/MovieViewSkin.java Thu Apr 30 20:59:15 2009
@@ -0,0 +1,252 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License,
+ * Version 2.0 (the "License"); you may not use this file except in
+ * compliance with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package pivot.wtk.skin;
+
+import java.awt.AlphaComposite;
+import java.awt.Color;
+import java.awt.Composite;
+import java.awt.Graphics2D;
+import java.awt.Transparency;
+
+import pivot.wtk.Component;
+import pivot.wtk.Dimensions;
+import pivot.wtk.HorizontalAlignment;
+import pivot.wtk.MovieView;
+import pivot.wtk.MovieViewListener;
+import pivot.wtk.VerticalAlignment;
+import pivot.wtk.media.Movie;
+import pivot.wtk.media.MovieListener;
+
+/**
+ * Movie view skin.
+ *
+ * @author tvolkert
+ */
+public class MovieViewSkin extends ComponentSkin implements MovieViewListener {
+    private Color backgroundColor = null;
+    private HorizontalAlignment horizontalAlignment = HorizontalAlignment.CENTER;
+    private VerticalAlignment verticalAlignment = VerticalAlignment.CENTER;
+
+    private MovieListener movieListener = new MovieListener.Adapter() {
+        @Override
+        public void regionUpdated(Movie movie, int x, int y, int width, int height) {
+            // TODO This is wrong, considering alignment values
+            repaintComponent(x, y, width, height);
+        }
+    };
+
+    public void install(Component component) {
+        super.install(component);
+
+        MovieView movieView = (MovieView)component;
+        movieView.getMovieViewListeners().add(this);
+
+        Movie movie = movieView.getMovie();
+        if (movie != null) {
+            movie.getMovieListeners().add(movieListener);
+        }
+    }
+
+    public void uninstall() {
+        MovieView movieView = (MovieView)getComponent();
+        Movie movie = movieView.getMovie();
+        if (movie != null) {
+            movie.getMovieListeners().remove(movieListener);
+        }
+
+        movieView.getMovieViewListeners().remove(this);
+
+        super.uninstall();
+    }
+
+    public int getPreferredWidth(int height) {
+        MovieView movieView = (MovieView)getComponent();
+        Movie movie = movieView.getMovie();
+
+        return (movie == null) ? 0 : movie.getWidth();
+    }
+
+    public int getPreferredHeight(int width) {
+        MovieView movieView = (MovieView)getComponent();
+        Movie movie = movieView.getMovie();
+
+        return (movie == null) ? 0 : movie.getHeight();
+    }
+
+    public Dimensions getPreferredSize() {
+        MovieView movieView = (MovieView)getComponent();
+        Movie movie = movieView.getMovie();
+
+        return (movie == null) ? new Dimensions(0, 0) : movie.getSize();
+    }
+
+    public void layout() {
+        // No-op for component skins
+    }
+
+    public void paint(Graphics2D graphics) {
+        MovieView movieView = (MovieView)getComponent();
+        Movie movie = movieView.getMovie();
+
+        int width = getWidth();
+        int height = getHeight();
+
+        if (backgroundColor != null) {
+            graphics.setPaint(backgroundColor);
+            graphics.fillRect(0, 0, width, height);
+        }
+
+        if (movie != null) {
+            Dimensions movieSize = movie.getSize();
+
+            int movieX, movieY;
+
+            switch (horizontalAlignment) {
+            case CENTER:
+                movieX = (width - movieSize.width) / 2;
+                break;
+            case RIGHT:
+                movieX = width - movieSize.width;
+                break;
+            default:
+                movieX = 0;
+                break;
+            }
+
+            switch (verticalAlignment) {
+            case CENTER:
+                movieY = (height - movieSize.height) / 2;
+                break;
+            case BOTTOM:
+                movieY = height - movieSize.height;
+                break;
+            default:
+                movieY = 0;
+                break;
+            }
+
+            graphics.translate(movieX, movieY);
+            movie.paint(graphics);
+        }
+    }
+
+    /**
+     * @return
+     * <tt>false</tt>; movie views are not focusable.
+     */
+    @Override
+    public boolean isFocusable() {
+        return false;
+    }
+
+    /**
+     * A movie view's background color dictates whether or not it's opaque.
+     */
+    @Override
+    public boolean isOpaque() {
+        boolean opaque = false;
+
+        if (backgroundColor != null
+            && backgroundColor.getTransparency() == Transparency.OPAQUE) {
+            opaque = true;
+        }
+
+        return opaque;
+    }
+
+    public Color getBackgroundColor() {
+        return backgroundColor;
+    }
+
+    public void setBackgroundColor(Color backgroundColor) {
+        this.backgroundColor = backgroundColor;
+        repaintComponent();
+    }
+
+    public final void setBackgroundColor(String backgroundColor) {
+        if (backgroundColor == null) {
+            throw new IllegalArgumentException("backgroundColor is null.");
+        }
+
+        setBackgroundColor(decodeColor(backgroundColor));
+    }
+
+    public HorizontalAlignment getHorizontalAlignment() {
+        return horizontalAlignment;
+    }
+
+    public void setHorizontalAlignment(HorizontalAlignment horizontalAlignment) {
+        if (horizontalAlignment == null) {
+            throw new IllegalArgumentException("horizontalAlignment is null.");
+        }
+
+        if (horizontalAlignment == HorizontalAlignment.JUSTIFY) {
+            throw new IllegalArgumentException("JUSTIFY alignment is not supported");
+        }
+
+        this.horizontalAlignment = horizontalAlignment;
+        repaintComponent();
+    }
+
+    public final void setHorizontalAlignment(String horizontalAlignment) {
+        if (horizontalAlignment == null) {
+            throw new IllegalArgumentException("horizontalAlignment is null.");
+        }
+
+        setHorizontalAlignment(HorizontalAlignment.decode(horizontalAlignment));
+    }
+
+    public VerticalAlignment getVerticalAlignment() {
+        return verticalAlignment;
+    }
+
+    public void setVerticalAlignment(VerticalAlignment verticalAlignment) {
+        if (verticalAlignment == null) {
+            throw new IllegalArgumentException("verticalAlignment is null.");
+        }
+
+        if (verticalAlignment == VerticalAlignment.JUSTIFY) {
+            throw new IllegalArgumentException("JUSTIFY alignment is not supported");
+        }
+
+        this.verticalAlignment = verticalAlignment;
+        repaintComponent();
+    }
+
+    public final void setVerticalAlignment(String verticalAlignment) {
+        if (verticalAlignment == null) {
+            throw new IllegalArgumentException("verticalAlignment is null.");
+        }
+
+        setVerticalAlignment(VerticalAlignment.decode(verticalAlignment));
+    }
+
+    // MovieViewListener methods
+
+    public void movieChanged(MovieView movieView, Movie previousMovie) {
+        if (previousMovie != null) {
+            previousMovie.getMovieListeners().remove(movieListener);
+        }
+
+        Movie movie = movieView.getMovie();
+        if (movie != null) {
+            movie.getMovieListeners().add(movieListener);
+        }
+
+        invalidateComponent();
+    }
+}