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

CENTER_TO_FACTOR =
{ 0 => -1, 0.5 => 0, 1 => 1 }

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

Attributes included from Helpers::GameObject

#game_objects

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

#add_inputs, #holding?, #holding_all?, #holding_any?, #input, #input=, #on_input

Methods included from Helpers::InputDispatcher

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

Methods included from Helpers::GameObject

#game_objects_of_class, #load_game_objects, #save_game_objects

Methods included from Helpers::GFX

#draw_arc, #draw_circle, #draw_rect, #fill, #fill_arc, #fill_circle, #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
# 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] || Gosu::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]
  @attributes = options[:attributes] || [:x, :y, :angle, :zorder, :factor_x, :factor_y, :alpha]

  #
  # Turn on cursor + turn it back to its original value in finalize()
  #
  @original_cursor = $window.cursor
  $window.cursor = true
  
  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]
  draw_toolbar_objects
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.



674
675
676
677
678
# File 'lib/chingu/game_states/edit.rb', line 674

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

#bounding_box(game_object) ⇒ Object Also known as: bb

Returns a bounding box (Rect-class) for any gameobject It will take into considerations rotation_center and scaling



659
660
661
662
663
664
665
666
# File 'lib/chingu/game_states/edit.rb', line 659

def bounding_box(game_object)
  width, height = game_object.width, game_object.height
  x = game_object.x - width * game_object.center_x
  y = game_object.y - height * game_object.center_y
  x += width * CENTER_TO_FACTOR[game_object.center_x]   if game_object.factor_x < 0
  y += height * CENTER_TO_FACTOR[game_object.center_y]  if game_object.factor_y < 0
  return Rect.new(x, y, width, height)
end

#copy_game_object(template) ⇒ Object



639
640
641
642
643
644
645
646
647
648
649
650
651
652
# File 'lib/chingu/game_states/edit.rb', line 639

def copy_game_object(template)
  game_object = template.class.create(:parent => previous_game_state)
  # If we don't create it from the toolbar, we're cloning another object
  # When cloning we wan't the cloned objects attributes
  game_object.attributes = template.attributes  unless template.options[:toolbar]       
  game_object.x = self.mouse_x
  game_object.y = self.mouse_y
  game_object.options[:created_with_editor] = true
          
  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
  
  return game_object
end

#create_object_1Object



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

def create_object_1; create_object_nr(0); end

#create_object_2Object



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

def create_object_2; create_object_nr(1); end

#create_object_3Object



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

def create_object_3; create_object_nr(2); end

#create_object_4Object



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

def create_object_4; create_object_nr(3); end

#create_object_5Object



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

def create_object_5; create_object_nr(4); end

#create_object_nr(number) ⇒ Object



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

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



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

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

#dec_zorderObject



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

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

#deselect_selected_game_objectsObject



426
427
428
# File 'lib/chingu/game_states/edit.rb', line 426

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



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

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

#display_helpObject



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
205
206
207
208
209
210
211
# File 'lib/chingu/game_states/edit.rb', line 171

def display_help
text = "  F1: This help screen\n  ESC: Return to Edit\n  \n  1-5: create object 1..5 shown in toolbar at mousecursor\n  CTRL+A: select all objects (not in-code-created ones though)\n  CTRL+S: Save\n  E: Save and Quit\n  Q: Quit (without saving)\n  ESC: Deselect all objects\n  Right Mouse Button Click: Copy object bellow cursor for fast duplication\n  Arrow-keys (with selected objects): Move objects 1 pixel at the time\n  Arrow-keys (with no selected objects): Scroll within a viewport\n  \n\n  Bellow keys operates on all currently selected game objects\n  -----------------------------------------------------------------------------------\n  DEL: delete selected objects\n  BACKSPACE: reset angle and scale to default values\n  Page Up: Increase zorder\n  Page Down: Decrease zorder\n  \n  R: scale up\n  F: scale down\n  T: tilt left\n  G: tilt right\n  Y: inc zorder\n  H: dec zorder\n  U: less transparency\n  J: more transparencty\n\n  Mouse Wheel (with no selected objects): Scroll viewport up/down\n  Mouse Wheel: Scale up/down\n  SHIFT + Mouse Wheel: Tilt left/right\n  CTRL + Mouse Wheel: Zorder up/down\n  ALT + Mouse Wheel: Transparency less/more\n"
  
  push_game_state( GameStates::Popup.new(:text => text) )
end

#drawObject

DRAW



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
# File 'lib/chingu/game_states/edit.rb', line 289

def draw
  # Draw prev game state onto screen (the level we're editing)
  previous_game_state.draw
  
  # Restart z-ordering, everything after this will be drawn on top
  $window.flush
          
  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)
       
  #
  # Draw gameobjects
  #
  super
          
  #
  # Draw red rectangles/circles around all selected game objects
  #        
  if defined?(previous_game_state.viewport)
    previous_game_state.viewport.apply { draw_selections }
  else
    draw_selections
  end
  
  @cursor_game_object.draw_at($window.mouse_x, $window.mouse_y)   if @cursor_game_object
end

#draw_gridObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/chingu/game_states/edit.rb', line 213

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, 0, :additive)
  end
  (start_y .. $window.height).step(@grid.last).each do |y|
    $window.draw_line(1, y, @grid_color, $window.width, y, @grid_color, 0, :additive)
  end
  
