Crossfire JXClient, Trunk  R20561
Expression.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff.
19  * Copyright (C) 2006-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.gui;
23 
24 import org.jetbrains.annotations.NotNull;
25 
30 public class Expression {
31 
35  private final int constant;
36 
40  private final int widthFactor;
41 
45  private final int heightFactor;
46 
50  private final int prefWidthFactor;
51 
55  private final int prefHeightFactor;
56 
65  public Expression(final int constant, final int widthFactor, final int heightFactor, final int prefWidthFactor, final int prefHeightFactor) {
66  this.constant = constant;
67  this.widthFactor = widthFactor;
68  this.heightFactor = heightFactor;
69  this.prefWidthFactor = prefWidthFactor;
70  this.prefHeightFactor = prefHeightFactor;
71  }
72 
80  public Expression(@NotNull final Expression expression1, final boolean negative, @NotNull final Expression expression2) {
81  final int factor = negative ? -1 : 1;
82  constant = expression1.constant+expression2.constant*factor;
83  widthFactor = expression1.widthFactor+expression2.widthFactor*factor;
84  heightFactor = expression1.heightFactor+expression2.heightFactor*factor;
85  prefWidthFactor = expression1.prefWidthFactor+expression2.prefWidthFactor*factor;
86  prefHeightFactor = expression1.prefHeightFactor+expression2.prefHeightFactor*factor;
87  }
88 
97  public int evaluate(final int width, final int height, final int prefWidth, final int prefHeight) {
98  return constant+applyFactor(width, widthFactor)+applyFactor(height, heightFactor)+applyFactor(prefWidth, prefWidthFactor)+applyFactor(prefHeight, prefHeightFactor);
99  }
100 
105  public int evaluateConstant() {
106  if (widthFactor != 0 || heightFactor != 0) {
107  throw new IllegalStateException("widthFactor="+widthFactor+", heightFactor="+heightFactor);
108  }
109 
110  return constant;
111  }
112 
119  private static int applyFactor(final int value, final int factor) {
120  return (value*factor+1)/2;
121  }
122 
123 }
Expression(final int constant, final int widthFactor, final int heightFactor, final int prefWidthFactor, final int prefHeightFactor)
Creates a new instance.
Definition: Expression.java:65
int evaluate(final int width, final int height, final int prefWidth, final int prefHeight)
Evaluates the expression into a constant.
Definition: Expression.java:97
final int prefWidthFactor
The preferred width dependent factor.
Definition: Expression.java:50
final int prefHeightFactor
The preferred height dependent factor.
Definition: Expression.java:55
static int applyFactor(final int value, final int factor)
Applies a factor to a value.
An expression yielding an integer value derived from a screen resolution.
Definition: Expression.java:30
Expression(@NotNull final Expression expression1, final boolean negative, @NotNull final Expression expression2)
Creates a new instance as the sum or difference of two expressions.
Definition: Expression.java:80
int evaluateConstant()
Evaluates the expression into a constant.
final int heightFactor
The screen height dependent factor.
Definition: Expression.java:45
final int widthFactor
The screen width dependent factor.
Definition: Expression.java:40