Example #1
0
 private int getCurrentBrickFieldValue(Brick.BrickField brickField) {
   String stringValue =
       formulaBrick
           .getFormulaWithBrickField(brickField)
           .getDisplayString(seekbarView.getContext());
   int value = Double.valueOf(stringValue.replace(",", ".")).intValue();
   return value;
 }
 private void updateBrickViewAndFormula(FormulaBrick newBrick, Brick.BrickField brickField) {
   formulaBrick = newBrick;
   cloneFormulaBrick(newBrick);
   updateBrickView();
   currentBrickField = brickField;
   currentFormula = clonedFormulaBrick.getFormulaWithBrickField(brickField);
   setInputFormula(currentBrickField, SET_FORMULA_ON_CREATE_VIEW);
   fragmentView.getViewTreeObserver().addOnGlobalLayoutListener(this);
   updateButtonsOnKeyboardAndInvalidateOptionsMenu();
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(!ViewConfiguration.get(getActivity()).hasPermanentMenuKey());
    setUpActionBar();
    onFormulaChangedListener =
        (OnFormulaChangedListener)
            ((ScriptActivity) getActivity()).getFragment(ScriptActivity.FRAGMENT_SCRIPTS);

    formulaBrick = (FormulaBrick) getArguments().getSerializable(FORMULA_BRICK_BUNDLE_ARGUMENT);
    currentBrickField =
        Brick.BrickField.valueOf(getArguments().getString(BRICKFIELD_BUNDLE_ARGUMENT));
    cloneFormulaBrick(formulaBrick);
    currentFormula = clonedFormulaBrick.getFormulaWithBrickField(currentBrickField);
  }
  private void setInputFormula(Brick.BrickField brickField, int mode) {

    switch (mode) {
      case SET_FORMULA_ON_CREATE_VIEW:
        formulaEditorEditText.enterNewFormula(currentFormula.getInternFormulaState());
        currentFormula.highlightTextField(brickView);
        refreshFormulaPreviewString();
        break;

      case SET_FORMULA_ON_SWITCH_EDIT_TEXT:
        Formula newFormula = clonedFormulaBrick.getFormulaWithBrickField(brickField);
        if (currentFormula == newFormula && formulaEditorEditText.hasChanges()) {
          formulaEditorEditText.quickSelect();
          break;
        }
        if (formulaEditorEditText.hasChanges()) {
          confirmSwitchEditTextTimeStamp[0] = confirmSwitchEditTextTimeStamp[1];
          confirmSwitchEditTextTimeStamp[1] = System.currentTimeMillis();
          confirmSwitchEditTextCounter++;
          if (!saveFormulaIfPossible()) {
            return;
          }
        }
        MenuItem undo = currentMenu.findItem(R.id.menu_undo);
        if (undo != null) {
          undo.setIcon(R.drawable.icon_undo_disabled);
          undo.setEnabled(false);
        }

        MenuItem redo = currentMenu.findItem(R.id.menu_redo);
        redo.setIcon(R.drawable.icon_redo_disabled);
        redo.setEnabled(false);

        formulaEditorEditText.endEdit();
        currentBrickField = brickField;
        currentFormula = newFormula;
        formulaEditorEditText.enterNewFormula(newFormula.getInternFormulaState());
        refreshFormulaPreviewString();
        break;
      default:
        break;
    }
  }
