Advertisement
Guest User

Untitled

a guest
Mar 19th, 2016
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.86 KB | None | 0 0
  1. package be.kdg.kandoe.kandoe.fragment;
  2.  
  3. import android.graphics.Color;
  4. import android.graphics.drawable.BitmapDrawable;
  5. import android.graphics.drawable.ColorDrawable;
  6. import android.graphics.drawable.Drawable;
  7. import android.graphics.drawable.GradientDrawable;
  8. import android.graphics.drawable.LayerDrawable;
  9. import android.graphics.drawable.ShapeDrawable;
  10. import android.os.Bundle;
  11. import android.support.annotation.Nullable;
  12. import android.support.v4.app.Fragment;
  13. import android.support.v4.content.ContextCompat;
  14. import android.view.LayoutInflater;
  15. import android.view.View;
  16. import android.view.ViewGroup;
  17. import android.view.ViewTreeObserver;
  18. import android.widget.ImageButton;
  19. import android.widget.RelativeLayout;
  20. import android.widget.Toast;
  21.  
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Random;
  25.  
  26. import be.kdg.kandoe.kandoe.R;
  27. import be.kdg.kandoe.kandoe.activity.ThemeCardActivity;
  28. import be.kdg.kandoe.kandoe.dom.Card;
  29. import be.kdg.kandoe.kandoe.dom.Circle;
  30. import be.kdg.kandoe.kandoe.dom.Theme;
  31. import be.kdg.kandoe.kandoe.exception.ThemeException;
  32.  
  33.  
  34. public class GameFragment extends Fragment {
  35.     private Theme currentTheme;
  36.     private Circle currentCircle;
  37.     private int currentOrganisationId;
  38.     private List<Card> circleCards;
  39.     private List<ImageButton> circleButtons;
  40.     private int viewHeight;
  41.     private int viewWidth;
  42.     private int marginCard;
  43.     private int[] tableStarts;
  44.     private RelativeLayout background;
  45.     private ViewTreeObserver vto;
  46.     public GameFragment() {
  47.         circleCards = new ArrayList<>();
  48.         circleButtons = new ArrayList<>();
  49.         tableStarts = new int[9];
  50.     }
  51.  
  52.     public static synchronized GameFragment getSingletonObject() {
  53.         if (ref == null) {
  54.             ref = new GameFragment();
  55.         }
  56.         return ref;
  57.     }
  58.  
  59.     private static GameFragment ref;
  60.  
  61.     @Override
  62.     public void onCreate(@Nullable Bundle savedInstanceState) {
  63.         super.onCreate(savedInstanceState);
  64.     }
  65.  
  66.     @Nullable
  67.     @Override
  68.     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  69.         final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_game, container, false);
  70.  
  71.         ViewTreeObserver viewTreeObserver = rootView.getViewTreeObserver();
  72.         if (viewTreeObserver.isAlive()) {
  73.             viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  74.                 @Override
  75.                 public void onGlobalLayout() {
  76.                     rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  77.                     viewWidth = rootView.getWidth();
  78.                     viewHeight = rootView.getHeight();
  79.                     marginCard = (viewHeight / 9) / 2;
  80.                     for (int i = 0; i < 9; i++) {
  81.                         tableStarts[i] = viewHeight - marginCard - ((marginCard * i) * 2);
  82.                     }
  83.                     drawCards();
  84.                 }
  85.             });
  86.         }
  87.  
  88. //        vto = rootView.getViewTreeObserver();
  89. //        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  90. //            @Override
  91. //            public void onGlobalLayout() {
  92. //                vto.removeOnGlobalLayoutListener(this);
  93. //                viewWidth = rootView.getWidth();
  94. //                viewHeight = rootView.getHeight();
  95. //                marginCard = (viewHeight / 9) / 2;
  96. //                for (int i = 0; i < 9; i++) {
  97. //                    tableStarts[i] = marginCard + (marginCard * i) * 2;
  98. //                }
  99. //                drawCards();
  100. //            }
  101. //        });
  102.         try {
  103.             currentTheme = ThemeCardActivity.getCurrentTheme();
  104.             currentCircle = currentTheme.getCircle();
  105.         } catch (Exception e) {
  106.             throw new ThemeException("theme not found");
  107.         }
  108.         background = setBackground(rootView);
  109.         circleCards = currentCircle.getCards();
  110.  
  111.         return rootView;
  112.     }
  113.  
  114.     public void addCardToCircleSet(Card card) {
  115.         circleCards.add(card);
  116.         drawCards();
  117.     }
  118.  
  119.  
  120.  
  121.     public void removeCardFromCircleSet(Card card) {
  122.         circleCards.remove(card);
  123.         drawCards();
  124.     }
  125.  
  126.     private RelativeLayout setBackground(View view) {
  127.         background = (RelativeLayout) view.findViewById(R.id.game_background);
  128.         Drawable drawable = ContextCompat.getDrawable(view.getContext(), R.drawable.rsz_c8);
  129.         switch (currentCircle.getTotalRounds()) {
  130.             case 3:
  131.                 drawable = ContextCompat.getDrawable(view.getContext(), R.drawable.c3);
  132.                 break;
  133.             case 4:
  134.                 //drawable = ContextCompat.getDrawable(view.getContext(), R.drawable.c4); TODO c4.png missing..
  135.                 break;
  136.             case 5:
  137.                 drawable = ContextCompat.getDrawable(view.getContext(), R.drawable.c5);
  138.                 break;
  139.             case 6:
  140.                 drawable = ContextCompat.getDrawable(view.getContext(), R.drawable.c6);
  141.                 break;
  142.             case 7:
  143.                 drawable = ContextCompat.getDrawable(view.getContext(), R.drawable.c7);
  144.                 break;
  145.             case 8:
  146.                 drawable = ContextCompat.getDrawable(view.getContext(), R.drawable.rsz_c8);
  147.                 break;
  148.             case 9:
  149.                 drawable = ContextCompat.getDrawable(view.getContext(), R.drawable.c9);
  150.                 break;
  151.             default:
  152.                 throw new NullPointerException("number of rounds not found");
  153.         }
  154.         background.setBackground(drawable);
  155.         return background;
  156.     }
  157.  
  158.     private void drawCards() {
  159.         circleButtons = new ArrayList<>();
  160.         for (Card card : circleCards) {
  161.             ImageButton myImageButton = new ImageButton(this.getContext()); //generate ImageButton
  162.             myImageButton.setId(card.getCardId()); //Set Id of button
  163.             myImageButton.setBackgroundResource(R.drawable.circle_card_icon);
  164.             RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(150, 150);
  165.  
  166.             Random rnd = new Random();
  167.             int min = 25;
  168.             int max = viewWidth;
  169.             int range = max - min -25;
  170.             int rndLeft = rnd.nextInt(range) + min;
  171.  
  172.             Random rndCol = new Random();
  173.             int color = Color.argb(255, rndCol.nextInt(256), rndCol.nextInt(256), rndCol.nextInt(256));
  174.  
  175.             Drawable backgroundWhereShapeIsPlaced = myImageButton.getBackground();
  176.             if (backgroundWhereShapeIsPlaced instanceof ShapeDrawable) {
  177.                 ShapeDrawable shapeDrawable = (ShapeDrawable)backgroundWhereShapeIsPlaced;
  178.                 shapeDrawable.getPaint().setColor(color);
  179.             } else if (backgroundWhereShapeIsPlaced instanceof GradientDrawable) {
  180.                 GradientDrawable gradientDrawable = (GradientDrawable)backgroundWhereShapeIsPlaced;
  181.                 gradientDrawable.setColor(color);
  182.             } else if (backgroundWhereShapeIsPlaced instanceof ColorDrawable) {
  183.                 ColorDrawable colorDrawable = (ColorDrawable)backgroundWhereShapeIsPlaced;
  184.                 colorDrawable.setColor(color);
  185.             }
  186.  
  187.             params.leftMargin = rndLeft;
  188.             params.topMargin = tableStarts[0]; //TODO 0 -> card.getScore()
  189.             background.addView(myImageButton, params); //Add view
  190.             myImageButton.setOnClickListener(getButtonAndDoAction(myImageButton));
  191.             circleButtons.add(myImageButton);
  192.         }
  193.     }
  194.  
  195.     View.OnClickListener getButtonAndDoAction(final ImageButton button) {
  196.         return new View.OnClickListener() {
  197.             public void onClick(View v) {
  198.                 Toast.makeText(getActivity(), "button clicked" + button.getId(), Toast.LENGTH_SHORT).show();
  199.             }
  200.         };
  201.     }
  202. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement