From 695c77b183e3a541669ea18b583760c1f057b747 Mon Sep 17 00:00:00 2001 From: cyberarm Date: Wed, 9 Sep 2020 09:51:18 -0500 Subject: [PATCH] Added support for Label text alignement, improved EditBox to correctly position caret and support mouse caret positioning, added debugging Container boundry (WIP) --- lib/cyberarm_engine/ui/elements/container.rb | 25 ++++++++ lib/cyberarm_engine/ui/elements/edit_box.rb | 60 +++++++++++++------- lib/cyberarm_engine/ui/elements/label.rb | 16 +++++- lib/cyberarm_engine/ui/theme.rb | 3 + 4 files changed, 81 insertions(+), 23 deletions(-) diff --git a/lib/cyberarm_engine/ui/elements/container.rb b/lib/cyberarm_engine/ui/elements/container.rb index 5c75b42..38ecf12 100644 --- a/lib/cyberarm_engine/ui/elements/container.rb +++ b/lib/cyberarm_engine/ui/elements/container.rb @@ -48,6 +48,31 @@ module CyberarmEngine Gosu.clip_to(@x, @y, width, height) do @children.each(&:draw) 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 def update diff --git a/lib/cyberarm_engine/ui/elements/edit_box.rb b/lib/cyberarm_engine/ui/elements/edit_box.rb index a4c0d1f..40d83c2 100644 --- a/lib/cyberarm_engine/ui/elements/edit_box.rb +++ b/lib/cyberarm_engine/ui/elements/edit_box.rb @@ -51,10 +51,13 @@ module CyberarmEngine end 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 - @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 - @text.x + @text.width(@text_input.text[0...@text_input.send(method)]) + _x + @text.width(line) end end @@ -75,32 +78,45 @@ module CyberarmEngine end def caret_position_under_mouse(mouse_x, mouse_y) - active_line = ((mouse_y - @text.y) / @text.textobject.height).round - active_line = 0 if @active_line < 0 - active_line = @text.text.strip.lines.size if @active_line > @text.text.strip.lines.size + active_line = row_at(mouse_y) + right_offset = column_at(mouse_x, mouse_y) - # 1.upto(@text.text.length) do |i| - # if mouse_x < @text.x - @offset_x + @text.width(@text.text[0...i]) - # return i - 1 - # 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 = @text_input.text.lines[0..active_line].join if active_line != 0 + buffer = @text_input.text.lines.first if active_line == 0 + line = buffer.lines.last - buffer += line + if buffer.chars.last == "\n" + (buffer.length - line.length) + right_offset - 1 + else + (buffer.length - line.length) + right_offset end - - @text_input.text.length end 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 def button_down(id) diff --git a/lib/cyberarm_engine/ui/elements/label.rb b/lib/cyberarm_engine/ui/elements/label.rb index 6dc951a..0b8e196 100644 --- a/lib/cyberarm_engine/ui/elements/label.rb +++ b/lib/cyberarm_engine/ui/elements/label.rb @@ -31,10 +31,24 @@ module CyberarmEngine @width = _width ? _width : @text.width.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.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 end diff --git a/lib/cyberarm_engine/ui/theme.rb b/lib/cyberarm_engine/ui/theme.rb index 0463d7b..43462e2 100644 --- a/lib/cyberarm_engine/ui/theme.rb +++ b/lib/cyberarm_engine/ui/theme.rb @@ -69,6 +69,7 @@ module CyberarmEngine border_color: ["ffd59674".hex, "ffff8746".hex], border_radius: 0, background: ["ffc75e61".to_i(16), "ffe26623".to_i(16)], + text_align: :center, hover: { color: Gosu::Color.rgb(200,200,200), @@ -89,6 +90,7 @@ module CyberarmEngine caret_color: Gosu::Color::WHITE, caret_interval: 500, selection_color: Gosu::Color.rgba(255, 128, 50, 200), + text_align: :left, }, Image: { # < Element @@ -98,6 +100,7 @@ module CyberarmEngine Label: { # < Element text_size: 28, text_shadow: false, + text_align: :left, font: "Arial", margin: 0, padding: 2