123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281 |
- import { SMouseEvent } from "@saga-web/base/lib";
- import { SPainter, SRect } from "@saga-web/draw/lib";
- import { SGraphItem } from "./SGraphItem";
- import { SGraphView } from "./SGraphView";
- /**
- * Graphy图形引擎场景类
- *
- * @author 庞利祥(sybotan@126.com)
- */
- export class SGraphScene {
- /** 展示场景的视图 */
- view: SGraphView | null = null;
- /** 根节点 */
- protected root: SGraphItem = new SGraphItem();
- /** 当前捕获Item */
- grabItem: SGraphItem | null = null;
- /** 鼠标所在Item */
- hoverItem: SGraphItem | null = null;
- /**
- * 构造函数
- */
- constructor() {
- this.root.scene = this;
- } // Constructor
- /**
- * 添加item对象到场景。
- *
- * @param item 添加的对象
- */
- addItem(item: SGraphItem): void {
- item.parent = this.root;
- } // Functin addItem()
- /**
- * 从场景中移除Item。
- *
- * @param item 被移除的对象
- */
- removeItem(item: SGraphItem): void {
- item.parent = null;
- } // Function removeItem()
- /**
- * 绘制场景
- *
- * @param painter painter对象
- * @param rect 更新绘制区域
- */
- drawScene(painter: SPainter, rect: SRect): void {
- this.root.onPaint(painter, rect);
- } // Function drawScene()
- /**
- * 绘制背景
- *
- * @param painter painter对象
- * @param rect 更新绘制区域
- */
- drawBackground(painter: SPainter, rect: SRect) {
- // DO NOTHING
- } // Function drawBackground()
- /**
- * 绘制前景
- *
- * @param painter painter对象
- * @param rect 更新绘制区域
- */
- drawForeground(painter: SPainter, rect: SRect) {
- // DO NOTHING
- } // Function drawForeground()
- /**
- * 所有item占用的矩形区域
- */
- allItemRect(): SRect | null {
- let rect: SRect | null = null;
- // 依次取item列中的所有item。将所有item的边界做并焦处理。
- for (let item of this.root.children) {
- if (rect == null) {
- rect = item.boundingRect().translated(item.pos.x, item.pos.y);
- } else {
- rect.union(
- item.boundingRect().translated(item.pos.x, item.pos.y)
- );
- }
- }
- return rect;
- } // Function allItemRect()
- /**
- * 被选中item占用的矩形区域
- */
- selectedItemRect(): SRect | null {
- let rect: SRect | null = null;
- // 依次取item列中的所有item。将所有item的边界做并焦处理。
- for (let item of this.root.children) {
- // 如果item未被选中,则去选择下一个item
- if (!item.selected) {
- continue;
- }
- if (rect == null) {
- rect = item.boundingRect().translated(item.pos.x, item.pos.y);
- } else {
- rect.union(
- item.boundingRect().translated(item.pos.x, item.pos.y)
- );
- }
- }
- return rect;
- } // Function selectedItemRect()
- /**
- * 获得选中的对象列表
- *
- * @return 选中对象列表
- */
- selectedItems(): SGraphItem[] {
- let itemList = Array<SGraphItem>();
- for (let item of this.root.children) {
- // 如果item未被选中,则去选择下一个item
- if (item.selected) {
- itemList.push(item);
- }
- }
- return itemList;
- } // Function selectedItems()
- // =================================================================================================================
- // 事件
- /**
- * 鼠标单击事件
- *
- * @param event 保存事件参数
- * @return boolean
- */
- onClick(event: SMouseEvent): boolean {
- if (this.grabItem != null) {
- return this.grabItem.onClick(
- SGraphScene.toGrabItemMotionEvent(this.grabItem, event)
- );
- }
- return this.root.onClick(event);
- } // Function onClick()
- /**
- * 鼠标双击事件
- *
- * @param event 保存事件参数
- * @return boolean
- */
- onDoubleClick(event: SMouseEvent): boolean {
- if (this.grabItem != null) {
- return this.grabItem.onDoubleClick(
- SGraphScene.toGrabItemMotionEvent(this.grabItem, event)
- );
- }
- return this.root.onDoubleClick(event);
- } // Function onDoubleClick()
- /**
- * 鼠标按下事件
- *
- * @param event 保存事件参数
- * @return boolean
- */
- onMouseDown(event: SMouseEvent): boolean {
- if (this.grabItem != null) {
- return this.grabItem.onMouseDown(
- SGraphScene.toGrabItemMotionEvent(this.grabItem, event)
- );
- }
- return this.root.onMouseDown(event);
- } // Function onMouseDown()
- /**
- * 鼠标移动事件
- *
- * @param event 保存事件参数
- * @return boolean
- */
- onMouseMove(event: SMouseEvent): boolean {
- if (this.grabItem != null) {
- return this.grabItem.onMouseMove(
- SGraphScene.toGrabItemMotionEvent(this.grabItem, event)
- );
- }
- return this.root.onMouseMove(event);
- } // Function onMouseMove()
- /**
- * 释放鼠标事件
- *
- * @param event 保存事件参数
- * @return boolean
- */
- onMouseUp(event: SMouseEvent): boolean {
- if (this.grabItem != null) {
- return this.grabItem.onMouseUp(
- SGraphScene.toGrabItemMotionEvent(this.grabItem, event)
- );
- }
- return this.root.onMouseUp(event);
- } // Function onMouseUp()
- /**
- * 上下文菜单事件
- *
- * @param event 事件参数
- */
- onContextMenu(event: SMouseEvent): boolean {
- if (this.grabItem != null) {
- return this.grabItem.onContextMenu(
- SGraphScene.toGrabItemMotionEvent(this.grabItem, event)
- );
- }
- return this.root.onContextMenu(event);
- } // Function onContextMenu()
- /**
- * 按键按下事件
- *
- * @param event 事件参数
- */
- onKeyDown(event: KeyboardEvent): void {
- if (this.grabItem != null) {
- return this.grabItem.onKeyDown(event);
- }
- return this.root.onKeyDown(event);
- } // Function onKeyDown()
- // /**
- // * 按键press事件
- // *
- // * @param event 事件参数
- // */
- // onKeyPress(event: KeyboardEvent): void {
- // if (this.grabItem != null) {
- // this.grabItem.onKeyPress(event);
- // }
- // } // Function onKeyPress()
- /**
- * 按键松开事件
- *
- * @param event 事件参数
- */
- onKeyUp(event: KeyboardEvent): void {
- if (this.grabItem != null) {
- return this.grabItem.onKeyUp(event);
- }
- return this.root.onKeyUp(event);
- } // Function onKeyUp()
- /**
- * 转换场景事件坐标到指定Item坐标事件
- *
- * @param item 指定的item对象
- * @param event 场景事件
- * @return {}
- */
- private static toGrabItemMotionEvent(
- item: SGraphItem,
- event: SMouseEvent
- ): SMouseEvent {
- let se = { ...event };
- let p = item.mapFromScene(event.x, event.y);
- se.x = p.x;
- se.y = p.y;
- return se;
- } // Function toGrabItemMotionEvent()
- } // Class SGraphScene
|