设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 1625|回复: 1

[rgss3]嘗試一下寫個鼠標用的UI組件

[复制链接]

酱油的

梦石
0
星屑
930
在线时间
2161 小时
注册时间
2007-12-22
帖子
3271

贵宾

发表于 2012-6-19 09:25:21 | 显示全部楼层 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
本帖最后由 禾西 于 2012-7-12 14:29 编辑

鼠標核心由這裡取得:http://rpg.blue/thread-147594-1-1.html

這是測試用的依賴部分
RUBY 代码复制
  1. def Object.wrt_binding(*words)
  2.   return if words.size < 1
  3.   buff = ""
  4.   for i in 1...words.size
  5.     buff += "@#{words[i]}=val;"
  6.   end
  7.   class_eval %{
  8.     def #{words[0]}=(val);#{buff}end
  9.   }
  10. end
  11. $alias_acount = 0
  12. def Object.amend(method_name, &amendment)
  13.   $alias_acount += 1
  14.   class_eval %{
  15.     @@proc#{$alias_acount} = amendment
  16.     alias method#{$alias_acount} #{method_name}
  17.     def #{method_name}(*args)
  18.       method#{$alias_acount}(*args)
  19.       @@proc#{$alias_acount}.call(*args)
  20.     end
  21.   }
  22. end


