Revert StaticDisplayState; For hosted only allow debug to be used.
[SquirrelJME.git] / modules / midp-lcdui / src / main / java / cc / squirreljme / runtime / lcdui / gfx / ProxyGraphics.java
blob3ebb0b78b02ff99e2af13a7d64d5ebc79d6c37a9
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.runtime.lcdui.gfx;
12 import cc.squirreljme.runtime.cldc.debug.Debugging;
13 import javax.microedition.lcdui.Font;
14 import javax.microedition.lcdui.Graphics;
15 import javax.microedition.lcdui.Image;
16 import javax.microedition.lcdui.Text;
18 /**
19 * This is a proxy version of {@link Graphics} where each instance can
20 * refer to a single graphics instance. When a draw operation occurs, the
21 * parameters of this current graphics will be set on the target.
23 * @see ProxyGraphicsTarget
24 * @since 2022/02/25
26 public final class ProxyGraphics
27 extends Graphics
29 /** The target graphics to draw into. */
30 protected final ProxyGraphicsTarget target;
32 /** The current alpha color. */
33 private int _argbColor;
35 /** The current blending mode. */
36 private int _blendingMode;
38 /** The clip height. */
39 private int _clipHeight;
41 /** The clip width. */
42 private int _clipWidth;
44 /** The clip X position. */
45 private int _clipX;
47 /** The clip Y position. */
48 private int _clipY;
50 /** The current font used. */
51 private Font _font;
53 /** The current stroke style. */
54 private int _strokeStyle;
56 /** The current X translation. */
57 private int _transX;
59 /** The current Y translation. */
60 private int _transY;
62 /**
63 * Initializes the proxy graphics with the given target.
65 * @param __target The target graphics proxy.
66 * @param __width The graphics width.
67 * @param __height The graphics height.
68 * @throws NullPointerException On null arguments.
69 * @since 2022/02/25
71 public ProxyGraphics(ProxyGraphicsTarget __target, int __width,
72 int __height)
73 throws NullPointerException
75 if (__target == null)
76 throw new NullPointerException("NARG");
78 this.target = __target;
79 this._clipWidth = __width;
80 this._clipHeight = __height;
83 /**
84 * {@inheritDoc}
85 * @since 2022/02/25
87 @Override
88 public void clipRect(int __a, int __b, int __c, int __d)
90 throw Debugging.todo();
93 /**
94 * {@inheritDoc}
95 * @since 2022/02/25
97 @Override
98 public void copyArea(int __a, int __b, int __c, int __d, int __e,
99 int __f, int __g)
101 this.__graphics().copyArea(__a, __b, __c, __d, __e, __f, __g);
105 * {@inheritDoc}
106 * @since 2022/02/25
108 @Override
109 public void drawArc(int __a, int __b, int __c, int __d, int __e,
110 int __f)
112 this.__graphics().drawArc(__a, __b, __c, __d, __e, __f);
116 * {@inheritDoc}
117 * @since 2022/02/25
119 @Override
120 public void drawARGB16(short[] __data, int __off, int __scanlen,
121 int __x, int __y, int __w, int __h)
123 this.__graphics().drawARGB16(__data, __off, __scanlen, __x, __y, __w,
124 __h);
128 * {@inheritDoc}
129 * @since 2022/02/25
131 @Override
132 public void drawChar(char __a, int __b, int __c, int __d)
134 this.__graphics().drawChar(__a, __b, __c, __d);
138 * {@inheritDoc}
139 * @since 2022/02/25
141 @Override
142 public void drawChars(char[] __a, int __b, int __c, int __d,
143 int __e, int __f)
145 this.__graphics().drawChars(__a, __b, __c, __d, __e, __f);
149 * {@inheritDoc}
150 * @since 2022/02/25
152 @Override
153 public void drawImage(Image __a, int __b, int __c, int __d)
155 this.__graphics().drawImage(__a, __b, __c, __d);
159 * {@inheritDoc}
160 * @since 2022/02/25
162 @Override
163 public void drawLine(int __a, int __b, int __c, int __d)
165 this.__graphics().drawLine(__a, __b, __c, __d);
169 * {@inheritDoc}
170 * @since 2022/02/25
172 @Override
173 public void drawRGB(int[] __a, int __b, int __c, int __d, int __e,
174 int __f, int __g, boolean __h)
176 this.__graphics().drawRGB(__a, __b, __c, __d, __e, __f, __g, __h);
180 * {@inheritDoc}
181 * @since 2022/02/25
183 @Override
184 public void drawRGB16(short[] __data, int __off, int __scanlen,
185 int __x, int __y, int __w, int __h)
187 this.__graphics().drawRGB16(__data, __off, __scanlen, __x, __y, __w, __h);
191 * {@inheritDoc}
192 * @since 2022/02/25
194 @Override
195 public void drawRect(int __a, int __b, int __c, int __d)
197 this.__graphics().drawRect(__a, __b, __c, __d);
201 * {@inheritDoc}
202 * @since 2022/02/25
204 @Override
205 public void drawRegion(Image __a, int __b, int __c, int __d,
206 int __e, int __f, int __g, int __h, int __i)
208 this.__graphics().drawRegion(__a, __b, __c, __d, __e, __f, __g, __h,
209 __i);
213 * {@inheritDoc}
214 * @since 2022/02/25
216 @Override
217 public void drawRegion(Image __src, int __xsrc, int __ysrc,
218 int __w, int __h, int __trans, int __xdest, int __ydest, int __anch,
219 int __wdest, int __hdest)
221 this.__graphics().drawRegion(__src, __xsrc, __ysrc, __w, __h, __trans,
222 __xdest, __ydest, __anch, __wdest, __hdest);
226 * {@inheritDoc}
227 * @since 2022/02/25
229 @Override
230 public void drawRoundRect(int __a, int __b, int __c, int __d,
231 int __e, int __f)
233 this.__graphics().drawRoundRect(__a, __b, __c, __d, __e, __f);
237 * {@inheritDoc}
238 * @since 2022/02/25
240 @Override
241 public void drawString(String __a, int __b, int __c, int __d)
243 this.__graphics().drawString(__a, __b, __c, __d);
247 * {@inheritDoc}
248 * @since 2022/02/25
250 @Override
251 public void drawSubstring(String __a, int __b, int __c, int __d,
252 int __e, int __f)
254 this.__graphics().drawSubstring(__a, __b, __c, __d, __e, __f);
258 * {@inheritDoc}
259 * @since 2022/02/25
261 @Override
262 public void drawText(Text __t, int __x, int __y)
264 this.__graphics().drawText(__t, __x, __y);
268 * {@inheritDoc}
269 * @since 2022/02/25
271 @Override
272 public void fillArc(int __a, int __b, int __c, int __d, int __e,
273 int __f)
275 this.__graphics().fillArc(__a, __b, __c, __d, __e, __f);
279 * {@inheritDoc}
280 * @since 2022/02/25
282 @Override
283 public void fillRect(int __x, int __y, int __w, int __h)
285 this.__graphics().fillRect(__x, __y, __w, __h);
289 * {@inheritDoc}
290 * @since 2022/02/25
292 @Override
293 public void fillRoundRect(int __a, int __b, int __c, int __d,
294 int __e, int __f)
296 this.__graphics().fillRoundRect(__a, __b, __c, __d, __e, __f);
300 * {@inheritDoc}
301 * @since 2022/02/25
303 @Override
304 public void fillTriangle(int __a, int __b, int __c, int __d,
305 int __e, int __f)
307 this.__graphics().fillTriangle(__a, __b, __c, __d, __e, __f);
311 * {@inheritDoc}
312 * @since 2022/02/25
314 @Override
315 public int getAlpha()
317 return (this._argbColor >> 24) & 0xFF;
321 * {@inheritDoc}
322 * @since 2017/02/10
324 @Override
325 public int getAlphaColor()
327 return this._argbColor;
331 * {@inheritDoc}
332 * @since 2022/02/25
334 @Override
335 public int getBlendingMode()
337 return this._blendingMode;
341 * {@inheritDoc}
342 * @since 2022/02/25
344 @Override
345 public int getBlueComponent()
347 return (this._argbColor) & 0xFF;
351 * {@inheritDoc}
352 * @since 2022/02/25
354 @Override
355 public int getClipHeight()
357 return this._clipHeight;
361 * {@inheritDoc}
362 * @since 2022/02/25
364 @Override
365 public int getClipWidth()
367 return this._clipWidth;
371 * {@inheritDoc}
372 * @since 2022/02/25
374 @Override
375 public int getClipX()
377 return this._clipX - this._transX;
381 * {@inheritDoc}
382 * @since 2022/02/25
384 @Override
385 public int getClipY()
387 return this._clipY - this._transY;
391 * {@inheritDoc}
392 * @since 2022/02/25
394 @Override
395 public int getColor()
397 return this._argbColor & 0xFFFFFF;
401 * {@inheritDoc}
402 * @since 2022/02/25
404 @Override
405 public int getDisplayColor(int __a)
407 return this.__graphics().getDisplayColor(__a);
411 * {@inheritDoc}
412 * @since 2022/02/25
414 @Override
415 public Font getFont()
417 return this.__font();
421 * {@inheritDoc}
422 * @since 2022/02/25
424 @Override
425 public int getGrayScale()
427 return (((this._argbColor >> 16) & 0xFF) +
428 ((this._argbColor >> 8) & 0xFF) +
429 ((this._argbColor) & 0xFF)) / 3;
433 * {@inheritDoc}
434 * @since 2022/02/25
436 @Override
437 public int getGreenComponent()
439 return (this._argbColor >> 8) & 0xFF;
443 * {@inheritDoc}
444 * @since 2022/02/25
446 @Override
447 public int getRedComponent()
449 return (this._argbColor >> 16) & 0xFF;
453 * {@inheritDoc}
454 * @since 2022/02/25
456 @Override
457 public int getStrokeStyle()
459 return this._strokeStyle;
463 * {@inheritDoc}
464 * @since 2022/02/25
466 @Override
467 public int getTranslateX()
469 return this._transX;
473 * {@inheritDoc}
474 * @since 2022/02/25
476 @Override
477 public int getTranslateY()
479 return this._transY;
483 * {@inheritDoc}
484 * @since 2022/02/25
486 @Override
487 public void setAlpha(int __a)
489 this.setAlphaColor(__a,
490 this.getRedComponent(),
491 this.getGreenComponent(),
492 this.getBlueComponent());
496 * {@inheritDoc}
497 * @since 2022/02/25
499 @Override
500 public void setAlphaColor(int __argb)
502 this._argbColor = __argb;
506 * {@inheritDoc}
507 * @since 2022/02/25
509 @Override
510 public void setAlphaColor(int __a, int __r, int __g, int __b)
512 // {@squirreljme.error EB2y Color out of range. (Alpha; Red; Green;
513 // Blue)}
514 if (__a < 0 || __a > 255 || __r < 0 || __r > 255 ||
515 __g < 0 || __g > 255 || __b < 0 || __b > 255)
516 throw new IllegalArgumentException(String.format(
517 "EB2y %d %d %d %d", __a, __r, __g, __b));
519 // Set
520 this.setAlphaColor((__a << 24) | (__r << 16) | (__g << 8) | __b);
524 * {@inheritDoc}
525 * @since 2022/02/25
527 @Override
528 public void setBlendingMode(int __m)
530 // {@squirreljme.error EB2x Invalid blending mode. (The mode)}
531 if (__m != Graphics.SRC && __m != Graphics.SRC_OVER)
532 throw new IllegalArgumentException("EB2x " + __m);
534 // Cache locally
535 this._blendingMode = __m;
539 * {@inheritDoc}
540 * @since 2022/02/25
542 @Override
543 public void setClip(int __x, int __y, int __w, int __h)
545 // Calculate the base clip coordinates
546 int startX = __x + this._transX;
547 int startY = __y + this._transY;
548 int endX = startX + __w;
549 int endY = startY + __h;
551 // Normalize X
552 if (endX < startX)
554 int temp = endX;
555 endX = startX;
556 startX = temp;
559 // Normalize Y
560 if (endY < startY)
562 int temp = endY;
563 endY = startY;
564 startY = temp;
567 // Determine the bounds of all of these
568 int clipX = Math.max(0, startX);
569 int clipY = Math.max(0, startY);
570 int clipEndX = Math.max(0, endX);
571 int clipEndY = Math.max(0, endY);
573 // Record internally
574 this._clipX = clipX;
575 this._clipY = clipY;
576 this._clipWidth = clipEndX - clipX;
577 this._clipHeight = clipEndY - clipY;
581 * {@inheritDoc}
582 * @since 2022/02/25
584 @Override
585 public void setColor(int __rgb)
587 this.setAlphaColor((this.getAlphaColor() & 0xFF_000000) |
588 (__rgb & 0x00_FFFFFF));
592 * {@inheritDoc}
593 * @since 2022/02/25
595 @Override
596 public void setColor(int __r, int __g, int __b)
598 this.setAlphaColor(this.getAlpha(), __r, __g, __b);
602 * {@inheritDoc}
603 * @since 2022/02/25
605 @Override
606 public void setFont(Font __font)
608 this.__graphics().setFont(__font);
612 * {@inheritDoc}
613 * @since 2022/02/25
615 @Override
616 public void setGrayScale(int __v)
618 this.setAlphaColor(this.getAlpha(), __v, __v, __v);
622 * {@inheritDoc}
623 * @since 2022/02/25
624 * @param __style
626 @Override
627 public void setStrokeStyle(int __style)
629 // {@squirreljme.error EB2z Illegal stroke style.}
630 if (__style != Graphics.SOLID && __style != Graphics.DOTTED)
631 throw new IllegalArgumentException("EB2z");
633 // Set
634 this._strokeStyle = __style;
638 * {@inheritDoc}
639 * @since 2022/02/25
641 @Override
642 public void translate(int __x, int __y)
644 // Cache locally
645 this._transX += __x;
646 this._transY += __y;
650 * Returns the font that should be used.
652 * @return The font used.
653 * @since 2022/02/25
655 private Font __font()
657 Font rv = this._font;
658 if (rv == null)
659 rv = Font.getDefaultFont();
660 return rv;
664 * Initializes and returns the target graphics accordingly.
666 * @return The resultant graphics object.
667 * @since 2022/02/25
669 private Graphics __graphics()
671 // This is the graphics we are drawing into
672 Graphics target = this.target._target;
674 // Pass all the adjustable parameters to the target
675 target.setAlphaColor(this._argbColor);
676 target.setBlendingMode(this._blendingMode);
677 target.setFont(this.__font());
678 target.setStrokeStyle(this._strokeStyle);
680 // Translation that is needed for clipping and translation
681 int targetTransX = target.getTranslateX();
682 int targetTransY = target.getTranslateY();
684 // Set absolute clipping area
685 target.setClip(
686 this._clipX - targetTransX, this._clipY - targetTransY,
687 this._clipWidth, this._clipHeight);
688 target.translate(this._transX - targetTransX,
689 this._transY - targetTransY);
691 // Return the graphics we are drawing into
692 return target;