Andy73

desktox.buffer

Feb 12th, 2017
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 64.47 KB | None | 0 0
  1.  
  2. -- luacheck:globals colours
  3.  
  4. -- Desktox
  5. --  A set of graphics libraries for ComputerCraft by @viluon <[email protected]>
  6.  
  7. --   desktox.buffer
  8. --    The Desktox framebuffer library
  9.  
  10. --    This Source Code Form is subject to the terms of the Mozilla Public
  11. --    License, v. 2.0. If a copy of the MPL was not distributed with this
  12. --    file, You can obtain one at http://mozilla.org/MPL/2.0/.
  13.  
  14. -- In case you'd forget, pixels are stored as { background_colour, foreground_colour, character }
  15.  
  16. local buffer = {}
  17.  
  18. local buffer_methods = {}
  19. local buffer_metatable = {}
  20.  
  21. -- Constants
  22. local TRANSPARENT_BACKGROUND = -1
  23. local TRANSPARENT_FOREGROUND = -2
  24. local TRANSPARENT_CHARACTER = "\0"
  25.  
  26. local DEFAULT_BACKGROUND = colours.white
  27. local DEFAULT_FOREGROUND = colours.black
  28. local DEFAULT_CHARACTER = " "
  29.  
  30. -- Utilities
  31. local setmetatable = setmetatable
  32. local tonumber = tonumber
  33. local pairs = pairs
  34. local error = error
  35. local type = type
  36.  
  37. local colours = colours
  38. local string = string
  39. local table = table
  40.  
  41. local tconcat = table.concat
  42.  
  43. local sub  = string.sub
  44. local gsub = string.gsub
  45. local char = string.char
  46.  
  47. -- An array of known colours
  48. local colour_array = {
  49.     colours.white,
  50.     colours.orange,
  51.     colours.magenta,
  52.     colours.lightBlue,
  53.     colours.yellow,
  54.     colours.lime,
  55.     colours.pink,
  56.     colours.grey,
  57.     colours.lightGrey,
  58.     colours.cyan,
  59.     colours.purple,
  60.     colours.blue,
  61.     colours.brown,
  62.     colours.green,
  63.     colours.red,
  64.     colours.black,
  65.     TRANSPARENT_BACKGROUND,
  66.     TRANSPARENT_FOREGROUND,
  67. }
  68.  
  69. -- Generate a bidirectional lookup table
  70. local colour_lookup = {}
  71. for i = 1, #colour_array do
  72.     local colour = colour_array[ i ]
  73.     local hex = sub( "0123456789abcdefgh", i, i )
  74.  
  75.     colour_lookup[ colour ] = hex
  76.     colour_lookup[ hex ] = colour
  77. end
  78.  
  79. -- Error messages
  80. local unable_to_set_optional_argument = "Unable to set optional argument "
  81. local expected_number_for = "Expected number for "
  82.  
  83. -- Utility functions
  84.  
  85. --- Round a number to a set amount of decimal places.
  86. -- @param n         The number to round
  87. -- @param places    (Optional) The amount of decimal places to keep, defaults to 0
  88. -- @return The rounded number
  89. local function round( n, places )
  90.     local mult = 10 ^ ( places or 0 )
  91.     local m = n * mult + 0.5
  92.  
  93.     return ( m - m % 1 ) / mult
  94. end
  95.  
  96. --- Get a teletext subpixel character from data for 6 pixels
  97. -- @param top_left      A boolean indicating whether the relevant subpixel is set
  98. -- @param top_right     A boolean indicating whether the relevant subpixel is set
  99. -- @param left          A boolean indicating whether the relevant subpixel is set
  100. -- @param right         A boolean indicating whether the relevant subpixel is set
  101. -- @param bottom_left   A boolean indicating whether the relevant subpixel is set
  102. -- @param bottom_right  A boolean indicating whether the relevant subpixel is set
  103. -- @return The character followed by a flag determining whether the background
  104. --         and foreground colours should be inverted.
  105. local function shrink_to_character( top_left, top_right, left, right, bottom_left, bottom_right )
  106.     local data = 128
  107.  
  108.     if not bottom_right then
  109.         if top_left then
  110.             data = data + 1
  111.         end
  112.  
  113.         if top_right then
  114.             data = data + 2
  115.         end
  116.  
  117.         if left then
  118.             data = data + 4
  119.         end
  120.  
  121.         if right then
  122.             data = data + 8
  123.         end
  124.  
  125.         if bottom_left then
  126.             data = data + 16
  127.         end
  128.  
  129.     else
  130.         if not top_left then
  131.             data = data + 1
  132.         end
  133.  
  134.         if not top_right then
  135.             data = data + 2
  136.         end
  137.  
  138.         if not left then
  139.             data = data + 4
  140.         end
  141.  
  142.         if not right then
  143.             data = data + 8
  144.         end
  145.  
  146.         if not bottom_left then
  147.             data = data + 16
  148.         end
  149.  
  150.     end
  151.  
  152.     return char( data ), bottom_right
  153. end
  154.  
  155. --- Resize the buffer.
  156. -- @param width             (Optional) The desired new width, defaults to self.width
  157. -- @param height            (Optional) The desired new height, defaults to self.height
  158. -- @param background_colour (Optional) The background colour to set any new pixels to, defaults to DEFAULT_BACKGROUND
  159. -- @param foreground_colour (Optional) The foreground colour to set any new pixels to, defaults to DEFAULT_FOREGROUND
  160. -- @param character         (Optional) The character to set any new pixels to, defaults to DEFAULT_CHARACTER
  161. -- @return self
  162. function buffer_methods:resize( width, height, background_colour, foreground_colour, character )
  163.     local insert = table.insert
  164.     local remove = table.remove
  165.  
  166.     local self_width = self.width
  167.     local self_height = self.height
  168.     local n_self = self.length
  169.  
  170.     width = width or self_width
  171.     height = height or self_height
  172.  
  173.     local new_pixel = {
  174.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  175.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  176.         character                     or DEFAULT_CHARACTER;
  177.     }
  178.  
  179.     -- Loop through all lines
  180.     for y = ( self_height < height and self_height or height ) - 1, 0, -1 do
  181.         if width > self_width then
  182.             local line_offset = y * self_width
  183.  
  184.             -- Insert pixels at the end of the line
  185.             for _ = 0, width - self_width - 1 do
  186.                 insert( self, line_offset, new_pixel )
  187.                 n_self = n_self + 1
  188.             end
  189.  
  190.         elseif width < self_width then
  191.             local line_offset = y * width
  192.  
  193.             -- Drop the pixels exceeding new width
  194.             for _ = 0, self_width - width - 1 do
  195.                 remove( self, line_offset )
  196.                 n_self = n_self - 1
  197.             end        
  198.         end
  199.     end
  200.  
  201.     if height > self_height then
  202.         -- Insert blank lines
  203.         for i = n_self, width * height - 1 do
  204.             self[ i ] = new_pixel
  205.         end
  206.  
  207.     elseif height < self_height then
  208.         -- Drop extra lines
  209.         for i = width * height - 1, n_self do
  210.             self[ i ] = nil
  211.         end
  212.     end
  213.  
  214.     self.width = width
  215.     self.height = height
  216.     self.length = width * height - 1
  217.  
  218.     self.x2 = self.x1 + width  - 1
  219.     self.y2 = self.y1 + height - 1
  220.  
  221.     return self
  222. end
  223.  
  224. --- Map the buffer with the given function.
  225. -- @param fn        The function to map with, given arguments self, x, y, pixel
  226. -- @param start_x   (Optional) The x coordinate in self to start mapping from, 0-based, defaults to 0
  227. -- @param start_y   (Optional) The y coordinate in self to start mapping from, 0-based, defaults to 0
  228. -- @param end_x     (Optional) The x coordinate in self to stop mapping at, 0-based, defaults to self.width - 1
  229. -- @param end_y     (Optional) The y coordinate in self to stop mapping at, 0-based, defaults to self.height - 1
  230. -- @return self, old_self   Self and the self state before mapping
  231. function buffer_methods:map( fn, start_x, start_y, end_x, end_y )
  232.     if type( fn ) ~= "function" then
  233.         error( "Expected function for 'fn'", 2 )
  234.     end
  235.  
  236.     local almost_w = self.width  - 1
  237.     local almost_h = self.height - 1
  238.  
  239.     -- Avoid function calls where possible
  240.     if  start_x then
  241.         start_x = start_x < 0 and 0 or start_x
  242.     else
  243.         start_x = 0
  244.     end
  245.  
  246.     if  end_x then
  247.         end_x = end_x > almost_w and almost_w or end_x
  248.     else
  249.         end_x = almost_w
  250.     end
  251.  
  252.     if end_x < start_x then
  253.         start_x, end_x = end_x, start_x
  254.     end
  255.  
  256.     if  start_y then
  257.         start_y = start_y < 0 and 0 or start_y
  258.     else
  259.         start_y = 0
  260.     end
  261.  
  262.     if  end_y then
  263.         end_y = end_y > almost_h and almost_h or end_y
  264.     else
  265.         end_y = almost_h
  266.     end
  267.  
  268.     if end_y < start_y then
  269.         start_y, end_y = end_y, start_y
  270.     end
  271.  
  272.     local clone = self:clone()
  273.     local w = almost_w + 1
  274.  
  275.     -- Loop through all pixels
  276.     for y = start_y, end_y do
  277.         local line_offset = y * w
  278.  
  279.         for x = start_x, end_x do
  280.             self[ line_offset + x ] = fn( self, x, y, clone[ line_offset + x ] )
  281.         end
  282.     end
  283.  
  284.     return self, clone
  285. end
  286.  
  287. --- Clone the buffer into a new object.
  288. -- @param data      (Optional) The data of the new clone (a regular array of pixels), pulls from self by default
  289. -- @return buffer   The new buffer, a clone of self
  290. function buffer_methods:clone( data )
  291.     local clone = buffer.new_from_points( self.x1, self.y1, self.x2, self.y2, self.parent, nil, nil, nil, true )
  292.  
  293.     -- Clone the pixel data
  294.     if data and type( data ) == "table" then
  295.         for i = 1, #data do
  296.             local pixel = data[ i ]
  297.  
  298.             clone[ i - 1 ] = {
  299.                 pixel[ 1 ];
  300.                 pixel[ 2 ];
  301.                 pixel[ 3 ];
  302.             }
  303.         end
  304.     else
  305.         local w = self.width
  306.  
  307.         for y = 0, self.height - 1 do
  308.             local line_offset = y * w
  309.  
  310.             for x = 0, w - 1 do
  311.                 local index = line_offset + x
  312.                 local pixel = self[ index ]
  313.  
  314.                 clone[ index ] = {
  315.                     pixel[ 1 ];
  316.                     pixel[ 2 ];
  317.                     pixel[ 3 ];
  318.                 }
  319.             end
  320.         end
  321.     end
  322.  
  323.     return clone
  324. end
  325.  
  326. --- Render the buffer to another buffer.
  327. -- @param target    (Optional) The buffer to render to, defaults to self.parent
  328. -- @param x         (Optional) The x coordinate in target to render self at, 0-based, defaults to self.x1
  329. -- @param y         (Optional) The y coordinate in target to render self at, 0-based, defaults to self.y1
  330. -- @param start_x   (Optional) The x coordinate in self to start rendering from, 0-based, defaults to 0
  331. -- @param start_y   (Optional) The y coordinate in self to start rendering from, 0-based, defaults to 0
  332. -- @param end_x     (Optional) The x coordinate in self to stop rendering at, 0-based, defaults to self.width - 1
  333. -- @param end_y     (Optional) The y coordinate in self to stop rendering at, 0-based, defaults to self.height - 1
  334. -- @return self
  335. function buffer_methods:render( target, x, y, start_x, start_y, end_x, end_y )
  336.     target = target or self.parent or error( unable_to_set_optional_argument .. "'target': self.parent is nil", 2 )
  337.     x      = x      or self.x1     or error( unable_to_set_optional_argument .. "'x': self.x1 is nil", 2 )
  338.     y      = y      or self.y1     or error( unable_to_set_optional_argument .. "'y': self.y1 is nil", 2 )
  339.  
  340.     local self_width = self.width
  341.     local target_width = target.width
  342.  
  343.     start_x = start_x or 0
  344.     start_y = start_y or 0
  345.     end_x = end_x or self_width - 1
  346.     end_y = end_y or self.height - 1
  347.  
  348.     -- Respect borders of the parent
  349.     start_x = start_x > -x and start_x or -x
  350.     start_y = start_y > -y and start_y or -y
  351.     end_x   = ( end_x < ( target_width  - x - 1 ) and end_x or target_width  - x - 1 )
  352.     end_y   = ( end_y < ( target.height - y - 1 ) and end_y or target.height - y - 1 )
  353.  
  354.     -- Will be explained further down
  355.     local false_parent = { parent = target }
  356.  
  357.     -- Loop through all coordinates
  358.     for _y = start_y, end_y do
  359.         -- Cache the line position for both the target buffer and us
  360.         local target_offset = ( _y + y ) * target_width + x
  361.         local local_offset  = _y * self_width
  362.  
  363.         for _x = start_x, end_x do
  364.             -- index is where we should store the resultant pixel in target
  365.             local index = target_offset + _x
  366.             -- pixel is the data from our buffer
  367.             local pixel = self[ local_offset + _x ]
  368.  
  369.             -- Cache the contents for faster access
  370.             local pixel1, pixel2, pixel3 = pixel[ 1 ], pixel[ 2 ], pixel[ 3 ]
  371.  
  372.             -- Set the pixel in target, resolving transparency along the way
  373.             if pixel1 == TRANSPARENT_BACKGROUND and pixel2 == TRANSPARENT_FOREGROUND and pixel3 == TRANSPARENT_CHARACTER then
  374.                 -- We actually don't have to draw anything anywhere - this pixel has no effect on the parent
  375.  
  376.             elseif pixel1 < 0 or pixel2 < 0 or pixel3 == TRANSPARENT_CHARACTER then
  377.                 local local_parent = false_parent
  378.  
  379.                 local background_colour = pixel1
  380.                 local foreground_colour = pixel2
  381.                 local character = pixel3
  382.  
  383.                 local tracked_offset_x = x
  384.                 local tracked_offset_y = y
  385.  
  386.                 -- Not only we have to get to the non-transparent colour
  387.                 -- when the background_colour is transparent, we also have
  388.                 -- to resolve the colour if the *foreground* uses it
  389.  
  390.                 if background_colour < 0 or pixel2 == TRANSPARENT_BACKGROUND then
  391.                     local tracked_colour = -1
  392.  
  393.                     -- Down into the rabbit hole we go. One parent level further with every iteration.
  394.                     -- We have to let the loop run at least once, for the TRANSPARENT_BACKGROUND to resolve
  395.                     repeat
  396.                         -- This wouldn't work the first time (when the loop starts), that's
  397.                         -- why we defined local_parent to be the false_parent
  398.                         local_parent = local_parent.parent
  399.  
  400.                         if not local_parent then
  401.                             -- We've reached the very bottom of the family stack, without luck
  402.                             background_colour = DEFAULT_BACKGROUND
  403.                             break
  404.                         end
  405.  
  406.                         -- All buffers have a position, we have to keep track of that
  407.                         tracked_offset_x = tracked_offset_x + local_parent.x1
  408.                         tracked_offset_y = tracked_offset_y + local_parent.y1
  409.  
  410.                         local actual_y = _y + tracked_offset_y
  411.                         local actual_x = _x + tracked_offset_x
  412.                         local p_width  = local_parent.width
  413.                         local p_height = local_parent.height
  414.  
  415.                         -- Check that we are within bounds
  416.                         -- We could subtract a 1 in the local definition above, but if actual_x < 0 then the operation
  417.                         -- doesn't even happen :P
  418.                         if actual_x < 0 or actual_x > p_width - 1 or actual_y < 0 or actual_y > p_height - 1 then
  419.                             -- We can't get a pixel out of bounds of this parent, so we'll just roll with
  420.                             -- the default colour.
  421.                             --TODO: Would it be better if we'd look for any parent that *is* within bounds instead?
  422.                             --      Probably not, the result isn't visible anyway since that part of self is outside
  423.                             --      of the parent
  424.                             background_colour = DEFAULT_BACKGROUND
  425.                             break
  426.                         end
  427.  
  428.                         background_colour = local_parent[ actual_y * p_width + actual_x ][ -tracked_colour ]
  429.                         tracked_colour = background_colour
  430.  
  431.                     until background_colour > 0
  432.                 end
  433.  
  434.                 -- The same goes for the foreground colour...
  435.                 if foreground_colour < 0 or pixel1 == TRANSPARENT_FOREGROUND then
  436.                     local_parent = false_parent
  437.                     tracked_offset_x = x
  438.                     tracked_offset_y = y
  439.  
  440.                     local tracked_colour = -2
  441.  
  442.                     repeat
  443.                         local_parent = local_parent.parent
  444.  
  445.                         if not local_parent then
  446.                             foreground_colour = DEFAULT_FOREGROUND
  447.                             break
  448.                         end
  449.  
  450.                         tracked_offset_x = tracked_offset_x + local_parent.x1
  451.                         tracked_offset_y = tracked_offset_y + local_parent.y1
  452.  
  453.                         local actual_y = _y + tracked_offset_y
  454.                         local actual_x = _x + tracked_offset_x
  455.                         local p_width  = local_parent.width
  456.                         local p_height = local_parent.height
  457.  
  458.                         if actual_x < 0 or actual_x > p_width - 1 or actual_y < 0 or actual_y > p_height - 1 then
  459.                             foreground_colour = DEFAULT_FOREGROUND
  460.                             break
  461.                         end
  462.  
  463.                         foreground_colour = local_parent[ actual_y * p_width + actual_x ][ -tracked_colour ]
  464.                         tracked_colour = foreground_colour
  465.  
  466.                     until foreground_colour > 0
  467.                 end
  468.  
  469.                 -- ...And finally for the character.
  470.                 -- Note that a colour cannot be transparent to a character, so
  471.                 -- there's no extra branch here
  472.                 local_parent = false_parent
  473.                 tracked_offset_x = x
  474.                 tracked_offset_y = y
  475.  
  476.                 while character == TRANSPARENT_CHARACTER do
  477.                     local_parent = local_parent.parent
  478.  
  479.                     if not local_parent then
  480.                         character = DEFAULT_CHARACTER
  481.                         break
  482.                     end
  483.  
  484.                     tracked_offset_x = tracked_offset_x + local_parent.x1
  485.                     tracked_offset_y = tracked_offset_y + local_parent.y1
  486.  
  487.                     local actual_y = _y + tracked_offset_y
  488.                     local actual_x = _x + tracked_offset_x
  489.                     local p_width = local_parent.width
  490.                     local p_height = local_parent.height
  491.  
  492.                     if actual_x < 0 or actual_x > p_width - 1 or actual_y < 0 or actual_y > p_height - 1 then
  493.                         character = DEFAULT_CHARACTER
  494.                         break
  495.                     end
  496.  
  497.                     character = local_parent[ actual_y * p_width + actual_x ][ 3 ]
  498.                 end
  499.  
  500.                 -- Now that we're certain we have non-transparent data, store them in a pixel
  501.                 -- We *could* use -1 and -2 indices, but there will be a lot of cases where
  502.                 -- one or both of them aren't used. Because creating arrays is faster
  503.                 -- (array creation can utilise SETLIST while table creation can only use
  504.                 -- SETTABLE) and this assignment has to be done for *all cases*, we'll limit
  505.                 -- the math to the target[ index ] assignment down below.
  506.                 local underneath = {
  507.                     background_colour;
  508.                     foreground_colour;
  509.                     character;
  510.                 }
  511.  
  512.                 -- Assign the proper data to the rendered pixel
  513.                 target[ index ] = {
  514.                     pixel1 > 0 and pixel1 or underneath[ -pixel1 ];
  515.                     pixel2 > 0 and pixel2 or underneath[ -pixel2 ];
  516.                     pixel3 == TRANSPARENT_CHARACTER and underneath[ 3 ] or pixel3;
  517.                 }
  518.             else
  519.                 -- That was quick! This is a speed up for the large number of cases
  520.                 -- in which no transparency is used
  521.                 target[ index ] = {
  522.                     pixel1;
  523.                     pixel2;
  524.                     pixel3;
  525.                 }
  526.             end
  527.         end
  528.     end
  529.  
  530.     return self
  531. end
  532.  
  533. --- Render the buffer to a CraftOS window object.
  534. -- @param target    The window to render to
  535. -- @param x         (Optional) The x coordinate in target to render self at, 1-based, defaults to self.x1 + 1
  536. -- @param y         (Optional) The y coordinate in target to render self at, 1-based, defaults to self.y1 + 1
  537. -- @param visible   (Optional) Whether the terminal object (if it's a window) should be visible after rendering.
  538. --                  If set, this option can significantly speed up the rendering process by avoiding window redraws.
  539. -- @param parent    (Optional) The buffer to use for transparency resolution, defaults to self.parent
  540. -- @param start_x   (Optional) The x coordinate to start reading data (from self) at, 0-based, defaults to 0
  541. -- @param start_y   (Optional) The y coordinate to start reading data (from self) at, 0-based, defaults to 0
  542. -- @param end_x     (Optional) The x coordinate to end reading data (from self) at, 0-based, defaults to self.width - 1
  543. -- @param end_y     (Optional) The y coordinate to end reading data (from self) at, 0-based, defaults to self.height - 1
  544. -- @param palette   (Optional) A lookup table to use for converting colours, defaults to ComputerCraft's native colour mapping
  545. -- @return self
  546. function buffer_methods:render_to_window( target, x, y, visible, parent, start_x, start_y, end_x, end_y, palette )
  547.     x = x or ( self.x1 and self.x1 + 1 or error( unable_to_set_optional_argument .. "'x': self.x1 is nil", 2 ) )
  548.     y = y or ( self.y1 and self.y1 + 1 or error( unable_to_set_optional_argument .. "'y': self.y1 is nil", 2 ) )
  549.    
  550.     parent = parent or self.parent
  551.  
  552.     local setc, blit  = target.setCursorPos, target.blit
  553.     local set_visible = target.setVisible
  554.  
  555.     if visible ~= nil and set_visible then
  556.         set_visible( false )
  557.     end
  558.  
  559.     -- Go through all lines of the buffer
  560.     --local before_cook = clock()
  561.  
  562.     local lines = self:cook_lines( parent, start_x, start_y, end_x, end_y, palette )
  563.  
  564.     --local before_blit = clock()
  565.     --time_spent_cooking = time_spent_cooking + before_blit - before_cook
  566.  
  567.     local line = 0
  568.  
  569.     for i = 1, #lines, 3 do
  570.         setc( x, y + line )
  571.         blit( lines[ i + 2 ], lines[ i + 1 ], lines[ i ] )
  572.  
  573.         line = line + 1
  574.     end
  575.  
  576.     --time_spent_blitting = time_spent_blitting + clock() - before_blit
  577.  
  578.     if visible == true and set_visible then
  579.         set_visible( true )
  580.     end
  581.  
  582.     return self
  583. end
  584.  
  585. --- Cook the buffer contents into blit()able data.
  586. -- @param target    (Optional) The target buffer to use for transparency resolution, defaults to self.parent
  587. -- @param start_x   (Optional) The x coordinate to start reading data at, 0-based, defaults to 0
  588. -- @param start_y   (Optional) The y coordinate to start reading data at, 0-based, defaults to 0
  589. -- @param end_x     (Optional) The x coordinate to end reading data at, 0-based, defaults to self.width - 1
  590. -- @param end_y     (Optional) The y coordinate to end reading data at, 0-based, defaults to self.height - 1
  591. -- @return lines An array of triplets of strings, in this order: background_colours, foreground_colours, text
  592. --         (data for each line of the buffer)
  593. function buffer_methods:cook_lines( target, start_x, start_y, end_x, end_y, palette )
  594.     target = target or self.parent
  595.  
  596.     local lookup = colour_lookup
  597.  
  598.     if palette and type( palette ) == "table" then
  599.         -- Go through all known colours and ensure all keys are set
  600.         for i = 1, #colour_array do
  601.             local colour = colour_array[ i ]
  602.             palette[ colour ] = palette[ colour ] or colour_lookup[ colour ]
  603.         end
  604.  
  605.         lookup = palette
  606.     end
  607.  
  608.     local lines = {}
  609.     local i = 1
  610.  
  611.     local h = self.height
  612.  
  613.     local self_width = self.width
  614.     -- This avoids attempts to something something nil while enabling a nil parent
  615.     local target_width = target and target.width or 0
  616.  
  617.     start_x = start_x or 0
  618.     start_y = start_y or 0
  619.     end_x = end_x or self_width - 1
  620.     end_y = end_y or h - 1
  621.  
  622.     if target then
  623.         -- Respect borders of the target
  624.         start_x = start_x > -self.x1 and start_x or -self.x1
  625.         start_y = start_y > -self.y1 and start_y or -self.y1
  626.         end_x   = ( end_x < ( target_width  - self.x1 - 1 ) and end_x or target_width  - self.x1 - 1 )
  627.         end_y   = ( end_y < ( target.height - self.y1 - 1 ) and end_y or target.height - self.y1 - 1 )
  628.     end
  629.  
  630.     -- Will be explained further down
  631.     -- (in case target is nil, transparency resolution will exit cleanly)
  632.     local false_parent = { parent = target }
  633.  
  634.     -- Go through all lines
  635.     for y = start_y, end_y do
  636.         local line1 = {}
  637.         local line2 = {}
  638.         local line3 = {}
  639.  
  640.         local line_index = 1
  641.  
  642.         -- Cache the line position
  643.         local local_offset = y * self_width
  644.  
  645.         for x = start_x, end_x do
  646.             -- pixel is the data from our buffer
  647.             local pixel = self[ local_offset + x ]
  648.  
  649.             -- Cache the contents for faster access
  650.             local pixel1, pixel2, pixel3 = pixel[ 1 ], pixel[ 2 ], pixel[ 3 ]
  651.  
  652.             -- Set the pixel in target, resolving transparency along the way
  653.             if pixel1 < 0 or pixel2 < 0 or pixel3 == TRANSPARENT_CHARACTER then
  654.                 local local_parent = false_parent
  655.  
  656.                 local background_colour = pixel1
  657.                 local foreground_colour = pixel2
  658.                 local character = pixel3
  659.  
  660.                 local tracked_offset_x = x
  661.                 local tracked_offset_y = y
  662.  
  663.                 -- Not only we have to get to the non-transparent colour
  664.                 -- when the background_colour is transparent, we also have
  665.                 -- to resolve the colour if the *foreground* uses it
  666.  
  667.                 if background_colour < 0 or pixel2 == TRANSPARENT_BACKGROUND then
  668.                     local tracked_colour = -1
  669.  
  670.                     -- Down into the rabbit hole we go. One parent level further with every iteration.
  671.                     -- We have to let the loop run at least once, for the TRANSPARENT_BACKGROUND to resolve
  672.                     repeat
  673.                         -- This wouldn't work the first time (when the loop starts), that's
  674.                         -- why we defined local_parent to be the false_parent
  675.                         local_parent = local_parent.parent
  676.  
  677.                         if not local_parent then
  678.                             -- We've reached the very bottom of the family stack, without luck
  679.                             -- Note that this also happens in case the target is nil, as
  680.                             -- is frequent for top-level buffers
  681.                             background_colour = DEFAULT_BACKGROUND
  682.                             break
  683.                         end
  684.  
  685.                         -- All buffers have a position, we have to keep track of that
  686.                         tracked_offset_x = tracked_offset_x + local_parent.x1
  687.                         tracked_offset_y = tracked_offset_y + local_parent.y1
  688.  
  689.                         local actual_y = y + tracked_offset_y
  690.                         local actual_x = x + tracked_offset_x
  691.                         local p_width  = local_parent.width
  692.                         local p_height = local_parent.height
  693.  
  694.                         -- Check that we are within bounds
  695.                         -- We could subtract a 1 in the local definition above, but if actual_x < 0 then the operation
  696.                         -- doesn't even happen :P
  697.                         if actual_x < 0 or actual_x > p_width - 1 or actual_y < 0 or actual_y > p_height - 1 then
  698.                             -- We can't get a pixel out of bounds of this parent, so we'll just roll with
  699.                             -- the default colour.
  700.                             --TODO: Would it be better if we'd look for any parent that *is* within bounds instead?
  701.                             --      Probably not, the result isn't visible anyway since that part of self is outside
  702.                             --      of the parent
  703.                             background_colour = DEFAULT_BACKGROUND
  704.                             break
  705.                         end
  706.  
  707.                         background_colour = local_parent[ actual_y * p_width + actual_x ][ -tracked_colour ]
  708.                         tracked_colour = background_colour
  709.  
  710.                     until background_colour > 0
  711.                 end
  712.  
  713.                 -- The same goes for foreground colour...
  714.                 if foreground_colour < 0 or pixel1 == TRANSPARENT_FOREGROUND then
  715.                     local_parent = false_parent
  716.                     tracked_offset_x = x
  717.                     tracked_offset_y = y
  718.  
  719.                     local tracked_colour = -2
  720.  
  721.                     repeat
  722.                         local_parent = local_parent.parent
  723.  
  724.                         if not local_parent then
  725.                             foreground_colour = DEFAULT_FOREGROUND
  726.                             break
  727.                         end
  728.  
  729.                         tracked_offset_x = tracked_offset_x + local_parent.x1
  730.                         tracked_offset_y = tracked_offset_y + local_parent.y1
  731.  
  732.                         local actual_y = y + tracked_offset_y
  733.                         local actual_x = x + tracked_offset_x
  734.                         local p_width  = local_parent.width
  735.                         local p_height = local_parent.height
  736.  
  737.                         if actual_x < 0 or actual_x > p_width - 1 or actual_y < 0 or actual_y > p_height - 1 then
  738.                             foreground_colour = DEFAULT_FOREGROUND
  739.                             break
  740.                         end
  741.  
  742.                         foreground_colour = local_parent[ actual_y * p_width + actual_x ][ -tracked_colour ]
  743.                         tracked_colour = foreground_colour
  744.  
  745.                     until foreground_colour > 0
  746.                 end
  747.  
  748.                 -- ...And finally for the character.
  749.                 -- Note that a colour cannot be transparent to a character, so
  750.                 -- there's no extra branch here
  751.                 local_parent = false_parent
  752.                 tracked_offset_x = x
  753.                 tracked_offset_y = y
  754.  
  755.                 while character == TRANSPARENT_CHARACTER do
  756.                     local_parent = local_parent.parent
  757.  
  758.                     if not local_parent then
  759.                         character = DEFAULT_CHARACTER
  760.                         break
  761.                     end
  762.  
  763.                     tracked_offset_x = tracked_offset_x + local_parent.x1
  764.                     tracked_offset_y = tracked_offset_y + local_parent.y1
  765.  
  766.                     local actual_y = y + tracked_offset_y
  767.                     local actual_x = x + tracked_offset_x
  768.                     local p_width = local_parent.width
  769.                     local p_height = local_parent.height
  770.  
  771.                     if actual_x < 0 or actual_x > p_width - 1 or actual_y < 0 or actual_y > p_height - 1 then
  772.                         character = DEFAULT_CHARACTER
  773.                         break
  774.                     end
  775.  
  776.                     character = local_parent[ actual_y * p_width + actual_x ][ 3 ]
  777.                 end
  778.  
  779.                 -- Now that we're certain we have non-transparent data, store them in a pixel
  780.                 -- We *could* use -1 and -2 indices, but there will be a lot of cases where
  781.                 -- one or both of them aren't used. Because creating arrays is faster
  782.                 -- (array creation can utilise SETLIST while table creation can only use
  783.                 -- SETTABLE) and this assignment has to be done for *all cases*, we'll limit
  784.                 -- the math to the lines concatenation down below.
  785.                 local underneath = {
  786.                     background_colour;
  787.                     foreground_colour;
  788.                     character;
  789.                 }
  790.  
  791.                 -- Assign the proper data to the rendered pixel
  792.                 line1[ line_index ] = lookup[ pixel1 > 0 and pixel1 or underneath[ -pixel1 ] ]
  793.                 line2[ line_index ] = lookup[ pixel2 > 0 and pixel2 or underneath[ -pixel2 ] ]
  794.                 line3[ line_index ] = ( pixel3 == TRANSPARENT_CHARACTER   and underneath[ 3 ] or pixel3 )
  795.             else
  796.                 -- That was quick! This is a speed up for the large number of cases
  797.                 -- in which no transparency is used
  798.                 line1[ line_index ] = lookup[ pixel1 ]
  799.                 line2[ line_index ] = lookup[ pixel2 ]
  800.                 line3[ line_index ] = pixel3
  801.             end
  802.  
  803.             line_index = line_index + 1
  804.         end
  805.  
  806.         lines[ i     ] = tconcat( line1, "" )
  807.         lines[ i + 1 ] = tconcat( line2, "" )
  808.         lines[ i + 2 ] = tconcat( line3, "" )
  809.  
  810.         i = i + 3
  811.     end
  812.  
  813.     return lines
  814. end
  815.  
  816. --- Get a CraftOS window-like interface for interaction with the buffer.
  817. --  The window is a valid term object, which means that the CraftOS term
  818. --  can be term.redirect()ed to it. Functions without a return value,
  819. --  unlike those in the native API, return the window object, allowing for
  820. --  method chaining.
  821. -- @param parent    The parent terminal, such as term.current() or a window
  822. -- @param x         (Optional) The x coordinate of the window object in parent, 1-based, defaults to 1
  823. -- @param y         (Optional) The y coordinate of the window object in parent, 1-based, defaults to 1
  824. -- @param width     (Optional) The width of the window object, defaults to self.width. If different from self.width, buffer will be resized
  825. -- @param height    (Optional) The height of the window object, defaults to self.height. If different from self.height, buffer will be resized
  826. -- @param visible   (Optional) Whether the window is visible (i.e. changes are propagated to parent in real time), defaults to false
  827. -- @param is_colour (Optional) Whether the window supports colour (will only display monochromatic colours otherwise),
  828. --                  defaults to parent.isColour()
  829. -- @return window   CraftOS-like window object
  830. function buffer_methods:get_window_interface( parent, x, y, width, height, visible, is_colour )
  831.     --TODO: Make parent optional (win.redirect?) or at least let it be a buffer
  832.     local win = {}
  833.  
  834.     x = x or 1
  835.     y = y or 1
  836.     width  = width  or self.width
  837.     height = height or self.height
  838.     -- Visibility defaults to false
  839.     visible = visible and true or false
  840.  
  841.     -- Colour defaults to parent.isColour()
  842.     if is_colour == nil then
  843.         is_colour = parent.isColour and parent.isColour()
  844.     end
  845.  
  846.     -- Retrieve information from parent
  847.     local cursor_blink = parent.getCursorBlink and parent.getCursorBlink() or false
  848.  
  849.     local background_colour = parent.getBackgroundColour()
  850.     local text_colour = parent.getTextColour()
  851.  
  852.     local cursor_x = 1
  853.     local cursor_y = 1
  854.  
  855.     function win.write( text )
  856.         local    x_offset = cursor_x - 1
  857.         local line_offset = ( cursor_y - 1 ) * width
  858.  
  859.         for i = 1, #text do
  860.             if x_offset >= width then
  861.                 break
  862.             end
  863.  
  864.             self[ line_offset + x_offset ] = { background_colour, text_colour, sub( text, i, i ) }
  865.             x_offset = x_offset + 1
  866.         end
  867.  
  868.         cursor_x = x_offset + 1
  869.  
  870.         if visible then
  871.             return win.redraw()
  872.         end
  873.  
  874.         return win
  875.     end
  876.  
  877.     function win.blit( text, text_colours, background_colours )
  878.         self:blit( cursor_x - 1, cursor_y - 1, text, background_colours, text_colours )
  879.  
  880.         if visible then
  881.             return win.redraw()
  882.         end
  883.  
  884.         return win
  885.     end
  886.  
  887.     function win.clear()
  888.         self:clear( background_colour )
  889.  
  890.         if visible then
  891.             return win.redraw()
  892.         end
  893.  
  894.         return win
  895.     end
  896.  
  897.     function win.clearLine()
  898.         self:clear_line( cursor_y - 1 )
  899.  
  900.         if visible then
  901.             return win.redraw()
  902.         end
  903.  
  904.         return win
  905.     end
  906.  
  907.     function win.getCursorPos()
  908.         return cursor_x, cursor_y
  909.     end
  910.  
  911.     function win.setCursorPos( new_x, new_y )
  912.         new_x = tonumber( new_x ) or error( expected_number_for .. "'new_x'", 2 )
  913.         new_y = tonumber( new_y ) or error( expected_number_for .. "'new_y'", 2 )
  914.  
  915.         cursor_x, cursor_y = new_x, new_y
  916.     end
  917.  
  918.     function win.setCursorBlink( bool )
  919.         cursor_blink = bool
  920.  
  921.         return win
  922.     end
  923.  
  924.     function win.isColor()
  925.         return is_colour
  926.     end
  927.  
  928.     function win.getSize()
  929.         return width, height
  930.     end
  931.  
  932.     function win.scroll( n )
  933.         self:scroll( n, background_colour )
  934.  
  935.         if visible then
  936.             return win.redraw()
  937.         end
  938.  
  939.         return win
  940.     end
  941.  
  942.     function win.setTextColor( colour )
  943.         text_colour = colour
  944.         return win
  945.     end
  946.  
  947.     function win.getTextColor()
  948.         return text_colour
  949.     end
  950.  
  951.     function win.setBackgroundColor( colour )
  952.         background_colour = colour
  953.         return win
  954.     end
  955.  
  956.     function win.getBackgroundColor()
  957.         return background_colour
  958.     end
  959.  
  960.     function win.setTextScale( _ )
  961.         -- Scale is ignored for now
  962.         return win
  963.     end
  964.  
  965.     function win.setVisible( visibility )
  966.         if visible ~= visibility then
  967.             visible = visibility
  968.  
  969.             if visible then
  970.                 return win.redraw()
  971.             end
  972.         end
  973.  
  974.         return win
  975.     end
  976.  
  977.     function win.redraw()
  978.         if visible then
  979.             self:render_to_window( parent, x, y )
  980.         end
  981.  
  982.         return win
  983.     end
  984.  
  985.     function win.restoreCursor()
  986.         parent.setCursorBlink( cursor_blink )
  987.         parent.setTextColor( text_colour )
  988.  
  989.         if cursor_x >= 1 and cursor_y >= 1 and cursor_x <= width and cursor_y <= height then
  990.             parent.setCursorPos( cursor_x + x - 1, cursor_y + y - 1 )
  991.         else
  992.             parent.setCursorPos( 0, 0 )
  993.         end
  994.  
  995.         return win
  996.     end
  997.  
  998.     function win.getPosition()
  999.         return x, y
  1000.     end
  1001.  
  1002.     function win.reposition( new_x, new_y, new_w, new_h )
  1003.         new_x = tonumber( new_x ) or error( expected_number_for .. "'new_x'", 2 )
  1004.         new_y = tonumber( new_y ) or error( expected_number_for .. "'new_y'", 2 )
  1005.  
  1006.         x = new_x
  1007.         y = new_y
  1008.  
  1009.         if tonumber( new_w ) and tonumber( new_h ) then
  1010.             self:resize( new_w, new_h, background_colour )
  1011.  
  1012.             width = new_w
  1013.             height = new_h
  1014.         end
  1015.  
  1016.         if visible then
  1017.             return win.redraw()
  1018.         end
  1019.  
  1020.         return win
  1021.     end
  1022.  
  1023.     -- Proper language!
  1024.     win.isColour            = win.isColor
  1025.     win.setTextColour       = win.setTextColor
  1026.     win.getTextColour       = win.getTextColor
  1027.     win.setBackgroundColour = win.setBackgroundColor
  1028.     win.getBackgroundColour = win.getBackgroundColor
  1029.  
  1030.     return win
  1031. end
  1032.  
  1033. --- Scroll the buffer contents.
  1034. -- @param lines             The number of lines to scroll
  1035. -- @param background_colour (Optional) The background colour to fill any empty pixels with, defaults to DEFAULT_BACKGROUND
  1036. -- @param foreground_colour (Optional) The foreground colour to fill any empty pixels with, defaults to DEFAULT_FOREGROUND
  1037. -- @param character         (Optional) The character to fill any empty pixels with, defaults to DEFAULT_CHARACTER
  1038. -- @return self
  1039. function buffer_methods:scroll( lines, background_colour, foreground_colour, character )
  1040.     lines = tonumber( lines ) or error( expected_number_for .. "'lines'", 2 )
  1041.  
  1042.     local n_self = self.length
  1043.     local width, height = self.width, self.height
  1044.  
  1045.     local new_pixel = {
  1046.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1047.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1048.         character                     or DEFAULT_CHARACTER;
  1049.     }
  1050.  
  1051.     if lines > 0 then
  1052.         local remove = table.remove
  1053.  
  1054.         -- Drop the pixels that will go off screen (scrolling upwards)
  1055.         -- Because 0 doesn't belong into a proper Lua array, we'll set
  1056.         -- the last pixel as the 0th index
  1057.         local last_removed
  1058.         for _ = 0, lines * width - 1 do
  1059.             last_removed = remove( self, 1 )
  1060.             n_self = n_self - 1
  1061.         end
  1062.  
  1063.         self[ 0 ] = last_removed
  1064.  
  1065.         -- Fill the extra pixels
  1066.         for i = n_self, width * height - 1 do
  1067.             self[ i ] = new_pixel
  1068.         end
  1069.  
  1070.     elseif lines < 0 then
  1071.         local insert = table.insert
  1072.  
  1073.         -- Drop the pixels that will go off screen (scrolling downwards)
  1074.         -- Note that lines is negative
  1075.         for i = n_self + lines * width, n_self - 1 do
  1076.             self[ i ] = nil
  1077.             n_self = n_self - 1
  1078.         end
  1079.  
  1080.         -- Insert new pixels at the top of the buffer
  1081.         -- Again, 0 isn't a proper array element, so we'll
  1082.         -- set it separately
  1083.         for i = 1, -lines * width do
  1084.             insert( self, i, new_pixel )
  1085.         end
  1086.  
  1087.         -- 0 is now one line lower
  1088.         self[ width ] = self[ 0 ]
  1089.         self[ 0 ] = new_pixel
  1090.     end
  1091.  
  1092.     return self
  1093. end
  1094.  
  1095. --- Clear the buffer.
  1096. -- @param background_colour (Optional) The background colour to clear with, defaults to DEFAULT_BACKGROUND
  1097. -- @param foreground_colour (Optional) The foreground colour to clear with, defaults to DEFAULT_FOREGROUND
  1098. -- @param character         (Optional) The character to clear with, defaults to DEFAULT_CHARACTER
  1099. -- @return self
  1100. function buffer_methods:clear( background_colour, foreground_colour, character )
  1101.     local clear_pixel = {
  1102.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1103.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1104.         character                     or DEFAULT_CHARACTER;
  1105.     }
  1106.  
  1107.     -- Go through all pixels and set them to the clear pixel
  1108.     for i = 0, self.length do
  1109.         self[ i ] = clear_pixel
  1110.     end
  1111.  
  1112.     return self
  1113. end
  1114.  
  1115. --- Clear a line of the buffer.
  1116. -- @param y                 The y coordinate indicating the line to clear, 0-based
  1117. -- @param background_colour (Optional) The background colour to clear with, defaults to DEFAULT_BACKGROUND
  1118. -- @param foreground_colour (Optional) The foreground colour to clear with, defaults to DEFAULT_FOREGROUND
  1119. -- @param character         (Optional) The character to clear with, defaults to DEFAULT_CHARACTER
  1120. -- @param start_x           (Optional) The x coordinate to start clearing on, 0-based, defaults to 0
  1121. -- @param end_x             (Optional) The x coordinate to stop clearing on, 0-based, defaults to self.width - 1
  1122. -- @return self
  1123. function buffer_methods:clear_line( y, background_colour, foreground_colour, character, start_x, end_x )
  1124.     y = tonumber( y ) or error( expected_number_for .. "'y'", 2 )
  1125.  
  1126.     if y < 0 or y >= self.height then
  1127.         return self
  1128.     end
  1129.  
  1130.     local clear_pixel = {
  1131.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1132.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1133.         character                     or DEFAULT_CHARACTER;
  1134.     }
  1135.  
  1136.     local w = self.width
  1137.  
  1138.     start_x = tonumber( start_x ) or 0
  1139.     start_x = start_x > 0 and start_x or 0
  1140.  
  1141.     end_x = tonumber( end_x ) or w
  1142.  
  1143.     -- Go through all pixels on this line and set them to clear_pixel
  1144.     local line_offset = y * w
  1145.     for x = start_x, ( end_x < ( w - 1 ) and end_x or w - 1 ) do
  1146.         self[ line_offset + x ] = clear_pixel
  1147.     end
  1148.  
  1149.     return self
  1150. end
  1151.  
  1152. --- Clear a column of the buffer.
  1153. -- @param x                 The x coordinate indicating the column to clear, 0-based
  1154. -- @param background_colour (Optional) The background colour to clear with, defaults to DEFAULT_BACKGROUND
  1155. -- @param foreground_colour (Optional) The foreground colour to clear with, defaults to DEFAULT_FOREGROUND
  1156. -- @param character         (Optional) The character to clear with, defaults to DEFAULT_CHARACTER
  1157. -- @param start_y           (Optional) The y coordinate to start clearing on, 0-based, defaults to 0
  1158. -- @param end_y             (Optional) The y coordinate to stop clearing on, 0-based, defaults to self.height - 1
  1159. -- @return self
  1160. function buffer_methods:clear_column( x, background_colour, foreground_colour, character, start_y, end_y )
  1161.     x = tonumber( x ) or error( expected_number_for .. "'x'", 2 )
  1162.  
  1163.     local w = self.width
  1164.  
  1165.     if x < 0 or x >= w then
  1166.         return self
  1167.     end
  1168.  
  1169.     local h = self.height
  1170.  
  1171.     local clear_pixel = {
  1172.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1173.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1174.         character                     or DEFAULT_CHARACTER;
  1175.     }
  1176.  
  1177.     start_y = tonumber( start_y )
  1178.     start_y = start_y > 0 and start_y or 0
  1179.  
  1180.     end_y = tonumber( end_y ) or h
  1181.  
  1182.     -- Go through all pixels in this column and set them to clear_pixel
  1183.     for y = start_y, end_y < h and end_y or h do
  1184.         self[ y * w + x ] = clear_pixel
  1185.     end
  1186.  
  1187.     return self
  1188. end
  1189.  
  1190. --- Write text to the buffer using the specified colours.
  1191. --  text, background_colours and foreground_colours have to
  1192. --  be of the same length
  1193. -- @param x                     The x coordinate to write at, 0-based
  1194. -- @param y                     The y coordinate to write at, 0-based
  1195. -- @param text                  The text to write
  1196. -- @param background_colours    The background colours to use, in paint format (characters)
  1197. -- @param foreground_colours    The foreground colours to use, in paint format (characters)
  1198. -- @return self
  1199. function buffer_methods:blit( x, y, text, background_colours, foreground_colours )
  1200.     x = tonumber( x ) or error( expected_number_for .. "'x'", 2 )
  1201.     y = tonumber( y ) or error( expected_number_for .. "'y'", 2 )
  1202.  
  1203.     local w = self.width
  1204.  
  1205.     local text_length = #text
  1206.  
  1207.     if text_length ~= #background_colours or text_length ~= #foreground_colours then
  1208.         error( "'text', 'background_colours', and 'foreground_colours' all have to be of the same length", 2 )
  1209.     end
  1210.  
  1211.     background_colours = gsub( background_colours, "%s", "f" )
  1212.     foreground_colours = gsub( foreground_colours, "%s", "0" )
  1213.  
  1214.     local offset = y * w + x - 1
  1215.     text_length  = ( text_length < ( w - x ) and text_length or w - x )
  1216.  
  1217.     for i = ( -x > 0 and -x or 0 ) + 1, text_length do
  1218.         -- A -1 for i is included in offset
  1219.         self[ offset + i ] = {
  1220.             colour_lookup[ sub( background_colours, i, i ) ];
  1221.             colour_lookup[ sub( foreground_colours, i, i ) ];
  1222.             sub( text, i, i );
  1223.         }
  1224.     end
  1225.  
  1226.     return self
  1227. end
  1228.  
  1229. --- Draw a filled, solid colour rectangle using two points.
  1230. --  The order of the two points does not matter, i.e. 2:2, 1:1 is the same
  1231. --  as 1:1, 2:2
  1232. -- @param start_x           The x coordinate to start drawing at, 0-based
  1233. -- @param start_y           The y coordinate to start drawing at, 0-based
  1234. -- @param end_x             The x coordinate to end drawing at, 0-based
  1235. -- @param end_y             The y coordinate to end drawing at, 0-based
  1236. -- @param background_colour (Optional) The background colour to fill the rectangle (including borders) with, defaults to DEFAULT_BACKGROUND
  1237. -- @param foreground_colour (Optional) The foreground colour to fill the rectangle (including borders) with, defaults to DEFAULT_FOREGROUND
  1238. -- @param character         (Optional) The character to fill the rectangle (including borders) with, defaults to DEFAULT_CHARACTER
  1239. -- @return self
  1240. function buffer_methods:draw_filled_rectangle_from_points( start_x, start_y, end_x, end_y, background_colour, foreground_colour, character )
  1241.     local w = self.width
  1242.  
  1243.     -- Starting values must be smaller than ending ones
  1244.     if start_x > end_x then
  1245.         end_x, start_x = start_x, end_x
  1246.     end
  1247.  
  1248.     if start_y > end_y then
  1249.         end_y, start_y = start_y, end_y
  1250.     end
  1251.  
  1252.     local new_pixel = {
  1253.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1254.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1255.         character                     or DEFAULT_CHARACTER;
  1256.     }
  1257.  
  1258.     -- Go through all pixels of this rectangle and set them to new_pixel
  1259.     for y = start_y, end_y do
  1260.         local line_offset = y * w
  1261.  
  1262.         for x = start_x, end_x do
  1263.             self[ line_offset + x ] = new_pixel
  1264.         end
  1265.     end
  1266.  
  1267.     return self
  1268. end
  1269.  
  1270. --- Draw a filled, solid colour rectangle using one point, width and height.
  1271. -- @param x                 The x coordinate to start drawing at, 0-based
  1272. -- @param y                 The y coordinate to start drawing at, 0-based
  1273. -- @param width             The width of the rectangle
  1274. -- @param height            The height of the rectangle
  1275. -- @param background_colour (Optional) The background colour to fill the rectangle (including borders) with, defaults to DEFAULT_BACKGROUND
  1276. -- @param foreground_colour (Optional) The foreground colour to fill the rectangle (including borders) with, defaults to DEFAULT_FOREGROUND
  1277. -- @param character         (Optional) The character to fill the rectangle (including borders) with, defaults to DEFAULT_CHARACTER
  1278. -- @return self
  1279. function buffer_methods:draw_filled_rectangle( x, y, width, height, background_colour, foreground_colour, character )
  1280.     local w = self.width
  1281.     local end_x = x + width - 1
  1282.  
  1283.     local new_pixel = {
  1284.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1285.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1286.         character                     or DEFAULT_CHARACTER;
  1287.     }
  1288.  
  1289.     -- Go through all pixels of this rectangle and set them to new_pixel
  1290.     for _y = y, y + height - 1 do
  1291.         local line_offset = _y * w
  1292.  
  1293.         for _x = x, end_x do
  1294.             self[ line_offset + _x ] = new_pixel
  1295.         end
  1296.     end
  1297.  
  1298.     return self
  1299. end
  1300.  
  1301. --- Draw a solid colour rectangle border using two points.
  1302. -- @param start_x           The x coordinate to start drawing at, 0-based
  1303. -- @param start_y           The y coordinate to start drawing at, 0-based
  1304. -- @param end_x             The x coordinate to end drawing at, 0-based
  1305. -- @param end_y             The y coordinate to end drawing at, 0-based
  1306. -- @param border_width      (Optional) The width of the border (larger values extending to the centre), defaults to 1
  1307. -- @param background_colour (Optional) The background colour to fill the rectangle border with, defaults to DEFAULT_BACKGROUND
  1308. -- @param foreground_colour (Optional) The foreground colour to fill the rectangle border with, defaults to DEFAULT_FOREGROUND
  1309. -- @param character         (Optional) The character to fill the rectangle border with, defaults to DEFAULT_CHARACTER
  1310. -- @return self
  1311. function buffer_methods:draw_rectangle_from_points( start_x, start_y, end_x, end_y, border_width,
  1312.                                                      background_colour, foreground_colour, character )
  1313.     border_width = border_width or 1
  1314.  
  1315.     -- Starting values must be smaller than ending ones
  1316.     if start_x > end_x then
  1317.         end_x, start_x = start_x, end_x
  1318.     end
  1319.  
  1320.     if start_y > end_y then
  1321.         end_y, start_y = start_y, end_y
  1322.     end
  1323.  
  1324.     -- Go through all lines and columns of this rectangle and clear them with the desired data
  1325.     -- (clear_line and clear_column will handle defaults for us)
  1326.     for i = 0, border_width - 1 do
  1327.         self:clear_line( start_y + i, background_colour, foreground_colour, character, start_x + i, end_x - i )
  1328.     end
  1329.  
  1330.     for i = 0, border_width - 1 do
  1331.         self:clear_line( end_y - i, background_colour, foreground_colour, character, start_x + i, end_x - i )
  1332.     end
  1333.  
  1334.     -- Columns have a little less work, so that corners aren't painted twice
  1335.     for i = 0, border_width - 1 do
  1336.         self:clear_column( start_x + i, background_colour, foreground_colour, character, start_y + i + 1, end_y - i - 1 )
  1337.     end
  1338.  
  1339.     for i = 0, border_width - 1 do
  1340.         self:clear_column( end_x - i, background_colour, foreground_colour, character, start_y + i + 1, end_y - i - 1 )
  1341.     end
  1342.  
  1343.     return self
  1344. end
  1345.  
  1346. --- Draw a solid colour rectangle border using one point, width and height.
  1347. -- @param x                 The x coordinate to start drawing at, 0-based
  1348. -- @param y                 The y coordinate to start drawing at, 0-based
  1349. -- @param width             The width of the rectangle
  1350. -- @param height            The height of the rectangle
  1351. -- @param border_width      (Optional) The width of the border (larger values extending to the centre)
  1352. -- @param background_colour (Optional) The background colour to fill the rectangle border with, defaults to DEFAULT_BACKGROUND
  1353. -- @param foreground_colour (Optional) The foreground colour to fill the rectangle border with, defaults to DEFAULT_FOREGROUND
  1354. -- @param character         (Optional) The character to fill the rectangle border with, defaults to DEFAULT_CHARACTER
  1355. -- @return Tail call of self:draw_rectangle_from_points(), resulting in self
  1356. function buffer_methods:draw_rectangle( x, y, width, height, border_width, background_colour, foreground_colour, character )
  1357.     return self
  1358.     :draw_rectangle_from_points( x, y, x + width - 1, y + height - 1, border_width, background_colour, foreground_colour, character )
  1359. end
  1360.  
  1361. --- Draw a circle outline using the centre point and radius, without corrections for CC rectangular pixels.
  1362. -- @param centre_x          The x coordinate of the circle's centre, 0-based
  1363. -- @param centre_y          The y coordinate of the circle's centre, 0-based
  1364. -- @param radius            The radius of the circle
  1365. -- @param background_colour (Optional) The background colour to fill the circle outline with, defaults to DEFAULT_BACKGROUND
  1366. -- @param foreground_colour (Optional) The foreground colour to fill the circle outline with, defaults to DEFAULT_FOREGROUND
  1367. -- @param character         (Optional) The character to fill the circle outline with, defaults to DEFAULT_CHARACTER
  1368. -- @return self
  1369. function buffer_methods:draw_circle_raw( centre_x, centre_y, radius, background_colour, foreground_colour, character )
  1370.     local w = self.width
  1371.  
  1372.     local new_pixel = {
  1373.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1374.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1375.         character                     or DEFAULT_CHARACTER;
  1376.     }
  1377.  
  1378.     local x = radius
  1379.     local y = 0
  1380.     local err = 0
  1381.  
  1382.     -- Generate points for the first octant, going counterclockwise
  1383.     -- and mirroring them to other octants
  1384.     while x >= y do
  1385.         self[ ( centre_y + y ) * w + centre_x + x ] = new_pixel
  1386.         self[ ( centre_y + x ) * w + centre_x + y ] = new_pixel
  1387.         self[ ( centre_y + x ) * w + centre_x - y ] = new_pixel
  1388.         self[ ( centre_y + y ) * w + centre_x - x ] = new_pixel
  1389.         self[ ( centre_y - y ) * w + centre_x - x ] = new_pixel
  1390.         self[ ( centre_y - x ) * w + centre_x - y ] = new_pixel
  1391.         self[ ( centre_y - x ) * w + centre_x + y ] = new_pixel
  1392.         self[ ( centre_y - y ) * w + centre_x + x ] = new_pixel
  1393.  
  1394.         y = y + 1
  1395.         err = err + 1 + 2 * y
  1396.  
  1397.         if 2 * ( err - x ) + 1 > 0 then
  1398.             x = x - 1
  1399.             err = err + 1 - 2 * x
  1400.         end
  1401.     end
  1402.  
  1403.     return self
  1404. end
  1405.  
  1406. --- Draw a circle outline using the centre point and radius, with corrections for CC rectangular pixels.
  1407. -- @param centre_x          The x coordinate of the circle's centre, 0-based
  1408. -- @param centre_y          The y coordinate of the circle's centre, 0-based
  1409. -- @param radius            The radius of the circle
  1410. -- @param background_colour (Optional) The background colour to fill the circle outline with, defaults to DEFAULT_BACKGROUND
  1411. -- @param foreground_colour (Optional) The foreground colour to fill the circle outline with, defaults to DEFAULT_FOREGROUND
  1412. -- @param character         (Optional) The character to fill the circle outline with, defaults to DEFAULT_CHARACTER
  1413. -- @return self
  1414. function buffer_methods:draw_circle( centre_x, centre_y, radius, background_colour, foreground_colour, character )
  1415.     local w = self.width
  1416.     local self_height = self.height
  1417.  
  1418.     local new_pixel = {
  1419.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1420.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1421.         character                     or DEFAULT_CHARACTER;
  1422.     }
  1423.  
  1424.     local x = radius
  1425.     local y = 0
  1426.     local err = 0
  1427.  
  1428.     -- Generate points for the first octant, going counterclockwise
  1429.     -- and mirroring them to other octants
  1430.     while x >= y do
  1431.         local this_point_y, this_point_x
  1432.  
  1433.         -- Segment
  1434.         this_point_y = ( centre_y + round( ( 2 / 3 ) * y ) )
  1435.         this_point_x = centre_x + x
  1436.  
  1437.         if this_point_y >= 0 and this_point_y < self_height and this_point_x >= 0 and this_point_x < w then
  1438.             self[ this_point_y * w + this_point_x ] = new_pixel
  1439.         end
  1440.  
  1441.         -- Segment
  1442.         this_point_y = ( centre_y + round( ( 2 / 3 ) * x ) )
  1443.         this_point_x = centre_x + y
  1444.  
  1445.         if this_point_y >= 0 and this_point_y < self_height and this_point_x >= 0 and this_point_x < w then
  1446.             self[ this_point_y * w + this_point_x ] = new_pixel
  1447.         end
  1448.  
  1449.         -- Segment
  1450.         this_point_y = ( centre_y + round( ( 2 / 3 ) * x ) )
  1451.         this_point_x = centre_x - y
  1452.  
  1453.         if this_point_y >= 0 and this_point_y < self_height and this_point_x >= 0 and this_point_x < w then
  1454.             self[ this_point_y * w + this_point_x ] = new_pixel
  1455.         end
  1456.  
  1457.         -- Segment
  1458.         this_point_y = ( centre_y + round( ( 2 / 3 ) * y ) )
  1459.         this_point_x = centre_x - x
  1460.  
  1461.         if this_point_y >= 0 and this_point_y < self_height and this_point_x >= 0 and this_point_x < w then
  1462.             self[ this_point_y * w + this_point_x ] = new_pixel
  1463.         end
  1464.  
  1465.         -- Segment
  1466.         this_point_y = ( centre_y - round( ( 2 / 3 ) * y ) )
  1467.         this_point_x = centre_x - x
  1468.  
  1469.         if this_point_y >= 0 and this_point_y < self_height and this_point_x >= 0 and this_point_x < w then
  1470.             self[ this_point_y * w + this_point_x ] = new_pixel
  1471.         end
  1472.  
  1473.         -- Segment
  1474.         this_point_y = ( centre_y - round( ( 2 / 3 ) * x ) )
  1475.         this_point_x = centre_x - y
  1476.  
  1477.         if this_point_y >= 0 and this_point_y < self_height and this_point_x >= 0 and this_point_x < w then
  1478.             self[ this_point_y * w + this_point_x ] = new_pixel
  1479.         end
  1480.  
  1481.         -- Segment
  1482.         this_point_y = ( centre_y - round( ( 2 / 3 ) * x ) )
  1483.         this_point_x = centre_x + y
  1484.  
  1485.         if this_point_y >= 0 and this_point_y < self_height and this_point_x >= 0 and this_point_x < w then
  1486.             self[ this_point_y * w + this_point_x ] = new_pixel
  1487.         end
  1488.  
  1489.         -- Segment
  1490.         this_point_y = ( centre_y - round( ( 2 / 3 ) * y ) )
  1491.         this_point_x = centre_x + x
  1492.  
  1493.         if this_point_y >= 0 and this_point_y < self_height and this_point_x >= 0 and this_point_x < w then
  1494.             self[ this_point_y * w + this_point_x ] = new_pixel
  1495.         end
  1496.  
  1497.  
  1498.         y = y + 1
  1499.         err = err + 1 + 2 * y
  1500.  
  1501.         if 2 * ( err - x ) + 1 > 0 then
  1502.             x = x - 1
  1503.             err = err + 1 - 2 * x
  1504.         end
  1505.     end
  1506.  
  1507.     return self
  1508. end
  1509.  
  1510. --- Draw a filled ellipse.
  1511. --  Sorry for the inconsistency here, this code has been borrowed
  1512. --  from Xenthera's graphics library
  1513. -- @param x                 The x coordinate of the ellipse's centre
  1514. -- @param y                 The y coordinate of the ellipse's centre
  1515. -- @param width             The width  of the ellipse
  1516. -- @param height            The height of the ellipse
  1517. -- @param background_colour (Optional) The background colour to fill the ellipse with, defaults to DEFAULT_BACKGROUND
  1518. -- @param foreground_colour (Optional) The foreground colour to fill the ellipse with, defaults to DEFAULT_FOREGROUND
  1519. -- @param character         (Optional) The character to fill the ellipse with, defaults to DEFAULT_CHARACTER
  1520. -- @return self
  1521. function buffer_methods:draw_filled_ellipse( x, y, width, height, background_colour, foreground_colour, character )
  1522.     local self_width  = self.width
  1523.     local self_height = self.height
  1524.  
  1525.     local hh = height * height
  1526.     local ww = width * width
  1527.     local hhww = hh * ww
  1528.     local x0 = width - 1
  1529.     local delta_x = 0
  1530.  
  1531.     local new_pixel = {
  1532.         tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1533.         tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1534.         character                     or DEFAULT_CHARACTER;
  1535.     }
  1536.  
  1537.     if y < self_height and y >= 0 then
  1538.         local offset = y * self_width
  1539.  
  1540.         for _x = ( x - width ) > 0 and ( x - width ) or 0, ( ( x + width ) < ( self_width - 1 ) and ( x + width ) or self_width - 1 ) do
  1541.             self[ offset + _x ] = new_pixel
  1542.         end
  1543.     end
  1544.  
  1545.     for _y = 0, height - 1 do
  1546.         local x1 = x0 - ( delta_x - 1 )
  1547.  
  1548.         for _ = x1, 0, -1 do
  1549.             if ( x1 * x1 * hh + _y * _y * ww <= hhww ) then
  1550.                 break
  1551.             end
  1552.  
  1553.             x1 = x1 - 1
  1554.         end
  1555.  
  1556.         delta_x = x0 - x1
  1557.         x0 = x1
  1558.  
  1559.         local y_pos1 = y - _y
  1560.         local y_pos2 = y + _y
  1561.  
  1562.         if y_pos1 >= 0 and y_pos1 < self_height then
  1563.             y_pos1 = y_pos1 * self_width
  1564.  
  1565.             for _x = ( x - x0 ) > 0 and ( x - x0 ) or 0, ( ( x + x0 ) < ( self_width - 1 ) and ( x + x0 ) or self_width - 1 ) do
  1566.                 self[ y_pos1 + _x ] = new_pixel
  1567.             end
  1568.         end
  1569.  
  1570.         if y_pos2 >= 0 and y_pos2 < self_height then
  1571.             y_pos2 = y_pos2 * self_width
  1572.  
  1573.             for _x = ( x - x0 ) > 0 and ( x - x0 ) or 0, ( ( x + x0 ) < ( self_width - 1 ) and ( x + x0 ) or self_width - 1 ) do
  1574.                 self[ y_pos2 + _x ] = new_pixel
  1575.             end
  1576.         end
  1577.     end
  1578.  
  1579.     return self
  1580. end
  1581.  
  1582. --- Write text to the buffer.
  1583. -- @param x                 The x coordinate to start writing at, 0-based
  1584. -- @param y                 The y coordinate to start writing at, 0-based
  1585. -- @param text              The text to write
  1586. -- @param background_colour (Optional) The background colour to use, defaults to DEFAULT_BACKGROUND
  1587. -- @param foreground_colour (Optional) The foreground colour to use, defaults to DEFAULT_FOREGROUND
  1588. -- @return self
  1589. function buffer_methods:write( x, y, text, background_colour, foreground_colour )
  1590.     background_colour = background_colour or DEFAULT_BACKGROUND
  1591.     foreground_colour = foreground_colour or DEFAULT_FOREGROUND
  1592.  
  1593.     local width = self.width
  1594.  
  1595.     if x < width and y >= 0 and y < self.height then
  1596.         local len = #text
  1597.  
  1598.         if len == 1 then
  1599.             -- Avoid calling sub() altogether if the string is only a single character
  1600.             if x >= 0 then
  1601.                 self[ y * width + x ] = {
  1602.                     background_colour;
  1603.                     foreground_colour;
  1604.                     text;
  1605.                 }
  1606.             end
  1607.  
  1608.         else
  1609.             local line_offset = y * width + x - 1
  1610.             local last_pixel, last_char
  1611.  
  1612.             -- Go through the string, writing the new pixels
  1613.             for i = ( -x > 0 and -x or 0 ) + 1, ( len < ( width - x ) and len or width - x ) do
  1614.                 local ch = sub( text, i, i )
  1615.  
  1616.                 -- Reduce memory usage whenever there are two identical characters
  1617.                 if ch ~= last_char then
  1618.                     last_pixel = {
  1619.                         background_colour;
  1620.                         foreground_colour;
  1621.                         ch;
  1622.                     }
  1623.  
  1624.                     last_char = ch
  1625.                 end
  1626.  
  1627.                 -- A -1 for i is included in line_offset
  1628.                 local index = line_offset + i
  1629.  
  1630.                 self[ index ] = last_pixel
  1631.             end
  1632.         end
  1633.     end
  1634.  
  1635.     return self
  1636. end
  1637.  
  1638. --- Fix errors in the buffer, such as missing pixels or unknown colours.
  1639. --  Fixes the following:
  1640. --  * Missing pixels
  1641. --  * Strings longer than one character for pixel[3]
  1642. --  * Missing or modified methods
  1643. --  * Missing length
  1644. --  * Missing x/y position
  1645. --  * Unknown colours
  1646. -- @param start_x           (Optional) The x coordinate to start repairing at, 0-based, defaults to 0
  1647. -- @param start_y           (Optional) The y coordinate to start repairing at, 0-based, defaults to 0
  1648. -- @param end_x             (Optional) The x coordinate to end repairing at, 0-based, defaults to self.width - 1
  1649. -- @param end_y             (Optional) The y coordinate to end repairing at, 0-based, defaults to self.height - 1
  1650. -- @param background_colour (Optional) The background colour to replace a broken pixel with, defaults to DEFAULT_BACKGROUND
  1651. -- @param foreground_colour (Optional) The foreground colour to replace a broken pixel with, defaults to DEFAULT_FOREGROUND
  1652. -- @param character         (Optional) The character to replace a broken pixel with, defaults to DEFAULT_CHARACTER
  1653. -- @return self, number of errors found
  1654. function buffer_methods:repair( start_x, start_y, end_x, end_y, background_colour, foreground_colour, character )
  1655.     start_x = start_x or 0
  1656.     start_y = start_y or 0
  1657.  
  1658.     end_x = end_x or ( self.width and self.width - 1 or error( "self.width is not present, buffer is unrepairable", 2 ) )
  1659.     end_y = end_y or ( self.height and self.height - 1 or error( "self.height is not present, buffer is unrepairable", 2 ) )
  1660.  
  1661.     background_colour = background_colour or DEFAULT_BACKGROUND
  1662.     foreground_colour = foreground_colour or DEFAULT_FOREGROUND
  1663.     character         = character         or DEFAULT_CHARACTER
  1664.  
  1665.     local w = self.width
  1666.     local n_errors = 0
  1667.  
  1668.     local new_pixel = {
  1669.         background_colour;
  1670.         foreground_colour;
  1671.         character;
  1672.     }
  1673.  
  1674.     -- Cache pixels that were already corrected
  1675.     local corrected_pixels = {}
  1676.  
  1677.     -- Go through all pixels in the to-be-repaired area
  1678.     for y = start_y, end_y do
  1679.         local line_offset = y * w
  1680.  
  1681.         for x = start_x, end_x do
  1682.             local pixel = self[ line_offset + x ]
  1683.  
  1684.             if not pixel or not corrected_pixels[ pixel ] then
  1685.                 if type( pixel ) ~= "table" then
  1686.                     -- The pixel is not here at all, set it to a blank pixel
  1687.                     n_errors = n_errors + 1
  1688.                     self[ line_offset + x ] = new_pixel
  1689.  
  1690.                 else
  1691.                     -- The pixel is here, check all data
  1692.                     if type( pixel[ 1 ] ) ~= "number" or not colour_lookup[ pixel[ 1 ] ] then
  1693.                         n_errors = n_errors + 1
  1694.                         pixel[ 1 ] = background_colour
  1695.                     end
  1696.  
  1697.                     if type( pixel[ 2 ] ) ~= "number" or not colour_lookup[ pixel[ 2 ] ] then
  1698.                         n_errors = n_errors + 1
  1699.                         pixel[ 2 ] = foreground_colour
  1700.                     end
  1701.  
  1702.                     if type( pixel[ 3 ] ) ~= "string" or #pixel[ 3 ] ~= 1 then
  1703.                         n_errors = n_errors + 1
  1704.                         pixel[ 3 ] = character
  1705.                     end
  1706.  
  1707.                     corrected_pixels[ pixel ] = true
  1708.                 end
  1709.             end
  1710.         end
  1711.     end
  1712.  
  1713.     -- This will hopefully force the GC to free the memory ASAP
  1714.     corrected_pixels = nil
  1715.  
  1716.     -- Check that all methods of the buffer are present and were not
  1717.     -- modified, and reset those that aren't/were
  1718.     for name, fn in pairs( buffer_methods ) do
  1719.         if self[ name ] ~= fn then
  1720.             n_errors = n_errors + 1
  1721.             self[ name ] = fn
  1722.         end
  1723.     end
  1724.  
  1725.     if not self.length then
  1726.         self.length = self.width * self.height - 1
  1727.         n_errors = n_errors + 1
  1728.     end
  1729.  
  1730.     if not self.x1 then
  1731.         self.x1 = 0
  1732.         n_errors = n_errors + 1
  1733.     end
  1734.  
  1735.     if not self.y2 then
  1736.         self.y2 = 0
  1737.         n_errors = n_errors + 1
  1738.     end
  1739.  
  1740.     return self, n_errors
  1741. end
  1742.  
  1743. --- Iterate over the buffer.
  1744. -- @param start_x   (Optional) The x coordinate to start iterating from, 0-based, defaults to 0
  1745. -- @param start_y   (Optional) The y coordinate to start iterating from, 0-based, defaults to 0
  1746. -- @param end_x     (Optional) The x coordinate to end iterating at, 0-based, defaults to self.width - 1
  1747. -- @param end_y     (Optional) The y coordinate to end iterating at, 0-based, defaults to self.height - 1
  1748. -- @return fn An iterator function which, when called, returns the next pixel, its coordinates and its index
  1749. function buffer_methods:iter( start_x, start_y, end_x, end_y )
  1750.     local w = self.width
  1751.  
  1752.     local x = ( start_x or 0 ) - 1
  1753.     local y = start_y or 0
  1754.  
  1755.     end_x   = end_x   or self.width - 1
  1756.     end_y   = end_y   or self.height - 1
  1757.  
  1758.     local line_offset = y * w
  1759.  
  1760.     return function()
  1761.         x = x + 1
  1762.  
  1763.         if x > end_x then
  1764.             y = y + 1
  1765.  
  1766.             if y > end_y then
  1767.                 return nil
  1768.             end
  1769.  
  1770.             line_offset = y * w
  1771.             x = 0
  1772.         end
  1773.  
  1774.         local index = line_offset + x
  1775.  
  1776.         return self[ index ], x, y, index
  1777.     end
  1778. end
  1779.  
  1780. --[[
  1781. --- Shrink the buffer contents using teletext characters from CC 1.76+
  1782. -- @param target    (Optional) The target buffer, defaults to self.parent
  1783. -- @param x         (Optional) The x coordinate in target to shrink self to, 0-based, defaults to self.x1
  1784. -- @param y         (Optional) The y coordinate in target to shrink self to, 0-based, defaults to self.y1
  1785. -- @param start_x   (Optional) The x coordinate in self to start shrinking from, 0-based, defaults to 0
  1786. -- @param start_y   (Optional) The y coordinate in self to start shrinking from, 0-based, defaults to 0
  1787. -- @param end_x     (Optional) The x coordinate in self to stop shrinking at, 0-based, defaults to self.width - 1
  1788. -- @param end_y     (Optional) The y coordinate in self to stop shrinking at, 0-based, defaults to self.height - 1
  1789. -- @return self
  1790. function buffer_methods:shrink( target, x, y, start_x, start_y, end_x, end_y )
  1791.     target = target or self.parent or error( unable_to_set_optional_argument .. "'target': self.parent is nil", 2 )
  1792.     x      = x      or self.x1     or error( unable_to_set_optional_argument .. "'x': self.x1 is nil", 2 )
  1793.     y      = y      or self.y1     or error( unable_to_set_optional_argument .. "'y': self.y1 is nil", 2 )
  1794.  
  1795.    
  1796.  
  1797.     return self
  1798. end
  1799. --]]
  1800.  
  1801. -- Aliases
  1802. buffer_methods.render_to_term = buffer_methods.render_to_window
  1803.  
  1804. buffer_methods.draw_rect = buffer_methods.draw_rectangle
  1805. buffer_methods.draw_rect_from_points = buffer_methods.draw_rectangle_from_points
  1806. buffer_methods.draw_filled_rect = buffer_methods.draw_filled_rectangle
  1807. buffer_methods.draw_filled_rect_from_points = buffer_methods.draw_filled_rectangle_from_points
  1808.  
  1809. buffer_methods.iterate = buffer_methods.iter
  1810.  
  1811. --- Create a new buffer using one point, width and height.
  1812. -- @param x                 (Optional) The x coordinate of the buffer in parent, 0-based, defaults to 0
  1813. -- @param y                 (Optional) The y coordinate of the buffer in parent, 0-based, defaults to 0
  1814. -- @param width             (Optional) The width of the buffer, defaults to 0
  1815. -- @param height            (Optional) The height of the buffer, defaults to 0
  1816. -- @param parent            This buffer's render target
  1817. -- @param background_colour (Optional) The background colour to prefill the buffer with, defaults to DEFAULT_BACKGROUND
  1818. -- @param foreground_colour (Optional) The foreground colour to prefill the buffer with, defaults to DEFAULT_FOREGROUND
  1819. -- @param character         (Optional) The character to prefill the buffer with, defaults to DEFAULT_CHARACTER
  1820. -- @param no_prefill        (Optional) Disable prefilling of the buffer, defaults to false
  1821. -- @param existing_table    (Optional) Use this table for the object instead of creating a new one
  1822. -- @return Tail call of buffer.new_from_points(), resulting in the new buffer
  1823. -- @see buffer.new_from_points
  1824. function buffer.new( x, y, width, height, parent, background_colour, foreground_colour, character, no_prefill, existing_table )
  1825.     return buffer.new_from_points( x, y, x and ( x + width - 1 ) or 0, y and ( y + height - 1 ) or 0, parent,
  1826.         background_colour, foreground_colour, character, no_prefill, existing_table  )
  1827. end
  1828.  
  1829. --- Create a new buffer using two points.
  1830. -- @param x1                (Optional) The x coordinate of the first point of the buffer in parent, 0-based, defaults to 0
  1831. -- @param y1                (Optional) The y coordinate of the first point of the buffer in parent, 0-based, defaults to 0
  1832. -- @param x2                (Optional) The x coordinate of the second point of the buffer in parent, 0-based, defaults to 0
  1833. -- @param y2                (Optional) The y coordinate of the second point of the buffer in parent, 0-based, defaults to 0
  1834. -- @param parent            This buffer's render target
  1835. -- @param background_colour (Optional) The background colour to prefill the buffer with, defaults to DEFAULT_BACKGROUND
  1836. -- @param foreground_colour (Optional) The foreground colour to prefill the buffer with, defaults to DEFAULT_FOREGROUND
  1837. -- @param character         (Optional) The character to prefill the buffer with, defaults to DEFAULT_CHARACTER
  1838. -- @param no_prefill        (Optional) Disable prefilling of the buffer, defaults to false
  1839. -- @param existing_table    (Optional) Use this table for the object instead of creating a new one
  1840. -- @return buffer           The new buffer
  1841. function buffer.new_from_points( x1, y1, x2, y2, parent, background_colour, foreground_colour, character, no_prefill, existing_table )
  1842.     local n = setmetatable( existing_table or {}, buffer_metatable )
  1843.  
  1844.     x1 = x1 or 0
  1845.     y1 = y1 or 0
  1846.     x2 = x2 or 0
  1847.     y2 = y2 or 0
  1848.  
  1849.     -- Ending values must be larger than starting ones
  1850.     if x1 > x2 then
  1851.         x2, x1 = x1, x2
  1852.     end
  1853.  
  1854.     if y1 > y2 then
  1855.         y2, y1 = y1, y2
  1856.     end
  1857.  
  1858.     local width  = x2 - x1 + 1
  1859.     local height = y2 - y1 + 1
  1860.  
  1861.     if not no_prefill then
  1862.         local new_pixel = {
  1863.             tonumber( background_colour ) or DEFAULT_BACKGROUND;
  1864.             tonumber( foreground_colour ) or DEFAULT_FOREGROUND;
  1865.             character                     or DEFAULT_CHARACTER;
  1866.         }
  1867.  
  1868.         -- Prefill the buffer with new_pixel
  1869.         for y = 0, height - 1 do
  1870.             local line_offset = y * width
  1871.  
  1872.             for x = 0, width - 1 do
  1873.                 n[ line_offset + x ] = new_pixel
  1874.             end
  1875.         end
  1876.     end
  1877.  
  1878.     -- Copy the buffer methods to this instance
  1879.     for k, fn in pairs( buffer_methods ) do
  1880.         n[ k ] = fn
  1881.     end
  1882.  
  1883.     n.x1 = x1
  1884.     n.y1 = y1
  1885.     n.x2 = x2
  1886.     n.y2 = y2
  1887.  
  1888.     n.width  = width
  1889.     n.height = height
  1890.     n.length = width * height - 1
  1891.     n.parent = parent
  1892.  
  1893.     -- Metadata
  1894.     n.__type = "desktox.buffer"
  1895.  
  1896.     return n
  1897. end
  1898.  
  1899. --- Syntactic sugar for both :render() and :render_to_window().
  1900. --  Will attempt to decide which of the 2 methods to use, based
  1901. --  on self.parent and given arguments. Defaults to
  1902. --  :render_to_window(). Should be *avoided* for best performance.
  1903. -- @param target    The buffer or window to render to
  1904. -- @param ...       Any arguments passed to :render() or :render_to_window()
  1905. -- @return Tail call of :render() or :render_to_window()
  1906. function buffer_metatable:__call( target, ... )
  1907.     if self.parent or ( type( target ) == "table" and target.__type == "desktox.buffer" ) then
  1908.         return self:render( target, ... )
  1909.     else
  1910.         return self:render_to_window( target, ... )
  1911.     end
  1912. end
  1913.  
  1914. -- Exports
  1915. buffer.clone  = buffer_methods.clone
  1916. buffer.repair = buffer_methods.repair
  1917.  
  1918. -- Export the complete method table
  1919. buffer.methods = buffer_methods
  1920.  
  1921. -- Export utility functions
  1922. buffer.round = round
  1923. buffer.shrink_to_character = shrink_to_character
  1924.  
  1925. -- Export the default values
  1926. buffer.DEFAULT_BACKGROUND = DEFAULT_BACKGROUND
  1927. buffer.DEFAULT_FOREGROUND = DEFAULT_FOREGROUND
  1928. buffer.DEFAULT_CHARACTER  = DEFAULT_CHARACTER
  1929.  
  1930. return buffer
Advertisement
Add Comment
Please, Sign In to add comment