unless defined?(Ruby310Compat) begin compat_candidates = [] begin compat_candidates << File.expand_path('000_Ruby310_Compat.rb', File.dirname(__FILE__)) rescue StandardError end begin cwd = Dir.pwd.to_s compat_candidates << File.expand_path('Data/Scripts/000_Ruby310_Compat.rb', cwd) unless cwd.empty? compat_candidates << File.expand_path('000_Ruby310_Compat.rb', cwd) unless cwd.empty? rescue StandardError end compat_candidates.compact.uniq.each do |compat_path| next unless File.file?(compat_path) load compat_path break if defined?(Ruby310Compat) end rescue StandardError end end #supported wheel, by Teravisor #original code by sumptuaryspade@live.ca - Basic Mouse System v2.7d # mouse wheel now uses mkxp-z native Input.scroll_v CPOS = Ruby310Compat.native_api 'user32', 'GetCursorPos', ['p'], 'v' GCRECT = Ruby310Compat.native_api 'user32', 'GetClientRect', ['l','p'], 'v' CL2SCR = Ruby310Compat.native_api 'user32', 'ClientToScreen', ['l','p'], 'i' FGWND = Ruby310Compat.native_api 'user32', 'GetForegroundWindow', 'V', 'L' SHOWMOUS = Ruby310Compat.native_api 'user32', 'ShowCursor', 'i', 'i' SETCURSOR = Ruby310Compat.native_api 'user32', 'SetCursor', ['l'], 'l' LOADCURSOR = Ruby310Compat.native_api 'user32', 'LoadCursorA', ['l','i'], 'l' CREATECURSOR = Ruby310Compat.native_api 'user32', 'CreateCursor', ['l','i','i','i','i','p','p'], 'l' begin SETCLASSCURSOR = Ruby310Compat.native_api 'user32', 'SetClassLongPtrA', ['l','i','l'], 'l' rescue StandardError begin SETCLASSCURSOR = Ruby310Compat.native_api 'user32', 'SetClassLongA', ['l','i','l'], 'l' rescue StandardError SETCLASSCURSOR = nil end end #LOCKMOUS = Ruby310Compat.native_api 'user32', 'ClipCursor', 'i', 'i' module SDLMouseCursorControl SDL2_LIBS = ['SDL2', 'SDL2.dll', 'libSDL2', 'libSDL2.dll'] SDL3_LIBS = ['SDL3', 'SDL3.dll', 'libSDL3', 'libSDL3.dll'] class << self def build_api(lib_names, function_name, import, export) lib_names.each do |lib_name| begin return Ruby310Compat.native_api(lib_name, function_name, import, export) rescue StandardError end end nil end def sdl2_show_cursor @sdl2_show_cursor ||= build_api(SDL2_LIBS, 'SDL_ShowCursor', 'I', 'I') end def sdl3_hide_cursor @sdl3_hide_cursor ||= build_api(SDL3_LIBS, 'SDL_HideCursor', 'V', 'I') end def sdl3_show_cursor @sdl3_show_cursor ||= build_api(SDL3_LIBS, 'SDL_ShowCursor', 'V', 'I') end def hide begin fn = sdl3_hide_cursor return fn.call if fn rescue StandardError end begin fn = sdl2_show_cursor return fn.call(0) if fn rescue StandardError end nil end def show begin fn = sdl3_show_cursor return fn.call if fn rescue StandardError end begin fn = sdl2_show_cursor return fn.call(1) if fn rescue StandardError end nil end end end module Mouse @enabled = false @setup = false @@last_direction = 0 TILE_SIZE = 32.0 def self.center_offset TILE_SIZE / 2.0 end def self.setup @enabled = false @coreX = 0.0 + ((Graphics.width/2) - Graphics.width) @coreY = 0.0 + ((Graphics.height/2) - Graphics.height) @lastPosX = 0 @lastPosY = 0 @lastPosX2 = 0 @lastPosY2 = 0 @m_x = Graphics.width / 2 @m_y = Graphics.height / 2 @idle_timer = 0 @idle_timer_max = 360 @idle = true @setup = true $mouseCursor = Mouse_Cursor.new $mouseMapGrid = Mouse_DrawGridOnMap.new p "Mouse.setup Done" end def self.resolve_hwnd candidates = [] begin hwnd = Graphics.respond_to?(:hwnd) ? Graphics.hwnd : 0 candidates << hwnd if hwnd && hwnd != 0 rescue StandardError end begin hwnd = FGWND.call candidates << hwnd if hwnd && hwnd != 0 rescue StandardError end candidates.compact.uniq.each do |hwnd| begin rect = [0, 0, 0, 0].pack('l4') GCRECT.call(hwnd, rect) left, top, right, bottom = rect.unpack('l4') return hwnd if (right - left) > 0 && (bottom - top) > 0 rescue StandardError end end nil end GCLP_HCURSOR = -12 IDC_ARROW = 32512 def self.enforce_cursor_hidden return unless @enabled hide_system_cursor hwnd = resolve_hwnd apply_hidden_class_cursor(hwnd) end def self.hidden_cursor_handle return @hidden_cursor_handle if @hidden_cursor_handle && @hidden_cursor_handle != 0 begin and_mask = ([0xFF].pack('C') * 128) xor_mask = ([0x00].pack('C') * 128) @hidden_cursor_handle = CREATECURSOR.call(0, 0, 0, 32, 32, and_mask, xor_mask) rescue StandardError @hidden_cursor_handle = 0 end @hidden_cursor_handle end def self.apply_hidden_class_cursor(hwnd) return if hwnd.nil? || hwnd == 0 || SETCLASSCURSOR.nil? @cursor_class_hwnd ||= hwnd if @cursor_class_hwnd != hwnd @class_cursor_hidden = false @cursor_class_hwnd = hwnd end return if @class_cursor_hidden hidden = hidden_cursor_handle return if hidden.nil? || hidden == 0 begin prev = SETCLASSCURSOR.call(hwnd, GCLP_HCURSOR, hidden) @saved_class_cursor = prev if prev && prev != 0 && prev != hidden @class_cursor_hidden = true rescue StandardError end end def self.restore_class_cursor hwnd = @cursor_class_hwnd || resolve_hwnd return if hwnd.nil? || hwnd == 0 || SETCLASSCURSOR.nil? handle = @saved_class_cursor if handle.nil? || handle == 0 begin handle = LOADCURSOR.call(0, IDC_ARROW) rescue StandardError handle = 0 end end begin SETCLASSCURSOR.call(hwnd, GCLP_HCURSOR, handle) rescue StandardError end @class_cursor_hidden = false end def self.update return false unless @enabled enforce_cursor_hidden hwnd = resolve_hwnd return false if hwnd.nil? || hwnd == 0 begin cursor_pt = [0, 0].pack('l2') CPOS.call(cursor_pt) screen_x, screen_y = cursor_pt.unpack('l2') client_origin = [0, 0].pack('l2') CL2SCR.call(hwnd, client_origin) origin_x, origin_y = client_origin.unpack('l2') rect = [0, 0, 0, 0].pack('l4') GCRECT.call(hwnd, rect) left, top, right, bottom = rect.unpack('l4') client_w = [right - left, 1].max client_h = [bottom - top, 1].max x = screen_x - origin_x y = screen_y - origin_y x = 0 if x < 0 y = 0 if y < 0 x = client_w - 1 if x > client_w - 1 y = client_h - 1 if y > client_h - 1 @m_x = ((x.to_f / client_w.to_f) * Graphics.width).to_i @m_y = ((y.to_f / client_h.to_f) * Graphics.height).to_i @m_x = [[@m_x, 0].max, Graphics.width - 1].min @m_y = [[@m_y, 0].max, Graphics.height - 1].min rescue StandardError return false end self.UpdateIdle return true end def self.working? return false unless @enabled return true end #def self.update # return false unless @enabled # @window_loc = WINX.call(0,0,"RGSS PLAYER",0) # rect = '0000000000000000' # cursor_pos = '00000000' # WREC.call(@window_loc, rect) # side, top = rect.unpack("ll") # CPOS.call(cursor_pos) # @m_x, @m_y = cursor_pos.unpack("ll") # w_x = side + SMET.call(5) + SMET.call(45) # w_y = top + SMET.call(6) + SMET.call(46) + SMET.call(4) # @m_x -= w_x; @m_y -= w_y # @m_x = [[@m_x, 0].max,Graphics.width].min # @m_y = [[@m_y, 0].max,Graphics.height].min # self.UpdateIdle # return true #end #def self.ReleaseWheelDll # Unload_Mouse_Wheel.call if !Get_Wheel_Position.nil? #end def self.setuped? @setup end def self.pos? return[-50,-50] unless self.update return [@m_x, @m_y] end def self.moving? #check in outside if @m_x != @lastPosX2 || @m_y != @lastPosY2 @lastPosX2 = @m_x @lastPosY2 = @m_y true else false end end def self.GetMouseXY #check in outside self.update if @m_x.nil? || @m_y.nil? [@m_x || Graphics.width / 2, @m_y || Graphics.height / 2] end def self.ForceMove #check in outside @lastPosX2 = -1 @lastPosY2 = -1 end def self.ForceIdle @m_x = @lastPosX @m_y = @lastPosY @idle = true @idle_timer = -10 end def self.UpdateIdle if @idle_timer < 0 @idle_timer += 1 return true elsif @m_x != @lastPosX || @m_y != @lastPosY || Input.MousePressed? @lastPosX = @m_x @lastPosY = @m_y @idle_timer = @idle_timer_max elsif @idle_timer > 0 @idle_timer -= 1 end @idle = @idle_timer <= 0 end def self.within?(rect) return unless self.update return false if @m_x < rect.x or @m_y < rect.y bound_x = rect.x + rect.width; bound_y = rect.y + rect.height return true if @m_x < bound_x and @m_y < bound_y return false end def self.within_XYWH?(tmpX,tmpY,tmpW,tmpH) return unless self.working? return false if @m_x < tmpX or @m_y < tmpY bound_x = tmpX + tmpW; bound_y = tmpY + tmpH return true if @m_x < bound_x and @m_y < bound_y return false end def self.within_rect?(rect) within_XYWH?(rect.x, rect.y, rect.width, rect.height) end def self.hide_system_cursor(force = false) @hide_cursor_tick ||= 0 @hide_cursor_tick += 1 hidden = hidden_cursor_handle begin SETCURSOR.call(hidden && hidden != 0 ? hidden : 0) rescue StandardError end begin SDLMouseCursorControl.hide rescue StandardError end return unless force || @hide_cursor_tick >= 4 @hide_cursor_tick = 0 begin 32.times do result = SHOWMOUS.call(0) break if result < 0 end rescue StandardError end end def self.show_system_cursor begin SDLMouseCursorControl.show rescue StandardError end begin 16.times do result = SHOWMOUS.call(1) break if result >= 0 end rescue StandardError end end def self.disable @enabled = false @idle = true InputUtils.mouse_off InputUtils.load_input_settings restore_class_cursor show_system_cursor end def self.enable @enabled = true @idle = false @idle_timer = @idle_timer_max enforce_cursor_hidden hide_system_cursor(true) InputUtils.mouse_on InputUtils.load_input_settings self.update rescue nil self.ForceMove end def self.enable? @enabled && !@idle end def self.usable? @enabled end def self.idle? @idle end def self.get_idle_opacity @idle_timer end #def self.GetDirection # x = @coreX + @m_x # y = @coreY + @m_y+4 # angle = Math.atan(x.abs/y.abs) * (180 / Math::PI) # angle = (90 - angle) + 90 if x > 0 && y > 0 # angle += 180 if x < 0 && y >= 0 # angle = 90 - angle + 180 + 90 if x < 0 && y < 0 # #p "angleC = #{x} #{y} #{angle}" # #return 8 if angle < 60 || angle > 298 # #return 6 if angle < 119 # #return 2 if angle < 240 # #return 4 if angle < 298 # return 0 if angle == 0 # return 8 if angle <= 45 || angle >= 315 # return 6 if angle <= 135 # return 2 if angle <= 225 # return 4 if angle <= 315 # return 0 #end def self.GetDirection self.update if @m_x.nil? || @m_y.nil? return @@last_direction if @m_x.nil? || @m_y.nil? # 相對畫面中心向量 dx = @m_x - Graphics.width / 2.0 dy = @m_y - Graphics.height / 2.0 # 中心死區32x32(半徑16px,綠方塊範圍) if dx.abs < 16 && dy.abs < 16 return @@last_direction # 死區內不變,回上一次 end # 標準atan2角度(0右,90上,180左,270下),轉正值0~360 angle = Math.atan2(dy, dx) * 180 / Math::PI angle += 360 if angle < 0 # 45度X分界(紅線) dir = case when angle < 45 || angle >= 315 then 6 # 右 when angle < 135 then 2 # 下 when angle < 225 then 4 # 左 when angle < 315 then 8 # 上 else 0 end @@last_direction = dir # 更新上次方向 return dir end def self.GetScreenEdge self.update if @m_x.nil? || @m_y.nil? return 0 if @m_x.nil? || @m_y.nil? return 4 if @m_x <= 5 return 8 if @m_y <= 5 return 2 if @m_y >= Graphics.height - 5 return 6 if @m_x >= Graphics.width - 5 return 0 end end ############################################################################################################### module Input #550_InputMenu.rb #unused OEM key replace with wheel, DUmb Dumb but i dont need too much new code. @lastMouseWheel = 0 @RecordedDIR=0 @MouseSkipKeyPressed = 0 @doubleTapTimer = 0 @doubleTapKeyRec = nil class <= 0 @doubleTapKeyRec = nil @doubleTapTimer = 0 return true else @doubleTapKeyRec = key @doubleTapTimer = 15 return false end end def self.UpdatelastDirRecord @RecordedDIR= 2 if self.trigger?(:DOWN) @RecordedDIR= 4 if self.trigger?(:LEFT) @RecordedDIR= 6 if self.trigger?(:RIGHT) @RecordedDIR= 8 if self.trigger?(:UP) end def self.MousePressed? #mostly used to check mouse idle return true if [:LBUTTON,:RBUTTON,:MBUTTON,:XBUTTON1,:XBUTTON2].any? {|key| self.press?(key)} return true if [:WheelU,:WheelD].any? {|key| self.trigger?(key)} return true if self.WheelReport != nil false end def self.MouseButtoned? #for main menu check return true if [:LBUTTON,:RBUTTON,:MBUTTON,:XBUTTON1,:XBUTTON2].any? {|key| self.press?(key)} return true if self.WheelReport != nil false end def self.Mouse_LR_Pressed? # unused return true if self.press?(:MX_LINK) return true if self.press?(:MZ_LINK) return false end def self.Mouse_Wheeled? #for main menu check return true if [:WheelU,:WheelD].any? {|key| self.trigger?(key)} return false end def self.MouseWheelForceIdle? #when input scroll or key board. force idle return true if [:UP,:DOWN,:LEFT,:RIGHT].any? {|key| self.trigger?(key)} return true if [:UP,:DOWN,:LEFT,:RIGHT].any? {|key| self.repeat?(key)} #return true if self.repeat?(:L) #return true if self.repeat?(:R) return false end def self.MsgSkipKeyPressed? return true if self.press?(:CTRL) if self.press?(:MZ_LINK) @MouseSkipKeyPressed += 1 return true if @MouseSkipKeyPressed >= 30 else @MouseSkipKeyPressed = 0 return false end end def self.KeyboardMouseGetScreenEdgeDir4 #new def tmpDir4 = self.dir4 return self.dir4 if dir4 > 0 return 0 if !Mouse.enable? tmpDir4 = Mouse.GetScreenEdge return tmpDir4 if tmpDir4 > 0 0 end def self.KeyboardMouseDir4 #new def tmpDir4 = self.dir4 return self.dir4 if dir4 > 0 return 0 if !Mouse.enable? tmpDir4 = Mouse.GetDirection return tmpDir4 if self.press?(:MRB) && tmpDir4 > 0 0 end #def self.TriggerMouseDir4?(tgtdir) # #return false if !self.trigger?(:MRB) # input_dir = [self.KeyboardMouseDir4,self.KeyboardMouseGetScreenEdgeDir4].max # result = false # case tgtdir # when :DOWN ; result = true if input_dir == 2 # when :LEFT ; result = true if input_dir == 4 # when :RIGHT ; result = true if input_dir == 6 # when :UP ; result = true if input_dir == 8 # else # return false # end # return false unless Mouse.enable? # if self.press?(:MRB) # if @RecordedMouseTriggerDIR != input_dir # @RecordedMouseTriggerDIR = input_dir # return result # else # return false # end # end #end def self.TriggerMouseDir4?(tgtdir) return false unless Mouse.enable? @RecordedMouseTriggerDIR = 0 if !self.press?(:MRB) return false unless self.press?(:MRB) input_dir = [self.KeyboardMouseDir4, self.KeyboardMouseGetScreenEdgeDir4].max dir_map = { :DOWN => 2, :LEFT => 4, :RIGHT => 6, :UP => 8 } return false unless dir_map.key?(tgtdir) expected_dir = dir_map[tgtdir] return false if input_dir != expected_dir # Trigger only if direction changed since last time if @RecordedMouseTriggerDIR != input_dir @RecordedMouseTriggerDIR = input_dir return true end return false end def self.UpdateMouseDirTriggerReset @RecordedMouseTriggerDIR = nil unless self.press?(:MRB) end def self.WheelReport # true up, false down begin return nil unless respond_to?(:scroll_v) delta = self.scroll_v.to_i return true if delta > 0 return false if delta < 0 rescue StandardError end nil end def self.MouseUpdate #new def return if !Mouse.usable? tmpWheelReport = self.WheelReport if !tmpWheelReport.nil? if tmpWheelReport key = KEYMAP[:WheelU].to_i else key = KEYMAP[:WheelD].to_i end if @state[key]# && DOWN_STATE_MASK == DOWN_STATE_MASK @released[key] = false @pressed[key] = true if (@triggered[key] = !@pressed[key]) @repeated[key] < 17 ? @repeated[key] += 1 : @repeated[key] = 15 elsif !@released[key] and @pressed[key] @triggered[key] = false @pressed[key] = false @repeated[key] = 0 @released[key] = true else @released[key] = false end end end end class Scene_Base def mouse_cursor_update Mouse.hide_system_cursor if Mouse.usable? pos = Mouse.pos? $mouseMapGrid.update $mouseCursor.x = pos[0] -1#+ CURSOR_OFFSET_X $mouseCursor.y = pos[1] -1#+ CURSOR_OFFSET_Y $mouseCursor.opacity = Mouse.enable? ? 255 : 75 #[50,Mouse.get_idle_opacity].max return $mouseCursor.set_icon(542) if Input.press?(:MLB) || Input.press?(:MRB) return $mouseCursor.set_icon(541) end end class Mouse_Cursor < Sprite_Base def initialize super #@icon = 541 #@iconClick = 542 @icon = nil @icon_bitmap = Cache.system("Iconset") self.bitmap = Bitmap.new(24,24) set_icon(541) self.z = System_Settings::SCENE_Menu_CursorMouse_Z self.opacity = 0 end def set_icon(icon) return if @icon == icon @icon = icon draw_cursor(@icon) end def draw_cursor(icon) self.bitmap.clear rect = Rect.new(icon % 16 * 24, icon / 16 * 24, 24, 24) self.bitmap.blt(0, 0, @icon_bitmap, rect) end end #only used in game_map class Mouse_DrawGridOnMap < Sprite_Base def initialize super self.bitmap = Bitmap.new(32,32) self.bitmap.fill_rect(self.bitmap.rect,Color.new(255,255,255)) self.bitmap.clear_rect(1,1,30,30) self.bitmap.clear_rect(0,8,32,16) self.bitmap.clear_rect(8,0,16,32) #self.bitmap.clear_rect(4,0,8,32) self.opacity = 0 self.z = System_Settings::MOUSE_GRID_ON_MAP_Z #MAP_VP2_Z @updateX = 0 @updateY = 0 end def start(tmpX,tmpY) self.x = $game_map.adjust_x(tmpX) * 32 self.y = $game_map.adjust_y(tmpY) * 32 self.opacity = 250 @updateX = tmpX @updateY = tmpY end def update return if self.opacity <= 0 self.x = $game_map.adjust_x(@updateX) * 32 self.y = $game_map.adjust_y(@updateY) * 32 self.opacity -= 25 end end ############################################################################################################### #class Window_Selectable < Window_Base class Window_ChoiceList < Window_Command #alias mouse_update update #def update # mouse_update # update_mouse_option_window if self.active #end def update_mouse_option_window return Mouse.ForceIdle if Input.MouseWheelForceIdle? return if !Mouse.enable? tmpPressed_MZ_LINK = Input.trigger?(:MZ_LINK) return process_cancel if Input.trigger?(:MX_LINK) && cancel_enabled? return if !Mouse.within_XYWH?(@viewport.rect.x,@viewport.rect.y,@viewport.rect.width,@viewport.rect.height+standard_padding) #可Window_ChoiceList Window_Command viewport return if !tmpPressed_MZ_LINK #p "mouseXY #{Mouse.GetMouseXY}" tmpOptionIndex = self.index target_rect = [-1,-1,-1,-1] item_max.times {|i| x_fix = self.x - self.ox y_fix = (self.y + standard_padding + (i*item_height)) - self.oy #self.oy 為卷軸Y x_fix += self.viewport.rect.x - self.viewport.ox if !self.viewport.nil? x_fix = item_rect(i).x+(item_rect(i).width/2) - (text_size(command_name(i)).width/2) x_fix += standard_padding w_fix = text_size(command_name(i)).width exportRect = [x_fix,y_fix,w_fix,item_height] next unless Mouse.within_XYWH?(*exportRect) self.index = i target_rect = exportRect #p "self.index #{self.index}" #p "item_rect(self.index) #{item_rect(self.index)}" #p "command_name(index) #{command_name(self.index)}" ##p text_width(command_name(self.index)) } if self.index != tmpOptionIndex select(self.index) SndLib.play_cursor return elsif tmpPressed_MZ_LINK && ok_enabled? && Mouse.within_XYWH?(*target_rect) process_ok SndLib.play_cursor elsif tmpPressed_MZ_LINK SndLib.sys_buzzer end end end class Window_TradeStorageLeft < Window_ItemList #sample for normal trade def mouse_input return Mouse.ForceIdle if Input.MouseWheelForceIdle? return if !Mouse.enable? @mouse_all_rects = [] item_max.times {|i| rect = item_rect(i) rect.x += self.x + standard_padding - self.ox rect.y += self.y + standard_padding - self.oy if !self.viewport.nil? rect.x += self.viewport.rect.x - self.viewport.ox rect.y += self.viewport.rect.y - self.viewport.oy end @mouse_all_rects.push(rect) } tmpIndex = self.index item_max.times {|i| next unless Mouse.within?(@mouse_all_rects[i]) self.index = i } if self.index != tmpIndex select(self.index) SndLib.play_cursor return end end def within_index item_max.times {|i| return true if Mouse.within?(@mouse_all_rects[i]) } return false end end class Scene_File < Scene_MenuBase #alias mouse_update update #def update # mouse_update # mouse_input #end def mouse_input return if !Mouse.enable? return on_savefile_cancel if Input.trigger?(:MX_LINK) #and Mouse.within?(rectttl) return if !Input.trigger?(:MZ_LINK) xx = 0 yy = 20 saveWidth = Graphics.width/2-32 rectcm1 = Rect.new(xx, yy, saveWidth, savefile_height) rectcm2 = Rect.new(xx, yy + rectcm1.height, saveWidth, savefile_height) rectcm3 = Rect.new(xx, yy + rectcm1.height * 2, saveWidth, savefile_height) rectcm4 = Rect.new(xx, yy + rectcm1.height * 3, saveWidth, savefile_height) rectttl = Rect.new(xx, yy, saveWidth, rectcm1.height * 4) @scroll = self.top_index last_index = @index @index = (0 + @scroll) if Mouse.within?(rectcm1) @index = (1 + @scroll) if Mouse.within?(rectcm2) @index = (2 + @scroll) if Mouse.within?(rectcm3) @index = (3 + @scroll) if Mouse.within?(rectcm4) if @index != last_index refresh_cursor(last_index,@index) #SndLib.play_cursor #@savefile_windows[last_index].selected = false #@savefile_windows[@index].selected = true #@overview_window.set_file_index(@index) elsif Input.trigger?(:MZ_LINK) and Mouse.within?(rectttl) on_savefile_ok elsif Input.trigger?(:MZ_LINK) and !Mouse.within?(rectttl) SndLib.sys_buzzer end end end ############################################################################################ ############################################################################################ ############################################################################################