end

#draw_selectionsObject

Draw a red rectangle around all selected objects



324
325
326
# File 'lib/chingu/game_states/edit.rb', line 324

def draw_selections
  selected_game_objects.each { |game_object| draw_rect(bounding_box(game_object), Color::RED, 10000) }
end

#draw_toolbar_objectsObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/chingu/game_states/edit.rb', line 142

def draw_toolbar_objects
  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.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, :max_width => 55, :rotation_center => :top_left, :align => :center, :factor => 1)
        game_object.size = @toolbar_icon_size
        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

#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.



408
409
410
# File 'lib/chingu/game_states/edit.rb', line 408

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

#empty_area_at_cursorObject



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

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



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

def esc
  deselect_selected_game_objects
  @cursor_game_object = nil
end

#finalizeObject



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

def finalize
  if defined?(previous_game_state.viewport)
    previous_game_state.viewport.game_area = @game_area_backup
  end
  $window.cursor = @original_cursor
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.



479
480
481
482
483
484
# File 'lib/chingu/game_states/edit.rb', line 479

def game_object_at(x, y)
  editable_game_objects.select do |game_object|
    next if game_object.is_a? Text
    bounding_box(game_object).collide_point?(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.



438
439
440
441
442
# File 'lib/chingu/game_states/edit.rb', line 438

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



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

def game_object_icon_at(x, y)
  game_objects.select do |game_object|
    next if game_object.is_a? Text
    next unless game_object.image
    bounding_box(game_object).collide_point?(x,y)
  end.first
end

#inc_alphaObject



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

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

#inc_zorderObject



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

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)


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

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



331
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
# File 'lib/chingu/game_states/edit.rb', line 331

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 = copy_game_object(@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)
      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



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

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



540
541
542
543
544
545
546
547
548
549
# File 'lib/chingu/game_states/edit.rb', line 540

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



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

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



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

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



522
523
524
525
# File 'lib/chingu/game_states/edit.rb', line 522

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

#move_leftObject



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

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

#move_rightObject



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

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

#move_upObject



518
519
520
521
# File 'lib/chingu/game_states/edit.rb', line 518

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

#page_downObject



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

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

#page_upObject



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

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

#quitObject



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

def quit
  pop_game_state
end

#released_left_mouse_buttonObject

RELASED LEFT MOUSE BUTTON



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

def released_left_mouse_button
  @left_mouse_button = false
end

#released_right_mouse_buttonObject



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

def released_right_mouse_button
end

#reset_selected_game_objectsObject

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



460
461
462
463
464
465
# File 'lib/chingu/game_states/edit.rb', line 460

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



387
388
389
# File 'lib/chingu/game_states/edit.rb', line 387

def right_mouse_button
  @cursor_game_object = @cursor_game_object ?  nil : game_object_at(mouse_x, mouse_y)
end

#saveObject



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

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

#save_and_quitObject



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

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

#scale_downObject



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

def scale_down
  scale_down_x && scale_down_y
end

#scale_down_xObject



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

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



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

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

#scale_upObject



568
569
570
# File 'lib/chingu/game_states/edit.rb', line 568

def scale_up
  scale_up_x && scale_up_y
end

#scale_up_xObject



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

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

#scale_up_yObject



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

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

#scroll_down(amount = 10) ⇒ Object



613
614
615
# File 'lib/chingu/game_states/edit.rb', line 613

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



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

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



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

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



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

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



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

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

#setupObject

SETUP



233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'lib/chingu/game_states/edit.rb', line 233

def setup
  @scroll_border_thickness = 30
  @file = options[:file] || previous_game_state.filename + ".yml"
  @title = Text.create("File: #{@file}", :x => 5, :y => 2, :factor => 1, :size => 16)
  @title.text += " - Grid: #{@grid}" if @grid
  @text = Text.create("", :x => 300, :y => 20, :factor => 1, :size => 16)
  @status_text = Text.create("-", :x => 5, :y => 20, :factor => 1, :size => 16)
  
  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



562
563
564
# File 'lib/chingu/game_states/edit.rb', line 562

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

#tilt_rightObject



565
566
567
# File 'lib/chingu/game_states/edit.rb', line 565

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

#try_saveObject



489
490
491
# File 'lib/chingu/game_states/edit.rb', line 489

def try_save
  save if holding?(:left_ctrl)
end

#try_scroll_downObject



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

def try_scroll_down
  scroll_down if selected_game_objects.empty?
end

#try_scroll_leftObject



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

def try_scroll_left
  scroll_left if selected_game_objects.empty?
end

#try_scroll_rightObject



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

def try_scroll_right
  scroll_right if selected_game_objects.empty?
end

#try_scroll_upObject



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

def try_scroll_up
  scroll_up   if selected_game_objects.empty?
end

#try_select_allObject



486
487
488
# File 'lib/chingu/game_states/edit.rb', line 486

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

#updateObject

UPDATE



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
# File 'lib/chingu/game_states/edit.rb', line 251

def update        
  super
  
  @status_text.text = "#{self.mouse_x.to_i} / #{self.mouse_y.to_i}"
    
  @text.text = @selected_game_object.to_s
  
  #
  # 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
  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