SSTVEncoder2/app/src/main/java/om/sstvencoder/EditTextActivity.java

264 lines
9 KiB
Java
Raw Normal View History

2017-01-03 18:32:45 +01:00
/*
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;
import android.content.Intent;
import android.support.v4.app.DialogFragment;
2017-01-03 18:32:45 +01:00
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
2017-01-03 18:32:45 +01:00
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import java.util.List;
2017-01-03 18:32:45 +01:00
import om.sstvencoder.TextOverlay.Label;
public class EditTextActivity extends AppCompatActivity
implements AdapterView.OnItemSelectedListener, ColorFragment.OnColorSelectedListener {
private enum EditColorMode {
None,
Text,
Outline
}
2017-01-03 18:32:45 +01:00
public static final int REQUEST_CODE = 101;
public static final String EXTRA = "EDIT_TEXT_EXTRA";
private Label mLabel;
private EditColorMode mEditColor;
private FontFamilySet mFontFamilySet;
private FontFamilySet.FontFamily mSelectedFontFamily;
private List<String> mFontFamilyNameList;
private CheckBox mEditItalic, mEditBold, mEditOutline;
2017-01-03 18:32:45 +01:00
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_text);
mEditColor = EditColorMode.None;
mEditBold = (CheckBox) findViewById(R.id.edit_bold);
mEditItalic = (CheckBox) findViewById(R.id.edit_italic);
mEditOutline = (CheckBox) findViewById(R.id.edit_outline);
2017-01-03 18:32:45 +01:00
}
@Override
protected void onStart() {
super.onStart();
mLabel = ((Label) getIntent().getSerializableExtra(EXTRA)).getClone();
initText();
initTextSizeSpinner(mLabel.getTextSize());
mEditBold.setChecked(mLabel.getBold());
mEditItalic.setChecked(mLabel.getItalic());
initFontFamilySpinner(mLabel.getFamilyName());
updateBoldAndItalic();
mEditOutline.setChecked(mLabel.getOutline());
initOutlineSizeSpinner(mLabel.getOutlineSize());
findViewById(R.id.edit_color).setBackgroundColor(mLabel.getForeColor());
findViewById(R.id.edit_outline_color).setBackgroundColor(mLabel.getOutlineColor());
}
private void initText() {
EditText editText = (EditText) findViewById(R.id.edit_text);
editText.setText(mLabel.getText());
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
mLabel.setText(charSequence.toString());
}
@Override
public void afterTextChanged(Editable editable) {
}
});
}
private void initFontFamilySpinner(String familyName) {
Spinner spinner = (Spinner) findViewById(R.id.edit_font_family);
spinner.setOnItemSelectedListener(this);
mFontFamilySet = new FontFamilySet();
mSelectedFontFamily = mFontFamilySet.getFontFamily(familyName);
mFontFamilyNameList = mFontFamilySet.getFontFamilyDisplayNameList();
spinner.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, mFontFamilyNameList));
spinner.setSelection(mFontFamilyNameList.indexOf(mSelectedFontFamily.displayName));
2017-01-03 18:32:45 +01:00
}
private void initTextSizeSpinner(float textSize) {
Spinner spinner = (Spinner) findViewById(R.id.edit_text_size);
spinner.setOnItemSelectedListener(this);
String[] sizeList = new String[]{"Small", "Normal", "Large", "Huge"};
spinner.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, sizeList));
spinner.setSelection(textSizeToPosition(textSize));
2017-01-03 18:32:45 +01:00
}
private void initOutlineSizeSpinner(float outlineSize) {
Spinner spinner = (Spinner) findViewById(R.id.edit_outline_size);
spinner.setOnItemSelectedListener(this);
String[] sizeList = new String[]{"Thin", "Normal", "Thick"};
spinner.setAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, sizeList));
spinner.setSelection(outlineSizeToPosition(outlineSize));
2017-03-04 15:47:30 +01:00
}
2017-01-03 18:32:45 +01:00
private int textSizeToPosition(float textSize) {
int position = (int) (textSize - 1f);
if (0 <= position && position <= 3)
return position;
mLabel.setTextSize(Label.TEXT_SIZE_NORMAL);
2017-01-03 18:32:45 +01:00
return 1;
}
private float positionToTextSize(int position) {
return position + 1f;
}
private int outlineSizeToPosition(float outlineSize) {
int position = (int) (outlineSize * 2f / Label.OUTLINE_SIZE_NORMAL - 1f);
2017-03-04 15:47:30 +01:00
if (0 <= position && position <= 2)
return position;
mLabel.setOutlineSize(Label.OUTLINE_SIZE_NORMAL);
2017-03-04 15:47:30 +01:00
return 1;
}
private float positionToOutlineSize(int position) {
return Label.OUTLINE_SIZE_NORMAL * 0.5f * (position + 1f);
2017-03-04 15:47:30 +01:00
}
2017-01-03 18:32:45 +01:00
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (parent.getId()) {
case R.id.edit_text_size:
mLabel.setTextSize(positionToTextSize(position));
break;
case R.id.edit_outline_size:
mLabel.setOutlineSize(positionToOutlineSize(position));
2017-03-04 15:47:30 +01:00
break;
case R.id.edit_font_family:
String displayName = mFontFamilyNameList.get(position);
mSelectedFontFamily = mFontFamilySet.getFontFamilyFromDisplayName(displayName);
mLabel.setFamilyName(mSelectedFontFamily.name);
updateBoldAndItalic();
break;
default:
break;
}
}
private void updateBoldAndItalic() {
mEditBold.setEnabled(mSelectedFontFamily.bold);
if (!mEditBold.isEnabled()) {
mEditBold.setChecked(false);
mLabel.setBold(false);
}
mEditItalic.setEnabled(mSelectedFontFamily.italic);
if (!mEditItalic.isEnabled()) {
mEditItalic.setChecked(false);
mLabel.setItalic(false);
}
2017-01-03 18:32:45 +01:00
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_edit_text, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_done:
done();
return true;
}
return super.onOptionsItemSelected(item);
}
public void onBoldClick(View view) {
mLabel.setBold(mEditBold.isChecked());
}
public void onItalicClick(View view) {
mLabel.setItalic(mEditItalic.isChecked());
}
public void onOutlineClick(View view) {
mLabel.setOutline(mEditOutline.isChecked());
}
public void onColorClick(View view) {
showColorDialog(R.string.color, mLabel.getForeColor());
mEditColor = EditColorMode.Text;
}
public void onOutlineColorClick(View view) {
showColorDialog(R.string.outline_color, mLabel.getOutlineColor());
mEditColor = EditColorMode.Outline;
}
private void showColorDialog(int title, int color) {
ColorFragment fragment = new ColorFragment();
fragment.setTitle(title);
fragment.setColor(color);
fragment.addOnColorSelectedListener(this);
fragment.show(getSupportFragmentManager(), ColorFragment.class.getName());
}
@Override
public void onColorSelected(DialogFragment fragment, int color) {
switch (mEditColor) {
case Text:
mLabel.setForeColor(color);
findViewById(R.id.edit_color).setBackgroundColor(color);
break;
case Outline:
mLabel.setOutlineColor(color);
findViewById(R.id.edit_outline_color).setBackgroundColor(color);
break;
}
mEditColor = EditColorMode.None;
}
@Override
public void onCancel(DialogFragment fragment) {
mEditColor = EditColorMode.None;
}
2017-01-03 18:32:45 +01:00
private void done() {
Intent intent = new Intent();
intent.putExtra(EXTRA, mLabel);
2017-01-03 18:32:45 +01:00
setResult(RESULT_OK, intent);
finish();
}
}