00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.gui.gui;
00023
00024 import org.jetbrains.annotations.NotNull;
00025
00030 public class Expression {
00031
00035 private final int constant;
00036
00040 private final int widthFactor;
00041
00045 private final int heightFactor;
00046
00050 private final int prefWidthFactor;
00051
00055 private final int prefHeightFactor;
00056
00065 public Expression(final int constant, final int widthFactor, final int heightFactor, final int prefWidthFactor, final int prefHeightFactor) {
00066 this.constant = constant;
00067 this.widthFactor = widthFactor;
00068 this.heightFactor = heightFactor;
00069 this.prefWidthFactor = prefWidthFactor;
00070 this.prefHeightFactor = prefHeightFactor;
00071 }
00072
00080 public Expression(@NotNull final Expression expression1, final boolean negative, @NotNull final Expression expression2) {
00081 final int factor = negative ? -1 : 1;
00082 constant = expression1.constant+expression2.constant*factor;
00083 widthFactor = expression1.widthFactor+expression2.widthFactor*factor;
00084 heightFactor = expression1.heightFactor+expression2.heightFactor*factor;
00085 prefWidthFactor = expression1.prefWidthFactor+expression2.prefWidthFactor*factor;
00086 prefHeightFactor = expression1.prefHeightFactor+expression2.prefHeightFactor*factor;
00087 }
00088
00097 public int evaluate(final int width, final int height, final int prefWidth, final int prefHeight) {
00098 return constant+applyFactor(width, widthFactor)+applyFactor(height, heightFactor)+applyFactor(prefWidth, prefWidthFactor)+applyFactor(prefHeight, prefHeightFactor);
00099 }
00100
00105 public int evaluateConstant() {
00106 if (widthFactor != 0 || heightFactor != 0) {
00107 throw new IllegalStateException();
00108 }
00109
00110 return constant;
00111 }
00112
00119 private static int applyFactor(final int value, final int factor) {
00120 return (value*factor+1)/2;
00121 }
00122
00123 }