這才是主腳本
RUBY 代码复制
  1. # ---------------------------------------------------------------------- #
  2. # 作者:禾西                                                             #
  3. # 最新update: 2012/7/07                                                  #
  4. # ---------------------------------------------------------------------- #
  5. module Mouse
  6.   class Point
  7.     attr_reader :x
  8.     attr_reader :y
  9.     def initalize(x,y)
  10.       set(x,y)
  11.     end
  12.     def set(pos)
  13.       # pos有可能等于nil,所以還是檢查一下。
  14.       return false unless pos.is_a?(Array)
  15.       return false if @x == pos[0] && @y == pos[1]
  16.       @x = pos[0]
  17.       @y = pos[1]
  18.       return true
  19.     end
  20.   end
  21.   # -------------------------------------------------------------------- #
  22.   # 矩形的區域判斷:x, y 為左上角
  23.   # -------------------------------------------------------------------- #
  24.   module UIRect
  25.     def include?(point)
  26.       if point.x >= @x && point.x <= (@x + @w) &&
  27.          point.y >= @y && point.y <= (@y + @h)
  28.         return true
  29.       end
  30.       return false
  31.     end
  32.   end
  33.   # -------------------------------------------------------------------- #
  34.   # 圓形的區域判斷:x, y 為矩形的左上角。圓心為 (x + w, y + h)
  35.   # -------------------------------------------------------------------- #
  36.   module UICircle
  37.     def include?(point)
  38.       if (point.x - @x - @w)**2 + (point.y - @y - @h)**2 < @w*@h/4
  39.         return true
  40.       end
  41.       return false
  42.     end
  43.   end
  44.   # -------------------------------------------------------------------- #
  45.   # Component 的數據記錄(將來可能會和Component直接結合,所以不建議使用)#
  46.   # -------------------------------------------------------------------- #
  47.   module UICore
  48.     module_function
  49.     def init
  50.       @@components = []
  51.     end
  52.     def add(comp)
  53.       #z值反向排序(最高的放最前面),
  54.       #查詢的時候正向查詢,以便快速找到z值最高的。
  55.       for i in 0...@@components.size
  56.         unless comp.z < @@components[i].z #如果這個組件的z值不比它大
  57.           @@components.insert(i,comp) #則取代這個組件的位置(把組件推後)
  58.           return
  59.         end
  60.       end
  61.       #如果列表搜索已經到頭(z值都比它大)
  62.       @@components.push(comp) #直接插入尾部(最小位置)
  63.     end
  64.  
  65.     def del(comp)
  66.       @@components.delete(comp)
  67.     end
  68.     def check(point)
  69.       @@components.each do |c|
  70.         return c if c.include?(point)
  71.       end
  72.       return nil
  73.     end
  74.  
  75.     # ------------------------------------------------------------------ #
  76.     # Debug                                                              #
  77.     # ------------------------------------------------------------------ #
  78.     def getComponents
  79.       return @@components
  80.     end
  81.   end
  82.  
  83.   # -------------------------------------------------------------------- #
  84.   # 抽象的 Component 基類                                                #
  85.   # -------------------------------------------------------------------- #
  86.   class Component
  87.     include UIRect
  88.     attr_reader :x
  89.     attr_reader :y
  90.     attr_reader :w
  91.     attr_reader :h
  92.     attr_reader :z
  93.     attr_reader :str
  94.     attr_reader :disposed
  95.     def initialize(x,y,w,h,z,parent = nil)
  96.       @x = x
  97.       @y = y
  98.       @w = w
  99.       @h = h
  100.       @z = z
  101.       @disposed = false
  102.       UICore.add(self)
  103.       @children = []
  104.       if parent != nil
  105.         test(parent)
  106.         parent.add(self)
  107.       end
  108.       @str = ""
  109.       @bound = false
  110.       @_spr = Sprite.new()
  111.       @_spr.x = x
  112.       @_spr.y = y
  113.       @_spr.z = z
  114.       @_spr.bitmap = Bitmap.new(w,h)
  115.       onInit
  116.     end
  117.  
  118.     # ------------------------------------------------------------------ #
  119.     def add(comp)
  120.       test(comp)
  121.       @children.push(comp) if !@children.include?(comp)
  122.     end
  123.  
  124.     # ------------------------------------------------------------------ #
  125.     def del(comp)
  126.       test(comp)
  127.       @children.delete(comp)
  128.       comp.dispose
  129.     end
  130.  
  131.     # ------------------------------------------------------------------ #
  132.     def dispose
  133.       @disposed = true
  134.       @children.each do |comp|
  135.         comp.dispose
  136.       end
  137.       UICore.del(self)
  138.       @_spr.dispose
  139.     end
  140.  
  141.     # ------------------------------------------------------------------ #
  142.     def x=(new_x)
  143.       delta = new_x - @x
  144.       @children.each do |comp|
  145.         comp.x += delta
  146.       end
  147.       @x = new_x
  148.       @_spr.x = new_x
  149.     end
  150.  
  151.     # ------------------------------------------------------------------ #
  152.     def y=(new_y)
  153.       delta = new_y - @y
  154.       @children.each do |comp|
  155.         comp.y += delta
  156.       end
  157.       @y = new_y
  158.       @_spr.y = new_y
  159.     end
  160.  
  161.     # ------------------------------------------------------------------ #
  162.     def z=(new_z)
  163.       @z = new_z
  164.       @_spr.z = new_z
  165.       UICore.del(self)
  166.       UICore.add(self)
  167.     end
  168.  
  169.     # ------------------------------------------------------------------ #
  170.     def str=(new_str)
  171.       @str = new_str
  172.       onInit
  173.     end
  174.  
  175.     # ------------------------------------------------------------------ #
  176.     def getContents
  177.       return @_spr.bitmap
  178.     end
  179.  
  180.     # ------------------------------------------------------------------ #
  181.     def setContents(bitmap)
  182.       @_spr.bitmap = bitmap
  183.     end
  184.  
  185.     # ------------------------------------------------------------------ #
  186.     def drawStr(str, x, y,fontSize = nil)
  187.       getContents.font.size = fontSize if fontSize != nil
  188.       rect = getContents.text_size(str)
  189.       rect.x = x
  190.       rect.y = y
  191.       getContents.draw_text(rect,str)
  192.     end
  193.  
  194.     # ------------------------------------------------------------------ #
  195.     def leave
  196.       return if !@bound
  197.       @bound = false
  198.       onLeave
  199.     end
  200.  
  201.     # ------------------------------------------------------------------ #
  202.     def enter
  203.       return if @bound
  204.       @bound = true
  205.       onEnter
  206.     end
  207.  
  208.  
  209.     # ------------------------------------------------------------------ #
  210.     def test(comp)
  211.       if !comp.is_a?(Component)
  212.         raise "#{comp} is not a object deriving from Component!"
  213.       end
  214.     end
  215.  
  216.     # ------------------------------------------------------------------ #
  217.     def send(msg)
  218.       raise "#{self} received msg #{msg}"
  219.     end
  220.  
  221.     # ------------------------------------------------------------------ #
  222.     def onInit
  223.       getContents.fill_rect(0, 0, w, h, Color.new(255,0,0))
  224.       getContents.fill_rect(6, 6, w-12, h-12, Color.new(0,0,0,0))
  225.       rect = getContents.text_size(str)
  226.       rect.x = (w - rect.width)/2
  227.       rect.y = (h - rect.height)/2
  228.       getContents.draw_text(rect,str)
  229.       p "init #{self}"
  230.     end
  231.     # ------------------------------------------------------------------ #
  232.     def onLeave
  233.       msg = str == "" ? self : str
  234.       p "leave #{msg}"
  235.     end
  236.     # ------------------------------------------------------------------ #
  237.     def onEnter
  238.       msg = str == "" ? self : str
  239.       p "enter #{msg}"
  240.     end
  241.     # ------------------------------------------------------------------ #
  242.     def onClick(key)
  243.       msg = str == "" ? self : str
  244.       p "click #{msg}"
  245.     end
  246.   end
  247.  
  248.   # -------------------------------------------------------------------- #
  249.   def self.init
  250.     @@point  = Point.new(0, 0) #初始化坐標標記
  251.     @@thread = Thread.new{}    #初始化搜索thread
  252.     @@current_comp = nil       #初始化所處組件標記
  253.     UICore.init
  254.   end
  255.  
  256.   # -------------------------------------------------------------------- #
  257.   def self.run
  258.     update
  259.     # -------------------------------------------------------- #
  260.     # 如果設置坐標成功 => (pos != []) && (point != pos) 則搜索 #
  261.     # 這段代碼的效率非常的糟糕,但是我想不到更好的辦法         #
  262.     # -------------------------------------------------------- #
  263.     if @@point.set(Mouse.pos)
  264.       @@thread.kill if @@thread.alive?
  265.       @@thread = Thread.new {
  266.         t1 = Time.now
  267.         current_comp = UICore.check(@@point)
  268.         if current_comp != @@current_comp
  269.           @@current_comp.leave unless @@current_comp.nil?
  270.           @@current_comp = current_comp
  271.           @@current_comp.enter unless @@current_comp.nil?
  272.         end
  273.       }
  274.     end
  275.     # -------------------------------------------------------- #
  276.     key = 0
  277.     key^=1 if up?(1)
  278.     key^=2 if up?(2)
  279.     key^=4 if up?(4)
  280.     if !@@current_comp.nil? && key > 0
  281.       if @@current_comp.disposed
  282.         @@current_comp = nil
  283.       else
  284.         @@current_comp.onClick(key)
  285.       end
  286.     end
  287.     # -------------------------------------------------------- #
  288.   end
  289. end
  290.  
  291. class Debug
  292.   def self.test(a, b, off = 0)
  293.     if !a.equal?(b)
  294.       raise "fail! Expect #{a}, but got #{b}"
  295.     end
  296.   end
  297. end
  298. Mouse.init
  299. Scene_Base.amend(:update_basic){ Mouse.run }

酱油的

梦石
0
星屑
930
在线时间
2161 小时
注册时间
2007-12-22
帖子
3271

贵宾

 楼主| 发表于 2012-6-22 17:07:01 | 显示全部楼层
本帖最后由 禾西 于 2012-7-12 14:30 编辑

沒有歷史版本> <
不做頭像做簽名,看我囧冏有神(多謝山人有情提供 )
回复 支持 反对

使用道具 举报

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-3-29 13:27

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表