private static void AddButtonToPickerGrid(ref int col, ref int y, string name, int texture, Block.BlockType blockType = 0, LightSourceType? lightSourceType = null) { if (name.StartsWith("Placeholder")) return; Debug.Assert(!(blockType != 0 && lightSourceType.HasValue), "Cannot have block and light source types at the same time."); if (col % GRID_BUTTONS_PER_ROW == 0) { col = 1; y += BUTTON_SIZE; } var button = new Button(ButtonType.GridPicker, Settings.Game.Width / 2 - (GRID_BUTTONS_PER_ROW / 2 * BUTTON_SIZE) + (col * BUTTON_SIZE), y, texture) { BlockType = blockType, LightSourceType = lightSourceType }; _blockPickerGridButtons.Add(button); col++; }
public static void Load() { //load action buttons _actionButtons = new Button[NUMBER_OF_ACTION_BAR_BUTTONS]; for (var i = 0; i <= NUMBER_OF_ACTION_BAR_BUTTONS - 1; i++) { var button = new Button(ButtonType.Action, Settings.Game.Width / 2 - (NUMBER_OF_ACTION_BAR_BUTTONS / 2 * BUTTON_SIZE) + (i * BUTTON_SIZE), Settings.Game.Height - BUTTON_SIZE) { KeyBind = Convert.ToChar((i == 9 ? 0 : i + 1).ToString()) }; switch (i + 1) { case 1: button.Texture = TextureLoader.GetUiTexture(UiTextureType.Shovel); button.BlockType = Block.BlockType.Air; break; case 2: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.Sand); button.BlockType = Block.BlockType.Sand; break; case 3: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.Bricks); button.BlockType = Block.BlockType.Bricks; break; case 4: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.Cobble); button.BlockType = Block.BlockType.Cobble; break; case 5: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.SteelPlate); button.BlockType = Block.BlockType.SteelPlate; break; case 6: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.TreeTrunk); button.BlockType = Block.BlockType.Tree; break; case 7: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.WoodTile1); button.BlockType = Block.BlockType.WoodTile1; break; case 8: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.Grass); button.BlockType = Block.BlockType.Grass; break; case 9: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.Dirt); button.BlockType = Block.BlockType.Dirt; break; case 10: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.Gravel); button.BlockType = Block.BlockType.Gravel; break; } _actionButtons[i] = button; } //load block type picker grid buttons _blockPickerGridButtons = new List<Button>(Enum.GetValues(typeof(Block.BlockType)).Length + Enum.GetValues(typeof(LightSourceType)).Length); var buttonColumn = 1; var buttonY = Settings.Game.Height / 2 - (_blockPickerGridButtons.Capacity / GRID_BUTTONS_PER_ROW * BUTTON_SIZE / 2); foreach (Block.BlockType blockType in Enum.GetValues(typeof(Block.BlockType))) { AddButtonToPickerGrid(ref buttonColumn, ref buttonY, blockType.ToString(), blockType == Block.BlockType.Air ? TextureLoader.GetUiTexture(UiTextureType.Shovel) : TextureLoader.GetBlockTexture(Block.FaceTexture(blockType, Face.Top)), blockType); } foreach (LightSourceType lightSourceType in Enum.GetValues(typeof(LightSourceType))) { AddButtonToPickerGrid(ref buttonColumn, ref buttonY, lightSourceType.ToString(), TextureLoader.GetItemTexture(ItemTextureType.Lantern), lightSourceType: lightSourceType); } //load tool buttons _toolButtons = new Button[Enum.GetValues(typeof(ToolType)).Length]; for (var i = 0; i < _toolButtons.Length; i++) { var button = new Button(ButtonType.Tool, Settings.Game.Width - BUTTON_SIZE, Settings.Game.Height / 2 - _toolButtons.Length * BUTTON_SIZE / 2 + i * BUTTON_SIZE) { ToolType = (ToolType)i }; switch((ToolType)i) { case ToolType.Default: button.Texture = TextureLoader.GetUiTexture(UiTextureType.ToolDefault); break; case ToolType.ToolBlockType: button.Texture = TextureLoader.GetBlockTexture(BlockTextureType.Oil); button.BlockType = Block.BlockType.Oil; break; case ToolType.Cuboid: button.Texture = TextureLoader.GetUiTexture(UiTextureType.ToolCuboid); break; case ToolType.FastBuild: button.Texture = TextureLoader.GetUiTexture(UiTextureType.ToolFastBuild); break; case ToolType.FastDestroy: button.Texture = TextureLoader.GetUiTexture(UiTextureType.ToolFastDestroy); break; case ToolType.Tree: button.Texture = TextureLoader.GetUiTexture(UiTextureType.ToolTree); break; case ToolType.Tower: button.Texture = TextureLoader.GetUiTexture(UiTextureType.Tower); break; case ToolType.SmallKeep: button.Texture = TextureLoader.GetUiTexture(UiTextureType.SmallKeep); break; case ToolType.LargeKeep: button.Texture = TextureLoader.GetUiTexture(UiTextureType.LargeKeep); break; } _toolButtons[i] = button; } _currentToolButton = _currentToolButton == null ? _toolButtons[0] : _toolButtons[(int)CurrentTool]; //allows retention of selected tool when reloading, for example when the window is resized _currentToolButton.Hightlight = true; }
/// <summary>Select a new tool or reset to the default.</summary> internal static void SelectTool(ToolType toolType) { _currentToolButton.Hightlight = false; _previousToolButton = _currentToolButton; _currentToolButton = _toolButtons[(int)toolType]; _currentToolButton.Hightlight = true; }
public static void LeftMouseDown(int x, int y) { foreach (var button in _actionButtons) { if (button.ContainsCoords(x, y)) //clicked action bar button { if (BlockPickerGridVisible) //picker already open { _currentActionButton.Hightlight = false; //unhighlight current action button if(button == _currentActionButton) { //reclicked currently selected button, close picker BlockPickerGridVisible = false; return; } } BlockPickerGridVisible = true; //open block picker button.Hightlight = true; //highlight selected button _currentActionButton = button; //store the button that was clicked so the block type can be set once picked _cancelMouseUp = true; //dont run mouseup for this click to prevent accidentally using current tool "through" the button that was clicked return; } } if (BlockPickerGridVisible) { foreach (var button in _blockPickerGridButtons) { if (button.ContainsCoords(x, y)) //block picked from grid { _currentActionButton.Texture = button.Texture; _currentActionButton.BlockType = button.BlockType; _currentActionButton.LightSourceType = button.LightSourceType; if (_currentActionButton.ToolType == ToolType.ToolBlockType) { //selected block type for right bar block type; re-select previous tool _currentToolButton = _previousToolButton; _currentToolButton.Hightlight = true; _previousToolButton = null; } BlockPickerGridVisible = false; _cancelMouseUp = true; _currentActionButton = null; return; } } } if (Config.CreativeMode) { foreach (var button in _toolButtons) { if (button.ContainsCoords(x, y)) //tool button clicked { SelectTool(button.ToolType); _cancelMouseUp = true; if (button.ToolType == ToolType.ToolBlockType) { BlockPickerGridVisible = !BlockPickerGridVisible; //open/close block picker _currentActionButton = button; //store the ToolBlockType button as the current action button so the block type can be set once picked } else //close the block picker grid in case it was open for the ToolBlockType tool { BlockPickerGridVisible = false; } return; } } switch (CurrentTool) { case ToolType.Cuboid: _leftMouseDownPosition = BlockCursorHost.PositionAdd; break; case ToolType.FastBuild: case ToolType.FastDestroy: //perform one action as soon as clicked then start timer to continue if mouse held down _fastToolTimer_Elapsed(null, null); FastToolTimer.Start(); break; } } }