Class: CryptopunksGui

Inherits:
Object
  • Object
show all
Includes:
Glimmer
Defined in:
app/cryptopunks_gui.rb

Constant Summary collapse

PALETTES =
['Standard'] + (Palette8bit.constants).map(&:name).map {|palette| palette.split('_').map(&:capitalize).join(' ')}.reject { |palette| palette.include?(' ') }
STYLES =
['Normal', 'Flip', 'Mirror', 'Led', 'Sketch']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCryptopunksGui

Returns a new instance of CryptopunksGui.



18
19
20
21
22
23
24
25
# File 'app/cryptopunks_gui.rb', line 18

def initialize
  initialize_punks
  initialize_defaults
  observe_image_attribute_changes
  create_gui
  self.punk_index = 0
  @root.open
end

Instance Attribute Details

#paletteObject

Returns the value of attribute palette.



16
17
18
# File 'app/cryptopunks_gui.rb', line 16

def palette
  @palette
end

#punk_indexObject

Returns the value of attribute punk_index.



16
17
18
# File 'app/cryptopunks_gui.rb', line 16

def punk_index
  @punk_index
end

#styleObject

Returns the value of attribute style.



16
17
18
# File 'app/cryptopunks_gui.rb', line 16

def style
  @style
end

#zoomObject

Returns the value of attribute zoom.



16
17
18
# File 'app/cryptopunks_gui.rb', line 16

def zoom
  @zoom
end

Instance Method Details

#create_guiObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/cryptopunks_gui.rb', line 79

def create_gui
  @root = root {
    title 'CryptoPunks GUI'
    iconphoto File.expand_path('../icons/cryptopunks-gui.png', __dir__)
    
    frame {
      label {
        text 'Select Punk Index and Zoom To Mint Cryptopunk'
        font weight: 'bold'
      }
      
      label {
        text 'Punk Index:'
      }
      spinbox {
        from 0
        to @punks.size - 1
        text <=> [self, :punk_index]
      }
      
      label {
        text 'Zoom:'
      }
      spinbox {
        from 1
        to 72
        text <=> [self, :zoom]
      }
      
      label {
        text 'Palette:'
      }
      combobox {
        readonly true
        text <=> [self, :palette]
      }
      
      label {
        text 'Style:'
      }
      combobox {
        readonly true
        text <=> [self, :style, before_write: ->(v) {@previous_style = @style}]
      }
      
      label {
        text 'Output Location:'
      }
      @message_entry = entry {
        readonly true
      }
      
      @image_label = label {
        grid row_weight: 1
      }
    }
  }
end

#generate_imageObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'app/cryptopunks_gui.rb', line 57

def generate_image
  image_location = File.join(@punk_directory, "punk-#{@punk_index}#{"x#{@zoom}" if @zoom.to_i > 1}#{"-#{@palette.underscore}" if @palette != PALETTES.first}#{"-#{@style.underscore}" if @style != STYLES.first}.png")
  puts "Writing punk image to #{image_location}"
  selected_punk = @punks[@punk_index.to_i]
  selected_punk = selected_punk.change_palette8bit(Palette8bit.const_get(@palette.gsub(' ', '_').upcase.to_sym)) if @palette != PALETTES.first
  @original_zoom = @zoom
  if @style != STYLES.first
    selected_punk = selected_punk.send(@style.underscore)
    if @style != @previous_style
      @zoom = 12 if @style == 'Flip' && !['Normal', 'Mirror'].include?(@previous_style)
      @zoom = 12 if @style == 'Mirror' && !['Normal', 'Flip'].include?(@previous_style)
      @zoom = 2 if @style == 'Sketch'
      @zoom = 1 if @style == 'Led'
    end
  end
  selected_punk = selected_punk.zoom(@zoom.to_i)
  selected_punk.save(image_location)
  @image_label.image = image_location
  @message_entry.text = image_location
  notify_observers(:zoom) if @zoom != @original_zoom
end

#initialize_defaultsObject



43
44
45
46
47
# File 'app/cryptopunks_gui.rb', line 43

def initialize_defaults
  @zoom = 12
  @palette = PALETTES.first
  @style = STYLES.first
end

#initialize_punksObject



35
36
37
38
39
40
41
# File 'app/cryptopunks_gui.rb', line 35

def initialize_punks
  @punk_directory = File.join(Dir.home, '.cryptopunks')
  FileUtils.mkdir_p(@punk_directory)
  @punk_file = File.join(@punk_directory, 'punks.png')
  File.write(@punk_file, Net::HTTP.get(URI('https://raw.githubusercontent.com/larvalabs/cryptopunks/master/punks.png'))) unless File.exist?(@punk_file)
  @punks = Punks::Image::Composite.read(@punk_file)
end

#observe_image_attribute_changesObject



49
50
51
52
53
54
55
# File 'app/cryptopunks_gui.rb', line 49

def observe_image_attribute_changes
  observer = Glimmer::DataBinding::Observer.proc { generate_image }
  observer.observe(self, :punk_index)
  observer.observe(self, :zoom)
  observer.observe(self, :palette)
  observer.observe(self, :style)
end

#palette_optionsObject



27
28
29
# File 'app/cryptopunks_gui.rb', line 27

def palette_options
  PALETTES
end

#style_optionsObject



31
32
33
# File 'app/cryptopunks_gui.rb', line 31

def style_options
  STYLES
end