Class: Chingu::GameStates::Edit

Inherits:
Chingu::GameState show all
Defined in:
lib/chingu/game_states/edit.rb

Overview

Premade game state for chingu - simple level editing. Start editing in a gamestate with:

push_game_state(Chingu::GameStates::Edit)

requires the global $window set to the instance of Gosu::Window (automaticly handled if you use Chingu::Window)

Edit will only edit game objects created with the editor itself or that’s been loaded with load_game_objects. This makes mixing of loaded game objects and code create game objects possible, in the game, and in the editor.

Various shortcuts are available in the editor

1-5: create object 1..5 shown in toolbar DEL: delete selected objects CTRL+A: select all objects (not code-created ones though) S: Save E: Save and Quit Right Mouse Button Click: Copy object that was clicked on for fast duplication Arrows: Scroll within a viewport Page up/down: Modify the zorder of selected game objects

Constant Summary

Constants included from Helpers::GFX

Helpers::GFX::CIRCLE_STEP

Instance Attribute Summary collapse

Attributes inherited from Chingu::GameState

#game_objects, #game_state_manager, #options, #previous_game_state

Attributes included from Helpers::InputDispatcher

#input_clients

Instance Method Summary collapse

Methods inherited from Chingu::GameState

#button_down, #button_up, #close, #close_game, #draw_trait, #filename, #setup_trait, #to_s, #to_sym, trait, #trait_options, traits, #update_trait

Methods included from Helpers::ClassInheritableAccessor

included

Methods included from Helpers::InputClient

#holding?, #input, #input=

Methods included from Helpers::InputDispatcher

#add_input_client, #dispatch_action, #dispatch_button_down, #dispatch_button_up, #dispatch_input_for, #remove_input_client

Methods included from Helpers::GameObject

#add_game_object, #game_objects, #game_objects_of_class, #load_game_objects, #remove_game_object, #save_game_objects

Methods included from Helpers::GameState

#clear_game_states, #current_game_state, #game_states, #pop_game_state, #push_game_state, #switch_game_state, #transitional_game_state

Methods included from Helpers::GFX

#draw_circle, #draw_rect, #fill, #fill_gradient, #fill_rect

Constructor Details

#initialize(options = {}) ⇒ Edit

Returns a new instance of Edit.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/chingu/game_states/edit.rb', line 50

