PageRenderTime 13ms CodeModel.GetById 9ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/tutorial/vision/widget/window_palette2.e

http://github.com/tybor/Liberty
Specman e | 130 lines | 109 code | 13 blank | 8 comment | 5 complexity | fd8f365e7a540798b3d22562a567a902 MD5 | raw file
  1class WINDOW_PALETTE2
  2	-- Draw color palette on sub_window.
  3	-- Mouse wheel is used to change blue value.
  4	-- Update is done line by line (color line) which avoid the full black window.
  5	-- window and is more responsive to multiple color changes.
  6
  7inherit
  8	GRAPHIC
  9
 10creation {ANY}
 11	make
 12
 13feature {}
 14	sub_window: SUB_WINDOW
 15
 16	make is
 17		local
 18			toplevel_window: TOPLEVEL_WINDOW
 19		do
 20			create toplevel_window.default_create
 21			toplevel_window.when_close_requested(agent vision.loop_stack.break)
 22			toplevel_window.set_background_color(black_color)
 23			toplevel_window.map
 24			toplevel_window.set_shrink(True)
 25			create draw_kit
 26			create sub_window.make(toplevel_window)
 27			sub_window.set_shrink(True)
 28			sub_window.set_expand(True)
 29			sub_window.map
 30			sub_window.when_wheel_up(agent shift(sub_window, 25))
 31			sub_window.when_wheel_down(agent shift(sub_window, -25))
 32			sub_window.when_expose(agent update(sub_window))
 33			progressive_color_refresh := 255
 34			vision.start
 35		end
 36
 37	update (w: WINDOW) is
 38		do
 39			draw_palette(w, blue)
 40		end
 41
 42	progressive_color_refresh: INTEGER
 43
 44	progressive_update (w: WINDOW) is
 45		do
 46			w.clear_area(0,
 47							 (progressive_color_refresh * w.height / 256).floor.force_to_integer_32,
 48							 w.width,
 49							 (w.height / 256).ceiling.force_to_integer_32)
 50		end
 51
 52	blue: INTEGER
 53
 54	shift (w: WINDOW; offset: INTEGER) is
 55		local
 56			blue_value: INTEGER
 57		do
 58			blue_value := blue + offset
 59			if blue_value > 255 then
 60				blue_value := 255
 61			elseif blue_value < 0 then
 62				blue_value := 0
 63			end
 64			if blue_value /= blue then
 65				blue := blue_value
 66				progressive_color_refresh := 0
 67				progressive_update(w)
 68			end
 69		end
 70
 71	draw_kit: DRAW_KIT
 72
 73	draw_palette (w: WINDOW; blue_value: INTEGER) is
 74		require
 75			w /= Void
 76			blue_value.in_range(0, 255)
 77		local
 78			red, green: INTEGER; max_red, max_green: INTEGER; color: COLOR; x, y: INTEGER
 79			width, height: INTEGER; area: RECT
 80		do
 81			draw_kit.set_drawable(w)
 82			area := vision.expose_area.intersect(w.area)
 83			if not area.is_empty then
 84				width := (w.width / 256).ceiling.force_to_integer_32
 85				height := (w.height / 256).ceiling.force_to_integer_32
 86				from
 87					red := scale_from_pos(area.y, w.height, 256)
 88					max_red := scale_from_pos(area.y + area.height - 1, w.height, 256)
 89					max_green := scale_from_pos(area.x + area.width - 1, w.width, 256)
 90				until
 91					red > max_red
 92				loop
 93					y := (w.height * red / 256).floor.force_to_integer_32
 94					from
 95						green := scale_from_pos(area.x, w.width, 256)
 96					until
 97						green > max_green
 98					loop
 99						x := (w.width * green / 256).floor.force_to_integer_32
100						create color.like_rgb_8(red, green, blue_value)
101						draw_kit.set_color(color)
102						draw_kit.fill_rectangle(x, y, width, height)
103						green := green + 1
104					end
105					red := red + 1
106				end
107				if progressive_color_refresh < 255 then
108					progressive_color_refresh := progressive_color_refresh + 1
109					progressive_update(w)
110				end
111			end
112		end
113
114	scale_from_pos (pos, nb_pos, nb_scale: INTEGER): INTEGER is
115			-- Search nearest inferior scale. Scale position can be
116			-- computed like this: ((scale*nb_pos)/nb_scale).floor
117			-- This is equivalent to (scale * nb_pos) // nb_scale
118			-- scale range from 0 to nb_scale-1
119		require
120			pos.in_range(0, nb_pos - 1)
121		do
122			Result := (pos * nb_scale / nb_pos).floor.force_to_integer_32
123			if ((Result + 1) * nb_pos / nb_scale).floor <= pos then
124				Result := Result + 1
125			end
126		ensure
127			Result.in_range(0, nb_scale - 1)
128		end
129
130end -- class WINDOW_PALETTE2