diff --git a/lib/cyberarm_engine/text.rb b/lib/cyberarm_engine/text.rb index 2c11693..0b83430 100644 --- a/lib/cyberarm_engine/text.rb +++ b/lib/cyberarm_engine/text.rb @@ -106,7 +106,7 @@ module CyberarmEngine text.lines.count > 0 ? (text.lines.count) * textobject.height : @textobject.height end - def draw + def draw(method = :draw_markup) if @shadow && !ARGV.join.include?("--no-shadow") shadow_alpha = @color.alpha <= 30 ? @color.alpha : @shadow_alpha shadow_color = @shadow_color ? @shadow_color : Gosu::Color.rgba(@color.red, @color.green, @color.blue, shadow_alpha) @@ -115,22 +115,22 @@ module CyberarmEngine _y = @shadow_size @rendered_shadow ||= Gosu.render((self.width+(shadow_size*2)).ceil, (self.height+(@shadow_size*2)).ceil) do - @textobject.draw_markup(@text, _x-@shadow_size, _y, @z) - @textobject.draw_markup(@text, _x-@shadow_size, _y-@shadow_size, @z) + @textobject.send(method, @text, _x-@shadow_size, _y, @z) + @textobject.send(method, @text, _x-@shadow_size, _y-@shadow_size, @z) - @textobject.draw_markup(@text, _x, _y-@shadow_size, @z, @factor_x) - @textobject.draw_markup(@text, _x+@shadow_size, _y-@shadow_size, @z) + @textobject.send(method, @text, _x, _y-@shadow_size, @z, @factor_x) + @textobject.send(method, @text, _x+@shadow_size, _y-@shadow_size, @z) - @textobject.draw_markup(@text, _x, _y+@shadow_size, @z) - @textobject.draw_markup(@text, _x-@shadow_size, _y+@shadow_size, @z) + @textobject.send(method, @text, _x, _y+@shadow_size, @z) + @textobject.send(method, @text, _x-@shadow_size, _y+@shadow_size, @z) - @textobject.draw_markup(@text, _x+@shadow_size, _y, @z) - @textobject.draw_markup(@text, _x+@shadow_size, _y+@shadow_size, @z) + @textobject.send(method, @text, _x+@shadow_size, _y, @z) + @textobject.send(method, @text, _x+@shadow_size, _y+@shadow_size, @z) end @rendered_shadow.draw(@x-@shadow_size, @y-@shadow_size, @z, @factor_x, @factor_y, shadow_color) end - @textobject.draw_markup(@text, @x, @y, @z, @factor_x, @factor_y, @color) + @textobject.send(method, @text, @x, @y, @z, @factor_x, @factor_y, @color) end def alpha=(n) diff --git a/lib/cyberarm_engine/ui/elements/edit_box.rb b/lib/cyberarm_engine/ui/elements/edit_box.rb index 2f4a763..a4c0d1f 100644 --- a/lib/cyberarm_engine/ui/elements/edit_box.rb +++ b/lib/cyberarm_engine/ui/elements/edit_box.rb @@ -5,6 +5,39 @@ module CyberarmEngine super(*args) @active_line = 0 + + @repeatable_keys = [ + { + key: Gosu::KB_UP, + down: false, + repeat_delay: 50, + last_repeat: 0, + action: proc {move(:up)} + }, + { + key: Gosu::KB_DOWN, + down: false, + repeat_delay: 50, + last_repeat: 0, + action: proc {move(:down)} + } + ] + end + + def update + super + + caret_stay_left_of_last_newline + calculate_active_line + + @repeatable_keys.each do |key| + if key[:down] + if Gosu.milliseconds > key[:last_repeat] + key[:repeat_delay] + key[:action].call + key[:last_repeat] = Gosu.milliseconds + end + end + end end def draw_caret @@ -25,11 +58,42 @@ module CyberarmEngine end end + def set_position(int) + @text_input.selection_start = @text_input.caret_pos = int + end + + def calculate_active_line + sub_text = @text_input.text[0...@text_input.caret_pos] + @active_line = sub_text.lines.size-1 + end + + def caret_stay_left_of_last_newline + @text_input.text+="\n" unless @text_input.text.end_with?("\n") + + eof = @text_input.text.chomp.length + set_position(eof) if @text_input.caret_pos > eof + end + def caret_position_under_mouse(mouse_x, mouse_y) - 1.upto(@text.text.length) do |i| - if mouse_x < @text.x - @offset_x + @text.textobject.text_width(@text.text[0...i]) - return i - 1; + 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 + + # 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 += line end @text_input.text.length @@ -42,6 +106,14 @@ module CyberarmEngine def button_down(id) super + @repeatable_keys.detect do |key| + if key[:key] == id + key[:down] = true + key[:last_repeat] = Gosu.milliseconds + key[:repeat_delay] + return true + end + end + case id when Gosu::KB_ENTER, Gosu::KB_RETURN caret_pos = @text_input.caret_pos @@ -50,6 +122,33 @@ module CyberarmEngine end end + def button_up(id) + super + + @repeatable_keys.detect do |key| + if key[:key] == id + key[:down] = false + return true + end + end + end + + def move(direction) + pos = @text_input.caret_pos + line = nil + + case direction + when :up + return if @active_line == 0 + when :down + return if @active_line == @text_input.text.chomp.lines + text = @text_input.text.chomp.lines[0..@active_line].join("\n") + pos = text.length + end + + set_position(pos) + end + def drag_update(sender, x, y, button) @text_input.caret_pos = caret_position_under_mouse(x, y) diff --git a/lib/cyberarm_engine/ui/elements/edit_line.rb b/lib/cyberarm_engine/ui/elements/edit_line.rb index 5502feb..6b29467 100644 --- a/lib/cyberarm_engine/ui/elements/edit_line.rb +++ b/lib/cyberarm_engine/ui/elements/edit_line.rb @@ -34,6 +34,10 @@ module CyberarmEngine end end + def draw_text + @text.draw(:draw_text) + end + def draw_caret Gosu.draw_rect(caret_position, @text.y, @caret_width, @caret_height, @caret_color, @z) end @@ -110,8 +114,8 @@ module CyberarmEngine def caret_position_under_mouse(mouse_x) 1.upto(@text.text.length) do |i| - if mouse_x < @text.x - @offset_x + @text.textobject.text_width(@text.text[0...i]) - return i - 1; + if mouse_x < @text.x - @offset_x + @text.width(@text.text[0...i]) + return i - 1 end end