FillGridView displays Grid and Background grid
parent
b754c535f4
commit
fd02dcffd1
|
|
@ -0,0 +1,15 @@
|
|||
package org.agp8x.android.games.fillgrid;
|
||||
|
||||
import android.graphics.Paint;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.agp8x.android.games.fillgrid.data.Cell;
|
||||
|
||||
/**
|
||||
* Created by clemensk on 09.04.17.
|
||||
*/
|
||||
|
||||
public interface CellPaintProvider {
|
||||
@NonNull
|
||||
Paint getPaint(Cell cell);
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
package org.agp8x.android.games.fillgrid;
|
||||
|
||||
import android.graphics.Paint;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.agp8x.android.games.fillgrid.data.Cell;
|
||||
|
||||
/**
|
||||
* Created by clemensk on 09.04.17.
|
||||
*/
|
||||
|
||||
public class CellPaintProviderBackground implements CellPaintProvider {
|
||||
@NonNull
|
||||
@Override
|
||||
public Paint getPaint(Cell cell) {
|
||||
Paint paint = new Paint();
|
||||
paint.setAlpha(128);
|
||||
return paint;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,39 @@
|
|||
package org.agp8x.android.games.fillgrid;
|
||||
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.support.annotation.NonNull;
|
||||
|
||||
import org.agp8x.android.games.fillgrid.data.Cell;
|
||||
import org.agp8x.android.games.fillgrid.data.GroupCell;
|
||||
|
||||
/**
|
||||
* Created by clemensk on 09.04.17.
|
||||
*/
|
||||
|
||||
public class CellPaintProviderDefault implements CellPaintProvider {
|
||||
|
||||
@NonNull
|
||||
@Override
|
||||
public Paint getPaint(Cell cell) {
|
||||
Paint paint = new Paint();
|
||||
if (cell instanceof GroupCell) {
|
||||
GroupCell groupCell = (GroupCell) cell;
|
||||
switch (groupCell.getGroup()) {
|
||||
case 0:
|
||||
paint.setColor(Color.RED);
|
||||
break;
|
||||
case 1:
|
||||
paint.setColor(Color.GREEN);
|
||||
break;
|
||||
case 2:
|
||||
paint.setColor(Color.BLUE);
|
||||
break;
|
||||
default:
|
||||
paint.setColor(Color.MAGENTA);
|
||||
}
|
||||
paint.setAlpha(192);
|
||||
}
|
||||
return paint;
|
||||
}
|
||||
}
|
||||
|
|
@ -2,6 +2,7 @@ package org.agp8x.android.games.fillgrid;
|
|||
|
||||
import android.support.v7.app.AppCompatActivity;
|
||||
import android.os.Bundle;
|
||||
import android.widget.RelativeLayout;
|
||||
|
||||
import org.agp8x.android.games.fillgrid.data.GridBlock;
|
||||
import org.agp8x.android.games.fillgrid.data.GridObject;
|
||||
|
|
@ -15,6 +16,10 @@ public class FillGrid extends AppCompatActivity {
|
|||
|
||||
//GridObject<String> sgo = new GridBlock<>("asdf");
|
||||
//GridObject<Asdf> ago = new GridBlock<>(Asdf.A);
|
||||
RelativeLayout layout = (RelativeLayout) findViewById(R.id.mainLayout);
|
||||
|
||||
FillGridView gridview = new FillGridView(this);
|
||||
layout.addView(gridview);
|
||||
}
|
||||
private enum Asdf{
|
||||
A,B,C;
|
||||
|
|
|
|||
|
|
@ -0,0 +1,122 @@
|
|||
package org.agp8x.android.games.fillgrid;
|
||||
|
||||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.AttributeSet;
|
||||
import android.util.Pair;
|
||||
import android.view.View;
|
||||
|
||||
import org.agp8x.android.games.fillgrid.data.GridBlock;
|
||||
import org.agp8x.android.games.fillgrid.data.GridBoard;
|
||||
import org.agp8x.android.games.fillgrid.data.GroupCell;
|
||||
import org.agp8x.android.games.fillgrid.data.Offset;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by clemensk on 07.04.17.
|
||||
*/
|
||||
|
||||
public class FillGridView extends View {
|
||||
protected Pair<Integer, Integer> contentSize = null;
|
||||
protected GridBoard<GridBlock<GroupCell>, GroupCell> board;
|
||||
protected List<GridBlock<GroupCell>> blocks;
|
||||
protected Map<Offset, GroupCell> background;
|
||||
protected GroupGridPainter painter;
|
||||
protected GroupGridPainter bgPainter;
|
||||
|
||||
public FillGridView(Context context) {
|
||||
super(context);
|
||||
init(null, 0);
|
||||
}
|
||||
|
||||
public FillGridView(Context context, @Nullable AttributeSet attrs) {
|
||||
super(context, attrs);
|
||||
init(attrs, 0);
|
||||
}
|
||||
|
||||
public FillGridView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
||||
super(context, attrs, defStyleAttr);
|
||||
init(attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
public FillGridView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
|
||||
super(context, attrs, defStyleAttr, defStyleRes);
|
||||
init(attrs, defStyleAttr);
|
||||
}
|
||||
|
||||
protected void init(AttributeSet attrs, int defStyleAttr) {
|
||||
Offset dimensions = new Offset(10, 10);
|
||||
board = new GridBoard<>(dimensions);
|
||||
|
||||
blocks = new ArrayList<>();
|
||||
GridBlock<GroupCell> block0 = new GridBlock<>();
|
||||
block0.getObjects().put(new Offset(0, 0), new GroupCell(0));
|
||||
blocks.add(block0);
|
||||
block0 = new GridBlock<>();
|
||||
block0.getObjects().put(new Offset(0, 0), new GroupCell(1));
|
||||
block0.getObjects().put(new Offset(0, 1), new GroupCell(1));
|
||||
blocks.add(block0);
|
||||
block0 = new GridBlock<>();
|
||||
block0.getObjects().put(new Offset(0, 0), new GroupCell(2));
|
||||
block0.getObjects().put(new Offset(0, 1), new GroupCell(2));
|
||||
block0.getObjects().put(new Offset(0, 2), new GroupCell(2));
|
||||
blocks.add(block0);
|
||||
|
||||
background = new HashMap<>();
|
||||
GroupCell bgCell = new GroupCell(-1);
|
||||
for (int i = 0; i < dimensions.x; i++) {
|
||||
for (int j = 0; j < dimensions.y; j++) {
|
||||
background.put(new Offset(i, j), bgCell);
|
||||
}
|
||||
}
|
||||
|
||||
CellPaintProvider bgCpp = new CellPaintProviderBackground();
|
||||
bgPainter = new GroupGridPainter(dimensions,bgCpp);
|
||||
|
||||
CellPaintProvider cpp = new CellPaintProviderDefault();
|
||||
painter = new GroupGridPainter(dimensions, cpp);
|
||||
board.drop(new Offset(4, 4), makeSquare());
|
||||
board.drop(new Offset(4,4), makeAngle());
|
||||
}
|
||||
|
||||
private GridBlock<GroupCell> makeSquare() {
|
||||
GridBlock<GroupCell> grid = new GridBlock<>(new HashMap<>());
|
||||
grid.getObjects().put(new Offset(0, 0), new GroupCell(0));
|
||||
grid.getObjects().put(new Offset(0, 1), new GroupCell(0));
|
||||
grid.getObjects().put(new Offset(1, 0), new GroupCell(0));
|
||||
grid.getObjects().put(new Offset(1, 1), new GroupCell(0));
|
||||
return grid;
|
||||
}
|
||||
|
||||
private GridBlock<GroupCell> makeAngle() {
|
||||
GridBlock<GroupCell> grid = new GridBlock<>(new HashMap<>());
|
||||
grid.getObjects().put(new Offset(0, 2), new GroupCell(1));
|
||||
grid.getObjects().put(new Offset(1, 2), new GroupCell(1));
|
||||
grid.getObjects().put(new Offset(2, 2), new GroupCell(1));
|
||||
grid.getObjects().put(new Offset(2, 1), new GroupCell(1));
|
||||
grid.getObjects().put(new Offset(2, 0), new GroupCell(1));
|
||||
return grid;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
if (contentSize == null) {
|
||||
//TODO: maybe fix for square? optional?
|
||||
int paddingTop = getPaddingTop();
|
||||
int paddingBottom = getPaddingBottom();
|
||||
int paddingLeft = getPaddingLeft();
|
||||
int paddingRight = getPaddingRight();
|
||||
contentSize = new Pair<>(getHeight() - paddingTop - paddingBottom, getWidth() - paddingLeft - paddingRight);
|
||||
}
|
||||
|
||||
bgPainter.draw(canvas, background, contentSize);
|
||||
painter.draw(canvas, board.getGrid(), contentSize);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,18 @@
|
|||
package org.agp8x.android.games.fillgrid;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.agp8x.android.games.fillgrid.data.GroupCell;
|
||||
import org.agp8x.android.games.fillgrid.data.Offset;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by clemensk on 07.04.17.
|
||||
*/
|
||||
|
||||
public interface GridPainter {
|
||||
|
||||
void draw(Canvas canvas, Map<Offset, GroupCell> cells, Pair<Integer,Integer> realDimensions);
|
||||
}
|
||||
|
|
@ -0,0 +1,37 @@
|
|||
package org.agp8x.android.games.fillgrid;
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.util.Pair;
|
||||
|
||||
import org.agp8x.android.games.fillgrid.data.GroupCell;
|
||||
import org.agp8x.android.games.fillgrid.data.Offset;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Created by clemensk on 07.04.17.
|
||||
*/
|
||||
|
||||
public class GroupGridPainter implements GridPainter {
|
||||
private Offset dimensions;
|
||||
private CellPaintProvider styleInfo;
|
||||
private int margin;
|
||||
private int radius;
|
||||
|
||||
public GroupGridPainter(Offset dimensions, CellPaintProvider styleInfo) {
|
||||
this.dimensions = dimensions;
|
||||
this.styleInfo = styleInfo;
|
||||
radius = 30;
|
||||
margin = radius + radius / 4;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas, Map<Offset, GroupCell> cells, Pair<Integer, Integer> realDimensions) {
|
||||
cells.forEach((offset, groupCell) -> drawCell(canvas, offset, groupCell, realDimensions));
|
||||
}
|
||||
|
||||
private void drawCell(Canvas canvas, Offset offset, GroupCell cell, Pair<Integer, Integer> realDimensions) {
|
||||
Pair<Float, Float> origin = new Pair<>((offset.x / (float) dimensions.x) * realDimensions.first, (offset.y / (float) dimensions.y) * realDimensions.second);
|
||||
canvas.drawCircle(origin.second + margin, origin.first + margin, 30, styleInfo.getPaint(cell));
|
||||
}
|
||||
}
|
||||
|
|
@ -8,4 +8,5 @@ public interface Cell {
|
|||
String getTableSymbol();
|
||||
|
||||
void markCollapsed();
|
||||
boolean isCollapsed();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,6 +10,10 @@ import java.util.Map;
|
|||
public class GridBlock<C extends Cell> implements GridObject<C> {
|
||||
private final Map<Offset, C> content;
|
||||
|
||||
public GridBlock() {
|
||||
content = new HashMap<>();
|
||||
}
|
||||
|
||||
public GridBlock(Map<Offset, C> content) {
|
||||
this.content = new HashMap<>(content);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -17,8 +17,12 @@ public class GridBoard<G extends GridObject<C>, C extends Cell> implements Grid<
|
|||
private final Offset dimensions;
|
||||
|
||||
public GridBoard(int heigth, int width) {
|
||||
this(new Offset(width, heigth));
|
||||
}
|
||||
|
||||
public GridBoard(Offset dimensions) {
|
||||
this.contents = new HashMap<>();
|
||||
dimensions = new Offset(width, heigth);
|
||||
this.dimensions = dimensions;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -45,6 +49,10 @@ public class GridBoard<G extends GridObject<C>, C extends Cell> implements Grid<
|
|||
contents.clear();
|
||||
}
|
||||
|
||||
public void removeCollapsed() {
|
||||
contents.entrySet().stream().filter(offsetCEntry -> offsetCEntry.getValue().isCollapsed()).forEach(offsetCEntry -> contents.remove(offsetCEntry.getKey()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Find lines with all cells filled and mark them as collapsed
|
||||
*
|
||||
|
|
|
|||
|
|
@ -0,0 +1,39 @@
|
|||
package org.agp8x.android.games.fillgrid.data;
|
||||
|
||||
/**
|
||||
* Created by clemensk on 07.04.17.
|
||||
*/
|
||||
|
||||
public class GroupCell implements Cell {
|
||||
private int group;
|
||||
private boolean collapsed = false;
|
||||
private boolean collapsionMark = false;
|
||||
|
||||
public GroupCell(int group) {
|
||||
this.group = group;
|
||||
}
|
||||
|
||||
public int getGroup() {
|
||||
return group;
|
||||
}
|
||||
|
||||
public boolean isCollapsionMark() {
|
||||
return collapsionMark;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTableSymbol() {
|
||||
return "" + group;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void markCollapsed() {
|
||||
collapsionMark =true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCollapsed() {
|
||||
return collapsed;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -4,7 +4,7 @@ package org.agp8x.android.games.fillgrid.data;
|
|||
* Created by clemensk on 29.03.17.
|
||||
*/
|
||||
|
||||
class Offset {
|
||||
public class Offset {
|
||||
public final int x;
|
||||
public final int y;
|
||||
|
||||
|
|
|
|||
|
|
@ -35,4 +35,9 @@ public class StringCell implements Cell {
|
|||
public void markCollapsed() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCollapsed() {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,19 +1,12 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<android.support.constraint.ConstraintLayout
|
||||
<RelativeLayout
|
||||
xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context="org.agp8x.android.games.fillgrid.FillGrid">
|
||||
tools:context="org.agp8x.android.games.fillgrid.FillGrid"
|
||||
android:id="@+id/mainLayout">
|
||||
|
||||
<TextView
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintLeft_toLeftOf="parent"
|
||||
app:layout_constraintRight_toRightOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent"/>
|
||||
|
||||
</android.support.constraint.ConstraintLayout>
|
||||
</RelativeLayout>
|
||||
|
|
|
|||
Loading…
Reference in New Issue