Properly structure HEX_BGs class

This commit is contained in:
Mewrry the Kitty 2025-11-29 23:37:18 -06:00
parent def877cd92
commit adf99db206

44
main.rb
View file

@ -60,38 +60,44 @@ def recalculate_hexagons
puts "(#{$hex_points[3][0]},#{$hex_points[3][1]})".center(HEX_DEBUG_WIDTH) puts "(#{$hex_points[3][0]},#{$hex_points[3][1]})".center(HEX_DEBUG_WIDTH)
$hex_x_offset = $hex_width/2 $hex_x_offset = $hex_width/2
$hex_y_offset = $hex_tip + $hex_mid $hex_y_offset = $hex_tip + $hex_mid
$hex_bgs.clear $hex_bgs.clear!
end end
class HexEditXref_BgGrphStore class HexEditXref_BgGrphStore
def initialize def initialize
clear!
end
def clear!
@hex_ids = Hash.new #hexid => color @hex_ids = Hash.new #hexid => color
@hex_refs = Hash.new #color => [num_refs, hexids] @hex_refs = Hash.new #color => hexids
@hex_bgs = Hash.new #color => bg @hex_bgs = Hash.new #color => bg
end end
def register(hexid,color) def register(hexid,color)
argb=color.argb raise TypeError, ("color must be a Gosu::Color, not a '#{color.class}'"
) unless color.is_a? Gosu::Color
argb = color.argb
return if (@hex_ids.has_key?(hexid) &&
@hex_ids[hexid] == argb)
unregister hexid unregister hexid
@hex_ids[hexid] = argb @hex_ids[hexid] = argb
if @hex_refs.has_key? argb if @hex_refs.has_key? argb
@hex_refs[argb][0] += 1 @hex_refs[argb].add(hexid)
unless @hex_refs[argb][1].include? hexid else
@hex_refs[argb][1].append hexid @hex_refs[argb] = Set.new([hexid, ])
end
end end
render argb render argb
end end
def unregister(hexid) def unregister(hexid)
if @hex_ids.has_key? hexid return unless @hex_ids.has_key? hexid
# ➣ If the key doesn't exist, assume
# the call was made mistakenly.
# Note: may cause leaks later
argb = @hex_ids[hexid] argb = @hex_ids[hexid]
@hex_ids.delete hexid @hex_ids.delete hexid
end
if @hex_refs.has_key? argb if @hex_refs.has_key? argb
if @hex_refs[argb][1].include? hexid @hex_refs[argb].delete hexid
@hex_refs[argb][1].delete hexid if @hex_refs[argb].size < 1
@hex_refs[argb][0] -= 1
end
if @hex_refs[argb][0] < 1
@hex_refs.delete argb @hex_refs.delete argb
end end
end end
@ -101,6 +107,8 @@ class HexEditXref_BgGrphStore
end end
end end
def [](color) def [](color)
raise TypeError, ("color must be a Gosu::Color, not a '#{color.class}'"
) unless color.is_a? Gosu::Color
argb = color.argb argb = color.argb
raise RuntimeError.new( raise RuntimeError.new(
"Color #{argb} was requested from "+ "Color #{argb} was requested from "+
@ -109,9 +117,10 @@ class HexEditXref_BgGrphStore
) unless @hex_bgs.has_key? argb ) unless @hex_bgs.has_key? argb
return @hex_bgs[argb] return @hex_bgs[argb]
end end
private
def render(argb) def render(argb)
color = Gosu::Color.new(argb) color = Gosu::Color.new(argb)
@hex_bgs[argb] = [Gosu::record($hex_width, $hex_mid+($hex_tip*2)) { @hex_bgs[argb] = Gosu::record($hex_width, $hex_mid+($hex_tip*2)) {
# Double Quad Method (0145, 1234): # Double Quad Method (0145, 1234):
Gosu::draw_quad( # 0145 Gosu::draw_quad( # 0145
$hex_points[0][0], $hex_points[0][1], color, #0 $hex_points[0][0], $hex_points[0][1], color, #0
@ -127,8 +136,7 @@ class HexEditXref_BgGrphStore
$hex_points[4][0], $hex_points[4][1], color, #4 $hex_points[4][0], $hex_points[4][1], color, #4
0 0
) )
},Time.now }
]
end end
end end
@ -339,7 +347,7 @@ class HexagonTerminalWindow < Gosu::Window
end end
end end
$hex_bgs = HexBgCache.new $hex_bgs = HexEditXref_BgGrphStore.new
recalculate_hexagons recalculate_hexagons
window = HexagonTerminalWindow.new(800, 600, false) window = HexagonTerminalWindow.new(800, 600, false)
window.show window.show