Class: Circule::GUI

Inherits:
Object
  • Object
show all
Includes:
Glimmer
Defined in:
lib/circule/gui.rb

Overview

Glimmer Tk GUI for Circule

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGUI

Returns a new instance of GUI.



21
22
23
# File 'lib/circule/gui.rb', line 21

def initialize
  initialize_defaults
end

Instance Attribute Details

#bit0Object

Returns the value of attribute bit0.



19
20
21
# File 'lib/circule/gui.rb', line 19

def bit0
  @bit0
end

#bitnObject

Returns the value of attribute bitn.



19
20
21
# File 'lib/circule/gui.rb', line 19

def bitn
  @bitn
end

#canvasObject

Returns the value of attribute canvas.



19
20
21
# File 'lib/circule/gui.rb', line 19

def canvas
  @canvas
end

#hexObject

Returns the value of attribute hex.



19
20
21
# File 'lib/circule/gui.rb', line 19

def hex
  @hex
end

#oxObject

Returns the value of attribute ox.



19
20
21
# File 'lib/circule/gui.rb', line 19

def ox
  @ox
end

#oyObject

Returns the value of attribute oy.



19
20
21
# File 'lib/circule/gui.rb', line 19

def oy
  @oy
end

#scaleObject

Returns the value of attribute scale.



19
20
21
# File 'lib/circule/gui.rb', line 19

def scale
  @scale
end

#stepObject

Returns the value of attribute step.



19
20
21
# File 'lib/circule/gui.rb', line 19

def step
  @step
end

Instance Method Details

#create_guiObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/circule/gui.rb', line 69

def create_gui
  @root ||= root {
    title 'Circule GUI'

    scrollbar_frame {
      xscrollbar false

      entry   {                   text <=> [self, :hex]     ; grid row:  1, column: 1, column_span: 2, column_weight: 1 }

      label   {                   text            'bit0:'   ; grid row:  2, column: 1, column_span: 1, column_weight: 0 }
      spinbox { from   0; to 255; text <=> [self, :bit0]    ; grid row:  2, column: 2, column_span: 1, column_weight: 1 }
      label   {                   text            'step:'   ; grid row:  3, column: 1, column_span: 1, column_weight: 0 }
      spinbox { from   4; to  64; text <=> [self, :step]    ; grid row:  3, column: 2, column_span: 1, column_weight: 1 }
      label   {                   text            'bitn:'   ; grid row:  4, column: 1, column_span: 1, column_weight: 0 }
      spinbox { from   0; to 255; text <=> [self, :bitn]    ; grid row:  4, column: 2, column_span: 1, column_weight: 1 }
      label   {                   text            'canvas:' ; grid row:  5, column: 1, column_span: 1, column_weight: 0 }
      spinbox { from  24; to  96; text <=> [self, :canvas]  ; grid row:  5, column: 2, column_span: 1, column_weight: 1 }
      label   {                   text            'ox:'     ; grid row:  6, column: 1, column_span: 1, column_weight: 0 }
      spinbox { from -96; to  96; text <=> [self, :ox]      ; grid row:  6, column: 2, column_span: 1, column_weight: 1 }
      label   {                   text            'oy:'     ; grid row:  7, column: 1, column_span: 1, column_weight: 0 }
      spinbox { from -96; to  96; text <=> [self, :oy]      ; grid row:  7, column: 2, column_span: 1, column_weight: 1 }
      label   {                   text            'scale:'  ; grid row:  8, column: 1, column_span: 1, column_weight: 0 }
      spinbox { from   1; to  24; text <=> [self, :scale]   ; grid row:  8, column: 2, column_span: 1, column_weight: 1 }

      frame {
        @image_label = label { anchor 'center' }            ; grid row:  9, column: 1, column_span: 2, column_weight: 1
      }

      button { text 'Open'; command { open    }             ; grid row: 10, column: 1, column_span: 2, column_weight: 1 }
      button { text 'Save'; command { save    }             ; grid row: 11, column: 1, column_span: 2, column_weight: 1 }
      button { text 'Exit'; command { exit(0) }             ; grid row: 12, column: 1, column_span: 2, column_weight: 1 }
    }
  }
end

#generate_imageObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/circule/gui.rb', line 48

def generate_image
  tfile = Tempfile.new(%w[circule- .png])

  image = Circule.new(
    hex:    @hex,
    bit0:   @bit0.to_i,
    step:   @step.to_i,
    bitn:   @bitn.to_i,
    canvas: @canvas.to_i,
    ox:     @ox.to_i,
    oy:     @oy.to_i,
  ).image

  width  = image.width * @scale.to_i
  height = image.height * @scale.to_i

  image.resize(width, height).save(tfile.path)

  @image_label.image = tfile.path
end

#initialize_defaultsObject



25
26
27
28
29
30
31
32
33
34
# File 'lib/circule/gui.rb', line 25

def initialize_defaults
  @hex    = ARGV[0] || SecureRandom.hex(32)
  @bit0   = 0
  @step   = 16
  @bitn   = 255
  @canvas = 24
  @ox     = 0
  @oy     = 0
  @scale  = 1
end

#launchObject



115
116
117
118
119
120
# File 'lib/circule/gui.rb', line 115

def launch
  observe_image_attribute_changes
  create_gui
  generate_image
  @root.open
end

#observe_image_attribute_changesObject



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/circule/gui.rb', line 36

def observe_image_attribute_changes
  observer = Glimmer::DataBinding::Observer.proc { generate_image }
  observer.observe(self, :hex)
  observer.observe(self, :bit0)
  observer.observe(self, :step)
  observer.observe(self, :bitn)
  observer.observe(self, :canvas)
  observer.observe(self, :ox)
  observer.observe(self, :oy)
  observer.observe(self, :scale)
end

#open(source = nil) ⇒ Object



104
105
106
107
# File 'lib/circule/gui.rb', line 104

def open(source = nil)
  source   ||= send(:get_open_file, parent: root)
  self.hex   = Digest::SHA256.hexdigest File.read(source)
end

#save(target = nil) ⇒ Object



109
110
111
112
113
# File 'lib/circule/gui.rb', line 109

def save(target = nil)
  source   = @image_label.image
  target ||= send(:get_save_file, parent: @root)
  source.write(target)
end