def initialize(options = {})
  super
          
  options = {:draw_grid => true, :snap_to_grid => true, :resize_to_grid => true}.merge(options)
  
  @grid = options[:grid] || [8,8]
  @grid_color = options[:grid_color] || Color.new(0xaa222222)
  @draw_grid = options[:draw_grid]
  @snap_to_grid = options[:snap_to_grid]      # todo
  @resize_to_grid = options[:resize_to_grid]  # todo
  
  @classes = Array(options[:classes] || game_object_classes)
  @except = options[:except] || []
  @classes -= Array(@except)
  @debug = options[:debug]
  @zorder = 10000

  p @classes  if @debug

  @hud_color = Gosu::Color.new(200,70,70,70)
  @selected_game_object = nil
  self.input =  {
    :f1 => :display_help,
    :left_mouse_button => :left_mouse_button,
    :released_left_mouse_button => :released_left_mouse_button,
    :right_mouse_button => :right_mouse_button,
    :released_right_mouse_button => :released_right_mouse_button,

    :delete    => :destroy_selected_game_objects,
    :d         => :destroy_selected_game_objects,
    :backspace => :reset_selected_game_objects,

    :holding_numpad_7 => :scale_down,
    :holding_numpad_9 => :scale_up,
    :holding_numpad_4 => :tilt_left,
    :holding_numpad_8 => :tilt_right,
    :holding_numpad_1 => :dec_alpha,
    :holding_numpad_3 => :inc_alpha,

    :r => :scale_up,
    :f => :scale_down,
    :t => :tilt_left,
    :g => :tilt_right,
    :y => :inc_zorder,
    :h => :dec_zorder,
    :u => :inc_alpha,
    :j => :dec_alpha,

    :page_up => :inc_zorder,
    :page_down => :dec_zorder,

    :s => :try_save,
    :a => :try_select_all,

    :e => :save_and_quit,

    :esc => :esc,
    :q => :quit,

    :up_arrow => :move_up,
    :down_arrow => :move_down,
    :left_arrow => :move_left,
    :right_arrow => :move_right,

    :holding_up_arrow => :try_scroll_up,
    :holding_down_arrow => :try_scroll_down,
    :holding_left_arrow => :try_scroll_left,
    :holding_right_arrow => :try_scroll_right,

    :plus => :scale_up,
    :minus => :scale_down,
    :mouse_wheel_up => :mouse_wheel_up,
    :mouse_wheel_down => :mouse_wheel_down,

    :"1" => :create_object_1,
    :"2" => :create_object_2,
    :"3" => :create_object_3,
    :"4" => :create_object_4,
    :"5" => :create_object_5,
  }

  @hud_height = 140
  @toolbar_icon_size = [32,32]
  x = 20
  y = 60
  @classes.each do |klass|
    puts "Creating a #{klass}"  if @debug

    # We initialize x,y,zorder,rotation_center after creation
    # so they're not overwritten by the class initialize/setup or simular
    begin
      game_object = klass.create(:paused => true)
      game_object.x = x + 10
      game_object.y = y
      game_object.zorder = @zorder
      game_object.options[:toolbar] = true
      game_object.rotation_center = :center_center

      # Scale down object to fit our toolbar
      if game_object.image
        Text.create("#{klass.to_s[0..9]}\n#{game_object.width.to_i}x#{game_object.height.to_i}", :size => 12, :x=>x-16, :y=>y+18, :zorder => @zorder, :max_width => 55, :rotation_center => :top_left, :align => :center, :factor => 1)
        game_object.size = @toolbar_icon_size
        game_object.cache_bounding_box if game_object.respond_to?(:cache_bounding_box)
        x += 50
      else
        puts "Skipping #{klass} - no image" if @debug
        game_object.destroy
      end
    rescue
      puts "Couldn't use #{klass} in editor: #{$!}"
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

If we’re editing a game state with automaticly called special methods, the following takes care of those.



688
689
690
691
692
# File 'lib/chingu/game_states/edit.rb', line 688

def method_missing(symbol, *args)
  if symbol != :button_down || symbol != :button_up
    previous_game_state.__send__(symbol, *args)
  end
end

Instance Attribute Details

#classesObject (readonly)

Returns the value of attribute classes.



48
49
50
# File 'lib/chingu/game_states/edit.rb', line 48

def classes
  @classes
end

#debugObject

Returns the value of attribute debug.



47
48
49
# File 'lib/chingu/game_states/edit.rb', line 47

def debug
  @debug
end

#excludeObject (readonly)

Returns the value of attribute exclude.



48
49
50
# File 'lib/chingu/game_states/edit.rb', line 48

def exclude
  @exclude
end

#fileObject

Returns the value of attribute file.



47
48
49
# File 'lib/chingu/game_states/edit.rb', line 47

def file
  @file
end

#gridObject

Returns the value of attribute grid.



47
48
49
# File 'lib/chingu/game_states/edit.rb', line 47

def grid
  @grid
end

#hud_colorObject

Returns the value of attribute hud_color.



47
48
49
# File 'lib/chingu/game_states/edit.rb', line 47

def hud_color
  @hud_color
end

Instance Method Details

#create_new_game_object_from(template) ⇒ Object



654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
# File 'lib/chingu/game_states/edit.rb', line 654

