GameActivity

com.j0h1.contour

## Class GameActivity

- java.lang.Object
- - Activity
  - - com.j0h1.contour.GameActivity

-     public class GameActivity
      extends Activity

  This activity contains the whole game logic.
  Author:
    Johannes Riedmann

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `private class` | `GameActivity.CalculateEdgeImageTask` Asynchronous Task that manages the edge image calculation.

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `private Image` | `currentEdgeImage`
    `private double` | `currentScore`
    `private Bitmap` | `drawing`
    `private DrawingView` | `drawingView`
    `static boolean` | `GAME_STARTED`
    `private double` | `overallScore`
    `private ProgressDialog` | `progressDialog`
    `private Bitmap` | `scoreImage`
    `private TextView` | `scoreView`
    `private boolean` | `sessionFinished`
    `private java.lang.String` | `sessionType`
    `private Toast` | `toast`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `GameActivity()`

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `private void` | `initializeDrawingSession()` Initialize the drawing session by attaching an edge image to the DrawingView and setting the header data.
    `void` | `onBackPressed()` When the back button is pressed during a drawing session prompt the user if he wants to end the session (and discard it) or if he wants to continue.
    `void` | `onClickNext()`
    `void` | `onConfigurationChanged(Configuration newConfig)` The activity handles the orientation changes itself, so on every orientation change (portrait to landscape and vice versa) this method is called.
    `protected void` | `onCreate(Bundle savedInstanceState)`
    `void` | `onPause()` When the user closes the game, the current progress (paths of the drawing) need to be stored.
    `private void` | `persistPaths()` Persist the currently drawn paths, held by the DrawingView, to the local database.
    `private Bitmap` | `prepareImageForScaling(Image image)` Prepare Image object, returned from local database, for scaling by converting it into a Bitmap.
    `private void` | `prepareToast(java.lang.String message)`
    `private Bitmap` | `scaleEdgeBitmapToFitParent(Bitmap edgeImageBitmap, int width_parent, int height_parent)` Scales the input image towards the dimensions of it's parent.
    `private void` | `setControlButtonsEnabled(boolean enabled)` Enables/Disabled control buttons when edge image calculation is running.
    `private void` | `setHeaderData(Image image)` Sets the header data for the session and the current drawing.
    `private void` | `setUpDrawingView(int width_parent, int height_parent)` The DrawingView holds a background image that is either an edge image or the score image.
    `private void` | `setUpLayoutListenerForDrawingViewRescaling()` This method makes sure, that the DrawingView is rescaled AFTER the layout is fully inflated and set up.
    `private void` | `showProgressDialog()` Shows a progress dialog when edge image calculation Task is in process.
    `protected void` | `updateGameActivity()` This method is called after the edge image calculation is finished.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### drawingView

          private DrawingView drawingView

    - #### drawing

          private Bitmap drawing

    - #### scoreImage

          private Bitmap scoreImage

    - #### currentEdgeImage

          private Image currentEdgeImage

    - #### currentScore

          private double currentScore

    - #### overallScore

          private double overallScore

    - #### scoreView

          private TextView scoreView

    - #### toast

          private Toast toast

    - #### progressDialog

          private ProgressDialog progressDialog

    - #### sessionType

          private java.lang.String sessionType

    - #### sessionFinished

          private boolean sessionFinished

    - #### GAME_STARTED

          public static boolean GAME_STARTED

  - ### Constructor Detail

    - #### GameActivity

          public GameActivity()

  - ### Method Detail

    - #### onCreate

          protected void onCreate(Bundle savedInstanceState)

    - #### onConfigurationChanged

          public void onConfigurationChanged(Configuration newConfig)

      The activity handles the orientation changes itself, so on every orientation change (portrait to landscape and vice versa) this method is called.

    - #### initializeDrawingSession

          private void initializeDrawingSession()

      Initialize the drawing session by attaching an edge image to the DrawingView and setting the header data. If the session is continued and some paths are persisted locally, these paths are set to the DrawingView.

    - #### onPause

          public void onPause()

      When the user closes the game, the current progress (paths of the drawing) need to be stored. If the activity is paused (user leaves the game)

    - #### setHeaderData

          private void setHeaderData(Image image)

      Sets the header data for the session and the current drawing.
      Parameters:
        `image` - Image, whose name and position is shown in the GameActivity header area.

    - #### setUpDrawingView

          private void setUpDrawingView(int width_parent,
                                        int height_parent)

      The DrawingView holds a background image that is either an edge image or the score image. By assigning the scaled background image to the DrawingView, the DrawingView is scaled towards the background image's dimensions (because it's LayoutParams are set to wrap_content in height and width)
      Parameters:
        `width_parent` - Width of DrawingView's parent.
        `height_parent` - Height of DrawingView's parent.

    - #### prepareImageForScaling

          private Bitmap prepareImageForScaling(Image image)

      Prepare Image object, returned from local database, for scaling by converting it into a Bitmap.
      Parameters:
        `image` - Image object that needs preparation.
      Returns:
        Bitmap object of extracted image data.

    - #### scaleEdgeBitmapToFitParent

          private Bitmap scaleEdgeBitmapToFitParent(Bitmap edgeImageBitmap,
                                                    int width_parent,
                                                    int height_parent)

      Scales the input image towards the dimensions of it's parent.
      Parameters:
        `edgeImageBitmap` - Bitmap that needs the be scaled.
        `width_parent` - Width of the parent container.
        `height_parent` - Height of the parent container.
      Returns:
        Scaled Bitmap that matches the parent in one dimension.

    - #### persistPaths

          private void persistPaths()

      Persist the currently drawn paths, held by the DrawingView, to the local database.

    - #### onClickNext

          public void onClickNext()

    - #### updateGameActivity

          protected void updateGameActivity()

      This method is called after the edge image calculation is finished. It's purpose is to update the score image information and put post-game options/information to the screen.

    - #### setUpLayoutListenerForDrawingViewRescaling

          private void setUpLayoutListenerForDrawingViewRescaling()

      This method makes sure, that the DrawingView is rescaled AFTER the layout is fully inflated and set up. This way the getWidth() and getHeight() methods called on it's parent return values bigger than zero.

    - #### onBackPressed

          public void onBackPressed()

      When the back button is pressed during a drawing session prompt the user if he wants to end the session (and discard it) or if he wants to continue.

    - #### setControlButtonsEnabled

          private void setControlButtonsEnabled(boolean enabled)

      Enables/Disabled control buttons when edge image calculation is running.
      Parameters:
        `enabled` - True if buttons should be enabled, false to disable.

    - #### prepareToast

          private void prepareToast(java.lang.String message)

    - #### showProgressDialog

          private void showProgressDialog()

      Shows a progress dialog when edge image calculation Task is in process.