note
	description: "[
		Connects an attack unit to a Eiffel Vision widget.
		]"
	author: "Jimmy J. Johnson"

deferred class
	ATTACK_UNIT_WIDGET

inherit

	VITP_WIDGET
		rename
			target as unit
		redefine
			create_interface_objects,
			extend_widgets,
			build_widgets,
			set_fonts_and_colors,
			position_widgets,
			set_widget_order,
--			build_bounding_figure,
			add_actions,
--			position_sub_widgets,
			set_target,
			paint,
			position_on_figure,
			target_imp
		end

inherit {NONE}

	SILHOUETTE_CONSTANTS
		undefine
			default_create
		end

feature {NONE} -- Initialization

	create_interface_objects
			-- Create attributes
		do
			Precursor {VITP_WIDGET}
			create tile
			create border.make (default_tile_size, default_tile_size, border_size)
			create bonus_circle
			create air_bonus_circle
			create anchor
			create gunnery_factor_mt
			create defense_factor_mt
			create speed_factor_mt
			create air_strike_factor_mt
--			create picture.make_with_coordinates (silhouette_coordinates (unit))
			create picture
		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.
		local
			list: ARRAYED_LIST [EV_COORDINATE]
		do
			Precursor {VITP_WIDGET}
			extend (tile)
			extend (border)
			extend (bonus_circle)
			extend (air_bonus_circle)
			extend (anchor)
			extend (picture)
			text_group.extend (gunnery_factor_mt)
			text_group.extend (defense_factor_mt)
			text_group.extend (speed_factor_mt)
			text_group.extend (air_strike_factor_mt)
				-- Add points to the `tile'
			tile.extend_point (create {EV_COORDINATE}.make (0, 0))
			tile.extend_point (create {EV_COORDINATE}.make (default_tile_size.rounded, 0))
			tile.extend_point (create {EV_COORDINATE}.make (default_tile_size.rounded, default_tile_size.rounded))
			tile.extend_point (create {EV_COORDINATE}.make (0, default_tile_size.rounded))
				-- Add points to `picture'
			list := silhouette_coordinates (unit)
			from list.start
			until list.after
			loop
					-- `extend_point' adds a copy of the point
				picture.extend_point (list.item)
				list.forth
			end
		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 {VITP_WIDGET}
		end

	set_fonts_and_colors
			-- Set the default fonts for any text widgets and set
			-- the default colors for any contained widgets.
		do
			Precursor {VITP_WIDGET}
			name_mt.set_font (Unit_name_font)
			gunnery_factor_mt.set_font (Unit_speed_font)
			defense_factor_mt.set_font (Unit_speed_font)
			speed_factor_mt.set_font (Unit_speed_font)
			air_strike_factor_mt.set_font (unit_air_font)
		end

	position_widgets
			-- Move Current and contained widgets to correct
			-- location and set their sizes
		do
			Precursor {VITP_WIDGET}
--			center_widget_on_other (picture, tile)
			picture.set_x_y (tile.x, tile.y)
				-- Set size and position for circles
			bonus_circle.set_point_a_position (tile.x - 70, tile.y + 30)
			bonus_circle.set_point_b_position (tile.x - 30, tile.y + 70)
			air_bonus_circle.set_point_a_position (tile.x + 45, tile.y - 55)
			air_bonus_circle.set_point_b_position (tile.x + 85, tile.y - 15)
--				-- Center name and defense factor to start
			name_mt.set_text (unit.name)
			name_mt.set_x_y (tile.x, tile.y - 77)
			gunnery_factor_mt.set_x_y (tile.x - 75, tile.y + 70)
			defense_factor_mt.set_x_y (tile.x - 5, tile.y + 70)
			speed_factor_mt.set_x_y (tile.x + 60, tile.y + 70)
			air_strike_factor_mt.set_x_y (tile.x + 65, tile.y - 40)
			anchor.scale (0.5)
			anchor.set_x_y (tile.x - 60, tile.y - 60)
			text_group.scale (0.80)
			scale (0.1)
		end

	set_widget_order
			-- Ensure the widgets are ordered properly
			-- (i.e. whose on top?)
		do
			Precursor {VITP_WIDGET}
			bring_to_front (anchor)
			bring_to_front (text_group)
			bring_to_front (picture)

--			dot.hide
		end

	build_tile
			-- Create the `tile
		do
			tile.extend_point (create {EV_COORDINATE}.make (0, 0))
			tile.extend_point (create {EV_COORDINATE}.make (default_tile_size.rounded, 0))
			tile.extend_point (create {EV_COORDINATE}.make (default_tile_size.rounded, default_tile_size.rounded))
			tile.extend_point (create {EV_COORDINATE}.make (0, default_tile_size.rounded))
		ensure
			correct_point_count: tile.point_count = 4
		end



--	build_bounding_figure
--			-- Add points to the `jj_bounding_figure'.
--			-- Redefined to use the tile edges as the bounding area
--		do
--			if has (jj_bounding_figure) then
--				prune_all (jj_bounding_figure)
--			end
--			create jj_bounding_figure
--			extend (jj_bounding_figure)
--			jj_bounding_figure.extend_point (tile.point_array.item (0))
--			jj_bounding_figure.extend_point (tile.point_array.item (1))
--			jj_bounding_figure.extend_point (tile.point_array.item (2))
--			jj_bounding_figure.extend_point (tile.point_array.item (3))
--			jj_bounding_figure.enable_closed
--			jj_bounding_figure.set_line_width (2)
--			jj_bounding_figure.hide
--		end

	add_actions
			-- Add delayed function calls to the widgets
		do
			Precursor {VITP_WIDGET}
			set_pebble (unit)
			set_accept_cursor ((create {EV_STOCK_PIXMAPS}).hyperlink_cursor)
--			set_pebble_function (agent on_get_pebble)
--			pick_actions.extend (agent on_picked)
--			pointer_button_press_actions.extend (agent on_clicked)
--			tile.pick_actions.extend (agent on_picked (unit, ?, ?))
--			picture.pick_actions.extend (agent on_picked (unit, ?, ?))
		end

	on_clicked (a_x, a_y, button: INTEGER;
				a_x_tilt, a_y_tilt, a_pressure: DOUBLE;
				a_screen_x, a_screen_y: INTEGER)
			-- React to a button click for testing
		do
			io.put_string ("ATTACK_UNIT_WIDGET.on_clicked: on " + unit.name + "%N")
			if attached vitp_world as bw then
--				bw.on_notify_button_pressed (a_unit, a_x, a_y, button)
			end
		end

feature -- Access

	default_tile_size: REAL_64 = 200.0
			-- Size used for building Current, resizing after built

	tile_size: REAL_64
			-- The size of the `tile' after finished scaling Current
		do
			Result := tile.i_th_point_x (2) - tile.i_th_point_x (1)
		end

	color: EV_COLOR
			-- The color of the `tile'
		do
			if attached tile.background_color as c then
				Result := c
			else
				Result := Green
			end
		end

feature -- Element change

	set_target (a_target: like unit)
			-- Set up Current based on `a_target'
		local
			s: REAL_64
			ax, ay, o_set: REAL_64
			t: EV_MODEL_TRANSFORMATION
		do
			Precursor {VITP_WIDGET} (a_target)
			gunnery_factor_mt.set_text (unit.gunnery_factor.out)
			defense_factor_mt.set_text (unit.defense_factor.out)
			speed_factor_mt.set_text (unit.speed_factor.out)
			air_strike_factor_mt.set_text (unit.airstrike_factor.out)
			paint
--			pick_actions.wipe_out
--			pointer_button_press_actions.extend (agent on_clicked (unit, ?,?,?,?,?,?,?,?))
--			pick_actions.extend (agent on_picked (unit, ?, ?))
		end