def create_new_game_object_from(template)
   game_object = template.class.create(:parent => previous_game_state)
   game_object.update
   #game_object.options[:selected] = true
   game_object.options[:created_with_editor] = true
   game_object.x = self.mouse_x
   game_object.y = self.mouse_y
            
   unless template.options[:toolbar]
     game_object.angle = template.angle
     game_object.factor_x = template.factor_x
     game_object.factor_y = template.factor_y
     game_object.color = template.color.dup
     game_object.zorder = template.zorder
     game_object.update
   else
     # Resize the new game object to fit the grid perfectly!
     wanted_width = game_object.image.width + @grid[0] - (game_object.image.width % @grid[0])
     wanted_height = game_object.image.height + @grid[1] - (game_object.image.height % @grid[1])
     game_object.factor_x = wanted_width.to_f / game_object.image.width.to_f
     game_object.factor_y = wanted_height.to_f / game_object.image.height.to_f
   end
   
   game_object.options[:mouse_x_offset] = (game_object.x - self.mouse_x) rescue 0
   game_object.options[:mouse_y_offset] = (game_object.y - self.mouse_y) rescue 0
   
   game_object.cache_bounding_box if game_object.respond_to?(:cache_bounding_box)
   return game_object
end

#create_object_1Object



457
# File 'lib/chingu/game_states/edit.rb', line 457

def create_object_1; create_object_nr(0); end

#create_object_2Object



458
# File 'lib/chingu/game_states/edit.rb', line 458

def create_object_2; create_object_nr(1); end

#create_object_3Object



459
# File 'lib/chingu/game_states/edit.rb', line 459

def create_object_3; create_object_nr(2); end

#create_object_4Object



460
# File 'lib/chingu/game_states/edit.rb', line 460

def create_object_4; create_object_nr(3); end

#create_object_5Object



461
# File 'lib/chingu/game_states/edit.rb', line 461

def create_object_5; create_object_nr(4); end

#create_object_nr(number) ⇒ Object



450
451
452
453
454
455
# File 'lib/chingu/game_states/edit.rb', line 450

def create_object_nr(number)
  c = @classes[number].create(:x => self.mouse_x, :y => self.mouse_y, :parent => previous_game_state)  if @classes[number]
  c.options[:created_with_editor] = true
  c.update
  #@text.text = "Created a #{c.class} @ #{c.x} / #{c.y}"
end

#dec_alphaObject



599
600
601
# File 'lib/chingu/game_states/edit.rb', line 599

def dec_alpha
  selected_game_objects.each { |game_object| game_object.alpha -= 1 }
end

#dec_zorderObject



593
594
595
# File 'lib/chingu/game_states/edit.rb', line 593

def dec_zorder
  selected_game_objects.each { |game_object| game_object.zorder -= 1 }
end

#deselect_selected_game_objectsObject



432
433
434
# File 'lib/chingu/game_states/edit.rb', line 432

def deselect_selected_game_objects
  selected_game_objects.each { |object| object.options[:selected] = nil }
end

#destroy_selected_game_objectsObject

Call destroy on all selected game objects



428
429
430
# File 'lib/chingu/game_states/edit.rb', line 428

def destroy_selected_game_objects
  selected_game_objects.each { |game_object| game_object.destroy }
end

#display_helpObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/chingu/game_states/edit.rb', line 164

def display_help
text = <<END_OF_STRING
  F1: This help screen
  ESC: Return to Edit
  
  1-5: create object 1..5 shown in toolbar at mousecursor
  CTRL+A: select all objects (not in-code-created ones though)
  CTRL+S: Save
  E: Save and Quit
  Q: Quit (without saving)
  ESC: Deselect all objects
  Right Mouse Button Click: Copy object bellow cursor for fast duplication
  Arrow-keys (with selected objects): Move objects 1 pixel at the time
  Arrow-keys (with no selected objects): Scroll within a viewport
  

  Bellow keys operates on all currently selected game objects
  -----------------------------------------------------------------------------------
  DEL: delete selected objects
  BACKSPACE: reset angle and scale to default values
  Page Up: Increase zorder
  Page Down: Decrease zorder
  
  R: scale up
  F: scale down
  T: tilt left
  G: tilt right
  Y: inc zorder
  H: dec zorder
  U: less transparency
  J: more transparencty

  Mouse Wheel (with no selected objects): Scroll viewport up/down
  Mouse Wheel: Scale up/down
  SHIFT + Mouse Wheel: Tilt left/right
  CTRL + Mouse Wheel: Zorder up/down
  ALT + Mouse Wheel: Transparency less/more
