Class: Viewport
- Inherits:
-
LiteRGSS::Viewport
- Object
- LiteRGSS::Disposable
- LiteRGSS::Viewport
- Viewport
- 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
-
#need_to_sort ⇒ Boolean
Tell if the viewport needs to sort.
Attributes inherited from LiteRGSS::Viewport
#__index__, #angle, #blendmode, #ox, #oy, #rect, #shader, #visible, #z, #zoom
Class Method Summary collapse
-
.create(x, y = 0, width = 1, height = 1, z = 0) ⇒ Viewport
Generating a viewport with one line of code.
-
.load_configs
Load the viewport configs.
Instance Method Summary collapse
-
#dispose ⇒ self
Dispose a viewport.
-
#flash(color, duration)
Flash the viewport.
-
#initialize(x, y, width, height, z = nil) ⇒ Viewport
constructor
Create a new viewport.
-
#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).
- #to_s (also: #inspect)
-
#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.
-
#update
Update the viewport.
Methods inherited from LiteRGSS::Viewport
Methods inherited from LiteRGSS::Disposable
Constructor Details
#initialize(x, y, width, height, z = nil) ⇒ Viewport
Create a new 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.(self) end |
Instance Attribute Details
#need_to_sort ⇒ Boolean
Tell if the viewport needs to sort
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
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. || 0 goy = @global_offset_y || PSDK_CONFIG. || 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
#dispose ⇒ self
Dispose a viewport
34 35 36 37 |
# File 'scripts/00000 Dependencies/00001 LiteRGSS2/00002 Viewport.rb', line 34 def dispose Graphics.(self) super end |
#flash(color, duration)
Flash the viewport
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)
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
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 |