Class: RetroRender::Dialbox

Inherits:
Object
  • Object
show all
Defined in:
lib/dialbox.rb

Instance Method Summary collapse

Constructor Details

#initialize(window, opts = {}) ⇒ Dialbox

Returns a new instance of Dialbox.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/dialbox.rb', line 3

def initialize(window, opts = {})
	pointor_size = opts.has_key?(:pointor_size) ? opts[:pointor_size] : [24, 36]
	dialbox_size = opts.has_key?(:dialbox_size) ? opts[:dialbox_size] : [12, 12]
	font_size = opts.has_key?(:font_size) ? opts[:font_size] : [16, 16]	
	# setup
	@step_duration = opts.has_key?(:step_duration) ? opts[:step_duration] : 5 # milliseconds
	@pointor_duration = opts.has_key?(:pointor_duration) ? opts[:pointor_duration] : 200 # milliseconds
	@max_lines = opts.has_key?(:max_lines) ? opts[:max_lines] : 4
	@padding = opts.has_key?(:padding) ? opts[:padding] : 24
	@space = opts.has_key?(:space) ? opts[:space] : 16
	@line_jump = opts.has_key?(:line_jump) ? opts[:line_jump] : font_size[1]	
	@window = window
	@gfx = {
		:pointor => Gosu::Image.load_tiles('gfx/dialbox/pointor.png', pointor_size[0], pointor_size[1], :retro=>true)
	}
	@dialbox_window = Gosu::Image.load_tiles('gfx/dialbox/base.png', dialbox_size[0], dialbox_size[1], :retro=>true)
	@font = Gosu::Image.load_tiles('gfx/dialbox/font.png', font_size[0], font_size[1], :retro=>true)
	index = 0
	signs = ['!', '?', ':', '/', '-', '.', ',', "'"]
	@letters = Hash.new
	((('a'..'z').to_a) + ('A'..'Z').to_a + ('0'..'9').to_a + signs).each do |letter|
		@letters[letter] = index
		index += 1
	end		
	@done = true
	@step = 0
	@step_tick = Gosu::milliseconds
	@pointor_frame = 0
	@pointor_tick = Gosu::milliseconds
end

Instance Method Details

#button_down(id) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/dialbox.rb', line 38

def button_down(id)
	if id == Gosu::KbReturn
		if (@current_line == @first_line + @max_lines - 1 or @current_line == @lines_to_display.size - 1) and @step == @lines_to_display[@current_line].size
			# the total rows were displayed, we change
			@first_line += @max_lines
			@current_line += 1
			@step = 0
		else
			# let's force the three lines... immediate Display
			if @first_line + @max_lines - 1 < @lines_to_display.size - 1
				@current_line = @first_line + @max_lines - 1
			else
				@current_line = @lines_to_display.size - 1
			end
			@step = @lines_to_display[@current_line].size
		end
	end
end

#cut_text(text) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/dialbox.rb', line 70

def cut_text(text)
	@lines_to_display = Array.new
	@lines_to_display.push ""
	text.split(/ /).each do |word|
		if pixel_length(@lines_to_display.last) + pixel_length(word) <= @window.width - 2 * @padding
			@lines_to_display[@lines_to_display.size - 1] += word + " "
			if word.include?("\n")
				@lines_to_display.push ""
			end
		else
			@lines_to_display.push word + " "
		end
	end
end

#done?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/dialbox.rb', line 34

def done?
	return @done
end

#draw(x = 0.0, y = 0.0, z = 0.0) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dialbox.rb', line 93