END_OF_STRING
  
  push_game_state( GameStates::Popup.new(:text => text) )
end

#drawObject

DRAW



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
# File 'lib/chingu/game_states/edit.rb', line 296

def draw
  # Draw prev game state onto screen (the level we're editing)
  previous_game_state.draw
  
  super
  
  draw_grid if @draw_grid
  
  #
  # Draw an edit HUD
  #
  $window.draw_quad(  0,0,@hud_color,
                      $window.width,0,@hud_color,
                      $window.width,@hud_height,@hud_color,
                      0,@hud_height,@hud_color, @zorder-1)
          
  #
  # Draw red rectangles/circles around all selected game objects
  #
  if defined?(previous_game_state.viewport)
    previous_game_state.viewport.apply {  selected_game_objects.each { |game_object| game_object.draw_debug } }
  else
    selected_game_objects.each { |game_object| game_object.draw_debug }
  end
  
  if @cursor_game_object
    @cursor_game_object.draw_at($window.mouse_x, $window.mouse_y)
  else
    draw_cursor_at($window.mouse_x, $window.mouse_y)
  end
  
end

#draw_cursor_at(x, y, c = Color::WHITE) ⇒ Object

draw a simple triangle-shaped cursor



492
493
494
# File 'lib/chingu/game_states/edit.rb', line 492

def draw_cursor_at(x, y, c = Color::WHITE)
  $window.draw_triangle(x, y, c, x, y+10, c, x+10, y+10, c, @zorder + 10)
end

#draw_gridObject



206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/chingu/game_states/edit.rb', line 206

def draw_grid
  return unless @grid
  
  start_x, start_y = 0,0
  if defined?(previous_game_state.viewport)
    start_x = -previous_game_state.viewport.x % @grid.first
    start_y = -previous_game_state.viewport.y % @grid.last
  end
  (start_x .. $window.width).step(@grid.first).each do |x|
    $window.draw_line(x, 1, @grid_color, x, $window.height, @grid_color, @zorder-10, :additive)
  end
  (start_y .. $window.height).step(@grid.last).each do |y|
    $window.draw_line(1, y, @grid_color, $window.width, y, @grid_color, @zorder-10, :additive)
  end
  
end

#editable_game_objectsObject

Returns a list of game objects the editor can create. 2 types of object gets this flag:

  • An object loaded with load_game_objects

  • An object created from within the editor

This helps us mix code-created with editor-created objects inside the editor and not muck around with the code-created ones.



414
415
416
# File 'lib/chingu/game_states/edit.rb', line 414

def editable_game_objects
  previous_game_state.game_objects.select { |o| o.options[:created_with_editor] }
end

#empty_area_at_cursorObject



436
437
438
439
# File 'lib/chingu/game_states/edit.rb', line 436

def empty_area_at_cursor
  game_object_at(self.mouse_x, self.mouse_y)==nil && 
  game_object_icon_at($window.mouse_x, $window.mouse_y) == nil
end

#escObject



615
616
617
618
# File 'lib/chingu/game_states/edit.rb', line 615

def esc
  deselect_selected_game_objects
  @cursor_game_object = nil
end

#finalizeObject



514
515
516
517
518
# File 'lib/chingu/game_states/edit.rb', line 514

def finalize
  if defined?(previous_game_state.viewport)
    previous_game_state.viewport.game_area = @game_area_backup
  end
end

#game_object_at(x, y) ⇒ Object

Get editable object at X/Y .. if there’s many objects at the same coordinate.. .. get the one with highest zorder.



483
484
485
486
487
# File 'lib/chingu/game_states/edit.rb', line 483

