From 8227904177be440932bf7c7a48fbf05ab3fd4ac2 Mon Sep 17 00:00:00 2001 From: jackiekircher Date: Sun, 6 Nov 2011 09:14:03 -0500 Subject: [PATCH] consolidate cursor positioning --- lib/vimcity/vim_city_game.rb | 54 +++++++++++++++++++----------------- lib/vimcity/vim_wrapper.rb | 8 ++++++ 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/lib/vimcity/vim_city_game.rb b/lib/vimcity/vim_city_game.rb index 11d3e6d..96390ef 100644 --- a/lib/vimcity/vim_city_game.rb +++ b/lib/vimcity/vim_city_game.rb @@ -27,6 +27,7 @@ def initialize @width = @main_window.width @insert_mode = false + @current_building = nil @map = Map.new(@main_buffer) @@ -110,9 +111,9 @@ def init_city def init_cursor @cursor = [" "] #use an array for area cursors - c = VIM::evaluate("getpos('.')") - @last_chars = cache_area(@main_buffer, c[1], 1, c[2], 1) - print_area_to_buffer(@main_buffer, c[1], c[2], @cursor) + c = get_cursor_pos + @last_chars = cache_area(@main_buffer, c[0], 1, c[1], 1) + print_area_to_buffer(@main_buffer, c[0], c[1], @cursor) end def update_status_bar @@ -125,37 +126,37 @@ def update_status_bar end def update_cursor(x,y) - c = VIM::evaluate("getpos('.')") + c = get_cursor_pos cursor_height = @cursor.size cursor_width = @cursor.first.size - print_area_to_buffer(@main_buffer, c[1], c[2], @last_chars) + print_area_to_buffer(@main_buffer, c[0], c[1], @last_chars) - c[1] += y - c[1] = 1 if c[1] < 1 - c[1] = @map.height+1 if c[1] >= @map.height+1 + c[0] += y + c[0] = 1 if c[0] < 1 + c[0] = @map.height+1 if c[0] >= @map.height+1 - c[2] += x - c[2] = @map.offset if c[2] < @map.offset - c[2] = @map.width-(@map.offset-1) if c[2] >= (@map.width+@map.offset) + c[1] += x + c[1] = @map.offset if c[1] < @map.offset + c[1] = @map.width-(@map.offset-0) if c[1] >= (@map.width+@map.offset) - VIM::evaluate("cursor(#{c[1]},#{c[2]})") + set_cursor_pos(c[0], c[1]) @last_char = cache_area(@main_buffer, - c[1], cursor_height, - c[2], cursor_width) - print_area_to_buffer(@main_buffer, c[1], c[2], @cursor) + c[0], cursor_height, + c[1], cursor_width) + print_area_to_buffer(@main_buffer, c[0], c[1], @cursor) end def reset_cursor - c = VIM::evaluate("getpos('.')") - print_area_to_buffer(@main_buffer, c[1], c[2], @last_chars) + c = get_cursor_pos + print_area_to_buffer(@main_buffer, c[0], c[1], @last_chars) @last_chars = cache_area(@main_buffer, - c[1], 1, - c[2], 1) + c[0], 1, + c[1], 1) @cursor = [" "] - print_area_to_buffer(@main_buffer, c[1], c[2], @cursor) + print_area_to_buffer(@main_buffer, c[0], c[1], @cursor) return end @@ -221,15 +222,16 @@ def building_menu quit #c = VIM::Window.current.cursor - c = VIM::evaluate("getpos('.')") - print_area_to_buffer(@main_buffer, c[1], c[2], @last_chars) + c = get_cursor_pos + print_area_to_buffer(@main_buffer, c[0], c[1], @last_chars) @last_chars = cache_area(@main_buffer, - c[1], building.height, - c[2], building.width) - @cursor = building.symbol if building - print_area_to_buffer(@main_buffer, c[1], c[2], @cursor) + c[0], building.height, + c[1], building.width) + @cursor = building.symbol + print_area_to_buffer(@main_buffer, c[0], c[1], @cursor) @insert_mode = true + @current_building = building return else break diff --git a/lib/vimcity/vim_wrapper.rb b/lib/vimcity/vim_wrapper.rb index 8423744..196ad56 100644 --- a/lib/vimcity/vim_wrapper.rb +++ b/lib/vimcity/vim_wrapper.rb @@ -1,5 +1,13 @@ module VimWrapper + def get_cursor_pos + return VIM::evaluate("getpos('.')")[1,2] + end + + def set_cursor_pos(y,x) + return VIM::evaluate("cursor(#{y},#{x})") + end + def redraw VIM::command("redraw") end