note
	description: "[
		A base class for building the Japanese or US flag
		]"
	author: "Jimmy J. Johnson"

class
	FLAG

inherit

	FONT_AND_COLOR_CONSTANTS
		undefine
			default_create
		end

	DIMABLE
		redefine
			default_create
		end

	EV_MODEL_WORLD
		redefine
			default_create,
			create_interface_objects
		end

create
	make

create {FLAG}
	list_make

feature {NONE} -- Initialization

	default_create
			-- Set up Current
		do
			Precursor {DIMABLE}
			Precursor {EV_MODEL_WORLD}
			build_widgets
		end

	make (a_location: like location)
			-- Set up Current
		do
			location_imp := a_location
			default_create
		end

	create_interface_objects
			-- Create attributes
		do
			Precursor {EV_MODEL_WORLD}
			create field
			create border.make (width, height, border_size)
			background_coordinates := [create {EV_COORDINATE}, create {EV_COORDINATE}]
			create background
			create charge
			create rising_sun
			create canton
			create canton_imp
			create stars.make
		end

	build_widgets
			-- Create the widget and associate it with `a_location'
		do
			build_field
			build_us_charge
			build_japanese_charge
			build_canton
			build_border
			scale (0.07)
		end

	build_border
			-- Add the border on top of other widgets in Current
		do
			extend (border)
		end

	build_field
			-- Create the flags background
		local
			p1, p2: EV_COORDINATE
		do
			create field
			extend (field)
--			create p1.make_precise (0.0, 0.0)
--			create p2.make_precise (width, height)
			create p1.make_precise (border_size, border_size)
			create p2.make_precise (width - border_size, height - border_size)
			create background.make_with_points (p1, p2)
				-- Save the points
			background_coordinates := [p1, p2]
			field.extend (background)
		end

	build_us_charge
			-- Build the `charge', sizing it relative to the `background' rectangle.
			-- Create the red stripes.  (The white is background.)
		local
			i: INTEGER
			x1, y1, x2, y2: REAL_64
			h: REAL_64
			stripe: EV_MODEL_POLYGON
		do
			create charge
			field.extend (charge)
			x1 := background_coordinates.point_1.x + 2
			y1 := background_coordinates.point_1.y + 2
			x2 := background_coordinates.point_2.x - 2
			y2 := background_coordinates.point_2.y - 2
			h := (y2 - y1) / 13
			from i := 1
			until i > 13
			loop
				if i \\ 2 = 1 then
					create stripe
					stripe.extend_point (create {EV_COORDINATE}.make_precise (x1, y1 + (i - 1) * h))
					stripe.extend_point (create {EV_COORDINATE}.make_precise (x2, y1 + (i - 1) * h))
					stripe.extend_point (create {EV_COORDINATE}.make_precise (x2, y1 + i * h))
					stripe.extend_point (create {EV_COORDINATE}.make_precise (x1, y1 + i * h))
					charge.extend (stripe)
				end
				i := i + 1
			end
		end

	build_japanese_charge
			-- Create the sun and rays for the Japanese flag
		local
			p1: EV_MODEL_POLYGON
			os: REAL_64
			clipper: POLYGON_CLIPPER
			ang, ang_step: REAL_64
			mid: EV_COORDINATE
			ray: EV_MODEL_POLYGON
		do
			build_rising_sun
			rising_sun.scale_x (1.8)
			rising_sun.scale_y (1.4)
			field.extend (rising_sun)
				-- Must move the charge a bit to line up with the background
			rising_sun.set_x_y (165, 100)
--			center_widget_on_other (rising_sun, field)
--			create japanese_charge
--			field.extend (japanese_charge)
--				-- Build the `japanese_charge' inside a square so the angles at which the rays
--				-- radiate from the sun can be uniformly spaced around a circle.  After
--				-- the rays are build the `charge' is scaled to match the flag's `width'
--				-- and `height'.
--				-- Create a polygon slightly smaller than the `field'
--			os := width / 10.0
--			create p1
--			p1.extend_point (create {EV_COORDINATE}.make_precise (os, os))
--			p1.extend_point (create {EV_COORDINATE}.make_precise (width - os, os))
--			p1.extend_point (create {EV_COORDINATE}.make_precise (width - os, width - os))
--			p1.extend_point (create {EV_COORDINATE}.make_precise (os, width - os))
--				-- Each of the 16 "rays" immenating from the sun is built as a triangle centered
--				-- at the midpoint, extending beyond the boundaries of `p1'.  Each ray is then
--				-- clipped against `p1'
--			create clipper
--			ang := (2.0 * Pi) / 16.0
--			create mid.make_precise (width / 2.0, width / 2.0)
--			from i := 1
--			until i > 16
--			loop
--				create ray.default_create
--				ray.extend_point (mid)
--				ax := delta_x (ang * i, width)
--				ay := delta_y (ang * i, width)
--				ray.extend_point (create {EV_COORDINATE}.make_precise (ax, ay))
--				ax := delta_x (ang * (i + 1), width)
--				ay := delta_y (ang * (i + 1), width)
--				ray.extend_point (create {EV_COORDINATE}.make_precise (ax, ay))
--				ray := clipper.clipped (ray, p1)
--				field.extend (ray)
--				i := i + 1
--			end
		end

--	build_canton
--			-- Create the union on US flag.
--			-- This contains the [50] stars (should be 48).
--		local
--			p1, p2: EV_COORDINATE
--			s: STAR
--			i, j: INTEGER
--			a, b: REAL_64
--			c, d: REAL_64
--			e, f, g, h: REAL_64
--			k: REAL_64
--			ax, ay: REAL_64
--			t: EV_MODEL_TRANSFORMATION
--		do
--			create t.make_zero
--			create canton
--			field.extend (canton)
--			a := height
--			b := width
--			c := a * 7/13
--			d := b * 2/5
--			e := c / 10
--			f := c / 10
--			g := d / 12
--			h := d / 12
--			k := a * 0.0616
--			create p1.make_precise (0.0, 0.0)
--			create p2.make_precise (d, c)
----			create canton_imp.make_with_points (p1, p2)
----			create canton_imp
--			canton_imp.extend_point (create {EV_COORDINATE}.make_precise (0.0, 0.0))
--			canton_imp.extend_point (create {EV_COORDINATE}.make_precise (d, 0.0))
--			canton_imp.extend_point (create {EV_COORDINATE}.make_precise (d, c))
--			canton_imp.extend_point (create {EV_COORDINATE}.make_precise (0.0, c))
--			canton.extend (canton_imp)
--				-- A star is created, scaled and then its center point is moved to
--				-- (0, 0).  From there the star is positioned based on the algorithm.
--				-- Add the 6 x 5 set of stars
--			from i := 1
--			until i > 6
--			loop
--				ax := (i - 1) * (2 * h) + g
--				from j := 1
--				until j > 5
--				loop
--					create s
----					s.set_target (location)
--					s.scale (0.0616)
--					canton.extend (s)
--					stars.extend (s)
--					t.translate (-s.dot.point.x, -s.dot.point.y)
--					s.transform (t)
--					ay := (j - 1) * (2 * f) + e
--					t.translate (ax, ay)
--					s.transform (t)
--					j := j + 1
--				end
--				i := i + 1
--			end
--				-- Add the remaining stars, the 5 x 4 set
--			from i := 1
--			until i > 5
--			loop
--				ax := i * (2 * h) - h + g
--				from j := 1
--				until j > 4
--				loop
--					create s
----					s.set_target (location)
--					s.scale (0.0616)
--					canton.extend (s)
--					t.translate (-s.dot.point.x, -s.dot.point.y)
--					s.transform (t)
--					ay := j * (2 * f) - f + e
--					t.translate (ax, ay)
--					s.transform (t)
--					j := j + 1
--				end
--				i := i + 1
--			end
--		end

	build_canton
			-- Create the 48-star union on US flag.
			-- The dimensions were estimated based on the 50-star version.
		local
--			p1, p2: EV_COORDINATE
			s: STAR
			i, j: INTEGER
			a, b: REAL_64
			c, d: REAL_64
			e, f, g, h: REAL_64
			k: REAL_64
			ax, ay: REAL_64
			marg: REAL_64	-- margin to account for boarder
			t: EV_MODEL_TRANSFORMATION
			x1, y1, x2, y2: REAL_64
		do
			create t.make_zero
			create canton
			field.extend (canton)
			x1 := background_coordinates.point_1.x + 2
			y1 := background_coordinates.point_1.y + 2
			x2 := background_coordinates.point_2.x - 2
			y2 := background_coordinates.point_2.y - 2

--			a := height - 2 * border_size
--			b := width - 2 * border_size
			a := y2 - y1
			b := x2 - x1
			c := a * 7/13
			d := b * 2/5
			e := c / 7		-- divided by number of rows + 1
			f := c / 7		-- divided by number of rows + 1
			g := d / 9		-- divided by number of columns + 1
			h := d / 9		-- divided by number of columns + 1
			k := a * 0.0616
--			create p1.make_precise (0.0, 0.0)
--			create p2.make_precise (d, c)
				-- Create the blue background for the stars
			canton_imp.extend_point (create {EV_COORDINATE}.make_precise (x1, y1))
			canton_imp.extend_point (create {EV_COORDINATE}.make_precise (x1 + d, y1))
			canton_imp.extend_point (create {EV_COORDINATE}.make_precise (x1 + d, y1 + c))
			canton_imp.extend_point (create {EV_COORDINATE}.make_precise (x1, y1 + c))
			canton.extend (canton_imp)
				-- A star is created, scaled and then its center point is moved to
				-- (0, 0).  From there the star is positioned based on the algorithm.
				-- Add the 6 x 5 set of stars
			from i := 1
			until i > 8
			loop
--				ax := (i - 1) * (2 * h) + g
--				ax := (i - 1) * h + g/2
				ax := x1 + (i - 1) * h + g
				from j := 1
				until j > 6
				loop
					create s
					s.scale (0.0616)
					canton.extend (s)
					stars.extend (s)
--					t.translate (-s.dot.point.x, -s.dot.point.y)
--					s.transform (t)
					s.set_x_y (0, 0)
--					ay := (j - 1) * (2 * f) + e
--					ay := (j - 1) * f + e/2
					ay := y1 + (j - 1) * f + e
					t.translate (ax, ay)
					s.transform (t)
					j := j + 1
				end
				i := i + 1
			end
		end

feature -- Access

	location: LOCATION
			-- The location (e.g. a PORT or a chart) to which
			-- this marker belongs
		require
			has_location: has_location
		do
			check attached location_imp as loc then
				Result := loc
			end
		end

	set_location (a_location: like location)
			-- Change the `location'
		do
			location_imp := a_location
		end

feature {NONE} -- Implementation

	location_imp: detachable like location
			-- Implementation for `location'

feature -- Status report

	has_location: BOOLEAN
			-- Has a location been assigned?
		do
			Result := attached location_imp
		end

	is_outlined: BOOLEAN
			-- Should the outline of the flag show?
			-- If yes, change the foreground color to show a line around Current.

	is_flat: BOOLEAN
			-- Should Current be drawn as a flat picture?

feature -- Status setting

	show_outline
				-- Make the outline of the flag visible
		do
			is_outlined := true
		end

	hide_outline
				-- Make the outline of the flag NOT visible
		do
			is_outlined := false
		end

	set_flat
			-- Cause the widget to be painted as a flat tile
		do
			is_flat := True
			paint
		end

	set_raised
			-- Cause the widget to be painted as a 3-d tile
		do
			is_flat := False
			paint
		end

feature -- Basic operations

--	set_dimming_level (a_level: INTEGER)
--			-- Change the `dimming_level'
--		do
--			Precursor {DIMABLE} (a_level)
--			paint
--		end

	paint
			-- Set the colors of the sub-widgets
		do
			paint_field
			paint_charge
			paint_canton
			paint_border
			if is_flat then
				border.hide
			else
				border.show
			end
			if location.is_allied then
				field.show
				charge.show
				canton.show
				rising_sun.hide
			elseif location.nationality = {NATIONALITY_CONSTANTS}.japanese then
				field.show
				rising_sun.show
				charge.hide
				canton.hide
			else
				field.hide
				charge.hide
				canton.hide
				rising_sun.hide
				border.hide
			end
		end

feature {NONE} -- Implementation

	paint_border
			-- Set the color for the `border'
		do
			border.set_color (adjusted_color (White))
		end

	paint_field
			-- Paint the `field'
		local
			c: EV_COLOR
		do
			c := us_flag_white_color
			background.set_background_color (adjusted_color (c))
				-- Set color for boarder around the background if shown.
				-- This is just the line around the flag on the OA Charts.
			if is_outlined then
				if location.nationality = {NATIONALITY_CONSTANTS}.japanese then
					c := japanese_unit_color
				else
					c := us_unit_color
				end
			end
			background.set_foreground_color (adjusted_color (c))
		end

	paint_charge
			-- Paint the `charge'
		do
			from charge.start
			until charge.exhausted
			loop
				if attached {EV_MODEL_POLYGON} charge.item as s then
					s.set_foreground_color (adjusted_color (US_flag_red_color))
					s.set_background_color (adjusted_color (US_flag_red_color))
				end
				rising_sun.set_foreground_color (adjusted_color (Japanese_unit_color))
				rising_sun.set_background_color (adjusted_color (Japanese_unit_color))
				charge.forth
			end
		end

	paint_canton
			-- Paint the canton area (and stars)
		local
			s: STAR
		do
			canton_imp.set_background_color (adjusted_color (US_flag_blue_color))
			canton_imp.set_foreground_color (adjusted_color (US_flag_blue_color))
				-- Must paint each star (to dim or brighten them)
			from stars.start
			until stars.off
			loop
				s := stars.item
				s.set_background_color (adjusted_color (white))
				s.set_foreground_color (adjusted_color (white))
				stars.forth
			end
		end

feature {NONE} -- Implementation

	frozen height: REAL_64 = 200.0
			-- The span of a flag down the side parallel to the flagpole

--	width: REAL_64 = 380.0
	width: REAL_64 = 330.0
			-- The span of the flag along the side at right angles to the flagpole

	ratio: REAL_64
			-- The ratio of the `width' / `height' (1.9 for US flags)

	background_coordinates: TUPLE [point_1: EV_COORDINATE; point_2: EV_COORDINATE]
			-- The corner points for the `background'.
			-- Used EV_COORDINATE to be more precise (i.e. REAL_64, not INTEGER_32)

	background: EV_MODEL_RECTANGLE
			-- The background of Current (usually white)

	border: BORDER
			-- The borders around the `field', giving a raised appearance

	border_size: REAL_64
			-- Ratio of the size of the `border' to `width'
		do
			Result:= (height.min (width)) / 20
		end

	canton: EV_MODEL_WORLD
			-- The upper hoist (left) quarter (contains the field of stars in US flag)

	charge: EV_MODEL_WORLD
			-- A figure or symbol appearing on the flag

	rising_sun: EV_MODEL_POLYGON
			-- The red sun and rays on the Japanese flag

	field: EV_MODEL_WORLD
			-- The top container for Current

	canton_imp: EV_MODEL_POLYGON
			-- The rectangle to make a blue canton

	stars: LINKED_SET [STAR]
			-- Keep track of all the stars in Current, so they can be dimmed
			-- when `set_dimming_level' is called

	build_rising_sun
			-- Make the polygon
		do
			create rising_sun
				-- Points generated by Grabber application
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0, 0))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (32.395061728395063, 39.308641975308639))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (35.358024691358025, 37.728395061728392))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (38.913580246913575, 35.55555555555555))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (20.938271604938265, 1.7777777777777786))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (33.777777777777771, 1.7777777777777786))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (43.851851851851848, 33.777777777777764))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (46.617283950617285, 33.58024691358024))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (49.97530864197531, 32.79012345679012))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (47.407407407407412, 1.5802469135802468))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (58.66666666666665, 1.9753086419753103))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (56.888888888888879, 33.185185185185176))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (59.851851851851841, 32.987654320987652))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (64.395061728395063, 33.975308641975303))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (74.074074074074048, 1.3827160493827169))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (87.111111111111114, 1.7777777777777786))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (69.925925925925895, 35.950617283950614))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (72.296296296296276, 37.92592592592591))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (74.271604938271594, 40.296296296296291))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (107.25925925925924, 0.98765432098765338))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (130.17283950617283, 1.1851851851851851))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (78.81481481481481, 44.444444444444436))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (80.790123456790099, 47.012345679012341))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (82.370370370370352, 50.962962962962955))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (168.09876543209876, 1.3827160493827169))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (167.90123456790118, 30.024691358024683))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (85.333333333333314, 55.506172839506164))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (85.925925925925895, 58.074074074074055))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (85.925925925925895, 62.222222222222207))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (167.90123456790118, 54.716049382716044))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (168.09876543209876, 76.246913580246883))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (85.925925925925895, 68.938271604938251))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (85.53086419753086, 72.098765432098759))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (85.135802469135797, 75.851851851851848))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (167.3086419753086, 100.34567901234567))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (166.71604938271602, 130.56790123456787))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (83.160493827160479, 80.592592592592581))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (81.382716049382708, 83.160493827160479))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (79.407407407407391, 85.728395061728378))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (130.56790123456787, 128.59259259259255))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (107.06172839506172, 129.38271604938268))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (75.259259259259238, 89.679012345679013))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (72.691358024691368, 92.049382716049365))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (69.53086419753086, 94.024691358024654))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (87.308641975308632, 128.98765432098762))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (74.074074074074048, 129.18518518518516))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (64.592592592592581, 96.395061728395035))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (61.432098765432094, 97.185185185185162))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (57.283950617283942, 97.185185185185162))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (60.049382716049372, 128.79012345679007))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (47.999999999999993, 128.79012345679007))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (50.37037037037036, 97.382716049382708))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (47.407407407407412, 97.185185185185162))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (44.049382716049372, 95.999999999999972))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (34.172839506172842, 128.98765432098762))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (19.950617283950621, 129.18518518518516))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (38.716049382716044, 94.024691358024654))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (35.358024691358025, 92.839506172839464))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (32.592592592592588, 90.469135802469111))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0.39506172839506704, 128.59259259259255))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0.39506172839506704, 109.43209876543207))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (28.839506172839513, 85.925925925925895))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (26.074074074074076, 83.358024691357997))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (24.493827160493822, 79.999999999999972))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0.19753086419753174, 93.03703703703701))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0.19753086419753174, 82.172839506172807))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (21.728395061728399, 75.061728395061692))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (21.728395061728399, 71.703703703703695))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (21.925925925925924, 68.34567901234567))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0.39506172839506704, 70.123456790123441))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0.39506172839506704, 61.234567901234563))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (21.728395061728399, 62.617283950617271))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (21.333333333333336, 59.061728395061728))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (22.320987654320987, 56.493827160493829))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0.19753086419753174, 49.185185185185176))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0.39506172839506704, 37.530864197530853))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (24.691358024691361, 50.567901234567891))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (26.271604938271594, 47.012345679012341))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (28.444444444444436, 44.049382716049379))
			rising_sun.extend_point (create {EV_COORDINATE}.make_precise (0, 20.938271604938272))
		end

end