def draw(x = 0.0, y = 0.0, z = 0.0)
	# DIALBOX window
	content_height = ((@max_lines - 1) * @line_jump + @max_lines * @font.first.height).to_f + @padding * 2 - @dialbox_window[1].height - @dialbox_window[7].height
	@dialbox_window[0].draw(x, y, z) # top left corner
	@dialbox_window[1].draw(x + @dialbox_window[0].width, y, z, (@window.width - 2 * @dialbox_window[0].width).to_f / @dialbox_window[1].width, 1) # top border
	@dialbox_window[2].draw(@window.width - @dialbox_window[2].width, y, z) # top right corner
	@dialbox_window[3].draw(x, y + @dialbox_window[0].height, z, 1, (content_height / @dialbox_window[3].height)) # left border
	@dialbox_window[4].draw(x + @dialbox_window[3].width, y + @dialbox_window[1].height, z, (@window.width - @dialbox_window[3].width - @dialbox_window[5].width).to_f / @dialbox_window[4].width, content_height / @dialbox_window[4].height) # main fill
	@dialbox_window[5].draw(@window.width - @dialbox_window[5].width, y + @dialbox_window[1].height, z, 1, (content_height / @dialbox_window[5].height)) # right border
	@dialbox_window[6].draw(x, y + content_height + @dialbox_window[1].height, z) # bottom left corner
	@dialbox_window[7].draw(x + @dialbox_window[6].width, y + content_height + @dialbox_window[1].height, z, (@window.width - @dialbox_window[3].width - @dialbox_window[5].width).to_f / @dialbox_window[7].width, 1) # bottom border
	@dialbox_window[8].draw(@window.width - @dialbox_window[8].width, y + content_height + @dialbox_window[1].height, z) # bottom right corner
	
	# TEXT
	x, y = @padding, @padding
	for line in @first_line...@first_line + @max_lines
		if line < @lines_to_display.size
			if line <= @current_line
				text_to_draw = @lines_to_display[line]
				if line == @current_line
					text_to_draw = text_to_draw.slice(0, @step)
				end
				text_to_draw.split(' ').each do |word|
					draw_word(word, x, y, z)
					x += pixel_length(word) + @space
				end
				x = @padding; y += @line_jump + @font.first.height
			end
		end
	end

	if @current_line <= @lines_to_display.size - 1
		# text step handling
		if Gosu::milliseconds - @step_tick >= @step_duration			
			if @step >= @lines_to_display[@current_line].size and @current_line + 1 < @first_line + @max_lines
				@step = 0; @current_line += 1
				if @current_line > @lines_to_display.size - 1
					@current_line = @lines_to_display.size - 1 
					@step = @lines_to_display.last.size
				end
			else
				@step_tick = Gosu::milliseconds
				@step += 1 if @step < @lines_to_display[@current_line].size
			end
		end		
		# pointor drawing
		if @current_line <= @lines_to_display.size - 1
			if @step == @lines_to_display[@current_line].size and (@current_line == @first_line + @max_lines - 1 or @current_line == @lines_to_display.size - 1)
				if Gosu::milliseconds - @pointor_tick >= @pointor_duration
					@pointor_frame = (@pointor_frame == 0 ? 1 : 0)
					@pointor_tick = Gosu::milliseconds
				end			
				@gfx[:pointor][@pointor_frame].draw(@window.width - @dialbox_window[8].width * 3, content_height - 12, 0)
			end
		end
	else
		@done = true
	end
end

#draw_word(text, x, y, z) ⇒ Object



85
86
87
88
89
90
91
# File 'lib/dialbox.rb', line 85

def draw_word(text, x, y, z)
	text.split('').each do |letter|
		raise("the letter \"#{letter}\" doesn't exist in this Font") if !@letters.has_key?(letter)
		@font[@letters[letter]].draw(x, y, z)
		x += @font.first.width
	end		
end

#pixel_length(word) ⇒ Object



66
67
68
# File 'lib/dialbox.rb', line 66

def pixel_length(word)
	return word.size * 16
end

#set_text(text) ⇒ Object



57
58
59
60
61
62
63
64
# File 'lib/dialbox.rb', line 57

def set_text(text)
	@done = false
	@step = 0
	@first_line = 0
	@current_line = 0
	@step_tick = Gosu::milliseconds		
	cut_text(text)
end