common_event
简介
通用的静态事件库
组件接口
common_event.gs
FCommonEvent.gs
通用静态事件组件
函数原型 | 函数作用 |
---|---|
void listen_self(string event, CommonEventCallback func) | 用自己的函数响应一个自己的事件 |
void listen(string event, CommonEventCallback event_callback) | 用任意的函数响应一个自己的事件 |
void listen_other(string ob_name, string event, CommonEventCallback event_callback) | 用任意的函数响应一个指定对象的事件 |
void raise(string event, ...) | 触发一个事件 |
mixed raise_answer(string event, ...) | 触发一个抢答式的事件 |
类
CommonEventTree
事件树形结构
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|---|---|---|---|
event_dict | map | nil | 可选 | 事件词典 |
成员方法
SingleCommonEventTree
继承自 CommonEventTree
叶子节点为单一事件处理函数的事件树
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|
成员方法
函数原型 | 函数作用 |
---|---|
void set_event_func(string event, CommonEventCallback func) | 设置事件处理函数 |
bool delete_event_func(string event, CommonEventCallback func) | 删除事件处理函数 |
MultiCommonEventTree
继承自 CommonEventTree
叶子节点为多事件处理函数的事件树
成员变量
变量名 | 类型 | 初始值 | 须初始化 | 描述 |
---|
成员方法
函数原型 | 函数作用 |
---|---|
void set_event_func(string event, CommonEventCallback func) | 设置事件处理函数 |
bool delete_event_func(string event, CommonEventCallback func) | 删除事件处理函数 |
样例
// sample0.gs
// sample code
import pkg.common_event;
import pkg.gtest;
import gs.lang.*;
import gs.util.*;
import .ptest0;
import .ctest0;
import .ctest1;
import .ctest2;
test();
public void test()
{
// 加载所有静态对象
// 以便建立事件触发/响应网络
load_static(ptest0);
load_static(ctest0);
load_static(ctest1);
load_static(ctest2);
array ptest0_arr = common_event.get_event_callbacks(ptest0, "ccc");
printf("ptest0_arr: %O\n", ptest0_arr);
gtest.test_equal(ptest0_arr.length(), 1 + 2 + 3);
array ctest0_arr = common_event.get_event_callbacks(ctest0, "aaa");
printf("ctest0_arr: %O\n", ctest0_arr);
gtest.test_equal(ctest0_arr.length(), 1);
array ctest1_arr = common_event.get_event_callbacks(ctest1, "aaa");
printf("ctest0_arr: %O\n", ctest1_arr);
gtest.test_equal(ctest1_arr.length(), 2);
array ctest2_arr = common_event.get_event_callbacks(ctest2, "aaa");
printf("ctest2_arr: %O\n", ctest2_arr);
gtest.test_equal(ctest2_arr.length(), 3);
array ctest0_brr = common_event.get_event_callbacks(ctest0, "bbb");
printf("ctest0_brr: %O\n", ctest0_brr);
gtest.test_equal(ctest0_brr.length(), 1);
array ctest1_brr = common_event.get_event_callbacks(ctest1, "bbb");
printf("ctest1_brr: %O\n", ctest1_brr);
gtest.test_equal(ctest1_brr.length(), 2);
array ctest2_brr = common_event.get_event_callbacks(ctest2, "bbb");
printf("ctest2_brr: %O\n", ctest2_brr);
gtest.test_equal(ctest2_brr.length(), 3);
object p0 = new_object(ptest0);
object c2 = new_object(ctest2);
object c1 = new_object(ctest1);
object c0 = new_object(ctest0);
raise_all(p0, [ "ccc" ]);
// ccc only raised by ptest0
raise_all(c2, [ "aaa", "bbb", "ccc" ]);
raise_all(c1, [ "aaa", "bbb", "ccc" ]);
raise_all(c0, [ "aaa", "bbb", "ccc" ]);
}
void raise_all(object ob, array events)
{
printf("------------------------------>\n");
for (string event : events)
{
printf("-<%s raise %s>-\n", ob.desc(), event);
ob.raise(event);
}
}