From f88581426bf62ae773cd5198f697bc27c54bade9 Mon Sep 17 00:00:00 2001 From: jiri hubacek Date: Sat, 27 Sep 2014 22:26:33 +0300 Subject: [PATCH] Make 'shape4a' compatible with 'shape'. --- .../contrib/shape_android/AndroidManifest.xml | 2 +- .../src/org/ocera/orte/shape4a/Box.java | 151 --------------- .../src/org/ocera/orte/shape4a/BoxType.java | 180 ++++++++++++++++++ .../ocera/orte/shape4a/PublisherShape.java | 14 +- .../ocera/orte/shape4a/SubscriberElement.java | 8 +- 5 files changed, 192 insertions(+), 163 deletions(-) delete mode 100644 orte/contrib/shape_android/src/org/ocera/orte/shape4a/Box.java create mode 100644 orte/contrib/shape_android/src/org/ocera/orte/shape4a/BoxType.java diff --git a/orte/contrib/shape_android/AndroidManifest.xml b/orte/contrib/shape_android/AndroidManifest.xml index e872068..6b0fcf3 100644 --- a/orte/contrib/shape_android/AndroidManifest.xml +++ b/orte/contrib/shape_android/AndroidManifest.xml @@ -2,7 +2,7 @@ + android:versionName="1.1" > . - * - */ -package org.ocera.orte.shape4a; - -import org.ocera.orte.*; -import org.ocera.orte.types.*; - -import android.util.Log; - -/** - * Object to be send throw ORTE. - * - * @author jiri hubacek - * @version %I%, %G% - * @since 1.0 - */ -public class Box extends MessageData -{ - private static final double DESTINATION_WIDTH = 389.0; - private static final double DESTINATION_HEIGHT = 256.0; - - public boolean allowScaling; - public int color; - // is more shape than strength, - // strength is solved throw - // publisherProperties or - // throw subscriberProperties... - public int strength; - public BoxRect rectangle; - - private double scaleWidth; - private double scaleHeight; - - /** - * Add topic of new object. - * - * @param appDomain - * @param newTopic - * @since 1.0 - */ - public Box(DomainApp appDomain, String newTopic) { - super(); - this.setTopic(newTopic); - - if (!appDomain.regNewDataType("Box", this.getMaxDataLength())) { - Log.e("Box", "Cannot register data type 'Box'."); - } - - this.rectangle = new BoxRect(); - this.allowScaling = true; - this.scaleWidth = 1; - this.scaleHeight = 1; - } - - /** - * Read data from buffer. - * @since 1.0 - */ - @Override - public void read() { - buffer.rewind(); - if (this.allowScaling) { - this.rectangle.top_left_x = (short) (buffer.getShort() / this.scaleWidth); - this.rectangle.top_left_y = (short) (buffer.getShort() / this.scaleHeight); - this.rectangle.bottom_right_x = (short) (buffer.getShort() / this.scaleWidth); - this.rectangle.bottom_right_y = (short) (buffer.getShort() / this.scaleHeight); - } else { - this.rectangle.top_left_x = buffer.getShort(); - this.rectangle.top_left_y = buffer.getShort(); - this.rectangle.bottom_right_x = buffer.getShort(); - this.rectangle.bottom_right_y = buffer.getShort(); - } - - this.strength = (int) buffer.getShort(); - } - - /** - * Write data to buffer. - * @since 1.0 - */ - @Override - public void write() { - buffer.rewind(); - if (this.allowScaling) { - buffer.putShort((short) (rectangle.top_left_x * this.scaleWidth)); - buffer.putShort((short) (rectangle.top_left_y * this.scaleHeight)); - buffer.putShort((short) (rectangle.bottom_right_x * this.scaleWidth)); - buffer.putShort((short) (rectangle.bottom_right_y * this.scaleHeight)); - } else { - buffer.putShort(rectangle.top_left_x); - buffer.putShort(rectangle.top_left_y); - buffer.putShort(rectangle.bottom_right_x); - buffer.putShort(rectangle.bottom_right_y); - } - - buffer.putShort((short) this.strength); - } - - /** - * Get maximum length of data. - * @since 1.0 - */ - @Override - public int getMaxDataLength() { - return 5 * ORTEConstant.SHORT_FIELD_SIZE; - } - - /** - * Object to be truly send throw ORTE. - * - * @since 1.0 - */ - public class BoxRect - { - public short top_left_x; - public short top_left_y; - public short bottom_right_x; - public short bottom_right_y; - - public BoxRect() - {} - } - - /** - * When screen rotates, change scale variables to - * fit the destination screen. - * - * @since 1.0 - */ - public void setScale(int currentWidth, int currentHeight) - { - this.scaleWidth = DESTINATION_WIDTH / currentWidth; - this.scaleHeight = DESTINATION_HEIGHT / currentHeight; - } -} diff --git a/orte/contrib/shape_android/src/org/ocera/orte/shape4a/BoxType.java b/orte/contrib/shape_android/src/org/ocera/orte/shape4a/BoxType.java new file mode 100644 index 0000000..c8482a5 --- /dev/null +++ b/orte/contrib/shape_android/src/org/ocera/orte/shape4a/BoxType.java @@ -0,0 +1,180 @@ +/** + * + * This file is part of shape4a. + * + * shape4a is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * shape4a is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with shape4a. If not, see . + * + */ +package org.ocera.orte.shape4a; + +import org.ocera.orte.DomainApp; +import org.ocera.orte.types.MessageData; +import org.ocera.orte.types.ORTEConstant; + +import android.util.Log; + +/** + * Class for object sent and received throw the ORTE middleware. + * + * @author jiri hubacek + * @version %I, %G + * @since 1.1 + */ +public class BoxType extends MessageData +{ + private static final byte FUZZY = 0; + private static final double DESTINATION_WIDTH = 389.0; + private static final double DESTINATION_HEIGHT = 331.0; + + public byte color; + public byte shape; + public BoxRect rectangle = new BoxRect(); + + public boolean allowScaling; + private double scaleWidth; + private double scaleHeight; + + /** + * Register new domain type, set default params. + * + * @since 1.1 + */ + public BoxType(DomainApp appDomain, String newTopic) + { + super(); + this.setTopic(newTopic); + + if (!appDomain.regNewDataType("BoxType", this.getMaxDataLength())) { + Log.e("BoxType", "Cannot register data type 'BoxType'."); + } + + this.allowScaling = true; + } + + /* (non-Javadoc) + * @see org.ocera.orte.types.MessageData#read() + */ + @Override + public void read() + { + buffer.rewind(); + + // get color + this.color = buffer.get(); + + // skip fuzzy bytes + buffer.get();buffer.get();buffer.get(); + + // get shape + this.shape = buffer.get(); + + // skip fuzzy bytes + buffer.get();buffer.get();buffer.get(); + + // get rect position (with scaling) + if (this.allowScaling) { + this.rectangle.top_left_x = (short) (buffer.getShort() / this.scaleWidth); + this.rectangle.top_left_y = (short) (buffer.getShort() / this.scaleHeight); + this.rectangle.bottom_right_x = (short) (buffer.getShort() / this.scaleWidth); + this.rectangle.bottom_right_y = (short) (buffer.getShort() / this.scaleHeight); + } else { + this.rectangle.top_left_x = buffer.getShort(); + this.rectangle.top_left_y = buffer.getShort(); + this.rectangle.bottom_right_x = buffer.getShort(); + this.rectangle.bottom_right_y = buffer.getShort(); + } + + // don't care about last fuzzy byte because of buffer.rewind() + + //Log.d("BoxType", "receiving - color: "+this.color+", shape: "+this.shape+ ", rectangle:{"+this.rectangle.top_left_x+","+this.rectangle.top_left_y+","+this.rectangle.bottom_right_x+","+this.rectangle.bottom_right_y+",}"); + } + + /* (non-Javadoc) + * @see org.ocera.orte.types.MessageData#write() + */ + @Override + public void write() + { + buffer.rewind(); + + // put color + buffer.put(this.color); + + // put fuzzy bytes + buffer.put(BoxType.FUZZY);buffer.put(BoxType.FUZZY);buffer.put(BoxType.FUZZY); + + // put shape + buffer.put(this.shape); + + // put fuzzy bytes + buffer.put(BoxType.FUZZY);buffer.put(BoxType.FUZZY);buffer.put(BoxType.FUZZY); + + // put rectange position (with scaling) + if (this.allowScaling) { + buffer.putShort((short) (this.rectangle.top_left_x * this.scaleWidth)); + buffer.putShort((short) (this.rectangle.top_left_y * this.scaleHeight)); + buffer.putShort((short) (this.rectangle.bottom_right_x * this.scaleWidth)); + buffer.putShort((short) (this.rectangle.bottom_right_y * this.scaleHeight)); + } else { + buffer.putShort(this.rectangle.top_left_x); + buffer.putShort(this.rectangle.top_left_y); + buffer.putShort(this.rectangle.bottom_right_x); + buffer.putShort(this.rectangle.bottom_right_y); + } + + // put fuzzy byte + buffer.put(BoxType.FUZZY); + } + + /* (non-Javadoc) + * @see org.ocera.orte.types.MessageData#getMaxDataLength() + */ + @Override + public int getMaxDataLength() + { + return ORTEConstant.BYTE_FIELD_SIZE + ORTEConstant.LONG_FIELD_SIZE + 4*ORTEConstant.SHORT_FIELD_SIZE; + } + + /** + * When screen rotates, change scale variables to + * fit the destination screen. + * + * Method of former Box class. + * + * @since 1.0 + */ + public void setScale(int currentWidth, int currentHeight) + { + this.scaleWidth = DESTINATION_WIDTH / currentWidth; + this.scaleHeight = DESTINATION_HEIGHT / currentHeight; + } + + /** + * Object parameters to be sent throw ORTE. + * + * Class from former Box class. + * + * @since 1.0 + */ + public class BoxRect + { + public short top_left_x; + public short top_left_y; + public short bottom_right_x; + public short bottom_right_y; + + public BoxRect() + {} + } +} diff --git a/orte/contrib/shape_android/src/org/ocera/orte/shape4a/PublisherShape.java b/orte/contrib/shape_android/src/org/ocera/orte/shape4a/PublisherShape.java index 2f90bf3..d1dde68 100644 --- a/orte/contrib/shape_android/src/org/ocera/orte/shape4a/PublisherShape.java +++ b/orte/contrib/shape_android/src/org/ocera/orte/shape4a/PublisherShape.java @@ -54,7 +54,7 @@ public class PublisherShape extends ShapeDrawable private NtpTime persistence; public Publication publication; - public Box box; + public BoxType box; /** * Set new {@link Shape}, strength and color. @@ -84,15 +84,15 @@ public class PublisherShape extends ShapeDrawable this.manual = false; this.persistence = new NtpTime(5); - this.box = new Box(appDomain, PublisherShape.getColorName(c)); + this.box = new BoxType(appDomain, PublisherShape.getColorName(c)); this.publisherProperties = new PublProp(this.box.getTopic(), - "Box", + "BoxType", this.persistence, s+1); this.publication = appDomain.createPublication(this.publisherProperties, this.box); - this.box.strength = s; - this.box.color = c; + this.box.shape = (byte) s; + this.box.color = (byte) c; this.getPaint().setColor(PublisherShape.getColorConstant(c)); this.setPadding(0, 0, 0, 0); @@ -107,7 +107,7 @@ public class PublisherShape extends ShapeDrawable * @return Object to send. * @since 1.0 */ - public Box toSend() + public BoxType toSend() { this.box.rectangle.top_left_x = (short) this.getBounds().left; this.box.rectangle.top_left_y = (short) this.getBounds().top; @@ -202,7 +202,7 @@ public class PublisherShape extends ShapeDrawable */ public String getShapeName() { - switch (this.box.strength) { + switch (this.box.shape) { case 0: return "Square"; case 1: diff --git a/orte/contrib/shape_android/src/org/ocera/orte/shape4a/SubscriberElement.java b/orte/contrib/shape_android/src/org/ocera/orte/shape4a/SubscriberElement.java index 6a635f5..8dcaec1 100644 --- a/orte/contrib/shape_android/src/org/ocera/orte/shape4a/SubscriberElement.java +++ b/orte/contrib/shape_android/src/org/ocera/orte/shape4a/SubscriberElement.java @@ -48,7 +48,7 @@ public class SubscriberElement extends SubscriptionCallback private NtpTime deadline; private NtpTime minSeparation; - private Box box; + private BoxType box; private ShapeDrawable shape; private View parentView; @@ -67,9 +67,9 @@ public class SubscriberElement extends SubscriptionCallback this.deadline = new NtpTime(6); this.minSeparation = new NtpTime(0); - this.box = new Box(appDomain, PublisherShape.getColorName(color)); + this.box = new BoxType(appDomain, PublisherShape.getColorName(color)); SubsProp subscriberProperties = new SubsProp(this.box.getTopic(), - "Box", + "BoxType", this.minSeparation, this.deadline, ORTEConstant.IMMEDIATE, @@ -136,7 +136,7 @@ public class SubscriberElement extends SubscriptionCallback */ public void setShape() { - switch (this.box.strength) { + switch (this.box.shape) { case 0: this.shape.setShape(new RectShape()); break; -- 2.39.2