Implemented jumping of the activated Label inside the screen, if it was outside

(because it could be far outside after screen orientation change),

Changed the minimum for decision, whether Label is outside, to minimum of defined minimum size and one letter size,
Added TextOverlay.Position class
pull/3/head
Olga Miller 2017-01-04 18:00:22 +01:00
rodzic fe487e3763
commit 7af631dae6
4 zmienionych plików z 107 dodań i 18 usunięć

Wyświetl plik

@ -92,6 +92,7 @@ public class LabelCollection {
mLabels.remove(mActiveLabel);
mPreviousX = x;
mPreviousY = y;
mActiveLabel.jumpInside(mTextSizeFactor, mScreenSize.width(), mScreenSize.height());
return true;
}

Wyświetl plik

@ -24,12 +24,12 @@ import java.io.IOException;
class LabelContainer {
private Label mLabel;
private LabelPainter mPainter;
private float mX, mY; // left-bottom corner
private Position mPosition; // left-bottom corner
LabelContainer(@NonNull Label label) {
mLabel = label;
mPainter = new LabelPainter(label);
mX = mY = 0f;
mPosition = new Position();
}
boolean contains(float x, float y) {
@ -48,13 +48,16 @@ class LabelContainer {
mPainter.draw(canvas, src, dst);
}
void jumpInside(float textSizeFactor, float screenW, float screenH) {
mPainter.moveLabelInside(textSizeFactor, screenW, screenH, mPosition);
}
void offset(float x, float y) {
mX += x;
mY += y;
mPosition.offset(x, y);
}
void update(float textSizeFactor, float screenW, float screenH) {
mPainter.update(textSizeFactor, screenW, screenH, mX, mY);
mPainter.update(textSizeFactor, screenW, screenH, mPosition);
}
Label getContent() {
@ -69,8 +72,8 @@ class LabelContainer {
void write(IWriter writer) throws IOException {
writer.beginRootObject();
{
writer.write("position_x", mX);
writer.write("position_y", mY);
writer.write("position_x", mPosition.getX());
writer.write("position_y", mPosition.getY());
writer.beginObject("label");
{
writeLabel(writer, mLabel);
@ -83,8 +86,7 @@ class LabelContainer {
void read(IReader reader) throws IOException {
reader.beginRootObject();
{
mX = reader.readFloat();
mY = reader.readFloat();
mPosition.set(reader.readFloat(), reader.readFloat());
reader.beginObject();
{
readLabel(reader, mLabel);

Wyświetl plik

@ -41,8 +41,7 @@ class LabelPainter {
private InDrawer(float sizeFactor, float x, float y) {
mSizeFactor = sizeFactor;
mX = x;
mY = y;
setPosition(x, y);
setPaintSettings(mSizeFactor);
}
@ -97,6 +96,17 @@ class LabelPainter {
return bounds;
}
private void setPosition(float x, float y) {
mX = x;
mY = y;
}
private float getOneLetterSize() {
Rect bounds = new Rect();
mPaint.getTextBounds("M", 0, 1, bounds);
return bounds.width();
}
private Rect getTextBounds() {
Rect bounds = new Rect();
String text = mLabel.getText();
@ -277,27 +287,59 @@ class LabelPainter {
mLabel = label;
}
void update(float sizeFactor, float screenW, float screenH, float x, float y) {
InDrawer inDrawer = new InDrawer(sizeFactor, x, y);
void moveLabelInside(float sizeFactor, float screenW, float screenH, Position position) {
if (isLabelInside())
return;
float x = position.getX();
float y = position.getY();
InDrawer inDrawer = new InDrawer(sizeFactor, x, y);
RectF rect = inDrawer.getBounds();
float minSize = 1.5f * sizeFactor;
float min = Math.min(getMinSize(sizeFactor), inDrawer.getOneLetterSize());
if (rect.right < min) // left out
x = min - rect.width();
else if (rect.bottom < min) // top out
y = min;
else if (rect.left > (screenW - min)) // right out
x = screenW - min;
else if (rect.top > (screenH - min)) // bottom out
y = screenH + rect.height() - min;
inDrawer.setPosition(x, y);
mDrawer = inDrawer;
position.set(x, y);
}
void update(float sizeFactor, float screenW, float screenH, Position position) {
InDrawer inDrawer = new InDrawer(sizeFactor, position.getX(), position.getY());
RectF rect = inDrawer.getBounds();
float minSize = getMinSize(sizeFactor);
float min = Math.min(minSize, inDrawer.getOneLetterSize());
OutDrawer outDrawer = null;
if (rect.right < minSize) { // left out
if (rect.right < min) { // left out
outDrawer = new OutDrawer(minSize);
outDrawer.leftOut(rect, screenH);
} else if (rect.bottom < minSize) {// top out
} else if (rect.bottom < min) {// top out
outDrawer = new OutDrawer(minSize);
outDrawer.topOut(rect, screenW);
} else if (rect.left > (screenW - minSize)) { // right out
} else if (rect.left > (screenW - min)) { // right out
outDrawer = new OutDrawer(minSize);
outDrawer.rightOut(rect, screenW, screenH);
} else if (rect.top > (screenH - minSize)) { // bottom out
} else if (rect.top > (screenH - min)) { // bottom out
outDrawer = new OutDrawer(minSize);
outDrawer.bottomOut(rect, screenW, screenH);
}
mDrawer = outDrawer == null ? inDrawer : outDrawer;
}
private boolean isLabelInside() {
return mDrawer instanceof InDrawer;
}
private float getMinSize(float sizeFactor) {
return 1.5f * sizeFactor;
}
}

Wyświetl plik

@ -0,0 +1,44 @@
/*
Copyright 2017 Olga Miller <olga.rgb@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package om.sstvencoder.TextOverlay;
class Position {
private float mX;
private float mY;
Position() {
mX = 0f;
mY = 0f;
}
void set(float x, float y) {
mX = x;
mY = y;
}
void offset(float x, float y) {
mX += x;
mY += y;
}
float getX() {
return mX;
}
float getY() {
return mY;
}
}