Example #5
0
  public View getView(Context context) {
    seekbarView = View.inflate(context, R.layout.fragment_rgb_color_chooser, null);

    seekbarView.setFocusableInTouchMode(true);
    seekbarView.requestFocus();

    OnClickListener onClickListener =
        new OnClickListener() {
          @Override
          public void onClick(View view) {
            switch (view.getId()) {
              case R.id.rgb_red_value:
                FormulaEditorFragment.showFragment(view, formulaBrick, redField);
                break;
              case R.id.rgb_green_value:
                FormulaEditorFragment.showFragment(view, formulaBrick, greenField);
                break;
              case R.id.rgb_blue_value:
                FormulaEditorFragment.showFragment(view, formulaBrick, blueField);
                break;
            }
          }
        };

    formulaEditorEditTextRed = (TextView) seekbarView.findViewById(R.id.rgb_red_value);
    formulaEditorEditTextRed.setOnClickListener(onClickListener);
    formulaBrick.getFormulaWithBrickField(redField).setTextFieldId(R.id.rgb_red_value);
    formulaBrick.getFormulaWithBrickField(redField).refreshTextField(seekbarView);

    formulaEditorEditTextGreen = (TextView) seekbarView.findViewById(R.id.rgb_green_value);
    formulaEditorEditTextGreen.setOnClickListener(onClickListener);
    formulaBrick.getFormulaWithBrickField(greenField).setTextFieldId(R.id.rgb_green_value);
    formulaBrick.getFormulaWithBrickField(greenField).refreshTextField(seekbarView);

    formulaEditorEditTextBlue = (TextView) seekbarView.findViewById(R.id.rgb_blue_value);
    formulaEditorEditTextBlue.setOnClickListener(onClickListener);
    formulaBrick.getFormulaWithBrickField(blueField).setTextFieldId(R.id.rgb_blue_value);
    formulaBrick.getFormulaWithBrickField(blueField).refreshTextField(seekbarView);

    redSeekBar = (SeekBar) seekbarView.findViewById(R.id.color_rgb_seekbar_red);
    greenSeekBar = (SeekBar) seekbarView.findViewById(R.id.color_rgb_seekbar_green);
    blueSeekBar = (SeekBar) seekbarView.findViewById(R.id.color_rgb_seekbar_blue);

    colorPreviewView = seekbarView.findViewById(R.id.color_rgb_preview);

    int color =
        Color.rgb(
            getCurrentBrickFieldValue(redField),
            getCurrentBrickFieldValue(greenField),
            getCurrentBrickFieldValue(blueField));
    redSeekBar.setProgress(Color.red(color));
    greenSeekBar.setProgress(Color.green(color));
    blueSeekBar.setProgress(Color.blue(color));

    colorPreviewView.setBackgroundColor(color);
    colorPreviewView.invalidate();

    //		redSeekBar.setProgress(getCurrentBrickFieldValue(redField));
    //		greenSeekBar.setProgress(getCurrentBrickFieldValue(greenField));
    //		blueSeekBar.setProgress(getCurrentBrickFieldValue(blueField));

    SeekBar.OnSeekBarChangeListener seekBarChangeListener =
        new SeekBar.OnSeekBarChangeListener() {
          @Override
          public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
            switch (seekBar.getId()) {
              case R.id.color_rgb_seekbar_red:
                formulaEditorEditTextRed.setText(String.valueOf(seekBar.getProgress()));
                break;
              case R.id.color_rgb_seekbar_green:
                formulaEditorEditTextGreen.setText(String.valueOf(seekBar.getProgress()));
                break;
              case R.id.color_rgb_seekbar_blue:
                formulaEditorEditTextBlue.setText(String.valueOf(seekBar.getProgress()));
                break;
              default:
                break;
            }
            int color =
                Color.argb(
                    0xFF,
                    redSeekBar.getProgress(),
                    greenSeekBar.getProgress(),
                    blueSeekBar.getProgress());
            colorPreviewView.setBackgroundColor(color);
            colorPreviewView.invalidate();
          }

          @Override
          public void onStartTrackingTouch(SeekBar seekBar) {}

          @Override
          public void onStopTrackingTouch(SeekBar seekBar) {

            Brick.BrickField changedBrickField = null;

            switch (seekBar.getId()) {
              case R.id.color_rgb_seekbar_red:
                FormulaEditorFragment.changeInputField(seekbarView, redField);
                changedBrickField = redField;
                break;
              case R.id.color_rgb_seekbar_green:
                FormulaEditorFragment.changeInputField(seekbarView, greenField);
                changedBrickField = greenField;
                break;
              case R.id.color_rgb_seekbar_blue:
                FormulaEditorFragment.changeInputField(seekbarView, blueField);
                changedBrickField = blueField;
                break;
              default:
                break;
            }
            FormulaEditorFragment.overwriteFormula(seekbarView, new Formula(seekBar.getProgress()));
            // ToDo: this is a hack for saving the value immediately
            FormulaEditorFragment.changeInputField(seekbarView, getOtherField(changedBrickField));
            FormulaEditorFragment.changeInputField(seekbarView, changedBrickField);
            // end of hack
          }
        };

    redSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
    greenSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);
    blueSeekBar.setOnSeekBarChangeListener(seekBarChangeListener);

    return seekbarView;
  }