mirror of
https://github.com/rfc2822/GfxTablet
synced 2025-10-03 17:49:17 +02:00
107 lines
3.5 KiB
Java
107 lines
3.5 KiB
Java
package at.bitfire.gfxtablet;
|
|
|
|
import android.annotation.SuppressLint;
|
|
import android.content.Context;
|
|
import android.content.SharedPreferences;
|
|
import android.graphics.Color;
|
|
import android.preference.PreferenceManager;
|
|
import android.util.AttributeSet;
|
|
import android.util.Log;
|
|
import android.view.MotionEvent;
|
|
import android.view.View;
|
|
|
|
import at.bitfire.gfxtablet.NetEvent.Type;
|
|
|
|
@SuppressLint("ViewConstructor")
|
|
public class CanvasView extends View {
|
|
private static final String TAG = "GfxTablet.CanvasView";
|
|
|
|
final SharedPreferences settings;
|
|
final NetworkClient netClient;
|
|
boolean acceptStylusOnly;
|
|
int maxX, maxY;
|
|
|
|
public CanvasView(Context context, NetworkClient networkClient) {
|
|
super(context);
|
|
this.netClient = networkClient;
|
|
|
|
// process preferences
|
|
settings = PreferenceManager.getDefaultSharedPreferences(context);
|
|
acceptStylusOnly = settings.getBoolean(SettingsActivity.KEY_PREF_STYLUS_ONLY, false);
|
|
}
|
|
|
|
@Override
|
|
protected void onAttachedToWindow() {
|
|
super.onAttachedToWindow();
|
|
if (settings.getBoolean(SettingsActivity.KEY_DARK_CANVAS, false))
|
|
setBackgroundColor(Color.BLACK);
|
|
else
|
|
setBackgroundResource(R.drawable.bg_grid_pattern);
|
|
}
|
|
|
|
@Override
|
|
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
|
Log.i(TAG, "Canvas size changed: " + w + "x" + h + " (before: " + oldw + "x" + oldh + ")");
|
|
maxX = w;
|
|
maxY = h;
|
|
}
|
|
|
|
@Override
|
|
public boolean onGenericMotionEvent(MotionEvent event) {
|
|
if (isEnabled()) {
|
|
for (int ptr = 0; ptr < event.getPointerCount(); ptr++)
|
|
if (!acceptStylusOnly || (event.getToolType(ptr) == MotionEvent.TOOL_TYPE_STYLUS)) {
|
|
Log.v(TAG, String.format("Generic motion event logged: %f|%f, pressure %f", event.getX(ptr), event.getY(ptr), event.getPressure(ptr)));
|
|
if (event.getActionMasked() == MotionEvent.ACTION_HOVER_MOVE)
|
|
netClient.getQueue().add(new NetEvent(Type.TYPE_MOTION,
|
|
normalizeX(event.getX(ptr)),
|
|
normalizeY(event.getY(ptr)),
|
|
normalizePressure(event.getPressure(ptr))
|
|
));
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public boolean onTouchEvent(MotionEvent event) {
|
|
if (isEnabled()) {
|
|
for (int ptr = 0; ptr < event.getPointerCount(); ptr++)
|
|
if (!acceptStylusOnly || (event.getToolType(ptr) == MotionEvent.TOOL_TYPE_STYLUS)) {
|
|
short nx = normalizeX(event.getX(ptr)),
|
|
ny = normalizeY(event.getY(ptr)),
|
|
npressure = normalizePressure(event.getPressure(ptr));
|
|
Log.v(TAG, String.format("Touch event logged: action %d @ %f|%f (pressure %f)", event.getActionMasked(), event.getX(ptr), event.getY(ptr), event.getPressure(ptr)));
|
|
switch (event.getActionMasked()) {
|
|
case MotionEvent.ACTION_MOVE:
|
|
netClient.getQueue().add(new NetEvent(Type.TYPE_MOTION, nx, ny, npressure));
|
|
break;
|
|
case MotionEvent.ACTION_DOWN:
|
|
netClient.getQueue().add(new NetEvent(Type.TYPE_BUTTON, nx, ny, npressure, 0, true));
|
|
break;
|
|
case MotionEvent.ACTION_UP:
|
|
case MotionEvent.ACTION_CANCEL:
|
|
netClient.getQueue().add(new NetEvent(Type.TYPE_BUTTON, nx, ny, npressure, 0, false));
|
|
break;
|
|
}
|
|
|
|
}
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
short normalizeX(float x) {
|
|
return (short)(Math.min(Math.max(0, x), maxX) * Short.MAX_VALUE/maxX);
|
|
}
|
|
|
|
short normalizeY(float x) {
|
|
return (short)(Math.min(Math.max(0, x), maxY) * Short.MAX_VALUE/maxY);
|
|
}
|
|
|
|
short normalizePressure(float x) {
|
|
return (short)(Math.min(Math.max(0, x), 2.0) * Short.MAX_VALUE/2.0);
|
|
}
|
|
}
|