mirror of
https://github.com/rfc2822/GfxTablet
synced 2025-10-04 18:19:16 +02:00
Major update
* use AndroidStudio/gradle instead of Eclipse/ant * upgrade to SDK v22 and build tools v22 * manage network connection from CanvasActivity instead of CanvasView * show a "No host configured" message instead of opening Settings automatically when no host is configured * use Android 4.4 immersive full-screen (fixes #59) * use Material design (using support library) * add setting for dark canvas (closes #54, #73, closes #76)
This commit is contained in:
parent
ff865c297b
commit
29e84b8fd0
41 changed files with 675 additions and 462 deletions
|
@ -0,0 +1,98 @@
|
|||
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.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);
|
||||
|
||||
netClient = networkClient;
|
||||
|
||||
// process preferences
|
||||
settings = PreferenceManager.getDefaultSharedPreferences(context);
|
||||
acceptStylusOnly = settings.getBoolean(SettingsActivity.KEY_PREF_STYLUS_ONLY, false);
|
||||
setBackgroundColor(settings.getBoolean(SettingsActivity.KEY_DARK_CANVAS, false) ? Color.BLACK : Color.WHITE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int 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: %f|%f, pressure %f", 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);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue