Crossfire JXClient, Trunk
GUIDialogBackground.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-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.gui.misc;
24 
31 import java.awt.AlphaComposite;
32 import java.awt.Dimension;
33 import java.awt.Graphics;
34 import java.awt.Graphics2D;
35 import java.awt.Image;
36 import java.awt.Point;
37 import java.awt.event.MouseEvent;
38 import javax.swing.JComponent;
39 import org.jetbrains.annotations.NotNull;
40 import org.jetbrains.annotations.Nullable;
41 
47 
51  private static final long serialVersionUID = 1L;
52 
56  @NotNull
58 
62  @NotNull
63  private final GuiFactory guiFactory;
64 
68  @NotNull
69  private final Image frameNW;
70 
74  @NotNull
75  private final Image frameN;
76 
80  @NotNull
81  private final Image frameNE;
82 
86  @NotNull
87  private final Image frameW;
88 
92  @NotNull
93  private final Image frameC;
94 
98  @NotNull
99  private final Image frameE;
100 
104  @NotNull
105  private final Image frameSW;
106 
110  @NotNull
111  private final Image frameS;
112 
116  @NotNull
117  private final Image frameSE;
118 
122  private final int heightN;
123 
127  private final int heightS;
128 
132  private final int widthW;
133 
137  private final int widthE;
138 
143  private final float alpha;
144 
148  private boolean opaque;
149 
153  private boolean dragging;
154 
158  private boolean top;
159 
163  private boolean left;
164 
168  private boolean right;
169 
173  private boolean bottom;
174 
178  private int mouseX;
179 
183  private int mouseY;
184 
188  private int startX1;
189 
193  private int startY1;
194 
199  private int startX2;
200 
205  private int startY2;
206 
226  public GUIDialogBackground(@NotNull final TooltipManager tooltipManager, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final GUIElementListener elementListener, @NotNull final String name, final float alpha, @NotNull final Image frameNW, @NotNull final Image frameN, @NotNull final Image frameNE, @NotNull final Image frameW, @NotNull final Image frameC, @NotNull final Image frameE, @NotNull final Image frameSW, @NotNull final Image frameS, @NotNull final Image frameSE, @NotNull final GuiFactory guiFactory) {
228  this.windowRenderer = windowRenderer;
229  this.guiFactory = guiFactory;
230  this.frameNW = frameNW;
231  this.frameN = frameN;
232  this.frameNE = frameNE;
233  this.frameW = frameW;
234  this.frameC = frameC;
235  this.frameE = frameE;
236  this.frameSW = frameSW;
237  this.frameS = frameS;
238  this.frameSE = frameSE;
239  this.alpha = alpha;
240  heightN = frameN.getHeight(null);
241  heightS = frameS.getHeight(null);
242  widthW = frameW.getWidth(null);
243  widthE = frameE.getWidth(null);
244  if (frameNW.getWidth(null) != widthW) {
245  throw new IllegalArgumentException("frameNW.width="+frameNW.getWidth(null)+" != "+widthW+"=widthW");
246  }
247  if (frameSW.getWidth(null) != widthW) {
248  throw new IllegalArgumentException("frameSW.width="+frameSW.getWidth(null)+" != "+widthW+"=widthW");
249  }
250  if (frameNE.getWidth(null) != widthE) {
251  throw new IllegalArgumentException("frameNE.width="+frameNE.getWidth(null)+" != "+widthE+"=widthE");
252  }
253  if (frameSE.getWidth(null) != widthE) {
254  throw new IllegalArgumentException("frameSE.width="+frameSE.getWidth(null)+" != "+widthE+"=widthE");
255  }
256  if (frameNW.getHeight(null) != heightN) {
257  throw new IllegalArgumentException("frameNW.height="+frameNW.getHeight(null)+" != "+heightN+"heightN");
258  }
259  if (frameSW.getHeight(null) != heightS) {
260  throw new IllegalArgumentException("frameSW.height="+frameSW.getHeight(null)+" != "+heightS+"=heightS");
261  }
262  if (frameNE.getHeight(null) != heightN) {
263  throw new IllegalArgumentException("frameNE.height="+frameNE.getHeight(null)+" != "+heightN+"=heightN");
264  }
265  if (frameSE.getHeight(null) != heightS) {
266  throw new IllegalArgumentException("frameSE.height="+frameSE.getHeight(null)+" != "+heightS+"=heightS");
267  }
268  }
269 
274  public void setOpaqueDialogBackground(final boolean opaque) {
275  if (this.opaque == opaque) {
276  return;
277  }
278 
279  this.opaque = opaque;
280  setOpaque(opaque || alpha >= 1.0F);
281  repaint();
282  }
283 
284  @Override
285  public void paintComponent(@NotNull final Graphics g) {
286  final Graphics paint;
287  if (opaque || alpha >= 1.0F) {
288  paint = g;
289  } else {
290  final Graphics2D g2d = (Graphics2D)g.create();
291  g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
292  paint = g2d;
293  }
294 
295  super.paintComponent(paint);
296  final int w = Math.max(1, getWidth()-widthW-widthE);
297  final int h = Math.max(1, getHeight()-heightN-heightS);
298  paint.drawImage(frameNW, 0, 0, null);
299  paint.drawImage(frameN, widthW, 0, widthW+w, heightN, 0, 0, w, heightN, null);
300  paint.drawImage(frameNE, widthW+w, 0, null);
301  paint.drawImage(frameW, 0, heightN, widthW, heightN+h, 0, 0, widthW, h, null);
302  paint.drawImage(frameC, widthW, heightN, widthW+w, heightN+h, 0, 0, w, h, null);
303  paint.drawImage(frameE, widthW+w, heightN, widthW+w+widthE, heightN+h, 0, 0, widthE, h, null);
304  paint.drawImage(frameSW, 0, heightN+h, null);
305  paint.drawImage(frameS, widthW, heightN+h, widthW+w, heightN+h+heightS, 0, 0, w, heightS, null);
306  paint.drawImage(frameSE, widthW+w, heightN+h, null);
307  if (paint != g) {
308  paint.dispose();
309  }
310  }
311 
312  @Nullable
313  @Override
314  @SuppressWarnings("MethodDoesntCallSuperMethod")
315  public Dimension getPreferredSize() {
316  return getMinimumSize();
317  }
318 
319  @Nullable
320  @Override
321  @SuppressWarnings("MethodDoesntCallSuperMethod")
322  public Dimension getMinimumSize() {
323  return new Dimension(widthW+1+widthE, heightN+1+heightS);
324  }
325 
326  @Override
327  public void notifyOpen() {
328  }
329 
330  @Nullable
331  @Override
333  return null;
334  }
335 
336  @Override
337  @SuppressWarnings("MethodDoesntCallSuperMethod")
338  public int getDialogBorderTop() {
339  return frameN.getHeight(null);
340  }
341 
342  @Override
343  @SuppressWarnings("MethodDoesntCallSuperMethod")
344  public int getDialogBorderLeft() {
345  return frameW.getWidth(null);
346  }
347 
348  @Override
349  @SuppressWarnings("MethodDoesntCallSuperMethod")
350  public int getDialogBorderRight() {
351  return frameE.getWidth(null);
352  }
353 
354  @Override
355  @SuppressWarnings("MethodDoesntCallSuperMethod")
356  public int getDialogBorderBottom() {
357  return frameS.getHeight(null);
358  }
359 
360  @Override
361  public void mousePressed(@NotNull final MouseEvent e) {
362  super.mousePressed(e);
363 
364  if (!isEnabled()) {
365  return;
366  }
367 
368  final Gui gui = guiFactory.getGui(this);
369  if (gui == null || gui.isAutoSize() || !gui.isUserResizable()) {
370  dragging = false;
371  return;
372  }
373 
374  final Point point = e.getLocationOnScreen();
375  dragging = true;
376 
377  left = e.getX() < frameW.getWidth(null);
378  right = e.getX() >= getWidth()-frameE.getWidth(null);
379  top = e.getY() < frameN.getHeight(null);
380  bottom = e.getY() >= getHeight()-frameS.getHeight(null);
381  final JComponent component = gui.getComponent();
382  startX1 = component.getX();
383  startY1 = component.getY();
384  startX2 = startX1+component.getWidth();
385  startY2 = startY1+component.getHeight();
386 
387  mouseX = point.x-startX1;
388  mouseY = point.y-startY1;
389  }
390 
391  @Override
392  public void mouseReleased(@NotNull final MouseEvent e) {
393  super.mouseReleased(e);
394  if (isEnabled()) {
395  moveTo(e);
396  dragging = false;
397  }
398  }
399 
400  @Override
401  public void mouseDragged(@NotNull final MouseEvent e) {
402  super.mouseDragged(e);
403  if (isEnabled()) {
404  moveTo(e);
405  }
406  }
407 
412  private void moveTo(@NotNull final MouseEvent e) {
413  if (!dragging) {
414  return;
415  }
416 
417  final Gui gui = guiFactory.getGui(this);
418  if (gui == null || gui.isAutoSize() || !gui.isUserResizable()) {
419  dragging = false;
420  return;
421  }
422 
423  final Point point = e.getLocationOnScreen();
424 
425  final Dimension minimumSize = gui.getComponent().getMinimumSize();
426  final Dimension maximumSize = gui.getComponent().getMaximumSize();
427 
428  int x1;
429  int x2;
430  if (left) {
431  x1 = point.x-mouseX;
432  x2 = startX2;
433  if (minimumSize != null && x2-x1 < minimumSize.width) {
434  x1 = x2-minimumSize.width;
435  }
436  if (maximumSize != null && x2-x1 > maximumSize.width) {
437  x1 = x2-maximumSize.width;
438  }
439  } else if (right) {
440  x1 = startX1;
441  x2 = point.x-mouseX+startX2-startX1;
442  if (minimumSize != null && x2-x1 < minimumSize.width) {
443  x2 = x1+minimumSize.width;
444  }
445  if (maximumSize != null && x2-x1 > maximumSize.width) {
446  x2 = x1+maximumSize.width;
447  }
448  } else {
449  x1 = startX1;
450  x2 = startX2;
451  }
452 
453  int y1;
454  int y2;
455  if (top) {
456  y1 = point.y-mouseY;
457  y2 = startY2;
458  if (minimumSize != null && y2-y1 < minimumSize.height) {
459  y1 = y2-minimumSize.height;
460  }
461  if (maximumSize != null && y2-y1 > maximumSize.height) {
462  y1 = y2-maximumSize.height;
463  }
464  } else if (bottom) {
465  y1 = startY1;
466  y2 = point.y-mouseY+startY2-startY1;
467  if (minimumSize != null && y2-y1 < minimumSize.height) {
468  y2 = y1+minimumSize.height;
469  }
470  if (maximumSize != null && y2-y1 > maximumSize.height) {
471  y2 = y1+maximumSize.height;
472  }
473  } else {
474  y1 = startY1;
475  y2 = startY2;
476  }
477 
478  gui.setBounds(x1, y1, x2-x1, y2-y1, windowRenderer.getWindowWidth(), windowRenderer.getWindowHeight());
479  }
480 
481 }
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.name
final String name
Definition: AbstractGUIElement.java:77
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.gui.Gui.isAutoSize
boolean isAutoSize()
Definition: Gui.java:179
com.realtime.crossfire.jxclient.gui.gui.Gui.isUserResizable
boolean isUserResizable()
Definition: Gui.java:211
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.frameSE
final Image frameSE
Definition: GUIDialogBackground.java:117
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.startY1
int startY1
Definition: GUIDialogBackground.java:193
com.realtime.crossfire.jxclient.gui.gui.Gui
Definition: Gui.java:49
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.getDialogBorderTop
int getDialogBorderTop()
Definition: GUIDialogBackground.java:338
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.GUIDialogBackground
GUIDialogBackground(@NotNull final TooltipManager tooltipManager, @NotNull final JXCWindowRenderer windowRenderer, @NotNull final GUIElementListener elementListener, @NotNull final String name, final float alpha, @NotNull final Image frameNW, @NotNull final Image frameN, @NotNull final Image frameNE, @NotNull final Image frameW, @NotNull final Image frameC, @NotNull final Image frameE, @NotNull final Image frameSW, @NotNull final Image frameS, @NotNull final Image frameSE, @NotNull final GuiFactory guiFactory)
Definition: GUIDialogBackground.java:226
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.dragging
boolean dragging
Definition: GUIDialogBackground.java:153
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.bottom
boolean bottom
Definition: GUIDialogBackground.java:173
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.mouseY
int mouseY
Definition: GUIDialogBackground.java:183
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground
Definition: GUIDialogBackground.java:46
com.realtime.crossfire.jxclient.gui.gui.Gui.setBounds
void setBounds(final int x, final int y, final int width, final int height, final int windowWidth, final int windowHeight)
Definition: Gui.java:662
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.startX2
int startX2
Definition: GUIDialogBackground.java:199
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.widthE
final int widthE
Definition: GUIDialogBackground.java:137
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.right
boolean right
Definition: GUIDialogBackground.java:168
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.heightS
final int heightS
Definition: GUIDialogBackground.java:127
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.widthW
final int widthW
Definition: GUIDialogBackground.java:132
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer
Definition: JXCWindowRenderer.java:87
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.heightN
final int heightN
Definition: GUIDialogBackground.java:122
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.notifyOpen
void notifyOpen()
Definition: GUIDialogBackground.java:327
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.opaque
boolean opaque
Definition: GUIDialogBackground.java:148
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.frameNE
final Image frameNE
Definition: GUIDialogBackground.java:81
com.realtime.crossfire.jxclient.skin.skin.GuiFactory.getGui
Gui getGui(@NotNull final AbstractGUIElement element)
Definition: GuiFactory.java:110
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.frameNW
final Image frameNW
Definition: GUIDialogBackground.java:69
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.alpha
final float alpha
Definition: GUIDialogBackground.java:143
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.startY2
int startY2
Definition: GUIDialogBackground.java:205
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.mouseX
int mouseX
Definition: GUIDialogBackground.java:178
com.realtime.crossfire.jxclient.gui.gui.Gui.getComponent
JComponent getComponent()
Definition: Gui.java:161
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.startX1
int startX1
Definition: GUIDialogBackground.java:188
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer.getWindowHeight
int getWindowHeight()
Definition: JXCWindowRenderer.java:1116
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.getDialogBorderBottom
int getDialogBorderBottom()
Definition: GUIDialogBackground.java:356
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.mouseReleased
void mouseReleased(@NotNull final MouseEvent e)
Definition: GUIDialogBackground.java:392
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.frameN
final Image frameN
Definition: GUIDialogBackground.java:75
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.moveTo
void moveTo(@NotNull final MouseEvent e)
Definition: GUIDialogBackground.java:412
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.frameSW
final Image frameSW
Definition: GUIDialogBackground.java:105
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.frameS
final Image frameS
Definition: GUIDialogBackground.java:111
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.elementListener
final GUIElementListener elementListener
Definition: AbstractGUIElement.java:89
com.realtime.crossfire.jxclient.gui.gui.TooltipManager
Definition: TooltipManager.java:33
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.guiFactory
final GuiFactory guiFactory
Definition: GUIDialogBackground.java:63
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.getDialogBorderLeft
int getDialogBorderLeft()
Definition: GUIDialogBackground.java:344
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.mousePressed
void mousePressed(@NotNull final MouseEvent e)
Definition: GUIDialogBackground.java:361
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.getMinimumSize
Dimension getMinimumSize()
Definition: GUIDialogBackground.java:322
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.frameW
final Image frameW
Definition: GUIDialogBackground.java:87
com.realtime.crossfire.jxclient.gui.gui.TooltipText
Definition: TooltipText.java:31
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.setOpaqueDialogBackground
void setOpaqueDialogBackground(final boolean opaque)
Definition: GUIDialogBackground.java:274
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.getDialogBorderRight
int getDialogBorderRight()
Definition: GUIDialogBackground.java:350
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.serialVersionUID
static final long serialVersionUID
Definition: GUIDialogBackground.java:51
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.frameE
final Image frameE
Definition: GUIDialogBackground.java:99
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.paintComponent
void paintComponent(@NotNull final Graphics g)
Definition: GUIDialogBackground.java:285
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement
Definition: AbstractGUIElement.java:37
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.frameC
final Image frameC
Definition: GUIDialogBackground.java:93
com.realtime.crossfire.jxclient.gui.misc.JXCWindowRenderer.getWindowWidth
int getWindowWidth()
Definition: JXCWindowRenderer.java:1108
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.left
boolean left
Definition: GUIDialogBackground.java:163
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.getTooltip
TooltipText getTooltip()
Definition: GUIDialogBackground.java:332
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.mouseDragged
void mouseDragged(@NotNull final MouseEvent e)
Definition: GUIDialogBackground.java:401
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.getPreferredSize
Dimension getPreferredSize()
Definition: GUIDialogBackground.java:315
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.top
boolean top
Definition: GUIDialogBackground.java:158
com.realtime.crossfire.jxclient.gui.gui.GUIElementListener
Definition: GUIElementListener.java:32
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.tooltipManager
final TooltipManager tooltipManager
Definition: AbstractGUIElement.java:83
com.realtime.crossfire.jxclient.gui.misc.GUIDialogBackground.windowRenderer
final JXCWindowRenderer windowRenderer
Definition: GUIDialogBackground.java:57