public void DisplayTileSetBrush() { var e = Event.current; bool is_mouse_inside_tileSetBrush_palette_scroll_area = e.isMouse && tileSetBrush_palette_scroll_area_rect.Contains(e.mousePosition); using (new EditorGUILayoutBeginHorizontalScope()) { string tileSetBrushId_label = tileSet.selected_tileSetBrushId > 0 ? string.Format("(id:{0})", tileSet.selected_tileSetBrushId) : ""; EditorGUILayout.LabelField("TileSetBrush Palette" + tileSetBrushId_label, EditorStyles.boldLabel); is_display_tileSetBrush_reordableList = GUILayoutUtil.ToggleButton("Display List", is_display_tileSetBrush_reordableList); } // tileSet.tileSetBrush_type_mask = EditorGUILayout.MaskField("TileSetBrush Mask", tileSet.tileSetBrush_type_mask, // tileSet.GetTileSetBrushTypeArray()); int column_count = (int)(tile_scroll_size_rect.width / (tileSet.visual_tile_size.x + visual_tile_padding)); if (column_count <= 0) { column_count = 1; } float tileSetBrushes_scroll_max_height = Screen.height / 4; //commented because m_rTileScrollSize.width.height was changed to Screen.height; fBrushesScrollMaxHeight -= fBrushesScrollMaxHeight % 2; // sometimes because size of tile scroll affects size of brush scroll, the height is dancing between +-1, so this is always taking the pair value float tileSetBrush_palette_scroll_area_height = Mathf.Min(tileSetBrushes_scroll_max_height, 4 + (tileSet.visual_tile_size.y + visual_tile_padding) * (1 + (tileSet.tileSetBrush_list.Count / column_count))); using (new EditorGUILayoutBeginVerticalScope(GUILayout.MinHeight(tileSetBrush_palette_scroll_area_height))) { if (is_display_tileSetBrush_reordableList) { DisplayTileSetBrushReorderableList(); } else { bool is_refresh_tileSetBrushList = false; Vector2 tileSetBrushes_scroll_pos = sharedTileSetData.tileSetBrushes_scroll_pos; using (new EditorGUILayoutBeginScrollViewScope(ref tileSetBrushes_scroll_pos, GUIStyleConst.Scroll_Style)) { if (is_update_scroll_pos) { sharedTileSetData.tileSetBrushes_scroll_pos = tileSetBrushes_scroll_pos; } Rect scroll_view_rect = new Rect(0, 0, tile_scroll_size_rect.width, 0); column_count = Mathf.Clamp((int)scroll_view_rect.width / (int)(tileSet.visual_tile_size.x + visual_tile_padding), 1, column_count); if (tileSet.tileSetBrush_list != null) { GUILayout.Space((tileSet.visual_tile_size.y + visual_tile_padding) * (1 + (tileSet.tileSetBrush_list.Count - 1) / column_count)); for (int i = 0, index = 0; i < tileSet.tileSetBrush_list.Count; ++i, ++index) { var tileSetBrushContainer = tileSet.tileSetBrush_list[i]; if (tileSetBrushContainer.tileSetBrush == null || tileSetBrushContainer.tileSetBrush.tileSet != tileSet) { is_refresh_tileSetBrushList = true; continue; } if (!tileSetBrushContainer.tileSetBrush.is_show_in_palette || !tileSet.IsTileSetBrushVisibleByTypeMask(tileSetBrushContainer.tileSetBrush)) { --index; continue; } int tile_x = index % column_count; int tile_y = index / column_count; Rect visual_tile_rect = new Rect(2 + tile_x * (tileSet.visual_tile_size.x + visual_tile_padding), 2 + tile_y * (tileSet.visual_tile_size.y + visual_tile_padding), tileSet.visual_tile_size.x, tileSet.visual_tile_size.y); //Fix Missing Tileset reference if (tileSetBrushContainer.tileSetBrush.tileSet == null) { LogCat.warn(string.Format("Fixed missing tileset reference in brush {0} Id({1})", tileSetBrushContainer.tileSetBrush.name, tileSetBrushContainer.id)); tileSetBrushContainer.tileSetBrush.tileSet = tileSet; } uint tileData = TileSetConst.TileData_Empty; if (tileSetBrushContainer.tileSetBrush.IsAnimated()) { tileData = tileSetBrushContainer.tileSetBrush.GetAnimTileData(); } else { tileData = tileSetBrushContainer.tileSetBrush.PreviewTileData(); } TileSetEditor.DoGUIDrawTileFromTileData(visual_tile_rect, tileData, tileSet, tileSetBrushContainer.tileSetBrush.GetAnimUV()); if ((is_left_mouse_released || is_right_mouse_released || mouseDoubleClick.IsDoubleClick) && //左键放开,右键放开,双击鼠标 is_mouse_inside_tileSetBrush_palette_scroll_area && visual_tile_rect.Contains(Event.current.mousePosition))//点钟该区域 { tileSet.selected_tileSetBrushId = tileSetBrushContainer.id; RemoveTileSelection(); if (mouseDoubleClick.IsDoubleClick) { EditorGUIUtility.PingObject(tileSetBrushContainer.tileSetBrush); selected_tileSetBrush_in_inspector = tileSetBrushContainer.tileSetBrush; } if (is_right_mouse_released) { TilePropertiesEditor.Show(tileSet); } } else if (tileSet.selected_tileSetBrushId == tileSetBrushContainer.id) { Rect selection_rect = new Rect(2 + tile_x * (tileSet.visual_tile_size.x + visual_tile_padding), 2 + tile_y * (tileSet.visual_tile_size.y + visual_tile_padding), (tileSet.visual_tile_size.x + visual_tile_padding), (tileSet.visual_tile_size.y + visual_tile_padding)); DrawUtil.HandlesDrawSolidRectangleWithOutline(selection_rect, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 0f, 1f)); } } if (is_refresh_tileSetBrushList) { tileSet.RemoveInvalidTileSetBrushList(); tileSet.UpdateTileSetBrushTypeArray(); } } } if (e.type == EventType.Repaint) { tileSetBrush_palette_scroll_area_rect = GUILayoutUtility.GetLastRect(); } } } if (GUILayout.Button("Import all tileSetBrush_list found in the project")) { AddAllTileSetBrushesFoundInTheProject(tileSet); EditorUtility.SetDirty(tileSet); } }
private Vector2 last_tile_palette_scroll_mouse_pos;//上次在tile_palette中scroll_mouse_pos public void DrawTilePalette() { Event e = Event.current; is_left_mouse_released = e.type == EventType.MouseUp && e.button == 0; //鼠标左键放开 is_right_mouse_released = e.type == EventType.MouseUp && e.button == 1; //鼠标右键放开 is_mouse_inside_tile_palette_scroll_area = e.isMouse && tile_palette_scroll_area_rect.Contains(e.mousePosition); //鼠标是否在tile_palette中 //header string tileId_label = tileSet.selected_tileId != TileSetConst.TileId_Empty ? string.Format("(id:{0})", tileSet.selected_tileId) : ""; EditorGUILayout.LabelField(string.Format("Tile Palette {0}", tileId_label), EditorStyles.boldLabel); sharedTileSetData.tileView_column_count_in_tile_palette = Mathf.Max(1, sharedTileSetData.tileView_column_count_in_tile_palette); float tile_area_width = sharedTileSetData.tileView_column_count_in_tile_palette * (tileSet.visual_tile_size.x + visual_tile_padding) + 4f; float tile_area_height = (tileSet.visual_tile_size.y + visual_tile_padding) * (1 + (visible_tile_count - 1) / sharedTileSetData.tileView_column_count_in_tile_palette) + 4f; sharedTileSetData.tileView_column_count_in_tile_palette = tileView != null ? tileView.tileSelection.column_count : tileSet.column_tile_count_in_palette; Vector2 tile_palette_scroll_pos = sharedTileSetData.tile_palette_scroll_pos; using (new EditorGUILayoutBeginScrollViewScope(ref tile_palette_scroll_pos, GUIStyleConst.Scroll_Style)) { //更新新tile_palette中的scroll_pos if (is_update_scroll_pos) { sharedTileSetData.tile_palette_scroll_pos = tile_palette_scroll_pos; } if (e.type == EventType.MouseDrag && (e.button == 1 || e.button == 2))//鼠标中键或者右键拖动中 { sharedTileSetData.tile_palette_scroll_pos = sharedTileSetData.tile_palette_scroll_pos - e.delta; } else { DoAutoScroll(); } //记录上次在tile_palette中scroll_mouse_pos if (e.isMouse) { last_tile_palette_scroll_mouse_pos = e.mousePosition; } if (tileSet.tile_list != null) { GUILayoutUtility.GetRect(tile_area_width, tile_area_height); visible_tile_count = 0; List <uint> visible_tile_list = new List <uint>(); int tileView_column_count = tileView != null ? tileView.tileSelection.column_count : tileSet.column_tile_count; int tileView_row_count = tileView != null ? ((tileView.tileSelection.selection_tileData_list.Count - 1) / tileView.tileSelection.column_count) + 1 : tileSet.row_tile_count; //在tile_palette中全部的tile个数(可能比实际的tile_count多,因为可能出现空的tile来填满行或者列的tile) int total_tile_count_in_tile_palette = ((((tileView_column_count - 1) / sharedTileSetData.tileView_column_count_in_tile_palette) + 1) * sharedTileSetData.tileView_column_count_in_tile_palette) * tileView_row_count; int tileId_offset = 0; for (int i = 0; i < total_tile_count_in_tile_palette; ++i) { //i是在tile_palette中的index(在column_tile_count_in_palette下) int tileId = GetTileIdFromIndex(i, sharedTileSetData.tileView_column_count_in_tile_palette, tileView_column_count, tileView_row_count) + tileId_offset; uint tileData = (uint)tileId; if (tileView != null && tileId != TileSetConst.TileId_Empty) { //如果是tileVeiw则,上面的tileId是在tileView中的index tileData = tileView.tileSelection.selection_tileData_list[tileId - tileId_offset]; tileId = TileSetUtil.GetTileIdFromTileData(tileData); } Tile tile = tileSet.GetTile(tileId); while (tile != null && tile.uv == default(Rect)) // 忽略无效的tiles(tile.uv == default(Rect),普通的tile是有uv的) { tile = tileSet.GetTile(++tileId); tileData = (uint)tileId; tileId_offset = tileId; } visible_tile_list.Add(tileData); int tile_x = visible_tile_count % sharedTileSetData.tileView_column_count_in_tile_palette; //在tile_palette中的x int tile_y = visible_tile_count / sharedTileSetData.tileView_column_count_in_tile_palette; //在tile_palette中的y //当前tile的rect Rect visual_tile_rect = new Rect(2 + tile_x * (tileSet.visual_tile_size.x + visual_tile_padding), 2 + tile_y * (tileSet.visual_tile_size.y + visual_tile_padding), tileSet.visual_tile_size.x, tileSet.visual_tile_size.y); Rect local_visual_tile_rect = visual_tile_rect; local_visual_tile_rect.position -= sharedTileSetData.tile_palette_scroll_pos;//相对于当前的scroll_rect区域矩形位置 if (local_visual_tile_rect.Overlaps(tile_scroll_size_rect)) { // 画tile (Draw Tile) if (tile == null) { DrawUtil.HandlesDrawSolidRectangleWithOutline(visual_tile_rect, new Color(0f, 0f, 0f, 0.2f), new Color(0f, 0f, 0f, 0.2f)); } else { DrawUtil.HandlesDrawSolidRectangleWithOutline(visual_tile_rect, new Color(0f, 0f, 0f, 0.1f), new Color(0f, 0f, 0f, 0.1f)); TileSetEditor.DoGUIDrawTileFromTileData(visual_tile_rect, tileData, tileSet); } Rect tile_rect = new Rect(2 + tile_x * (tileSet.visual_tile_size.x + visual_tile_padding), 2 + tile_y * (tileSet.visual_tile_size.y + visual_tile_padding), (tileSet.visual_tile_size.x + visual_tile_padding), (tileSet.visual_tile_size.y + visual_tile_padding)); if (visual_tile_rect.Contains(e.mousePosition)) //当前tile的矩形包含鼠标 { if (e.type == EventType.MouseDrag && e.button == 0) //鼠标左键点击并且拖动,设置sharedTileSetData.pointed_tile_index_rect { sharedTileSetData.pointed_tile_index_rect = new KeyValuePair <int, Rect>(visible_tile_count, tile_rect); } else if (e.type == EventType.MouseDown && e.button == 0)//鼠标左键按下,设置sharedTileSetData.start_drag_tile_index_rect,sharedTileSetData.pointed_tile_index_rect, sharedTileSetData.end_drag_tile_index_rect { sharedTileSetData.start_drag_tile_index_rect = sharedTileSetData.pointed_tile_index_rect = sharedTileSetData.end_drag_tile_index_rect = new KeyValuePair <int, Rect>(visible_tile_count, tile_rect); } else if (e.type == EventType.MouseUp && e.button == 0)//放开鼠标左键,设置sharedTileSetData.end_drag_tile_index_rect { sharedTileSetData.end_drag_tile_index_rect = new KeyValuePair <int, Rect>(visible_tile_count, tile_rect); DoSetTileSelection(); } } if ((is_left_mouse_released || is_right_mouse_released) && //放开鼠标左键或者右键 is_mouse_inside_tile_palette_scroll_area && visual_tile_rect.Contains(e.mousePosition) && //鼠标位置处在该tile中 (sharedTileSetData.start_drag_tile_index_rect.Key == sharedTileSetData.end_drag_tile_index_rect.Key) && // 没有拖动 tile_scroll_size_rect.Contains(e.mousePosition - sharedTileSetData.tile_palette_scroll_pos))// and it's inside the scroll area { tileSet.selected_tileId = tileId; //Give focus to SceneView to get key events FocusSceneView(); //放开鼠标右键,打开显示tile的属性面板 if (is_right_mouse_released) { TilePropertiesEditor.Show(tileSet); } } else if (tile != null && tileSet.selected_tileId == tileId)//用黄色外边框选中的tile { DrawUtil.HandlesDrawSolidRectangleWithOutline(tile_rect, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 0f, 1f)); } } ++visible_tile_count; } this.visible_tile_list = visible_tile_list; // 画选中的矩形 用于TileSelection if (sharedTileSetData.start_drag_tile_index_rect.Key != sharedTileSetData.pointed_tile_index_rect.Key) { Rect selection_rect = new Rect(sharedTileSetData.start_drag_tile_index_rect.Value.center, sharedTileSetData.pointed_tile_index_rect.Value.center - sharedTileSetData.start_drag_tile_index_rect.Value.center); selection_rect.Set(Mathf.Min(selection_rect.xMin, selection_rect.xMax), Mathf.Min(selection_rect.yMin, selection_rect.yMax), Mathf.Abs(selection_rect.width), Mathf.Abs(selection_rect.height)); selection_rect.xMin -= sharedTileSetData.start_drag_tile_index_rect.Value.width / 2; selection_rect.xMax += sharedTileSetData.start_drag_tile_index_rect.Value.width / 2; selection_rect.yMin -= sharedTileSetData.start_drag_tile_index_rect.Value.height / 2; selection_rect.yMax += sharedTileSetData.start_drag_tile_index_rect.Value.height / 2; DrawUtil.HandlesDrawSolidRectangleWithOutline(selection_rect, new Color(0f, 0f, 0f, 0.1f), new Color(1f, 1f, 1f, 1f)); } } } if (e.type == EventType.Repaint) { tile_palette_scroll_area_rect = GUILayoutUtility.GetLastRect(); tile_scroll_size_rect = tile_palette_scroll_area_rect; tile_scroll_size_rect.position = Vector2.zero; // reset position to the Contains and Overlaps inside the tile scroll view without repositioning the position of local positions if (tile_area_width > tile_scroll_size_rect.width) { tile_scroll_size_rect.height -= GUI.skin.verticalScrollbar.fixedWidth; } if (tile_area_height > tile_scroll_size_rect.height) { tile_scroll_size_rect.width -= GUI.skin.verticalScrollbar.fixedWidth; } } }