def game_object_at(x, y)
  editable_game_objects.select do |game_object| 
    game_object.respond_to?(:collision_at?) && game_object.collision_at?(x,y)
  end.sort {|x,y| y.zorder <=> x.zorder }.first
end

#game_object_classesObject

Get all classes based on GameObject except Chingus internal classes.



444
445
446
447
448
# File 'lib/chingu/game_states/edit.rb', line 444

def game_object_classes
  ObjectSpace.enum_for(:each_object, class << GameObject; self; end).to_a.select do |game_class|
    game_class.instance_methods && !game_class.to_s.include?("Chingu::")
  end
end

#game_object_icon_at(x, y) ⇒ Object



473
474
475
476
477
# File 'lib/chingu/game_states/edit.rb', line 473

def game_object_icon_at(x, y)
  game_objects.select do |game_object| 
    game_object.respond_to?(:collision_at?) && game_object.collision_at?(x,y)
  end.first
end

#inc_alphaObject



596
597
598
# File 'lib/chingu/game_states/edit.rb', line 596

def inc_alpha
  selected_game_objects.each { |game_object| game_object.alpha += 1 }
end

#inc_zorderObject



590
591
592
# File 'lib/chingu/game_states/edit.rb', line 590

def inc_zorder
  selected_game_objects.each { |game_object| game_object.zorder += 1 }
end

#inside_window?(x = $window.mouse_x, y = $window.mouse_y) ⇒ Boolean

Returns:

  • (Boolean)


650
651
652
# File 'lib/chingu/game_states/edit.rb', line 650

def inside_window?(x = $window.mouse_x, y = $window.mouse_y)
  x >= 0 && x <= $window.width && y >= 0 && y <= $window.height
end

#left_mouse_buttonObject

CLICKED LEFT MOUSE BUTTON



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/chingu/game_states/edit.rb', line 332

def left_mouse_button
  @left_mouse_button  = true
  @selected_game_object = false
  
  if defined?(self.previous_game_state.viewport)
    @left_mouse_click_at = [self.previous_game_state.viewport.x + $window.mouse_x, self.previous_game_state.viewport.y + $window.mouse_y]
  else
    @left_mouse_click_at = [$window.mouse_x, $window.mouse_y]
  end
  
  # Put out a new game object in the editor window and select it right away
  @selected_game_object = create_new_game_object_from(@cursor_game_object)  if @cursor_game_object
  
  # Check if user clicked on anything in the icon-toolbar of available game objects
  @cursor_game_object = game_object_icon_at($window.mouse_x, $window.mouse_y)

  # Get editable game object that was clicked at (if any)
  @selected_game_object ||= game_object_at(self.mouse_x, self.mouse_y)
  
  if @selected_game_object && defined?(self.previous_game_state.viewport)
    self.previous_game_state.viewport.center_around(@selected_game_object)  if @left_double_click
  end
                
  if @selected_game_object
    #
    # If clicking on a new object that's wasn't previosly selected
    #  --> deselect all objects unless holding left_ctrl
    #
    if @selected_game_object.options[:selected] == nil
      selected_game_objects.each { |object| object.options[:selected] = nil } unless holding?(:left_ctrl)
    end
    
    if holding?(:left_ctrl)
      @selected_game_object.options[:selected] = !@selected_game_object.options[:selected]
    else
      @selected_game_object.options[:selected] = true
    end
    
    if holding?(:left_shift)
      #puts @selected_game_object.class.all.size
      previous_game_state.game_objects.select { |x| x.class == @selected_game_object.class }.each do |game_object|
        game_object.options[:selected] = true
      end
    end
      
    #
    # Re-align all objects x/y offset in relevance to the cursor
    #
    selected_game_objects.each do |selected_game_object|
      selected_game_object.options[:mouse_x_offset] = selected_game_object.x - self.mouse_x
      selected_game_object.options[:mouse_y_offset] = selected_game_object.y - self.mouse_y
    end
  else
    deselect_selected_game_objects unless holding?(:left_ctrl)
  end
end

