Java Question Library development help needed

osasigbinedion

Member
Licensed User
Longtime User
I am having issues wrapping a map rendering code into a B4A library. I have looked all over the forum and I have made some progress but I am approaching a deadline and need to be pointed in the right directions.

I can raise events when the user touches the view however I can't get the view render/update itself. I read about multithreading and the renderer running in a seperate thread however I don't have much experience with this and it is a fairly complicated code I need some guidance with this.

*code snippet for my ViewWrapper
@ShortName("Viewport2DView")
@Events(values={"Touch(f1() as float, f2() As float, id() as Int,action as Int )"})
public class Viewport2DView extends ViewWrapper<osas.track24.intermaphics.Viewport2DView> {
private BA ba;
private String eventName;
private Handler mHandler = new Handler();
private GestureDetector detector;
public void Initialize(BA pBA, String EventName) {
eventName = EventName.toLowerCase(BA.cul);
ba = pBA;
innerInitialize(pBA, EventName.toLowerCase(BA.cul), false);
}

@Hide
public void innerInitialize(BA pBA, String pEventName, boolean pKeepOldObject) {
if (!pKeepOldObject) {
setObject(new osas.track24.intermaphics.Viewport2DView(pBA.context));
detector = new GestureDetector(getObject(), mHandler);
detector.setOnDoubleTapListener(getObject());
super.innerInitialize(pBA, pEventName, pKeepOldObject);

getObject().setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
int count = event.getPointerCount();
float[] x = new float [count];
float[] y = new float [count];
int[] id = new int[count];
for (int i = 0; i < count; ++i) {
x = event.getX(i);
y = event.getY(i);
id = event.getPointerId(i);
}

switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_POINTER_DOWN:
ba.raiseEvent(this, eventName + "_touch",x,y,id,MotionEvent.ACTION_POINTER_DOWN);
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
ba.raiseEvent(this, eventName + "_touch",x,y,id,MotionEvent.ACTION_POINTER_UP);
case MotionEvent.ACTION_MOVE:
ba.raiseEvent(this, eventName + "_touch",x,y,id,MotionEvent.ACTION_MOVE);
}

return true;
//
}

});
}


}

public void setController(Controller c) {
getObject().setController(c);
mHandler.postDelayed(updateTimeTask, getObject().getTick());
}




I have attached a demo of the application I am attempting to wrap.

I want to be able to have this library incorporated within my B4A application and have the view respond to touches (zoom in and out, maybe draw circles on the view)

I would appreciate any tip and help right now I have been stuck banging my head on this for a week.
 

osasigbinedion

Member
Licensed User
Longtime User
You do not need to do anything special for a view to render itself. You just need to add it to an activity or panel. The "controller" code looks problematic however this is probably not the cause for the drawing issue.

I added the view successfully, I have a hunch that the renderer runs in a seperate thread and has to be updated for the changes made to view.
how do I get that to happen.
@Override
public void onDrawFrame(GL10 gl) {
if (frameTime > 0) {
endTime = System.currentTimeMillis();
try {
long dt = endTime - startTime;
if (dt < frameTime)
Thread.sleep(frameTime - dt);
} catch (InterruptedException e) {}
startTime = System.currentTimeMillis();
}

controller.onUpdate();
viewport.update();
}
 

warwound

Expert
Licensed User
Longtime User
Is the problem that you are calling:

B4X:
super.innerInitialize(pBA, pEventName, pKeepOldObject);

And at that point pKeepOldObject is false, the object you just created is being discarded?

Look at this template that i use to create a ViewWrapper class:

B4X:
public class AndroidViewWrapper extends ViewWrapper<AndroidView> {
   /**
    * Comment your Initialize method here.
    */
   @Override
   public void Initialize(BA ba, String EventName) {
     super.Initialize(ba, EventName);
   }

   // you need an innerInitialize to create and set your AndroidView object
   @Hide
   public void innerInitialize(BA ba, String eventName, boolean keepOldObject) {
     if (!keepOldObject){
       setObject(new AndroidView(ba.context));
     }
     super.innerInitialize(ba, eventName, true);
   }
}

These are the lines to look at:

B4X:
     if (!keepOldObject){
       setObject(new AndroidView(ba.context));
     }
     super.innerInitialize(ba, eventName, true);

See how if keepOldObject is false then a new object is created and after that if code block is executed, super.innerInitialize(ba, eventName, true); is executed.
The newly created object is 'kept'.

Try this:

B4X:
  public void innerInitialize(BA pBA, String pEventName, boolean pKeepOldObject) {
  if (!pKeepOldObject) {
  setObject(new osas.track24.intermaphics.Viewport2DView(pBA.context));
  detector = new GestureDetector(getObject(), mHandler);
  detector.setOnDoubleTapListener(getObject());
  getObject().setOnTouchListener(new OnTouchListener() {
  public boolean onTouch(View v, MotionEvent event) {
  detector.onTouchEvent(event);
  int count = event.getPointerCount();
  float[] x = new float [count];
  float[] y = new float [count];
  int[] id = new int[count];
  for (int i = 0; i &lt; count; ++i) {
  x = event.getX(i);
  y = event.getY(i);
  id = event.getPointerId(i);
  }
  switch (event.getAction() & MotionEvent.ACTION_MASK) {
  case MotionEvent.ACTION_DOWN:
  case MotionEvent.ACTION_POINTER_DOWN:
  ba.raiseEvent(this, eventName + "_touch",x,y,id,MotionEvent.ACTION_POINTER_DOWN);
  case MotionEvent.ACTION_UP:
  case MotionEvent.ACTION_POINTER_UP:
  ba.raiseEvent(this, eventName + "_touch",x,y,id,MotionEvent.ACTION_POINTER_UP);
  case MotionEvent.ACTION_MOVE:
  ba.raiseEvent(this, eventName + "_touch",x,y,id,MotionEvent.ACTION_MOVE);
  }
  return true;
  //
  }
  });
     pKeepOldObject=true;   //   now when super.innerInitialize(pBA, pEventName, pKeepOldObject); is executed the new object will be kept
  }
   super.innerInitialize(pBA, pEventName, pKeepOldObject);
  }

Martin.
 

osasigbinedion

Member
Licensed User
Longtime User
Do you have any instructions on how to use this library (from a Java project)?

I was away yesterday and couldn't do this. The source code is proprietary the 3rd party company wouldn't want me posting it up on a forum. I have a dropbox link for the demo app. I am attempting to wrap that view and raise events from it so I can use it within my application I am writing with B4A.

https://www.dropbox.com/sh/ctcz5i5u8iafdwo/2YTfYiozuK
 
Top