Class: AuthorEngine::Loader

Inherits:
Container show all
Defined in:
lib/author_engine/containers/loader.rb

Defined Under Namespace

Classes: Project

Constant Summary

Constants included from Part::Colors

Part::Colors::COLORS

Instance Method Summary collapse

Methods inherited from Container

#button_down, #initialize

Methods included from Part::Colors

#black, #blue, #brown, #dark_blue, #dark_gray, #dark_green, #dark_purple, #green, #indigo, #light_gray, #orange, #peach, #pink, #red, #rgb, #white, #xml_color, #yellow

Methods included from Support

#code_editor, #mouse_over?, #sprite_editor, #window

Constructor Details

This class inherits a constructor from AuthorEngine::Container

Instance Method Details

#button_up(id) ⇒ Object



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
137
138
139
140
# File 'lib/author_engine/containers/loader.rb', line 108

def button_up(id)
  @new_button.button_up(id)

  case id
  when Gosu::KbUp
    # @list.rotate!(-1)
    # @index-=1
    # @index = (@index % @list.size-1)*-1
    # p @index
  when Gosu::KbDown
    @list.rotate!(1)
    @index+=1
    @index = 0 if @list.size == 0
    @index = @index % @list.size-1 if @list.size != 0
  when Gosu::KbEnter, Gosu::KbReturn
    filename = window.text_input.text.strip if window.text_input

    if @entering_name
      if File.exists?(filename)
        @name_exists = filename
      else
        window.text_input = nil
        savefile = SaveFile.create(filename+".authorengine")
        load(filename+".authorengine")
      end
    else
      @list[@index].block.call if @list[@index]&.block
    end
  when Gosu::KbEscape
    @entering_name = false
    window.text_input = nil
  end
end

#closeObject



104
105
106
# File 'lib/author_engine/containers/loader.rb', line 104

def close
  window.close!
end

#drawObject



50
51
52
53
54
55
56
57
58
# File 'lib/author_engine/containers/loader.rb', line 50

def draw
  Gosu.draw_rect(0, 0, window.width, window.height, window.darken(dark_gray, 50))

  if @entering_name
    draw_inputter
  else
    draw_loader
  end
end

#draw_inputterObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/author_engine/containers/loader.rb', line 60

def draw_inputter
  caret_x = @font.text_width(window.text_input.text[0..window.text_input.caret_pos-1])
  caret_x = 0 if window.text_input.caret_pos == 0
  if Gosu.milliseconds > @last_caret_update + @caret_interval
    @draw_caret = !@draw_caret
    @last_caret_update = Gosu.milliseconds
  end

  x = window.width/2 - @font.text_width(window.text_input.text+".authorengine")/2
  error_x = window.width/2 - @font.text_width(window.text_input.text.strip+".authorengine already exists!")/2
  y = window.height/2 - @font.height/2

  Gosu.draw_rect(x+caret_x, y, 2, @font.height, Gosu::Color::WHITE) if @draw_caret
  if window.text_input.text.strip+".authorengine" == @name_exists
    @font.draw_text(window.text_input.text.strip+".authorengine already exists!", error_x, y - 32, 0, 1,1, red) if @name_exists
  else
    @name_exists = false
  end
  @font.draw_text(window.text_input.text+".authorengine", x, y, 0)
end

#draw_loaderObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/author_engine/containers/loader.rb', line 81

def draw_loader
  Gosu.draw_rect(0, @new_button.y, window.width, @new_button.height, @header_color)
  @font.draw_text("AuthorEngine", 1*window.square_scale,@font.height/2,0)
  @new_button.draw

  Gosu.clip_to(0, @font.height*2, window.width, window.height-(@font.height*4)) do
    y = (window.height/2-@font.height) - (@height/2)

    @list.each_with_index do |project, index|
      label = project.name.sub(@root_directory+"/", '')
      x = window.width/2 - @font.text_width(label)/2
      if project == @list[@index]
        Gosu.draw_rect(0, y, window.width, @font.height, red)
      end
      @font.draw_text(label, x, y, 0)
      y+=@font.height
    end
  end
end

#load(filename) ⇒ Object



44
45
46
47
48
# File 'lib/author_engine/containers/loader.rb', line 44

def load(filename)
  p filename
  savefile = SaveFile.new(filename)
  window.container = Editor.new(savefile)
end

#setupObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/author_engine/containers/loader.rb', line 4

def setup
  if ARGV[0] && File.exists?(ARGV[0]) && Gosu.milliseconds < 1500
    if ARGV[0].end_with?(".authorengine")
      load(ARGV[0])
      return
    elsif File.directory?(ARGV[0])
      @root_directory = ARGV[0]
    else
      puts "AuthorEngine: #{ARGV[0]} is not a compatible file."
    end
  end
  @root_directory ||= Dir.pwd #"#{Dir.home}/AuthorEngineProjects"

  @list = []
  @files = Dir.glob(@root_directory+"/*.authorengine")
  @font = Gosu::Font.new((6 * window.square_scale).floor, name: AuthorEngine::Text::FONT_DEFAULT)

  @files.each do |file|
    @list << Project.new(file, proc {load(file)})
  end

  @index = ((@list.size)/2.0).floor
  @list.rotate!(@index)
  @height = (@list.size-1)*@font.height

  @last_index = @index
  @entering_name = false

  @header_color = Gosu::Color.rgba(dark_green.red, dark_green.green, dark_green.blue, 100)
  @new_button   = Button.new(label: "New Project", color: @header_color) do
    window.text_input = Gosu::TextInput.new
    @entering_name = true
  end
  @new_button.x = window.width - @new_button.width

  @draw_caret = true
  @last_caret_update = Gosu.milliseconds
  @caret_interval = 500
end

#updateObject



101
102
# File 'lib/author_engine/containers/loader.rb', line 101

def update
end