summaryrefslogtreecommitdiff
path: root/NET/worlds/console/ScapePicPanel.java
blob: 29f5f75aa3be87c64ed746e821f00f93d0c52b74 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package NET.worlds.console;

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Panel;
import java.awt.Point;

public class ScapePicPanel extends Panel {
   private static final long serialVersionUID = -1019314311757410999L;
   private static final int QUARTERED = 0;
   private static final int UPPERLEFT = 1;
   private ScapePicImage image = null;
   private int hWnd;
   private int drawMode = 0;

   public ScapePicPanel() {
      this(null);
   }

   public ScapePicPanel(ScapePicImage image) {
      this.setImage(image);
   }

   public void setImage(ScapePicImage image) {
      this.image = image;
   }

   @Override
   public void paint(Graphics g) {
      this.drawBackground();
      super.paint(g);
   }

   @Override
   public void update(Graphics g) {
      this.paint(g);
   }

   public void setQuartered() {
      this.drawMode = 0;
   }

   public void setUpperLeft() {
      this.drawMode = 1;
   }

   private void drawBackground() {
      assert this.drawMode == 1 || this.drawMode == 0;

      if (this.image != null) {
         Dimension size = this.getSize();

         assert size != null;

         assert size.height >= 0 && size.width >= 0;

         if (this.drawMode == 1) {
            for (int j = 0; j < size.height; j += this.image.getHeight()) {
               for (int i = 0; i < size.width; i += this.image.getWidth()) {
                  this.drawScapePicImage(this.image, i, j, 0, 0, this.image.getWidth(), this.image.getHeight());
               }
            }
         }

         if (this.drawMode == 0) {
            int cx = size.width / 2;
            int cy = size.height / 2;

            for (int j = 0; j < cy; j += this.image.getHeight()) {
               for (int i = 0; i < cx; i += this.image.getWidth()) {
                  int endX = Math.min(cx - i, this.image.getWidth());
                  int endY = Math.min(cy - j, this.image.getHeight());
                  this.drawScapePicImage(this.image, i, j, 0, 0, endX, endY);
               }
            }

            for (int j = 0; j < cy; j += this.image.getHeight()) {
               for (int i = size.width; i > cx; i -= this.image.getWidth()) {
                  int startX = Math.max(i - this.image.getWidth(), cx);
                  int imageStartX = this.image.getWidth() - (i - startX);
                  int endY = Math.min(cy - j, this.image.getHeight());
                  this.drawScapePicImage(this.image, startX, j, imageStartX, 0, i - startX, endY);
               }
            }

            for (int j = size.height; j > cy; j -= this.image.getHeight()) {
               for (int i = 0; i < cx; i += this.image.getWidth()) {
                  int endX = Math.min(cx - i, this.image.getWidth());
                  int startY = Math.max(j - this.image.getHeight(), cy);
                  int imageStartY = this.image.getHeight() - (j - startY);
                  this.drawScapePicImage(this.image, i, startY, 0, imageStartY, endX, j - startY);
               }
            }

            for (int j = size.height; j > cy; j -= this.image.getHeight()) {
               for (int i = size.width; i > cx; i -= this.image.getWidth()) {
                  int startX = Math.max(i - this.image.getWidth(), cx);
                  int imageStartX = this.image.getWidth() - (i - startX);
                  int startY = Math.max(j - this.image.getHeight(), cy);
                  int imageStartY = this.image.getHeight() - (j - startY);
                  this.drawScapePicImage(this.image, startX, startY, imageStartX, imageStartY, i - startX, j - startY);
               }
            }
         }
      }
   }

   public void drawScapePicImage(ScapePicImage image, int xDst, int yDst, int xSrc, int ySrc, int width, int height) {
      if (this.hWnd == 0) {
         Point loc = this.locationInWindow();
         Dimension size = this.getSize();
         String title = this.getFrameTitle();

         assert title != null;

         int hWndParent = Window.findWindow(title);

         assert hWndParent != 0;

         this.hWnd = Window.findChildWindow(hWndParent, loc.x, loc.y, size.width, size.height);

         assert this.hWnd != 0;
      }

      ScapePicCanvas.bitBlt(this.hWnd, image.getDIB(), xDst, yDst, xSrc, ySrc, width, height);
   }

   private String getFrameTitle() {
      for (Container c = this.getParent(); c != null; c = c.getParent()) {
         if (c instanceof Frame) {
            return ((Frame)c).getTitle();
         }
      }

      return null;
   }

   private Point locationInWindow() {
      Point offset = this.location();

      for (Container c = this.getParent(); c != null; c = c.getParent()) {
         Point move = c.location();
         offset.translate(move.x, move.y);
      }

      return offset;
   }
}