Class: Viewport

Inherits:
LiteRGSS::Viewport show all
Defined in:
scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb,
scripts/00700 Ajout_PSDK/01800 XXX_simple_mouse_in_.rb

Overview

Class that describes a surface of the screen where texts and sprites are shown (with some global effect)

Defined Under Namespace

Modules: WithToneAndColors

Constant Summary collapse

CONFIGS =

Hash containing all the Viewport configuration (:main, :sub etc…)

{}
VIEWPORT_CONF_COMP =

Filename for viewport compiled config

'Data/Viewport.rxdata'
VIEWPORT_CONF_TEXT =

Filename for viewport uncompiled config

'Data/Viewport.json'

Instance Attribute Summary collapse

Attributes inherited from LiteRGSS::Viewport

#__index__, #angle, #blendmode, #ox, #oy, #rect, #shader, #visible, #z, #zoom

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from LiteRGSS::Viewport

new, #snap_to_bitmap, #sort_z

Methods inherited from LiteRGSS::Disposable

#disposed?

Constructor Details

#initialize(x, y, width, height, z = nil) ⇒ Viewport

Create a new viewport

Parameters:

  • x (Integer)

    x coordinate of the viewport on screen

  • y (Integer)

    y coordinate of the viewport on screen

  • width (Integer)

    width of the viewport

  • height (Integer)

    height of the viewport

  • z (Integer) (defaults to: nil)

    z coordinate of the viewport



25
26
27
28
29
30
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb', line 25

def initialize(x, y, width, height, z = nil)
  super(Graphics.window, x, y, width, height)
  self.z = z if z
  @need_to_sort = true
  Graphics.register_viewport(self)
end

Instance Attribute Details

#need_to_sortBoolean

Tell if the viewport needs to sort

Returns:

  • (Boolean)


17
18
19
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb', line 17

def need_to_sort
  @need_to_sort
end

Class Method Details

.create(screen_name_symbol, z = nil) ⇒ Viewport .create(x, y = 0, width = 1, height = 1, z = nil) ⇒ Viewport .create(opts) ⇒ Viewport

Generating a viewport with one line of code

Overloads:

  • .create(screen_name_symbol, z = nil) ⇒ Viewport

    Parameters:

    • screen_name_symbol (:main, :sub)

      describe with screen surface the viewport is (loaded from maker options)

    • z (Integer, nil) (defaults to: nil)

      superiority of the viewport

  • .create(x, y = 0, width = 1, height = 1, z = nil) ⇒ Viewport

    Parameters:

    • x (Integer)

      x coordinate of the viewport

    • y (Integer) (defaults to: 0)

      y coordinate of the viewport

    • width (Integer) (defaults to: 1)

      width of the viewport

    • height (Integer) (defaults to: 1)

      height of the viewport

    • z (Integer, nil) (defaults to: nil)

      superiority of the viewport

  • .create(opts) ⇒ Viewport

    Parameters:

    • opts (Hash)

      opts of the viewport definition

    Options Hash (opts):

    • :x (Integer) — default: 0

      x coordinate of the viewport

    • :y (Integer) — default: 0

      y coordinate of the viewport

    • :width (Integer) — default: 320

      width of the viewport

    • :height (Integer) — default: 240

      height of the viewport

    • :z (Integer, nil) — default: nil

      superiority of the viewport

Returns:



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb', line 58

def create(x, y = 0, width = 1, height = 1, z = 0)
  if x.is_a?(Hash)
    z = x[:z] || z
    y = x[:y] || 0
    width = x[:width] || Configs.display.game_resolution.x
    height = x[:height] || Configs.display.game_resolution.y
    x = x[:x] || 0
  elsif x.is_a?(Symbol)
    return create(CONFIGS[x], 0, 1, 1, y)
  end
  gox = @global_offset_x || PSDK_CONFIG.viewport_offset_x || 0
  goy = @global_offset_y || PSDK_CONFIG.viewport_offset_y || 0
  v = Viewport.new(x + gox, y + goy, width, height, z)
  return v
end

.load_configs

Load the viewport configs



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb', line 75