feature -- Basic operations

	paint
			-- Set the colors for the `tile'
		local
			c: EV_COLOR
			bc: EV_COLOR
			ac: EV_COLOR
		do
			Precursor {VITP_WIDGET}
			c := unit_color
			bc := border_color
			bonus_circle.hide
			air_bonus_circle.hide
			if unit.attack_bonus then
				if unit.airstrike_factor > 0 then
					air_bonus_circle.show
				else
					bonus_circle.show
				end
			end
			if is_flipped then
				ac := adjusted_color (white)
				tile.set_background_color (ac)
				border.set_color (ac)
				if is_flat then
					border.hide
					tile.set_foreground_color (bc)
				else
					border.show
					border.paint
				end
				picture.set_foreground_color (bc)
				picture.set_background_color (bc)
					-- paint the circle even if it is not shown
				bonus_circle.set_foreground_color (bc)
				bonus_circle.remove_background_color
				air_bonus_circle.set_foreground_color (bc)
				air_bonus_circle.remove_background_color
			else
				ac := adjusted_color (c)
				tile.set_background_color (ac)
				border.set_color (ac)
				if is_flat then
					border.hide
				else
					border.show
					border.paint
				end
--				picture.set_color (adjusted_color (Dark_grey))
				picture.set_foreground_color (adjusted_color (Black))
				picture.set_background_color (adjusted_color (Black))
				bonus_circle.set_foreground_color (adjusted_color (White))
				bonus_circle.set_background_color (adjusted_color (White))
				air_bonus_circle.set_foreground_color (adjusted_color (White))
				air_bonus_circle.set_background_color (adjusted_color (White))
			end
			paint_text
				-- Remove the line from around the polygon
			tile.set_line_width (0)
				-- Draw the anchor if the ship is in port
			if game.ports.has (unit.location.name) then
				anchor.show
				anchor.set_foreground_color (adjusted_color (Silver))
				anchor.set_background_color (adjusted_color (Silver))
--			anchor.set_color (Silver)
--			anchor.set_color (Green)
--			anchor.set_color (Yellow)
--			anchor.set_color (Steel)
--			anchor.set_color (Cyan)
--			anchor.set_color (Chrome)
--			anchor.set_color (Bronze)
			else
				anchor.hide
			end
		end

	paint_text
			-- Set the color of the text fields
		local
			c: EV_COLOR
		do
			c := adjusted_color (foreground_color)
			if is_flipped then
				c := adjusted_color (background_color)
				c := adjusted_color (Black)
			else
				c := adjusted_color (Black)
			end
			name_mt.set_foreground_color (c)
			gunnery_factor_mt.set_foreground_color (c)
			defense_factor_mt.set_foreground_color (c)
			speed_factor_mt.set_foreground_color (c)
			air_strike_factor_mt.set_foreground_color (c)
			if unit.attack_bonus and unit.airstrike_factor > 0 then
				air_strike_factor_mt.show
			else
				air_strike_factor_mt.hide
			end
		end

feature -- Status report

	is_flipped: BOOLEAN
			-- Is the back of the tile showing?

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

feature -- Status setting

	flip
			-- Toggle the flip status
		do
			io.put_string ("ATTACK_UNIT_WIDGET.flip_tile %N")
			if is_flipped then
				set_unflipped
			else
				set_flipped
			end
			paint
		end

	set_flipped
			-- Show the back of the tile
		do
			is_flipped := True
			paint
		end

	set_unflipped
			-- Show the front of the tile
		do
			is_flipped := False
			paint
		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 -- Query

	position_on_figure (ax, ay: INTEGER_32): BOOLEAN
			-- Is the point on (`ax', `ay') on this figure?
		do
--			Result := ax >= tile.i_th_point_x (1) and ax <= tile.i_th_point_x (2) and
--						ay >= tile.i_th_point_y (1) and ay <= tile.i_th_point_y (3)
			Result := tile.position_on_figure (ax, ay)
		end

feature {NONE} -- Actions

