Skip to content

Commit

Permalink
array negative indices, android touch improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
samtupy committed Sep 27, 2024
1 parent 7e82a16 commit 0c47947
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 14 deletions.
6 changes: 3 additions & 3 deletions ASAddon/include/scriptarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,14 @@ class CScriptArray
void Resize(asUINT numElements);

// Get a pointer to an element. Returns 0 if out of bounds
void *At(asUINT index);
const void *At(asUINT index) const;
void *At(asINT64 index);
const void *At(asINT64 index) const;

// Set value of an element.
// The value arg should be a pointer to the value that will be copied to the element.
// Remember, if the array holds handles the value parameter should be the
// address of the handle. The refCount of the object will also be incremented
void SetValue(asUINT index, void *value);
void SetValue(asINT64 index, void *value);

// Copy the contents of one array to another (only if the types are the same)
CScriptArray &operator=(const CScriptArray&);
Expand Down
19 changes: 10 additions & 9 deletions ASAddon/src/scriptarray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ static void RegisterScriptArray_Native(asIScriptEngine *engine)
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_RELEASE, "void f()", asMETHOD(CScriptArray,Release), asCALL_THISCALL); assert( r >= 0 );

// The index operator returns the template subtype
r = engine->RegisterObjectMethod("array<T>", "T &opIndex(uint index)", asMETHODPR(CScriptArray, At, (asUINT), void*), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("array<T>", "const T &opIndex(uint index) const", asMETHODPR(CScriptArray, At, (asUINT) const, const void*), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("array<T>", "T &opIndex(int64 index)", asMETHODPR(CScriptArray, At, (asINT64), void*), asCALL_THISCALL); assert( r >= 0 );
r = engine->RegisterObjectMethod("array<T>", "const T &opIndex(int64 index) const", asMETHODPR(CScriptArray, At, (asINT64) const, const void*), asCALL_THISCALL); assert( r >= 0 );

// The assignment operator
r = engine->RegisterObjectMethod("array<T>", "array<T> &opAssign(const array<T>&in)", asMETHOD(CScriptArray, operator=), asCALL_THISCALL); assert( r >= 0 );
Expand Down Expand Up @@ -558,7 +558,7 @@ CScriptArray::CScriptArray(asUINT length, void *defVal, asITypeInfo *ti)
SetValue(n, defVal);
}

void CScriptArray::SetValue(asUINT index, void *value)
void CScriptArray::SetValue(asINT64 index, void *value)
{
// At() will take care of the out-of-bounds checking, though
// if called from the application then nothing will be done
Expand Down Expand Up @@ -926,9 +926,10 @@ void CScriptArray::RemoveLast()
}

// Return a pointer to the array element. Returns 0 if the index is out of bounds
const void *CScriptArray::At(asUINT index) const
const void *CScriptArray::At(asINT64 index) const
{
if( buffer == 0 || index >= buffer->numElements )
if( index < 0) index = buffer->numElements + index;
if( buffer == 0 || index < 0 || index >= buffer->numElements )
{
// If this is called from a script we raise a script exception
asIScriptContext *ctx = asGetActiveContext();
Expand All @@ -942,7 +943,7 @@ const void *CScriptArray::At(asUINT index) const
else
return buffer->data + elementSize*index;
}
void *CScriptArray::At(asUINT index)
void *CScriptArray::At(asINT64 index)
{
return const_cast<void*>(const_cast<const CScriptArray *>(this)->At(index));
}
Expand Down Expand Up @@ -2046,7 +2047,7 @@ static void ScriptArrayFindByRef2_Generic(asIScriptGeneric *gen)

static void ScriptArrayAt_Generic(asIScriptGeneric *gen)
{
asUINT index = gen->GetArgDWord(0);
asINT64 index = gen->GetArgQWord(0);
CScriptArray *self = (CScriptArray*)gen->GetObject();

gen->SetReturnAddress(self->At(index));
Expand Down Expand Up @@ -2227,8 +2228,8 @@ static void RegisterScriptArray_Generic(asIScriptEngine *engine)
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_LIST_FACTORY, "array<T>@ f(int&in, int&in) {repeat T}", asFUNCTION(ScriptArrayListFactory_Generic), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_ADDREF, "void f()", asFUNCTION(ScriptArrayAddRef_Generic), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectBehaviour("array<T>", asBEHAVE_RELEASE, "void f()", asFUNCTION(ScriptArrayRelease_Generic), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("array<T>", "T &opIndex(uint index)", asFUNCTION(ScriptArrayAt_Generic), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("array<T>", "const T &opIndex(uint index) const", asFUNCTION(ScriptArrayAt_Generic), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("array<T>", "T &opIndex(int64 index)", asFUNCTION(ScriptArrayAt_Generic), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("array<T>", "const T &opIndex(int64 index) const", asFUNCTION(ScriptArrayAt_Generic), asCALL_GENERIC); assert( r >= 0 );
r = engine->RegisterObjectMethod("array<T>", "array<T> &opAssign(const array<T>&in)", asFUNCTION(ScriptArrayAssignment_Generic), asCALL_GENERIC); assert( r >= 0 );

r = engine->RegisterObjectMethod("array<T>", "void insert_at(uint index, const T&in value)", asFUNCTION(ScriptArrayInsertAt_Generic), asCALL_GENERIC); assert( r >= 0 );
Expand Down
21 changes: 20 additions & 1 deletion jni/src/main/java/org/libsdl/app/SDLSurface.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.accessibility.AccessibilityManager;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
Expand All @@ -30,7 +31,7 @@
Because of this, that's where we set up the SDL thread
*/
public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
View.OnApplyWindowInsetsListener, View.OnKeyListener, View.OnTouchListener, SensorEventListener {
View.OnApplyWindowInsetsListener, View.OnKeyListener, View.OnHoverListener, View.OnTouchListener, SensorEventListener {

// Sensors
protected SensorManager mSensorManager;
Expand All @@ -42,6 +43,9 @@ public class SDLSurface extends SurfaceView implements SurfaceHolder.Callback,
// Is SurfaceView ready for rendering
public boolean mIsSurfaceReady;

// Accessibility manager
protected AccessibilityManager mAccessibilityManager;

// Startup
public SDLSurface(Context context) {
super(context);
Expand All @@ -53,9 +57,11 @@ public SDLSurface(Context context) {
setOnApplyWindowInsetsListener(this);
setOnKeyListener(this);
setOnTouchListener(this);
setOnHoverListener(this);

mDisplay = ((WindowManager)context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
mAccessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);

setOnGenericMotionListener(SDLActivity.getMotionListener());

Expand Down Expand Up @@ -234,6 +240,14 @@ private float getNormalizedY(float y)

// Touch events
@Override
public boolean onHover(View v, MotionEvent event) {
if (mAccessibilityManager.isTouchExplorationEnabled()) {
return onTouch(v, event);
} else {
return super.onHoverEvent(event);
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
/* Ref: http://developer.android.com/training/gestures/multi.html */
int touchDevId = event.getDeviceId();
Expand All @@ -242,6 +256,11 @@ public boolean onTouch(View v, MotionEvent event) {
int pointerFingerId;
int i = -1;
float x,y,p;

// Convert hover events to touch events if we've received them. Should we only do this if accessibility manager?
if (action == MotionEvent.ACTION_HOVER_ENTER) action = MotionEvent.ACTION_DOWN;
else if (action == MotionEvent.ACTION_HOVER_MOVE) action = MotionEvent.ACTION_MOVE;
else if (action == MotionEvent.ACTION_HOVER_EXIT) action = MotionEvent.ACTION_UP;

// 12290 = Samsung DeX mode desktop mouse
// 12290 = 0x3002 = 0x2002 | 0x1002 = SOURCE_MOUSE | SOURCE_TOUCHSCREEN
Expand Down
2 changes: 1 addition & 1 deletion src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ CScriptArray* query_touch_device(uint64_t device_id) {
SDL_Finger** fingers = SDL_GetTouchFingers(device_id, &finger_count);
if (!fingers) return array;
array->Reserve(finger_count);
for (int i = 0; i < finger_count; i++) array->InsertLast(fingers + i);
for (int i = 0; i < finger_count; i++) array->InsertLast(*(fingers + i));
SDL_free(fingers);
return array;
}
Expand Down
1 change: 1 addition & 0 deletions test/interact/lowlevel_touch.nvgt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ tts_voice tts;
touch_finger[]@ fingers = query_touch_device();
if (fingers.length() != prev_finger_count) {
tts.speak(fingers.length() + " fingers", true);
if (fingers.length() > 0) tts.speak(round(fingers[-1].x, 2) + ", " + round(fingers[-1].y, 2));
prev_finger_count = fingers.length();
}
}
Expand Down

0 comments on commit 0c47947

Please sign in to comment.