Skip to content
Snippets Groups Projects
Commit 62d37197 authored by FurWaz's avatar FurWaz
Browse files

Comments, again

parent 5ded7768
No related branches found
No related tags found
No related merge requests found
...@@ -10,23 +10,21 @@ import android.widget.Toast; ...@@ -10,23 +10,21 @@ import android.widget.Toast;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder mHolder; private SurfaceHolder mHolder;
private Camera mCamera; private Camera mCamera;
// Constructor
public CameraPreview(Context context, Camera camera) { public CameraPreview(Context context, Camera camera) {
super(context); super(context);
mCamera = camera; mCamera = camera;
// Install a SurfaceHolder.Callback so we get notified when the
// underlying surface is created and destroyed.
mHolder = getHolder(); mHolder = getHolder();
mHolder.addCallback(this); mHolder.addCallback(this);
// deprecated setting, but required on Android versions prior to 3.0
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} }
// Get the optimal preview size (based of camera's supported preview sizes and the view's size)
private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) { private Camera.Size getOptimalPreviewSize(List<Camera.Size> sizes, int w, int h) {
final double ASPECT_TOLERANCE = 0.1; final double ASPECT_TOLERANCE = 0.1;
double targetRatio = (double)h / w; double targetRatio = (double)h / w;
...@@ -59,6 +57,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback ...@@ -59,6 +57,7 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
return optimalSize; return optimalSize;
} }
// Bind the camera preview to the SurfaceHolder
protected void setCameraPreview(SurfaceHolder holder) { protected void setCameraPreview(SurfaceHolder holder) {
Camera.Parameters params = mCamera.getParameters(); Camera.Parameters params = mCamera.getParameters();
params.set("orientation", "portrait"); params.set("orientation", "portrait");
...@@ -78,33 +77,24 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback ...@@ -78,33 +77,24 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
} }
} }
// Event holder for surface creation
public void surfaceCreated(SurfaceHolder holder) { public void surfaceCreated(SurfaceHolder holder) {
setCameraPreview(holder); setCameraPreview(holder);
} }
// Event holder for surface destruction
public void surfaceDestroyed(SurfaceHolder holder) { public void surfaceDestroyed(SurfaceHolder holder) {
// empty. Take care of releasing the Camera preview in your activity. mCamera.stopPreview();
mCamera.release();
} }
// Event holder for surface change
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// If your preview can change or rotate, take care of those events here. if (mHolder.getSurface() == null) return;
// Make sure to stop the preview before resizing or reformatting it.
if (mHolder.getSurface() == null){
// preview surface does not exist
return;
}
// stop preview before making changes
try { try {
mCamera.stopPreview(); mCamera.stopPreview();
} catch (Exception e){ } catch (Exception e) {}
// ignore: tried to stop a non-existent preview
}
// set preview size and make any resize, rotate or
// reformatting changes here
setCameraPreview(holder); setCameraPreview(holder);
} }
} }
...@@ -13,24 +13,29 @@ import Common.Coord; ...@@ -13,24 +13,29 @@ import Common.Coord;
public class ClickDetector extends View { public class ClickDetector extends View {
Callback cb = null; Callback cb = null;
// set the on touch callback function
protected void init() { protected void init() {
setOnTouchListener(this::touchListener); setOnTouchListener(this::touchListener);
} }
// constructor
public ClickDetector(Context context) { public ClickDetector(Context context) {
super(context); super(context);
this.init(); this.init();
} }
// constructor
public ClickDetector(Context context, AttributeSet attrs) { public ClickDetector(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
this.init(); this.init();
} }
// set the callback function
public void setOnClickCallback(Callback c) { public void setOnClickCallback(Callback c) {
this.cb = c; this.cb = c;
} }
// on touch, if the touch is valid (only one finger, and the action is up), call the callback function with the touch coordinates
protected boolean touchListener(View v, MotionEvent ev) { protected boolean touchListener(View v, MotionEvent ev) {
if (ev.getPointerCount() != 1) return true; if (ev.getPointerCount() != 1) return true;
if (ev.getAction() != MotionEvent.ACTION_DOWN) return true; if (ev.getAction() != MotionEvent.ACTION_DOWN) return true;
... ...
......
...@@ -30,20 +30,24 @@ public class DoorsSelector extends View { ...@@ -30,20 +30,24 @@ public class DoorsSelector extends View {
RectF editedRect = null; RectF editedRect = null;
List<RectF> btns = new ArrayList<>(); List<RectF> btns = new ArrayList<>();
// set the on touch callback function
protected void init() { protected void init() {
setOnTouchListener(this::touchListener); setOnTouchListener(this::touchListener);
} }
// constructor
public DoorsSelector(Context context) { public DoorsSelector(Context context) {
super(context); super(context);
this.init(); this.init();
} }
// constructor
public DoorsSelector(Context context, AttributeSet attrs) { public DoorsSelector(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
this.init(); this.init();
} }
// draw all the pathviews of the current photo (+ the current edited pathview)
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
...@@ -66,6 +70,7 @@ public class DoorsSelector extends View { ...@@ -66,6 +70,7 @@ public class DoorsSelector extends View {
); );
} }
// draw one pathview on the canvas
protected void drawDoorZone(Canvas c, RectF rect, boolean showButton, String name) { protected void drawDoorZone(Canvas c, RectF rect, boolean showButton, String name) {
Paint p = new Paint(); Paint p = new Paint();
int width = getWidth(); int width = getWidth();
...@@ -129,6 +134,7 @@ public class DoorsSelector extends View { ...@@ -129,6 +134,7 @@ public class DoorsSelector extends View {
} }
} }
// checks if the click at the given position is on a pathview, if so, show the edit popup (or remove the pathview if the click is on the remove button)
protected void propagateClick(Coord c) { protected void propagateClick(Coord c) {
List<PathView> pathviews = photo.getPathViews(); List<PathView> pathviews = photo.getPathViews();
for(int i = 0; i < pathviews.size(); i++) { for(int i = 0; i < pathviews.size(); i++) {
...@@ -148,6 +154,7 @@ public class DoorsSelector extends View { ...@@ -148,6 +154,7 @@ public class DoorsSelector extends View {
} }
} }
// touch listener (for all interactions: click, drag, etc.)
protected boolean touchListener(View v, MotionEvent ev) { protected boolean touchListener(View v, MotionEvent ev) {
if (ev.getPointerCount() != 1) return true; if (ev.getPointerCount() != 1) return true;
...@@ -185,14 +192,17 @@ public class DoorsSelector extends View { ...@@ -185,14 +192,17 @@ public class DoorsSelector extends View {
return true; return true;
} }
// set the doors selector's current room
public void setRoom(RoomInfo room) { public void setRoom(RoomInfo room) {
this.room = room; this.room = room;
} }
// set the doors selector's current photo
public void setPhoto(PhotoInfo photo) { public void setPhoto(PhotoInfo photo) {
this.photo = photo; this.photo = photo;
} }
// show the add popup for a new door
protected void showAddDoorPopup() { protected void showAddDoorPopup() {
RectF rect = new RectF(editedRect.left, editedRect.top, editedRect.right, editedRect.bottom); RectF rect = new RectF(editedRect.left, editedRect.top, editedRect.right, editedRect.bottom);
new PathviewPopup( new PathviewPopup(
...@@ -219,6 +229,7 @@ public class DoorsSelector extends View { ...@@ -219,6 +229,7 @@ public class DoorsSelector extends View {
).show(); ).show();
} }
// show the edit popup for a door
protected void showEditPathviewPopup(int index) { protected void showEditPathviewPopup(int index) {
PathView pv = photo.getPathView(index); PathView pv = photo.getPathView(index);
new PathviewPopup( new PathviewPopup(
... ...
......
...@@ -36,6 +36,7 @@ public class InfoAdapter<A> extends BaseAdapter { ...@@ -36,6 +36,7 @@ public class InfoAdapter<A> extends BaseAdapter {
private int tile = 0; private int tile = 0;
// constructor
public InfoAdapter(Context context, List<A> data, View noDataLayout, int tile) { public InfoAdapter(Context context, List<A> data, View noDataLayout, int tile) {
this.context = context; this.context = context;
this.data = data; this.data = data;
...@@ -44,6 +45,7 @@ public class InfoAdapter<A> extends BaseAdapter { ...@@ -44,6 +45,7 @@ public class InfoAdapter<A> extends BaseAdapter {
this.tile = tile; this.tile = tile;
} }
// sets the adapter data list (and updates it)
public void setData(List<A> data) { public void setData(List<A> data) {
this.data = data; this.data = data;
this.updateQueryList(); this.updateQueryList();
...@@ -64,6 +66,7 @@ public class InfoAdapter<A> extends BaseAdapter { ...@@ -64,6 +66,7 @@ public class InfoAdapter<A> extends BaseAdapter {
return i; return i;
} }
// update the displayed data list based on the search query
public void updateQueryList() { public void updateQueryList() {
this.queryList.clear(); this.queryList.clear();
for(A info: this.data) { for(A info: this.data) {
...@@ -80,6 +83,7 @@ public class InfoAdapter<A> extends BaseAdapter { ...@@ -80,6 +83,7 @@ public class InfoAdapter<A> extends BaseAdapter {
this.notifyDataSetChanged(); this.notifyDataSetChanged();
} }
// returns the view of the given item (with the correct data, based on the binding list)
@Override @Override
public View getView(int i, View view, ViewGroup viewGroup) { public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(context).inflate(this.tile, viewGroup, false); view = LayoutInflater.from(context).inflate(this.tile, viewGroup, false);
...@@ -100,11 +104,13 @@ public class InfoAdapter<A> extends BaseAdapter { ...@@ -100,11 +104,13 @@ public class InfoAdapter<A> extends BaseAdapter {
return view; return view;
} }
// setup the click listeners for the given view
private void setupOnClick(View view, int i) { private void setupOnClick(View view, int i) {
LinearLayout ll = view.findViewById(R.id.tile_box); LinearLayout ll = view.findViewById(R.id.tile_box);
Button b1 = view.findViewById(R.id.tile_remove); Button b1 = view.findViewById(R.id.tile_remove);
Button b2 = view.findViewById(R.id.tile_edit); Button b2 = view.findViewById(R.id.tile_edit);
// if there is no edit button, then the view button is the edit button
if (b2 == null) { if (b2 == null) {
ll.setOnClickListener(view1 -> { ll.setOnClickListener(view1 -> {
if (this.editCallback != null) if (this.editCallback != null)
...@@ -133,11 +139,13 @@ public class InfoAdapter<A> extends BaseAdapter { ...@@ -133,11 +139,13 @@ public class InfoAdapter<A> extends BaseAdapter {
}); });
} }
// set the bindings list (and update the query list)
public void setBindings(InfoBinding[] objs) { public void setBindings(InfoBinding[] objs) {
this.objs = objs; this.objs = objs;
this.updateQueryList(); this.updateQueryList();
} }
// add a binding to the bindings list (and update the query list)
public void addBinding(InfoBinding binding) { public void addBinding(InfoBinding binding) {
InfoBinding[] newObjs = new InfoBinding[this.objs.length + 1]; InfoBinding[] newObjs = new InfoBinding[this.objs.length + 1];
for (int i = 0; i < this.objs.length; i++) for (int i = 0; i < this.objs.length; i++)
...@@ -147,18 +155,22 @@ public class InfoAdapter<A> extends BaseAdapter { ...@@ -147,18 +155,22 @@ public class InfoAdapter<A> extends BaseAdapter {
this.updateQueryList(); this.updateQueryList();
} }
// set the edit callback
public void setOnEditListener(Callback cb) { public void setOnEditListener(Callback cb) {
this.editCallback = cb; this.editCallback = cb;
} }
// set the view callback
public void setOnViewListener(Callback cb) { public void setOnViewListener(Callback cb) {
this.viewCallback = cb; this.viewCallback = cb;
} }
// set the remove callback
public void setOnRemoveListener(Callback cb) { public void setOnRemoveListener(Callback cb) {
this.removeCallback = cb; this.removeCallback = cb;
} }
// set the search query (and update the query list)
public void setSearchQuery(String query) { public void setSearchQuery(String query) {
this.searchQuery = query; this.searchQuery = query;
this.updateQueryList(); this.updateQueryList();
... ...
......
...@@ -16,18 +16,21 @@ public class LevelCanvas extends View { ...@@ -16,18 +16,21 @@ public class LevelCanvas extends View {
Rect dims = new Rect(0, 0, 10, 10); Rect dims = new Rect(0, 0, 10, 10);
float r; float r;
// constructor
public LevelCanvas(Context context, AttributeSet attrs) { public LevelCanvas(Context context, AttributeSet attrs) {
super(context, attrs); super(context, attrs);
p = new Paint(Paint.ANTI_ALIAS_FLAG); p = new Paint(Paint.ANTI_ALIAS_FLAG);
p.setColor(Color.BLACK); p.setColor(Color.BLACK);
} }
// set the canvas dimensions
@Override @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh); super.onSizeChanged(w, h, oldw, oldh);
dims = new Rect(0, 0, w, h); dims = new Rect(0, 0, w, h);
} }
// draw the phone's level based on the given accelerometer values (using askForDraw)
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
...@@ -64,9 +67,10 @@ public class LevelCanvas extends View { ...@@ -64,9 +67,10 @@ public class LevelCanvas extends View {
canvas.drawLine(radius, radius, p_x, p_y, p); canvas.drawLine(radius, radius, p_x, p_y, p);
} }
// ask for a refresh of the level, with the given rotation (smooth it a little bit)
public void askForDraw(float rot) { public void askForDraw(float rot) {
float newRot = - rot; float newRot = - rot;
this.r += (newRot - this.r) / 4; // to smooth it a little bit this.r += (newRot - this.r) / 4; // to smooth it
this.invalidate(); this.invalidate();
} }
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment