note
	description: "[
		Widget representing a {SEA_AREA}.
		]"
	author: "Jimmy J. Johnson"

deferred class
	SEA_AREA_WIDGET

inherit

	LOCATION_WIDGET
		rename
			location as sea_area
		redefine
			create_interface_objects,
--			initialize,
			extend_widgets,
			build_widgets,
			set_fonts_and_colors,
			position_widgets,
			set_widget_order,
			add_actions,
			set_target,
			set_dimming_level,
--			set_font,
			paint,
			on_button_released,
--			on_pointer_motion,
			target_imp
		end

feature {NONE} -- Initialization

	create_interface_objects
			-- Set up the widgets in Current.
		do
			Precursor {LOCATION_WIDGET}
			create ships_to_mt
			create japan_poc_mt
			create allies_poc_mt
			create ocean
			create flag.make (sea_area)
			create saved_point
		end

	extend_widgets
			-- Put widgets into Current.  This cannot be done
			-- in `create_interface_object', because cannot call
			-- `extend' feature until all objects [in descendants]
			-- are also created.
		do
			text_group.extend (ships_to_mt)
			text_group.extend (japan_poc_mt)
			text_group.extend (allies_poc_mt)
			extend (ocean)
			extend (flag)
			Precursor {LOCATION_WIDGET}
		end

	build_widgets
			-- Now that widgets were created in `create_interface_objects'
			-- and added to Current in `extend_widgets', build up the
			-- widgets by adding internal structure to each widget.
		do
			Precursor {LOCATION_WIDGET}
			build_ocean
		end

	set_fonts_and_colors
			-- Set the default fonts for any text widgets and set
			-- the default colors for any contained widgets.
		do
			Precursor {LOCATION_WIDGET}
			background_color := Sea_area_color
			name_mt.set_font (Sea_area_font)
			ships_to_mt.set_font (Subtext_font)
			japan_poc_mt.set_font (POC_font)
			allies_poc_mt.set_font (POC_font)
		end

	position_widgets
			-- Move Current and contained widgets to correct
			-- location and set their their sizes
		local
			px, py: INTEGER
		do
			Precursor {LOCATION_WIDGET}
				-- Use the first point in the sea area as the reference
				-- The sea area was centered on the dot which is at 0, 0
				-- We now need to move the sea area so its reference point (the zero-zero
				-- point that was the starting point measured on the map) is at the
				-- origin instead of the `dot' at the origin.
				-- We can't really physically measure the center point of the ocean
				-- on the game board, because it is determined by the computer; there
				-- is no physical point on the actual game board that marks the center.
			check
				ocean_was_defined: ocean.point_count >= 1
					-- because of the call to `build_ocean'
			end
			px := ocean.i_th_point_x (1)
			py := ocean.i_th_point_y (1)
			translate_widget (Current, -px, -py)
--			translate_widget (flag, 40, 180)
--			translate_widget (text_widget, 20, 150)
		end

	set_widget_order
			-- Ensure the widgets are ordered properly
			-- (i.e. whose on top?)
		do
			Precursor {LOCATION_WIDGET}
				-- Bring all the text to front
--			bring_to_front (name_mt)
--			bring_to_front (ships_to_mt)
--			bring_to_front (japan_poc_mt)
--			bring_to_front (allies_poc_mt)
				-- Set order for other widgets
			bring_to_front (ocean)
			bring_to_front (land)
			bring_to_front (text_group)
			bring_to_front (flag)
			bring_to_front (dot)
		end

	build_ocean
			-- Add defining points to the `ocean'.
		do
		end

	add_actions
			-- Add actions to the port (really just the marker)
		do
			Precursor {LOCATION_WIDGET}
--			drop_actions.extend (agent on_drop_unit)
			flag.pointer_button_press_actions.extend (agent on_flag_pressed)
--			pointer_button_release_actions.extend (agent on_button_released)
--			pointer_motion_actions.extend (agent on_pointer_moved)
			jj_defining_figure.pointer_leave_actions.extend (agent on_leave_bounding_figure)
		end

	position_text
			-- Set the position of the text inside Current
		local
			nh: REAL_64
			n_mid: REAL_64
			x_mid: REAL_64
			ax, ay: INTEGER
			d: REAL_64
			t: EV_MODEL_TRANSFORMATION
		do
			nh := name_mt.height
			ships_to_mt.set_x_y (name_mt.x, name_mt.y + name_mt.height // 2)
			japan_poc_mt.set_y (ships_to_mt.y + ships_to_mt.height)
			allies_poc_mt.set_y (ships_to_mt.y + ships_to_mt.height)

			japan_poc_mt.set_x (name_mt.x - japan_poc_mt.width)
			allies_poc_mt.set_x (name_mt.x + allies_poc_mt.width)
		end

feature -- Element change

	set_target (a_target: like sea_area)
			-- Change the `target'.
		local
			s, s1, s2: STRING
			pos: INTEGER
		do
			Precursor {LOCATION_WIDGET} (a_target)
			flag.set_location (sea_area)
			ships_to_mt.set_text (ships_to_text)
			japan_poc_mt.set_text (japan_poc_text)
			allies_poc_mt.set_text (allies_poc_text)
			position_text
		end

	set_dimming_level (a_level: like dimming_level)
			-- Change the `dimming_level'
		do
			Precursor {LOCATION_WIDGET} (a_level)
			flag.set_dimming_level (a_level)
			paint
		end

--	set_font (a_font: EV_FONT)
--			-- Change the appearance of the `font' from which
--			-- the fonts (height, weight, etc.) of any text
--			-- widgets in Current can be based.
--		local
--			f: EV_FONT
--		do
--			font := a_font
--			name_mt.set_font (font)
--				-- Set the other text models based on `font'
--			f := font.twin
--			f.set_height (f.height // 2)
--			ships_to_mt.set_font (f)
--			f := font.twin
--			f.set_height (f.height // 2)
--			japan_poc_mt.set_font (f)
--			allies_poc_mt.set_font (f)
--		end

feature -- Basic operations

--	on_drop_unit (a_unit: ATTACK_UNIT)
--			-- React to `a_unit' dropped into Current
--		do
--			io.put_string ("SEA_AREA_WIDGET.on_drop_unit:  " + a_unit.name + "  dropped on " + Current.sea_area.name + "%N")
--		end

	restore_flag_position
			-- Reset the `flag' to its default position (i.e. centered on `dot')
		do
			flag.set_x_y (dot.x, dot.y)
		end

	paint
			-- Set the colors for all widgets in Current
		do
			Precursor {LOCATION_WIDGET}
			print ("SEA_AREA_WIDGET.paint:  dimming level = " + dimming_level.out + "%N")
			ships_to_mt.set_foreground_color (adjusted_color (foreground_color))
			japan_poc_mt.set_foreground_color (adjusted_color (foreground_color))
			allies_poc_mt.set_foreground_color (adjusted_color (foreground_color))
			ocean.set_foreground_color (adjusted_color (background_color))
			ocean.set_background_color (adjusted_color (background_color))
			flag.paint
		end

feature {NONE} -- Implementation (actions)

	on_flag_pressed (ax: INTEGER; ay: INTEGER; a_button: INTEGER; x_tilt: DOUBLE; y_tilt: DOUBLE;
								pressure: DOUBLE; a_screen_x: INTEGER; a_screen_y: INTEGER)
				-- The `flag' was pressed
		do
			io.put_string ("SEA_AREA_WIDGET.on_flag_pressed:  button " + a_button.out + " released at (" + ax.out + ", " + ay.out + ") %N")
			is_dragging_flag := True
			saved_point.set (ax, ay)
			enable_capture
		end

	on_button_released (ax: INTEGER; ay: INTEGER; a_button: INTEGER; x_tilt: DOUBLE; y_tilt: DOUBLE;
								pressure: DOUBLE; a_screen_x: INTEGER; a_screen_y: INTEGER)
		local
			c: detachable EV_COORDINATE
		do
			Precursor {LOCATION_WIDGET} (ax, ay, a_button, x_tilt, y_tilt, pressure, a_screen_x, a_screen_y)
			io.put_string ("SEA_AREA_WIDGET.on_button_release:  button " + a_button.out + " released at (" + ax.out + ", " + ay.out + ") %N")
--			Precursor {LOCATION_WIDGET} (ax, ay, a_button, x_tilt, y_tilt, pressure, a_screen_x, a_screen_y)
			if is_dragging_flag then
				create c.make (ax, ay)
--				flag.center_on_point (c)
				flag.set_x_y (c.x, c.y)
				disable_capture
			end
			is_dragging_flag := False
		end

	on_leave_bounding_figure
			-- If pointer leaves `jj_bounding_figure', then release the capture and drop the flag
		do
			if is_dragging_flag then
--				flag.center_on_point (saved_point)
				flag.set_x_y (saved_point.x, saved_point.y)
				disable_capture
			end
			is_dragging_flag := False
		end

	on_pointer_motion (ax: INTEGER; ay: INTEGER; x_tilt: DOUBLE; y_tilt: DOUBLE;
						pressure: DOUBLE; a_screen_x: INTEGER; a_screen_y: INTEGER)
			-- The pointer is moving
		do
--			Precursor {LOCATION_WIDGET} (ax, ay, x_tilt, y_tilt, pressure, a_screen_x, a_screen_y)
			saved_point.set (ax, ay)
			if is_dragging_flag then
--				flag.center_on_point (saved_point)
				flag.set_x_y (saved_point.x, saved_point.y)
			end
		end

feature {NONE} -- Implementation

	ships_to_text: STRING
			-- The line of text that is under the sea area name, begining with "Ships to..."
		do
			Result := "Ships to (if friendly) " + adjacent_ports_text + " or any RED port"
				-- Look up the list of adjacent ports in the game graph?
		end

	japan_poc_text: STRING
			-- The line of text for the Japanese POC
		do
			Result := "JAPAN  " +  sea_area.japanese_poc.out + "  POC"
		end

	allies_poc_text: STRING
			-- The line of text for the Japanese POC
		do
			Result := "ALLIES  " +  sea_area.allied_poc.out + "  POC"
		end

	adjacent_ports_text: STRING
			-- The names of green ports touching this sea area.
		local
			lin: LINEAR [PORT]
			i, n: INTEGER
			p: PORT
		do
			Result := ""
			lin := sea_area.adjoining_ports
				-- Count green ports
			from lin.start
			until lin.exhausted
			loop
				if attached {GREEN_PORT} lin.item then
					n := n + 1
				end
				lin.forth
			end
			from lin.start
			until lin.exhausted
			loop
				p := lin.item
				lin.forth
				if attached {GREEN_PORT} p then
					Result := Result + p.name
					i := i + 1
					if n >= 3 and i < n then
						Result := Result + ", "
					elseif n >= 2 and i < n then
						Result := Result + " or "
					end
				end
			end
		end

feature {NONE} -- Implementation

	ships_to_mt: EV_MODEL_TEXT
			-- Displays the "Ships to ..." text

	japan_poc_mt: EV_MODEL_TEXT
			-- Displays the Japanese poc text

	allies_poc_mt: EV_MODEL_TEXT
			-- Displays the Allies poc text

	ocean: EV_MODEL_POLYGON
			-- To draw the blue sea

	flag: FLAG
			-- The control marker for this sea area

	is_dragging_flag: BOOLEAN
			-- Is the `flag' being moved?

	saved_point: EV_COORDINATE
			-- The location withing Currenty to where the mouse last moved

	target_imp: detachable SEA_AREA
			-- Anchor for the `target' represented by Current

invariant

end