#mouse_wheel_downObject



561
562
563
564
565
566
567
568
569
570
# File 'lib/chingu/game_states/edit.rb', line 561

def mouse_wheel_down
  if selected_game_objects.empty?
    scroll_down(40)
  else
    tilt_right && return if holding?(:left_shift)
    dec_zorder && return if holding?(:left_ctrl)
    dec_alpha && return if holding?(:left_alt)
    scale_down
  end
end

#mouse_wheel_upObject



550
551
552
553
554
555
556
557
558
559
# File 'lib/chingu/game_states/edit.rb', line 550

def mouse_wheel_up
  if selected_game_objects.empty?
    scroll_up(40)
  else
    tilt_left && return if holding?(:left_shift)
    inc_zorder && return if holding?(:left_ctrl)
    inc_alpha && return if holding?(:left_alt)
    scale_up
  end
end

#mouse_xObject



638
639
640
641
642
# File 'lib/chingu/game_states/edit.rb', line 638

def mouse_x
  x = $window.mouse_x
  x += self.previous_game_state.viewport.x if defined?(self.previous_game_state.viewport)
  return x
end

#mouse_yObject



644
645
646
647
648
# File 'lib/chingu/game_states/edit.rb', line 644

def mouse_y
  y = $window.mouse_y
  y += self.previous_game_state.viewport.y if defined?(self.previous_game_state.viewport)
  return y
end

#move_downObject



532
533
534
535
# File 'lib/chingu/game_states/edit.rb', line 532

def move_down
  scroll_down && return   if selected_game_objects.empty?
  selected_game_objects.each { |game_object| game_object.y += 1 }
end

#move_leftObject



520
521
522
523
# File 'lib/chingu/game_states/edit.rb', line 520

def move_left
  scroll_left && return   if selected_game_objects.empty?
  selected_game_objects.each { |game_object| game_object.x -= 1 }
end

#move_rightObject



524
525
526
527
# File 'lib/chingu/game_states/edit.rb', line 524

def move_right
  scroll_right && return  if selected_game_objects.empty?
  selected_game_objects.each { |game_object| game_object.x += 1 }
end

#move_upObject



528
529
530
531
# File 'lib/chingu/game_states/edit.rb', line 528

def move_up
  scroll_up && return     if selected_game_objects.empty?
  selected_game_objects.each { |game_object| game_object.y -= 1 }
end

#page_downObject



622
623
624
# File 'lib/chingu/game_states/edit.rb', line 622

def page_down
  self.previous_game_state.viewport.y += $window.height if defined?(self.previous_game_state.viewport)
end

#page_upObject



619
620
621
# File 'lib/chingu/game_states/edit.rb', line 619

def page_up
  self.previous_game_state.viewport.y -= $window.height if defined?(self.previous_game_state.viewport)
end

#quitObject



503
504
505
# File 'lib/chingu/game_states/edit.rb', line 503

def quit
  pop_game_state(:setup => false)
end

#recache_bounding_boxesObject



572
573
574
# File 'lib/chingu/game_states/edit.rb', line 572

def recache_bounding_boxes
  selected_game_objects.each { |game_object| game_object.cache_bounding_box if game_object.respond_to?(:cache_bounding_box)}
end

#released_left_mouse_buttonObject

RELASED LEFT MOUSE BUTTON



402
403
404
# File 'lib/chingu/game_states/edit.rb', line 402

def released_left_mouse_button
  @left_mouse_button = false
end

#released_right_mouse_buttonObject



396
397
# File 'lib/chingu/game_states/edit.rb', line 396

def released_right_mouse_button
end

#reset_selected_game_objectsObject

Resets selected game objects defaults, angle=0, scale=1.



466
467
468
469
470
471
# File 'lib/chingu/game_states/edit.rb', line 466

def reset_selected_game_objects
  selected_game_objects.each do |game_object|
    game_object.angle = 0
    game_object.scale = 1
  end
end

#right_mouse_buttonObject



