Skip to content

Commit

Permalink
simplified Areas class tree. Haven't got DisplayBorder working yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Radvendii committed Aug 3, 2015
1 parent c9f3e6a commit e0c854c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 62 deletions.
104 changes: 43 additions & 61 deletions display.rb
Expand Up @@ -29,35 +29,35 @@ def initialize(x, y, func)

def display
@func.call.each do |ch|
Termbox.tb_change_cell ch.x+@x, ch.y+@y, ch.ord, ch.fg, ch.bg
Termbox.tb_change_cell ch.x+@x, ch.y+@y, ch.ord, ch.fg, ch.bg
end
end
end

module Bounded
#requires
#self.interior
#or
#self.border
def border
ret = []
interior = self.interior
interior.each do |p|
p.neighborhood.each do |n|
ret << n unless interior.include? n
end
end
return ret
class AreaBounded < NewArea
attr_writer :border, :interior
def initialize(x, y, func, type, border: nil, interior: nil)
super x, y, func
@border = border
@interior = interior
end

def borderDisp
border.map do |p|
Char.new(p[0], p[1], ' '.ord, 0, 7)
def border
@border || Proc do
ret = []
interior = self.interior
interior.each do |p|
p.neighborhood.each do |n|
ret << n unless interior.include? n
end
end
return ret
end
end

def interior
self.border.partition {|p| p[0]}.flat_map do |p|
@interior ||
self.border.partition {|p| p[0]}.flat_map do |p|
p.sort {|a, b| a[1] <=> b[1]}
.each_slice(2).flat_map do |ps|
if ps.length == 1
Expand All @@ -74,66 +74,48 @@ def interior?(p)
end

def display
self.borderDisp.each do |ch|
Termbox.tb_change_cell ch.x+@x, ch.y+@y, ch.ord, ch.fg, ch.bg
@func.call.each do |ch|
Termbox.tb_change_cell ch.x+@x, ch.y+@y, ch.ord, ch.fg, ch.bg if self.interior? [ch.x, ch.y]
end
end
end

module Clip
include Bounded
module DisplayBorder
def borderDisp
border.map do |p|
Char.new(p[0], p[1], ' '.ord, 0, 7)
end
end

def display
super
@func.call.each do |ch|
Termbox.tb_change_cell ch.x+@x, ch.y+@y, ch.ord, ch.fg, ch.bg if self.interior? [ch.x, ch.y]
self.borderDisp.each do |ch|
Termbox.tb_change_cell ch.x+@x, ch.y+@y, ch.ord, ch.fg, ch.bg
end
end
end
#TODO: MAYBE ADD A METHOD TO AREA THAT JUST RUNS @FUNC.CALL SO THAT WE CAN ADD DIFFERENT WAYS OF INTERPRETING THE DATA FROM @FUNC (LIKE TAIL)
module Wrap

#TODO: MAYBE ADD A METHOD TO AREA THAT JUST RUNS @FUNC.CALL SO THAT WE CAN ADD DIFFERENT WAYS OF INTERPRETING THE DATA FROM @FUNC (LIKE TAIL)

class AreaWrap < AreaBounded
#requires that @func only returns Chars at positive coordinates
include Bounded
def display
super
grid=[]
rows=0
@func.call.partition {|ch| ch.y}.each do |p| p.each do |ch|
if self.interior? [ch.x, ch.y+rows]
Termbox.tb_change_cell ch.x+@x, ch.y+@y+rows, ch.ord, ch.fg, ch.bg
else
limit = self.interior.select {|p| p[1] == ch.y+rows}.max {|p| p[0]}
if limit.nil? then return end
p.each do |och|
och.x -= limit[0]
end
rows += 1
redo
if self.interior? [ch.x, ch.y+rows]
Termbox.tb_change_cell ch.x+@x, ch.y+@y+rows, ch.ord, ch.fg, ch.bg
else
limit = self.interior.select {|p| p[1] == ch.y+rows}.max {|p| p[0]}
if limit.nil? then return end
p.each do |och|
och.x -= limit[0]
end
rows += 1
redo
end
end
end
end

class BoundedArea < NewArea
attr_writer :border, :interior
include Bounded
def initialize(x, y, func, type, border: nil, interior: nil)
if type == :wrap
self.extend Wrap
elsif type == :clip
self.extend Clip
end
super x, y, func
@border = border
@interior = interior
end

def border
@border || super
end

def interior
@interior || super
end
end

Expand Down
2 changes: 1 addition & 1 deletion main.rb
Expand Up @@ -79,7 +79,7 @@ def main()
#Display::AREAS << Display::Area.new([1,1], [21,11], Maptest::JOHN.method("disp"), :clip)
#Display::AREAS << Display::Area.new([20,20], [40,30], Maptest::MAP.method("disp"), :clip)
#Display::AREAS << Display::Area.new([50,1], [100,50], proc {Display.text.disp}, :clip)
Display::AREAS << test = Display::BoundedArea.new(2, 2, proc {Display.text.disp}, :wrap, border: (-1..10).flat_map {|i| [[i,-1], [-1,i], [10,i], [i,10]]}.uniq)
Display::AREAS << test = Display::AreaBounded.new(2, 2, proc {Display.text.disp}, :wrap, border: (-1..10).flat_map {|i| [[i,-1], [-1,i], [10,i], [i,10]]}.uniq)
loop do
main
end
Expand Down

0 comments on commit e0c854c

Please sign in to comment.