def load_configs
  unless PSDK_CONFIG.release?
    unless File.exist?(VIEWPORT_CONF_COMP) && File.exist?(VIEWPORT_CONF_TEXT)
      if File.exist?(VIEWPORT_CONF_TEXT)
        save_data(JSON.parse(File.read(VIEWPORT_CONF_TEXT), symbolize_names: true), VIEWPORT_CONF_COMP)
      else
        vp_conf = { main: { x: 0, y: 0, width: 320, height: 240 } }
        File.write(VIEWPORT_CONF_TEXT, vp_conf.to_json)
        sleep(1)
        save_data(vp_conf, VIEWPORT_CONF_COMP)
      end
    end
    # Load json conf if newer than binary conf
    if File.mtime(VIEWPORT_CONF_TEXT) > File.mtime(VIEWPORT_CONF_COMP)
      log_debug('Updating Viewport Configuration...')
      save_data(JSON.parse(File.read(VIEWPORT_CONF_TEXT), symbolize_names: true), VIEWPORT_CONF_COMP)
    end
  end
  CONFIGS.merge!(load_data(VIEWPORT_CONF_COMP))
end

Instance Method Details

#disposeself

Dispose a viewport

Returns:

  • (self)


34
35
36
37
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb', line 34

def dispose
  Graphics.unregitser_viewport(self)
  super
end

#flash(color, duration)

Flash the viewport

Parameters:



104
105
106
107
108
109
110
111
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb', line 104

def flash(color, duration)
  self.shader ||= Shader.create(:color_shader_with_background)
  color ||= Color.new(0, 0, 0)
  @flash_color = color
  @flash_color_running = color.dup
  @flash_counter = 0
  @flash_duration = duration.to_f
end

#simple_mouse_in?(mouse_x = Mouse.x, mouse_y = Mouse.y) ⇒ Boolean

Detect if the mouse is in the sprite (without rotation and stuff like that)

Parameters:

  • mouse_x (Integer) (defaults to: Mouse.x)

    the mouse x position on the screen

  • mouse_y (Integer) (defaults to: Mouse.y)

    the mouse y position on the screen

Returns:

  • (Boolean)

Author:

  • Nuri Yuri



95
96
97
98
99
100
101
102
# File 'scripts/00700 Ajout_PSDK/01800 XXX_simple_mouse_in_.rb', line 95

def simple_mouse_in?(mouse_x = Mouse.x, mouse_y = Mouse.y)
  vp_rect = rect
  if vp_rect.x <= mouse_x && (vp_rect.x + vp_rect.width) > mouse_x &&
      vp_rect.y <= mouse_y && (vp_rect.y + vp_rect.height) > mouse_y
    return true
  end
  return false
end

#to_s Also known as: inspect



97
98
99
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb', line 97

def to_s
  return format('#<Vewport:%08x : %00d>', __id__, __index__)
end

#translate_mouse_coords(mouse_x = Mouse.x, mouse_y = Mouse.y) ⇒ Array(Integer, Integer)

Convert mouse coordinate on the screen to mouse coordinates on the sprite

Parameters:

  • mouse_x (Integer) (defaults to: Mouse.x)

    the mouse x position on the screen

  • mouse_y (Integer) (defaults to: Mouse.y)

    the mouse y position on the screen

Returns:

Author:

  • Nuri Yuri



109
110
111
112
# File 'scripts/00700 Ajout_PSDK/01800 XXX_simple_mouse_in_.rb', line 109

def translate_mouse_coords(mouse_x = Mouse.x, mouse_y = Mouse.y)
  vp_rect = rect
  return mouse_x - vp_rect.x + ox, mouse_y - vp_rect.y + oy
end

#update

Update the viewport



114
115
116
117
118
119
120
121
122
123
124
125
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb', line 114

def update
  if @flash_color
    alpha = 1 - @flash_counter / @flash_duration
    @flash_color_running.alpha = @flash_color.alpha * alpha
    self.shader.set_float_uniform('color', @flash_color_running)
    @flash_counter += 1
    if @flash_counter >= @flash_duration
      self.shader.set_float_uniform('color', [0, 0, 0, 0])
      @flash_color_running = @flash_color = nil
    end
  end
end