import com.badlogic.gdx.Gdx; public class GdxLog { public static boolean DEBUG; @SuppressWarnings("all") public static void print(String tag, String message) { if (DEBUG) { Gdx.app.log(tag, message); } } @SuppressWarnings("all") public static void d(String tag, String message, Integer...values) { if (DEBUG) { Gdx.app.log(tag, String.format(message, values)); } } @SuppressWarnings("all") public static void f(String tag, String message, Float...values) { if (DEBUG) { Gdx.app.log(tag, String.format(message.replaceAll("%f", "%.0f"), values)); } } } //... GdxLog.d(TAG, "worldWidth: %d", worldWidth);
Gdx.app.postRunnable(new Runnable() { @Override public void run() { // } });
// null may be only String params public void postRunnable(final String name, final Object...params) { Gdx.app.postRunnable(new Runnable() { @Override public void run() { Method method = null; Class[] classes = new Class[params.length]; for (int i = 0; i < params.length; i++) { classes[i] = params[i] == null ? String.class : params[i].getClass(); } try { method = World.class.getMethod(name, classes); } catch (SecurityException e) { GdxLog.print(TAG, e.toString()); } catch (NoSuchMethodException e) { GdxLog.print(TAG, e.toString()); } if (method == null) { return; } try { method.invoke(WorldAdapter.this, params); } catch (IllegalArgumentException e) { GdxLog.print(TAG, e.toString()); } catch (IllegalAccessException e) { GdxLog.print(TAG, e.toString()); } catch (InvocationTargetException e) { GdxLog.print(TAG, e.toString()); } } }); }
public class ActivityMain extends AppCompatActivity implements AndroidFragmentApplication.Callbacks { protected FragmentWorld fragmentWorld; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // ... getSupportFragmentManager() .beginTransaction() .add(R.id.world, fragmentWorld, FragmentWorld.class.getSimpleName()) .commitAllowingStateLoss(); } @Override public void exit() {}
public class FragmentWorld extends AndroidFragmentApplication { public World world; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { int worldWidth = getResources().getDimensionPixelSize(R.dimen.world_width); int worldHeight = getResources().getDimensionPixelSize(R.dimen.world_height); world = new World(BuildConfig.DEBUG, worldWidth, worldHeight); return initializeForView(world); } }
final Pixmap pixmap = getScreenshot(); Observable.fromCallable(new Callable <Boolean> () { @Override public Boolean call() throws Exception { PixmapIO.PNG writer = new PixmapIO.PNG((int)(pixmap.getWidth() * pixmap.getHeight() * 1.5 f)); writer.setFlipY(false); ByteArrayOutputStream output = new ByteArrayOutputStream(); try { writer.write(output, pixmap); } finally { StreamUtils.closeQuietly(output); writer.dispose(); pixmap.dispose(); } byte[] bytes = output.toByteArray(); Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); return true; } }).subscribeOn(Schedulers.io()).subscribe();
protected Color parseColor(String hex) { String s1 = hex.substring(0, 2); int v1 = Integer.parseInt(s1, 16); float f1 = 1 f * v1 / 255 f; String s2 = hex.substring(2, 4); int v2 = Integer.parseInt(s2, 16); float f2 = 1 f * v2 / 255 f; String s3 = hex.substring(4, 6); int v3 = Integer.parseInt(s3, 16); float f3 = 1 f * v3 / 255 f; return new Color(f1, f2, f3, 1 f); }
Sticker sticker = (Sticker) stickersStage.hit(coordinates.x, coordinates.y, false);
@Override public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) { // initialPointer doesn't change // all vectors contains device coordinates Sticker sticker = getCurrentSticker(); if (sticker == null) { return false; } Vector2 startVector = new Vector2(initialPointer1).sub(initialPointer2); Vector2 currentVector = new Vector2(pointer1).sub(pointer2); sticker.setScale(sticker.startScale * currentVector.len() / startVector.len()); float startAngle = (float) Math.toDegrees(Math.atan2(startVector.x, startVector.y)); float endAngle = (float) Math.toDegrees(Math.atan2(currentVector.x, currentVector.y)); sticker.setRotation(sticker.startRotation + endAngle - startAngle); return false; }
@Override public boolean touchDown(float x, float y, int pointer, int button) { if (pointer == FIRST_FINGER) { Vector2 coordinates = stickersStage.screenToStageCoordinates(new Vector2(x, y)); Sticker sticker = (Sticker) stickersStage.hit(coordinates.x, coordinates.y, false); if (sticker != null) { // sticker.setPinchStarts(); currentSticker = sticker.index; } } return false; } @Override public void pinchStop() { Sticker sticker = getCurrentSticker(); if (sticker != null) { // sticker.setPinchStarts(); } }
spriteBatch.begin(); stickersStage.act(); stickersStage.getRoot().draw(spriteBatch, 1); spriteBatch.end();
gradientTopLeftColor = parseColor(topLeftColor); gradientBottomRightColor = parseColor(bottomRightColor); gradientBlendedColor = new Color(gradientTopLeftColor).add(gradientBottomRightColor);
@Override public boolean pan(float x, float y, float deltaX, float deltaY) { if (currentSticker != Sticker.INDEX_NONE) { Sticker sticker = getCurrentSticker(); if (sticker != null) { sticker.moveBy(deltaX * worldDensity, -deltaY * worldDensity); } } return false; }
@Override public void resize(int width, int height) { if (height > width) { worldDensity = 1f * worldWidth / width; } else { worldDensity = 1f * worldHeight / height; } viewport.update(width, height, true); }
public void onAppear() { ScaleToAction scaleToAction = scaleToPool.obtain(); scaleToAction.setPool(scaleToPool); scaleToAction.setScale(startScale); scaleToAction.setDuration(ANIMATION_TIME_APPEAR); addAction(scaleToAction); }
Source: https://habr.com/ru/post/338398/
All Articles