| 赞 | 16 |
| VIP | |
| 好人卡 | |
| 积分 | 37 |
| 经验 | |
| 最后登录 | 2025-12-5 |
| 在线时间 | 3081 小时 |
- 梦石
- 0
- 星屑
- 3703
- 在线时间
- 3081 小时
- 注册时间
- 2011-11-17
- 回帖
- 943
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有账号?注册会员
×
为了帮小伙伴做一套炫酷的ui 所以有了这东西 [pre lang="ruby" line="1"]class NotifyCenter
def initialize
@obsarray = [[],[]]
@procarray = [[],[]]
@desarr = [[],[]]
end
def addObserver(obs,proc,des)#增加监听
@obsarray[obs.kind].push(obs)
if obs.kind == 1#这里预留按钮机制 触摸优先级 可以无视
@obsarray[obs.kind].sort! {|a,b|b.z - a.z}
index = @obsarray[obs.kind].index(obs)
@procarray[obs.kind].insert( index, proc)
@desarr[obs.kind].insert( index, des)
else
@procarray[obs.kind].push(proc)
@desarr[obs.kind].push(des)
end
end
def removeObserver(obs)#移除监听
loop do
index = @obsarray[obs.kind].index(obs)
break if index == nil
@obsarray[obs.kind].delete_at(index)
@procarray[obs.kind].delete_at(index)
@desarr[obs.kind].delete_at(index)
end
#一个对象可能同时被观察多个请求 所以用了循环
end
def postmsg(kind,des)#发送通知 对所有被监听的小伙伴 回调
for i in 0..@desarr[kind].size
if @desarr[kind] == des
@procarray[kind].call
end
end
end
end
###################testclass##############
class Obstest
attr_reader :kind
attr_reader :testnum
def initialize(testnum)
@kind = 0
@testnum = testnum
$notifycenter.addObserver(self,Proc.new { self.test },"aaa")
$notifycenter.addObserver(self,Proc.new { self.test2 },"bbb")
end
def test
p "test"
end
def test2
p @testnum.to_s
end
end
class Obstest2
attr_reader :kind
def initialize
@kind = 0
$notifycenter.addObserver(self,Proc.new { self.test },"ccc")
end
def test
p "testccc"
end
end
[/pre]
测试代码偷懒写在main里了
[pre lang="ruby" line="1"]#==============================================================================
# ■ Main
#------------------------------------------------------------------------------
# 各定义结束后、从这里开始实际处理。
#==============================================================================
begin
# 准备过渡
# 设置系统默认字体
Font.default_name = (["黑体"])
Graphics.freeze
# 生成场景对像 (标题画面)
$scene = Scene_Title.new
################################
$notifycenter = NotifyCenter.new
a = Obstest.new(0)
$notifycenter.postmsg(0,"aaa")
$notifycenter.postmsg(0,"ccc")
p "step 1"
#通知aaa 和 ccc需求 a对象绑定了 aaa 和 bbb需求 所以 aaa对应的函数会被执行 显示test
#但并没有对象绑定ccc 所以 ccc这条消息发出去也是没意义的
b = Obstest.new(1)
$notifycenter.postmsg(0,"aaa")
$notifycenter.postmsg(0,"bbb")
p "step 2"
#通知aaa 和 bbb需求 因为 增加了b对象 所以通知aaa需求的时候 会同时通知 a 和 b对象
#先打印2个 test 再通知bbb需求 打印 0 和1
$notifycenter.removeObserver(a)#将a对象移除监听
$notifycenter.postmsg(0,"aaa")
$notifycenter.postmsg(0,"bbb")
p "step 3"
#因为a对象被移除了 所以 通知 aaa 和 bbb时 只剩下 b对象了 打印 test 和 1
c = Obstest2.new
$notifycenter.postmsg(0,"ccc")
p "over"
#有了c对象 所以也就有了ccc的绑定 这时再通知 会显示testccc了
###############################
# $scene 为有效的情况下调用 main 过程
while $scene != nil
$scene.main
end
# 淡入淡出
Graphics.transition(20)
rescue Errno::ENOENT
# 补充 Errno::ENOENT 以外错误
# 无法打开文件的情况下、显示信息后结束
filename = $!.message.sub("No such file or directory - ", "")
print("找不到文件 #{filename}。 ")
end
[/pre] |
|