Added support for Label text alignement, improved EditBox to correctly position caret and support mouse caret positioning, added debugging Container boundry (WIP)

This commit is contained in:
2020-09-09 09:51:18 -05:00
parent b06ceaabce
commit 695c77b183
4 changed files with 81 additions and 23 deletions

View File

@@ -48,6 +48,31 @@ module CyberarmEngine
Gosu.clip_to(@x, @y, width, height) do Gosu.clip_to(@x, @y, width, height) do
@children.each(&:draw) @children.each(&:draw)
end end
if false#DEBUG
Gosu.flush
Gosu.draw_line(
self.x, self.y, Gosu::Color::RED,
self.x + outer_width, self.y, Gosu::Color::RED,
Float::INFINITY
)
Gosu.draw_line(
self.x + outer_width, self.y, Gosu::Color::RED,
self.x + outer_width, self.y + outer_height, Gosu::Color::RED,
Float::INFINITY
)
Gosu.draw_line(
self.x + outer_width, self.y + outer_height, Gosu::Color::RED,
self.x, self.y + outer_height, Gosu::Color::RED,
Float::INFINITY
)
Gosu.draw_line(
self.x, outer_height, Gosu::Color::RED,
self.x, self.y, Gosu::Color::RED,
Float::INFINITY
)
end
end end
def update def update

View File

@@ -51,10 +51,13 @@ module CyberarmEngine
end end
def text_input_position_for(method) def text_input_position_for(method)
line = @text_input.text[0...@text_input.caret_pos].lines.last
_x = @text.x + @offset_x
if @type == :password if @type == :password
@text.x + @text.width(default(:password_character) * @text_input.text[0...@text_input.send(method)].length) _x + @text.width(default(:password_character) * line.length)
else else
@text.x + @text.width(@text_input.text[0...@text_input.send(method)]) _x + @text.width(line)
end end
end end
@@ -75,32 +78,45 @@ module CyberarmEngine
end end
def caret_position_under_mouse(mouse_x, mouse_y) def caret_position_under_mouse(mouse_x, mouse_y)
active_line = ((mouse_y - @text.y) / @text.textobject.height).round active_line = row_at(mouse_y)
active_line = 0 if @active_line < 0 right_offset = column_at(mouse_x, mouse_y)
active_line = @text.text.strip.lines.size if @active_line > @text.text.strip.lines.size
# 1.upto(@text.text.length) do |i| buffer = @text_input.text.lines[0..active_line].join if active_line != 0
# if mouse_x < @text.x - @offset_x + @text.width(@text.text[0...i]) buffer = @text_input.text.lines.first if active_line == 0
# return i - 1 line = buffer.lines.last
# end
# end
buffer = ""
@text.text.strip.lines.each do |line|
buffer.length.upto(line.length) do |i|
if mouse_x < @text.x - @offset_x + @text.width(@text.text[buffer.length...i])
puts "#{i}"
return i - 1
end
end
buffer += line if buffer.chars.last == "\n"
(buffer.length - line.length) + right_offset - 1
else
(buffer.length - line.length) + right_offset
end end
@text_input.text.length
end end
def move_caret_to_mouse(mouse_x, mouse_y) def move_caret_to_mouse(mouse_x, mouse_y)
@text_input.caret_pos = @text_input.selection_start = caret_position_under_mouse(mouse_x, mouse_y) set_position( caret_position_under_mouse(mouse_x, mouse_y) )
end
def row_at(y)
((y - @text.y) / @text.textobject.height).round
end
def column_at(x, y)
row = row_at(y)
buffer = @text_input.text.lines[0..row].join if row != 0
buffer = @text_input.text.lines.first if row == 0
line = @text_input.text.lines[row]
line = "" unless line
column = 0
line.length.times do |i|
break if @text.textobject.text_width(line[0...column]) >= (x - @text.x).clamp(0.0, Float::INFINITY)
column += 1
end
return column
end end
def button_down(id) def button_down(id)

View File

@@ -31,10 +31,24 @@ module CyberarmEngine
@width = _width ? _width : @text.width.round @width = _width ? _width : @text.width.round
@height= _height ? _height : @text.height.round @height= _height ? _height : @text.height.round
@text.x = @style.border_thickness_left + @style.padding_left + @x
@text.y = @style.border_thickness_top + @style.padding_top + @y @text.y = @style.border_thickness_top + @style.padding_top + @y
@text.z = @z + 3 @text.z = @z + 3
if text_alignment = @options[:text_align]
case text_alignment
when :left
@text.x = @style.border_thickness_left + @style.padding_left + @x
when :center
if @text.width <= outer_width
@text.x = @x + outer_width / 2 - @text.width / 2
else # Act as left aligned
@text.x = @style.border_thickness_left + @style.padding_left + @x
end
when :right
@text.x = @x + outer_width - (@text.width + @style.border_thickness_right + @style.padding_right)
end
end
update_background update_background
end end

View File

@@ -69,6 +69,7 @@ module CyberarmEngine
border_color: ["ffd59674".hex, "ffff8746".hex], border_color: ["ffd59674".hex, "ffff8746".hex],
border_radius: 0, border_radius: 0,
background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)], background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)],
text_align: :center,
hover: { hover: {
color: Gosu::Color.rgb(200,200,200), color: Gosu::Color.rgb(200,200,200),
@@ -89,6 +90,7 @@ module CyberarmEngine
caret_color: Gosu::Color::WHITE, caret_color: Gosu::Color::WHITE,
caret_interval: 500, caret_interval: 500,
selection_color: Gosu::Color.rgba(255, 128, 50, 200), selection_color: Gosu::Color.rgba(255, 128, 50, 200),
text_align: :left,
}, },
Image: { # < Element Image: { # < Element
@@ -98,6 +100,7 @@ module CyberarmEngine
Label: { # < Element Label: { # < Element
text_size: 28, text_size: 28,
text_shadow: false, text_shadow: false,
text_align: :left,
font: "Arial", font: "Arial",
margin: 0, margin: 0,
padding: 2 padding: 2