Advertisement
Pirnogion

OC/cursor

Sep 11th, 2015
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.29 KB | None | 0 0
  1. local function range(bottom, top, value, step)
  2.     if ( value + step > top ) then
  3.         return top
  4.     elseif ( value + step < bottom ) then
  5.         return bottom
  6.     end
  7.  
  8.     return value + step
  9. end
  10.  
  11. local function isPositive(value)
  12.     return value == math.abs(value)
  13. end
  14.  
  15. local function isPositive_zero(value)
  16.     return ( value == 0 and 0 ) or value == math.abs(value)
  17. end
  18.  
  19. local cursor = {}
  20.  
  21. function cursor.init()
  22.     cursor.absolute = {}
  23.     cursor.absolute.x = 1
  24.     cursor.absolute.y = 1
  25.     cursor.absolute.prev = {}
  26.     cursor.absolute.prev.x = 1
  27.     cursor.absolute.prev.y = 1
  28.  
  29.     cursor.relative = {}
  30.     cursor.relative.x = 1
  31.     cursor.relative.y = 1
  32.     cursor.relative.prev = {}
  33.     cursor.relative.prev.x = 1
  34.     cursor.relative.prev.y = 1
  35.  
  36.     cursor.delta = {}
  37.     cursor.delta.x = 0
  38.     cursor.delta.y = 0
  39.     cursor.delta.prev = {}
  40.     cursor.delta.prev.x = 0
  41.     cursor.delta.prev.y = 0
  42.  
  43.     cursor.space = {}
  44.     cursor.space.lengthX = 1
  45.     cursor.space.lengthY = 1
  46.  
  47.     --[[
  48.         wall = {
  49.             len_string,
  50.             len_string,
  51.             ...
  52.             len_string
  53.         }
  54.     ]]--
  55.     cursor.space.wall = {}
  56.     cursor.space.freeSpace = true
  57.  
  58.     cursor.allowMoveX = true
  59.     cursor.allowMoveY = true
  60. end
  61.  
  62. function cursor.addStringLen(number, position)
  63.     if ( number < 0 ) then
  64.         number = 0
  65.     end
  66.  
  67.     table.insert( cursor.space.wall, position, number )
  68.  
  69.     if ( cursor.absolute.x > cursor.space.wall[cursor.absolute.y] ) then
  70.         cursor.moveEnd()
  71.         cursor.calculate()
  72.     end
  73. end
  74.  
  75. function cursor.removeStringLen(position)
  76.     if ( #cursor.space.wall > 1 ) then
  77.         table.remove( cursor.space.wall, position )
  78.  
  79.         cursor.absolute.prev.y = cursor.absolute.y
  80.         if ( cursor.onBottom() ) then
  81.             cursor.absolute.y = #cursor.space.wall
  82.         end
  83.         if ( cursor.absolute.x > cursor.space.wall[cursor.absolute.y] ) then
  84.             cursor.moveEnd()
  85.         end
  86.     else
  87.         cursor.space.wall[cursor.absolute.y] = 0
  88.         cursor.moveHome()
  89.     end
  90.  
  91.     cursor.calculate()
  92. end
  93.  
  94. function cursor.modifyStringLen_onCursor(number, realtive)
  95.     if ( realtive ) then
  96.         cursor.space.wall[cursor.absolute.y] = cursor.space.wall[cursor.absolute.y] + number
  97.     else
  98.         cursor.space.wall[cursor.absolute.y] = number
  99.     end
  100.  
  101.     if ( cursor.space.wall[cursor.absolute.y] < 0 ) then
  102.         cursor.space.wall[cursor.absolute.y] = 0
  103.     end
  104.  
  105.     if ( cursor.absolute.x > cursor.space.wall[cursor.absolute.y] ) then
  106.         cursor.moveEnd()
  107.         cursor.calculate()
  108.     end
  109. end
  110.  
  111. function cursor.modifyStringLen(number, position, realtive)
  112.     if ( not position ) then
  113.         return nil
  114.     end
  115.  
  116.     if ( realtive ) then
  117.         cursor.space.wall[position] = cursor.space.wall[position] + number
  118.     else
  119.         cursor.space.wall[position] = number
  120.     end
  121.  
  122.     if ( cursor.space.wall[cursor.absolute.y] < 0 ) then
  123.         cursor.space.wall[cursor.absolute.y] = 0
  124.     end
  125.  
  126.     if ( cursor.absolute.x > cursor.space.wall[cursor.absolute.y] ) then
  127.         cursor.moveEnd()
  128.         cursor.calculate()
  129.     end
  130. end
  131.  
  132. function cursor.onEndLine()
  133.     return cursor.absolute.x == cursor.space.wall[cursor.absolute.y] + 1
  134. end
  135.  
  136. function cursor.onHomeLine()
  137.     return cursor.absolute.x == 1
  138. end
  139.  
  140. function cursor.onBottom()
  141.     return cursor.absolute.y >= #cursor.space.wall
  142. end
  143.  
  144. function cursor.onTop()
  145.     return cursor.absolute.y == 1
  146. end
  147.  
  148. function cursor.checkLine(step)
  149.     local lineExist = cursor.space.wall[cursor.absolute.y+step] ~= nil
  150.     local lineLessCurrent = nil
  151.     if ( lineExist ) then
  152.         lineLessCurrent = cursor.space.wall[cursor.absolute.y+step] < cursor.space.wall[cursor.absolute.y]
  153.         lineLessCurrent = lineLessCurrent and cursor.absolute.x > cursor.space.wall[cursor.absolute.y+step]
  154.     end
  155.  
  156.     return lineExist, lineLessCurrent
  157. end
  158.  
  159. function cursor.calculate()
  160.     cursor.relative.prev.x = cursor.relative.x
  161.     cursor.relative.prev.y = cursor.relative.y
  162.     cursor.relative.x = math.max( math.min(cursor.absolute.x, cursor.space.lengthX), 1 )
  163.     cursor.relative.y = math.max( math.min(cursor.absolute.y, cursor.space.lengthY), 1 )
  164.  
  165.     cursor.delta.prev.x = cursor.delta.x
  166.     cursor.delta.prev.y = cursor.delta.x
  167.     cursor.delta.x = cursor.absolute.x - cursor.relative.x
  168.     cursor.delta.y = cursor.absolute.y - cursor.relative.y
  169. end
  170.  
  171. function cursor.set(absolute_x, absolute_y)
  172.     cursor.absolute.prev.x = cursor.absolute.x
  173.     cursor.absolute.prev.y = cursor.absolute.y
  174.  
  175.     cursor.absolute.x = absolute_x
  176.     cursor.absolute.y = absolute_y
  177.  
  178.     cursor.calculate()
  179. end
  180.  
  181. function cursor.move(step_x, step_y)
  182.     local stepY_positive = isPositive_zero(step_y)
  183.     if ( stepY_positive ~= 0 ) then
  184.         local action = ( stepY_positive and cursor.moveEndNextLine ) or cursor.moveEndPrevLine
  185.  
  186.         local exist, less = cursor.checkLine(step_y)
  187.         if ( exist and less ) then
  188.             action(step_y)
  189.         elseif ( exist ) then
  190.             cursor.absolute.prev.y = cursor.absolute.y
  191.             cursor.absolute.y = cursor.absolute.y + step_y
  192.         end
  193.     end
  194.  
  195.     local stepX_positive = isPositive_zero(step_x)
  196.     if ( stepX_positive == true and cursor.onEndLine() and not cursor.onBottom() ) then
  197.         cursor.absolute.prev.y = cursor.absolute.y
  198.         cursor.absolute.y = cursor.absolute.y + 1
  199.         cursor.moveHome()
  200.     elseif ( stepX_positive == false and cursor.onHomeLine() and not cursor.onTop() ) then
  201.         cursor.absolute.prev.y = cursor.absolute.y
  202.         cursor.absolute.y = cursor.absolute.y - 1
  203.         cursor.moveEnd()
  204.     elseif ( stepX_positive ~= 0 ) then
  205.         cursor.absolute.prev.x = cursor.absolute.x
  206.         cursor.absolute.x = range(1, cursor.space.wall[cursor.absolute.y] + 1, cursor.absolute.x, step_x)
  207.     end
  208.  
  209.     cursor.calculate()
  210. end
  211.  
  212. function cursor.moveHome()
  213.     if ( cursor.space.wall[cursor.absolute.y] ) then
  214.         cursor.absolute.prev.x = cursor.absolute.x
  215.         cursor.absolute.x = 1
  216.     end
  217. end
  218.  
  219. function cursor.moveEnd()
  220.     if ( cursor.space.wall[cursor.absolute.y] ) then
  221.         cursor.absolute.prev.x = cursor.absolute.x
  222.         cursor.absolute.x = cursor.space.wall[cursor.absolute.y] + 1
  223.     end
  224. end
  225.  
  226. function cursor.moveEndNextLine(step)
  227.     if ( cursor.space.wall[cursor.absolute.y+step] ) then
  228.         cursor.absolute.prev.y = cursor.absolute.y
  229.         cursor.absolute.y = cursor.absolute.y + step
  230.  
  231.         cursor.absolute.prev.x = cursor.absolute.x
  232.         cursor.absolute.x = cursor.space.wall[cursor.absolute.y] + 1
  233.     end
  234. end
  235.  
  236. function cursor.moveEndPrevLine(step)
  237.     if ( cursor.space.wall[cursor.absolute.y+step] ) then
  238.         cursor.absolute.prev.y = cursor.absolute.y
  239.         cursor.absolute.y = cursor.absolute.y + step
  240.  
  241.         cursor.absolute.prev.x = cursor.absolute.x
  242.         cursor.absolute.x = cursor.space.wall[cursor.absolute.y] + 1
  243.     end
  244. end
  245.  
  246. return cursor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement