/** * Construct a new immutable {@code JCameraFPSStyleMouseRegion} instance. * @param origin The value for the {@code origin} attribute * @param width The value for the {@code width} attribute * @param height The value for the {@code height} attribute * @return An immutable JCameraFPSStyleMouseRegion instance */ public static JCameraFPSStyleMouseRegion of(JCameraScreenOrigin origin, double width, double height) { return validate(new JCameraFPSStyleMouseRegion(origin, width, height)); }
private static JCameraFPSStyleMouseRegion validate(JCameraFPSStyleMouseRegion instance) { instance.checkPreconditions(); return instance; }
/** * This instance is equal to all instances of {@code JCameraFPSStyleMouseRegion} that have equal attribute values. * @return {@code true} if {@code this} is equal to {@code another} instance */ @Override public boolean equals(Object another) { if (this == another) return true; return another instanceof JCameraFPSStyleMouseRegion && equalTo((JCameraFPSStyleMouseRegion) another); }
@Test public void testCoefficientsBottomLeftExhaustive() { final JCameraRotationCoefficients out = new JCameraRotationCoefficients(); for (int width = 2; width <= 640; ++width) { for (int height = 2; height <= 480; ++height) { final JCameraFPSStyleMouseRegion r = JCameraFPSStyleMouseRegion.newRegion( JCameraScreenOrigin.SCREEN_ORIGIN_BOTTOM_LEFT, width, height); Assert.assertEquals( JCameraScreenOrigin.SCREEN_ORIGIN_BOTTOM_LEFT, r.getOrigin()); Assert.assertEquals(height, r.getHeight(), 0.0f); Assert.assertEquals(width, r.getWidth(), 0.0f); r.getCoefficients(width / 2.0f, height / 2.0f, out); Assert.assertEquals(0.0f, out.getHorizontal(), 0.0f); Assert.assertEquals(0.0f, out.getVertical(), 0.0f); r.getCoefficients(0, 0, out); Assert.assertEquals(-1.0f, out.getHorizontal(), 0.0f); Assert.assertEquals(1.0f, out.getVertical(), 0.0f); r.getCoefficients(width, height, out); Assert.assertEquals(1.0f, out.getHorizontal(), 0.0f); Assert.assertEquals(-1.0f, out.getVertical(), 0.0f); } } }
/** * Construct a new mouse region. * * @param in_origin The screen origin. * @param in_width The region width. * @param in_height The region height. * * @return A new mouse region. */ public static JCameraFPSStyleMouseRegion newRegion( final JCameraScreenOrigin in_origin, final float in_width, final float in_height) { return new JCameraFPSStyleMouseRegion(in_origin, in_width, in_height); }
@Test public void testCoefficientsTopLeft() { final JCameraRotationCoefficients out = new JCameraRotationCoefficients(); final JCameraFPSStyleMouseRegion r = JCameraFPSStyleMouseRegion.newRegion( JCameraScreenOrigin.SCREEN_ORIGIN_TOP_LEFT, 640, 480); r.getCoefficients(0, 0, out); Assert.assertEquals(1.0f, out.getHorizontal(), 0.0f); Assert.assertEquals(1.0f, out.getVertical(), 0.0f); r.getCoefficients(640, 480, out); Assert.assertEquals(-1.0f, out.getHorizontal(), 0.0f); Assert.assertEquals(-1.0f, out.getVertical(), 0.0f); r.getCoefficients(320, 240, out); Assert.assertEquals(0.0f, out.getHorizontal(), 0.0f); Assert.assertEquals(0.0f, out.getVertical(), 0.0f); } }
/** * Creates an immutable copy of a {@link JCameraFPSStyleMouseRegionType} value. * Uses accessors to get values to initialize the new immutable instance. * If an instance is already immutable, it is returned as is. * @param instance The instance to copy * @return A copied immutable JCameraFPSStyleMouseRegion instance */ public static JCameraFPSStyleMouseRegion copyOf(JCameraFPSStyleMouseRegionType instance) { if (instance instanceof JCameraFPSStyleMouseRegion) { return (JCameraFPSStyleMouseRegion) instance; } return JCameraFPSStyleMouseRegion.builder() .from(instance) .build(); }
@Override public void mouseMoved( final @Nullable MouseEvent e) { assert e != null; /* * If the camera is enabled, get the rotation coefficients for the mouse * movement. */ if (this.sim.cameraIsEnabled()) { this.rotations.from( this.mouse_region.get().coefficients( (double) e.getX(), (double) e.getY())); this.input.addRotationAroundHorizontal(this.rotations.horizontal()); this.input.addRotationAroundVertical(this.rotations.vertical()); } } }
/** * Builds a new {@link JCameraFPSStyleMouseRegion JCameraFPSStyleMouseRegion}. * @return An immutable instance of JCameraFPSStyleMouseRegion * @throws java.lang.IllegalStateException if any required attributes are missing */ public JCameraFPSStyleMouseRegion build() { if (initBits != 0) { throw new IllegalStateException(formatRequiredAttributesMessage()); } return JCameraFPSStyleMouseRegion.validate(new JCameraFPSStyleMouseRegion(null, origin, width, height)); }
@Test public void testCoefficientsBottomLeft() { final JCameraRotationCoefficients out = new JCameraRotationCoefficients(); final JCameraFPSStyleMouseRegion r = JCameraFPSStyleMouseRegion.newRegion( JCameraScreenOrigin.SCREEN_ORIGIN_BOTTOM_LEFT, 640, 480); r.getCoefficients(0, 0, out); Assert.assertEquals(-1.0f, out.getHorizontal(), 0.0f); Assert.assertEquals(1.0f, out.getVertical(), 0.0f); r.getCoefficients(640, 480, out); Assert.assertEquals(1.0f, out.getHorizontal(), 0.0f); Assert.assertEquals(-1.0f, out.getVertical(), 0.0f); r.getCoefficients(320, 240, out); Assert.assertEquals(0.0f, out.getHorizontal(), 0.0f); Assert.assertEquals(0.0f, out.getVertical(), 0.0f); }
/** * Copy the current immutable object by setting a value for the {@link JCameraFPSStyleMouseRegionType#origin() origin} attribute. * A value equality check is used to prevent copying of the same value by returning {@code this}. * @param value A new value for origin * @return A modified copy of the {@code this} object */ public final JCameraFPSStyleMouseRegion withOrigin(JCameraScreenOrigin value) { if (this.origin == value) return this; JCameraScreenOrigin newValue = Objects.requireNonNull(value, "origin"); return validate(new JCameraFPSStyleMouseRegion(this, newValue, this.width, this.height)); }
/** * Copy the current immutable object by setting a value for the {@link JCameraFPSStyleMouseRegionType#width() width} attribute. * A value strict bits equality used to prevent copying of the same value by returning {@code this}. * @param value A new value for width * @return A modified copy of the {@code this} object */ public final JCameraFPSStyleMouseRegion withWidth(double value) { if (Double.doubleToLongBits(this.width) == Double.doubleToLongBits(value)) return this; return validate(new JCameraFPSStyleMouseRegion(this, this.origin, value, this.height)); }
/** * Copy the current immutable object by setting a value for the {@link JCameraFPSStyleMouseRegionType#height() height} attribute. * A value strict bits equality used to prevent copying of the same value by returning {@code this}. * @param value A new value for height * @return A modified copy of the {@code this} object */ public final JCameraFPSStyleMouseRegion withHeight(double value) { if (Double.doubleToLongBits(this.height) == Double.doubleToLongBits(value)) return this; return validate(new JCameraFPSStyleMouseRegion(this, this.origin, this.width, value)); }