--	prepick_operations
--			-- Feature called by `on_picked' when a pick-and-put (PNP)
--			-- operation starts (i.e. when a right-click occurs).
--			-- Because the operations must have started in Current, the
--			-- pebble in transport must be `unit' which is from Current.
--		local
--			sop: VITP_SEQUENCE_OF_PLAY
--			s: LINKED_SET [PORT]
--			v: VIEW
--			lin: LINEAR [VIEW]
--		do
--			print ("{ATTACK_UNIT_WIDGET}.prepick_operations %N")
--print ("%T {ATTACK_UNIT_WIDGET}.prepick_operations:  unit = " + unit.name + "%N")
--			sop := game.sequence_of_play
--			if sop.is_reinforcement_step then
--					-- Get ports to which `unit' may move ...
--				s := unit.reinforceable_ports
--						-- ... and add drop agent for the widgets
--				lin := linear_with_set (s)
--print ("%T {ATTACK_UNIT_WIDGET}.prepick_operations:  lin.is_empty = " + lin.is_empty.out + "%N")
--				from lin.start
--				until lin.after
--				loop
--					v := lin.item
--					check attached {PORT_WIDGET} v as pw then
--print ("%T {ATTACK_UNIT_WIDGET}.prepick_operations:  " + pw.generating_type.name + " }.port.name = " + pw.port.name + "%N")
--					end
--print ("%T {ATTACK_UNIT_WIDGET}.prepick_operations:  about to dim view %N")
--					v.set_completely_dimmed
--					if attached {JJ_PICK_AND_DROPABLE} v as d then
--						d.drop_actions.extend (agent on_drop_target)
--					end
----					v.paint    add feature to VIEW; call when dimmed
--					lin.forth
--				end
--			end
--		end

--	on_drop_target (a_unit: ATTACK_UNIT)
--			-- React when `a_unit' is dropped on Current
--		do
--			print ("{ATTACK_UNIT_WIDGET}.on_drop_target %N")
--		end

feature {NONE} -- Implementation

	gunnery_text: STRING
		do
			if attached {AIR_UNIT} unit then
				Result := unit.airstrike_factor.out
			else
				Result := unit.gunnery_factor.out
			end
		end

	defense_text: STRING
		do
			Result := unit.defense_factor.out
		end

	speed_text: STRING
		do
			if attached {AIR_UNIT} unit or attached {SUBMARINE} unit then
				Result := "*"
			else
				Result := unit.speed_factor.out
			end
		end

	air_text: STRING
		do
			if attached {AIR_UNIT} unit then
				Result := ""
			elseif unit.airstrike_factor < 1 then
				Result := ""
			else
				Result := unit.airstrike_factor.out
			end
		end

feature {NONE} -- Implementation

	unit_color: EV_COLOR
			-- Get the color to paint the tile based on the unit's nationality
		do
			if unit.nationality = game.japanese then
				Result := Japanese_unit_color
			elseif unit.nationality = game.us then
				Result := US_unit_color
			elseif unit.nationality = game.british then
				Result := British_unit_color
			elseif unit.nationality = game.dutch or unit.nationality = game.australian then
				Result := Dutch_unit_color
			else
				check
					should_not_happen: False
				end
				Result := Green
			end
		end

	border_color: EV_COLOR
			-- Get the color to paint the boarder of the tile
		do
			if unit.nationality = game.japanese then
				Result := Japanese_unit_boarder_color
			elseif unit.nationality = game.us then
				Result := US_unit_color
			elseif unit.nationality = game.british then
				Result := British_unit_color
			elseif unit.nationality = game.dutch or unit.nationality = game.australian then
				Result := Dutch_unit_color
			else
				check
					should_not_happen: False
				end
				Result := Green
			end
		end

feature {NONE} -- Implementation

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

	tile: EV_MODEL_POLYGON
			-- The square on which to place other widgets

	picture: SILHOUETTE	--EV_MODEL_POLYGON
			-- The polygon (or shape) of the unit on the tile

	bonus_circle: EV_MODEL_ELLIPSE
			-- The circle around the gunnery factor if unit has a bonus

	air_bonus_circle: EV_MODEL_ELLIPSE
			-- The circle around the air factor if unit has a bonus

	border: BORDER
			-- The borders of the tile, giving a raised appearance

	border_size: REAL_64
			-- The size to make the boarder
		do
			Result := Default_tile_size / 10
		end

	anchor: ANCHOR	-- attribute create Result.make (unit) end
			-- Indicates if a unit is in port

	gunnery_factor_mt: EV_MODEL_TEXT
			-- Widget for the `gunnery_factor' of the `unit' text

	defense_factor_mt: EV_MODEL_TEXT
			-- Widget for the `defense_factor' of the `unit' text

	speed_factor_mt: EV_MODEL_TEXT
			-- Widget for the `speed' of the `unit' text

	air_strike_factor_mt: EV_MODEL_TEXT
			-- Widget for the `air_strike_factor' of the `unit' text

end