389
390
391
392
393
394
395
# File 'lib/chingu/game_states/edit.rb', line 389

def right_mouse_button
  if @cursor_game_object
    @cursor_game_object = nil
  else
    @cursor_game_object = game_object_at(self.mouse_x, self.mouse_y)
  end
end

#saveObject



506
507
508
# File 'lib/chingu/game_states/edit.rb', line 506

def save 
  save_game_objects(:game_objects => editable_game_objects, :file => @file, :classes => @classes)
end

#save_and_quitObject



509
510
511
512
# File 'lib/chingu/game_states/edit.rb', line 509

def save_and_quit
  save unless holding?(:left_ctrl)
  quit
end

#scale_downObject



585
586
587
588
# File 'lib/chingu/game_states/edit.rb', line 585

def scale_down
  scale_down_x && scale_down_y
  recache_bounding_boxes
end

#scale_down_xObject



608
609
610
# File 'lib/chingu/game_states/edit.rb', line 608

def scale_down_x
  selected_game_objects.each { |game_object| game_object.width -= grid[0] if game_object.width > grid[0] }
end

#scale_down_yObject



611
612
613
# File 'lib/chingu/game_states/edit.rb', line 611

def scale_down_y
  selected_game_objects.each { |game_object| game_object.height -= grid[1] if game_object.height > grid[1] }
end

#scale_upObject



581
582
583
584
# File 'lib/chingu/game_states/edit.rb', line 581

def scale_up
  scale_up_x && scale_up_y
  recache_bounding_boxes
end

#scale_up_xObject



602
603
604
# File 'lib/chingu/game_states/edit.rb', line 602

def scale_up_x
  selected_game_objects.each { |game_object| game_object.width += grid[0] }
end

#scale_up_yObject



605
606
607
# File 'lib/chingu/game_states/edit.rb', line 605

def scale_up_y
  selected_game_objects.each { |game_object| game_object.height += grid[1] }
end

#scroll_down(amount = 10) ⇒ Object



628
629
630
# File 'lib/chingu/game_states/edit.rb', line 628

def scroll_down(amount = 10)
  self.previous_game_state.viewport.y += amount if defined?(self.previous_game_state.viewport)
end

#scroll_left(amount = 10) ⇒ Object



631
632
633
# File 'lib/chingu/game_states/edit.rb', line 631

def scroll_left(amount = 10)
  self.previous_game_state.viewport.x -= amount if defined?(self.previous_game_state.viewport)
end

#scroll_right(amount = 10) ⇒ Object



634
635
636
# File 'lib/chingu/game_states/edit.rb', line 634

def scroll_right(amount = 10)
  self.previous_game_state.viewport.x += amount if defined?(self.previous_game_state.viewport)
end

#scroll_up(amount = 10) ⇒ Object



625
626
627
# File 'lib/chingu/game_states/edit.rb', line 625

def scroll_up(amount = 10)
  self.previous_game_state.viewport.y -= amount if defined?(self.previous_game_state.viewport)
end

#selected_game_objectsObject

Returns a list of selected game objects



421
422
423
# File 'lib/chingu/game_states/edit.rb', line 421

def selected_game_objects
  editable_game_objects.select { |o| o.options[:selected] }
end

#setupObject

SETUP



226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/chingu/game_states/edit.rb', line 226

def setup
  @scroll_border_thickness = 50
  @file = options[:file] || previous_game_state.filename + ".yml"
  @title = Text.create("File: #{@file}", :x => 5, :y => 2, :factor => 1, :size => 16, :zorder => @zorder)
  @title.text += " - Grid: #{@grid}" if @grid
  @text = Text.create("", :x => 300, :y => 20, :factor => 1, :size => 16, :zorder => @zorder)
  @status_text = Text.create("-", :x => 5, :y => 20, :factor => 1, :size => 16, :zorder => @zorder)
  
  if defined?(previous_game_state.viewport)
    @game_area_backup = previous_game_state.viewport.game_area.dup
    previous_game_state.viewport.game_area.x -= @hud_height
    previous_game_state.viewport.game_area.y -= @hud_height
  end
end

#tilt_leftObject



575
576
577
# File 'lib/chingu/game_states/edit.rb', line 575

def tilt_left
  selected_game_objects.each { |game_object| game_object.angle -= 5 }
end

#tilt_rightObject



578
579
580
# File 'lib/chingu/game_states/edit.rb', line 578

def tilt_right
  selected_game_objects.each { |game_object| game_object.angle += 5 }        
end

#try_saveObject



499
500
501
# File 'lib/chingu/game_states/edit.rb', line 499

def try_save
  save if holding?(:left_ctrl)
end

#try_scroll_downObject



546
547
548
# File 'lib/chingu/game_states/edit.rb', line 546

def try_scroll_down
  scroll_down if selected_game_objects.empty?
end

#try_scroll_leftObject



537
538
539
# File 'lib/chingu/game_states/edit.rb', line 537

def try_scroll_left
  scroll_left if selected_game_objects.empty?
end

#try_scroll_rightObject



540
541
542
# File 'lib/chingu/game_states/edit.rb', line 540

def try_scroll_right
  scroll_right if selected_game_objects.empty?
end

#try_scroll_upObject



543
544
545
# File 'lib/chingu/game_states/edit.rb', line 543

def try_scroll_up
  scroll_up   if selected_game_objects.empty?
end

#try_select_allObject



496
497
498
# File 'lib/chingu/game_states/edit.rb', line 496

def try_select_all
  editable_game_objects.each { |x| x.options[:selected] = true }  if holding?(:left_ctrl)
end

#updateObject

UPDATE



244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
# File 'lib/chingu/game_states/edit.rb', line 244

def update
  # Sync all changes to previous game states game objects list
  # This is needed since we don't call update on it.
  previous_game_state.game_objects.sync
  
  super
  
  @status_text.text = "#{self.mouse_x.to_i} / #{self.mouse_y.to_i}"
  
  if s = @selected_game_object
    @text.text = "#{s.class.to_s} @ #{s.x.to_i} / #{s.y.to_i}"
    @text.text = "Size: #{s.width.to_i} x #{s.height.to_i} Ratio: #{sprintf("%.2f",s.width/s.height)}"
    @text.text += " [Scale: #{sprintf("%.2f", s.factor_x)}/#{sprintf("%.2f", s.factor_y)} Angle: #{s.angle.to_i} Z: #{s.zorder}]"
  end
  
  #
  # We got a selected game object and the left mouse button is held down
  #
  if @left_mouse_button && @selected_game_object
    selected_game_objects.each do |selected_game_object|            
      selected_game_object.x = self.mouse_x + selected_game_object.options[:mouse_x_offset]
      selected_game_object.y = self.mouse_y + selected_game_object.options[:mouse_y_offset]
      
      if @snap_to_grid
        selected_game_object.x -= selected_game_object.x % @grid[0]
        selected_game_object.y -= selected_game_object.y % @grid[1]
      end
    end
    
    # TODO: better cleaner sollution
    if @selected_game_object.respond_to?(:bounding_box)
      @selected_game_object.bounding_box.x = @selected_game_object.x
      @selected_game_object.bounding_box.y = @selected_game_object.y
    end
  elsif @left_mouse_button
    if defined?(self.previous_game_state.viewport)
      self.previous_game_state.viewport.x = @left_mouse_click_at[0] - $window.mouse_x
      self.previous_game_state.viewport.y = @left_mouse_click_at[1] - $window.mouse_y
    end
  end
  
  if inside_window?($window.mouse_x, $window.mouse_y)
    scroll_right  if $window.mouse_x > $window.width - @scroll_border_thickness
    scroll_left   if $window.mouse_x < @scroll_border_thickness
    scroll_up     if $window.mouse_y < @scroll_border_thickness
    scroll_down   if $window.mouse_y > $window.height - @scroll_border_thickness
  end
end